summaryrefslogtreecommitdiff
path: root/camlibs/soundvision/soundvision.c
blob: 0aa34b69df5742217eed5dcbc4e4fe8d1dc03066 (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
/*
 * soundvision.c
 *
 * 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.
 *
 * 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.
 *
 * 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
 *
 *
 * For cameras using the Clarity2 chip made by Soundvision Inc.
 * Made entirely w/o Soundvision's help.  They would not give
 * documentation.  Reverse-engineered from usb-traces.
 *
 * Copyright 2002-2003 Vince Weaver <vince@deater.net>
 *
 * Originally based on the digita driver
 * Copyright 1999-2000 Johannes Erdfelt, VA Linux Systems
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

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

#include "libgphoto2/i18n.h"

#include "soundvision.h"


#define GP_MODULE "soundvision"

static const struct {
   char *name;
   unsigned short idVendor;
   unsigned short idProduct;
   char serial;
} models[] = {
        {"Agfa:ePhoto CL18",0x06bd,0x0403,0},

        {"Mustek:gSmart 350",0x055f,0xa350,0},

        {"RCA:CDS1005",0x0784,0x0100,0},
        {"Ixla:DualCam 640",0x0784,0x0100,0},

        {"Pretec:dc530",0x0784,0x5300,0},

        {"Generic SoundVision:Clarity2",0x0919,0x100,0},
        {"Argus:DC-2200",0x0919,0x100,0},
        {"CoolCam:CP086",0x0919,0x100,0},
        {"DigitalDream:l'elite",0x0919,0x0100,0},
        {"FujiFilm:@xia ix-100",0x0919,0x0100,0},
        {"Media-Tech:mt-406",0x0919,0x0100,0},
        {"Oregon Scientific:DShot II",0x0919,0x0100,0},
        {"Oregon Scientific:DShot III",0x0919,0x0100,0},
        {"Scott:APX 30",0x0919,0x0100,0},
        {"StarCam:CP086",0x0919,0x100,0},
        {"Tiger:Fast Flicks",0x0919,0x0100,0},
        {NULL,0,0,0}
};

int camera_id(CameraText *id) {

    strcpy(id->text, "soundvision");

    return GP_OK;
}


int camera_abilities(CameraAbilitiesList *list) {

    int i;
    CameraAbilities a;

    for(i=0; models[i].name; i++) {
       memset(&a, 0, sizeof(a));
       strcpy(a.model, models[i].name);

          /* Agfa and Dshot types owned by author and tested */
          /* Others the testing not quite certain            */
       if ( (models[i].idVendor==0x06bd) || (models[i].idVendor==0x0919))
          a.status = GP_DRIVER_STATUS_PRODUCTION;
       else
          a.status = GP_DRIVER_STATUS_EXPERIMENTAL;

          /* For now all are USB.  Have the spec for Serial */
          /* If ever one shows up                           */
       a.port       = GP_PORT_USB;
       a.speed[0] = 0;
       a.usb_vendor = models[i].idVendor;
       a.usb_product= models[i].idProduct;

          /* All should support image capture */
       a.operations     = 	GP_OPERATION_CAPTURE_IMAGE;

          /* Folders not supported on any */
       a.folder_operations = 	GP_FOLDER_OPERATION_NONE;

       a.file_operations   = 	GP_FILE_OPERATION_PREVIEW |
				GP_FILE_OPERATION_DELETE;
         /* Dshot compat can upload files */
       if (models[i].idVendor == 0x0919) {
          a.file_operations|=GP_FOLDER_OPERATION_PUT_FILE;
       }


       gp_abilities_list_append(list, a);
    }
    return GP_OK;
}

static int camera_exit (Camera *camera, GPContext *context) {

    GP_DEBUG("MAKE ME GP_DEBUG Reset: %i  pics: %i  odd_command: %i\n",
	                          camera->pl->reset_times,
	                          camera->pl->num_pictures,
	                          camera->pl->odd_command);

       /* We _must_ reset an even number of times ?? */
       /* It's more complicated than that.  *sigh*   */
    if (camera->pl->reset_times==1) {
       soundvision_reset (camera->pl,NULL,NULL);
#if 0
          /* Odd number of pics */
       if (camera->pl->num_pictures%2==1) {
	  if (!camera->pl->odd_command)
	     soundvision_reset (camera->pl,NULL,NULL);
       }
       else {
	  if (camera->pl->odd_command)
	     soundvision_reset (camera->pl,NULL,NULL);
       }
#endif
    }


    if (camera->pl) {
	if (camera->pl->file_list) {
	    free(camera->pl->file_list);
	    camera->pl->file_list = NULL;
	}
	free(camera->pl);
	camera->pl = NULL;
    }

    return GP_OK;
}

