summaryrefslogtreecommitdiff
path: root/libgphoto2/gphoto2-widget.c
blob: 8e8266d5cb0b20b701f1e497513529bfdadaca5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/** \file
 *
 * \author Copyright 2000 Scott Fritzinger
 *
 * \note
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * \note
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details. 
 *
 * \note
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "config.h"
#include <gphoto2/gphoto2-widget.h>

#include <stdlib.h>
#include <string.h>

#include <gphoto2/gphoto2-result.h>
#include <gphoto2/gphoto2-port-log.h>

#define CHECK_NULL(r)        {if (!(r)) return (GP_ERROR_BAD_PARAMETERS);}

/**
 * CameraWidget:
 *
 * The internals of the #CameraWidget are only visible to gphoto2. You can
 * only access them using the functions provided by gphoto2.
 **/
struct _CameraWidget {
	CameraWidgetType type;
	char    label [256];
	char    info [1024];
	char    name [256];

	CameraWidget *parent;

	/* Current value of the widget */
	char   *value_string;
	int     value_int;
	float   value_float;

	/* For Radio and Menu */
	char    choice [100] [64];
	int     choice_count;

	/* For Range */
	float   min; 
	float   max; 
	float   increment;

	/* Child info */
	CameraWidget *children [64];
	int           children_count;

	/* Widget was changed */
	int     changed;

	/* Reference count */
	int     ref_count;

	/* Unique identifier */
	int     id;

	/* Callback */
	CameraWidgetCallback callback;
};

/**
 * The function creates a new #CameraWidget of specified type and with
 * given label.
 *
 * @param type the type
 * @param label the label
 * @param widget 
 * @return a gphoto2 error code.
 * 
 **/
int
gp_widget_new (CameraWidgetType type, const char *label, 
		   CameraWidget **widget) 
{
	int x;
	static int i = 0;

	CHECK_NULL (label && widget);

	*widget = (CameraWidget*) malloc (sizeof (CameraWidget));
	memset (*widget, 0, sizeof (CameraWidget));

	(*widget)->type = type;
	strcpy ((*widget)->label, label);
	
	/* set the value to nothing */
	(*widget)->value_int    	= 0;
        (*widget)->value_float  	= 0.0;
        (*widget)->value_string 	= NULL;

        (*widget)->ref_count    	= 1;
	(*widget)->choice_count 	= 0;
	(*widget)->id			= i++;

        /* Clear all children pointers */
	memset ((*widget)->children, 0, sizeof((*widget)->children));
	(*widget)->children_count = 0;

	/* Clear out the choices */
	for (x = 0; x < sizeof((*widget)->choice)/sizeof((*widget)->choice[0]); x++)
		strcpy ((*widget)->choice[x], "");

	return (GP_OK);
}

/**
 * Frees a #CameraWidget
 *
 * @param widget the #CameraWidget to be freed
 * @return a gphoto2 error code.
 * 
 **/
int
gp_widget_free (CameraWidget *widget)
{
	CHECK_NULL (widget);

	/* Free children recursively */
	if ((widget->type == GP_WIDGET_WINDOW) ||
	    (widget->type == GP_WIDGET_SECTION)) {
	    	int x;

	    	for (x = 0; x < gp_widget_count_children (widget); x++)
			gp_widget_free (widget->children[x]);
	}
	    	
        if (widget->value_string)
		free (widget->value_string);
	free (widget);

	return (GP_OK);
}

