1 #include "samples.h"
2 
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 /*
8  * This function looks up a label or key entry of
9  * a configuration widget.
10  * The functions descend recursively, so you can just
11  * specify the last component.
12  */
13 
14 static int
_lookup_widget(CameraWidget * widget,const char * key,CameraWidget ** child)15 _lookup_widget(CameraWidget*widget, const char *key, CameraWidget **child) {
16 	int ret;
17 	ret = gp_widget_get_child_by_name (widget, key, child);
18 	if (ret < GP_OK)
19 		ret = gp_widget_get_child_by_label (widget, key, child);
20 	return ret;
21 }
22 
23 /* Gets a string configuration value.
24  * This can be:
25  *  - A Text widget
26  *  - The current selection of a Radio Button choice
27  *  - The current selection of a Menu choice
28  *
29  * Sample (for Canons eg):
30  *   get_config_value_string (camera, "owner", &ownerstr, context);
31  */
32 int
get_config_value_string(Camera * camera,const char * key,char ** str,GPContext * context)33 get_config_value_string (Camera *camera, const char *key, char **str, GPContext *context) {
34 	CameraWidget		*widget = NULL, *child = NULL;
35 	CameraWidgetType	type;
36 	int			ret;
37 	char			*val;
38 
39 	ret = gp_camera_get_single_config (camera, key, &child, context);
40 	if (ret == GP_OK) {
41 		if (!child) fprintf(stderr,"child is NULL?\n");
42 		widget = child;
43 	} else {
44 		ret = gp_camera_get_config (camera, &widget, context);
45 		if (ret < GP_OK) {
46 			fprintf (stderr, "camera_get_config failed: %d\n", ret);
47 			return ret;
48 		}
49 		ret = _lookup_widget (widget, key, &child);
50 		if (ret < GP_OK) {
51 			fprintf (stderr, "lookup widget failed: %d\n", ret);
52 			goto out;
53 		}
54 	}
55 
56 	/* This type check is optional, if you know what type the label
57 	 * has already. If you are not sure, better check. */
58 	ret = gp_widget_get_type (child, &type);
59 	if (ret < GP_OK) {
60 		fprintf (stderr, "widget get type failed: %d\n", ret);
61 		goto out;
62 	}
63 	switch (type) {
64         case GP_WIDGET_MENU:
65         case GP_WIDGET_RADIO:
66         case GP_WIDGET_TEXT:
67 		break;
68 	default:
69 		fprintf (stderr, "widget has bad type %d\n", type);
70 		ret = GP_ERROR_BAD_PARAMETERS;
71 		goto out;
72 	}
73 
74 	/* This is the actual query call. Note that we just
75 	 * a pointer reference to the string, not a copy... */
76 	ret = gp_widget_get_value (child, &val);
77 	if (ret < GP_OK) {
78 		fprintf (stderr, "could not query widget value: %d\n", ret);
79 		goto out;
80 	}
81 	/* Create a new copy for our caller. */
82 	*str = strdup (val);
83 out:
84 	gp_widget_free (widget);
85 	return ret;
86 }
87 
88 
89 /* Sets a string configuration value.
90  * This can set for:
91  *  - A Text widget
92  *  - The current selection of a Radio Button choice
93  *  - The current selection of a Menu choice
94  *
95  * Sample (for Canons eg):
96  *   get_config_value_string (camera, "owner", &ownerstr, context);
97  */
98 int
set_config_value_string(Camera * camera,const char * key,const char * val,GPContext * context)99 set_config_value_string (Camera *camera, const char *key, const char *val, GPContext *context) {
100 	CameraWidget		*widget = NULL, *child = NULL;
101 	CameraWidgetType	type;
102 	int			ret;
103 
104 	ret = gp_camera_get_config (camera, &widget, context);
105 	if (ret < GP_OK) {
106 		fprintf (stderr, "camera_get_config failed: %d\n", ret);
107 		return ret;
108 	}
109 	ret = _lookup_widget (widget, key, &child);
110 	if (ret < GP_OK) {
111 		fprintf (stderr, "lookup widget failed: %d\n", ret);
112 		goto out;
113 	}
114 
115 	/* This type check is optional, if you know what type the label
116 	 * has already. If you are not sure, better check. */
117 	ret = gp_widget_get_type (child, &type);
118 	if (ret < GP_OK) {
119 		fprintf (stderr, "widget get type failed: %d\n", ret);
120 		goto out;
121 	}
122 	switch (type) {
123         case GP_WIDGET_MENU:
124         case GP_WIDGET_RADIO:
125         case GP_WIDGET_TEXT:
126 		break;
127 	default:
128 		fprintf (stderr, "widget has bad type %d\n", type);
129 		ret = GP_ERROR_BAD_PARAMETERS;
130 		goto out;
131 	}
132 
133 	/* This is the actual set call. Note that we keep
134 	 * ownership of the string and have to free it if necessary.
135 	 */
136 	ret = gp_widget_set_value (child, val);
137 	if (ret < GP_OK) {
138 		fprintf (stderr, "could not set widget value: %d\n", ret);
139 		goto out;
140 	}
141 	ret = gp_camera_set_single_config (camera, key, child, context);
142 	if (ret != GP_OK) {
143 		/* This stores it on the camera again */
144 		ret = gp_camera_set_config (camera, widget, context);
145 		if (ret < GP_OK) {
146 			fprintf (stderr, "camera_set_config failed: %d\n", ret);
147 			return ret;
148 		}
149 	}
150 out:
151 	gp_widget_free (widget);
152 	return ret;
153 }
154 
155 
156 /*
157  * This enables/disables the specific canon capture mode.
158  *
159  * For non canons this is not required, and will just return
160  * with an error (but without negative effects).
161  */
162 int
canon_enable_capture(Camera * camera,int onoff,GPContext * context)163 canon_enable_capture (Camera *camera, int onoff, GPContext *context) {
164 	CameraWidget		*widget = NULL, *child = NULL;
165 	CameraWidgetType	type;
166 	int			ret;
167 
168 	ret = gp_camera_get_single_config (camera, "capture", &widget, context);
169 	if (ret < GP_OK) {
170 		fprintf (stderr, "camera_get_config failed: %d\n", ret);
171 		return ret;
172 	}
173 
174 	ret = gp_widget_get_type (child, &type);
175 	if (ret < GP_OK) {
176 		fprintf (stderr, "widget get type failed: %d\n", ret);
177 		goto out;
178 	}
179 	switch (type) {
180         case GP_WIDGET_TOGGLE:
181 		break;
182 	default:
183 		fprintf (stderr, "widget has bad type %d\n", type);
184 		ret = GP_ERROR_BAD_PARAMETERS;
185 		goto out;
186 	}
187 	/* Now set the toggle to the wanted value */
188 	ret = gp_widget_set_value (child, &onoff);
189 	if (ret < GP_OK) {
190 		fprintf (stderr, "toggling Canon capture to %d failed with %d\n", onoff, ret);
191 		goto out;
192 	}
193 	/* OK */
194 	ret = gp_camera_set_single_config (camera, "capture", widget, context);
195 	if (ret < GP_OK) {
196 		fprintf (stderr, "camera_set_config failed: %d\n", ret);
197 		return ret;
198 	}
199 out:
200 	gp_widget_free (widget);
201 	return ret;
202 }
203