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 = HeapReAlloc(GetProcessHeap(), 0, devices, sizeof(devices[0])*(nrdevices+1));
89 devices = HeapAlloc(GetProcessHeap(), 0, sizeof(devices[0]));
90 if ((devices[nrdevices].modname = HeapAlloc(GetProcessHeap(), 0, strlen(dsname) + 1)))
91 lstrcpyA(devices[nrdevices].modname, dsname);
92 devices[nrdevices].identity = sourceId;
99 static int detectionrun = 0;
102 twain_autodetect(void) {
103 if (detectionrun) return;
106 twain_add_onedriver("sane.ds");
107 twain_add_onedriver("gphoto2.ds");
109 twain_add_onedriver("c:\\windows\\Twain_32\\Largan\\sp503a.ds");
110 twain_add_onedriver("c:\\windows\\Twain_32\\vivicam10\\vivicam10.ds");
111 twain_add_onedriver("c:\\windows\\Twain_32\\ws30slim\\sp500a.ds");
115 /* DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS */
116 TW_UINT16 TWAIN_CloseDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
118 TW_UINT16 twRC = TWRC_SUCCESS;
119 pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
120 activeDS *currentDS = NULL, *prevDS = NULL;
122 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS\n");
124 for (currentDS = activeSources; currentDS; currentDS = currentDS->next) {
125 if (currentDS->identity.Id == pIdentity->Id)
130 DSM_twCC = TWCC_NODS;
133 twRC = currentDS->dsEntry (pOrigin, DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, pData);
134 /* This causes crashes due to still open Windows, so leave out for now.
135 * FreeLibrary (currentDS->hmod);
138 prevDS->next = currentDS->next;
140 activeSources = currentDS->next;
141 HeapFree (GetProcessHeap(), 0, currentDS);
142 if (twRC == TWRC_SUCCESS)
143 DSM_twCC = TWCC_SUCCESS;
144 else /* FIXME: unclear how to get TWCC */
145 DSM_twCC = TWCC_SEQERROR;
149 /* DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT */
150 TW_UINT16 TWAIN_IdentityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
152 pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
154 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT\n");
155 DSM_twCC = TWCC_NODS;
159 *pSourceIdentity = devices[0].identity;
160 DSM_twCC = TWCC_SUCCESS;
164 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
165 TW_UINT16 TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin, TW_MEMREF pData)
167 pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
169 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
172 TRACE ("no entries found.\n");
173 DSM_twCC = TWCC_NODS;
176 DSM_currentDevice = 0;
177 *pSourceIdentity = devices[DSM_currentDevice++].identity;
181 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
182 TW_UINT16 TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin, TW_MEMREF pData)
184 pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
186 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
187 if (!nrdevices || (DSM_currentDevice == nrdevices)) {
188 DSM_twCC = TWCC_SUCCESS;
189 return TWRC_ENDOFLIST;
191 *pSourceIdentity = devices[DSM_currentDevice++].identity;
195 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
196 TW_UINT16 TWAIN_OpenDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
199 pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
201 const char *modname = NULL;
204 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_OPENDS\n");
205 TRACE("pIdentity is %s\n", pIdentity->ProductName);
206 if (DSM_currentState != 3) {
207 FIXME("seq error\n");
208 DSM_twCC = TWCC_SEQERROR;
214 DSM_twCC = TWCC_NODS;
218 if (pIdentity->ProductName[0] != '\0') {
219 /* Make sure the source to be opened exists in the device list */
220 for (i = 0; i<nrdevices; i++)
221 if (!strcmp (devices[i].identity.ProductName, pIdentity->ProductName))
225 } /* else use the first device */
227 /* the source is found in the device list */
228 newSource = HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS));
230 DSM_twCC = TWCC_LOWMEMORY;
231 FIXME("Out of memory.\n");
234 hmod = LoadLibraryA(devices[i].modname);
236 ERR("Failed to load TWAIN Source %s\n", modname);
237 DSM_twCC = TWCC_OPERATIONERROR;
238 HeapFree(GetProcessHeap(), 0, newSource);
241 newSource->hmod = hmod;
242 newSource->dsEntry = (DSENTRYPROC)GetProcAddress(hmod, "DS_Entry");
243 if (TWRC_SUCCESS != newSource->dsEntry (pOrigin, DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, pIdentity)) {
244 DSM_twCC = TWCC_OPERATIONERROR;
245 HeapFree(GetProcessHeap(), 0, newSource);
248 /* Assign name and id for the opened data source */
249 pIdentity->Id = DSM_sourceId ++;
250 /* add the data source to an internal active source list */
251 newSource->next = activeSources;
252 newSource->identity.Id = pIdentity->Id;
253 strcpy (newSource->identity.ProductName, pIdentity->ProductName);
254 activeSources = newSource;
255 DSM_twCC = TWCC_SUCCESS;
259 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
260 TW_UINT16 TWAIN_UserSelect (pTW_IDENTITY pOrigin, TW_MEMREF pData)
262 pTW_IDENTITY selected = (pTW_IDENTITY)pData;
265 DSM_twCC = TWCC_OPERATIONERROR;
268 *selected = devices[0].identity;
269 DSM_twCC = TWCC_SUCCESS;
273 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
274 TW_UINT16 TWAIN_CloseDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
276 activeDS *currentDS = activeSources, *nextDS;
278 TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
280 if (DSM_currentState == 3)
282 DSM_initialized = FALSE;
283 DSM_currentState = 2;
285 /* If there are data sources still open, close them now. */
286 while (currentDS != NULL)
288 nextDS = currentDS->next;
289 currentDS->dsEntry (pOrigin, DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, pData);
290 HeapFree (GetProcessHeap(), 0, currentDS);
293 activeSources = NULL;
294 DSM_twCC = TWCC_SUCCESS;
297 DSM_twCC = TWCC_SEQERROR;
302 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
303 TW_UINT16 TWAIN_OpenDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
305 TW_UINT16 twRC = TWRC_SUCCESS;
307 TRACE("DG_CONTROL/DAT_PARENT/MSG_OPENDSM\n");
308 if (DSM_currentState == 2) {
309 if (!DSM_initialized) {
310 DSM_currentDevice = 0;
311 DSM_initialized = TRUE;
313 DSM_currentState = 3;
314 DSM_twCC = TWCC_SUCCESS;
317 /* operation invoked in invalid state */
318 DSM_twCC = TWCC_SEQERROR;
324 /* DG_CONTROL/DAT_STATUS/MSG_GET */
325 TW_UINT16 TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin, TW_MEMREF pData)
327 pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
329 TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
331 pSourceStatus->ConditionCode = DSM_twCC;
332 DSM_twCC = TWCC_SUCCESS; /* clear the condition code */