$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2009, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <stdlib.h> 00037 #include <stdio.h> 00038 #include <string.h> 00039 #include <photo/photo.h> 00040 00041 static int 00042 find_widget_by_name (Camera *photo, GPContext *context, const char *param, CameraWidget **child, CameraWidget **rootconfig) { 00043 int ret; 00044 00045 ret = gp_camera_get_config (photo, rootconfig, context); 00046 if (ret != GP_OK) return ret; 00047 ret = gp_widget_get_child_by_name (*rootconfig, param, child); 00048 if (ret != GP_OK) 00049 ret = gp_widget_get_child_by_label (*rootconfig, param, child); 00050 if (ret != GP_OK) { 00051 char *part, *s, *newname; 00052 00053 newname = strdup (param); 00054 if (!newname) 00055 return GP_ERROR_NO_MEMORY; 00056 00057 *child = *rootconfig; 00058 part = newname; 00059 while (part[0] == '/') 00060 part++; 00061 while (1) { 00062 CameraWidget *tmp; 00063 00064 s = strchr (part,'/'); 00065 if (s) 00066 *s='\0'; 00067 ret = gp_widget_get_child_by_name (*child, part, &tmp); 00068 if (ret != GP_OK) 00069 ret = gp_widget_get_child_by_label (*child, part, &tmp); 00070 if (ret != GP_OK) 00071 break; 00072 *child = tmp; 00073 if (!s) /* end of path */ 00074 break; 00075 part = s+1; 00076 while (part[0] == '/') 00077 part++; 00078 } 00079 if (s) { /* if we have stuff left over, we failed */ 00080 gp_context_error (context,"%s not found in configuration tree.", newname); 00081 free (newname); 00082 gp_widget_free (*rootconfig); 00083 return GP_ERROR; 00084 } 00085 free (newname); 00086 } 00087 return GP_OK; 00088 } 00089 00090 int photo_set_config(photo_p photo, const char *param, const char *value) 00091 { 00092 CameraWidget *rootconfig, *child; 00093 int ret; 00094 const char *label; 00095 CameraWidgetType type; 00096 00097 ret = find_widget_by_name(photo->cam, photo->context, param, &child, &rootconfig); 00098 if (ret!=GP_OK) 00099 return 0; 00100 00101 ret = gp_widget_get_type(child, &type); 00102 if (ret!=GP_OK) { 00103 gp_widget_free(rootconfig); 00104 return 0; 00105 } 00106 ret = gp_widget_get_label(child, &label); 00107 if (ret!=GP_OK) { 00108 gp_widget_free(rootconfig); 00109 return 0; 00110 } 00111 00112 switch (type) { 00113 case GP_WIDGET_TEXT: { /* char * */ 00114 ret = gp_widget_set_value(child, value); 00115 if (ret!=GP_OK) 00116 gp_context_error(photo->context,"Failed to set the value of text widget %s to %s.", param, value); 00117 break; 00118 } 00119 case GP_WIDGET_RANGE: { /* float */ 00120 float f, t, b, s; 00121 00122 ret = gp_widget_get_range(child, &b, &t, &s); 00123 if (ret!=GP_OK) 00124 break; 00125 if (!sscanf(value, "%f", &f)) { 00126 gp_context_error(photo->context,"The passed value %s is not a floating point value.", value); 00127 ret = GP_ERROR_BAD_PARAMETERS; 00128 break; 00129 } 00130 if ((f<b)||(f>t)) { 00131 gp_context_error(photo->context,"The passed value %f is not within the expected range %f - %f.", f, b, t); 00132 ret = GP_ERROR_BAD_PARAMETERS; 00133 break; 00134 } 00135 ret = gp_widget_set_value(child, &f); 00136 if (ret!=GP_OK) 00137 gp_context_error(photo->context,"Failed to set the value of range widget %s to %f.", param, f); 00138 break; 00139 } 00140 case GP_WIDGET_TOGGLE: { /* int */ 00141 int t; 00142 00143 t = 2; 00144 if (!strcasecmp(value, "off")|| 00145 !strcasecmp(value, "no")|| 00146 !strcasecmp(value, "false")|| 00147 !strcmp(value, "0")) 00148 t = 0; 00149 if (!strcasecmp(value, "on")|| 00150 !strcasecmp(value, "yes")|| 00151 !strcasecmp(value, "true")|| 00152 !strcmp(value, "1")) 00153 t = 1; 00154 /*fprintf (stderr," value %s, t %d\n", value, t);*/ 00155 if (t==2) { 00156 gp_context_error(photo->context,"The passed value %s is not a valid toggle value.", value); 00157 ret = GP_ERROR_BAD_PARAMETERS; 00158 break; 00159 } 00160 ret = gp_widget_set_value(child, &t); 00161 if (ret!=GP_OK) 00162 gp_context_error(photo->context,"Failed to set values %s of toggle widget %s.", value, param); 00163 break; 00164 } 00165 case GP_WIDGET_DATE: { /* int */ 00166 int t = -1; 00167 #ifdef HAVE_STRPTIME 00168 struct tm xtm; 00169 00170 if (strptime (value, "%c", &xtm) || strptime (value, "%Ec", &xtm)) 00171 t = mktime (&xtm); 00172 #endif 00173 if (t==-1) { 00174 if (!sscanf(value, "%d", &t)) { 00175 gp_context_error(photo->context,"The passed value %s is neither a valid time nor an integer.", value); 00176 ret = GP_ERROR_BAD_PARAMETERS; 00177 break; 00178 } 00179 } 00180 ret = gp_widget_set_value(child, &t); 00181 if (ret!=GP_OK) 00182 gp_context_error(photo->context,"Failed to set new time of date/time widget %s to %s.", param, value); 00183 break; 00184 } 00185 case GP_WIDGET_MENU: 00186 case GP_WIDGET_RADIO: { /* char * */ 00187 int cnt, i; 00188 00189 cnt = gp_widget_count_choices(child); 00190 if (cnt<GP_OK) { 00191 ret = cnt; 00192 break; 00193 } 00194 ret = GP_ERROR_BAD_PARAMETERS; 00195 for(i = 0; i<cnt; i++) { 00196 const char *choice; 00197 00198 ret = gp_widget_get_choice(child, i, &choice); 00199 if (ret!=GP_OK) 00200 continue; 00201 if (!strcmp(choice, value)) { 00202 ret = gp_widget_set_value(child, value); 00203 break; 00204 } 00205 } 00206 if (i!=cnt) 00207 break; 00208 00209 if (sscanf(value, "%d", &i)) { 00210 if ((i>=0)&&(i<cnt)) { 00211 const char *choice; 00212 00213 ret = gp_widget_get_choice(child, i, &choice); 00214 if (ret==GP_OK) 00215 ret = gp_widget_set_value(child, choice); 00216 break; 00217 } 00218 } 00219 gp_context_error(photo->context,"Choice %s not found within list of choices.", value); 00220 break; 00221 } 00222 00223 /* ignore: */ 00224 case GP_WIDGET_WINDOW: 00225 case GP_WIDGET_SECTION: 00226 case GP_WIDGET_BUTTON: 00227 gp_context_error(photo->context,"The %s widget is not configurable.", param); 00228 ret = GP_ERROR_BAD_PARAMETERS; 00229 break; 00230 } 00231 if (ret==GP_OK) { 00232 ret = gp_camera_set_config(photo->cam, rootconfig, photo->context); 00233 if (ret!=GP_OK) 00234 gp_context_error(photo->context,"Failed to set new configuration value %s for configuration entry %s.", value, param); 00235 } 00236 gp_widget_free(rootconfig); 00237 return (ret==GP_OK); 00238 } 00239 00240 int photo_get_config(photo_p photo, const char *param, char **value) 00241 { 00242 CameraWidget *rootconfig, *child; 00243 int ret; 00244 const char *label; 00245 CameraWidgetType type; 00246 00247 /* find widget */ 00248 ret = find_widget_by_name(photo->cam, photo->context, param, &child, &rootconfig); 00249 if (ret != GP_OK) 00250 return 0; 00251 00252 /* get widget type */ 00253 ret = gp_widget_get_type (child, &type); 00254 if (ret != GP_OK) { 00255 gp_widget_free (rootconfig); 00256 return 0; 00257 } 00258 00259 /* get widget label */ 00260 ret = gp_widget_get_label (child, &label); 00261 if (ret != GP_OK) { 00262 gp_widget_free (rootconfig); 00263 return 0; 00264 } 00265 00266 // printf ("Label: %s\n", label); /* "Label:" is not i18ned, the "label" variable is */ 00267 switch (type) { 00268 case GP_WIDGET_TEXT: { /* char * */ 00269 char *txt; 00270 00271 ret = gp_widget_get_value (child, &txt); 00272 if (ret != GP_OK) { 00273 gp_context_error (photo->context,"Failed to retrieve value of text widget %s.", param); 00274 } 00275 *value = txt; 00276 break; 00277 } 00278 case GP_WIDGET_RANGE: { /* float */ 00279 float f, t,b,s; 00280 00281 ret = gp_widget_get_range (child, &b, &t, &s); 00282 if (ret != GP_OK){ 00283 gp_context_error (photo->context,"Failed to retrieve values of range widget %s.", param); 00284 } 00285 ret = gp_widget_get_value (child, &f); 00286 if (ret != GP_OK) { 00287 gp_context_error (photo->context,"Failed to value of range widget %s.", param); 00288 } 00289 sprintf(*value,"%f",f); 00290 break; 00291 } 00292 case GP_WIDGET_TOGGLE: { /* int */ 00293 int t; 00294 00295 ret = gp_widget_get_value (child, &t); 00296 if (ret != GP_OK) { 00297 gp_context_error (photo->context,"Failed to retrieve values of toggle widget %s.", param); 00298 } 00299 sprintf(*value,"%d",t); 00300 break; 00301 } 00302 case GP_WIDGET_DATE: { /* int */ 00303 int ret, t; 00304 time_t xtime; 00305 struct tm *xtm; 00306 char timebuf[200]; 00307 00308 ret = gp_widget_get_value (child, &t); 00309 if (ret != GP_OK) { 00310 gp_context_error (photo->context,"Failed to retrieve values of date/time widget %s.", param); 00311 break; 00312 } 00313 xtime = t; 00314 xtm = localtime (&xtime); 00315 ret = strftime (timebuf, sizeof(timebuf), "%c", xtm); 00316 sprintf(*value,"%s",timebuf); 00317 break; 00318 } 00319 case GP_WIDGET_MENU: 00320 case GP_WIDGET_RADIO: { /* char * */ 00321 char *current; 00322 ret = gp_widget_get_value (child, ¤t); 00323 if (ret != GP_OK) { 00324 gp_context_error (photo->context,"Failed to retrieve values of radio widget %s.", param); 00325 } 00326 sprintf(*value,"%s",current); 00327 break; 00328 } 00329 00330 /* ignore: */ 00331 case GP_WIDGET_WINDOW: 00332 case GP_WIDGET_SECTION: 00333 case GP_WIDGET_BUTTON: 00334 break; 00335 } 00336 gp_widget_free (rootconfig); 00337 return (ret==GP_OK); 00338 } 00339 00340