aboutsummaryrefslogtreecommitdiff
path: root/gphoto/gphoto.c
blob: 94179b2e27783876d2d8e81ac57d268a90e0453e (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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* Gphoto PD External */
/* Copyright Ben Bogart, 2009 */
/* This program is distributed under the params of the GNU Public License */

///////////////////////////////////////////////////////////////////////////////////
/* This file is part of the Gphoto PD External.                                 */
/*                                                                               */
/* Gphoto PD External is free software; you can redistribute it and/or modify   */
/* it under the terms of the GNU General Public License as published by          */
/* the Free Software Foundation; either version 2 of the License, or             */
/* (at your option) any later version.                                           */
/*                                                                               */
/* The Gphoto PD External is distributed in the hope that they will be useful,  */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of                */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 */
/* GNU General Public License for more details.                                  */
/*                                                                               */
/* You should have received a copy of the GNU General Public License             */
/* along with the Chaos PD Externals; if not, write to the Free Software         */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA     */
///////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <m_pd.h>
#include <fcntl.h>
#include <pthread.h>
#include <gphoto2/gphoto2-camera.h>

t_class *gphoto_class;

typedef struct gphoto_struct {
	t_object x_obj;
	t_outlet *connectedOutlet;
	Camera *camera;
	int connected;

} gphoto_struct;

// Struct to store A_GIMME data passed to thread.
typedef struct gphoto_gimme_struct {
	gphoto_struct *gphoto;
	t_symbol *s;
	int argc;
	t_atom *argv;
} gphoto_gimme_struct;

// Open connection to camera, do autodetection and initialization.
void *openCam(void *gphoto) {
	int gp_ret;

	gp_ret = gp_camera_new (&((gphoto_struct *)gphoto)->camera);
	if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref( ((gphoto_struct *)gphoto)->camera ); return(NULL);}
	if (gp_ret == -105) {error("gphoto: Are you sure the camera is supported, connected and powered on?"); gp_camera_unref( ((gphoto_struct *)gphoto)->camera); return(NULL);}
	post("gphoto: Autodetecting Camera.");

	// INIT camera (without context)	
	gp_ret = gp_camera_init (((gphoto_struct *)gphoto)->camera, NULL);
	if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref( ((gphoto_struct *)gphoto)->camera); return(NULL);}
	if (gp_ret == -105) {error("gphoto: Are you sure the camera is supported, connected and powered on?"); gp_camera_unref( ((gphoto_struct *)gphoto)->camera); return(NULL);}
	post("gphoto: Connected to Camera.");

	// Send state out 2nd outlet.
	sys_lock();
	outlet_float(((gphoto_struct *)gphoto)->connectedOutlet, 1);
	sys_unlock();

	// Set to connected state.
	((gphoto_struct *)gphoto)->connected = 1;
	
	return(NULL);	
}

// Wrap Open
static void wrapOpen(gphoto_struct *gphoto) {
	int ret;
	pthread_t thread1;

	// Create thread and pass reference to object struct
	ret = pthread_create( &thread1, NULL, openCam, gphoto);

	return;
}

// Close connection to camera (unref)
void *closeCam(void *gphoto) {
	int gp_ret;

	gp_camera_free(((gphoto_struct *)gphoto)->camera);
	post("camera free");

	// Send state out 2nd outlet.
	sys_lock();
	outlet_float(((gphoto_struct *)gphoto)->connectedOutlet, 0);
	sys_unlock();

	// Set to connected state.
	((gphoto_struct *)gphoto)->connected = 0;
	
	return(NULL);	
}

// Wrap Open
static void wrapClose(gphoto_struct *gphoto) {
	int ret;
	pthread_t thread1;

	// Create thread and pass reference to object struct
	ret = pthread_create( &thread1, NULL, closeCam, gphoto);

	return;
}

// TODO use listConfig to make a condigDetails function to get range and step data for cam params.

