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 /* calls the Nikon DSLR or Canon DSLR autofocus method. */
24 int
camera_eosviewfinder(Camera * camera,GPContext * context,int onoff)25 camera_eosviewfinder(Camera *camera, GPContext *context, int onoff) {
26 	CameraWidget		*widget = NULL, *child = NULL;
27 	CameraWidgetType	type;
28 	int			ret,val;
29 
30 	ret = gp_camera_get_config (camera, &widget, context);
31 	if (ret < GP_OK) {
32 		fprintf (stderr, "camera_get_config failed: %d\n", ret);
33 		return ret;
34 	}
35 	ret = _lookup_widget (widget, "eosviewfinder", &child);
36 	if (ret < GP_OK) {
37 		fprintf (stderr, "lookup 'eosviewfinder' failed: %d\n", ret);
38 		goto out;
39 	}
40 
41 	/* check that this is a toggle */
42 	ret = gp_widget_get_type (child, &type);
43 	if (ret < GP_OK) {
44 		fprintf (stderr, "widget get type failed: %d\n", ret);
45 		goto out;
46 	}
47 	switch (type) {
48         case GP_WIDGET_TOGGLE:
49 		break;
50 	default:
51 		fprintf (stderr, "widget has bad type %d\n", type);
52 		ret = GP_ERROR_BAD_PARAMETERS;
53 		goto out;
54 	}
55 
56 	ret = gp_widget_get_value (child, &val);
57 	if (ret < GP_OK) {
58 		fprintf (stderr, "could not get widget value: %d\n", ret);
59 		goto out;
60 	}
61 	val = onoff;
62 	ret = gp_widget_set_value (child, &val);
63 	if (ret < GP_OK) {
64 		fprintf (stderr, "could not set widget value to 1: %d\n", ret);
65 		goto out;
66 	}
67 
68 	ret = gp_camera_set_config (camera, widget, context);
69 	if (ret < GP_OK) {
70 		fprintf (stderr, "could not set config tree to eosviewfinder: %d\n", ret);
71 		goto out;
72 	}
73 out:
74 	gp_widget_free (widget);
75 	return ret;
76 }
77 
78 int
camera_auto_focus(Camera * camera,GPContext * context,int onoff)79 camera_auto_focus(Camera *camera, GPContext *context, int onoff) {
80 	CameraWidget		*widget = NULL, *child = NULL;
81 	CameraWidgetType	type;
82 	int			ret,val;
83 
84 	ret = gp_camera_get_config (camera, &widget, context);
85 	if (ret < GP_OK) {
86 		fprintf (stderr, "camera_get_config failed: %d\n", ret);
87 		return ret;
88 	}
89 	ret = _lookup_widget (widget, "autofocusdrive", &child);
90 	if (ret < GP_OK) {
91 		fprintf (stderr, "lookup 'autofocusdrive' failed: %d\n", ret);
92 		goto out;
93 	}
94 
95 	/* check that this is a toggle */
96 	ret = gp_widget_get_type (child, &type);
97 	if (ret < GP_OK) {
98 		fprintf (stderr, "widget get type failed: %d\n", ret);
99 		goto out;
100 	}
101 	switch (type) {
102         case GP_WIDGET_TOGGLE:
103 		break;
104 	default:
105 		fprintf (stderr, "widget has bad type %d\n", type);
106 		ret = GP_ERROR_BAD_PARAMETERS;
107 		goto out;
108 	}
109 
110 	ret = gp_widget_get_value (child, &val);
111 	if (ret < GP_OK) {
112 		fprintf (stderr, "could not get widget value: %d\n", ret);
113 		goto out;
114 	}
115 
116 	val = onoff;
117 
118 	ret = gp_widget_set_value (child, &val);
119 	if (ret < GP_OK) {
120 		fprintf (stderr, "could not set widget value to 1: %d\n", ret);
121 		goto out;
122 	}
123 
124 	ret = gp_camera_set_config (camera, widget, context);
125 	if (ret < GP_OK) {
126 		fprintf (stderr, "could not set config tree to autofocus: %d\n", ret);
127 		goto out;
128 	}
129 out:
130 	gp_widget_free (widget);
131 	return ret;
132 }
133 
134 
135 /* Manual focusing a camera...
136  * xx is -3 / -2 / -1 / 0 / 1 / 2 / 3
137  */
138 int
camera_manual_focus(Camera * camera,int xx,GPContext * context)139 camera_manual_focus (Camera *camera, int xx, GPContext *context) {
140 	CameraWidget		*widget = NULL, *child = NULL;
141 	CameraWidgetType	type;
142 	int			ret;
143 	float			rval;
144 	char			*mval;
145 
146 	ret = gp_camera_get_config (camera, &widget, context);
147 	if (ret < GP_OK) {
148 		fprintf (stderr, "camera_get_config failed: %d\n", ret);
149 		return ret;
150 	}
151 	ret = _lookup_widget (widget, "manualfocusdrive", &child);
152 	if (ret < GP_OK) {
153 		fprintf (stderr, "lookup 'manualfocusdrive' failed: %d\n", ret);
154 		goto out;
155 	}
156 
157 	/* check that this is a toggle */
158 	ret = gp_widget_get_type (child, &type);
159 	if (ret < GP_OK) {
160 		fprintf (stderr, "widget get type failed: %d\n", ret);
161 		goto out;
162 	}
163 	switch (type) {
164         case GP_WIDGET_RADIO: {
165 		int choices = gp_widget_count_choices (child);
166 
167 		ret = gp_widget_get_value (child, &mval);
168 		if (ret < GP_OK) {
169 			fprintf (stderr, "could not get widget value: %d\n", ret);
170 			goto out;
171 		}
172 		if (choices == 7) { /* see what Canon has in EOS_MFDrive */
173 			ret = gp_widget_get_choice (child, xx+4, (const char**)&mval);
174 			if (ret < GP_OK) {
175 				fprintf (stderr, "could not get widget choice %d: %d\n", xx+2, ret);
176 				goto out;
177 			}
178 			fprintf(stderr,"manual focus %d -> %s\n", xx, mval);
179 		}
180 		ret = gp_widget_set_value (child, mval);
181 		if (ret < GP_OK) {
182 			fprintf (stderr, "could not set widget value to 1: %d\n", ret);
183 			goto out;
184 		}
185 		break;
186 	}
187         case GP_WIDGET_RANGE:
188 		ret = gp_widget_get_value (child, &rval);
189 		if (ret < GP_OK) {
190 			fprintf (stderr, "could not get widget value: %d\n", ret);
191 			goto out;
192 		}
193 
194 		switch (xx) { /* Range is on Nikon from -32768 <-> 32768 */
195 		case -3:	rval = -1024;break;
196 		case -2:	rval =  -512;break;
197 		case -1:	rval =  -128;break;
198 		case  0:	rval =     0;break;
199 		case  1:	rval =   128;break;
200 		case  2:	rval =   512;break;
201 		case  3:	rval =  1024;break;
202 
203 		default:	rval = xx;	break; /* hack */
204 		}
205 
206 		fprintf(stderr,"manual focus %d -> %f\n", xx, rval);
207 
208 		ret = gp_widget_set_value (child, &rval);
209 		if (ret < GP_OK) {
210 			fprintf (stderr, "could not set widget value to 1: %d\n", ret);
211 			goto out;
212 		}
213 		break;
214 	default:
215 		fprintf (stderr, "widget has bad type %d\n", type);
216 		ret = GP_ERROR_BAD_PARAMETERS;
217 		goto out;
218 	}
219 
220 
221 	ret = gp_camera_set_config (camera, widget, context);
222 	if (ret < GP_OK) {
223 		fprintf (stderr, "could not set config tree to autofocus: %d\n", ret);
224 		goto out;
225 	}
226 out:
227 	gp_widget_free (widget);
228 	return ret;
229 }
230