Source code upload
[framework/connectivity/libgphoto2.git] / examples / autodetect.c
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
18 sample_autodetect (CameraList *list, GPContext *context) {
19         int                     ret, i;
20         CameraList              *xlist = NULL;
21
22         ret = gp_list_new (&xlist);
23         if (ret < GP_OK) goto out;
24         if (!portinfolist) {
25                 /* Load all the port drivers we have... */
26                 ret = gp_port_info_list_new (&portinfolist);
27                 if (ret < GP_OK) goto out;
28                 ret = gp_port_info_list_load (portinfolist);
29                 if (ret < 0) goto out;
30                 ret = gp_port_info_list_count (portinfolist);
31                 if (ret < 0) goto out;
32         }
33         /* Load all the camera drivers we have... */
34         ret = gp_abilities_list_new (&abilities);
35         if (ret < GP_OK) goto out;
36         ret = gp_abilities_list_load (abilities, context);
37         if (ret < GP_OK) goto out;
38
39         /* ... and autodetect the currently attached cameras. */
40         ret = gp_abilities_list_detect (abilities, portinfolist, xlist, context);
41         if (ret < GP_OK) goto out;
42
43         /* Filter out the "usb:" entry */
44         ret = gp_list_count (xlist);
45         if (ret < GP_OK) goto out;
46         for (i=0;i<ret;i++) {
47                 const char *name, *value;
48
49                 gp_list_get_name (xlist, i, &name);
50                 gp_list_get_value (xlist, i, &value);
51                 if (!strcmp ("usb:",value)) continue;
52                 gp_list_append (list, name, value);
53         }
54 out:
55         gp_list_free (xlist);
56         return gp_list_count(list);
57 }
58
59 /*
60  * This function opens a camera depending on the specified model and port.
61  */
62 int
63 sample_open_camera (Camera ** camera, const char *model, const char *port) {
64         int             ret, m, p;
65         CameraAbilities a;
66         GPPortInfo      pi;
67
68         ret = gp_camera_new (camera);
69         if (ret < GP_OK) return ret;
70
71         /* First lookup the model / driver */
72         m = gp_abilities_list_lookup_model (abilities, model);
73         if (m < GP_OK) return ret;
74         ret = gp_abilities_list_get_abilities (abilities, m, &a);
75         if (ret < GP_OK) return ret;
76         ret = gp_camera_set_abilities (*camera, a);
77         if (ret < GP_OK) return ret;
78
79         /* Then associate the camera with the specified port */
80         p = gp_port_info_list_lookup_path (portinfolist, port);
81         if (ret < GP_OK) return ret;
82         switch (p) {
83         case GP_ERROR_UNKNOWN_PORT:
84                 fprintf (stderr, "The port you specified "
85                         "('%s') can not be found. Please "
86                         "specify one of the ports found by "
87                         "'gphoto2 --list-ports' and make "
88                         "sure the spelling is correct "
89                         "(i.e. with prefix 'serial:' or 'usb:').",
90                                 port);
91                 break;
92         default:
93                 break;
94         }
95         if (ret < GP_OK) return ret;
96         ret = gp_port_info_list_get_info (portinfolist, p, &pi);
97         if (ret < GP_OK) return ret;
98         ret = gp_camera_set_port_info (*camera, pi);
99         if (ret < GP_OK) return ret;
100         return GP_OK;
101 }