2 * TWAIN32 Source Manager
4 * Copyright 2000 Corel Corporation
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
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
36 static TW_UINT16 DSM_initialized
; /* whether Source Manager is initialized */
37 static TW_UINT32 DSM_sourceId
; /* source id generator */
38 static TW_UINT16 DSM_currentDevice
; /* keep track of device during enumeration */
39 static HWND DSM_parent
;
40 static UINT event_message
;
47 static int nrdevices
= 0;
48 static struct all_devices
*devices
= NULL
;
51 twain_add_onedriver(const char *dsname
) {
54 TW_IDENTITY fakeOrigin
;
58 hmod
= LoadLibraryA(dsname
);
60 ERR("Failed to load TWAIN Source %s\n", debugstr_a(dsname
));
63 dsEntry
= (DSENTRYPROC
)GetProcAddress(hmod
, "DS_Entry");
65 ERR("Failed to find DS_Entry() in TWAIN DS %s\n", debugstr_a(dsname
));
68 /* Loop to do multiple detects, mostly for sane.ds and gphoto2.ds */
72 sourceId
.Id
= DSM_sourceId
;
73 sourceId
.ProtocolMajor
= TWON_PROTOCOLMAJOR
;
74 sourceId
.ProtocolMinor
= TWON_PROTOCOLMINOR
;
75 ret
= dsEntry (&fakeOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_GET
, &sourceId
);
76 if (ret
!= TWRC_SUCCESS
) {
77 ERR("Source->(DG_CONTROL,DAT_IDENTITY,MSG_GET) failed!\n");
80 TRACE("Manufacturer: %s\n", debugstr_a(sourceId
.Manufacturer
));
81 TRACE("ProductFamily: %s\n", debugstr_a(sourceId
.ProductFamily
));
82 TRACE("ProductName: %s\n", debugstr_a(sourceId
.ProductName
));
84 for (i
=0;i
<nrdevices
;i
++) {
85 if (!strcmp(sourceId
.ProductName
,devices
[i
].identity
.ProductName
))
91 devices
= HeapReAlloc(GetProcessHeap(), 0, devices
, sizeof(devices
[0])*(nrdevices
+1));
93 devices
= HeapAlloc(GetProcessHeap(), 0, sizeof(devices
[0]));
94 if ((devices
[nrdevices
].modname
= HeapAlloc(GetProcessHeap(), 0, strlen(dsname
) + 1)))
95 lstrcpyA(devices
[nrdevices
].modname
, dsname
);
96 devices
[nrdevices
].identity
= sourceId
;
103 static BOOL detectionrun
= FALSE
;
106 twain_autodetect(void) {
107 if (detectionrun
) return;
110 twain_add_onedriver("sane.ds");
111 twain_add_onedriver("gphoto2.ds");
113 twain_add_onedriver("c:\\windows\\Twain_32\\Largan\\sp503a.ds");
114 twain_add_onedriver("c:\\windows\\Twain_32\\vivicam10\\vivicam10.ds");
115 twain_add_onedriver("c:\\windows\\Twain_32\\ws30slim\\sp500a.ds");
119 /* DG_CONTROL/DAT_NULL/MSG_CLOSEDSREQ|MSG_DEVICEEVENT|MSG_XFERREADY */
120 TW_UINT16
TWAIN_ControlNull (pTW_IDENTITY pOrigin
, pTW_IDENTITY pDest
, activeDS
*pSource
, TW_UINT16 MSG
, TW_MEMREF pData
)
122 struct pending_message
*message
;
124 TRACE ("DG_CONTROL/DAT_NULL MSG=%i\n", MSG
);
126 if (MSG
!= MSG_CLOSEDSREQ
&&
127 MSG
!= MSG_DEVICEEVENT
&&
128 MSG
!= MSG_XFERREADY
)
130 DSM_twCC
= TWCC_BADPROTOCOL
;
134 message
= HeapAlloc(GetProcessHeap(), 0, sizeof(*message
));
137 DSM_twCC
= TWCC_LOWMEMORY
;
142 list_add_tail(&pSource
->pending_messages
, &message
->entry
);
144 /* Delphi twain only sends us messages from one window, and it
145 doesn't even give us the real handle to that window. Other
146 applications might decide to forward messages sent to DSM_parent
147 or to the one supplied to ENABLEDS. So let's try very hard to
148 find a window that will work. */
150 PostMessageW(DSM_parent
, event_message
, 0, 0);
151 if (pSource
->ui_window
&& pSource
->ui_window
!= DSM_parent
)
152 PostMessageW(pSource
->ui_window
, event_message
, 0, 0);
153 if (pSource
->event_window
&& pSource
->event_window
!= pSource
->ui_window
&&
154 pSource
->event_window
!= DSM_parent
)
155 PostMessageW(pSource
->event_window
, event_message
, 0, 0);
156 PostMessageW(0, event_message
, 0, 0);
161 /* Filters MSG_PROCESSEVENT messages before reaching the data source */
162 TW_UINT16
TWAIN_ProcessEvent (pTW_IDENTITY pOrigin
, activeDS
*pSource
, TW_MEMREF pData
)
164 TW_EVENT
*event
= (TW_EVENT
*)pData
;
165 MSG
*msg
= (MSG
*)event
->pEvent
;
166 TW_UINT16 result
= TWRC_NOTDSEVENT
;
168 TRACE("%x,%x\n", msg
->message
, event_message
);
170 if (msg
->message
== event_message
)
172 if (!list_empty (&pSource
->pending_messages
))
174 struct list
*entry
= list_head (&pSource
->pending_messages
);
175 struct pending_message
*message
= LIST_ENTRY(entry
, struct pending_message
, entry
);
176 event
->TWMessage
= message
->msg
;
178 TRACE("<-- %x\n", event
->TWMessage
);
181 event
->TWMessage
= MSG_NULL
;
182 result
= TWRC_DSEVENT
;
188 pSource
->event_window
= msg
->hwnd
;
189 if (!list_empty (&pSource
->pending_messages
) &&
190 !PeekMessageW(&dummy
, msg
->hwnd
, event_message
, event_message
, PM_NOREMOVE
))
192 PostMessageW(msg
->hwnd
, event_message
, 0, 0);
199 /* DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS */
200 TW_UINT16
TWAIN_CloseDS (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
202 TW_UINT16 twRC
= TWRC_SUCCESS
;
203 pTW_IDENTITY pIdentity
= (pTW_IDENTITY
) pData
;
204 activeDS
*currentDS
= NULL
, *prevDS
= NULL
;
206 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS\n");
208 for (currentDS
= activeSources
; currentDS
; currentDS
= currentDS
->next
) {
209 if (currentDS
->identity
.Id
== pIdentity
->Id
)
214 DSM_twCC
= TWCC_NODS
;
217 twRC
= currentDS
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_CLOSEDS
, pData
);
218 /* This causes crashes due to still open Windows, so leave out for now.
219 * FreeLibrary (currentDS->hmod);
222 prevDS
->next
= currentDS
->next
;
224 activeSources
= currentDS
->next
;
225 HeapFree (GetProcessHeap(), 0, currentDS
);
226 if (twRC
== TWRC_SUCCESS
)
227 DSM_twCC
= TWCC_SUCCESS
;
228 else /* FIXME: unclear how to get TWCC */
229 DSM_twCC
= TWCC_SEQERROR
;
233 /* DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT */
234 TW_UINT16
TWAIN_IdentityGetDefault (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
236 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
238 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT\n");
239 DSM_twCC
= TWCC_NODS
;
243 *pSourceIdentity
= devices
[0].identity
;
244 DSM_twCC
= TWCC_SUCCESS
;
248 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
249 TW_UINT16
TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
251 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
253 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
256 TRACE ("no entries found.\n");
257 DSM_twCC
= TWCC_NODS
;
260 DSM_currentDevice
= 0;
261 *pSourceIdentity
= devices
[DSM_currentDevice
++].identity
;
265 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
266 TW_UINT16
TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
268 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
270 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
271 if (!nrdevices
|| (DSM_currentDevice
== nrdevices
)) {
272 DSM_twCC
= TWCC_SUCCESS
;
273 return TWRC_ENDOFLIST
;
275 *pSourceIdentity
= devices
[DSM_currentDevice
++].identity
;
279 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
280 TW_UINT16
TWAIN_OpenDS (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
283 pTW_IDENTITY pIdentity
= (pTW_IDENTITY
) pData
;
285 const char *modname
= NULL
;
288 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_OPENDS\n");
289 TRACE("pIdentity is %s\n", pIdentity
->ProductName
);
290 if (!DSM_initialized
) {
291 FIXME("seq error\n");
292 DSM_twCC
= TWCC_SEQERROR
;
298 DSM_twCC
= TWCC_NODS
;
302 if (pIdentity
->ProductName
[0] != '\0') {
303 /* Make sure the source to be opened exists in the device list */
304 for (i
= 0; i
<nrdevices
; i
++)
305 if (!strcmp (devices
[i
].identity
.ProductName
, pIdentity
->ProductName
))
309 } /* else use the first device */
311 /* the source is found in the device list */
312 newSource
= HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS
));
314 DSM_twCC
= TWCC_LOWMEMORY
;
315 FIXME("Out of memory.\n");
318 hmod
= LoadLibraryA(devices
[i
].modname
);
320 ERR("Failed to load TWAIN Source %s\n", debugstr_a(modname
));
321 DSM_twCC
= TWCC_OPERATIONERROR
;
322 HeapFree(GetProcessHeap(), 0, newSource
);
325 newSource
->hmod
= hmod
;
326 newSource
->dsEntry
= (DSENTRYPROC
)GetProcAddress(hmod
, "DS_Entry");
327 /* Assign id for the opened data source */
328 pIdentity
->Id
= DSM_sourceId
++;
329 if (TWRC_SUCCESS
!= newSource
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_OPENDS
, pIdentity
)) {
330 DSM_twCC
= TWCC_OPERATIONERROR
;
331 HeapFree(GetProcessHeap(), 0, newSource
);
335 /* add the data source to an internal active source list */
336 newSource
->next
= activeSources
;
337 newSource
->identity
.Id
= pIdentity
->Id
;
338 strcpy (newSource
->identity
.ProductName
, pIdentity
->ProductName
);
339 list_init(&newSource
->pending_messages
);
340 newSource
->ui_window
= NULL
;
341 newSource
->event_window
= NULL
;
342 activeSources
= newSource
;
343 DSM_twCC
= TWCC_SUCCESS
;
352 static INT_PTR CALLBACK
userselect_dlgproc (HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
358 userselect_data
*data
= (userselect_data
*)lparam
;
361 BOOL any_devices
= FALSE
;
363 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LONG_PTR
)data
);
365 sourcelist
= GetDlgItem(hwnd
, IDC_LISTSOURCE
);
367 for (i
=0; i
<nrdevices
; i
++)
369 TW_IDENTITY
*id
= &devices
[i
].identity
;
372 if ((id
->SupportedGroups
& data
->origin
->SupportedGroups
) == 0)
375 index
= SendMessageA(sourcelist
, LB_ADDSTRING
, 0, (LPARAM
)id
->ProductName
);
376 SendMessageW(sourcelist
, LB_SETITEMDATA
, (WPARAM
)index
, (LPARAM
)i
);
382 EnableWindow(GetDlgItem(hwnd
, IDOK
), TRUE
);
384 /* FIXME: Select the supplied product name or default source. */
385 SendMessageW(sourcelist
, LB_SETCURSEL
, 0, 0);
394 if (wparam
== MAKEWPARAM(IDCANCEL
, BN_CLICKED
))
399 else if (wparam
== MAKEWPARAM(IDOK
, BN_CLICKED
) ||
400 wparam
== MAKEWPARAM(IDC_LISTSOURCE
, LBN_DBLCLK
))
402 userselect_data
*data
= (userselect_data
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
406 sourcelist
= GetDlgItem(hwnd
, IDC_LISTSOURCE
);
408 index
= SendMessageW(sourcelist
, LB_GETCURSEL
, 0, 0);
413 index
= SendMessageW(sourcelist
, LB_GETITEMDATA
, (WPARAM
)index
, 0);
415 *data
->result
= devices
[index
].identity
;
417 /* FIXME: Save this as the default source */
427 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
428 TW_UINT16
TWAIN_UserSelect (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
430 userselect_data param
= {pOrigin
, pData
};
431 HWND parent
= DSM_parent
;
433 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT SupportedGroups=0x%x ProductName=%s\n",
434 pOrigin
->SupportedGroups
, wine_dbgstr_a(param
.result
->ProductName
));
438 if (!IsWindow(parent
))
441 if (DialogBoxParamW(DSM_hinstance
, MAKEINTRESOURCEW(DLG_USERSELECT
),
442 parent
, userselect_dlgproc
, (LPARAM
)¶m
) == 0)
445 DSM_twCC
= TWCC_SUCCESS
;
449 TRACE("<-- %s\n", wine_dbgstr_a(param
.result
->ProductName
));
450 DSM_twCC
= TWCC_SUCCESS
;
454 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
455 TW_UINT16
TWAIN_CloseDSM (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
457 activeDS
*currentDS
= activeSources
, *nextDS
;
459 TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
463 DSM_initialized
= FALSE
;
465 /* If there are data sources still open, close them now. */
466 while (currentDS
!= NULL
)
468 nextDS
= currentDS
->next
;
469 currentDS
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_CLOSEDS
, pData
);
470 HeapFree (GetProcessHeap(), 0, currentDS
);
473 activeSources
= NULL
;
475 DSM_twCC
= TWCC_SUCCESS
;
478 DSM_twCC
= TWCC_SEQERROR
;
483 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
484 TW_UINT16
TWAIN_OpenDSM (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
486 TW_UINT16 twRC
= TWRC_SUCCESS
;
488 TRACE("DG_CONTROL/DAT_PARENT/MSG_OPENDSM\n");
489 if (!DSM_initialized
) {
490 event_message
= RegisterWindowMessageA("WINE TWAIN_32 EVENT");
491 DSM_currentDevice
= 0;
492 DSM_initialized
= TRUE
;
493 DSM_twCC
= TWCC_SUCCESS
;
496 /* operation invoked in invalid state */
497 DSM_twCC
= TWCC_SEQERROR
;
500 DSM_parent
= (HWND
)pData
;
504 /* DG_CONTROL/DAT_STATUS/MSG_GET */
505 TW_UINT16
TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
507 pTW_STATUS pSourceStatus
= (pTW_STATUS
) pData
;
509 TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
511 pSourceStatus
->ConditionCode
= DSM_twCC
;
512 DSM_twCC
= TWCC_SUCCESS
; /* clear the condition code */