/**
 * Increments the reference count for the #CameraWidget
 *
 * @param widget a #CameraWidget you want to ref-count
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_ref (CameraWidget *widget) 
{
	CHECK_NULL (widget);

	widget->ref_count += 1;

	return (GP_OK);
}

/**
 * Decrements the reference count for the #CameraWidget
 *
 * @param widget a #CameraWidget you want to unref
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_unref (CameraWidget *widget) 
{
	CHECK_NULL (widget);

	widget->ref_count -= 1;

	if (widget->ref_count == 0)
		gp_widget_free (widget);

	return (GP_OK);
}

/**
 * Retrieves the information about the widget
 *
 * @param widget a #CameraWidget
 * @param info
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_info (CameraWidget *widget, const char **info)
{
	CHECK_NULL (widget && info);

	*info = widget->info;
	return (GP_OK);
}


/**
 * Sets the information about the widget
 *
 * @param widget a #CameraWidget
 * @param info Information about above widget
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_set_info (CameraWidget *widget, const char *info)
{
	CHECK_NULL (widget && info);

	strcpy (widget->info, info);
	return (GP_OK);
}

/**
 * Gets the name of the widget
 *
 * @param widget a #CameraWidget
 * @param name Name of above widget
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_name (CameraWidget *widget, const char **name)
{
	CHECK_NULL (widget && name);

	*name = widget->name;
	return (GP_OK);
}

/**
 * Sets the name of the widget
 *
 * @param widget a #CameraWidget
 * @param name Name of above widget
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_set_name (CameraWidget *widget, const char *name)
{
	CHECK_NULL (widget && name);

	strcpy (widget->name, name);
	return (GP_OK);
}



/**
 * Retrieves the unique id of the #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param id 
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_id (CameraWidget *widget, int *id)
{
	CHECK_NULL (widget && id);

	*id = widget->id;
	return (GP_OK);
}

int
gp_widget_set_changed (CameraWidget *widget, int changed)
{
	CHECK_NULL (widget);

	widget->changed = changed;
	return (GP_OK);
}

/**
 * Retrieves the type of the #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param type
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_type (CameraWidget *widget, CameraWidgetType *type) 
{
	CHECK_NULL (widget && type);

	*type = widget->type;
	return (GP_OK);
}

/**
 * Retrieves the label of the #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param label
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_label (CameraWidget *widget, const char **label) 
{
	CHECK_NULL (widget && label);

	*label = widget->label;
	return (GP_OK);
}

/**
 * Sets the value of the widget
 *
 * @param widget a #CameraWidget
 * @param value 
 * @return a gphoto2 error code.
 *
 * Please pass
 * (char*) for GP_WIDGET_MENU, GP_WIDGET_TEXT,
 * (float) for GP_WIDGET_RANGE,
 * (int)   for GP_WIDGET_DATE, GP_WIDGET_TOGGLE, GP_WIDGET_RADIO, and
 * (CameraWidgetCallback) for GP_WIDGET_BUTTON.
 *
 **/
int
gp_widget_set_value (CameraWidget *widget, const void *value) 
{
	CHECK_NULL (widget && value);

        switch (widget->type) {
	case GP_WIDGET_BUTTON:
		widget->callback = (CameraWidgetCallback) value;
		return (GP_OK);
	case GP_WIDGET_MENU:
	case GP_WIDGET_RADIO:
        case GP_WIDGET_TEXT:
		gp_log (GP_LOG_DEBUG, "gphoto2-widget", "Setting value to "
			"'%s'...", (char*) value);
	        if (widget->value_string) {
                	if (strcmp (widget->value_string, (char*) value))
                    		widget->changed = 1;
                	free (widget->value_string);
        	} else
        		widget->changed = 1;
        	widget->value_string = strdup ((char*)value);
        	return (GP_OK);
        case GP_WIDGET_RANGE:
            	if (widget->value_float != *((float*)value)) {
                	widget->value_float  = *((float*)value);
                	widget->changed = 1;
            	}
            	return (GP_OK);
	case GP_WIDGET_DATE:
        case GP_WIDGET_TOGGLE:
        	if (widget->value_int != *((int*)value)) {
        		widget->value_int  = *((int*)value);
        		widget->changed = 1;
        	}
	        return (GP_OK);
	case GP_WIDGET_WINDOW:
	case GP_WIDGET_SECTION:
        default:
        	return (GP_ERROR_BAD_PARAMETERS);
        }
}

/**
 * Retrieves the value of the #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param value
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_value (CameraWidget *widget, void *value) 
{
	CHECK_NULL (widget && value);

        switch (widget->type) {
	case GP_WIDGET_BUTTON:
		*(CameraWidgetCallback*)value = widget->callback;
		return (GP_OK);
	case GP_WIDGET_MENU:
	case GP_WIDGET_RADIO:
        case GP_WIDGET_TEXT:
        	*((char**)value) = widget->value_string;
        	return (GP_OK);
        case GP_WIDGET_RANGE:
        	*((float*)value) = widget->value_float;
        	return (GP_OK);
        case GP_WIDGET_TOGGLE:
	case GP_WIDGET_DATE:
            	*((int*)value) = widget->value_int;
        	return (GP_OK);
	case GP_WIDGET_SECTION:
	case GP_WIDGET_WINDOW:
        default:
		return (GP_ERROR_BAD_PARAMETERS);
        }
}

/**
 * Appends a #CameraWidget to a #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param child the #CameraWidget you would like to append to above
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_append (CameraWidget *widget, CameraWidget *child) 
{
	CHECK_NULL (widget && child);

	/* Return if they can't have any children */
        if ((widget->type != GP_WIDGET_WINDOW) && 
	    (widget->type != GP_WIDGET_SECTION))
		return (GP_ERROR_BAD_PARAMETERS);

	widget->children[widget->children_count] = child;
	widget->children_count += 1;
	child->parent = widget;
	child->changed = 0;

	return (GP_OK);
}

