Source code upload
[framework/connectivity/libgphoto2.git] / examples / lunkwill-canon-capture.c
1 /* compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
2  * This code released into the public domain 21 July 2008
3  * 
4  * This program does the equivalent of:
5  * gphoto2 --shell
6  *   > set-config capture=1
7  *   > capture-image-and-download
8  * compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
9  *
10  * Taken from: http://credentiality2.blogspot.com/2008/07/linux-libgphoto2-image-capture-from.html 
11  */
12
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <string.h>
22 #include <gphoto2/gphoto2.h>
23
24 #include "samples.h"
25
26 struct timeval starttime;
27
28 static void errordumper(GPLogLevel level, const char *domain, const char *format,
29                  va_list args, void *data) {
30         struct timeval tv;
31
32         gettimeofday(&tv,NULL);
33         tv.tv_sec -= starttime.tv_sec;
34         tv.tv_usec -= starttime.tv_usec;
35         if (tv.tv_usec <0) {
36                 tv.tv_usec += 1000000;
37                 tv.tv_sec--;
38         }
39         fprintf(stdout,"%d.%06d: ", (int)tv.tv_sec, (int)tv.tv_usec);
40         vfprintf(stdout, format, args);
41         fprintf(stdout, "\n");
42         fflush (stdout);
43 }
44
45 /* This seems to have no effect on where images go
46 void set_capturetarget(Camera *canon, GPContext *canoncontext) {
47         int retval;
48         printf("Get root config.\n");
49         CameraWidget *rootconfig; // okay, not really
50         CameraWidget *actualrootconfig;
51
52         retval = gp_camera_get_config(canon, &rootconfig, canoncontext);
53         actualrootconfig = rootconfig;
54         printf("  Retval: %d\n", retval);
55
56         printf("Get main config.\n");
57         CameraWidget *child;
58         retval = gp_widget_get_child_by_name(rootconfig, "main", &child);
59         printf("  Retval: %d\n", retval);
60
61         printf("Get settings config.\n");
62         rootconfig = child;
63         retval = gp_widget_get_child_by_name(rootconfig, "settings", &child);
64         printf("  Retval: %d\n", retval);
65
66         printf("Get capturetarget.\n");
67         rootconfig = child;
68         retval = gp_widget_get_child_by_name(rootconfig, "capturetarget", &child);
69         printf("  Retval: %d\n", retval);
70
71
72         CameraWidget *capture = child;
73
74         const char *widgetinfo;
75         gp_widget_get_name(capture, &widgetinfo);
76         printf("config name: %s\n", widgetinfo );
77
78         const char *widgetlabel;
79         gp_widget_get_label(capture, &widgetlabel);
80         printf("config label: %s\n", widgetlabel);
81
82         int widgetid;
83         gp_widget_get_id(capture, &widgetid);
84         printf("config id: %d\n", widgetid);
85
86         CameraWidgetType widgettype;
87         gp_widget_get_type(capture, &widgettype);
88         printf("config type: %d == %d \n", widgettype, GP_WIDGET_RADIO);
89
90
91         printf("Set value.\n");
92
93         // capture to ram should be 0, although I think the filename also plays into
94         // it
95         int one=1;
96         retval = gp_widget_set_value(capture, &one);
97         printf("  Retval: %d\n", retval);
98
99         printf("Enabling capture to CF.\n");
100         retval = gp_camera_set_config(canon, actualrootconfig, canoncontext);
101         printf("  Retval: %d\n", retval);
102 }
103 */
104
105 static void
106 capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) {
107         int fd, retval;
108         CameraFile *canonfile;
109         CameraFilePath camera_file_path;
110
111         printf("Capturing.\n");
112
113         /* NOP: This gets overridden in the library to /capt0000.jpg */
114         strcpy(camera_file_path.folder, "/");
115         strcpy(camera_file_path.name, "foo.jpg");
116
117         retval = gp_camera_capture(canon, GP_CAPTURE_IMAGE, &camera_file_path, canoncontext);
118         printf("  Retval: %d\n", retval);
119
120         printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);
121
122         fd = open(fn, O_CREAT | O_WRONLY, 0644);
123         retval = gp_file_new_from_fd(&canonfile, fd);
124         printf("  Retval: %d\n", retval);
125         retval = gp_camera_file_get(canon, camera_file_path.folder, camera_file_path.name,
126                      GP_FILE_TYPE_NORMAL, canonfile, canoncontext);
127         printf("  Retval: %d\n", retval);
128
129         printf("Deleting.\n");
130         retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name,
131                         canoncontext);
132         printf("  Retval: %d\n", retval);
133
134         gp_file_free(canonfile);
135 }
136
137 int
138 main(int argc, char **argv) {
139         Camera  *canon;
140         int     retval;
141         GPContext *canoncontext = sample_create_context();
142
143         gettimeofday(&starttime,NULL);
144         gp_log_add_func(GP_LOG_ERROR, errordumper, NULL);
145         gp_camera_new(&canon);
146
147         /* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the
148          * init function seems to traverse the entire filesystem on the camera.  This
149          * is partly why it takes so long.
150          * (Marcus: the ptp2 driver does this by default currently.)
151          */
152         printf("Camera init.  Takes about 10 seconds.\n");
153         retval = gp_camera_init(canon, canoncontext);
154         if (retval != GP_OK) {
155                 printf("  Retval: %d\n", retval);
156                 exit (1);
157         }
158         canon_enable_capture(canon, TRUE, canoncontext);
159         /*set_capturetarget(canon, canoncontext);*/
160         capture_to_file(canon, canoncontext, "foo.jpg");
161         gp_camera_exit(canon, canoncontext);
162         return 0;
163 }