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