libgphoto2 photo camera library (libgphoto2) Internals  2.5.23
gphoto2-widget.c
Go to the documentation of this file.
1 
23 #include "config.h"
24 #include <gphoto2/gphoto2-widget.h>
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <gphoto2/gphoto2-result.h>
31 
38 struct _CameraWidget {
40  char label [256];
41  char info [1024];
42  char name [256];
43 
45 
46  /* Current value of the widget */
47  char *value_string;
48  int value_int;
49  float value_float;
50 
51  /* For Radio and Menu */
52  char **choice;
54 
55  /* For Range */
56  float min;
57  float max;
58  float increment;
59 
60  /* Child info */
63 
64  /* Widget was changed */
65  int changed;
66 
67  /* Widget is read only */
68  int readonly;
69 
70  /* Reference count */
71  int ref_count;
72 
73  /* Unique identifier */
74  int id;
75 
76  /* Callback */
78 };
79 
92 int
93 gp_widget_new (CameraWidgetType type, const char *label,
94  CameraWidget **widget)
95 {
96  static int i = 0;
97 
98  C_PARAMS (label && widget);
99 
100  C_MEM (*widget = calloc (1, sizeof (CameraWidget)));
101 
102  (*widget)->type = type;
103  strcpy ((*widget)->label, label);
104 
105  /* set the value to nothing */
106  (*widget)->value_int = 0;
107  (*widget)->value_float = 0.0;
108  (*widget)->value_string = NULL;
109 
110  (*widget)->ref_count = 1;
111  (*widget)->choice_count = 0;
112  (*widget)->choice = NULL;
113  (*widget)->readonly = 0;
114  (*widget)->id = i++;
115 
116  /* Clear all children pointers */
117  free ((*widget)->children);
118  (*widget)->children = NULL;
119  (*widget)->children_count = 0;
120 
121  return (GP_OK);
122 }
123 
131 int
133 {
134  int x;
135  C_PARAMS (widget);
136 
137  /* Free children recursively */
138  if ((widget->type == GP_WIDGET_WINDOW) ||
139  (widget->type == GP_WIDGET_SECTION)) {
140  for (x = 0; x < gp_widget_count_children (widget); x++)
141  gp_widget_free (widget->children[x]);
142  free (widget->children);
143  }
144  for (x = 0; x < widget->choice_count; x++)
145  free (widget->choice[x]);
146  free (widget->choice);
147  free (widget->value_string);
148  free (widget);
149  return (GP_OK);
150 }
151 
159 int
161 {
162  C_PARAMS (widget);
163 
164  widget->ref_count += 1;
165 
166  return (GP_OK);
167 }
168 
176 int
178 {
179  C_PARAMS (widget);
180 
181  widget->ref_count -= 1;
182 
183  if (widget->ref_count == 0)
184  gp_widget_free (widget);
185 
186  return (GP_OK);
187 }
188 
197 int
198 gp_widget_get_info (CameraWidget *widget, const char **info)
199 {
200  C_PARAMS (widget && info);
201 
202  *info = widget->info;
203  return (GP_OK);
204 }
205 
206 
215 int
216 gp_widget_set_info (CameraWidget *widget, const char *info)
217 {
218  C_PARAMS (widget && info);
219 
220  strcpy (widget->info, info);
221  return (GP_OK);
222 }
223 
232 int
233 gp_widget_get_name (CameraWidget *widget, const char **name)
234 {
235  C_PARAMS (widget && name);
236 
237  *name = widget->name;
238  return (GP_OK);
239 }
240 
249 int
250 gp_widget_set_name (CameraWidget *widget, const char *name)
251 {
252  C_PARAMS (widget && name);
253 
254  strcpy (widget->name, name);
255  return (GP_OK);
256 }
257 
258 
259 
268 int
269 gp_widget_get_id (CameraWidget *widget, int *id)
270 {
271  C_PARAMS (widget && id);
272 
273  *id = widget->id;
274  return (GP_OK);
275 }
276 
288 int
289 gp_widget_set_changed (CameraWidget *widget, int changed)
290 {
291  C_PARAMS (widget);
292 
293  widget->changed = changed;
294  return (GP_OK);
295 }
296 
309 int
310 gp_widget_set_readonly (CameraWidget *widget, int readonly)
311 {
312  C_PARAMS (widget);
313 
314  widget->readonly = readonly;
315  return (GP_OK);
316 }
317 
326 int
327 gp_widget_get_readonly (CameraWidget *widget, int *readonly)
328 {
329  C_PARAMS (widget && readonly);
330 
331  *readonly = widget->readonly;
332  return (GP_OK);
333 }
334 
343 int
345 {
346  C_PARAMS (widget && type);
347 
348  *type = widget->type;
349  return (GP_OK);
350 }
351 
360 int
361 gp_widget_get_label (CameraWidget *widget, const char **label)
362 {
363  C_PARAMS (widget && label);
364 
365  *label = widget->label;
366  return (GP_OK);
367 }
368 
383 int
384 gp_widget_set_value (CameraWidget *widget, const void *value)
385 {
386  C_PARAMS (widget && value);
387 
388  switch (widget->type) {
389  case GP_WIDGET_BUTTON:
390  widget->callback = (CameraWidgetCallback) value;
391  return (GP_OK);
392  case GP_WIDGET_MENU:
393  case GP_WIDGET_RADIO:
394  case GP_WIDGET_TEXT:
395  GP_LOG_D ("Setting value of widget '%s' to '%s'...",
396  widget->label, (char*) value);
397  if (widget->value_string) {
398  if (strcmp (widget->value_string, (char*) value))
399  widget->changed = 1;
400  free (widget->value_string);
401  } else
402  widget->changed = 1;
403  widget->value_string = strdup ((char*)value);
404  return (GP_OK);
405  case GP_WIDGET_RANGE:
406  if (widget->value_float != *((float*)value)) {
407  widget->value_float = *((float*)value);
408  widget->changed = 1;
409  }
410  return (GP_OK);
411  case GP_WIDGET_DATE:
412  case GP_WIDGET_TOGGLE:
413  if (widget->value_int != *((int*)value)) {
414  widget->value_int = *((int*)value);
415  widget->changed = 1;
416  }
417  return (GP_OK);
418  case GP_WIDGET_WINDOW:
419  case GP_WIDGET_SECTION:
420  default:
421  return (GP_ERROR_BAD_PARAMETERS);
422  }
423 }
424 
433 int
434 gp_widget_get_value (CameraWidget *widget, void *value)
435 {
436  C_PARAMS (widget && value);
437 
438  switch (widget->type) {
439  case GP_WIDGET_BUTTON:
440  *(CameraWidgetCallback*)value = widget->callback;
441  return (GP_OK);
442  case GP_WIDGET_MENU:
443  case GP_WIDGET_RADIO:
444  case GP_WIDGET_TEXT:
445  *((char**)value) = widget->value_string;
446  return (GP_OK);
447  case GP_WIDGET_RANGE:
448  *((float*)value) = widget->value_float;
449  return (GP_OK);
450  case GP_WIDGET_TOGGLE:
451  case GP_WIDGET_DATE:
452  *((int*)value) = widget->value_int;
453  return (GP_OK);
454  case GP_WIDGET_SECTION:
455  case GP_WIDGET_WINDOW:
456  default:
457  return (GP_ERROR_BAD_PARAMETERS);
458  }
459 }
460 
469 int
471 {
472  C_PARAMS (widget && child);
473 
474  /* Return if they can't have any children */
475  C_PARAMS ((widget->type == GP_WIDGET_WINDOW) ||
476  (widget->type == GP_WIDGET_SECTION));
477 
478  C_MEM (widget->children = realloc(widget->children, sizeof(CameraWidget*)*(widget->children_count+1)));
479  widget->children[widget->children_count] = child;
480  widget->children_count += 1;
481  child->parent = widget;
482  child->changed = 0;
483 
484  return (GP_OK);
485 }
486 
495 int
497 {
498  int x;
499 
500  C_PARAMS (widget && child);
501 
502  /* Return if they can't have any children */
503  C_PARAMS ((widget->type == GP_WIDGET_WINDOW) ||
504  (widget->type == GP_WIDGET_SECTION));
505 
506  C_MEM (widget->children = realloc(widget->children, sizeof(CameraWidget*)*(widget->children_count+1)));
507 
508  /* Shift down 1 */
509  for (x = widget->children_count; x > 0; x--)
510  widget->children[x] = widget->children[x - 1];
511 
512  /* Prepend the child */
513  widget->children[0] = child;
514  widget->children_count += 1;
515  child->parent = widget;
516  child->changed = 0;
517 
518  return (GP_OK);
519 }
520 
528 int
530 {
531  C_PARAMS (widget);
532 
533  return (widget->children_count);
534 }
535 
545 int
546 gp_widget_get_child (CameraWidget *widget, int child_number,
547  CameraWidget **child)
548 {
549  C_PARAMS (widget && child);
550  C_PARAMS (child_number < widget->children_count);
551 
552  *child = widget->children[child_number];
553  return (GP_OK);
554 }
555 
565 int
566 gp_widget_get_child_by_label (CameraWidget *widget, const char *label,
567  CameraWidget **child)
568 {
569  int x;
570 
571  C_PARAMS (widget && label && child);
572 
573  if (strcmp (widget->label, label) == 0) {
574  *child = widget;
575  return (GP_OK);
576  }
577 
578  for (x = 0; x < widget->children_count; x++) {
579  int result;
580  CameraWidget *child_rec;
581 
583  label, &child_rec);
584  if (result == GP_OK) {
585  *child = child_rec;
586  return (GP_OK);
587  }
588  }
589 
590  return (GP_ERROR_BAD_PARAMETERS);
591 }
592 
602 int
604 {
605  int x;
606 
607  C_PARAMS (widget && child);
608 
609  if (widget->id == id) {
610  *child = widget;
611  return (GP_OK);
612  }
613 
614  for (x = 0; x < widget->children_count; x++) {
615  int result;
616  CameraWidget *child_rec;
617 
618  result = gp_widget_get_child_by_id (widget->children[x], id,
619  &child_rec);
620  if (result == GP_OK) {
621  *child = child_rec;
622  return (GP_OK);
623  }
624  }
625 
626  return (GP_ERROR_BAD_PARAMETERS);
627 }
628 
637 int
638 gp_widget_get_child_by_name (CameraWidget *widget, const char *name,
639  CameraWidget **child)
640 {
641  int x;
642 
643  C_PARAMS (widget && child);
644 
645  if (!strcmp (widget->name, name)) {
646  *child = widget;
647  return (GP_OK);
648  }
649 
650  for (x = 0; x < widget->children_count; x++) {
651  int result;
652  CameraWidget *child_rec;
653 
654  result = gp_widget_get_child_by_name (widget->children[x], name,
655  &child_rec);
656  if (result == GP_OK) {
657  *child = child_rec;
658  return (GP_OK);
659  }
660  }
661 
662  return (GP_ERROR_BAD_PARAMETERS);
663 }
664 
673 int
675 {
676  C_PARAMS (widget && parent);
677 
678  *parent = widget->parent;
679 
680  return (GP_OK);
681 }
682 
691 int
693 {
694  C_PARAMS (widget && root);
695 
696  if (widget->parent)
697  return (gp_widget_get_root (widget->parent, root));
698  else {
699  *root = widget;
700  return (GP_OK);
701  }
702 }
703 
714 int
715 gp_widget_set_range (CameraWidget *range, float min, float max, float increment)
716 {
717  C_PARAMS (range);
718  C_PARAMS (range->type == GP_WIDGET_RANGE);
719 
720  range->min = min;
721  range->max = max;
722  range->increment = increment;
723 
724  return (GP_OK);
725 }
726 
737 int
738 gp_widget_get_range (CameraWidget *range, float *min, float *max,
739  float *increment)
740 {
741  C_PARAMS (range && min && max && increment);
742  C_PARAMS (range->type == GP_WIDGET_RANGE);
743 
744  *min = range->min;
745  *max = range->max;
746  *increment = range->increment;
747 
748  return (GP_OK);
749 }
750 
759 int
760 gp_widget_add_choice (CameraWidget *widget, const char *choice)
761 {
762  C_PARAMS (widget && choice);
763  C_PARAMS ((widget->type == GP_WIDGET_RADIO) ||
764  (widget->type == GP_WIDGET_MENU));
765 
766  C_MEM (widget->choice = realloc (widget->choice, sizeof(char*)*(widget->choice_count+1)));
767  widget->choice[widget->choice_count] = strdup(choice);
768  widget->choice_count += 1;
769  return (GP_OK);
770 }
771 
779 int
781 {
782  C_PARAMS (widget);
783  C_PARAMS ((widget->type == GP_WIDGET_RADIO) ||
784  (widget->type == GP_WIDGET_MENU));
785 
786  return (widget->choice_count);
787 }
788 
798 int
799 gp_widget_get_choice (CameraWidget *widget, int choice_number,
800  const char **choice)
801 {
802  C_PARAMS (widget && choice);
803  C_PARAMS ((widget->type == GP_WIDGET_RADIO) ||
804  (widget->type == GP_WIDGET_MENU));
805  C_PARAMS (choice_number < widget->choice_count);
806 
807  *choice = widget->choice[choice_number];
808  return (GP_OK);
809 }
810 
822 int
824 {
825  C_PARAMS (widget);
826 
827  return widget->changed;
828 }
_CameraWidget::label
char label[256]
Definition: gphoto2-widget.c:40
gp_widget_changed
int gp_widget_changed(CameraWidget *widget)
Tells if the widget has been changed.
Definition: gphoto2-widget.c:823
_CameraWidget::max
float max
Definition: gphoto2-widget.c:57
gp_widget_set_value
int gp_widget_set_value(CameraWidget *widget, const void *value)
Sets the value of the widget.
Definition: gphoto2-widget.c:384
gp_widget_count_children
int gp_widget_count_children(CameraWidget *widget)
Counts the children of the CameraWidget.
Definition: gphoto2-widget.c:529
gphoto2-widget.h
_CameraWidget::info
char info[1024]
Definition: gphoto2-widget.c:41
_CameraWidget::children_count
int children_count
Definition: gphoto2-widget.c:62
CameraWidgetType
CameraWidgetType
Type of the widget to be created.
Definition: gphoto2-widget.h:54
_CameraWidget::min
float min
Definition: gphoto2-widget.c:56
gp_widget_get_child_by_label
int gp_widget_get_child_by_label(CameraWidget *widget, const char *label, CameraWidget **child)
Retrieves the child with label label of the CameraWidget.
Definition: gphoto2-widget.c:566
_CameraWidget::value_string
char * value_string
Definition: gphoto2-widget.c:47
gp_widget_prepend
int gp_widget_prepend(CameraWidget *widget, CameraWidget *child)
Prepends a CameraWidget to a CameraWidget.
Definition: gphoto2-widget.c:496
gp_widget_get_id
int gp_widget_get_id(CameraWidget *widget, int *id)
Retrieves the unique id of the CameraWidget.
Definition: gphoto2-widget.c:269
_CameraWidget::parent
CameraWidget * parent
Definition: gphoto2-widget.c:44
gp_widget_set_name
int gp_widget_set_name(CameraWidget *widget, const char *name)
Sets the name of the widget.
Definition: gphoto2-widget.c:250
result
int result
Definition: gphoto2-result.c:44
_CameraWidget::choice_count
int choice_count
Definition: gphoto2-widget.c:53
gp_widget_get_choice
int gp_widget_get_choice(CameraWidget *widget, int choice_number, const char **choice)
Retrieves the choice number choice_number.
Definition: gphoto2-widget.c:799
gp_widget_get_value
int gp_widget_get_value(CameraWidget *widget, void *value)
Retrieves the value of the CameraWidget.
Definition: gphoto2-widget.c:434
gphoto2-result.h
CameraWidgetCallback
int(* CameraWidgetCallback)(Camera *, CameraWidget *, GPContext *)
Callback handler for Button widgets.
Definition: gphoto2-widget.h:71
gp_widget_append
int gp_widget_append(CameraWidget *widget, CameraWidget *child)
Appends a CameraWidget to a CameraWidget.
Definition: gphoto2-widget.c:470
gp_widget_add_choice
int gp_widget_add_choice(CameraWidget *widget, const char *choice)
Adds a choice to the CameraWidget.
Definition: gphoto2-widget.c:760
gp_widget_get_type
int gp_widget_get_type(CameraWidget *widget, CameraWidgetType *type)
Retrieves the type of the CameraWidget.
Definition: gphoto2-widget.c:344
GP_WIDGET_DATE
@ GP_WIDGET_DATE
Date entering widget.
Definition: gphoto2-widget.h:65
GP_ERROR_BAD_PARAMETERS
#define GP_ERROR_BAD_PARAMETERS
Bad parameters passed.
Definition: gphoto2-port-result.h:38
_CameraWidget
Definition: gphoto2-widget.c:38
_CameraWidget::choice
char ** choice
Definition: gphoto2-widget.c:52
GP_OK
#define GP_OK
Everything is OK.
Definition: gphoto2-port-result.h:30
_CameraWidget::changed
int changed
Definition: gphoto2-widget.c:65
gp_widget_ref
int gp_widget_ref(CameraWidget *widget)
Increments the reference count for the CameraWidget.
Definition: gphoto2-widget.c:160
_CameraWidget::ref_count
int ref_count
Definition: gphoto2-widget.c:71
_CameraWidget::increment
float increment
Definition: gphoto2-widget.c:58
GP_WIDGET_SECTION
@ GP_WIDGET_SECTION
Section widget (think Tab)
Definition: gphoto2-widget.h:58
gp_widget_count_choices
int gp_widget_count_choices(CameraWidget *widget)
Counts the choices of the CameraWidget.
Definition: gphoto2-widget.c:780
gp_widget_set_readonly
int gp_widget_set_readonly(CameraWidget *widget, int readonly)
Tells that the widget is readonly.
Definition: gphoto2-widget.c:310
gp_widget_set_changed
int gp_widget_set_changed(CameraWidget *widget, int changed)
Tells that the widget has been changed.
Definition: gphoto2-widget.c:289
gp_widget_unref
int gp_widget_unref(CameraWidget *widget)
Decrements the reference count for the CameraWidget.
Definition: gphoto2-widget.c:177
gp_widget_get_label
int gp_widget_get_label(CameraWidget *widget, const char **label)
Retrieves the label of the CameraWidget.
Definition: gphoto2-widget.c:361
gp_widget_get_child_by_id
int gp_widget_get_child_by_id(CameraWidget *widget, int id, CameraWidget **child)
Retrieves the child with id id of the widget.
Definition: gphoto2-widget.c:603
gp_widget_get_child
int gp_widget_get_child(CameraWidget *widget, int child_number, CameraWidget **child)
Retrieves the child number child_number of the parent.
Definition: gphoto2-widget.c:546
GP_WIDGET_TOGGLE
@ GP_WIDGET_TOGGLE
Toggle widget (think check box)
Definition: gphoto2-widget.h:61
_CameraWidget::id
int id
Definition: gphoto2-widget.c:74
gp_widget_get_range
int gp_widget_get_range(CameraWidget *range, float *min, float *max, float *increment)
Retrieves some range parameters of the CameraWidget.
Definition: gphoto2-widget.c:738
GP_WIDGET_RANGE
@ GP_WIDGET_RANGE
Slider widget.
Definition: gphoto2-widget.h:60
gp_widget_get_info
int gp_widget_get_info(CameraWidget *widget, const char **info)
Retrieves the information about the widget.
Definition: gphoto2-widget.c:198
_CameraWidget::type
CameraWidgetType type
Definition: gphoto2-widget.c:39
GP_WIDGET_WINDOW
@ GP_WIDGET_WINDOW
Window widget This is the toplevel configuration widget. It should likely contain multiple GP_WIDGET_...
Definition: gphoto2-widget.h:55
gp_widget_new
int gp_widget_new(CameraWidgetType type, const char *label, CameraWidget **widget)
Create a new widget.
Definition: gphoto2-widget.c:93
_CameraWidget::value_int
int value_int
Definition: gphoto2-widget.c:48
gp_widget_get_root
int gp_widget_get_root(CameraWidget *widget, CameraWidget **root)
Retrieves the root of the CameraWidget.
Definition: gphoto2-widget.c:692
gp_widget_get_readonly
int gp_widget_get_readonly(CameraWidget *widget, int *readonly)
Retrieves the readonly state of the CameraWidget.
Definition: gphoto2-widget.c:327
gp_widget_get_parent
int gp_widget_get_parent(CameraWidget *widget, CameraWidget **parent)
Retrieves the parent of a CameraWidget.
Definition: gphoto2-widget.c:674
GP_WIDGET_TEXT
@ GP_WIDGET_TEXT
Text widget.
Definition: gphoto2-widget.h:59
gp_widget_set_info
int gp_widget_set_info(CameraWidget *widget, const char *info)
Sets the information about the widget.
Definition: gphoto2-widget.c:216
_CameraWidget::readonly
int readonly
Definition: gphoto2-widget.c:68
_CameraWidget::value_float
float value_float
Definition: gphoto2-widget.c:49
GP_WIDGET_RADIO
@ GP_WIDGET_RADIO
Radio button widget.
Definition: gphoto2-widget.h:62
gphoto2-port-log.h
_CameraWidget::name
char name[256]
Definition: gphoto2-widget.c:42
gp_widget_get_name
int gp_widget_get_name(CameraWidget *widget, const char **name)
Gets the name of the widget.
Definition: gphoto2-widget.c:233
gp_widget_get_child_by_name
int gp_widget_get_child_by_name(CameraWidget *widget, const char *name, CameraWidget **child)
Retrieves the child with name name of the widget.
Definition: gphoto2-widget.c:638
gp_widget_free
int gp_widget_free(CameraWidget *widget)
Frees a CameraWidget.
Definition: gphoto2-widget.c:132
gp_widget_set_range
int gp_widget_set_range(CameraWidget *range, float min, float max, float increment)
Sets some range parameters of the CameraWidget.
Definition: gphoto2-widget.c:715
_CameraWidget::callback
CameraWidgetCallback callback
Definition: gphoto2-widget.c:77
GP_WIDGET_BUTTON
@ GP_WIDGET_BUTTON
Button press widget.
Definition: gphoto2-widget.h:64
GP_WIDGET_MENU
@ GP_WIDGET_MENU
Menu widget (same as RADIO).
Definition: gphoto2-widget.h:63
_CameraWidget::children
CameraWidget ** children
Definition: gphoto2-widget.c:61