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
30 #include "gphoto2_i.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
35 HINSTANCE GPHOTO2_instance
;
37 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
39 TRACE("%p,%x,%p\n", hinstDLL
, fdwReason
, lpvReserved
);
43 case DLL_PROCESS_ATTACH
:
44 GPHOTO2_instance
= hinstDLL
;
45 DisableThreadLibraryCalls(hinstDLL
);
47 activeDS
.context
= gp_context_new ();
51 case DLL_PROCESS_DETACH
:
52 GPHOTO2_instance
= NULL
;
60 static TW_UINT16
GPHOTO2_GetIdentity( pTW_IDENTITY
, pTW_IDENTITY
);
61 static TW_UINT16
GPHOTO2_OpenDS( pTW_IDENTITY
, pTW_IDENTITY
);
64 static TW_UINT16
GPHOTO2_SourceControlHandler (
70 TW_UINT16 twRC
= TWRC_SUCCESS
;
79 if (activeDS
.camera
) {
80 gp_camera_free (activeDS
.camera
);
81 activeDS
.camera
= NULL
;
87 twRC
= GPHOTO2_GetIdentity(pOrigin
,(pTW_IDENTITY
)pData
);
94 twRC
= GPHOTO2_OpenDS(pOrigin
,(pTW_IDENTITY
)pData
);
105 twRC
= GPHOTO2_CapabilityGet (pOrigin
, pData
);
108 twRC
= GPHOTO2_CapabilityGetCurrent (pOrigin
, pData
);
111 twRC
= GPHOTO2_CapabilityGetDefault (pOrigin
, pData
);
113 case MSG_QUERYSUPPORT
:
114 twRC
= GPHOTO2_CapabilityQuerySupport (pOrigin
, pData
);
117 twRC
= GPHOTO2_CapabilityReset (pOrigin
, pData
);
120 twRC
= GPHOTO2_CapabilitySet (pOrigin
, pData
);
124 FIXME("unrecognized opertion triplet\n");
128 case DAT_CUSTOMDSDATA
:
132 twRC
= GPHOTO2_CustomDSDataGet (pOrigin
, pData
);
135 twRC
= GPHOTO2_CustomDSDataSet (pOrigin
, pData
);
145 /*case MSG_AUTOMATICCAPTUREDIRECTORY:
146 twRC = GPHOTO2_AutomaticCaptureDirectory
149 case MSG_CHANGEDIRECTORY
:
150 twRC
= GPHOTO2_ChangeDirectory (pOrigin
, pData
);
153 twRC = GPHOTO2_FileSystemCopy (pOrigin, pData);
155 case MSG_CREATEDIRECTORY
:
156 twRC
= GPHOTO2_CreateDirectory (pOrigin
, pData
);
159 twRC
= GPHOTO2_FileSystemDelete (pOrigin
, pData
);
161 case MSG_FORMATMEDIA
:
162 twRC
= GPHOTO2_FormatMedia (pOrigin
, pData
);
165 twRC
= GPHOTO2_FileSystemGetClose (pOrigin
, pData
);
167 case MSG_GETFIRSTFILE
:
168 twRC
= GPHOTO2_FileSystemGetFirstFile (pOrigin
, pData
);
171 twRC
= GPHOTO2_FileSystemGetInfo (pOrigin
, pData
);
173 case MSG_GETNEXTFILE
:
174 twRC
= GPHOTO2_FileSystemGetNextFile (pOrigin
, pData
);
177 twRC
= GPHOTO2_FileSystemRename (pOrigin
, pData
);
186 if (MSG
== MSG_PROCESSEVENT
)
187 twRC
= GPHOTO2_ProcessEvent (pOrigin
, pData
);
193 if (MSG
== MSG_PASSTHRU
)
194 twRC
= GPHOTO2_PassThrough (pOrigin
, pData
);
199 case DAT_PENDINGXFERS
:
203 twRC
= GPHOTO2_PendingXfersEndXfer (pOrigin
, pData
);
206 twRC
= GPHOTO2_PendingXfersGet (pOrigin
, pData
);
209 twRC
= GPHOTO2_PendingXfersReset (pOrigin
, pData
);
211 /*case MSG_STOPFEEDER:
212 twRC = GPHOTO2_PendingXfersStopFeeder (pOrigin, pData);
219 case DAT_SETUPFILEXFER
:
223 twRC
= GPHOTO2_SetupFileXferGet (pOrigin
, pData
);
226 twRC
= GPHOTO2_SetupFileXferGetDefault (pOrigin
, pData
);
229 twRC
= GPHOTO2_SetupFileXferReset (pOrigin
, pData
);
232 twRC
= GPHOTO2_SetupFileXferSet (pOrigin
, pData
);
240 /*case DAT_SETUPFILEXFER2:
244 twRC = GPHOTO2_SetupFileXfer2Get (pOrigin, pData);
247 twRC = GPHOTO2_SetupFileXfer2GetDefault (pOrigin, pData);
250 twRC = GPHOTO2_SetupFileXfer2Reset (pOrigin, pData);
253 twRC = GPHOTO2_SetupFileXfer2Set (pOrigin, pData);
257 case DAT_SETUPMEMXFER
:
259 twRC
= GPHOTO2_SetupMemXferGet (pOrigin
, pData
);
266 twRC
= GPHOTO2_GetDSStatus (pOrigin
, pData
);
271 case DAT_USERINTERFACE
:
275 twRC
= GPHOTO2_DisableDSUserInterface (pOrigin
, pData
);
278 twRC
= GPHOTO2_EnableDSUserInterface (pOrigin
, pData
);
280 case MSG_ENABLEDSUIONLY
:
281 twRC
= GPHOTO2_EnableDSUIOnly (pOrigin
, pData
);
293 twRC
= GPHOTO2_XferGroupGet (pOrigin
, pData
);
296 twRC
= GPHOTO2_XferGroupSet (pOrigin
, pData
);
305 FIXME("code unknown: %d\n", DAT
);
314 TW_UINT16
GPHOTO2_ImageGroupHandler (
315 pTW_IDENTITY pOrigin
,
320 TW_UINT16 twRC
= TWRC_SUCCESS
;
326 twRC
= GPHOTO2_CIEColorGet (pOrigin
, pData
);
331 case DAT_EXTIMAGEINFO
:
333 twRC
= GPHOTO2_ExtImageInfoGet (pOrigin
, pData
);
338 case DAT_GRAYRESPONSE
:
342 twRC
= GPHOTO2_GrayResponseReset (pOrigin
, pData
);
345 twRC
= GPHOTO2_GrayResponseSet (pOrigin
, pData
);
349 activeDS
.twCC
= TWCC_BADPROTOCOL
;
350 FIXME("unrecognized operation triplet\n");
354 case DAT_IMAGEFILEXFER
:
356 twRC
= GPHOTO2_ImageFileXferGet (pOrigin
, pData
);
363 twRC
= GPHOTO2_ImageInfoGet (pOrigin
, pData
);
368 case DAT_IMAGELAYOUT
:
372 twRC
= GPHOTO2_ImageLayoutGet (pOrigin
, pData
);
375 twRC
= GPHOTO2_ImageLayoutGetDefault (pOrigin
, pData
);
378 twRC
= GPHOTO2_ImageLayoutReset (pOrigin
, pData
);
381 twRC
= GPHOTO2_ImageLayoutSet (pOrigin
, pData
);
385 activeDS
.twCC
= TWCC_BADPROTOCOL
;
386 ERR("unrecognized operation triplet\n");
391 case DAT_IMAGEMEMXFER
:
393 twRC
= GPHOTO2_ImageMemXferGet (pOrigin
, pData
);
398 case DAT_IMAGENATIVEXFER
:
400 twRC
= GPHOTO2_ImageNativeXferGet (pOrigin
, pData
);
405 case DAT_JPEGCOMPRESSION
:
409 twRC
= GPHOTO2_JPEGCompressionGet (pOrigin
, pData
);
412 twRC
= GPHOTO2_JPEGCompressionGetDefault (pOrigin
, pData
);
415 twRC
= GPHOTO2_JPEGCompressionReset (pOrigin
, pData
);
418 twRC
= GPHOTO2_JPEGCompressionSet (pOrigin
, pData
);
422 activeDS
.twCC
= TWCC_BADPROTOCOL
;
423 WARN("unrecognized operation triplet\n");
432 twRC
= GPHOTO2_Palette8Get (pOrigin
, pData
);
435 twRC
= GPHOTO2_Palette8GetDefault (pOrigin
, pData
);
438 twRC
= GPHOTO2_Palette8Reset (pOrigin
, pData
);
441 twRC
= GPHOTO2_Palette8Set (pOrigin
, pData
);
445 activeDS
.twCC
= TWCC_BADPROTOCOL
;
446 WARN("unrecognized operation triplet\n");
450 case DAT_RGBRESPONSE
:
454 twRC
= GPHOTO2_RGBResponseReset (pOrigin
, pData
);
457 twRC
= GPHOTO2_RGBResponseSet (pOrigin
, pData
);
461 activeDS
.twCC
= TWCC_BADPROTOCOL
;
462 WARN("unrecognized operation triplet\n");
469 activeDS
.twCC
= TWCC_BADPROTOCOL
;
470 FIXME("unrecognized DG type %d\n", DAT
);
475 /* Main entry point for the TWAIN library */
477 DS_Entry ( pTW_IDENTITY pOrigin
,
483 TW_UINT16 twRC
= TWRC_SUCCESS
; /* Return Code */
485 TRACE("(DG=%ld DAT=%d MSG=%d)\n", DG
, DAT
, MSG
);
490 twRC
= GPHOTO2_SourceControlHandler (pOrigin
,DAT
,MSG
,pData
);
493 twRC
= GPHOTO2_ImageGroupHandler (pOrigin
,DAT
,MSG
,pData
);
496 FIXME("The audio group of entry codes is not implemented.\n");
498 activeDS
.twCC
= TWCC_BADPROTOCOL
;
506 static GPPortInfoList
*port_list
;
507 static int curcamera
;
508 static CameraList
*detected_cameras
;
509 static CameraAbilitiesList
*abilities_list
;
512 gphoto2_auto_detect(void) {
515 if (detected_cameras
&& (gp_list_count (detected_cameras
) == 0)) {
516 /* Reload if previously no cameras, we might detect new ones. */
517 TRACE("Reloading portlist trying to detect cameras.\n");
519 gp_port_info_list_free (port_list
);
524 TRACE("Auto detecting gphoto cameras.\n");
525 TRACE("Loading ports...\n");
526 if (gp_port_info_list_new (&port_list
) < GP_OK
)
528 result
= gp_port_info_list_load (port_list
);
530 gp_port_info_list_free (port_list
);
533 count
= gp_port_info_list_count (port_list
);
536 if (gp_list_new (&detected_cameras
) < GP_OK
)
538 if (!abilities_list
) { /* Load only once per program start */
539 gp_abilities_list_new (&abilities_list
);
540 TRACE("Loading cameras...\n");
541 gp_abilities_list_load (abilities_list
, NULL
);
543 TRACE("Detecting cameras...\n");
544 gp_abilities_list_detect (abilities_list
, port_list
, detected_cameras
, NULL
);
546 TRACE("%d cameras detected\n", gp_list_count(detected_cameras
));
552 GPHOTO2_GetIdentity( pTW_IDENTITY pOrigin
, pTW_IDENTITY self
) {
554 const char *cname
, *pname
;
556 if (TWRC_SUCCESS
!= gphoto2_auto_detect())
559 count
= gp_list_count (detected_cameras
);
561 gp_list_free (detected_cameras
);
564 TRACE("%d cameras detected.\n", count
);
565 self
->ProtocolMajor
= TWON_PROTOCOLMAJOR
;
566 self
->ProtocolMinor
= TWON_PROTOCOLMINOR
;
567 lstrcpynA (self
->Manufacturer
, "The Wine Team", sizeof(self
->Manufacturer
) - 1);
568 lstrcpynA (self
->ProductFamily
, "GPhoto2 Camera", sizeof(self
->ProductFamily
) - 1);
570 if (!count
) { /* No camera detected. But we need to return an IDENTITY anyway. */
571 lstrcpynA (self
->ProductName
, "GPhoto2 Camera", sizeof(self
->ProductName
) - 1);
574 gp_list_get_name (detected_cameras
, curcamera
, &cname
);
575 gp_list_get_value (detected_cameras
, curcamera
, &pname
);
576 if (count
== 1) /* Normal case, only one camera. */
577 snprintf (self
->ProductName
, sizeof(self
->ProductName
), "%s", cname
);
579 snprintf (self
->ProductName
, sizeof(self
->ProductName
), "%s@%s", cname
, pname
);
580 curcamera
= (curcamera
+1) % count
;
585 GPHOTO2_OpenDS( pTW_IDENTITY pOrigin
, pTW_IDENTITY self
) {
586 int ret
, m
, p
, count
, i
;
589 const char *model
, *port
;
591 if (TWRC_SUCCESS
!= gphoto2_auto_detect())
594 if (lstrcmpA(self
->ProductFamily
,"GPhoto2 Camera")) {
595 FIXME("identity passed is not a gphoto camera, but %s!?!\n", self
->ProductFamily
);
598 count
= gp_list_count (detected_cameras
);
600 ERR("No camera found by autodetection. Returning failure.\n");
604 if (!lstrcmpA (self
->ProductName
, "GPhoto2 Camera")) {
605 TRACE("Potential undetected camera. Just using the first autodetected one.\n");
608 for (i
=0;i
<count
;i
++) {
609 const char *cname
, *pname
;
612 gp_list_get_name (detected_cameras
, i
, &cname
);
613 gp_list_get_value (detected_cameras
, i
, &pname
);
614 if (!lstrcmpA(self
->ProductName
,cname
))
616 snprintf(name
, sizeof(name
), "%s", cname
);
617 if (!lstrcmpA(self
->ProductName
,name
))
619 snprintf(name
, sizeof(name
), "%s@%s", cname
, pname
);
620 if (!lstrcmpA(self
->ProductName
,name
))
624 TRACE("Camera %s not found in autodetected list. Using first entry.\n", self
->ProductName
);
628 gp_list_get_name (detected_cameras
, i
, &model
);
629 gp_list_get_value (detected_cameras
, i
, &port
);
630 TRACE("model %s, port %s\n", model
, port
);
631 ret
= gp_camera_new (&activeDS
.camera
);
633 ERR("gp_camera_new: %d\n", ret
);
636 m
= gp_abilities_list_lookup_model (abilities_list
, model
);
638 FIXME("Model %s not found, %d!\n", model
, m
);
641 ret
= gp_abilities_list_get_abilities (abilities_list
, m
, &a
);
643 FIXME("gp_camera_list_get_abilities failed? %d\n", ret
);
646 ret
= gp_camera_set_abilities (activeDS
.camera
, a
);
648 FIXME("gp_camera_set_abilities failed? %d\n", ret
);
652 p
= gp_port_info_list_lookup_path (port_list
, port
);
654 FIXME("port %s not in portlist?\n", port
);
657 ret
= gp_port_info_list_get_info (port_list
, p
, &info
);
659 FIXME("could not get portinfo for port %s?\n", port
);
662 ret
= gp_camera_set_port_info (activeDS
.camera
, info
);
664 FIXME("could not set portinfo for port %s to camera?\n", port
);
667 list_init( &(activeDS
.files
) );
668 activeDS
.currentState
= 4;
669 activeDS
.twCC
= TWRC_SUCCESS
;
670 activeDS
.pixelflavor
= TWPF_CHOCOLATE
;
671 activeDS
.pixeltype
= TWPT_RGB
;
672 activeDS
.capXferMech
= TWSX_MEMORY
;