wined3d: Allow to apply extension emulation wrappers independently.
[wine.git] / dlls / gphoto2.ds / gphoto2_main.c
blob437709001a5d455e277dc428aa2387587fe01a40
1 /*
2 * SANE.DS functions
4 * Copyright 2000 Shi Quan He <shiquan@cyberdude.com>
5 * Copyright 2006 Marcus Meissner
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.
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.
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
22 #include "config.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdlib.h>
31 #include "gphoto2_i.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(twain);
37 #ifdef HAVE_GPHOTO2
38 static char* GPHOTO2_StrDup(const char* str)
40 char* dst = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
41 strcpy(dst, str);
42 return dst;
44 #endif
46 static void
47 load_filesystem(const char *folder) {
48 #ifdef HAVE_GPHOTO2
49 int i, count, ret;
50 CameraList *list;
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;
60 count = gp_list_count (list);
61 if (count < GP_OK) {
62 gp_list_free (list);
63 return;
65 for (i = 0; i < count; i++) {
66 const char *name;
67 struct gphoto2_file *gpfile;
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 );
81 gp_list_reset (list);
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;
89 count = gp_list_count (list);
90 if (count < GP_OK) {
91 FIXME("list_folders failed\n");
92 gp_list_free (list);
93 return;
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 */
109 gp_list_free (list);
110 #endif
113 /* DG_CONTROL/DAT_CAPABILITY/MSG_GET */
114 static TW_UINT16 GPHOTO2_CapabilityGet (pTW_IDENTITY pOrigin, TW_MEMREF pData)
116 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
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;
123 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GET);
124 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
127 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT */
128 static TW_UINT16 GPHOTO2_CapabilityGetCurrent (pTW_IDENTITY pOrigin, TW_MEMREF pData)
130 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
132 TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT\n");
134 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
135 activeDS.twCC = TWCC_SEQERROR;
136 return TWRC_FAILURE;
138 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GETCURRENT);
139 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
142 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT */
143 static TW_UINT16 GPHOTO2_CapabilityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
145 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
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;
152 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GETDEFAULT);
153 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
156 /* DG_CONTROL/DAT_CAPABILITY/MSG_QUERYSUPPORT */
157 static TW_UINT16 GPHOTO2_CapabilityQuerySupport (pTW_IDENTITY pOrigin,
158 TW_MEMREF pData)
160 FIXME ("stub!\n");
162 return TWRC_FAILURE;
165 /* DG_CONTROL/DAT_CAPABILITY/MSG_RESET */
166 static TW_UINT16 GPHOTO2_CapabilityReset (pTW_IDENTITY pOrigin,
167 TW_MEMREF pData)
169 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
171 TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_RESET\n");
173 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
174 activeDS.twCC = TWCC_SEQERROR;
175 return TWRC_FAILURE;
177 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_RESET);
178 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
181 /* DG_CONTROL/DAT_CAPABILITY/MSG_SET */
182 static TW_UINT16 GPHOTO2_CapabilitySet (pTW_IDENTITY pOrigin,
183 TW_MEMREF pData)
185 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
187 TRACE ("DG_CONTROL/DAT_CAPABILITY/MSG_SET\n");
189 if (activeDS.currentState != 4) {
190 activeDS.twCC = TWCC_SEQERROR;
191 return TWRC_FAILURE;
193 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_SET);
194 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
197 /* DG_CONTROL/DAT_CUSTOMDSDATA/MSG_GET */
198 static TW_UINT16 GPHOTO2_CustomDSDataGet (pTW_IDENTITY pOrigin,
199 TW_MEMREF pData)
201 FIXME ("stub!\n");
203 return TWRC_FAILURE;
206 /* DG_CONTROL/DAT_CUSTOMDSDATA/MSG_SET */
207 static TW_UINT16 GPHOTO2_CustomDSDataSet (pTW_IDENTITY pOrigin,
208 TW_MEMREF pData)
210 FIXME ("stub!\n");
212 return TWRC_FAILURE;
215 /* DG_CONTROL/DAT_FILESYSTEM/MSG_CHANGEDIRECTORY */
216 static TW_UINT16 GPHOTO2_ChangeDirectory (pTW_IDENTITY pOrigin,
217 TW_MEMREF pData)
219 FIXME ("stub!\n");
221 return TWRC_FAILURE;
224 /* DG_CONTROL/DAT_FILESYSTEM/MSG_CREATEDIRECTORY */
225 static TW_UINT16 GPHOTO2_CreateDirectory (pTW_IDENTITY pOrigin,
226 TW_MEMREF pData)
228 FIXME ("stub!\n");
230 return TWRC_FAILURE;
233 /* DG_CONTROL/DAT_FILESYSTEM/MSG_DELETE */
234 static TW_UINT16 GPHOTO2_FileSystemDelete (pTW_IDENTITY pOrigin,
235 TW_MEMREF pData)
237 FIXME ("stub!\n");
239 return TWRC_FAILURE;
242 /* DG_CONTROL/DAT_FILESYSTEM/MSG_FORMATMEDIA */
243 static TW_UINT16 GPHOTO2_FormatMedia (pTW_IDENTITY pOrigin,
244 TW_MEMREF pData)
246 FIXME ("stub!\n");
248 return TWRC_FAILURE;
251 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETCLOSE */
252 static TW_UINT16 GPHOTO2_FileSystemGetClose (pTW_IDENTITY pOrigin,
253 TW_MEMREF pData)
255 FIXME ("stub!\n");
257 return TWRC_FAILURE;
260 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETFIRSTFILE */
261 static TW_UINT16 GPHOTO2_FileSystemGetFirstFile (pTW_IDENTITY pOrigin,
263 TW_MEMREF pData)
265 FIXME ("stub!\n");
267 return TWRC_FAILURE;
270 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETINFO */
271 static TW_UINT16 GPHOTO2_FileSystemGetInfo (pTW_IDENTITY pOrigin,
272 TW_MEMREF pData)
274 FIXME ("stub!\n");
276 return TWRC_FAILURE;
279 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETNEXTFILE */
280 static TW_UINT16 GPHOTO2_FileSystemGetNextFile (pTW_IDENTITY pOrigin,
282 TW_MEMREF pData)
284 FIXME ("stub!\n");
286 return TWRC_FAILURE;
289 /* DG_CONTROL/DAT_FILESYSTEM/MSG_RENAME */
290 static TW_UINT16 GPHOTO2_FileSystemRename (pTW_IDENTITY pOrigin,
291 TW_MEMREF pData)
293 FIXME ("stub!\n");
295 return TWRC_FAILURE;
298 /* DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT */
299 static TW_UINT16 GPHOTO2_ProcessEvent (pTW_IDENTITY pOrigin,
300 TW_MEMREF pData)
302 TW_UINT16 twRC = TWRC_SUCCESS;
303 pTW_EVENT pEvent = (pTW_EVENT) pData;
305 TRACE("DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT\n");
307 if (activeDS.currentState < 5 || activeDS.currentState > 7) {
308 activeDS.twCC = TWCC_SEQERROR;
309 return TWRC_FAILURE;
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;
320 activeDS.twCC = TWCC_SUCCESS;
321 return twRC;
324 /* DG_CONTROL/DAT_PASSTHRU/MSG_PASSTHRU */
325 static TW_UINT16 GPHOTO2_PassThrough (pTW_IDENTITY pOrigin,
326 TW_MEMREF pData)
328 FIXME ("stub!\n");
330 return TWRC_FAILURE;
333 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER */
334 static TW_UINT16 GPHOTO2_PendingXfersEndXfer (pTW_IDENTITY pOrigin,
335 TW_MEMREF pData)
337 TW_UINT32 count;
338 pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
339 struct gphoto2_file *file;
341 TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER\n");
343 if (activeDS.currentState != 6 && activeDS.currentState != 7) {
344 activeDS.twCC = TWCC_SEQERROR;
345 return TWRC_FAILURE;
347 count = 0;
348 LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry ) {
349 if (file->download)
350 count++;
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;
364 activeDS.twCC = TWCC_SUCCESS;
365 return TWRC_SUCCESS;
368 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_GET */
369 static TW_UINT16 GPHOTO2_PendingXfersGet (pTW_IDENTITY pOrigin,
370 TW_MEMREF pData)
372 TW_UINT32 count;
373 pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
374 struct gphoto2_file *file;
376 TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_GET\n");
378 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
379 activeDS.twCC = TWCC_SEQERROR;
380 return TWRC_FAILURE;
383 count = 0;
384 LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry ) {
385 if (file->download)
386 count++;
388 TRACE("count = %d\n", count);
389 pPendingXfers->Count = count;
390 activeDS.twCC = TWCC_SUCCESS;
391 return TWRC_SUCCESS;
394 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET */
395 static TW_UINT16 GPHOTO2_PendingXfersReset (pTW_IDENTITY pOrigin,
396 TW_MEMREF pData)
398 pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
400 TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET\n");
402 if (activeDS.currentState != 6) {
403 activeDS.twCC = TWCC_SEQERROR;
404 return TWRC_FAILURE;
406 pPendingXfers->Count = 0;
407 activeDS.currentState = 5;
408 activeDS.twCC = TWCC_SUCCESS;
409 return TWRC_SUCCESS;
412 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_GET */
413 static TW_UINT16 GPHOTO2_SetupFileXferGet (pTW_IDENTITY pOrigin,
414 TW_MEMREF pData)
416 FIXME ("stub!\n");
418 return TWRC_FAILURE;
421 /* DG_CONTROL/DAT_SETUPXFER/MSG_GETDEFAULT */
422 static TW_UINT16 GPHOTO2_SetupFileXferGetDefault (pTW_IDENTITY pOrigin,
423 TW_MEMREF pData)
425 FIXME ("stub!\n");
427 return TWRC_FAILURE;
431 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_RESET */
432 static TW_UINT16 GPHOTO2_SetupFileXferReset (pTW_IDENTITY pOrigin,
433 TW_MEMREF pData)
435 FIXME ("stub!\n");
437 return TWRC_FAILURE;
440 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_SET */
441 static TW_UINT16 GPHOTO2_SetupFileXferSet (pTW_IDENTITY pOrigin,
442 TW_MEMREF pData)
444 FIXME ("stub!\n");
446 return TWRC_FAILURE;
449 /* DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET */
450 static TW_UINT16 GPHOTO2_SetupMemXferGet (pTW_IDENTITY pOrigin,
451 TW_MEMREF pData)
453 pTW_SETUPMEMXFER pSetupMemXfer = (pTW_SETUPMEMXFER)pData;
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;
463 /* DG_CONTROL/DAT_STATUS/MSG_GET */
464 static TW_UINT16 GPHOTO2_GetDSStatus (pTW_IDENTITY pOrigin,
465 TW_MEMREF pData)
467 pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
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;
476 /* DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS */
477 static TW_UINT16 GPHOTO2_DisableDSUserInterface (pTW_IDENTITY pOrigin,
478 TW_MEMREF pData)
480 TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS\n");
482 if (activeDS.currentState != 5) {
483 activeDS.twCC = TWCC_SEQERROR;
484 return TWRC_FAILURE;
486 activeDS.currentState = 4;
487 activeDS.twCC = TWCC_SUCCESS;
488 return TWRC_SUCCESS;
491 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS */
492 static TW_UINT16 GPHOTO2_EnableDSUserInterface (pTW_IDENTITY pOrigin,
493 TW_MEMREF pData)
495 pTW_USERINTERFACE pUserInterface = (pTW_USERINTERFACE) pData;
497 load_filesystem("/");
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;
505 activeDS.hwndOwner = pUserInterface->hParent;
506 if (pUserInterface->ShowUI)
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 */
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 */
523 activeDS.hwndOwner = pUserInterface->hParent;
524 activeDS.twCC = TWCC_SUCCESS;
525 return TWRC_SUCCESS;
528 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY */
529 static TW_UINT16 GPHOTO2_EnableDSUIOnly (pTW_IDENTITY pOrigin,
530 TW_MEMREF pData)
532 TRACE("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY\n");
534 if (activeDS.currentState != 4) {
535 activeDS.twCC = TWCC_SEQERROR;
536 return TWRC_FAILURE;
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;
545 /* DG_CONTROL/DAT_XFERGROUP/MSG_GET */
546 static TW_UINT16 GPHOTO2_XferGroupGet (pTW_IDENTITY pOrigin,
547 TW_MEMREF pData)
549 FIXME ("stub!\n");
550 return TWRC_FAILURE;
553 /* DG_CONTROL/DAT_XFERGROUP/MSG_SET */
554 static TW_UINT16 GPHOTO2_XferGroupSet (pTW_IDENTITY pOrigin,
555 TW_MEMREF pData)
557 FIXME ("stub!\n");
558 return TWRC_FAILURE;
561 HINSTANCE GPHOTO2_instance;
563 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
565 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
567 switch (fdwReason)
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;
578 return TRUE;
581 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
582 static TW_UINT16 GPHOTO2_GetIdentity( pTW_IDENTITY, pTW_IDENTITY);
583 static TW_UINT16 GPHOTO2_OpenDS( pTW_IDENTITY, pTW_IDENTITY);
584 #endif
586 static TW_UINT16 GPHOTO2_SourceControlHandler (
587 pTW_IDENTITY pOrigin,
588 TW_UINT16 DAT,
589 TW_UINT16 MSG,
590 TW_MEMREF pData)
592 TW_UINT16 twRC = TWRC_SUCCESS;
594 switch (DAT)
596 case DAT_IDENTITY:
597 switch (MSG)
599 case MSG_CLOSEDS:
600 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
601 if (activeDS.camera) {
602 gp_camera_free (activeDS.camera);
603 activeDS.camera = NULL;
605 #endif
606 break;
607 case MSG_GET:
608 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
609 twRC = GPHOTO2_GetIdentity(pOrigin,(pTW_IDENTITY)pData);
610 #else
611 twRC = TWRC_FAILURE;
612 #endif
613 break;
614 case MSG_OPENDS:
615 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
616 twRC = GPHOTO2_OpenDS(pOrigin,(pTW_IDENTITY)pData);
617 #else
618 twRC = TWRC_FAILURE;
619 #endif
620 break;
622 break;
623 case DAT_CAPABILITY:
624 switch (MSG)
626 case MSG_GET:
627 twRC = GPHOTO2_CapabilityGet (pOrigin, pData);
628 break;
629 case MSG_GETCURRENT:
630 twRC = GPHOTO2_CapabilityGetCurrent (pOrigin, pData);
631 break;
632 case MSG_GETDEFAULT:
633 twRC = GPHOTO2_CapabilityGetDefault (pOrigin, pData);
634 break;
635 case MSG_QUERYSUPPORT:
636 twRC = GPHOTO2_CapabilityQuerySupport (pOrigin, pData);
637 break;
638 case MSG_RESET:
639 twRC = GPHOTO2_CapabilityReset (pOrigin, pData);
640 break;
641 case MSG_SET:
642 twRC = GPHOTO2_CapabilitySet (pOrigin, pData);
643 break;
644 default:
645 twRC = TWRC_FAILURE;
646 FIXME("unrecognized operation triplet\n");
648 break;
650 case DAT_CUSTOMDSDATA:
651 switch (MSG)
653 case MSG_GET:
654 twRC = GPHOTO2_CustomDSDataGet (pOrigin, pData);
655 break;
656 case MSG_SET:
657 twRC = GPHOTO2_CustomDSDataSet (pOrigin, pData);
658 break;
659 default:
660 break;
662 break;
664 case DAT_FILESYSTEM:
665 switch (MSG)
667 /*case MSG_AUTOMATICCAPTUREDIRECTORY:
668 twRC = GPHOTO2_AutomaticCaptureDirectory
669 (pOrigin, pData);
670 break;*/
671 case MSG_CHANGEDIRECTORY:
672 twRC = GPHOTO2_ChangeDirectory (pOrigin, pData);
673 break;
674 /*case MSG_COPY:
675 twRC = GPHOTO2_FileSystemCopy (pOrigin, pData);
676 break;*/
677 case MSG_CREATEDIRECTORY:
678 twRC = GPHOTO2_CreateDirectory (pOrigin, pData);
679 break;
680 case MSG_DELETE:
681 twRC = GPHOTO2_FileSystemDelete (pOrigin, pData);
682 break;
683 case MSG_FORMATMEDIA:
684 twRC = GPHOTO2_FormatMedia (pOrigin, pData);
685 break;
686 case MSG_GETCLOSE:
687 twRC = GPHOTO2_FileSystemGetClose (pOrigin, pData);
688 break;
689 case MSG_GETFIRSTFILE:
690 twRC = GPHOTO2_FileSystemGetFirstFile (pOrigin, pData);
691 break;
692 case MSG_GETINFO:
693 twRC = GPHOTO2_FileSystemGetInfo (pOrigin, pData);
694 break;
695 case MSG_GETNEXTFILE:
696 twRC = GPHOTO2_FileSystemGetNextFile (pOrigin, pData);
697 break;
698 case MSG_RENAME:
699 twRC = GPHOTO2_FileSystemRename (pOrigin, pData);
700 break;
701 default:
702 twRC = TWRC_FAILURE;
703 break;
705 break;
707 case DAT_EVENT:
708 if (MSG == MSG_PROCESSEVENT)
709 twRC = GPHOTO2_ProcessEvent (pOrigin, pData);
710 else
711 twRC = TWRC_FAILURE;
712 break;
714 case DAT_PASSTHRU:
715 if (MSG == MSG_PASSTHRU)
716 twRC = GPHOTO2_PassThrough (pOrigin, pData);
717 else
718 twRC = TWRC_FAILURE;
719 break;
721 case DAT_PENDINGXFERS:
722 switch (MSG)
724 case MSG_ENDXFER:
725 twRC = GPHOTO2_PendingXfersEndXfer (pOrigin, pData);
726 break;
727 case MSG_GET:
728 twRC = GPHOTO2_PendingXfersGet (pOrigin, pData);
729 break;
730 case MSG_RESET:
731 twRC = GPHOTO2_PendingXfersReset (pOrigin, pData);
732 break;
733 /*case MSG_STOPFEEDER:
734 twRC = GPHOTO2_PendingXfersStopFeeder (pOrigin, pData);
735 break;*/
736 default:
737 twRC = TWRC_FAILURE;
739 break;
741 case DAT_SETUPFILEXFER:
742 switch (MSG)
744 case MSG_GET:
745 twRC = GPHOTO2_SetupFileXferGet (pOrigin, pData);
746 break;
747 case MSG_GETDEFAULT:
748 twRC = GPHOTO2_SetupFileXferGetDefault (pOrigin, pData);
749 break;
750 case MSG_RESET:
751 twRC = GPHOTO2_SetupFileXferReset (pOrigin, pData);
752 break;
753 case MSG_SET:
754 twRC = GPHOTO2_SetupFileXferSet (pOrigin, pData);
755 break;
756 default:
757 twRC = TWRC_FAILURE;
758 break;
760 break;
762 /*case DAT_SETUPFILEXFER2:
763 switch (MSG)
765 case MSG_GET:
766 twRC = GPHOTO2_SetupFileXfer2Get (pOrigin, pData);
767 break;
768 case MSG_GETDEFAULT:
769 twRC = GPHOTO2_SetupFileXfer2GetDefault (pOrigin, pData);
770 break;
771 case MSG_RESET:
772 twRC = GPHOTO2_SetupFileXfer2Reset (pOrigin, pData);
773 break;
774 case MSG_SET:
775 twRC = GPHOTO2_SetupFileXfer2Set (pOrigin, pData);
776 break;
778 break;*/
779 case DAT_SETUPMEMXFER:
780 if (MSG == MSG_GET)
781 twRC = GPHOTO2_SetupMemXferGet (pOrigin, pData);
782 else
783 twRC = TWRC_FAILURE;
784 break;
786 case DAT_STATUS:
787 if (MSG == MSG_GET)
788 twRC = GPHOTO2_GetDSStatus (pOrigin, pData);
789 else
790 twRC = TWRC_FAILURE;
791 break;
793 case DAT_USERINTERFACE:
794 switch (MSG)
796 case MSG_DISABLEDS:
797 twRC = GPHOTO2_DisableDSUserInterface (pOrigin, pData);
798 break;
799 case MSG_ENABLEDS:
800 twRC = GPHOTO2_EnableDSUserInterface (pOrigin, pData);
801 break;
802 case MSG_ENABLEDSUIONLY:
803 twRC = GPHOTO2_EnableDSUIOnly (pOrigin, pData);
804 break;
805 default:
806 twRC = TWRC_FAILURE;
807 break;
809 break;
811 case DAT_XFERGROUP:
812 switch (MSG)
814 case MSG_GET:
815 twRC = GPHOTO2_XferGroupGet (pOrigin, pData);
816 break;
817 case MSG_SET:
818 twRC = GPHOTO2_XferGroupSet (pOrigin, pData);
819 break;
820 default:
821 twRC = TWRC_FAILURE;
822 break;
824 break;
826 default:
827 FIXME("code unknown: %d\n", DAT);
828 twRC = TWRC_FAILURE;
829 break;
832 return twRC;
836 static TW_UINT16 GPHOTO2_ImageGroupHandler (
837 pTW_IDENTITY pOrigin,
838 TW_UINT16 DAT,
839 TW_UINT16 MSG,
840 TW_MEMREF pData)
842 TW_UINT16 twRC = TWRC_SUCCESS;
844 switch (DAT)
846 case DAT_CIECOLOR:
847 if (MSG == MSG_GET)
848 twRC = GPHOTO2_CIEColorGet (pOrigin, pData);
849 else
850 twRC = TWRC_FAILURE;
851 break;
853 case DAT_EXTIMAGEINFO:
854 if (MSG == MSG_GET)
855 twRC = GPHOTO2_ExtImageInfoGet (pOrigin, pData);
856 else
857 twRC = TWRC_FAILURE;
858 break;
860 case DAT_GRAYRESPONSE:
861 switch (MSG)
863 case MSG_RESET:
864 twRC = GPHOTO2_GrayResponseReset (pOrigin, pData);
865 break;
866 case MSG_SET:
867 twRC = GPHOTO2_GrayResponseSet (pOrigin, pData);
868 break;
869 default:
870 twRC = TWRC_FAILURE;
871 activeDS.twCC = TWCC_BADPROTOCOL;
872 FIXME("unrecognized operation triplet\n");
873 break;
875 break;
876 case DAT_IMAGEFILEXFER:
877 if (MSG == MSG_GET)
878 twRC = GPHOTO2_ImageFileXferGet (pOrigin, pData);
879 else
880 twRC = TWRC_FAILURE;
881 break;
883 case DAT_IMAGEINFO:
884 if (MSG == MSG_GET)
885 twRC = GPHOTO2_ImageInfoGet (pOrigin, pData);
886 else
887 twRC = TWRC_FAILURE;
888 break;
890 case DAT_IMAGELAYOUT:
891 switch (MSG)
893 case MSG_GET:
894 twRC = GPHOTO2_ImageLayoutGet (pOrigin, pData);
895 break;
896 case MSG_GETDEFAULT:
897 twRC = GPHOTO2_ImageLayoutGetDefault (pOrigin, pData);
898 break;
899 case MSG_RESET:
900 twRC = GPHOTO2_ImageLayoutReset (pOrigin, pData);
901 break;
902 case MSG_SET:
903 twRC = GPHOTO2_ImageLayoutSet (pOrigin, pData);
904 break;
905 default:
906 twRC = TWRC_FAILURE;
907 activeDS.twCC = TWCC_BADPROTOCOL;
908 ERR("unrecognized operation triplet\n");
909 break;
911 break;
913 case DAT_IMAGEMEMXFER:
914 if (MSG == MSG_GET)
915 twRC = GPHOTO2_ImageMemXferGet (pOrigin, pData);
916 else
917 twRC = TWRC_FAILURE;
918 break;
920 case DAT_IMAGENATIVEXFER:
921 if (MSG == MSG_GET)
922 twRC = GPHOTO2_ImageNativeXferGet (pOrigin, pData);
923 else
924 twRC = TWRC_FAILURE;
925 break;
927 case DAT_JPEGCOMPRESSION:
928 switch (MSG)
930 case MSG_GET:
931 twRC = GPHOTO2_JPEGCompressionGet (pOrigin, pData);
932 break;
933 case MSG_GETDEFAULT:
934 twRC = GPHOTO2_JPEGCompressionGetDefault (pOrigin, pData);
935 break;
936 case MSG_RESET:
937 twRC = GPHOTO2_JPEGCompressionReset (pOrigin, pData);
938 break;
939 case MSG_SET:
940 twRC = GPHOTO2_JPEGCompressionSet (pOrigin, pData);
941 break;
942 default:
943 twRC = TWRC_FAILURE;
944 activeDS.twCC = TWCC_BADPROTOCOL;
945 WARN("unrecognized operation triplet\n");
946 break;
948 break;
950 case DAT_PALETTE8:
951 switch (MSG)
953 case MSG_GET:
954 twRC = GPHOTO2_Palette8Get (pOrigin, pData);
955 break;
956 case MSG_GETDEFAULT:
957 twRC = GPHOTO2_Palette8GetDefault (pOrigin, pData);
958 break;
959 case MSG_RESET:
960 twRC = GPHOTO2_Palette8Reset (pOrigin, pData);
961 break;
962 case MSG_SET:
963 twRC = GPHOTO2_Palette8Set (pOrigin, pData);
964 break;
965 default:
966 twRC = TWRC_FAILURE;
967 activeDS.twCC = TWCC_BADPROTOCOL;
968 WARN("unrecognized operation triplet\n");
970 break;
972 case DAT_RGBRESPONSE:
973 switch (MSG)
975 case MSG_RESET:
976 twRC = GPHOTO2_RGBResponseReset (pOrigin, pData);
977 break;
978 case MSG_SET:
979 twRC = GPHOTO2_RGBResponseSet (pOrigin, pData);
980 break;
981 default:
982 twRC = TWRC_FAILURE;
983 activeDS.twCC = TWCC_BADPROTOCOL;
984 WARN("unrecognized operation triplet\n");
985 break;
987 break;
989 default:
990 twRC = TWRC_FAILURE;
991 activeDS.twCC = TWCC_BADPROTOCOL;
992 FIXME("unrecognized DG type %d\n", DAT);
994 return twRC;
997 /* Main entry point for the TWAIN library */
998 TW_UINT16 WINAPI
999 DS_Entry ( pTW_IDENTITY pOrigin,
1000 TW_UINT32 DG,
1001 TW_UINT16 DAT,
1002 TW_UINT16 MSG,
1003 TW_MEMREF pData)
1005 TW_UINT16 twRC = TWRC_SUCCESS; /* Return Code */
1007 TRACE("(DG=%d DAT=%d MSG=%d)\n", DG, DAT, MSG);
1009 switch (DG)
1011 case DG_CONTROL:
1012 twRC = GPHOTO2_SourceControlHandler (pOrigin,DAT,MSG,pData);
1013 break;
1014 case DG_IMAGE:
1015 twRC = GPHOTO2_ImageGroupHandler (pOrigin,DAT,MSG,pData);
1016 break;
1017 case DG_AUDIO:
1018 FIXME("The audio group of entry codes is not implemented.\n");
1019 default:
1020 activeDS.twCC = TWCC_BADPROTOCOL;
1021 twRC = TWRC_FAILURE;
1024 return twRC;
1027 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
1028 static GPPortInfoList *port_list;
1029 static int curcamera;
1030 static CameraList *detected_cameras;
1031 static CameraAbilitiesList *abilities_list;
1033 static TW_UINT16
1034 gphoto2_auto_detect(void) {
1035 int result, count;
1037 if (detected_cameras && (gp_list_count (detected_cameras) == 0)) {
1038 /* Reload if previously no cameras, we might detect new ones. */
1039 TRACE("Reloading portlist trying to detect cameras.\n");
1040 if (port_list) {
1041 gp_port_info_list_free (port_list);
1042 port_list = NULL;
1045 if (!port_list) {
1046 TRACE("Auto detecting gphoto cameras.\n");
1047 TRACE("Loading ports...\n");
1048 if (gp_port_info_list_new (&port_list) < GP_OK)
1049 return TWRC_FAILURE;
1050 result = gp_port_info_list_load (port_list);
1051 if (result < 0) {
1052 gp_port_info_list_free (port_list);
1053 return TWRC_FAILURE;
1055 count = gp_port_info_list_count (port_list);
1056 if (count <= 0)
1057 return TWRC_FAILURE;
1058 if (gp_list_new (&detected_cameras) < GP_OK)
1059 return TWRC_FAILURE;
1060 if (!abilities_list) { /* Load only once per program start */
1061 gp_abilities_list_new (&abilities_list);
1062 TRACE("Loading cameras...\n");
1063 gp_abilities_list_load (abilities_list, NULL);
1065 TRACE("Detecting cameras...\n");
1066 gp_abilities_list_detect (abilities_list, port_list, detected_cameras, NULL);
1067 curcamera = 0;
1068 TRACE("%d cameras detected\n", gp_list_count(detected_cameras));
1070 return TWRC_SUCCESS;
1073 static TW_UINT16
1074 GPHOTO2_GetIdentity( pTW_IDENTITY pOrigin, pTW_IDENTITY self) {
1075 int count;
1076 const char *cname, *pname;
1078 if (TWRC_SUCCESS != gphoto2_auto_detect())
1079 return TWRC_FAILURE;
1081 count = gp_list_count (detected_cameras);
1082 if (count < GP_OK) {
1083 gp_list_free (detected_cameras);
1084 return TWRC_FAILURE;
1086 TRACE("%d cameras detected.\n", count);
1087 self->ProtocolMajor = TWON_PROTOCOLMAJOR;
1088 self->ProtocolMinor = TWON_PROTOCOLMINOR;
1089 lstrcpynA (self->Manufacturer, "The Wine Team", sizeof(self->Manufacturer) - 1);
1090 lstrcpynA (self->ProductFamily, "GPhoto2 Camera", sizeof(self->ProductFamily) - 1);
1092 if (!count) { /* No camera detected. But we need to return an IDENTITY anyway. */
1093 lstrcpynA (self->ProductName, "GPhoto2 Camera", sizeof(self->ProductName) - 1);
1094 return TWRC_SUCCESS;
1096 gp_list_get_name (detected_cameras, curcamera, &cname);
1097 gp_list_get_value (detected_cameras, curcamera, &pname);
1098 if (count == 1) /* Normal case, only one camera. */
1099 snprintf (self->ProductName, sizeof(self->ProductName), "%s", cname);
1100 else
1101 snprintf (self->ProductName, sizeof(self->ProductName), "%s@%s", cname, pname);
1102 curcamera = (curcamera+1) % count;
1103 return TWRC_SUCCESS;
1106 static TW_UINT16
1107 GPHOTO2_OpenDS( pTW_IDENTITY pOrigin, pTW_IDENTITY self) {
1108 int ret, m, p, count, i;
1109 CameraAbilities a;
1110 GPPortInfo info;
1111 const char *model, *port;
1113 if (TWRC_SUCCESS != gphoto2_auto_detect())
1114 return TWRC_FAILURE;
1116 if (lstrcmpA(self->ProductFamily,"GPhoto2 Camera")) {
1117 FIXME("identity passed is not a gphoto camera, but %s!?!\n", self->ProductFamily);
1118 return TWRC_FAILURE;
1120 count = gp_list_count (detected_cameras);
1121 if (!count) {
1122 ERR("No camera found by autodetection. Returning failure.\n");
1123 return TWRC_FAILURE;
1126 if (!lstrcmpA (self->ProductName, "GPhoto2 Camera")) {
1127 TRACE("Potential undetected camera. Just using the first autodetected one.\n");
1128 i = 0;
1129 } else {
1130 for (i=0;i<count;i++) {
1131 const char *cname, *pname;
1132 TW_STR32 name;
1134 gp_list_get_name (detected_cameras, i, &cname);
1135 gp_list_get_value (detected_cameras, i, &pname);
1136 if (!lstrcmpA(self->ProductName,cname))
1137 break;
1138 snprintf(name, sizeof(name), "%s", cname);
1139 if (!lstrcmpA(self->ProductName,name))
1140 break;
1141 snprintf(name, sizeof(name), "%s@%s", cname, pname);
1142 if (!lstrcmpA(self->ProductName,name))
1143 break;
1145 if (i == count) {
1146 TRACE("Camera %s not found in autodetected list. Using first entry.\n", self->ProductName);
1147 i=0;
1150 gp_list_get_name (detected_cameras, i, &model);
1151 gp_list_get_value (detected_cameras, i, &port);
1152 TRACE("model %s, port %s\n", model, port);
1153 ret = gp_camera_new (&activeDS.camera);
1154 if (ret < GP_OK) {
1155 ERR("gp_camera_new: %d\n", ret);
1156 return TWRC_FAILURE;
1158 m = gp_abilities_list_lookup_model (abilities_list, model);
1159 if (m < GP_OK) {
1160 FIXME("Model %s not found, %d!\n", model, m);
1161 return TWRC_FAILURE;
1163 ret = gp_abilities_list_get_abilities (abilities_list, m, &a);
1164 if (ret < GP_OK) {
1165 FIXME("gp_camera_list_get_abilities failed? %d\n", ret);
1166 return TWRC_FAILURE;
1168 ret = gp_camera_set_abilities (activeDS.camera, a);
1169 if (ret < GP_OK) {
1170 FIXME("gp_camera_set_abilities failed? %d\n", ret);
1171 return TWRC_FAILURE;
1174 p = gp_port_info_list_lookup_path (port_list, port);
1175 if (p < GP_OK) {
1176 FIXME("port %s not in portlist?\n", port);
1177 return TWRC_FAILURE;
1179 ret = gp_port_info_list_get_info (port_list, p, &info);
1180 if (ret < GP_OK) {
1181 FIXME("could not get portinfo for port %s?\n", port);
1182 return TWRC_FAILURE;
1184 ret = gp_camera_set_port_info (activeDS.camera, info);
1185 if (ret < GP_OK) {
1186 FIXME("could not set portinfo for port %s to camera?\n", port);
1187 return TWRC_FAILURE;
1189 list_init( &(activeDS.files) );
1190 activeDS.currentState = 4;
1191 activeDS.twCC = TWRC_SUCCESS;
1192 activeDS.pixelflavor = TWPF_CHOCOLATE;
1193 activeDS.pixeltype = TWPT_RGB;
1194 activeDS.capXferMech = TWSX_MEMORY;
1195 TRACE("OK!\n");
1196 return TWRC_SUCCESS;
1198 #endif