// list configuration
void *listConfig(void *threadArgs) {
	int gp_ret, numsections, numchildren, i, j;
	CameraWidget *config = NULL;
	CameraWidget *child = NULL;
	CameraWidget *child2 = NULL;
	CameraWidgetType type;
	const char *childName;

	gp_ret = gp_camera_get_config (((gphoto_gimme_struct *)threadArgs)->gphoto->camera, &config, NULL); // get config from camera
	if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}

	numsections = gp_widget_count_children(config);
	for (i=0; i<numsections; i++) {
		gp_widget_get_child (config, i, &child);
		gp_widget_get_type (child, &type);
	
		if (type == GP_WIDGET_SECTION) {
			//post("gphoto: Config Section: %s\n", childName);
			numchildren = gp_widget_count_children(child);
			for (j=0; j<numchildren; j++) {
				gp_widget_get_child (child, j, &child2);
				gp_widget_get_name (child2, &childName);
				gp_widget_get_type (child2, &type);	
				
				sys_lock();
				outlet_symbol(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet, gensym(childName));
				sys_unlock();

				//post("gphoto: Config Child: %s\n", childName); // send through outlet?
			}
		}
	}

	// Free memory (not child?)
	gp_widget_unref (config);

	// Send bang out 1st outlet when operation is done.
	sys_lock();
	outlet_bang(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet);
	sys_unlock();	

	return(NULL);
}

// Wrap listConfig
static void wrapListConfig(gphoto_struct *gphoto) {
	int ret;
	pthread_t thread1;

	if (gphoto->connected) {

		// instance of structure
		gphoto_gimme_struct *threadArgs = (gphoto_gimme_struct *)malloc(sizeof(gphoto_gimme_struct));

		// packaging arguments into structure
		threadArgs->gphoto = gphoto;

		// Create thread
		ret = pthread_create( &thread1, NULL, listConfig, threadArgs);
	} else {
		error("gphoto: ERROR: Not connected.");
	}

	return;
}

// Get configuration (flags) from camera
void *getConfig(void *threadArgs) {
	int gp_ret;
	const char *textVal;
	const int *toggleVal;
	const float *rangeVal;
	float value;

	t_symbol *key;

	CameraWidget *config = NULL;
	CameraWidget *child = NULL;
	CameraWidgetType type;

	key = atom_getsymbol( ((gphoto_gimme_struct *)threadArgs)->argv ); // config key

	gp_ret = gp_camera_get_config (((gphoto_gimme_struct *)threadArgs)->gphoto->camera, &config, NULL); // get config from camera
	if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}

	gp_ret = gp_widget_get_child_by_name (config, key->s_name, &child); // get item from config
	if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));}

/*		post("types:");
	post("GP_WIDGET_TOGGLE: %d",   GP_WIDGET_TOGGLE);
	post("GP_WIDGET_TEXT: %d",   GP_WIDGET_TEXT);
	post("GP_WIDGET_RANGE: %d",   GP_WIDGET_RANGE);
	post("GP_WIDGET_RADIO: %d",   GP_WIDGET_RADIO);
	post("GP_WIDGET_MENU: %d",   GP_WIDGET_MENU);
	post("GP_WIDGET_BUTTON: %d",   GP_WIDGET_BUTTON);
	post("GP_WIDGET_DATE: %d",   GP_WIDGET_DATE);
	post("GP_WIDGET_WINDOW: %d",   GP_WIDGET_WINDOW);
	post("GP_WIDGET_SECTION: %d",   GP_WIDGET_SECTION);
*/

	gp_ret = gp_widget_get_type (child, &type);
	if (gp_ret != 0) {
		error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));
		error("gphoto: Invalid config key.");
	} else {
		switch (type) {
			case GP_WIDGET_TOGGLE:
				gp_ret = gp_widget_get_value (child, &toggleVal); //  get widget value
				outlet_float(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet, (int) toggleVal);
				break;
			case GP_WIDGET_TEXT:
				gp_ret = gp_widget_get_value (child, &textVal);
				outlet_symbol(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet, gensym(textVal));
				break;
			case GP_WIDGET_RANGE:
				gp_ret = gp_widget_get_value (child, &rangeVal);
				if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));}
				outlet_float(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet, (float) *rangeVal);
				break;
		}
	}

	// Free memory (not child?)
	gp_widget_unref (config);

	// Send bang out 1st outlet when operation is done.
	sys_lock();
	outlet_bang(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet);
	sys_unlock();	

	return(NULL);
}

