1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/* compile with gcc -Wall -o sample-photobooth -lgphoto2 sample-photobooth.c
*
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <gphoto2/gphoto2.h>
#include "samples.h"
#define CONFIG_FILE "config.txt"
#define PREVIEW "preview.jpg"
static void errordumper(GPLogLevel level, const char *domain, const char *str,
void *data) {
/* printf("%s (data %p)\n", str,data);*/
}
/* set by signal handler */
static int capture_now = 0;
static int read_config = 0;
static int
capture_to_file(Camera *camera, GPContext *context, char *fn) {
int fd, retval;
CameraFile *file;
CameraFilePath camera_file_path;
char *s, *t;
retval = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
if (retval < GP_OK) return retval;
s = strrchr (fn, '.');
t = strrchr (camera_file_path.name, '.');
/* replace the suffix by the one sent by the camera .. for RAW capture */
if (t && s && (strlen(t+1) == 3) && (strlen(s+1) == 3)) {
strcpy (s+1, t+1);
}
fd = open(fn, O_CREAT | O_WRONLY, 0644);
if (fd == -1)
return GP_ERROR;
retval = gp_file_new_from_fd(&file, fd);
if (retval < GP_OK) {
close(fd);
unlink(fn);
return retval;
}
retval = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name,
GP_FILE_TYPE_NORMAL, file, context);
if (retval < GP_OK) {
gp_file_free(file);
unlink(fn);
return retval;
}
retval = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context);
gp_file_free(file);
return GP_OK;
}
static void
sig_handler_capture_now (int sig_num)
{
signal (SIGUSR1, sig_handler_capture_now);
capture_now = 1;
}
static void
sig_handler_read_config (int sig_num)
{
signal (SIGUSR2, sig_handler_read_config);
read_config = 1;
}
int
main(int argc, char **argv) {
Camera *camera;
int retval;
int capturecnt = 0;
GPContext *context = sample_create_context();
printf("Sample photobooth.\n");
printf("Continously stores previews in 'preview.jpg'.\n");
printf("kill -USR1 %d to take a capture.\n", getpid());
printf("kill -USR2 %d to read the 'config.txt'.\n", getpid());
printf("kill -TERM %d to finish.\n", getpid());
signal (SIGUSR1, sig_handler_capture_now);
signal (SIGUSR2, sig_handler_read_config);
gp_log_add_func(GP_LOG_ERROR, errordumper, 0);
gp_camera_new(&camera);
retval = gp_camera_init(camera, context);
if (retval != GP_OK) {
printf(" Retval: %d\n", retval);
exit (1);
}
while (1) {
CameraFile *file;
char output_file[32];
/*
* Capture a full picture on demand. Use unique filenames.
*/
if (capture_now) {
capture_now = 0;
sprintf(output_file, "image-%04d.jpg", capturecnt++);
retval = capture_to_file(camera, context, output_file);
if (retval == GP_OK)
fprintf (stderr, "captured to %s\n", output_file);
}
/*
* Read configuration changes from "config.txt".
* Expected are key=value pairs where value is the value seen in "--get-config key", e.g.
* iso=Auto
* iso=200
* shutterspeed=1/200
*/
if (read_config) {
FILE *config;
char buf[512];
read_config = 0;
config = fopen (CONFIG_FILE, "r");
while (fgets (buf, sizeof(buf), config)) {
char *s;
/* kill linefeeds */
s = strchr(buf,'\r');
if (s)
*s=0;
s = strchr(buf,'\n');
if (s)
*s=0;
s = strchr(buf,'=');
if (!s) continue;
*s=0;
retval = set_config_value_string (camera, buf, s+1, context);
if (retval < GP_OK)
fprintf (stderr, "setting configuration '%s' to '%s' failed with %d.\n", buf, s+1, retval);
else
fprintf (stderr, "changed configuration '%s' to '%s'\n", buf, s+1);
}
fclose (config);
}
/*
* Capture a preview on every loop. Save as preview.jpg.
*/
retval = gp_file_new(&file);
if (retval != GP_OK) {
fprintf(stderr,"gp_file_new: %d\n", retval);
exit(1);
}
retval = gp_camera_capture_preview(camera, file, context);
if (retval != GP_OK) {
fprintf(stderr,"gp_camera_capture_preview failed: %d\n", retval);
exit(1);
}
retval = gp_file_save(file, PREVIEW);
if (retval != GP_OK) {
fprintf(stderr,"saving preview failed: %d\n", retval);
exit(1);
}
gp_file_unref(file);
/*
* Check and drain the event queue.
* Download newly captured images.
* Ignore the rest events.
*/
while (1) {
CameraEventType evttype;
CameraFilePath *path;
void *evtdata;
int fd;
evtdata = NULL;
retval = gp_camera_wait_for_event (camera, 1, &evttype, &evtdata, context);
if (retval != GP_OK) break;
switch (evttype) {
case GP_EVENT_FILE_ADDED: {
char *t;
path = (CameraFilePath*)evtdata;
t = strrchr (path->name, '.');
if (t && (strlen(t+1) == 3)) {
sprintf(output_file, "image-%04d.%s", capturecnt++, t+1);
} else {
sprintf(output_file, "image-%04d.jpg", capturecnt++);
}
fd = open(output_file, O_CREAT | O_WRONLY, 0644);
retval = gp_file_new_from_fd(&file, fd);
retval = gp_camera_file_get(camera, path->folder, path->name,
GP_FILE_TYPE_NORMAL, file, context);
retval = gp_camera_file_delete(camera, path->folder, path->name, context);
gp_file_free(file);
free (evtdata);
fprintf (stderr, "saved to %s\n", output_file);
break;
}
case GP_EVENT_FOLDER_ADDED:
path = (CameraFilePath*)evtdata;
printf("Folder added on camera: %s / %s\n", path->folder, path->name);
free (evtdata);
break;
case GP_EVENT_CAPTURE_COMPLETE:
printf("Capture Complete.\n");
break;
case GP_EVENT_TIMEOUT:
break;
case GP_EVENT_UNKNOWN:
if (evtdata) {
printf("Unknown event: %s.\n", (char*)evtdata);
free (evtdata);
} else {
printf("Unknown event.\n");
}
break;
default:
printf("Type %d?\n", evttype);
break;
}
if (evttype == GP_EVENT_TIMEOUT)
break;
}
}
gp_camera_exit(camera, context);
return 0;
}
|