/**
 * Prepends a #CameraWidget to a #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param child the #CameraWidget you would like to prepend to above
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_prepend (CameraWidget *widget, CameraWidget *child) 
{
	int x;

	CHECK_NULL (widget && child);

	/* Return if they can't have any children */
	if ((widget->type != GP_WIDGET_WINDOW) && 
	    (widget->type != GP_WIDGET_SECTION))
		return (GP_ERROR_BAD_PARAMETERS);

	/* Shift down 1 */
	for (x = widget->children_count; x > 0; x--)
		widget->children[x] = widget->children[x - 1];

	/* Prepend the child */
	widget->children[0] = child;
	widget->children_count += 1;
	child->parent = widget;
	child->changed = 0;

	return (GP_OK);
}

/**
 * Counts the children of the #CameraWidget
 *
 * @param widget a #CameraWidget
 * @return a gphoto2 error code or number of children
 *
 **/
int
gp_widget_count_children (CameraWidget *widget) 
{
	CHECK_NULL (widget);

	return (widget->children_count);
}

/**
 * Retrieves the child number \c child_number of the parent
 *
 * @param widget a #CameraWidget
 * @param child_number the number of the child
 * @param child
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_child (CameraWidget *widget, int child_number, 
		     CameraWidget **child) 
{
	CHECK_NULL (widget && child);

	if (child_number >= widget->children_count)
		return (GP_ERROR_BAD_PARAMETERS);

	*child = widget->children[child_number];
	return (GP_OK);
}

/**
 * Retrieves the child with label \c label of the #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param label the label of the child
 * @param child
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_child_by_label (CameraWidget *widget, const char *label, 
			      CameraWidget **child)
{
	int x;

	CHECK_NULL (widget && label && child);

	if (strcmp (widget->label, label) == 0) {
		*child = widget;
		return (GP_OK);
	}

	for (x = 0; x < widget->children_count; x++) {
		int result;
		CameraWidget *child_rec;
		
		result = gp_widget_get_child_by_label (widget->children[x], 
						       label, &child_rec);
		if (result == GP_OK) {
			*child = child_rec;
			return (GP_OK);
		}
	}

	return (GP_ERROR_BAD_PARAMETERS);
}

/**
 * Retrieves the child with id \c id of the widget
 *
 * @param widget a #CameraWidget
 * @param id the id of the child
 * @param child
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_child_by_id (CameraWidget *widget, int id, CameraWidget **child) 
{
	int x;

	CHECK_NULL (widget && child);

	if (widget->id == id) {
		*child = widget;
		return (GP_OK);
	}
	
	for (x = 0; x < widget->children_count; x++) {
		int result;
		CameraWidget *child_rec;
		
		result = gp_widget_get_child_by_id (widget->children[x], id, 
						    &child_rec);
		if (result == GP_OK) {
			*child = child_rec;
			return (GP_OK);
		}
	}

	return (GP_ERROR_BAD_PARAMETERS);
}

/**
 * Retrieves the child with name \c name of the widget
 *
 * @param widget a #CameraWidget
 * @param name the name of the child
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_child_by_name (CameraWidget *widget, const char *name,
			     CameraWidget **child)
{
	int x;

	CHECK_NULL (widget && child);

	if (!strcmp (widget->name, name)) {
		*child = widget;
		return (GP_OK);
	}
	
	for (x = 0; x < widget->children_count; x++) {
		int result;
		CameraWidget *child_rec;
		
		result = gp_widget_get_child_by_name (widget->children[x], name,
						      &child_rec);
		if (result == GP_OK) {
			*child = child_rec;
			return (GP_OK);
		}
	}

	return (GP_ERROR_BAD_PARAMETERS);
}

int
gp_widget_get_parent (CameraWidget *widget, CameraWidget **parent)
{
	CHECK_NULL (widget && parent);

	*parent = widget->parent;

	return (GP_OK);
}

/**
 * Retrieves the root of the #CameraWidget
 *
 * @param widget a #CameraWidget
 * @param root
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_root (CameraWidget *widget, CameraWidget **root) 
{
	CHECK_NULL (widget && root);

	if (widget->parent) 
		return (gp_widget_get_root (widget->parent, root));
	else {
		*root = widget;
		return (GP_OK);
	}
}

/**
 * Sets some range parameters of the #CameraWidget
 *
 * @param range a #CameraWidget of type GP_WIDGET_RANGE
 * @param min
 * @param max
 * @param increment
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_set_range (CameraWidget *range, float min, float max, float increment)
{
	CHECK_NULL (range);
	
	if (range->type != GP_WIDGET_RANGE)
		return (GP_ERROR_BAD_PARAMETERS);

	range->min = min;
	range->max = max;
	range->increment = increment;

	return (GP_OK);
}

/**
 * Retrieves some range parameters of the #CameraWidget
 *
 * @param range a #CameraWidget of type GP_WIDGET_RANGE
 * @param min
 * @param max
 * @param increment
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_get_range (CameraWidget *range, float *min, float *max, 
		     float *increment) 
{
	CHECK_NULL (range && min && max && increment);
	if (range->type != GP_WIDGET_RANGE)
		return (GP_ERROR_BAD_PARAMETERS);

	*min = range->min;
	*max = range->max;
	*increment = range->increment;

	return (GP_OK);
}

/**
 * Adds a choice to the #CameraWidget
 *
 * @param widget a #CameraWidget of type GP_WIDGET_RADIO or GP_WIDGET_MENU
 * @param choice
 * @return a gphoto2 error code.
 *
 **/
