-
Notifications
You must be signed in to change notification settings - Fork 3
/
widget.go
275 lines (250 loc) · 6.74 KB
/
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
package gphoto2
/** \file
*
* \author Copyright 2020 Jon Molin
*
* \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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
// #cgo LDFLAGS: -L/usr/lib/x86_64-linux-gnu -lgphoto2 -lgphoto2_port
// #cgo CFLAGS: -I/usr/include
// #include <gphoto2/gphoto2.h>
// #include <gphoto2/gphoto2-setting.h>
// #include <stdlib.h>
import "C"
import (
"fmt"
"strings"
"unsafe"
)
type WidgetType string
type CameraWidget struct {
id int
label string
name string
info string
widgetType WidgetType
gpWidgetType uint
readonly bool
parent *CameraWidget
children []*CameraWidget
camera *Camera
}
func (w *CameraWidget) String() string {
res := fmt.Sprintf("%s %s %s ", w.name, w.label, w.widgetType)
if opts, err := w.Options(); err == nil {
res += " [ " + strings.Join(opts, " | ") + " ] "
}
if val, err := w.Get(); err == nil {
str, ok := val.(string)
if ok {
res += str + " "
}
}
return res
}
func (w *CameraWidget) Find(name string) *CameraWidget {
if name == "" {
return nil
}
if w.name == name {
return w
}
if w.children != nil && len(w.children) > 0 {
for _, c := range w.children {
if m := c.Find(name); m != nil {
return m
}
}
}
return nil
}
func (w CameraWidget) ID() int {
return w.id
}
func (w CameraWidget) Type() WidgetType {
return w.widgetType
}
func (w CameraWidget) Label() string {
return w.label
}
func (w CameraWidget) Name() string {
return w.name
}
func (w CameraWidget) Info() string {
return w.Info()
}
func (w CameraWidget) ReadOnly() bool {
return w.readonly
}
func (w CameraWidget) Parent() *CameraWidget {
return w.parent
}
func (w CameraWidget) Children() []*CameraWidget {
return w.children
}
func (w CameraWidget) saveSetting(gpCW *C.CameraWidget) error {
gpName := C.CString(w.name)
if res := C.gp_camera_set_single_config(w.camera.gpCamera, gpName, gpCW, w.camera.Ctx.gpContext); res != GPOK {
return newError("Could not set widget value with gp_camera_set_single_config", int(res))
}
return nil
}
func (w CameraWidget) Options() ([]string, error) {
switch w.widgetType {
case WidgetToggle:
return []string{"on", "off"}, nil
case WidgetRadio, WidgetMenu:
return w.radioOptions()
}
return nil, newError("Widget has no options", ErrorWidgetHasNoOptions)
}
func (w CameraWidget) radioOptions() ([]string, error) {
var gpWidget *C.CameraWidget
var err error
if gpWidget, err = w.camera.getChildWidget(&w.name); err != nil {
return nil, err
}
defer w.camera.freeChildWidget(gpWidget)
choicesList := []string{}
numChoices := C.gp_widget_count_choices(gpWidget)
for i := 0; i < int(numChoices); i++ {
var gpChoice *C.char
C.gp_widget_get_choice(gpWidget, C.int(i), (**C.char)(unsafe.Pointer(&gpChoice)))
choicesList = append(choicesList, C.GoString(gpChoice))
}
return choicesList, nil
}
func (w CameraWidget) Set(input interface{}) error {
if w.readonly {
return newError("Widget is readonly", ErrorReadOnly)
}
switch w.widgetType {
case WidgetRadio, WidgetMenu:
return w.setRadio(input.(string))
case WidgetText:
return w.setText(input.(string))
case WidgetToggle:
return w.setToggle(input.(bool))
}
return nil
}
// TODO: use interface and scrap the three set functions?
func (w CameraWidget) setToggle(input bool) error {
to := C.int(1)
if !input {
to = C.int(0)
}
var gpWidget *C.CameraWidget
var err error
if gpWidget, err = w.camera.getChildWidget(&w.name); err != nil {
return err
}
defer w.camera.freeChildWidget(gpWidget)
if res := C.gp_widget_set_value(gpWidget, unsafe.Pointer(&to)); res != GPOK {
return newError("Could not set widget value", int(res))
}
return w.saveSetting(gpWidget)
}
func (w CameraWidget) setText(input string) error {
var err error
gpText := C.CString(input)
defer C.free(unsafe.Pointer(gpText))
var gpWidget *C.CameraWidget
if gpWidget, err = w.camera.getChildWidget(&w.name); err != nil {
return err
}
defer w.camera.freeChildWidget(gpWidget)
if res := C.gp_widget_set_value(gpWidget, unsafe.Pointer(gpText)); res != GPOK {
return newError("Could not set widget value", int(res))
}
return w.saveSetting(gpWidget)
}
func (w CameraWidget) SetInt(input int) error {
var err error
gpInt := C.int(input)
var gpWidget *C.CameraWidget
if gpWidget, err = w.camera.getChildWidget(&w.name); err != nil {
return err
}
defer w.camera.freeChildWidget(gpWidget)
if res := C.gp_widget_set_value(gpWidget, unsafe.Pointer(&gpInt)); res != GPOK {
return newError("Could not set widget value", int(res))
}
return w.saveSetting(gpWidget)
}
func (w CameraWidget) setRadio(input string) error {
if choices, err := w.Options(); err == nil {
for _, item := range choices {
if item == input {
return w.setText(input)
}
}
return newError("Could not find provided value in alloved values list", ErrorWidgetIllegalOption)
} else {
return err
}
}
func (w CameraWidget) Get() (interface{}, error) {
var gpWidget *C.CameraWidget
var err error
if gpWidget, err = w.camera.getChildWidget(&w.name); err != nil {
return nil, err
}
defer w.camera.freeChildWidget(gpWidget)
switch w.widgetType {
case WidgetRadio, WidgetMenu, WidgetText:
var data *C.char
if res := C.gp_widget_get_value(gpWidget, (unsafe.Pointer(&data))); res != GPOK {
return nil, newError("Cannot read widget property value", int(res))
}
val := C.GoString(data)
return val, nil
case WidgetToggle:
var data int
if res := C.gp_widget_get_value(gpWidget, (unsafe.Pointer(&data))); res != GPOK {
return nil, newError("Cannot read widget property value", int(res))
}
val := (data == 1)
return val, nil
}
return nil, nil
}
func widgetType(gpWidgetType C.CameraWidgetType) WidgetType {
switch int(gpWidgetType) {
case gpWidgetButton:
return WidgetButton
case gpWidgetDate:
return WidgetDate
case gpWidgetMenu:
return WidgetMenu
case gpWidgetRadio:
return WidgetRadio
case gpWidgetRange:
return WidgetRange
case gpWidgetSection:
return WidgetSection
case gpWidgetText:
return WidgetText
case gpWidgetToggle:
return WidgetToggle
case gpWidgetWindow:
return WidgetWindow
}
panic("should not be here")
}