Debian Codegraph @

camlibs/kodak/dc120/dc120.c

raw
1 /* dc120.c
2 *
3 * Copyright (C) Scott Fritzinger
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 */
20
21 #define _DARWIN_C_SOURCE
22 #define _DEFAULT_SOURCE
23 #define _POSIX_C_SOURCE 199309L
24
25 #include "config.h"
26
27 /* Must be included before including unistd.h to define _XOPEN_SOURCE
28 before unistd.h on FreeBSD. */
29 #include <gphoto2/gphoto2-port-portability.h>
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36
37 #include <gphoto2/gphoto2.h>
38 #include <gphoto2/gphoto2-port.h>
39
40 #include "libgphoto2/i18n.h"
41
42 #include "dc120.h"
43 #include "library.h"
44
45
46 #define CHECK_RESULT(result) {int r = (result); if (r < 0) return (r);}
47
48 int camera_id (CameraText *id) {
49 strcpy(id->text, "kodak-dc120");
50 return (GP_OK);
51 }
52
53 int camera_abilities (CameraAbilitiesList *list) {
54
55 CameraAbilities a;
56
57 memset (&a, 0, sizeof(a));
58 strcpy(a.model, "Kodak:DC120");
59 a.status = GP_DRIVER_STATUS_PRODUCTION;
60 a.port = GP_PORT_SERIAL;
61 a.speed[0] = 9600;
62 a.speed[1] = 19200;
63 a.speed[2] = 38400;
64 a.speed[3] = 57600;
65 a.speed[4] = 115200;
66 a.speed[5] = 0;
67 a.operations = GP_OPERATION_CAPTURE_IMAGE;
68 a.file_operations = GP_FILE_OPERATION_DELETE |
69 GP_FILE_OPERATION_PREVIEW;
70 a.folder_operations = GP_FOLDER_OPERATION_NONE;
71
72 gp_abilities_list_append(list, a);
73
74 return (GP_OK);
75 }
76
77 /** Parses a path for a folder and returns folder number and card */
78 static int find_folder( Camera *camera, const char *folder,
79 int *from_card, int *folder_nr, GPContext *context)
80 {
81 CameraList *albums = NULL;
82 const char* album_name;
83 size_t folder_len;
84 int i;
85 char *dc120_folder_card = _("CompactFlash Card");
86
87 if (folder[0] != '/') {
88 return (GP_ERROR);
89 }
90
91 folder++;
92
93 if (folder[0] == '\0') {
94 /* From memory */
95 *from_card = FALSE;
96 *folder_nr = 0;
97 return (GP_OK);
98 } else if (strncmp(folder, dc120_folder_card, strlen(dc120_folder_card))==0) {
99 /* From card */
100 *from_card = TRUE;
101 folder += strlen(dc120_folder_card);
102 } else {
103 /* Subfolder in memory */
104 *from_card = FALSE;
105 folder--; /* step back to slash */
106 }
107
108 if ((folder[0] == 0) ||
109 (folder[0] == '/' && folder[1] == 0)) { /* ok, finished */
110 *folder_nr = 0;
111 return (GP_OK);
112 } else if (folder[0] != '/')
113 return (GP_ERROR);
114
115 folder++; /* remove slash */
116
117 /* Have trailing slash */
118 folder_len = strlen(folder);
119 if (folder[folder_len-1] == '/') {
120 folder_len--;
121 }
122
123 /* ok, now we have a album. first get all albums */
124 if (gp_list_new(&albums) != (GP_OK)) {
125 return (GP_ERROR);
126 }
127
128 if (dc120_get_albums(camera, *from_card, albums, context) != (GP_OK)) {
129 gp_list_free(albums);
130 return (GP_ERROR);
131 }
132
133 /* no check if such a album exist */
134 for (i = 0; i<gp_list_count(albums); i++)
135 {
136 gp_list_get_name(albums, i, &album_name);
137 if (strlen(album_name) == folder_len &&
138 strncmp(album_name, folder, folder_len) == 0)
139 {
140 *folder_nr = i+1;
141 gp_list_free(albums); /* ok, we found it. */
142 return (GP_OK);
143 }
144 }
145
146 /* oh, we did not find the folder. bummer. */
147 gp_list_free(albums);
148 return (GP_ERROR);
149 }
150
151 static int folder_list_func (CameraFilesystem *fs, const char *folder,
152 CameraList *list, void *data, GPContext *context)
153 {
154 int res;
155 int from_card;
156 int folder_nr;
157 Camera *camera = data;
158 char *dc120_folder_card = _("CompactFlash Card");
159
160 res = find_folder(camera, folder, &from_card, &folder_nr, context);
161 if (res != (GP_OK)) {
162 return res;
163 }
164
165 if (!from_card && folder_nr==0) {
166 gp_list_append(list, dc120_folder_card, NULL);
167 return (dc120_get_albums(camera, from_card, list, context));
168 } else if (from_card && folder_nr==0) {
169 return (dc120_get_albums(camera, from_card, list, context));
170 } else {
171 return (GP_OK);
172 }
173 }
174
175 static int file_list_func (CameraFilesystem *fs, const char *folder,
176 CameraList *list, void *data, GPContext *context)
177 {
178 int res;
179 int from_card;
180 int folder_nr;
181 Camera *camera = data;
182
183 res = find_folder(camera, folder, &from_card, &folder_nr, context);
184 if (res != (GP_OK)) {
185 return res;
186 }
187
188 return dc120_get_filenames(camera, from_card, folder_nr, list, context);
189
190 /* Save the order of the pics (wtf: no filename access on dc120???) */
191 }
192
193 static int camera_file_action (Camera *camera, int action, CameraFile *file,
194 const char *folder, const char *filename,
195 GPContext *context)
196 {
197 CameraList *files = NULL;
198 const char* file_name;
199 int file_nr;
200 int i;
201 char *dot;
202 int picnum=0;
203 int result = GP_OK;
204
205 /* first find the file */
206 int from_card;
207 int folder_nr;
208
209 result = find_folder( camera, folder, &from_card, &folder_nr, context );
210 if( result != (GP_OK) ) {
211 return result;
212 }
213
214 result = gp_list_new( &files );
215 if( result != GP_OK ) {
216 goto fail;
217 }
218
219 result = dc120_get_filenames(camera, from_card, folder_nr, files, context);
220 if( result != GP_OK ) {
221 goto fail;
222 }
223
224
225 /* now we have the list, search for the file. */
226 file_nr = -1;
227 for( i = 0; i<gp_list_count( files ); i++ ) {
228 gp_list_get_name( files, i, &file_name );
229 if( strcmp( file_name, filename ) == 0 ) {
230 file_nr = i; /* ok, we found it. */
231 break;
232 }
233 }
234 gp_list_free( files );
235
236
237 if( file_nr == -1 ) { /* not found */
238 return GP_ERROR;
239 }
240
241
242 picnum = gp_filesystem_number(camera->fs, folder, filename, context);
243 if (picnum < 0)
244 return picnum;
245
246
247 if (action == DC120_ACTION_PREVIEW) { /* FIXME: marcus, fix type */
248 dot = strrchr(filename, '.');
249 if( dot && strlen( dot )>3 ) {
250 strcpy( dot+1, "ppm");
251 }
252 }
253 return (dc120_file_action(camera, action, from_card, folder_nr, picnum+1, file, context));
254 /* yes, after that it is to handle failures. */
255 fail:
256 if (files)
257 gp_list_free( files );
258 return result;
259
260 }
261
262
263 static int get_file_func (CameraFilesystem *fs, const char *folder,
264 const char *filename, CameraFileType type,
265 CameraFile *file, void *data, GPContext *context)
266 {
267 Camera *camera = data;
268
269 switch (type) {
270 case GP_FILE_TYPE_NORMAL:
271 return (camera_file_action (camera, DC120_ACTION_IMAGE, file,
272 folder, filename, context));
273 case GP_FILE_TYPE_PREVIEW:
274 return (camera_file_action (camera, DC120_ACTION_PREVIEW, file,
275 folder, filename, context));
276 default:
277 return (GP_ERROR_NOT_SUPPORTED);
278 }
279 }
280
281 static int delete_file_func (CameraFilesystem *fs, const char *folder,
282 const char *filename, void *data,
283 GPContext *context)
284 {
285 Camera *camera = data;
286 int retval;
287
288 retval = camera_file_action (camera, DC120_ACTION_DELETE, NULL,
289 folder, filename, context);
290
291 return (retval);
292 }
293
294 static int camera_capture (Camera *camera, CameraCaptureType type, CameraFilePath *path, GPContext *context) {
295 CameraList *list;
296 int count;
297 const char* name;
298
299 if (type != GP_CAPTURE_IMAGE)
300 return (GP_ERROR_NOT_SUPPORTED);
301
302 /* capture a image to flash (note: we do not check if full */
303 CHECK_RESULT (dc120_capture(camera, path, context));
304
305 /* Get the last picture in the Flash memory */
306 gp_list_new(&list);
307
308 dc120_get_filenames (camera, 0, 0, list, context);
309
310 count = gp_list_count(list);
311 gp_list_get_name (list, count - 1, &name);
312 gp_list_free(list);
313
314 /* Set the filename */
315 strcpy(path->folder, "/");
316
317 CHECK_RESULT (gp_filesystem_append (camera->fs,
318 path->folder,
319 path->name, context));
320 return (GP_OK);
321
322 }
323
324 static int camera_summary (Camera *camera, CameraText *summary, GPContext *context)
325 {
326 static char summary_string[2048] = "";
327 char buff[1024];
328 Kodak_dc120_status status;
329
330 if (dc120_get_status (camera, &status, context))
331 {
332 strcpy(summary_string, "Kodak DC120\n");
333
334 snprintf(buff, 1024, "Camera Identification: %s\n", status.camera_id);
335 strcat(summary_string, buff);
336
337 snprintf(buff, 1024, "Camera Type: %d\n", status.camera_type_id);
338 strcat(summary_string, buff);
339
340 snprintf(buff, 1024, "Firmware: %d.%d\n", status.firmware_major, status.firmware_minor);
341 strcat(summary_string, buff);
342
343 snprintf(buff, 1024, "Battery Status: %d\n", status.batteryStatusId);
344 strcat(summary_string, buff);
345
346 snprintf(buff, 1024, "AC Status: %d\n", status.acStatusId);
347 strcat(summary_string, buff);
348
349 strftime(buff, 1024, "Time: %a, %d %b %Y %T\n", localtime((time_t *)&status.time));
350 strcat(summary_string, buff);
351
352 snprintf(buff, 1024, "Total Pictures Taken: %d\n",
353 status.taken_pict_mem + status.taken_pict_card);
354 strcat(summary_string, buff);
355
356 }
357
358 strcpy(summary->text, summary_string);
359
360 return (GP_OK);
361 }
362
363
364 static int camera_manual (Camera *camera, CameraText *manual, GPContext *context)
365 {
366 strcpy (manual->text,
367 _("The Kodak DC120 camera uses the KDC file format "
368 "for storing images. If you want to view the images you "
369 "download from your camera, you will need to download "
370 "the \"kdc2tiff\" program. "
371 "It is available from http://kdc2tiff.sourceforge.net"));
372
373 return (GP_OK);
374 }
375
376 static int camera_about (Camera *camera, CameraText *about, GPContext *context)
377 {
378 strcpy(about->text,
379 _("Kodak DC120 Camera Library\n"
380 "Scott Fritzinger <[email protected]>\n"
381 "Camera Library for the Kodak DC120 camera.\n"
382 "(by popular demand)."));
383
384 return (GP_OK);
385 }
386
387 static CameraFilesystemFuncs fsfuncs = {
388 .file_list_func = file_list_func,
389 .folder_list_func = folder_list_func,
390 .get_file_func = get_file_func,
391 .del_file_func = delete_file_func,
392 };
393
394 int camera_init (Camera *camera, GPContext *context) {
395
396 GPPortSettings settings;
397 int speed;
398
399 /* First, set up all the function pointers */
400 camera->functions->capture = camera_capture;
401 camera->functions->summary = camera_summary;
402 camera->functions->manual = camera_manual;
403 camera->functions->about = camera_about;
404
405 gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
406
407 /* Configure the port (remember the speed) */
408 gp_port_get_settings (camera->port, &settings);
409 speed = settings.serial.speed;
410 settings.serial.speed = 9600;
411 settings.serial.bits = 8;
412 settings.serial.parity = 0;
413 settings.serial.stopbits = 1;
414 gp_port_set_settings (camera->port, settings);
415 gp_port_set_timeout (camera->port, TIMEOUT);
416
417 /* Reset the camera to 9600 */
418 gp_port_send_break (camera->port, 2);
419
420 /* Wait for it to update */
421 usleep(1500 * 1000);
422
423 if (dc120_set_speed (camera, speed) == GP_ERROR) {
424 return (GP_ERROR);
425 }
426
427 /* Try to talk after speed change */
428 if (dc120_get_status(camera, NULL, context) == GP_ERROR) {
429 return (GP_ERROR);
430 }
431
432 return (GP_OK);
433 }