1 #include <stdio.h>
2 #include <string.h>
3 
4 #include <gphoto2/gphoto2-camera.h>
5 
6 #include "samples.h"
7 
8 static GPPortInfoList		*portinfolist = NULL;
9 static CameraAbilitiesList	*abilities = NULL;
10 
11 /*
12  * This detects all currently attached cameras and returns
13  * them in a list. It avoids the generic usb: entry.
14  *
15  * This function does not open nor initialize the cameras yet.
16  */
17 int
sample_autodetect(CameraList * list,GPContext * context)18 sample_autodetect (CameraList *list, GPContext *context) {
19 	gp_list_reset (list);
20         return gp_camera_autodetect (list, context);
21 }
22 
23 /*
24  * This function opens a camera depending on the specified model and port.
25  */
26 int
sample_open_camera(Camera ** camera,const char * model,const char * port,GPContext * context)27 sample_open_camera (Camera ** camera, const char *model, const char *port, GPContext *context) {
28 	int		ret, m, p;
29 	CameraAbilities	a;
30 	GPPortInfo	pi;
31 
32 	ret = gp_camera_new (camera);
33 	if (ret < GP_OK) return ret;
34 
35 	if (!abilities) {
36 		/* Load all the camera drivers we have... */
37 		ret = gp_abilities_list_new (&abilities);
38 		if (ret < GP_OK) return ret;
39 		ret = gp_abilities_list_load (abilities, context);
40 		if (ret < GP_OK) return ret;
41 	}
42 
43 	/* First lookup the model / driver */
44         m = gp_abilities_list_lookup_model (abilities, model);
45 	if (m < GP_OK) return ret;
46         ret = gp_abilities_list_get_abilities (abilities, m, &a);
47 	if (ret < GP_OK) return ret;
48         ret = gp_camera_set_abilities (*camera, a);
49 	if (ret < GP_OK) return ret;
50 
51 	if (!portinfolist) {
52 		/* Load all the port drivers we have... */
53 		ret = gp_port_info_list_new (&portinfolist);
54 		if (ret < GP_OK) return ret;
55 		ret = gp_port_info_list_load (portinfolist);
56 		if (ret < 0) return ret;
57 		ret = gp_port_info_list_count (portinfolist);
58 		if (ret < 0) return ret;
59 	}
60 
61 	/* Then associate the camera with the specified port */
62         p = gp_port_info_list_lookup_path (portinfolist, port);
63         switch (p) {
64         case GP_ERROR_UNKNOWN_PORT:
65                 fprintf (stderr, "The port you specified "
66                         "('%s') can not be found. Please "
67                         "specify one of the ports found by "
68                         "'gphoto2 --list-ports' and make "
69                         "sure the spelling is correct "
70                         "(i.e. with prefix 'serial:' or 'usb:').",
71                                 port);
72                 break;
73         default:
74                 break;
75         }
76         if (p < GP_OK) return p;
77 
78         ret = gp_port_info_list_get_info (portinfolist, p, &pi);
79         if (ret < GP_OK) return ret;
80         ret = gp_camera_set_port_info (*camera, pi);
81         if (ret < GP_OK) return ret;
82 	return GP_OK;
83 }
84