forked from themakers/gphoto2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gp_camera_widget.go
358 lines (274 loc) · 7.69 KB
/
gp_camera_widget.go
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
package gphoto2
// #cgo pkg-config: libgphoto2
// #include <gphoto2.h>
// #include <stdlib.h>
import "C"
import (
"fmt"
"time"
"unsafe"
)
type CameraWidget struct {
c_ref *C.CameraWidget
cam *Camera
}
func (cam *Camera) RootWidget() (*CameraWidget, func() error, error) {
cw := &CameraWidget{
cam: cam,
}
c_name, c_name_free := cString("")
defer c_name_free()
if err := toError(C.gp_widget_new(C.GP_WIDGET_WINDOW, c_name, &cw.c_ref)); err != nil {
return nil, nil, err
} else if err := toError(C.gp_camera_get_config(cam.c_ref, &cw.c_ref, cam.ctx.c_ref)); err != nil {
cw.Close()
return nil, nil, err
}
return cw, func() error {
return toError(C.gp_camera_set_config(cam.c_ref, cw.c_ref, cam.ctx.c_ref))
}, nil
}
func (cw *CameraWidget) GetID() (int, error) {
var c_id C.int
if err := toError(C.gp_widget_get_id(cw.c_ref, &c_id)); err != nil {
return 0, err
} else {
return int(c_id), nil
}
}
func (cw *CameraWidget) Parent() (*CameraWidget, error) {
cwParent := &CameraWidget{
cam: cw.cam,
}
if err := toError(C.gp_widget_get_parent(cw.c_ref, &cwParent.c_ref)); err != nil {
return nil, err
}
return cw, nil
}
func (cw *CameraWidget) Close() {
if err := toError(C.gp_widget_free(cw.c_ref)); err != nil {
panic(err)
}
}
func (cw *CameraWidget) Readonly() (bool, error) {
var c_ro C.int
if err := toError(C.gp_widget_get_readonly(cw.c_ref, &c_ro)); err != nil {
return false, err
}
return c_ro == 1, nil
}
func (cw *CameraWidget) Name() (string, error) {
var c_name *C.char
if err := toError(C.gp_widget_get_name(cw.c_ref, &c_name)); err != nil {
return "", err
} else {
//defer C.free(unsafe.Pointer(c_name))
}
return goString(c_name), nil
}
func (cw *CameraWidget) Label() (string, error) {
var c_name *C.char
if err := toError(C.gp_widget_get_label(cw.c_ref, &c_name)); err != nil {
return "", err
} else {
//defer C.free(unsafe.Pointer(c_name))
}
return goString(c_name), nil
}
/***************************************************************
** Widget Children
**/
func (cw *CameraWidget) ChildByName(name string) (*CameraWidget, error) {
cwChild := &CameraWidget{
cam: cw.cam,
}
c_name, c_name_free := cString(name)
defer c_name_free()
if err := toError(C.gp_widget_get_child_by_name(cw.c_ref, c_name, &cwChild.c_ref)); err != nil {
return nil, err
}
return cwChild, nil
}
func (cw *CameraWidget) ChildByLabel(label string) (*CameraWidget, error) {
cwChild := &CameraWidget{
cam: cw.cam,
}
c_label, c_label_free := cString(label)
defer c_label_free()
if err := toError(C.gp_widget_get_child_by_label(cw.c_ref, c_label, &cwChild.c_ref)); err != nil {
return nil, err
}
return cwChild, nil
}
func (cw *CameraWidget) ChildrenCount() (int, error) {
c_count := C.gp_widget_count_children(cw.c_ref)
if err := toError(c_count); err != nil {
return 0, err
} else {
return int(c_count), nil
}
}
func (cw *CameraWidget) Child(n int) (*CameraWidget, error) {
cwChild := &CameraWidget{
cam: cw.cam,
}
if err := toError(C.gp_widget_get_child(cw.c_ref, (C.int)(n), &cwChild.c_ref)); err != nil {
return nil, err
}
return cwChild, nil
}
func (cw *CameraWidget) ChildByID(id int) (*CameraWidget, error) {
cwChild := &CameraWidget{
cam: cw.cam,
}
if err := toError(C.gp_widget_get_child_by_id(cw.c_ref, (C.int)(id), &cwChild.c_ref)); err != nil {
return nil, err
}
return cwChild, nil
}
/***************************************************************
** Widget Value
**/
func (cw *CameraWidget) value(val unsafe.Pointer) error {
if err := toError(C.gp_widget_get_value(cw.c_ref, val)); err != nil {
return err
}
return nil
}
func (cw *CameraWidget) ValueString() (string, error) {
var c_val *C.char
if err := cw.value(unsafe.Pointer(&c_val)); err != nil {
return "", err
}
if c_val != nil {
// FIXME
// defer C.free(unsafe.Pointer(c_val))
}
return goString(c_val), nil
}
func (cw *CameraWidget) ValueInt() (int, error) {
var c_val C.int
if err := cw.value(unsafe.Pointer(&c_val)); err != nil {
return 0, err
}
return int(c_val), nil
}
func (cw *CameraWidget) ValueFloat() (float32, error) {
var c_val C.float
if err := cw.value(unsafe.Pointer(&c_val)); err != nil {
return 0, err
}
return float32(c_val), nil
}
func (cw *CameraWidget) ValueDate() (time.Time, error) {
var c_val C.int
if err := cw.value(unsafe.Pointer(&c_val)); err != nil {
return time.Time{}, err
}
return time.Unix(int64(c_val), 0), nil
}
func (cw *CameraWidget) ChoicesCount() (int, error) {
c_count := C.gp_widget_count_choices(cw.c_ref)
if err := toError(c_count); err != nil {
return 0, err
} else {
return int(c_count), nil
}
}
func (cw *CameraWidget) Choices() ([]string, error) {
choicesCount, err := cw.ChoicesCount()
if err != nil {
return nil, err
}
choices := make([]string, choicesCount)
for n := 0; n < choicesCount; n++ {
var c_val *C.char
if err := toError(C.gp_widget_get_choice(cw.c_ref, C.int(n), &c_val)); err != nil {
return nil, err
}
choices[n] = goString(c_val)
}
return choices, nil
}
func (cw *CameraWidget) setValue(val unsafe.Pointer) error {
if err := toError(C.gp_widget_set_value(cw.c_ref, val)); err != nil {
return err
}
return nil
}
func (cw *CameraWidget) SetValueString(val string) error {
c_val, c_val_free := cString(val)
defer c_val_free()
return cw.setValue(unsafe.Pointer(c_val))
}
func (cw *CameraWidget) SetValueInt(val int) error {
return cw.setValue(unsafe.Pointer(uintptr(C.int(val))))
}
func (cw *CameraWidget) SetValueFloat(val float32) error {
return cw.setValue(unsafe.Pointer(uintptr(C.float(val))))
}
func (cw *CameraWidget) SetValueDate(val time.Time) error {
return cw.setValue(unsafe.Pointer(uintptr(C.int(val.Unix()))))
}
/***************************************************************
** Widget Type
**/
type WidgetValueType string
const (
WidgetValueInvalid WidgetValueType = ""
WidgetValueString WidgetValueType = "string"
WidgetValueInt WidgetValueType = "int"
WidgetValueFloat WidgetValueType = "float"
WidgetValueDate WidgetValueType = "date"
WidgetValueWeird WidgetValueType = "weird"
)
type WidgetType string
const (
WidgetInvalid WidgetType = ""
// Window widget This is the toplevel configuration widget. It should likely contain multiple widget seciton entries
WidgetWindow WidgetType = "window"
// Section widget (think Tab)
WidgetSection WidgetType = "section"
// Text widget
WidgetText WidgetType = "text"
// Slider widget
WidgetRange WidgetType = "range"
// Toggle widget (think check box)
WidgetToggle WidgetType = "toggle"
// Radio button widget
WidgetRadio WidgetType = "radio"
// Menu widget (same as RADIO)
WidgetMenu WidgetType = "menu"
// Button press widget
WidgetButton WidgetType = "button"
// Date entering widget
WidgetDate WidgetType = "date"
)
func (cw *CameraWidget) Type() (WidgetType, WidgetValueType, error) {
var c_type C.CameraWidgetType
if err := toError(C.gp_widget_get_type(cw.c_ref, &c_type)); err != nil {
return WidgetInvalid, WidgetValueInvalid, err
}
switch c_type {
case C.GP_WIDGET_WINDOW:
return WidgetWindow, WidgetValueWeird, nil
case C.GP_WIDGET_SECTION:
return WidgetSection, WidgetValueWeird, nil
case C.GP_WIDGET_TEXT:
return WidgetText, WidgetValueString, nil
case C.GP_WIDGET_RANGE:
return WidgetRange, WidgetValueFloat, nil
case C.GP_WIDGET_TOGGLE:
return WidgetToggle, WidgetValueInt, nil
case C.GP_WIDGET_RADIO:
return WidgetRadio, WidgetValueString, nil
case C.GP_WIDGET_MENU:
return WidgetMenu, WidgetValueInt, nil
case C.GP_WIDGET_BUTTON:
return WidgetButton, WidgetValueInt, nil
case C.GP_WIDGET_DATE:
return WidgetDate, WidgetValueDate, nil
default:
return WidgetInvalid, WidgetValueInvalid, fmt.Errorf("unhandled widget type value: %d", int(c_type))
}
}