static int file_list_func (CameraFilesystem *fs, const char *folder,
			   CameraList *list, void *data, GPContext *context) {

    Camera *camera=data;
    int i;
    char temp_file[14];

    GP_DEBUG ("camera_file_list %s\n", folder);

    if (soundvision_get_file_list(camera->pl) < 0) {
       GP_DEBUG ("Could not soundvision_file_list!");
       return GP_ERROR;
    }

    for(i=0; i < camera->pl->num_pictures; i++) {
       strncpy(temp_file,camera->pl->file_list+(13*i),12);
       temp_file[12]=0;
       gp_list_append (list, temp_file, NULL);
    }
    return GP_OK;
}


static int soundvision_file_get (Camera *camera, const char *filename, int thumbnail,
			    unsigned char **data, int *size) {

    int buflen,throwaway,result;

    if (thumbnail) GP_DEBUG( "Getting thumbnail '%s'...",filename);
    else GP_DEBUG( "Getting file '%s'...",filename);

    if (camera->pl->device_type==SOUNDVISION_TIGERFASTFLICKS) {
       result=tiger_set_pc_mode(camera->pl);
       if (thumbnail) buflen=soundvision_get_thumb_size(camera->pl,filename);
       else buflen=soundvision_get_pic_size(camera->pl,filename);
       if (buflen < 0) return buflen;
    } else {
       soundvision_reset(camera->pl,NULL,NULL);

          /* Always have to check num photos,
	   * then pic size no matter what.  Otherwise
	   * the camera will stop responding
	   */
       throwaway=soundvision_photos_taken(camera->pl);
       if (throwaway<0) {
	  result=throwaway;
	  goto file_get_error;
       }


          /* The below two lines might look wrong, but they aren't! */
       buflen = soundvision_get_pic_size(camera->pl,filename);
       if (thumbnail) buflen=soundvision_get_thumb_size(camera->pl,filename);
       if (buflen < 0) return buflen;
    }




       /* Don't try to download if size equals zero! */
    if (buflen) {

       *data = malloc(buflen+1);

       if (!*data) {
	  result=GP_ERROR_NO_MEMORY;
	  goto file_get_error;
       }

       memset(*data, 0, buflen);

       if (thumbnail) {
          result=soundvision_get_thumb(camera->pl, filename, *data, buflen);
          if (result < 0) {
	     GP_DEBUG("soundvision_get_thumb_failed!");
	     goto file_get_error;
	  }
       }
       else {
          result=soundvision_get_pic(camera->pl, filename, *data, buflen);
          if (result < 0) {
	     GP_DEBUG("soundvision_get_pic_failed!");
             goto file_get_error;
	  }
       }

       if (size)
          *size = buflen;
    }

    return GP_OK;

file_get_error:

    if (*data!=NULL) free(*data);
    return result;
}

static int get_file_func (CameraFilesystem *fs, const char *folder,
			  const char *filename, CameraFileType type,
			  CameraFile *file, void *user_data,
			  GPContext *context) {
    char *pos;

    Camera *camera = user_data;
    unsigned char *data = NULL;
    int size,ret;

    switch (type) {
       case GP_FILE_TYPE_NORMAL:
               ret=soundvision_file_get(camera, filename, 0, &data, &size);
               if (ret<0) return ret;
               break;
       case GP_FILE_TYPE_PREVIEW:
               ret=soundvision_file_get(camera, filename, 1, &data, &size);
               if (ret<0) return ret;
               break;
       default:
               return GP_ERROR_NOT_SUPPORTED;
    }

    if (!data) return GP_ERROR;

    gp_file_set_data_and_size (file, (char *)data, size);
       /* Maybe skip below if EXIF data present? */

       /* As far as I know we only support JPG and MOV */
       /* Maybe some have MP3???                       */
    pos=strchr (filename, '.');
    if (pos) {
       if ((!strcmp(pos,".JPG")) || ((!strcmp(pos,".jpg"))))
	  gp_file_set_mime_type (file, GP_MIME_JPEG);
       else if (!strcmp(pos,".MOV"))
	  gp_file_set_mime_type (file, GP_MIME_QUICKTIME);
       else
	  gp_file_set_mime_type (file, GP_MIME_UNKNOWN);
    }

    return GP_OK;
}

static int camera_summary(Camera *camera, CameraText *summary,
			  GPContext *context) {

    char revision[9];

    revision[0]=0;
    soundvision_reset(camera->pl,revision,NULL);

    if (camera->pl->device_type==SOUNDVISION_TIGERFASTFLICKS) {
       int mem_total,mem_free,num_pics, ret;

       ret = tiger_get_mem(camera->pl,&num_pics,&mem_total,&mem_free);
       if (ret < GP_OK)
           return ret;

       sprintf(summary->text, _("Firmware Revision: %8s\n"
				"Pictures:     %i\n"
				"Memory Total: %ikB\n"
				"Memory Free:  %ikB\n"),
				revision,num_pics,mem_total,mem_free);
    } else {
	sprintf(summary->text, _("Firmware Revision: %8s"), revision);
    }
    return GP_OK;
}

