user32/tests: Recognize undefined characters in OemKeyScan tests.
[wine.git] / dlls / gphoto2.ds / gphoto2_main.c
blob28100414cc7f7024265acdce6b9c9528e3f1c3f7
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);
36 DSMENTRYPROC GPHOTO2_dsmentry;
38 #ifdef HAVE_GPHOTO2
39 static char* GPHOTO2_StrDup(const char* str)
41 char* dst = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
42 strcpy(dst, str);
43 return dst;
45 #endif
47 static void
48 load_filesystem(const char *folder) {
49 #ifdef HAVE_GPHOTO2
50 int i, count, ret;
51 CameraList *list;
53 ret = gp_list_new (&list);
54 if (ret < GP_OK)
55 return;
56 ret = gp_camera_folder_list_files (activeDS.camera, folder, list, activeDS.context);
57 if (ret < GP_OK) {
58 gp_list_free (list);
59 return;
61 count = gp_list_count (list);
62 if (count < GP_OK) {
63 gp_list_free (list);
64 return;
66 for (i = 0; i < count; i++) {
67 const char *name;
68 struct gphoto2_file *gpfile;
70 ret = gp_list_get_name (list, i, &name);
71 if (ret < GP_OK)
72 continue;
73 gpfile = HeapAlloc(GetProcessHeap(), 0, sizeof(struct gphoto2_file)); /* FIXME: Leaked */
74 if (!gpfile)
75 continue;
76 TRACE("adding %s/%s\n", folder, name);
77 gpfile->folder = GPHOTO2_StrDup(folder);
78 gpfile->filename = GPHOTO2_StrDup(name);
79 gpfile->download = FALSE;
80 list_add_tail( &activeDS.files, &gpfile->entry );
82 gp_list_reset (list);
84 ret = gp_camera_folder_list_folders (activeDS.camera, folder, list, activeDS.context);
85 if (ret < GP_OK) {
86 FIXME("list_folders failed\n");
87 gp_list_free (list);
88 return;
90 count = gp_list_count (list);
91 if (count < GP_OK) {
92 FIXME("list_folders failed\n");
93 gp_list_free (list);
94 return;
96 for (i = 0; i < count; i++) {
97 const char *name;
98 char *newfolder;
99 ret = gp_list_get_name (list, i, &name);
100 if (ret < GP_OK)
101 continue;
102 TRACE("recursing into %s\n", name);
103 newfolder = HeapAlloc(GetProcessHeap(), 0, strlen(folder)+1+strlen(name)+1);
104 if (!strcmp(folder,"/"))
105 sprintf (newfolder, "/%s", name);
106 else
107 sprintf (newfolder, "%s/%s", folder, name);
108 load_filesystem (newfolder); /* recurse ... happily */
110 gp_list_free (list);
111 #endif
114 /* DG_CONTROL/DAT_CAPABILITY/MSG_GET */
115 static TW_UINT16 GPHOTO2_CapabilityGet (pTW_IDENTITY pOrigin, TW_MEMREF pData)
117 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
119 TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GET\n");
120 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
121 activeDS.twCC = TWCC_SEQERROR;
122 return TWRC_FAILURE;
124 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GET);
125 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
128 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT */
129 static TW_UINT16 GPHOTO2_CapabilityGetCurrent (pTW_IDENTITY pOrigin, TW_MEMREF pData)
131 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
133 TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT\n");
135 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
136 activeDS.twCC = TWCC_SEQERROR;
137 return TWRC_FAILURE;
139 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GETCURRENT);
140 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
143 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT */
144 static TW_UINT16 GPHOTO2_CapabilityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
146 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
148 TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT\n");
149 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
150 activeDS.twCC = TWCC_SEQERROR;
151 return TWRC_FAILURE;
153 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_GETDEFAULT);
154 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
157 /* DG_CONTROL/DAT_CAPABILITY/MSG_QUERYSUPPORT */
158 static TW_UINT16 GPHOTO2_CapabilityQuerySupport (pTW_IDENTITY pOrigin,
159 TW_MEMREF pData)
161 FIXME ("stub!\n");
163 return TWRC_FAILURE;
166 /* DG_CONTROL/DAT_CAPABILITY/MSG_RESET */
167 static TW_UINT16 GPHOTO2_CapabilityReset (pTW_IDENTITY pOrigin,
168 TW_MEMREF pData)
170 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
172 TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_RESET\n");
174 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
175 activeDS.twCC = TWCC_SEQERROR;
176 return TWRC_FAILURE;
178 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_RESET);
179 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
182 /* DG_CONTROL/DAT_CAPABILITY/MSG_SET */
183 static TW_UINT16 GPHOTO2_CapabilitySet (pTW_IDENTITY pOrigin,
184 TW_MEMREF pData)
186 pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
188 TRACE ("DG_CONTROL/DAT_CAPABILITY/MSG_SET\n");
190 if (activeDS.currentState != 4) {
191 activeDS.twCC = TWCC_SEQERROR;
192 return TWRC_FAILURE;
194 activeDS.twCC = GPHOTO2_SaneCapability (pCapability, MSG_SET);
195 return (activeDS.twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
198 /* DG_CONTROL/DAT_CUSTOMDSDATA/MSG_GET */
199 static TW_UINT16 GPHOTO2_CustomDSDataGet (pTW_IDENTITY pOrigin,
200 TW_MEMREF pData)
202 FIXME ("stub!\n");
204 return TWRC_FAILURE;
207 /* DG_CONTROL/DAT_CUSTOMDSDATA/MSG_SET */
208 static TW_UINT16 GPHOTO2_CustomDSDataSet (pTW_IDENTITY pOrigin,
209 TW_MEMREF pData)
211 FIXME ("stub!\n");
213 return TWRC_FAILURE;
216 /* DG_CONTROL/DAT_FILESYSTEM/MSG_CHANGEDIRECTORY */
217 static TW_UINT16 GPHOTO2_ChangeDirectory (pTW_IDENTITY pOrigin,
218 TW_MEMREF pData)
220 FIXME ("stub!\n");
222 return TWRC_FAILURE;
225 /* DG_CONTROL/DAT_FILESYSTEM/MSG_CREATEDIRECTORY */
226 static TW_UINT16 GPHOTO2_CreateDirectory (pTW_IDENTITY pOrigin,
227 TW_MEMREF pData)
229 FIXME ("stub!\n");
231 return TWRC_FAILURE;
234 /* DG_CONTROL/DAT_FILESYSTEM/MSG_DELETE */
235 static TW_UINT16 GPHOTO2_FileSystemDelete (pTW_IDENTITY pOrigin,
236 TW_MEMREF pData)
238 FIXME ("stub!\n");
240 return TWRC_FAILURE;
243 /* DG_CONTROL/DAT_FILESYSTEM/MSG_FORMATMEDIA */
244 static TW_UINT16 GPHOTO2_FormatMedia (pTW_IDENTITY pOrigin,
245 TW_MEMREF pData)
247 FIXME ("stub!\n");
249 return TWRC_FAILURE;
252 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETCLOSE */
253 static TW_UINT16 GPHOTO2_FileSystemGetClose (pTW_IDENTITY pOrigin,
254 TW_MEMREF pData)
256 FIXME ("stub!\n");
258 return TWRC_FAILURE;
261 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETFIRSTFILE */
262 static TW_UINT16 GPHOTO2_FileSystemGetFirstFile (pTW_IDENTITY pOrigin,
264 TW_MEMREF pData)
266 FIXME ("stub!\n");
268 return TWRC_FAILURE;
271 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETINFO */
272 static TW_UINT16 GPHOTO2_FileSystemGetInfo (pTW_IDENTITY pOrigin,
273 TW_MEMREF pData)
275 FIXME ("stub!\n");
277 return TWRC_FAILURE;
280 /* DG_CONTROL/DAT_FILESYSTEM/MSG_GETNEXTFILE */
281 static TW_UINT16 GPHOTO2_FileSystemGetNextFile (pTW_IDENTITY pOrigin,
283 TW_MEMREF pData)
285 FIXME ("stub!\n");
287 return TWRC_FAILURE;
290 /* DG_CONTROL/DAT_FILESYSTEM/MSG_RENAME */
291 static TW_UINT16 GPHOTO2_FileSystemRename (pTW_IDENTITY pOrigin,
292 TW_MEMREF pData)
294 FIXME ("stub!\n");
296 return TWRC_FAILURE;
299 static void GPHOTO2_Notify (TW_UINT16 message)
301 GPHOTO2_dsmentry (&activeDS.identity, &activeDS.appIdentity, DG_CONTROL, DAT_NULL, message, NULL);
304 /* DG_CONTROL/DAT_ENTRYPOINT/MSG_SET */
305 static TW_UINT16 GPHOTO2_SetEntryPoint (pTW_IDENTITY pOrigin, TW_MEMREF pData)
307 TW_ENTRYPOINT *entry = (TW_ENTRYPOINT*)pData;
309 GPHOTO2_dsmentry = entry->DSM_Entry;
311 return TWRC_SUCCESS;
314 /* DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT */
315 static TW_UINT16 GPHOTO2_ProcessEvent (pTW_IDENTITY pOrigin,
316 TW_MEMREF pData)
318 pTW_EVENT pEvent = (pTW_EVENT) pData;
320 TRACE("DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT\n");
322 if (activeDS.currentState < 5 || activeDS.currentState > 7) {
323 activeDS.twCC = TWCC_SEQERROR;
324 return TWRC_FAILURE;
327 pEvent->TWMessage = MSG_NULL; /* no message to the application */
328 activeDS.twCC = TWCC_SUCCESS;
329 return TWRC_NOTDSEVENT;
332 /* DG_CONTROL/DAT_PASSTHRU/MSG_PASSTHRU */
333 static TW_UINT16 GPHOTO2_PassThrough (pTW_IDENTITY pOrigin,
334 TW_MEMREF pData)
336 FIXME ("stub!\n");
338 return TWRC_FAILURE;
341 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER */
342 static TW_UINT16 GPHOTO2_PendingXfersEndXfer (pTW_IDENTITY pOrigin,
343 TW_MEMREF pData)
345 TW_UINT32 count;
346 pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
347 struct gphoto2_file *file;
349 TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER\n");
351 if (activeDS.currentState != 6 && activeDS.currentState != 7) {
352 activeDS.twCC = TWCC_SEQERROR;
353 return TWRC_FAILURE;
355 count = 0;
356 LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry ) {
357 if (file->download)
358 count++;
360 TRACE("count = %d\n", count);
361 pPendingXfers->Count = count;
362 if (pPendingXfers->Count != 0) {
363 activeDS.currentState = 6;
364 } else {
365 activeDS.currentState = 5;
366 /* Notify the application that it can close the data source */
367 GPHOTO2_Notify(MSG_CLOSEDSREQ);
368 /* close any Transferring dialog */
369 TransferringDialogBox(activeDS.progressWnd,-1);
370 activeDS.progressWnd = 0;
372 activeDS.twCC = TWCC_SUCCESS;
373 return TWRC_SUCCESS;
376 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_GET */
377 static TW_UINT16 GPHOTO2_PendingXfersGet (pTW_IDENTITY pOrigin,
378 TW_MEMREF pData)
380 TW_UINT32 count;
381 pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
382 struct gphoto2_file *file;
384 TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_GET\n");
386 if (activeDS.currentState < 4 || activeDS.currentState > 7) {
387 activeDS.twCC = TWCC_SEQERROR;
388 return TWRC_FAILURE;
391 count = 0;
392 LIST_FOR_EACH_ENTRY( file, &activeDS.files, struct gphoto2_file, entry ) {
393 if (file->download)
394 count++;
396 TRACE("count = %d\n", count);
397 pPendingXfers->Count = count;
398 activeDS.twCC = TWCC_SUCCESS;
399 return TWRC_SUCCESS;
402 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET */
403 static TW_UINT16 GPHOTO2_PendingXfersReset (pTW_IDENTITY pOrigin,
404 TW_MEMREF pData)
406 pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
408 TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET\n");
410 if (activeDS.currentState != 6) {
411 activeDS.twCC = TWCC_SEQERROR;
412 return TWRC_FAILURE;
414 pPendingXfers->Count = 0;
415 activeDS.currentState = 5;
416 activeDS.twCC = TWCC_SUCCESS;
417 return TWRC_SUCCESS;
420 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_GET */
421 static TW_UINT16 GPHOTO2_SetupFileXferGet (pTW_IDENTITY pOrigin,
422 TW_MEMREF pData)
424 FIXME ("stub!\n");
426 return TWRC_FAILURE;
429 /* DG_CONTROL/DAT_SETUPXFER/MSG_GETDEFAULT */
430 static TW_UINT16 GPHOTO2_SetupFileXferGetDefault (pTW_IDENTITY pOrigin,
431 TW_MEMREF pData)
433 FIXME ("stub!\n");
435 return TWRC_FAILURE;
439 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_RESET */
440 static TW_UINT16 GPHOTO2_SetupFileXferReset (pTW_IDENTITY pOrigin,
441 TW_MEMREF pData)
443 FIXME ("stub!\n");
445 return TWRC_FAILURE;
448 /* DG_CONTROL/DAT_SETUPFILEXFER/MSG_SET */
449 static TW_UINT16 GPHOTO2_SetupFileXferSet (pTW_IDENTITY pOrigin,
450 TW_MEMREF pData)
452 FIXME ("stub!\n");
454 return TWRC_FAILURE;
457 /* DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET */
458 static TW_UINT16 GPHOTO2_SetupMemXferGet (pTW_IDENTITY pOrigin,
459 TW_MEMREF pData)
461 pTW_SETUPMEMXFER pSetupMemXfer = (pTW_SETUPMEMXFER)pData;
463 TRACE("DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET\n");
464 /* Guessing */
465 pSetupMemXfer->MinBufSize = 20000;
466 pSetupMemXfer->MaxBufSize = 80000;
467 pSetupMemXfer->Preferred = 40000;
468 return TWRC_SUCCESS;
471 /* DG_CONTROL/DAT_STATUS/MSG_GET */
472 static TW_UINT16 GPHOTO2_GetDSStatus (pTW_IDENTITY pOrigin,
473 TW_MEMREF pData)
475 pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
477 TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
478 pSourceStatus->ConditionCode = activeDS.twCC;
479 /* Reset the condition code */
480 activeDS.twCC = TWCC_SUCCESS;
481 return TWRC_SUCCESS;
484 /* DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS */
485 static TW_UINT16 GPHOTO2_DisableDSUserInterface (pTW_IDENTITY pOrigin,
486 TW_MEMREF pData)
488 TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS\n");
490 if (activeDS.currentState != 5) {
491 activeDS.twCC = TWCC_SEQERROR;
492 return TWRC_FAILURE;
494 activeDS.currentState = 4;
495 activeDS.twCC = TWCC_SUCCESS;
496 return TWRC_SUCCESS;
499 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS */
500 static TW_UINT16 GPHOTO2_EnableDSUserInterface (pTW_IDENTITY pOrigin,
501 TW_MEMREF pData)
503 pTW_USERINTERFACE pUserInterface = (pTW_USERINTERFACE) pData;
505 load_filesystem("/");
507 TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS\n");
508 if (activeDS.currentState != 4) {
509 FIXME("Sequence error %d\n", activeDS.currentState);
510 activeDS.twCC = TWCC_SEQERROR;
511 return TWRC_FAILURE;
513 if (pUserInterface->ShowUI)
515 BOOL rc;
516 activeDS.currentState = 5; /* Transitions to state 5 */
517 rc = DoCameraUI();
518 if (!rc) {
519 GPHOTO2_Notify(MSG_CLOSEDSREQ);
520 } else {
521 /* FIXME: The GUI should have marked the files to download... */
522 GPHOTO2_Notify(MSG_XFERREADY);
523 activeDS.currentState = 6; /* Transitions to state 6 directly */
525 } else {
526 /* no UI will be displayed, so source is ready to transfer data */
527 GPHOTO2_Notify(MSG_XFERREADY);
528 activeDS.currentState = 6; /* Transitions to state 6 directly */
530 activeDS.twCC = TWCC_SUCCESS;
531 return TWRC_SUCCESS;
534 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY */
535 static TW_UINT16 GPHOTO2_EnableDSUIOnly (pTW_IDENTITY pOrigin,
536 TW_MEMREF pData)
538 TRACE("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY\n");
540 if (activeDS.currentState != 4) {
541 activeDS.twCC = TWCC_SEQERROR;
542 return TWRC_FAILURE;
544 /* FIXME: we should replace xscanimage with our own UI */
545 FIXME ("not implemented!\n");
546 activeDS.currentState = 5;
547 activeDS.twCC = TWCC_SUCCESS;
548 return TWRC_SUCCESS;
551 /* DG_CONTROL/DAT_XFERGROUP/MSG_GET */
552 static TW_UINT16 GPHOTO2_XferGroupGet (pTW_IDENTITY pOrigin,
553 TW_MEMREF pData)
555 FIXME ("stub!\n");
556 return TWRC_FAILURE;
559 /* DG_CONTROL/DAT_XFERGROUP/MSG_SET */
560 static TW_UINT16 GPHOTO2_XferGroupSet (pTW_IDENTITY pOrigin,
561 TW_MEMREF pData)
563 FIXME ("stub!\n");
564 return TWRC_FAILURE;
567 HINSTANCE GPHOTO2_instance;
569 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
571 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
573 switch (fdwReason)
575 case DLL_PROCESS_ATTACH:
576 GPHOTO2_instance = hinstDLL;
577 DisableThreadLibraryCalls(hinstDLL);
578 #ifdef HAVE_GPHOTO2
579 activeDS.context = gp_context_new ();
580 #endif
581 break;
584 return TRUE;
587 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
588 static TW_UINT16 GPHOTO2_GetIdentity( pTW_IDENTITY, pTW_IDENTITY);
589 static TW_UINT16 GPHOTO2_OpenDS( pTW_IDENTITY, pTW_IDENTITY);
590 #endif
592 static TW_UINT16 GPHOTO2_SourceControlHandler (
593 pTW_IDENTITY pOrigin,
594 TW_UINT16 DAT,
595 TW_UINT16 MSG,
596 TW_MEMREF pData)
598 TW_UINT16 twRC = TWRC_SUCCESS;
600 switch (DAT)
602 case DAT_IDENTITY:
603 switch (MSG)
605 case MSG_CLOSEDS:
606 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
607 if (activeDS.camera) {
608 gp_camera_free (activeDS.camera);
609 activeDS.camera = NULL;
611 #endif
612 break;
613 case MSG_GET:
614 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
615 twRC = GPHOTO2_GetIdentity(pOrigin,(pTW_IDENTITY)pData);
616 #else
617 twRC = TWRC_FAILURE;
618 #endif
619 break;
620 case MSG_OPENDS:
621 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
622 twRC = GPHOTO2_OpenDS(pOrigin,(pTW_IDENTITY)pData);
623 #else
624 twRC = TWRC_FAILURE;
625 #endif
626 break;
628 break;
629 case DAT_CAPABILITY:
630 switch (MSG)
632 case MSG_GET:
633 twRC = GPHOTO2_CapabilityGet (pOrigin, pData);
634 break;
635 case MSG_GETCURRENT:
636 twRC = GPHOTO2_CapabilityGetCurrent (pOrigin, pData);
637 break;
638 case MSG_GETDEFAULT:
639 twRC = GPHOTO2_CapabilityGetDefault (pOrigin, pData);
640 break;
641 case MSG_QUERYSUPPORT:
642 twRC = GPHOTO2_CapabilityQuerySupport (pOrigin, pData);
643 break;
644 case MSG_RESET:
645 twRC = GPHOTO2_CapabilityReset (pOrigin, pData);
646 break;
647 case MSG_SET:
648 twRC = GPHOTO2_CapabilitySet (pOrigin, pData);
649 break;
650 default:
651 twRC = TWRC_FAILURE;
652 FIXME("unrecognized operation triplet\n");
654 break;
656 case DAT_CUSTOMDSDATA:
657 switch (MSG)
659 case MSG_GET:
660 twRC = GPHOTO2_CustomDSDataGet (pOrigin, pData);
661 break;
662 case MSG_SET:
663 twRC = GPHOTO2_CustomDSDataSet (pOrigin, pData);
664 break;
665 default:
666 break;
668 break;
670 case DAT_FILESYSTEM:
671 switch (MSG)
673 /*case MSG_AUTOMATICCAPTUREDIRECTORY:
674 twRC = GPHOTO2_AutomaticCaptureDirectory
675 (pOrigin, pData);
676 break;*/
677 case MSG_CHANGEDIRECTORY:
678 twRC = GPHOTO2_ChangeDirectory (pOrigin, pData);
679 break;
680 /*case MSG_COPY:
681 twRC = GPHOTO2_FileSystemCopy (pOrigin, pData);
682 break;*/
683 case MSG_CREATEDIRECTORY:
684 twRC = GPHOTO2_CreateDirectory (pOrigin, pData);
685 break;
686 case MSG_DELETE:
687 twRC = GPHOTO2_FileSystemDelete (pOrigin, pData);
688 break;
689 case MSG_FORMATMEDIA:
690 twRC = GPHOTO2_FormatMedia (pOrigin, pData);
691 break;
692 case MSG_GETCLOSE:
693 twRC = GPHOTO2_FileSystemGetClose (pOrigin, pData);
694 break;
695 case MSG_GETFIRSTFILE:
696 twRC = GPHOTO2_FileSystemGetFirstFile (pOrigin, pData);
697 break;
698 case MSG_GETINFO:
699 twRC = GPHOTO2_FileSystemGetInfo (pOrigin, pData);
700 break;
701 case MSG_GETNEXTFILE:
702 twRC = GPHOTO2_FileSystemGetNextFile (pOrigin, pData);
703 break;
704 case MSG_RENAME:
705 twRC = GPHOTO2_FileSystemRename (pOrigin, pData);
706 break;
707 default:
708 twRC = TWRC_FAILURE;
709 break;
711 break;
713 case DAT_ENTRYPOINT:
714 if (MSG == MSG_SET)
715 twRC = GPHOTO2_SetEntryPoint (pOrigin, pData);
716 else
717 twRC = TWRC_FAILURE;
718 break;
720 case DAT_EVENT:
721 if (MSG == MSG_PROCESSEVENT)
722 twRC = GPHOTO2_ProcessEvent (pOrigin, pData);
723 else
724 twRC = TWRC_FAILURE;
725 break;
727 case DAT_PASSTHRU:
728 if (MSG == MSG_PASSTHRU)
729 twRC = GPHOTO2_PassThrough (pOrigin, pData);
730 else
731 twRC = TWRC_FAILURE;
732 break;
734 case DAT_PENDINGXFERS:
735 switch (MSG)
737 case MSG_ENDXFER:
738 twRC = GPHOTO2_PendingXfersEndXfer (pOrigin, pData);
739 break;
740 case MSG_GET:
741 twRC = GPHOTO2_PendingXfersGet (pOrigin, pData);
742 break;
743 case MSG_RESET:
744 twRC = GPHOTO2_PendingXfersReset (pOrigin, pData);
745 break;
746 /*case MSG_STOPFEEDER:
747 twRC = GPHOTO2_PendingXfersStopFeeder (pOrigin, pData);
748 break;*/
749 default:
750 twRC = TWRC_FAILURE;
752 break;
754 case DAT_SETUPFILEXFER:
755 switch (MSG)
757 case MSG_GET:
758 twRC = GPHOTO2_SetupFileXferGet (pOrigin, pData);
759 break;
760 case MSG_GETDEFAULT:
761 twRC = GPHOTO2_SetupFileXferGetDefault (pOrigin, pData);
762 break;
763 case MSG_RESET:
764 twRC = GPHOTO2_SetupFileXferReset (pOrigin, pData);
765 break;
766 case MSG_SET:
767 twRC = GPHOTO2_SetupFileXferSet (pOrigin, pData);
768 break;
769 default:
770 twRC = TWRC_FAILURE;
771 break;
773 break;
775 /*case DAT_SETUPFILEXFER2:
776 switch (MSG)
778 case MSG_GET:
779 twRC = GPHOTO2_SetupFileXfer2Get (pOrigin, pData);
780 break;
781 case MSG_GETDEFAULT:
782 twRC = GPHOTO2_SetupFileXfer2GetDefault (pOrigin, pData);
783 break;
784 case MSG_RESET:
785 twRC = GPHOTO2_SetupFileXfer2Reset (pOrigin, pData);
786 break;
787 case MSG_SET:
788 twRC = GPHOTO2_SetupFileXfer2Set (pOrigin, pData);
789 break;
791 break;*/
792 case DAT_SETUPMEMXFER:
793 if (MSG == MSG_GET)
794 twRC = GPHOTO2_SetupMemXferGet (pOrigin, pData);
795 else
796 twRC = TWRC_FAILURE;
797 break;
799 case DAT_STATUS:
800 if (MSG == MSG_GET)
801 twRC = GPHOTO2_GetDSStatus (pOrigin, pData);
802 else
803 twRC = TWRC_FAILURE;
804 break;
806 case DAT_USERINTERFACE:
807 switch (MSG)
809 case MSG_DISABLEDS:
810 twRC = GPHOTO2_DisableDSUserInterface (pOrigin, pData);
811 break;
812 case MSG_ENABLEDS:
813 twRC = GPHOTO2_EnableDSUserInterface (pOrigin, pData);
814 break;
815 case MSG_ENABLEDSUIONLY:
816 twRC = GPHOTO2_EnableDSUIOnly (pOrigin, pData);
817 break;
818 default:
819 twRC = TWRC_FAILURE;
820 break;
822 break;
824 case DAT_XFERGROUP:
825 switch (MSG)
827 case MSG_GET:
828 twRC = GPHOTO2_XferGroupGet (pOrigin, pData);
829 break;
830 case MSG_SET:
831 twRC = GPHOTO2_XferGroupSet (pOrigin, pData);
832 break;
833 default:
834 twRC = TWRC_FAILURE;
835 break;
837 break;
839 default:
840 FIXME("code unknown: %d\n", DAT);
841 twRC = TWRC_FAILURE;
842 break;
845 return twRC;
849 static TW_UINT16 GPHOTO2_ImageGroupHandler (
850 pTW_IDENTITY pOrigin,
851 TW_UINT16 DAT,
852 TW_UINT16 MSG,
853 TW_MEMREF pData)
855 TW_UINT16 twRC = TWRC_SUCCESS;
857 switch (DAT)
859 case DAT_CIECOLOR:
860 if (MSG == MSG_GET)
861 twRC = GPHOTO2_CIEColorGet (pOrigin, pData);
862 else
863 twRC = TWRC_FAILURE;
864 break;
866 case DAT_EXTIMAGEINFO:
867 if (MSG == MSG_GET)
868 twRC = GPHOTO2_ExtImageInfoGet (pOrigin, pData);
869 else
870 twRC = TWRC_FAILURE;
871 break;
873 case DAT_GRAYRESPONSE:
874 switch (MSG)
876 case MSG_RESET:
877 twRC = GPHOTO2_GrayResponseReset (pOrigin, pData);
878 break;
879 case MSG_SET:
880 twRC = GPHOTO2_GrayResponseSet (pOrigin, pData);
881 break;
882 default:
883 twRC = TWRC_FAILURE;
884 activeDS.twCC = TWCC_BADPROTOCOL;
885 FIXME("unrecognized operation triplet\n");
886 break;
888 break;
889 case DAT_IMAGEFILEXFER:
890 if (MSG == MSG_GET)
891 twRC = GPHOTO2_ImageFileXferGet (pOrigin, pData);
892 else
893 twRC = TWRC_FAILURE;
894 break;
896 case DAT_IMAGEINFO:
897 if (MSG == MSG_GET)
898 twRC = GPHOTO2_ImageInfoGet (pOrigin, pData);
899 else
900 twRC = TWRC_FAILURE;
901 break;
903 case DAT_IMAGELAYOUT:
904 switch (MSG)
906 case MSG_GET:
907 twRC = GPHOTO2_ImageLayoutGet (pOrigin, pData);
908 break;
909 case MSG_GETDEFAULT:
910 twRC = GPHOTO2_ImageLayoutGetDefault (pOrigin, pData);
911 break;
912 case MSG_RESET:
913 twRC = GPHOTO2_ImageLayoutReset (pOrigin, pData);
914 break;
915 case MSG_SET:
916 twRC = GPHOTO2_ImageLayoutSet (pOrigin, pData);
917 break;
918 default:
919 twRC = TWRC_FAILURE;
920 activeDS.twCC = TWCC_BADPROTOCOL;
921 ERR("unrecognized operation triplet\n");
922 break;
924 break;
926 case DAT_IMAGEMEMXFER:
927 if (MSG == MSG_GET)
928 twRC = GPHOTO2_ImageMemXferGet (pOrigin, pData);
929 else
930 twRC = TWRC_FAILURE;
931 break;
933 case DAT_IMAGENATIVEXFER:
934 if (MSG == MSG_GET)
935 twRC = GPHOTO2_ImageNativeXferGet (pOrigin, pData);
936 else
937 twRC = TWRC_FAILURE;
938 break;
940 case DAT_JPEGCOMPRESSION:
941 switch (MSG)
943 case MSG_GET:
944 twRC = GPHOTO2_JPEGCompressionGet (pOrigin, pData);
945 break;
946 case MSG_GETDEFAULT:
947 twRC = GPHOTO2_JPEGCompressionGetDefault (pOrigin, pData);
948 break;
949 case MSG_RESET:
950 twRC = GPHOTO2_JPEGCompressionReset (pOrigin, pData);
951 break;
952 case MSG_SET:
953 twRC = GPHOTO2_JPEGCompressionSet (pOrigin, pData);
954 break;
955 default:
956 twRC = TWRC_FAILURE;
957 activeDS.twCC = TWCC_BADPROTOCOL;
958 WARN("unrecognized operation triplet\n");
959 break;
961 break;
963 case DAT_PALETTE8:
964 switch (MSG)
966 case MSG_GET:
967 twRC = GPHOTO2_Palette8Get (pOrigin, pData);
968 break;
969 case MSG_GETDEFAULT:
970 twRC = GPHOTO2_Palette8GetDefault (pOrigin, pData);
971 break;
972 case MSG_RESET:
973 twRC = GPHOTO2_Palette8Reset (pOrigin, pData);
974 break;
975 case MSG_SET:
976 twRC = GPHOTO2_Palette8Set (pOrigin, pData);
977 break;
978 default:
979 twRC = TWRC_FAILURE;
980 activeDS.twCC = TWCC_BADPROTOCOL;
981 WARN("unrecognized operation triplet\n");
983 break;
985 case DAT_RGBRESPONSE:
986 switch (MSG)
988 case MSG_RESET:
989 twRC = GPHOTO2_RGBResponseReset (pOrigin, pData);
990 break;
991 case MSG_SET:
992 twRC = GPHOTO2_RGBResponseSet (pOrigin, pData);
993 break;
994 default:
995 twRC = TWRC_FAILURE;
996 activeDS.twCC = TWCC_BADPROTOCOL;
997 WARN("unrecognized operation triplet\n");
998 break;
1000 break;
1002 default:
1003 twRC = TWRC_FAILURE;
1004 activeDS.twCC = TWCC_BADPROTOCOL;
1005 FIXME("unrecognized DG type %d\n", DAT);
1007 return twRC;
1010 /* Main entry point for the TWAIN library */
1011 TW_UINT16 WINAPI
1012 DS_Entry ( pTW_IDENTITY pOrigin,
1013 TW_UINT32 DG,
1014 TW_UINT16 DAT,
1015 TW_UINT16 MSG,
1016 TW_MEMREF pData)
1018 TW_UINT16 twRC = TWRC_SUCCESS; /* Return Code */
1020 TRACE("(DG=%d DAT=%d MSG=%d)\n", DG, DAT, MSG);
1022 switch (DG)
1024 case DG_CONTROL:
1025 twRC = GPHOTO2_SourceControlHandler (pOrigin,DAT,MSG,pData);
1026 break;
1027 case DG_IMAGE:
1028 twRC = GPHOTO2_ImageGroupHandler (pOrigin,DAT,MSG,pData);
1029 break;
1030 case DG_AUDIO:
1031 FIXME("The audio group of entry codes is not implemented.\n");
1032 default:
1033 activeDS.twCC = TWCC_BADPROTOCOL;
1034 twRC = TWRC_FAILURE;
1037 return twRC;
1040 #if defined(HAVE_GPHOTO2) && defined(HAVE_GPHOTO2_PORT)
1041 static GPPortInfoList *port_list;
1042 static int curcamera;
1043 static CameraList *detected_cameras;
1044 static CameraAbilitiesList *abilities_list;
1046 static TW_UINT16
1047 gphoto2_auto_detect(void) {
1048 int result, count;
1050 if (detected_cameras && (gp_list_count (detected_cameras) == 0)) {
1051 /* Reload if previously no cameras, we might detect new ones. */
1052 TRACE("Reloading portlist trying to detect cameras.\n");
1053 if (port_list) {
1054 gp_port_info_list_free (port_list);
1055 port_list = NULL;
1058 if (!port_list) {
1059 TRACE("Auto detecting gphoto cameras.\n");
1060 TRACE("Loading ports...\n");
1061 if (gp_port_info_list_new (&port_list) < GP_OK)
1062 return TWRC_FAILURE;
1063 result = gp_port_info_list_load (port_list);
1064 if (result < 0) {
1065 gp_port_info_list_free (port_list);
1066 return TWRC_FAILURE;
1068 count = gp_port_info_list_count (port_list);
1069 if (count <= 0)
1070 return TWRC_FAILURE;
1071 if (gp_list_new (&detected_cameras) < GP_OK)
1072 return TWRC_FAILURE;
1073 if (!abilities_list) { /* Load only once per program start */
1074 gp_abilities_list_new (&abilities_list);
1075 TRACE("Loading cameras...\n");
1076 gp_abilities_list_load (abilities_list, NULL);
1078 TRACE("Detecting cameras...\n");
1079 gp_abilities_list_detect (abilities_list, port_list, detected_cameras, NULL);
1080 curcamera = 0;
1081 TRACE("%d cameras detected\n", gp_list_count(detected_cameras));
1083 return TWRC_SUCCESS;
1086 static TW_UINT16
1087 GPHOTO2_GetIdentity( pTW_IDENTITY pOrigin, pTW_IDENTITY self) {
1088 int count;
1089 const char *cname, *pname;
1091 if (TWRC_SUCCESS != gphoto2_auto_detect())
1092 return TWRC_FAILURE;
1094 count = gp_list_count (detected_cameras);
1095 if (count < GP_OK) {
1096 gp_list_free (detected_cameras);
1097 return TWRC_FAILURE;
1099 TRACE("%d cameras detected.\n", count);
1100 self->ProtocolMajor = TWON_PROTOCOLMAJOR;
1101 self->ProtocolMinor = TWON_PROTOCOLMINOR;
1102 self->SupportedGroups = DG_CONTROL | DG_IMAGE | DF_DS2;
1103 lstrcpynA (self->Manufacturer, "The Wine Team", sizeof(self->Manufacturer) - 1);
1104 lstrcpynA (self->ProductFamily, "GPhoto2 Camera", sizeof(self->ProductFamily) - 1);
1106 if (!count) { /* No camera detected. But we need to return an IDENTITY anyway. */
1107 lstrcpynA (self->ProductName, "GPhoto2 Camera", sizeof(self->ProductName) - 1);
1108 return TWRC_SUCCESS;
1110 gp_list_get_name (detected_cameras, curcamera, &cname);
1111 gp_list_get_value (detected_cameras, curcamera, &pname);
1112 if (count == 1) /* Normal case, only one camera. */
1113 snprintf (self->ProductName, sizeof(self->ProductName), "%s", cname);
1114 else
1115 snprintf (self->ProductName, sizeof(self->ProductName), "%s@%s", cname, pname);
1116 curcamera = (curcamera+1) % count;
1117 return TWRC_SUCCESS;
1120 static TW_UINT16
1121 GPHOTO2_OpenDS( pTW_IDENTITY pOrigin, pTW_IDENTITY self) {
1122 int ret, m, p, count, i;
1123 CameraAbilities a;
1124 GPPortInfo info;
1125 const char *model, *port;
1127 if (GPHOTO2_dsmentry == NULL)
1129 static const WCHAR twain32W[] = {'t','w','a','i','n','_','3','2',0};
1130 HMODULE moddsm = GetModuleHandleW(twain32W);
1132 if (moddsm)
1133 GPHOTO2_dsmentry = (void*)GetProcAddress(moddsm, "DSM_Entry");
1135 if (!GPHOTO2_dsmentry)
1137 ERR("can't find DSM entry point\n");
1138 return TWRC_FAILURE;
1142 if (TWRC_SUCCESS != gphoto2_auto_detect())
1143 return TWRC_FAILURE;
1145 if (lstrcmpA(self->ProductFamily,"GPhoto2 Camera")) {
1146 FIXME("identity passed is not a gphoto camera, but %s!?!\n", self->ProductFamily);
1147 return TWRC_FAILURE;
1149 count = gp_list_count (detected_cameras);
1150 if (!count) {
1151 ERR("No camera found by autodetection. Returning failure.\n");
1152 return TWRC_FAILURE;
1155 if (!lstrcmpA (self->ProductName, "GPhoto2 Camera")) {
1156 TRACE("Potential undetected camera. Just using the first autodetected one.\n");
1157 i = 0;
1158 } else {
1159 for (i=0;i<count;i++) {
1160 const char *cname, *pname;
1161 TW_STR32 name;
1163 gp_list_get_name (detected_cameras, i, &cname);
1164 gp_list_get_value (detected_cameras, i, &pname);
1165 if (!lstrcmpA(self->ProductName,cname))
1166 break;
1167 snprintf(name, sizeof(name), "%s", cname);
1168 if (!lstrcmpA(self->ProductName,name))
1169 break;
1170 snprintf(name, sizeof(name), "%s@%s", cname, pname);
1171 if (!lstrcmpA(self->ProductName,name))
1172 break;
1174 if (i == count) {
1175 TRACE("Camera %s not found in autodetected list. Using first entry.\n", self->ProductName);
1176 i=0;
1179 gp_list_get_name (detected_cameras, i, &model);
1180 gp_list_get_value (detected_cameras, i, &port);
1181 TRACE("model %s, port %s\n", model, port);
1182 ret = gp_camera_new (&activeDS.camera);
1183 if (ret < GP_OK) {
1184 ERR("gp_camera_new: %d\n", ret);
1185 return TWRC_FAILURE;
1187 m = gp_abilities_list_lookup_model (abilities_list, model);
1188 if (m < GP_OK) {
1189 FIXME("Model %s not found, %d!\n", model, m);
1190 return TWRC_FAILURE;
1192 ret = gp_abilities_list_get_abilities (abilities_list, m, &a);
1193 if (ret < GP_OK) {
1194 FIXME("gp_camera_list_get_abilities failed? %d\n", ret);
1195 return TWRC_FAILURE;
1197 ret = gp_camera_set_abilities (activeDS.camera, a);
1198 if (ret < GP_OK) {
1199 FIXME("gp_camera_set_abilities failed? %d\n", ret);
1200 return TWRC_FAILURE;
1203 p = gp_port_info_list_lookup_path (port_list, port);
1204 if (p < GP_OK) {
1205 FIXME("port %s not in portlist?\n", port);
1206 return TWRC_FAILURE;
1208 ret = gp_port_info_list_get_info (port_list, p, &info);
1209 if (ret < GP_OK) {
1210 FIXME("could not get portinfo for port %s?\n", port);
1211 return TWRC_FAILURE;
1213 ret = gp_camera_set_port_info (activeDS.camera, info);
1214 if (ret < GP_OK) {
1215 FIXME("could not set portinfo for port %s to camera?\n", port);
1216 return TWRC_FAILURE;
1218 list_init( &(activeDS.files) );
1219 activeDS.currentState = 4;
1220 activeDS.twCC = TWRC_SUCCESS;
1221 activeDS.pixelflavor = TWPF_CHOCOLATE;
1222 activeDS.pixeltype = TWPT_RGB;
1223 activeDS.capXferMech = TWSX_MEMORY;
1224 activeDS.identity.Id = self->Id;
1225 activeDS.appIdentity = *pOrigin;
1226 TRACE("OK!\n");
1227 return TWRC_SUCCESS;
1229 #endif