// Wrap getConfig
static void wrapGetConfig(gphoto_struct *gphoto, t_symbol *s, int argc, t_atom *argv) {
	int ret;
	pthread_t thread1;

	if (gphoto->connected) {

		// instance of structure
		gphoto_gimme_struct *threadArgs = (gphoto_gimme_struct *)malloc(sizeof(gphoto_gimme_struct));

		// packaging arguments into structure
		threadArgs->gphoto = gphoto;
		threadArgs->s = s;
		threadArgs->argc = argc;
		threadArgs->argv = malloc(sizeof(*argv)*argc);		// allocate new memory space for arguments.
		memcpy(threadArgs->argv, argv, sizeof(*argv)*argc);	// copy the arguments into new space.

		// Create thread
		ret = pthread_create( &thread1, NULL, getConfig, threadArgs);
	} else {
		error("gphoto: ERROR: Not connected.");
	}

	return;
}

void *setConfig(void *threadArgs) {
	int gp_ret;
	float floatValue;
	t_symbol *key;
	t_int *intValue;
	CameraWidget *config = NULL;
	CameraWidget *child = NULL;
	CameraWidgetType type;

	sys_lock();
	key = atom_getsymbol( ((gphoto_gimme_struct *)threadArgs)->argv ); // config key
	sys_unlock();

	gp_ret = gp_camera_get_config (((gphoto_gimme_struct *)threadArgs)->gphoto->camera, &config, NULL); // get config from camera
	if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}

	gp_ret = gp_widget_get_child_by_name (config, key->s_name, &child); // get item from config
	if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}

	gp_ret = gp_widget_get_type (child, &type);
	if (gp_ret != 0) {
		error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));
		error("gphoto: Invalid config key.");
	} else {
		switch (type) {
			case GP_WIDGET_TOGGLE:
				sys_lock();
				intValue = atom_getint( ((gphoto_gimme_struct *)threadArgs)->argv+1);
				sys_unlock();

				gp_ret = gp_widget_set_value (child, &intValue); //  set widget value
				if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));}

				gp_ret = gp_camera_set_config (((gphoto_gimme_struct *)threadArgs)->gphoto->camera, config, NULL); // set new config
				if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));}
				break;

			case GP_WIDGET_RANGE:
				floatValue = atom_getfloat( ((gphoto_gimme_struct *)threadArgs)->argv+1 );

				gp_ret = gp_widget_set_value (child, &floatValue); //  set widget value
				if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));}

				gp_ret = gp_camera_set_config (((gphoto_gimme_struct *)threadArgs)->gphoto->camera, config, NULL); // set new config
				if (gp_ret != 0) {error("gphoto: ERROR: %s\n", gp_result_as_string(gp_ret));}

				break;
		}
	}

	// Free memory
	gp_widget_unref (config);
	
	// Send bang out 1st outlet when operation is done.
	sys_lock();
	outlet_bang(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet);
	sys_unlock();

	return(NULL);
}

// Wrap setConfig
// TODO is there a way to have one wrapper for all funcs?
static void wrapSetConfig(gphoto_struct *gphoto,  t_symbol *s, int argc, t_atom *argv) {
	int ret;
	pthread_t thread1;

	if (gphoto->connected) {

		// instance of structure
		gphoto_gimme_struct *threadArgs = (gphoto_gimme_struct *)malloc(sizeof(gphoto_gimme_struct));

		// packaging arguments into structure
		threadArgs->gphoto = gphoto;
		threadArgs->s = s;
		threadArgs->argc = argc;
		threadArgs->argv = malloc(sizeof(*argv)*argc);		// allocate new memory space for arguments.
		memcpy(threadArgs->argv, argv, sizeof(*argv)*argc);	// copy the arguments into new space.

		// Create thread
		ret = pthread_create( &thread1, NULL, setConfig, threadArgs);
	} else {
		error("gphoto: ERROR: Not connected.");
	}

	return;
}

