gphoto4ruby 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,66 @@
1
+ /**
2
+ *
3
+ * Copyright 2008 neq4 company <http://neq4.com>
4
+ * Author: Sergey Kruk <sergey.kruk@gmail.com>
5
+ *
6
+ * This file is part of GPhoto4Ruby.
7
+ *
8
+ * GPhoto4Ruby is free software: you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation, either
11
+ * version 3 of the License, or (at your option) any later version.
12
+ *
13
+ * GPhoto4Ruby is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with GPhoto4Ruby. If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+ #include <stdlib.h>
24
+ #include <stdio.h>
25
+ #include <fcntl.h>
26
+ #include <string.h>
27
+ #include <gphoto2/gphoto2.h>
28
+ #include <ruby.h>
29
+ #include "gphoto2camera_event.h"
30
+ #include "gphoto2camera_utilities.h"
31
+
32
+ #ifndef _INC_CAMERA
33
+ #define _INC_CAMERA
34
+
35
+ VALUE rb_mGPhoto2;
36
+ VALUE rb_cGPhoto2Camera;
37
+
38
+ VALUE rb_cGPhoto2ConfigurationError;
39
+
40
+ void camera_mark(GPhoto2Camera *c);
41
+ void camera_free(GPhoto2Camera *c);
42
+ VALUE camera_allocate(VALUE klass);
43
+
44
+ VALUE camera_initialize(int argc, VALUE *argv, VALUE self);
45
+ VALUE camera_class_ports(VALUE klass);
46
+
47
+ VALUE camera_capture(int argc, VALUE *argv, VALUE self);
48
+
49
+ VALUE camera_save(int argc, VALUE *argv, VALUE self);
50
+ VALUE camera_delete(int argc, VALUE *argv, VALUE self);
51
+
52
+ VALUE camera_get_config(VALUE self);
53
+ VALUE camera_config_merge(VALUE self, VALUE hash);
54
+
55
+ VALUE camera_get_value(int argc, VALUE *argv, VALUE self);
56
+ VALUE camera_set_value(VALUE self, VALUE str, VALUE newVal);
57
+
58
+ VALUE camera_folder(VALUE self);
59
+ VALUE camera_subfolders(VALUE self);
60
+ VALUE camera_files(VALUE self);
61
+ VALUE camera_folder_up(VALUE self);
62
+ VALUE camera_folder_down(VALUE self, VALUE folder);
63
+
64
+ VALUE camera_wait(int argc, VALUE *argv, VALUE self);
65
+
66
+ #endif /* _INC_CAMERA */
@@ -0,0 +1,94 @@
1
+ /**
2
+ *
3
+ * Copyright 2008 neq4 company <http://neq4.com>
4
+ * Author: Sergey Kruk <sergey.kruk@gmail.com>
5
+ *
6
+ * This file is part of GPhoto4Ruby.
7
+ *
8
+ * GPhoto4Ruby is free software: you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation, either
11
+ * version 3 of the License, or (at your option) any later version.
12
+ *
13
+ * GPhoto4Ruby is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with GPhoto4Ruby. If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+ #include "gphoto2camera_event.h"
24
+
25
+ /*
26
+ * call-seq:
27
+ * type => string
28
+ *
29
+ * Returns type of event. Can be compared to EVENT_TYPE class constants
30
+ *
31
+ * Examples:
32
+ *
33
+ * c = GPhoto2::Camera.new
34
+ * # capture the image manually
35
+ * evt = c.wait
36
+ * evt.type #=> "file added"
37
+ * evt.type == GPhoto2::CameraEvent::EVENT_TYPE_FILE_ADDED
38
+ * #=> true
39
+ * evt.file #=> "DSC_0384.JPG"
40
+ *
41
+ * # do nothing
42
+ * c.wait(1).type #=> "timeout"
43
+ */
44
+ VALUE camera_event_type(VALUE self) {
45
+ GPhoto2CameraEvent *ce;
46
+
47
+ Data_Get_Struct(self, GPhoto2CameraEvent, ce);
48
+
49
+ switch (ce->type) {
50
+ case GP_EVENT_FILE_ADDED:
51
+ return EVENT_FILE_ADDED;
52
+ case GP_EVENT_FOLDER_ADDED:
53
+ return EVENT_FOLDER_ADDED;
54
+ case GP_EVENT_TIMEOUT:
55
+ return EVENT_TIMEOUT;
56
+ case GP_EVENT_UNKNOWN:
57
+ return EVENT_UNKNOWN;
58
+ default:
59
+ return Qnil;
60
+ }
61
+ }
62
+
63
+ /*
64
+ * call-seq:
65
+ * file => string or nil
66
+ *
67
+ * Returns file name of manually captured image. Only applies to
68
+ * EVENT_TYPE_FILE_ADDED event.
69
+ *
70
+ * Examples:
71
+ *
72
+ * c = GPhoto2::Camera.new
73
+ * # capture the image manually
74
+ * evt = c.wait
75
+ * evt.type #=> "file added"
76
+ * evt.type == GPhoto2::CameraEvent::EVENT_TYPE_FILE_ADDED
77
+ * #=> true
78
+ * evt.file #=> "DSC_0384.JPG"
79
+ *
80
+ * # do nothing
81
+ * c.wait(1).type #=> "timeout"
82
+ */
83
+ VALUE camera_event_file(VALUE self) {
84
+ GPhoto2CameraEvent *ce;
85
+
86
+ Data_Get_Struct(self, GPhoto2CameraEvent, ce);
87
+
88
+ if (ce->type == GP_EVENT_FILE_ADDED) {
89
+ return rb_str_new2(ce->path->name);
90
+ } else {
91
+ return Qnil;
92
+ }
93
+ }
94
+
@@ -0,0 +1,44 @@
1
+ /**
2
+ *
3
+ * Copyright 2008 neq4 company <http://neq4.com>
4
+ * Author: Sergey Kruk <sergey.kruk@gmail.com>
5
+ *
6
+ * This file is part of GPhoto4Ruby.
7
+ *
8
+ * GPhoto4Ruby is free software: you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation, either
11
+ * version 3 of the License, or (at your option) any later version.
12
+ *
13
+ * GPhoto4Ruby is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with GPhoto4Ruby. If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+ #include <gphoto2/gphoto2.h>
24
+ #include <ruby.h>
25
+
26
+ #ifndef _INC_CAMERA_EVENT
27
+ #define _INC_CAMERA_EVENT
28
+
29
+ typedef struct {
30
+ CameraEventType type;
31
+ CameraFilePath *path;
32
+ } GPhoto2CameraEvent;
33
+
34
+ VALUE rb_cGPhoto2CameraEvent;
35
+
36
+ #define EVENT_UNKNOWN rb_str_new2("unknown")
37
+ #define EVENT_TIMEOUT rb_str_new2("timeout")
38
+ #define EVENT_FILE_ADDED rb_str_new2("file added")
39
+ #define EVENT_FOLDER_ADDED rb_str_new2("folder added")
40
+
41
+ VALUE camera_event_type(VALUE self);
42
+ VALUE camera_event_file(VALUE self);
43
+
44
+ #endif /* _INC_CAMERA_EVENT */
@@ -0,0 +1,234 @@
1
+ /**
2
+ *
3
+ * Copyright 2008 neq4 company <http://neq4.com>
4
+ * Author: Sergey Kruk <sergey.kruk@gmail.com>
5
+ *
6
+ * This file is part of GPhoto4Ruby.
7
+ *
8
+ * GPhoto4Ruby is free software: you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation, either
11
+ * version 3 of the License, or (at your option) any later version.
12
+ *
13
+ * GPhoto4Ruby is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with GPhoto4Ruby. If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+ #include "gphoto2camera_utilities.h"
24
+
25
+ void rb_raise_gp_result(int retval) {
26
+ rb_raise(rb_cGPhoto2Exception, "LibGPhoto2 function returned: %s", gp_result_as_string(retval));
27
+ }
28
+
29
+ int gp_result_check(int retval) {
30
+ if (retval < GP_OK) {
31
+ rb_raise_gp_result(retval);
32
+ }
33
+ return retval;
34
+ }
35
+
36
+ VALUE getRadio(CameraWidget *cc) {
37
+ const char *val;
38
+
39
+ gp_result_check(gp_widget_get_value(cc, &val));
40
+ return rb_str_new2(val);
41
+ }
42
+
43
+ VALUE listRadio(CameraWidget *cc) {
44
+ int i, choicesTotal;
45
+ const char *choice;
46
+ VALUE arr;
47
+
48
+ choicesTotal = gp_result_check(gp_widget_count_choices(cc));
49
+ arr = rb_ary_new();
50
+ for (i = 0; i < choicesTotal; i++) {
51
+ gp_result_check(gp_widget_get_choice(cc, i, &choice));
52
+ rb_ary_push(arr, rb_str_new2(choice));
53
+ }
54
+ return arr;
55
+
56
+ VALUE setRadio(VALUE self, GPhoto2Camera *c, VALUE newVal, int save) {
57
+ const char *val;
58
+
59
+ val = RSTRING(rb_funcall(newVal, rb_intern("to_s"), 0))->ptr;
60
+
61
+ gp_result_check(gp_widget_set_value(c->childConfig, val));
62
+ if (save == 1) {
63
+ saveConfigs(self, c);
64
+ }
65
+ return newVal;
66
+ }
67
+
68
+ VALUE getText(CameraWidget *cc) {
69
+ const char *val;
70
+
71
+ gp_result_check(gp_widget_get_value(cc, &val));
72
+ return rb_str_new2(val);
73
+ }
74
+
75
+ VALUE setText(VALUE self, GPhoto2Camera *c, VALUE newVal, int save) {
76
+ const char *val;
77
+
78
+ val = RSTRING(rb_funcall(newVal, rb_intern("to_s"), 0))->ptr;
79
+
80
+ gp_result_check(gp_widget_set_value(c->childConfig, val));
81
+ if (save == 1) {
82
+ saveConfigs(self, c);
83
+ }
84
+ return newVal;
85
+ }
86
+
87
+ VALUE getRange(CameraWidget *cc) {
88
+ float val;
89
+ gp_result_check(gp_widget_get_value(cc, &val));
90
+ return rb_float_new(val);
91
+ }
92
+
93
+ VALUE listRange(CameraWidget *cc) {
94
+ float min, max, inc, i;
95
+ VALUE arr;
96
+
97
+ gp_result_check(gp_widget_get_range(cc, &min, &max, &inc));
98
+ arr = rb_ary_new();
99
+ if (inc > 0) {
100
+ for (i = min; i <= max; i = i + inc) {
101
+ rb_ary_push(arr, rb_float_new(i));
102
+ }
103
+ } else {
104
+ rb_ary_push(arr, rb_float_new(min));
105
+ }
106
+ return arr;
107
+ }
108
+
109
+ VALUE setRange(VALUE self, GPhoto2Camera *c, VALUE newNum, int save) {
110
+ float val;
111
+
112
+ val = NUM2DBL(rb_funcall(newNum, rb_intern("to_f"), 0));
113
+
114
+ gp_result_check(gp_widget_set_value(c->childConfig, &val));
115
+ if (save == 1) {
116
+ saveConfigs(self, c);
117
+ }
118
+ return newNum;
119
+ }
120
+
121
+ VALUE getToggle(CameraWidget *cc) {
122
+ int val;
123
+ gp_result_check(gp_widget_get_value(cc, &val));
124
+ if (val == 1) {
125
+ return Qtrue;
126
+ } else {
127
+ return Qfalse;
128
+ }
129
+ }
130
+
131
+ VALUE setToggle(VALUE self, GPhoto2Camera *c, VALUE newVal, int save) {
132
+ int val = -1;
133
+ char *nV;
134
+
135
+ switch(TYPE(newVal)) {
136
+ case T_TRUE:
137
+ val = 1;
138
+ break;
139
+ case T_FALSE:
140
+ val = 0;
141
+ break;
142
+ case T_SYMBOL:
143
+ nV = rb_id2name(rb_to_id(newVal));
144
+ if (strcmp(nV, "true") == 0) {
145
+ val = 1;
146
+ } else if(strcmp(nV, "false") == 0) {
147
+ val = 0;
148
+ }
149
+ }
150
+
151
+ if (val >= 0) {
152
+ gp_result_check(gp_widget_set_value(c->childConfig, &val));
153
+ if (save == 1) {
154
+ saveConfigs(self, c);
155
+ }
156
+ if (val == 1) {
157
+ return Qtrue;
158
+ } else {
159
+ return Qfalse;
160
+ }
161
+ } else {
162
+ return Qnil;
163
+ }
164
+ }
165
+
166
+ void saveConfigs(VALUE self, GPhoto2Camera *c) {
167
+ VALUE cfgs, cfg_changed, name;
168
+ CameraWidgetType widgettype;
169
+
170
+ gp_result_check(gp_camera_set_config(c->camera, c->config, c->context));
171
+ gp_result_check(gp_camera_get_config(c->camera, &(c->config), c->context));
172
+ cfg_changed = rb_iv_get(self, "@configs_changed");
173
+ cfgs = rb_iv_get(self, "@configuration");
174
+ name = rb_ary_shift(cfg_changed);
175
+ while (TYPE(name) != T_NIL) {
176
+ gp_result_check(gp_widget_get_child_by_name(c->config, RSTRING(name)->ptr, &(c->childConfig)));
177
+ gp_result_check(gp_widget_get_type(c->childConfig, &widgettype));
178
+ switch (widgettype) {
179
+ case GP_WIDGET_RADIO:
180
+ rb_hash_aset(cfgs, name, getRadio(c->childConfig));
181
+ break;
182
+ case GP_WIDGET_TEXT:
183
+ rb_hash_aset(cfgs, name, getText(c->childConfig));
184
+ break;
185
+ case GP_WIDGET_RANGE:
186
+ rb_hash_aset(cfgs, name, getRange(c->childConfig));
187
+ break;
188
+ case GP_WIDGET_TOGGLE:
189
+ rb_hash_aset(cfgs, name, getToggle(c->childConfig));
190
+ break;
191
+ default:
192
+ break;
193
+ }
194
+ name = rb_ary_shift(cfg_changed);
195
+ }
196
+ }
197
+
198
+ void populateWithConfigs(CameraWidget *cc, VALUE hash) {
199
+ int i, childrenTotal;
200
+ const char *name;
201
+ CameraWidget *child;
202
+ CameraWidgetType widgettype;
203
+
204
+ gp_result_check(gp_widget_get_type(cc, &widgettype));
205
+ switch (widgettype) {
206
+ case GP_WIDGET_RADIO:
207
+ gp_result_check(gp_widget_get_name(cc, &name));
208
+ rb_hash_aset(hash, rb_str_new2(name), getRadio(cc));
209
+ break;
210
+ case GP_WIDGET_TEXT:
211
+ gp_result_check(gp_widget_get_name(cc, &name));
212
+ rb_hash_aset(hash, rb_str_new2(name), getText(cc));
213
+ break;
214
+ case GP_WIDGET_RANGE:
215
+ gp_result_check(gp_widget_get_name(cc, &name));
216
+ rb_hash_aset(hash, rb_str_new2(name), getRange(cc));
217
+ break;
218
+ case GP_WIDGET_TOGGLE:
219
+ gp_result_check(gp_widget_get_name(cc, &name));
220
+ rb_hash_aset(hash, rb_str_new2(name), getToggle(cc));
221
+ break;
222
+ case GP_WIDGET_WINDOW:
223
+ case GP_WIDGET_SECTION:
224
+ childrenTotal = gp_result_check(gp_widget_count_children(cc));
225
+ for (i = 0; i < childrenTotal; i ++) {
226
+ gp_result_check(gp_widget_get_child(cc, i, &child));
227
+ populateWithConfigs(child, hash);
228
+ }
229
+ break;
230
+ default:
231
+ break;
232
+ }
233
+ }
234
+
@@ -0,0 +1,64 @@
1
+ /**
2
+ *
3
+ * Copyright 2008 neq4 company <http://neq4.com>
4
+ * Author: Sergey Kruk <sergey.kruk@gmail.com>
5
+ *
6
+ * This file is part of GPhoto4Ruby.
7
+ *
8
+ * GPhoto4Ruby is free software: you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation, either
11
+ * version 3 of the License, or (at your option) any later version.
12
+ *
13
+ * GPhoto4Ruby is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with GPhoto4Ruby. If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+ #include <stdlib.h>
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <gphoto2/gphoto2.h>
27
+ #include <ruby.h>
28
+
29
+ #ifndef _INC_CAMERA_UTILITIES
30
+ #define _INC_CAMERA_UTILITIES
31
+
32
+ typedef struct {
33
+ Camera *camera;
34
+ CameraWidget *config;
35
+ CameraWidget *childConfig;
36
+ CameraList *list;
37
+ CameraFilePath path;
38
+ CameraFile *file;
39
+ GPContext *context;
40
+
41
+ char *virtFolder;
42
+ } GPhoto2Camera;
43
+
44
+ VALUE rb_cGPhoto2Exception;
45
+
46
+ void rb_raise_gp_result(int retval);
47
+ int gp_result_check(int retval);
48
+
49
+ VALUE getRadio(CameraWidget *cc);
50
+ VALUE listRadio(CameraWidget *cc);
51
+ VALUE setRadio(VALUE self, GPhoto2Camera *c, VALUE newVal, int save);
52
+ VALUE getText(CameraWidget *cc);
53
+ VALUE setText(VALUE self, GPhoto2Camera *c, VALUE newVal, int save);
54
+ VALUE getRange(CameraWidget *cc);
55
+ VALUE listRange(CameraWidget *cc);
56
+ VALUE setRange(VALUE self, GPhoto2Camera *c, VALUE newNum, int save);
57
+ VALUE getToggle(CameraWidget *cc);
58
+ VALUE setToggle(VALUE self, GPhoto2Camera *c, VALUE newVal, int save);
59
+ void saveConfigs(VALUE self, GPhoto2Camera *c);
60
+
61
+ void populateWithConfigs(CameraWidget *cc, VALUE arr);
62
+
63
+ #endif /* _INC_CAMERA_UTILITIES */
64
+