git.jasLogic.tech
- better error handling and minor changes in photobox.c
[photobox.git] / src / photobox_photo.c
1 // Copyright (C) 2019 Jaslo Ziska
2
3 #include "photobox_photo.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include <gphoto2/gphoto2.h>
12 #include <gphoto2/gphoto2-camera.h>
13
14 static Camera *camera;
15 static GPContext *context;
16
17 static void dumperror(GPLogLevel level, const char *domain, const char *str, void *data)
18 {
19 // suppress unused parameter errors (ugly):
20 (void) level;
21 (void) data;
22
23 fprintf(stderr, "[Error Log] %s: %s\n", domain, str);
24 }
25
26 int pb_ph_init(void)
27 {
28 int ret;
29
30
31 ret = gp_log_add_func(GP_LOG_ERROR, (GPLogFunc) dumperror, 0);
32 if (ret < GP_OK) {
33 perror("Failed to add logging function");
34 return ret;
35 }
36
37
38 context = gp_context_new();
39
40 ret = gp_camera_new(&camera);
41 if (ret != GP_OK)
42 goto error;
43
44 ret = gp_camera_init(camera, context);
45 if (ret != GP_OK)
46 goto error;
47
48 return 0;
49
50 error:
51 gp_context_unref(context);
52 return ret;
53 }
54 void pb_ph_uninit(void)
55 {
56 gp_camera_exit(camera, context);
57 gp_context_unref(context);
58 }
59
60 // unused, unmaintained:
61 /*
62 int pb_ph_capture(pb_ph_buffer *buf)
63 {
64 int ret;
65 CameraFile *file;
66 CameraFilePath camera_file_path;
67
68 ret = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
69 if (ret != GP_OK) {
70 perror_inf("Failed to capture image");
71 return ret;
72 }
73
74 ret = gp_file_new(&file);
75 if (ret != 0) {
76 perror_inf("Failed to create new file");
77 return ret;
78 }
79
80 ret = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name, GP_FILE_TYPE_NORMAL,
81 file, context);
82 if (ret != 0) {
83 perror_inf("Failed to get image");
84 return ret;
85 }
86
87 ret = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context);
88 if (ret != 0) {
89 perror_inf("Failed to delete image from camera");
90 return ret;
91 }
92
93 gp_file_get_data_and_size(file, (const char **)&buf->base, &buf->size);
94 if (ret != 0) {
95 perror_inf("Failed to get data and size");
96 return ret;
97 }
98
99 return 0;
100 }
101 */
102
103 int pb_ph_capture_file(const char *fn)
104 {
105 int ret, fd;
106 CameraFile *file;
107 CameraFilePath camera_file_path;
108
109 ret = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
110 if (ret != GP_OK)
111 return ret;
112
113 fd = open(fn, O_CREAT | O_WRONLY, 0644);
114 if (fd == -1) {
115 ret = GP_ERROR;
116 perror("Failed to open/create file");
117 goto error_delete;
118 }
119
120 ret = gp_file_new_from_fd(&file, fd);
121 if (ret != GP_OK)
122 goto error_fd;
123
124 ret = gp_camera_file_get(camera, camera_file_path.folder,
125 camera_file_path.name, GP_FILE_TYPE_NORMAL, file, context);
126
127 gp_file_free(file);
128
129 error_fd:
130 // close fd? done in gphoto?
131 close(fd);
132
133 error_delete:
134 ret = gp_camera_file_delete(camera, camera_file_path.folder,
135 camera_file_path.name, context);
136
137 return ret;
138 }