dmime: DirectMusicSegment doesn't supports COM aggregation.
[wine] / dlls / gphoto2.ds / gphoto2_main.c
1 /*
2  * SANE.DS functions
3  *
4  * Copyright 2000 Shi Quan He <shiquan@cyberdude.com>
5  * Copyright 2006 Marcus Meissner
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdlib.h>
30
31 #include "gphoto2_i.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(twain);
35
36
37 #ifdef HAVE_GPHOTO2
38 static char* GPHOTO2_StrDup(const char* str)
39 {
40     char* dst = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
41     strcpy(dst, str);
42     return dst;
43 }
44 #endif
45
46 static void
47 load_filesystem(const char *folder) {
48 #ifdef HAVE_GPHOTO2
49     int         i, count, ret;
50     CameraList  *list;
51
52     ret = gp_list_new (&list);
53     if (ret < GP_OK)
54         return;
55     ret = gp_camera_folder_list_files (activeDS.camera, folder, list, activeDS.context);
56     if (ret < GP_OK) {
57         gp_list_free (list);
58         return;
59     }
60     count = gp_list_count (list);
61     if (count < GP_OK) {
62         gp_list_free (list);
63         return;
64     }
65     for (i = 0; i < count; i++) {
66         const char *name;
67         struct gphoto2_file *gpfile;
68
69         ret = gp_list_get_name (list, i, &name);
70         if (ret < GP_OK)
71             continue;
72         gpfile = HeapAlloc(GetProcessHeap(), 0, sizeof(struct gphoto2_file)); /* FIXME: Leaked */
73         if (!gpfile)
74             continue;
75         TRACE("adding %s/%s\n", folder, name);
76         gpfile->folder = GPHOTO2_StrDup(folder);
77         gpfile->filename = GPHOTO2_StrDup(name);
78         gpfile->download = FALSE;
79         list_add_tail( &activeDS.files, &gpfile->entry );
80     }
81     gp_list_reset (list);
82
83     ret = gp_camera_folder_list_folders (activeDS.camera, folder, list, activeDS.context);
84     if (ret < GP_OK) {
85         FIXME("list_folders failed\n");
86         gp_list_free (list);
87         return;
88     }
89     count = gp_list_count (list);
90     if (count < GP_OK) {
91         FIXME("list_folders failed\n");
92         gp_list_free (list);
93         return;
94     }
95     for (i = 0; i < count; i++) {
96         const char *name;
97         char *newfolder;
98         ret = gp_list_get_name (list, i, &name);
99         if (ret < GP_OK)
100             continue;
101         TRACE("recursing into %s\n", name);
102         newfolder = HeapAlloc(GetProcessHeap(), 0, strlen(folder)+1+strlen(name)+1);
103         if (!strcmp(folder,"/"))
104             sprintf (newfolder, "/%s", name);
105         else
106             sprintf (newfolder, "%s/%s", folder, name);
107         load_filesystem (newfolder); /* recurse ... happily */
108     }
109     gp_list_free (list);
110 #endif
111 }
112
113 /* DG_CONTROL/DAT_CAPABILITY/MSG_GET */
114 static TW_UINT16 GPHOTO2_CapabilityGet (pTW_IDENTITY pOrigin, TW_MEMREF pData)
115 {
116     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
117
118     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GET\n");
119     if (activeDS.currentState < 4 || activeDS.currentState > 7) {
120         activeDS.twCC = TWCC_SEQERROR;
121         return TWRC_FAILURE;
122     }
123     activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GET);
124     return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
125 }
126
127 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT */
128 static TW_UINT16 GPHOTO2_CapabilityGetCurrent (pTW_IDENTITY pOrigin, TW_MEMREF pData)
129 {
130     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
131
132     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT\n");
133
134     if (activeDS.currentState < 4 || activeDS.currentState > 7) {
135         activeDS.twCC = TWCC_SEQERROR;
136         return TWRC_FAILURE;
137     }
138     activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GETCURRENT);
139     return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
140 }
141
142 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT */
143 static TW_UINT16 GPHOTO2_CapabilityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
144 {
145     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
146
147     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT\n");
148     if (activeDS.currentState < 4 || activeDS.currentState > 7) {
149         activeDS.twCC = TWCC_SEQERROR;
150         return TWRC_FAILURE;
151     }
152     activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GETDEFAULT);
153     return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
154 }
155
156 /* DG_CONTROL/DAT_CAPABILITY/MSG_QUERYSUPPORT */
157 static TW_UINT16 GPHOTO2_CapabilityQuerySupport (pTW_IDENTITY pOrigin,
158                                         TW_MEMREF pData)
159 {
160     FIXME ("stub!\n");
161
162     return TWRC_FAILURE;
163 }
164
165 /* DG_CONTROL/DAT_CAPABILITY/MSG_RESET */
166 static TW_UINT16 GPHOTO2_CapabilityReset (pTW_IDENTITY pOrigin,
167                                  TW_MEMREF pData)
168 {
169     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
170
171     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_RESET\n");
172
173     if (activeDS.currentState < 4 || activeDS.currentState > 7) {
174         activeDS.twCC = TWCC_SEQERROR;
175         return TWRC_FAILURE;
176     }
177     activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_RESET);
178     return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
179 }
180
181 /* DG_CONTROL/DAT_CAPABILITY/MSG_SET */
182 static TW_UINT16 GPHOTO2_CapabilitySet (pTW_IDENTITY pOrigin,
183                                TW_MEMREF pData)
184 {
185     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
186
187     TRACE ("DG_CONTROL/DAT_CAPABILITY/MSG_SET\n");
188
189     if (activeDS.currentState != 4) {
190         activeDS.twCC = TWCC_SEQERROR;
191         return TWRC_FAILURE;
192     }
193     activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_SET);
194     return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
195 }
196
197 /* DG_CONTROL/DAT_CUSTOMDSDATA/MSG_GET */
198 static TW_UINT16 GPHOTO2_CustomDSDataGet (pTW_IDENTITY pOrigin,
199                                 TW_MEMREF pData)
200 {
201     FIXME ("stub!\n");
202
203     return TWRC_FAILURE;
204 }
205
206 /* DG_CONTROL/DAT_CUSTOMDSDATA/MSG_SET */
207 static TW_UINT16 GPHOTO2_CustomDSDataSet (pTW_IDENTITY pOrigin,
208                                  TW_MEMREF pData)
209 {
210     FIXME ("stub!\n");
211
212     return TWRC_FAILURE;
213 }
214
215 /* DG_CONTROL/DAT_FILESYSTEM/MSG_CHANGEDIRECTORY */
216 static TW_UINT16 GPHOTO2_ChangeDirectory (pTW_IDENTITY pOrigin,
217                                  TW_MEMREF pData)
218 {
219     FIXME ("stub!\n");
220
221     return TWRC_FAILURE;
222 }
223
224 /* DG_CONTROL/DAT_FILESYSTEM/MSG_CREATEDIRECTORY */
225 static TW_UINT16 GPHOTO2_CreateDirectory (pTW_IDENTITY pOrigin,
226                                  TW_MEMREF pData)
227 {
228     FIXME ("stub!\n");
229
230     return TWRC_FAILURE;
231 }
232
233 /* DG_CONTROL/DAT_FILESYSTEM/MSG_DELETE */
234 static TW_UINT16 GPHOTO2_FileSystemDelete (pTW_IDENTITY pOrigin,
235                                   TW_MEMREF pData)
236 {
237     FIXME ("stub!\n");
238
239     return TWRC_FAILURE;
240 }
241
242 /* DG_CONTROL/DAT_FILESYSTEM/MSG_FORMATMEDIA */
243 static TW_UINT16 GPHOTO2_FormatMedia (pTW_IDENTITY pOrigin,
244                              TW_MEMREF pData)
245 {
246     FIXME ("stub!\n");
247
248     return TWRC_FAILURE;
249 }
250
251 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETCLOSE */
252 static TW_UINT16 GPHOTO2_FileSystemGetClose (pTW_IDENTITY pOrigin,
253                                     TW_MEMREF pData)
254 {
255     FIXME ("stub!\n");
256
257     return TWRC_FAILURE;
258 }
259
260 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETFIRSTFILE */
261 static TW_UINT16 GPHOTO2_FileSystemGetFirstFile (pTW_IDENTITY pOrigin,
262
263                                         TW_MEMREF pData)
264 {
265     FIXME ("stub!\n");
266
267     return TWRC_FAILURE;
268 }
269
270 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETINFO */
271 static TW_UINT16 GPHOTO2_FileSystemGetInfo (pTW_IDENTITY pOrigin,
272                                    TW_MEMREF pData)
273 {
274     FIXME ("stub!\n");
275
276     return TWRC_FAILURE;
277 }
278
279 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETNEXTFILE */
280 static TW_UINT16 GPHOTO2_FileSystemGetNextFile (pTW_IDENTITY pOrigin,
281
282                                        TW_MEMREF pData)
283 {
284     FIXME ("stub!\n");
285
286     return TWRC_FAILURE;
287 }
288
289 /* DG_CONTROL/DAT_FILESYSTEM/MSG_RENAME */
290 static TW_UINT16 GPHOTO2_FileSystemRename (pTW_IDENTITY pOrigin,
291                                   TW_MEMREF pData)
292 {
293     FIXME ("stub!\n");
294
295     return TWRC_FAILURE;
296 }
297
298 /* DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT */
299 static TW_UINT16 GPHOTO2_ProcessEvent (pTW_IDENTITY pOrigin,
300                               TW_MEMREF pData)
301 {
302     TW_UINT16 twRC = TWRC_SUCCESS;
303     pTW_EVENT pEvent = (pTW_EVENT) pData;
304
305     TRACE("DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT\n");
306
307     if (activeDS.currentState < 5 || activeDS.currentState > 7) {
308         activeDS.twCC = TWCC_SEQERROR;
309         return TWRC_FAILURE;
310     }
311
312     if (activeDS.pendingEvent.TWMessage != MSG_NULL) {
313         pEvent->TWMessage = activeDS.pendingEvent.TWMessage;
314         activeDS.pendingEvent.TWMessage = MSG_NULL;
315         twRC = TWRC_SUCCESS;
316     } else {
317         pEvent->TWMessage = MSG_NULL;  /* no message to the application */
318         twRC = TWRC_NOTDSEVENT;
319     }
320     activeDS.twCC = TWCC_SUCCESS;
321     return twRC;
322 }
323
324 /* DG_CONTROL/DAT_PASSTHRU/MSG_PASSTHRU */
325 static TW_UINT16 GPHOTO2_PassThrough (pTW_IDENTITY pOrigin,
326                              TW_MEMREF pData)
327 {
328     FIXME ("stub!\n");
329
330     return TWRC_FAILURE;
331 }
332
333 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER */
334 static TW_UINT16 GPHOTO2_PendingXfersEndXfer (pTW_IDENTITY pOrigin,
335                                      TW_MEMREF pData)
336 {
337     TW_UINT32 count;
338     pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
339     struct gphoto2_file *file;
340
341     TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER\n");
342
343     if (activeDS.currentState != 6 && activeDS.currentState != 7) {
344         activeDS.twCC = TWCC_SEQERROR;
345         return TWRC_FAILURE;
346     }
347     count = 0;
348     LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry ) {
349         if (file->download)
350             count++;
351     }
352     TRACE("count = %d\n", count);
353     pPendingXfers->Count = count;
354     if (pPendingXfers->Count != 0) {
355         activeDS.currentState = 6;
356     } else {
357         activeDS.currentState = 5;
358         /* Notify the application that it can close the data source */
359         activeDS.pendingEvent.TWMessage = MSG_CLOSEDSREQ;
360         /* close any Transferring dialog */
361         TransferringDialogBox(activeDS.progressWnd,-1);
362         activeDS.progressWnd = 0;
363     }
364     activeDS.twCC = TWCC_SUCCESS;
365     return TWRC_SUCCESS;
366 }
367
368 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_GET */
369 static TW_UINT16 GPHOTO2_PendingXfersGet (pTW_IDENTITY pOrigin,
370                                  TW_MEMREF pData)
371 {
372     TW_UINT32 count;
373     pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
374     struct gphoto2_file *file;
375
376     TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_GET\n");
377
378     if (activeDS.currentState < 4 || activeDS.currentState > 7) {
379         activeDS.twCC = TWCC_SEQERROR;
380         return TWRC_FAILURE;
381     }
382
383     count = 0;
384     LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry ) {
385         if (file->download)
386             count++;
387     }
388     TRACE("count = %d\n", count);
389     pPendingXfers->Count = count;
390     activeDS.twCC = TWCC_SUCCESS;
391     return TWRC_SUCCESS;
392 }
393
394 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET */
395 static TW_UINT16 GPHOTO2_PendingXfersReset (pTW_IDENTITY pOrigin,
396                                    TW_MEMREF pData)
397 {
398     pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
399
400     TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET\n");
401
402     if (activeDS.currentState != 6) {
403         activeDS.twCC = TWCC_SEQERROR;
404         return TWRC_FAILURE;
405     }
406     pPendingXfers->Count = 0;
407     activeDS.currentState = 5;
408     activeDS.twCC = TWCC_SUCCESS;
409     return TWRC_SUCCESS;
410 }
411
412 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_GET */
413 static TW_UINT16 GPHOTO2_SetupFileXferGet (pTW_IDENTITY pOrigin,
414                                   TW_MEMREF pData)
415 {
416     FIXME ("stub!\n");
417
418     return TWRC_FAILURE;
419 }
420
421 /* DG_CONTROL/DAT_SETUPXFER/MSG_GETDEFAULT */
422 static TW_UINT16 GPHOTO2_SetupFileXferGetDefault (pTW_IDENTITY pOrigin,
423                                          TW_MEMREF pData)
424 {
425     FIXME ("stub!\n");
426
427     return TWRC_FAILURE;
428 }
429
430
431 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_RESET */
432 static TW_UINT16 GPHOTO2_SetupFileXferReset (pTW_IDENTITY pOrigin,
433                                     TW_MEMREF pData)
434 {
435     FIXME ("stub!\n");
436
437     return TWRC_FAILURE;
438 }
439
440 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_SET */
441 static TW_UINT16 GPHOTO2_SetupFileXferSet (pTW_IDENTITY pOrigin,
442                                   TW_MEMREF pData)
443 {
444     FIXME ("stub!\n");
445
446     return TWRC_FAILURE;
447 }
448
449 /* DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET */
450 static TW_UINT16 GPHOTO2_SetupMemXferGet (pTW_IDENTITY pOrigin,
451                                   TW_MEMREF pData)
452 {
453     pTW_SETUPMEMXFER  pSetupMemXfer = (pTW_SETUPMEMXFER)pData;
454
455     TRACE("DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET\n");
456     /* Guessing */
457     pSetupMemXfer->MinBufSize = 20000;
458     pSetupMemXfer->MaxBufSize = 80000;
459     pSetupMemXfer->Preferred = 40000;
460     return TWRC_SUCCESS;
461 }
462
463 /* DG_CONTROL/DAT_STATUS/MSG_GET */
464 static TW_UINT16 GPHOTO2_GetDSStatus (pTW_IDENTITY pOrigin,
465                              TW_MEMREF pData)
466 {
467     pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
468
469     TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
470     pSourceStatus->ConditionCode = activeDS.twCC;
471     /* Reset the condition code */
472     activeDS.twCC = TWCC_SUCCESS;
473     return TWRC_SUCCESS;
474 }
475
476 /* DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS */
477 static TW_UINT16 GPHOTO2_DisableDSUserInterface (pTW_IDENTITY pOrigin,
478                                         TW_MEMREF pData)
479 {
480     TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS\n");
481
482     if (activeDS.currentState != 5) {
483         activeDS.twCC = TWCC_SEQERROR;
484         return TWRC_FAILURE;
485     }
486     activeDS.currentState = 4;
487     activeDS.twCC = TWCC_SUCCESS;
488     return TWRC_SUCCESS;
489 }
490
491 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS */
492 static TW_UINT16 GPHOTO2_EnableDSUserInterface (pTW_IDENTITY pOrigin,
493                                        TW_MEMREF pData)
494 {
495     pTW_USERINTERFACE pUserInterface = (pTW_USERINTERFACE) pData;
496
497     load_filesystem("/");
498
499     TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS\n");
500     if (activeDS.currentState != 4) {
501         FIXME("Sequence error %d\n", activeDS.currentState);
502         activeDS.twCC = TWCC_SEQERROR;
503         return TWRC_FAILURE;
504     }
505     activeDS.hwndOwner = pUserInterface->hParent;
506     if (pUserInterface->ShowUI)
507     {
508         BOOL rc;
509         activeDS.currentState = 5; /* Transitions to state 5 */
510         rc = DoCameraUI();
511         if (!rc) {
512             activeDS.pendingEvent.TWMessage = MSG_CLOSEDSREQ;
513         } else {
514             /* FIXME: The GUI should have marked the files to download... */
515             activeDS.pendingEvent.TWMessage = MSG_XFERREADY;
516             activeDS.currentState = 6; /* Transitions to state 6 directly */
517         }
518     } else {
519         /* no UI will be displayed, so source is ready to transfer data */
520         activeDS.pendingEvent.TWMessage = MSG_XFERREADY;
521         activeDS.currentState = 6; /* Transitions to state 6 directly */
522     }
523     activeDS.hwndOwner = pUserInterface->hParent;
524     activeDS.twCC = TWCC_SUCCESS;
525     return TWRC_SUCCESS;
526 }
527
528 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY */
529 static TW_UINT16 GPHOTO2_EnableDSUIOnly (pTW_IDENTITY pOrigin,
530                                 TW_MEMREF pData)
531 {
532     TRACE("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY\n");
533
534     if (activeDS.currentState != 4) {
535         activeDS.twCC = TWCC_SEQERROR;
536         return TWRC_FAILURE;
537     }
538     /* FIXME: we should replace xscanimage with our own UI */
539     FIXME ("not implemented!\n");
540     activeDS.currentState = 5;
541     activeDS.twCC = TWCC_SUCCESS;
542     return TWRC_SUCCESS;
543 }
544
545 /* DG_CONTROL/DAT_XFERGROUP/MSG_GET */
546 static TW_UINT16 GPHOTO2_XferGroupGet (pTW_IDENTITY pOrigin,
547                               TW_MEMREF pData)
548 {
549     FIXME ("stub!\n");
550     return TWRC_FAILURE;
551 }
552
553 /* DG_CONTROL/DAT_XFERGROUP/MSG_SET */
554 static TW_UINT16 GPHOTO2_XferGroupSet (pTW_IDENTITY pOrigin,
555                                   TW_MEMREF pData)
556 {
557     FIXME ("stub!\n");
558     return TWRC_FAILURE;
559 }
560
561 HINSTANCE GPHOTO2_instance;
562
563 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
564 {
565     TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
566
567     switch (fdwReason)
568     {
569         case DLL_PROCESS_ATTACH:
570             GPHOTO2_instance = hinstDLL;
571             DisableThreadLibraryCalls(hinstDLL);
572 #ifdef HAVE_GPHOTO2
573             activeDS.context = gp_context_new ();
574 #endif
575             break;
576
577         case DLL_PROCESS_DETACH:
578             GPHOTO2_instance = NULL;
579             break;
580     }
581
582     return TRUE;
583 }
584
585 #ifdef HAVE_GPHOTO2
586 static TW_UINT16 GPHOTO2_GetIdentity( pTW_IDENTITY, pTW_IDENTITY);
587 static TW_UINT16 GPHOTO2_OpenDS( pTW_IDENTITY, pTW_IDENTITY);
588 #endif
589
590 static TW_UINT16 GPHOTO2_SourceControlHandler (
591    pTW_IDENTITY pOrigin,
592    TW_UINT16    DAT,
593    TW_UINT16    MSG,
594    TW_MEMREF    pData)
595 {
596     TW_UINT16 twRC = TWRC_SUCCESS;
597
598     switch (DAT)
599     {
600         case DAT_IDENTITY:
601             switch (MSG)
602             {
603                 case MSG_CLOSEDS:
604 #ifdef HAVE_GPHOTO2
605                      if (activeDS.camera) {
606                         gp_camera_free (activeDS.camera);
607                         activeDS.camera = NULL;
608                      }
609 #endif
610                      break;
611                 case MSG_GET:
612 #ifdef HAVE_GPHOTO2
613                      twRC = GPHOTO2_GetIdentity(pOrigin,(pTW_IDENTITY)pData);
614 #else
615                      twRC = TWRC_FAILURE;
616 #endif
617                      break;
618                 case MSG_OPENDS:
619 #ifdef HAVE_GPHOTO2
620                      twRC = GPHOTO2_OpenDS(pOrigin,(pTW_IDENTITY)pData);
621 #else
622                      twRC = TWRC_FAILURE;
623 #endif
624                      break;
625             }
626             break;
627         case DAT_CAPABILITY:
628             switch (MSG)
629             {
630                 case MSG_GET:
631                     twRC = GPHOTO2_CapabilityGet (pOrigin, pData);
632                     break;
633                 case MSG_GETCURRENT:
634                     twRC = GPHOTO2_CapabilityGetCurrent (pOrigin, pData);
635                     break;
636                 case MSG_GETDEFAULT:
637                     twRC = GPHOTO2_CapabilityGetDefault (pOrigin, pData);
638                     break;
639                 case MSG_QUERYSUPPORT:
640                     twRC = GPHOTO2_CapabilityQuerySupport (pOrigin, pData);
641                     break;
642                 case MSG_RESET:
643                     twRC = GPHOTO2_CapabilityReset (pOrigin, pData);
644                     break;
645                 case MSG_SET:
646                     twRC = GPHOTO2_CapabilitySet (pOrigin, pData);
647                     break;
648                 default:
649                     twRC = TWRC_FAILURE;
650                     FIXME("unrecognized opertion triplet\n");
651             }
652             break;
653
654         case DAT_CUSTOMDSDATA:
655             switch (MSG)
656             {
657                 case MSG_GET:
658                     twRC = GPHOTO2_CustomDSDataGet (pOrigin, pData);
659                     break;
660                 case MSG_SET:
661                     twRC = GPHOTO2_CustomDSDataSet (pOrigin, pData);
662                     break;
663                 default:
664                     break;
665             }
666             break;
667
668         case DAT_FILESYSTEM:
669             switch (MSG)
670             {
671                 /*case MSG_AUTOMATICCAPTUREDIRECTORY:
672                     twRC = GPHOTO2_AutomaticCaptureDirectory
673                                (pOrigin, pData);
674                     break;*/
675                 case MSG_CHANGEDIRECTORY:
676                     twRC = GPHOTO2_ChangeDirectory (pOrigin, pData);
677                     break;
678                 /*case MSG_COPY:
679                     twRC = GPHOTO2_FileSystemCopy (pOrigin, pData);
680                     break;*/
681                 case MSG_CREATEDIRECTORY:
682                     twRC = GPHOTO2_CreateDirectory (pOrigin, pData);
683                     break;
684                 case MSG_DELETE:
685                     twRC = GPHOTO2_FileSystemDelete (pOrigin, pData);
686                     break;
687                 case MSG_FORMATMEDIA:
688                     twRC = GPHOTO2_FormatMedia (pOrigin, pData);
689                     break;
690                 case MSG_GETCLOSE:
691                     twRC = GPHOTO2_FileSystemGetClose (pOrigin, pData);
692                     break;
693                 case MSG_GETFIRSTFILE:
694                     twRC = GPHOTO2_FileSystemGetFirstFile (pOrigin, pData);
695                     break;
696                 case MSG_GETINFO:
697                     twRC = GPHOTO2_FileSystemGetInfo (pOrigin, pData);
698                     break;
699                 case MSG_GETNEXTFILE:
700                     twRC = GPHOTO2_FileSystemGetNextFile (pOrigin, pData);
701                     break;
702                 case MSG_RENAME:
703                     twRC = GPHOTO2_FileSystemRename (pOrigin, pData);
704                     break;
705                 default:
706                     twRC = TWRC_FAILURE;
707                     break;
708             }
709             break;
710
711         case DAT_EVENT:
712             if (MSG == MSG_PROCESSEVENT)
713                 twRC = GPHOTO2_ProcessEvent (pOrigin, pData);
714             else
715                 twRC = TWRC_FAILURE;
716             break;
717
718         case DAT_PASSTHRU:
719             if (MSG == MSG_PASSTHRU)
720                 twRC = GPHOTO2_PassThrough (pOrigin, pData);
721             else
722                 twRC = TWRC_FAILURE;
723             break;
724
725         case DAT_PENDINGXFERS:
726             switch (MSG)
727             {
728                 case MSG_ENDXFER:
729                     twRC = GPHOTO2_PendingXfersEndXfer (pOrigin, pData);
730                     break;
731                 case MSG_GET:
732                     twRC = GPHOTO2_PendingXfersGet (pOrigin, pData);
733                     break;
734                 case MSG_RESET:
735                     twRC = GPHOTO2_PendingXfersReset (pOrigin, pData);
736                     break;
737                 /*case MSG_STOPFEEDER:
738                     twRC = GPHOTO2_PendingXfersStopFeeder (pOrigin, pData);
739                     break;*/
740                 default:
741                     twRC = TWRC_FAILURE;
742             }
743             break;
744
745         case DAT_SETUPFILEXFER:
746             switch (MSG)
747             {
748                 case MSG_GET:
749                     twRC = GPHOTO2_SetupFileXferGet (pOrigin, pData);
750                     break;
751                 case MSG_GETDEFAULT:
752                     twRC = GPHOTO2_SetupFileXferGetDefault (pOrigin, pData);
753                     break;
754                 case MSG_RESET:
755                     twRC = GPHOTO2_SetupFileXferReset (pOrigin, pData);
756                     break;
757                 case MSG_SET:
758                     twRC = GPHOTO2_SetupFileXferSet (pOrigin, pData);
759                     break;
760                 default:
761                     twRC = TWRC_FAILURE;
762                     break;
763             }
764             break;
765
766         /*case DAT_SETUPFILEXFER2:
767             switch (MSG)
768             {
769                 case MSG_GET:
770                     twRC = GPHOTO2_SetupFileXfer2Get (pOrigin, pData);
771                     break;
772                 case MSG_GETDEFAULT:
773                     twRC = GPHOTO2_SetupFileXfer2GetDefault (pOrigin, pData);
774                     break;
775                 case MSG_RESET:
776                     twRC = GPHOTO2_SetupFileXfer2Reset (pOrigin, pData);
777                     break;
778                 case MSG_SET:
779                     twRC = GPHOTO2_SetupFileXfer2Set (pOrigin, pData);
780                     break;
781             }
782             break;*/
783         case DAT_SETUPMEMXFER:
784             if (MSG == MSG_GET)
785                 twRC = GPHOTO2_SetupMemXferGet (pOrigin, pData);
786             else
787                 twRC = TWRC_FAILURE;
788             break;
789
790         case DAT_STATUS:
791             if (MSG == MSG_GET)
792                 twRC = GPHOTO2_GetDSStatus (pOrigin, pData);
793             else
794                 twRC = TWRC_FAILURE;
795             break;
796
797         case DAT_USERINTERFACE:
798             switch (MSG)
799             {
800                 case MSG_DISABLEDS:
801                     twRC = GPHOTO2_DisableDSUserInterface (pOrigin, pData);
802                     break;
803                 case MSG_ENABLEDS:
804                     twRC = GPHOTO2_EnableDSUserInterface (pOrigin, pData);
805                     break;
806                 case MSG_ENABLEDSUIONLY:
807                     twRC = GPHOTO2_EnableDSUIOnly (pOrigin, pData);
808                     break;
809                 default:
810                     twRC = TWRC_FAILURE;
811                     break;
812             }
813             break;
814
815         case DAT_XFERGROUP:
816             switch (MSG)
817             {
818                 case MSG_GET:
819                     twRC = GPHOTO2_XferGroupGet (pOrigin, pData);
820                     break;
821                 case MSG_SET:
822                     twRC = GPHOTO2_XferGroupSet (pOrigin, pData);
823                     break;
824                 default:
825                     twRC = TWRC_FAILURE;
826                     break;
827             }
828             break;
829
830         default:
831             FIXME("code unknown: %d\n", DAT);
832             twRC = TWRC_FAILURE;
833             break;
834     }
835
836     return twRC;
837 }
838
839
840 static TW_UINT16 GPHOTO2_ImageGroupHandler (
841            pTW_IDENTITY pOrigin,
842            TW_UINT16    DAT,
843            TW_UINT16    MSG,
844            TW_MEMREF    pData)
845 {
846     TW_UINT16 twRC = TWRC_SUCCESS;
847
848     switch (DAT)
849     {
850         case DAT_CIECOLOR:
851             if (MSG == MSG_GET)
852                 twRC = GPHOTO2_CIEColorGet (pOrigin, pData);
853             else
854                 twRC = TWRC_FAILURE;
855             break;
856
857         case DAT_EXTIMAGEINFO:
858             if (MSG == MSG_GET)
859                 twRC = GPHOTO2_ExtImageInfoGet (pOrigin, pData);
860             else
861                 twRC = TWRC_FAILURE;
862             break;
863
864         case DAT_GRAYRESPONSE:
865             switch (MSG)
866             {
867                 case MSG_RESET:
868                     twRC = GPHOTO2_GrayResponseReset (pOrigin, pData);
869                     break;
870                 case MSG_SET:
871                     twRC = GPHOTO2_GrayResponseSet (pOrigin, pData);
872                     break;
873                 default:
874                     twRC = TWRC_FAILURE;
875                     activeDS.twCC = TWCC_BADPROTOCOL;
876                     FIXME("unrecognized operation triplet\n");
877                     break;
878             }
879             break;
880         case DAT_IMAGEFILEXFER:
881             if (MSG == MSG_GET)
882                 twRC = GPHOTO2_ImageFileXferGet (pOrigin, pData);
883             else
884                 twRC = TWRC_FAILURE;
885             break;
886
887         case DAT_IMAGEINFO:
888             if (MSG == MSG_GET)
889                 twRC = GPHOTO2_ImageInfoGet (pOrigin, pData);
890             else
891                 twRC = TWRC_FAILURE;
892             break;
893
894         case DAT_IMAGELAYOUT:
895             switch (MSG)
896             {
897                 case MSG_GET:
898                     twRC = GPHOTO2_ImageLayoutGet (pOrigin, pData);
899                     break;
900                 case MSG_GETDEFAULT:
901                     twRC = GPHOTO2_ImageLayoutGetDefault (pOrigin, pData);
902                     break;
903                 case MSG_RESET:
904                     twRC = GPHOTO2_ImageLayoutReset (pOrigin, pData);
905                     break;
906                 case MSG_SET:
907                     twRC = GPHOTO2_ImageLayoutSet (pOrigin, pData);
908                     break;
909                 default:
910                     twRC = TWRC_FAILURE;
911                     activeDS.twCC = TWCC_BADPROTOCOL;
912                     ERR("unrecognized operation triplet\n");
913                     break;
914             }
915             break;
916
917         case DAT_IMAGEMEMXFER:
918             if (MSG == MSG_GET)
919                 twRC = GPHOTO2_ImageMemXferGet (pOrigin, pData);
920             else
921                 twRC = TWRC_FAILURE;
922             break;
923
924         case DAT_IMAGENATIVEXFER:
925             if (MSG == MSG_GET)
926                 twRC = GPHOTO2_ImageNativeXferGet (pOrigin, pData);
927             else
928                 twRC = TWRC_FAILURE;
929             break;
930
931         case DAT_JPEGCOMPRESSION:
932             switch (MSG)
933             {
934                 case MSG_GET:
935                     twRC = GPHOTO2_JPEGCompressionGet (pOrigin, pData);
936                     break;
937                 case MSG_GETDEFAULT:
938                     twRC = GPHOTO2_JPEGCompressionGetDefault (pOrigin, pData);
939                     break;
940                 case MSG_RESET:
941                     twRC = GPHOTO2_JPEGCompressionReset (pOrigin, pData);
942                     break;
943                 case MSG_SET:
944                     twRC = GPHOTO2_JPEGCompressionSet (pOrigin, pData);
945                     break;
946                 default:
947                     twRC = TWRC_FAILURE;
948                     activeDS.twCC = TWCC_BADPROTOCOL;
949                     WARN("unrecognized operation triplet\n");
950                     break;
951             }
952             break;
953
954         case DAT_PALETTE8:
955             switch (MSG)
956             {
957                 case MSG_GET:
958                     twRC = GPHOTO2_Palette8Get (pOrigin, pData);
959                     break;
960                 case MSG_GETDEFAULT:
961                     twRC = GPHOTO2_Palette8GetDefault (pOrigin, pData);
962                     break;
963                 case MSG_RESET:
964                     twRC = GPHOTO2_Palette8Reset (pOrigin, pData);
965                     break;
966                 case MSG_SET:
967                     twRC = GPHOTO2_Palette8Set (pOrigin, pData);
968                     break;
969                 default:
970                     twRC = TWRC_FAILURE;
971                     activeDS.twCC = TWCC_BADPROTOCOL;
972                     WARN("unrecognized operation triplet\n");
973             }
974             break;
975
976         case DAT_RGBRESPONSE:
977             switch (MSG)
978             {
979                 case MSG_RESET:
980                     twRC = GPHOTO2_RGBResponseReset (pOrigin, pData);
981                     break;
982                 case MSG_SET:
983                     twRC = GPHOTO2_RGBResponseSet (pOrigin, pData);
984                     break;
985                 default:
986                     twRC = TWRC_FAILURE;
987                     activeDS.twCC = TWCC_BADPROTOCOL;
988                     WARN("unrecognized operation triplet\n");
989                     break;
990             }
991             break;
992
993         default:
994             twRC = TWRC_FAILURE;
995             activeDS.twCC = TWCC_BADPROTOCOL;
996             FIXME("unrecognized DG type %d\n", DAT);
997     }
998     return twRC;
999 }
1000
1001 /* Main entry point for the TWAIN library */
1002 TW_UINT16 WINAPI
1003 DS_Entry ( pTW_IDENTITY pOrigin,
1004            TW_UINT32    DG,
1005            TW_UINT16    DAT,
1006            TW_UINT16    MSG,
1007            TW_MEMREF    pData)
1008 {
1009     TW_UINT16 twRC = TWRC_SUCCESS;  /* Return Code */
1010
1011     TRACE("(DG=%d DAT=%d MSG=%d)\n", DG, DAT, MSG);
1012
1013     switch (DG)
1014     {
1015         case DG_CONTROL:
1016             twRC = GPHOTO2_SourceControlHandler (pOrigin,DAT,MSG,pData);
1017             break;
1018         case DG_IMAGE:
1019             twRC = GPHOTO2_ImageGroupHandler (pOrigin,DAT,MSG,pData);
1020             break;
1021         case DG_AUDIO:
1022             FIXME("The audio group of entry codes is not implemented.\n");
1023         default:
1024             activeDS.twCC = TWCC_BADPROTOCOL;
1025             twRC = TWRC_FAILURE;
1026     }
1027
1028     return twRC;
1029 }
1030
1031 #ifdef HAVE_GPHOTO2
1032 static GPPortInfoList *port_list;
1033 static int curcamera;
1034 static CameraList *detected_cameras;
1035 static CameraAbilitiesList *abilities_list;
1036
1037 static TW_UINT16
1038 gphoto2_auto_detect(void) {
1039     int result, count;
1040
1041     if (detected_cameras && (gp_list_count (detected_cameras) == 0)) {
1042         /* Reload if previously no cameras, we might detect new ones. */
1043         TRACE("Reloading portlist trying to detect cameras.\n");
1044         if (port_list) {
1045             gp_port_info_list_free (port_list);
1046             port_list = NULL;
1047         }
1048     }
1049     if (!port_list) {
1050         TRACE("Auto detecting gphoto cameras.\n");
1051         TRACE("Loading ports...\n");
1052         if (gp_port_info_list_new (&port_list) < GP_OK)
1053             return TWRC_FAILURE;
1054         result = gp_port_info_list_load (port_list);
1055         if (result < 0) {
1056             gp_port_info_list_free (port_list);
1057             return TWRC_FAILURE;
1058         }
1059         count = gp_port_info_list_count (port_list);
1060         if (count <= 0)
1061             return TWRC_FAILURE;
1062         if (gp_list_new (&detected_cameras) < GP_OK)
1063             return TWRC_FAILURE;
1064         if (!abilities_list) { /* Load only once per program start */
1065             gp_abilities_list_new (&abilities_list);
1066             TRACE("Loading cameras...\n");
1067             gp_abilities_list_load (abilities_list, NULL);
1068         }
1069         TRACE("Detecting cameras...\n");
1070         gp_abilities_list_detect (abilities_list, port_list, detected_cameras, NULL);
1071         curcamera = 0;
1072         TRACE("%d cameras detected\n", gp_list_count(detected_cameras));
1073     }
1074     return TWRC_SUCCESS;
1075 }
1076
1077 static TW_UINT16
1078 GPHOTO2_GetIdentity( pTW_IDENTITY pOrigin, pTW_IDENTITY self) {
1079     int count;
1080     const char *cname, *pname;
1081
1082     if (TWRC_SUCCESS != gphoto2_auto_detect())
1083         return TWRC_FAILURE;
1084
1085     count = gp_list_count (detected_cameras);
1086     if (count < GP_OK) {
1087         gp_list_free (detected_cameras);
1088         return TWRC_FAILURE;
1089     }
1090     TRACE("%d cameras detected.\n", count);
1091     self->ProtocolMajor = TWON_PROTOCOLMAJOR;
1092     self->ProtocolMinor = TWON_PROTOCOLMINOR;
1093     lstrcpynA (self->Manufacturer, "The Wine Team", sizeof(self->Manufacturer) - 1);
1094     lstrcpynA (self->ProductFamily, "GPhoto2 Camera", sizeof(self->ProductFamily) - 1);
1095
1096     if (!count) { /* No camera detected. But we need to return an IDENTITY anyway. */
1097         lstrcpynA (self->ProductName, "GPhoto2 Camera", sizeof(self->ProductName) - 1);
1098         return TWRC_SUCCESS;
1099     }
1100     gp_list_get_name  (detected_cameras, curcamera, &cname);
1101     gp_list_get_value (detected_cameras, curcamera, &pname);
1102     if (count == 1) /* Normal case, only one camera. */
1103         snprintf (self->ProductName, sizeof(self->ProductName), "%s", cname);
1104     else
1105         snprintf (self->ProductName, sizeof(self->ProductName), "%s@%s", cname, pname);
1106     curcamera = (curcamera+1) % count;
1107     return TWRC_SUCCESS;
1108 }
1109
1110 static TW_UINT16
1111 GPHOTO2_OpenDS( pTW_IDENTITY pOrigin, pTW_IDENTITY self) {
1112     int ret, m, p, count, i;
1113     CameraAbilities a;
1114     GPPortInfo info;
1115     const char  *model, *port;
1116
1117     if (TWRC_SUCCESS != gphoto2_auto_detect())
1118         return TWRC_FAILURE;
1119
1120     if (lstrcmpA(self->ProductFamily,"GPhoto2 Camera")) {
1121         FIXME("identity passed is not a gphoto camera, but %s!?!\n", self->ProductFamily);
1122         return TWRC_FAILURE;
1123     }
1124     count = gp_list_count (detected_cameras);
1125     if (!count) {
1126         ERR("No camera found by autodetection. Returning failure.\n");
1127         return TWRC_FAILURE;
1128     }
1129
1130     if (!lstrcmpA (self->ProductName, "GPhoto2 Camera")) {
1131         TRACE("Potential undetected camera. Just using the first autodetected one.\n");
1132         i = 0;
1133     } else {
1134         for (i=0;i<count;i++) {
1135             const char *cname, *pname;
1136             TW_STR32    name;
1137
1138             gp_list_get_name  (detected_cameras, i, &cname);
1139             gp_list_get_value (detected_cameras, i, &pname);
1140             if (!lstrcmpA(self->ProductName,cname))
1141                 break;
1142             snprintf(name, sizeof(name), "%s", cname);
1143             if (!lstrcmpA(self->ProductName,name))
1144                 break;
1145             snprintf(name, sizeof(name), "%s@%s", cname, pname);
1146             if (!lstrcmpA(self->ProductName,name))
1147                 break;
1148         }
1149         if (i == count) {
1150             TRACE("Camera %s not found in autodetected list. Using first entry.\n", self->ProductName);
1151             i=0;
1152         }
1153     }
1154     gp_list_get_name  (detected_cameras, i, &model);
1155     gp_list_get_value  (detected_cameras, i, &port);
1156     TRACE("model %s, port %s\n", model, port);
1157     ret = gp_camera_new (&activeDS.camera);
1158     if (ret < GP_OK) {
1159         ERR("gp_camera_new: %d\n", ret);
1160         return TWRC_FAILURE;
1161     }
1162     m = gp_abilities_list_lookup_model (abilities_list, model);
1163     if (m < GP_OK) {
1164         FIXME("Model %s not found, %d!\n", model, m);
1165         return TWRC_FAILURE;
1166     }
1167     ret = gp_abilities_list_get_abilities (abilities_list, m, &a);
1168     if (ret < GP_OK) {
1169         FIXME("gp_camera_list_get_abilities failed? %d\n", ret);
1170         return TWRC_FAILURE;
1171     }
1172     ret = gp_camera_set_abilities (activeDS.camera, a);
1173     if (ret < GP_OK) {
1174         FIXME("gp_camera_set_abilities failed? %d\n", ret);
1175         return TWRC_FAILURE;
1176     }
1177
1178     p = gp_port_info_list_lookup_path (port_list, port);
1179     if (p < GP_OK) {
1180         FIXME("port %s not in portlist?\n", port);
1181         return TWRC_FAILURE;
1182     }
1183     ret = gp_port_info_list_get_info (port_list, p, &info);
1184     if (ret < GP_OK) {
1185         FIXME("could not get portinfo for port %s?\n", port);
1186         return TWRC_FAILURE;
1187     }
1188     ret = gp_camera_set_port_info (activeDS.camera, info);
1189     if (ret < GP_OK) {
1190         FIXME("could not set portinfo for port %s to camera?\n", port);
1191         return TWRC_FAILURE;
1192     }
1193     list_init( &(activeDS.files) );
1194     activeDS.currentState = 4;
1195     activeDS.twCC               = TWRC_SUCCESS;
1196     activeDS.pixelflavor        = TWPF_CHOCOLATE;
1197     activeDS.pixeltype          = TWPT_RGB;
1198     activeDS.capXferMech        = TWSX_MEMORY;
1199     TRACE("OK!\n");
1200     return TWRC_SUCCESS;
1201 }
1202 #endif