static int camera_about(Camera *camera, CameraText *about, GPContext *context) {

    strcpy(about->text, _("Soundvision Driver\nVince Weaver <vince@deater.net>\n"));

    return GP_OK;
}


    /* Below contributed by Ben Hague <benhague@btinternet.com> */
static int camera_capture (Camera *camera, CameraCaptureType type,
			   CameraFilePath *path, GPContext *context) {

    int result;

    if (camera->pl->device_type==SOUNDVISION_AGFACL18)
       result=agfa_capture(camera->pl,path);
    else if (camera->pl->device_type==SOUNDVISION_TIGERFASTFLICKS) {
       result=tiger_capture(camera->pl,path);
    }
    else return GP_ERROR_NOT_SUPPORTED;

    if (result < GP_OK)
       return result;

    soundvision_get_file_list(camera->pl);

       /* For some reason last taken picture is first on tiger? */
       /* Might be last on Agfa.  Who knows.  Craziness.        */
    if (camera->pl->num_pictures<1) return GP_ERROR;

    strcpy (path->name,camera->pl->file_list);
    strcpy (path->folder, "/");
/*    gp_filesystem_append (camera->fs, path->folder,
			  path->name, context);*/
    return GP_OK;
}


static int delete_file_func (CameraFilesystem *fs, const char *folder,
			     const char *filename, void *data,
			     GPContext *context) {

    Camera *camera = data;

    GP_DEBUG("Deleting '%s' in '%s'...",filename,folder);

    soundvision_delete_picture(camera->pl,filename);

       /* Update our file list */
    if (soundvision_get_file_list(camera->pl)<0) return GP_ERROR;

    return GP_OK;
}


static int put_file_func (CameraFilesystem *fs, const char *folder, const char *filename,
			  CameraFileType type, CameraFile *file, void *data, GPContext *context) {

    Camera *camera=data;
    const char *data_file;
    long unsigned int data_size;

    /*
     * Upload the file to the camera. Use gp_file_get_data_and_size, etc.
    */
    GP_DEBUG ("*** put_file_func");
    GP_DEBUG ("*** folder: %s", folder);
    GP_DEBUG ("*** filename: %s", filename);

    gp_file_get_data_and_size (file, &data_file, &data_size);
    if ( data_size == 0) {
       gp_context_error (context,
			 _("The file to be uploaded has a null length"));
       return GP_ERROR_BAD_PARAMETERS;
    }

    /* Should check memory here */

   /*        if (available_memory < data_size) {
       gp_context_error (context,
			 _("Not enough memory available on the memory card"));
       return GP_ERROR_NO_MEMORY;
     }
   */

    tiger_upload_file (camera->pl, filename,data_file,data_size);

    return GP_OK;
}

static CameraFilesystemFuncs fsfuncs = {
	.file_list_func = file_list_func,
	.get_file_func = get_file_func,
	.put_file_func = put_file_func,
	.del_file_func = delete_file_func,
};

int camera_init(Camera *camera, GPContext *context) {

    GPPortSettings settings;
    CameraAbilities a;
    int ret = 0;

       /* First, set up all the function pointers */
    camera->functions->exit         = camera_exit;
    camera->functions->summary      = camera_summary;
    camera->functions->about        = camera_about;
    camera->functions->capture      = camera_capture;

    GP_DEBUG ("Initializing the camera\n");

    switch (camera->port->type) {
       case GP_PORT_USB:
            ret=gp_port_get_settings(camera->port,&settings);
            if (ret<0) return ret;

            /* Use the defaults the core parsed */

            ret=gp_port_set_settings(camera->port,settings);
            if (ret<0) return ret;
            break;

       case GP_PORT_SERIAL:
            return GP_ERROR_IO_SUPPORTED_SERIAL;
       default:
            return GP_ERROR_NOT_SUPPORTED;
    }



       /* Set up camera private library */
    camera->pl = malloc (sizeof (CameraPrivateLibrary));
    if (!camera->pl) return (GP_ERROR_NO_MEMORY);
    memset (camera->pl, 0, sizeof (CameraPrivateLibrary));
    camera->pl->gpdev = camera->port;


       /* Set up the sub-architectures */
       /* Default to Agfa.  Should we default otherwise? */
    camera->pl->device_type=SOUNDVISION_AGFACL18;
    gp_camera_get_abilities (camera, &a);

    if ((a.usb_vendor==0x0919) && (a.usb_product==0x0100)) {
       camera->pl->device_type=SOUNDVISION_TIGERFASTFLICKS;
    }

    if ((a.usb_vendor==0x0784) && (a.usb_product==0x0100)) {
       camera->pl->device_type=SOUNDVISION_IXLA;
    }





       /* Keep track.  We _must_ reset an even number of times */
    camera->pl->reset_times=0;
    camera->pl->odd_command=0;

       /* Reset the camera */
    ret = soundvision_reset (camera->pl,NULL,NULL);
    if (ret < 0) {
	free (camera->pl);
	camera->pl = NULL;
	return (ret);
    }
    /* Tell the CameraFilesystem where to get lists from */
    return gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
}