int
gp_widget_add_choice (CameraWidget *widget, const char *choice) 
{
	CHECK_NULL (widget && choice);
	if ((widget->type != GP_WIDGET_RADIO) &&
	    (widget->type != GP_WIDGET_MENU))
		return (GP_ERROR_BAD_PARAMETERS);

	/* static list of choices is full */
	if (widget->choice_count >= sizeof(widget->choice)/sizeof(widget->choice[0]))
		return (GP_ERROR);

	strncpy (widget->choice[widget->choice_count], choice, sizeof(widget->choice[0]));
	widget->choice_count += 1;

	return (GP_OK);
}

/**
 * Counts the choices of the #CameraWidget
 *
 * @param widget a #CameraWidget of type GP_WIDGET_RADIO or GP_WIDGET_MENU
 * @return a gphoto2 error code or number of choices.
 * 
 **/
int
gp_widget_count_choices (CameraWidget *widget) 
{
	CHECK_NULL (widget);
	if ((widget->type != GP_WIDGET_RADIO) &&
	    (widget->type != GP_WIDGET_MENU))
		return (GP_ERROR_BAD_PARAMETERS);

	return (widget->choice_count);
}

/**
 * Retrieves the choice number \c choice_number
 *
 * @param widget a #CameraWidget of type GP_WIDGET_RADIO or GP_WIDGET_MENU
 * @param choice_number
 * @param choice
 * @return a gphoto2 error code
 *
 **/
int
gp_widget_get_choice (CameraWidget *widget, int choice_number, 
		      const char **choice) 
{
	CHECK_NULL (widget && choice);
	if ((widget->type != GP_WIDGET_RADIO) &&
	    (widget->type != GP_WIDGET_MENU))
		return (GP_ERROR_BAD_PARAMETERS);

	if (choice_number >= widget->choice_count)
		return (GP_ERROR_BAD_PARAMETERS);

	*choice = widget->choice[choice_number];
	return (GP_OK);
}

/**
 * Tells if the widget has been changed
 *
 * @param widget a #CameraWidget
 * @return a gphoto2 error code or changed flag.
 *
 * Returns 1 if the state of the #CameraWidget has been changed or 0 if not.
 * In addition, it resets the changed flag to 0.
 *
 **/
int
gp_widget_changed (CameraWidget *widget) 
{
        int val;

	CHECK_NULL (widget);

        val = widget->changed;
        widget->changed = 0;

        return (val);
}