summaryrefslogtreecommitdiff
path: root/examples/lunkwill-canon-capture.c
blob: 7c908809b0e1159da4cc9cc2fb87572cd1c5b880 (plain)
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
/* compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
 * This code released into the public domain 21 July 2008
 * 
 * This program does the equivalent of:
 * gphoto2 --shell
 *   > set-config capture=1
 *   > capture-image-and-download
 * compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
 *
 * Taken from: http://credentiality2.blogspot.com/2008/07/linux-libgphoto2-image-capture-from.html 
 */

#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <gphoto2/gphoto2.h>

static void errordumper(GPLogLevel level, const char *domain, const char *format,
                 va_list args, void *data) {


  vfprintf(stdout, format, args);
  fprintf(stdout, "\n");
}

static void enable_capture(Camera *canon, GPContext *canoncontext) {
	int retval;
	CameraWidget *rootconfig; /* okay, not really */
	CameraWidget *actualrootconfig;
	CameraWidget *child;
	CameraWidget *capture = child;
	const char *widgetinfo;
	const char *widgetlabel;
	int widgetid;
	CameraWidgetType widgettype;
	const int one=1;

	printf("Get root config.\n");

	retval = gp_camera_get_config(canon, &rootconfig, canoncontext);
	actualrootconfig = rootconfig;
	printf("  Retval: %d\n", retval);

	printf("Get main config.\n");
	retval = gp_widget_get_child_by_name(rootconfig, "main", &child);
	printf("  Retval: %d\n", retval);

	printf("Get settings config.\n");
	rootconfig = child;
	retval = gp_widget_get_child_by_name(rootconfig, "settings", &child);
	printf("  Retval: %d\n", retval);

	printf("Get capture config.\n");
	rootconfig = child;
	retval = gp_widget_get_child_by_name(rootconfig, "capture", &child);
	printf("  Retval: %d\n", retval);


	gp_widget_get_name(capture, &widgetinfo);
	printf("config name: %s\n", widgetinfo );

	gp_widget_get_label(capture, &widgetlabel);
	printf("config label: %s\n", widgetlabel);

	gp_widget_get_id(capture, &widgetid);
	printf("config id: %d\n", widgetid);

	gp_widget_get_type(capture, &widgettype);
	printf("config type: %d == %d \n", widgettype, GP_WIDGET_TOGGLE);

	printf("Set value.\n");

	retval = gp_widget_set_value(capture, &one);
	printf("  Retval: %d\n", retval);

	printf("Enabling capture.\n");
	retval = gp_camera_set_config(canon, actualrootconfig, canoncontext);
	printf("  Retval: %d\n", retval);
}

/* This seems to have no effect on where images go
void set_capturetarget(Camera *canon, GPContext *canoncontext) {
	int retval;
	printf("Get root config.\n");
	CameraWidget *rootconfig; // okay, not really
	CameraWidget *actualrootconfig;

	retval = gp_camera_get_config(canon, &rootconfig, canoncontext);
	actualrootconfig = rootconfig;
	printf("  Retval: %d\n", retval);

	printf("Get main config.\n");
	CameraWidget *child;
	retval = gp_widget_get_child_by_name(rootconfig, "main", &child);
	printf("  Retval: %d\n", retval);

	printf("Get settings config.\n");
	rootconfig = child;
	retval = gp_widget_get_child_by_name(rootconfig, "settings", &child);
	printf("  Retval: %d\n", retval);

	printf("Get capturetarget.\n");
	rootconfig = child;
	retval = gp_widget_get_child_by_name(rootconfig, "capturetarget", &child);
	printf("  Retval: %d\n", retval);


	CameraWidget *capture = child;

	const char *widgetinfo;
	gp_widget_get_name(capture, &widgetinfo);
	printf("config name: %s\n", widgetinfo );

	const char *widgetlabel;
	gp_widget_get_label(capture, &widgetlabel);
	printf("config label: %s\n", widgetlabel);

	int widgetid;
	gp_widget_get_id(capture, &widgetid);
	printf("config id: %d\n", widgetid);

	CameraWidgetType widgettype;
	gp_widget_get_type(capture, &widgettype);
	printf("config type: %d == %d \n", widgettype, GP_WIDGET_RADIO);


	printf("Set value.\n");

	// capture to ram should be 0, although I think the filename also plays into
	// it
	int one=1;
	retval = gp_widget_set_value(capture, &one);
	printf("  Retval: %d\n", retval);

	printf("Enabling capture to CF.\n");
	retval = gp_camera_set_config(canon, actualrootconfig, canoncontext);
	printf("  Retval: %d\n", retval);
}
*/

static void
capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) {
	int retval;
	CameraFile *canonfile;
	const char *filedata;
	unsigned long int filesize;

	printf("Capturing.\n");
	CameraFilePath camera_file_path;

	/* NOP: This gets overridden in the library to /capt0000.jpg */
	strcpy(camera_file_path.folder, "/");
	strcpy(camera_file_path.name, "foo.jpg");

	retval = gp_camera_capture(canon, GP_CAPTURE_IMAGE, &camera_file_path, canoncontext);
	printf("  Retval: %d\n", retval);

	printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);

	retval = gp_file_new(&canonfile);
	printf("  Retval: %d\n", retval);
	retval = gp_camera_file_get(canon, camera_file_path.folder, camera_file_path.name,
		     GP_FILE_TYPE_NORMAL, canonfile, canoncontext);
	printf("  Retval: %d\n", retval);

	retval = gp_file_get_data_and_size(canonfile, &filedata, &filesize);
	printf("  Retval: %d\n", retval);

	int fd = open(fn, O_CREAT | O_WRONLY, 0644);
	write(fd, filedata, filesize);
	close(fd);

	printf("Deleting.\n");
	retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name,
			canoncontext);
	printf("  Retval: %d\n", retval);

	gp_file_free(canonfile);
}

int
main(int argc, char **argv) {
	Camera *canon;
	int retval;

	gp_log_add_func(GP_LOG_ERROR, errordumper, NULL);
	GPContext *canoncontext = gp_context_new();
	gp_camera_new(&canon);

	/* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the
	 * init function seems to traverse the entire filesystem on the camera.  This
	 * is partly why it takes so long.
	 * (Marcus: the ptp2 driver does this by default currently.)
	 */
	printf("Camera init.  Takes about 10 seconds.\n");
	retval = gp_camera_init(canon, canoncontext);
	printf("  Retval: %d\n", retval);

	enable_capture(canon, canoncontext);
	/*set_capturetarget(canon, canoncontext);*/
	capture_to_file(canon, canoncontext, "foo.jpg");
	gp_camera_exit(canon, canoncontext);
	return 0;
}