void *captureImage(void *threadArgs) {
	int gp_ret, fd;
	CameraFile *camerafile;
	CameraFilePath camera_file_path;
	t_symbol *filename;
	
	filename = atom_getsymbol( ((gphoto_gimme_struct *)threadArgs)->argv ); // destination filename

	gp_ret = gp_camera_capture(((gphoto_gimme_struct *)threadArgs)->gphoto->camera, GP_CAPTURE_IMAGE, &camera_file_path, NULL); 
	if (gp_ret != 0) {error("3gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}

	fd = open( filename->s_name, O_CREAT | O_WRONLY, 0644); // create file descriptor

	gp_ret = gp_file_new_from_fd(&camerafile, fd); // create gphoto file from descriptor
	if (gp_ret != 0) {error("4gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}

	gp_ret = gp_camera_file_get(((gphoto_gimme_struct *)threadArgs)->gphoto->camera, camera_file_path.folder, camera_file_path.name,
		     GP_FILE_TYPE_NORMAL, camerafile, NULL); // get file from camera
	if (gp_ret != 0) {error("5gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}
	
	gp_ret = gp_camera_file_delete(((gphoto_gimme_struct *)threadArgs)->gphoto->camera, camera_file_path.folder, camera_file_path.name, NULL);
	if (gp_ret != 0) {error("6gphoto: ERROR: %s\n", gp_result_as_string(gp_ret)); gp_camera_unref(((gphoto_gimme_struct *)threadArgs)->gphoto->camera); return(NULL);}

	// Send bang out 2nd outlet when operation is done.
	sys_lock();
	outlet_bang(((gphoto_gimme_struct *)threadArgs)->gphoto->x_obj.ob_outlet);
	sys_unlock();

	return(NULL);	
}

// Wrap captureImage
// TODO is there a way to have one wrapper for all funcs?
static void wrapCaptureImage(gphoto_struct *gphoto,  t_symbol *s, int argc, t_atom *argv) {
	int ret;
	pthread_t thread1;

	if (gphoto->connected) {

		// instance of structure
		gphoto_gimme_struct *threadArgs = (gphoto_gimme_struct *)malloc(sizeof(gphoto_gimme_struct));

		// packaging arguments into structure
		threadArgs->gphoto = gphoto;
		threadArgs->s = s;
		threadArgs->argc = argc;
		threadArgs->argv = malloc(sizeof(*argv)*argc);		// allocate new memory space for arguments.
		memcpy(threadArgs->argv, argv, sizeof(*argv)*argc);	// copy the arguments into new space.

		// Create thread
		ret = pthread_create( &thread1, NULL, captureImage, threadArgs);
	} else {
		error("gphoto: ERROR: Not connected.");
	}

	return;
}

static void *gphoto_new(void) {
	gphoto_struct *gphoto = (gphoto_struct *) pd_new(gphoto_class);
	outlet_new(&gphoto->x_obj, NULL);
	gphoto->connectedOutlet = outlet_new(&gphoto->x_obj, &s_float);

	// Initially the external is not "connected"
	gphoto->connected = 0;
	
	return (void *)gphoto;
}

// Destructor to cleanup camera if its still open.
static void *gphoto_free(gphoto_struct *gphoto) {
	if (!gphoto->connected) {
		gp_camera_free(gphoto->camera);
		post("camera free"); // TODO remove me!
	}
}
	

void gphoto_setup(void) {
	gphoto_class = class_new(gensym("gphoto"), (t_newmethod) gphoto_new, 0, sizeof(gphoto_struct), 0, CLASS_DEFAULT, 0);
	class_addmethod(gphoto_class, (t_method) wrapOpen, gensym("open"), 0);
	class_addmethod(gphoto_class, (t_method) wrapClose, gensym("close"), 0);
	class_addmethod(gphoto_class, (t_method) wrapGetConfig, gensym("getconfig"), A_GIMME, 0);
	class_addmethod(gphoto_class, (t_method) wrapSetConfig, gensym("setconfig"), A_GIMME, 0);
	class_addmethod(gphoto_class, (t_method) wrapCaptureImage, gensym("capture"), A_GIMME, 0);
	class_addmethod(gphoto_class, (t_method) wrapListConfig, gensym("listconfig"), 0);
}