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
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
43 static int nrdevices
= 0;
44 static struct all_devices
*devices
= NULL
;
47 twain_add_onedriver(const char *dsname
) {
50 TW_IDENTITY fakeOrigin
;
54 hmod
= LoadLibraryA(dsname
);
56 ERR("Failed to load TWAIN Source %s\n", dsname
);
59 dsEntry
= (DSENTRYPROC
)GetProcAddress(hmod
, "DS_Entry");
61 ERR("Failed to find DS_Entry() in TWAIN DS %s\n", dsname
);
64 /* Loop to do multiple detects, mostly for sane.ds and gphoto2.ds */
68 sourceId
.Id
= DSM_sourceId
;
69 sourceId
.ProtocolMajor
= TWON_PROTOCOLMAJOR
;
70 sourceId
.ProtocolMinor
= TWON_PROTOCOLMINOR
;
71 ret
= dsEntry (&fakeOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_GET
, &sourceId
);
72 if (ret
!= TWRC_SUCCESS
) {
73 ERR("Source->(DG_CONTROL,DAT_IDENTITY,MSG_GET) failed!\n");
76 TRACE("Manufacturer: %s\n", debugstr_a(sourceId
.Manufacturer
));
77 TRACE("ProductFamily: %s\n", debugstr_a(sourceId
.ProductFamily
));
78 TRACE("ProductName: %s\n", debugstr_a(sourceId
.ProductName
));
80 for (i
=0;i
<nrdevices
;i
++) {
81 if (!strcmp(sourceId
.ProductName
,devices
[i
].identity
.ProductName
))
87 devices
= realloc(devices
, sizeof(devices
[0])*(nrdevices
+1));
89 devices
= malloc(sizeof(devices
[0]));
90 devices
[nrdevices
].modname
= strdup(dsname
);
91 devices
[nrdevices
].identity
= sourceId
;
98 static int detectionrun
= 0;
101 twain_autodetect(void) {
102 if (detectionrun
) return;
105 twain_add_onedriver("gphoto2.ds");
106 twain_add_onedriver("sane.ds");
108 twain_add_onedriver("c:\\windows\\Twain_32\\Largan\\sp503a.ds");
109 twain_add_onedriver("c:\\windows\\Twain_32\\vivicam10\\vivicam10.ds");
110 twain_add_onedriver("c:\\windows\\Twain_32\\ws30slim\\sp500a.ds");
114 /* DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS */
115 TW_UINT16
TWAIN_CloseDS (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
117 TW_UINT16 twRC
= TWRC_SUCCESS
;
118 pTW_IDENTITY pIdentity
= (pTW_IDENTITY
) pData
;
119 activeDS
*currentDS
= NULL
, *prevDS
= NULL
;
121 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS\n");
123 for (currentDS
= activeSources
; currentDS
; currentDS
= currentDS
->next
) {
124 if (currentDS
->identity
.Id
== pIdentity
->Id
)
129 DSM_twCC
= TWCC_NODS
;
132 twRC
= currentDS
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_CLOSEDS
, pData
);
133 /* This causes crashes due to still open Windows, so leave out for now.
134 * FreeLibrary (currentDS->hmod);
137 prevDS
->next
= currentDS
->next
;
139 activeSources
= currentDS
->next
;
140 HeapFree (GetProcessHeap(), 0, currentDS
);
141 if (twRC
== TWRC_SUCCESS
)
142 DSM_twCC
= TWCC_SUCCESS
;
143 else /* FIXME: unclear how to get TWCC */
144 DSM_twCC
= TWCC_SEQERROR
;
148 /* DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT */
149 TW_UINT16
TWAIN_IdentityGetDefault (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
151 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
153 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT\n");
154 DSM_twCC
= TWCC_NODS
;
158 *pSourceIdentity
= devices
[0].identity
;
159 DSM_twCC
= TWCC_SUCCESS
;
163 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
164 TW_UINT16
TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
166 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
168 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
171 TRACE ("no entries found.\n");
172 DSM_twCC
= TWCC_NODS
;
175 DSM_currentDevice
= 0;
176 *pSourceIdentity
= devices
[DSM_currentDevice
++].identity
;
180 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
181 TW_UINT16
TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
183 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
185 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
186 if (!nrdevices
|| (DSM_currentDevice
== nrdevices
)) {
187 DSM_twCC
= TWCC_SUCCESS
;
188 return TWRC_ENDOFLIST
;
190 *pSourceIdentity
= devices
[DSM_currentDevice
++].identity
;
194 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
195 TW_UINT16
TWAIN_OpenDS (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
198 pTW_IDENTITY pIdentity
= (pTW_IDENTITY
) pData
;
200 const char *modname
= NULL
;
203 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_OPENDS\n");
204 TRACE("pIdentity is %s\n", pIdentity
->ProductName
);
205 if (DSM_currentState
!= 3) {
206 FIXME("seq error\n");
207 DSM_twCC
= TWCC_SEQERROR
;
213 DSM_twCC
= TWCC_NODS
;
217 if (pIdentity
->ProductName
[0] != '\0') {
218 /* Make sure the source to be opened exists in the device list */
219 for (i
= 0; i
<nrdevices
; i
++)
220 if (!strcmp (devices
[i
].identity
.ProductName
, pIdentity
->ProductName
))
224 } /* else use the first device */
226 /* the source is found in the device list */
227 newSource
= HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS
));
229 DSM_twCC
= TWCC_LOWMEMORY
;
230 FIXME("Out of memory.\n");
233 hmod
= LoadLibraryA(devices
[i
].modname
);
235 ERR("Failed to load TWAIN Source %s\n", modname
);
236 DSM_twCC
= TWCC_OPERATIONERROR
;
237 HeapFree(GetProcessHeap(), 0, newSource
);
240 newSource
->hmod
= hmod
;
241 newSource
->dsEntry
= (DSENTRYPROC
)GetProcAddress(hmod
, "DS_Entry");
242 if (TWRC_SUCCESS
!= newSource
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_OPENDS
, pIdentity
)) {
243 DSM_twCC
= TWCC_OPERATIONERROR
;
244 HeapFree(GetProcessHeap(), 0, newSource
);
247 /* Assign name and id for the opened data source */
248 pIdentity
->Id
= DSM_sourceId
++;
249 /* add the data source to an internal active source list */
250 newSource
->next
= activeSources
;
251 newSource
->identity
.Id
= pIdentity
->Id
;
252 strcpy (newSource
->identity
.ProductName
, pIdentity
->ProductName
);
253 activeSources
= newSource
;
254 DSM_twCC
= TWCC_SUCCESS
;
258 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
259 TW_UINT16
TWAIN_UserSelect (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
261 pTW_IDENTITY selected
= (pTW_IDENTITY
)pData
;
264 DSM_twCC
= TWCC_OPERATIONERROR
;
267 *selected
= devices
[0].identity
;
268 DSM_twCC
= TWCC_SUCCESS
;
272 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
273 TW_UINT16
TWAIN_CloseDSM (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
275 activeDS
*currentDS
= activeSources
, *nextDS
;
277 TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
279 if (DSM_currentState
== 3)
281 DSM_initialized
= FALSE
;
282 DSM_currentState
= 2;
284 /* If there are data sources still open, close them now. */
285 while (currentDS
!= NULL
)
287 nextDS
= currentDS
->next
;
288 currentDS
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_CLOSEDS
, pData
);
289 HeapFree (GetProcessHeap(), 0, currentDS
);
292 activeSources
= NULL
;
293 DSM_twCC
= TWCC_SUCCESS
;
296 DSM_twCC
= TWCC_SEQERROR
;
301 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
302 TW_UINT16
TWAIN_OpenDSM (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
304 TW_UINT16 twRC
= TWRC_SUCCESS
;
306 TRACE("DG_CONTROL/DAT_PARENT/MSG_OPENDSM\n");
307 if (DSM_currentState
== 2) {
308 if (!DSM_initialized
) {
309 DSM_currentDevice
= 0;
310 DSM_initialized
= TRUE
;
312 DSM_currentState
= 3;
313 DSM_twCC
= TWCC_SUCCESS
;
316 /* operation invoked in invalid state */
317 DSM_twCC
= TWCC_SEQERROR
;
323 /* DG_CONTROL/DAT_STATUS/MSG_GET */
324 TW_UINT16
TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
326 pTW_STATUS pSourceStatus
= (pTW_STATUS
) pData
;
328 TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
330 pSourceStatus
->ConditionCode
= DSM_twCC
;
331 DSM_twCC
= TWCC_SUCCESS
; /* clear the condition code */