2 * TWAIN32 Source Manager
4 * Copyright 2000 Corel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(twain);
31 /* DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS */
32 TW_UINT16 TWAIN_CloseDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
38 TW_UINT16 twRC = TWRC_SUCCESS;
39 pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
40 activeDS *currentDS = NULL, *prevDS = NULL;
42 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS\n");
44 for (currentDS = activeSources; currentDS; currentDS = currentDS->next)
46 if (currentDS->identity.Id == pIdentity->Id)
52 /* Only valid to close a data source if it is in state 4 */
53 if (currentDS->currentState == 4)
55 sane_close (currentDS->deviceHandle);
56 /* remove the data source from active data source list */
58 prevDS->next = currentDS->next;
60 activeSources = currentDS->next;
61 HeapFree (GetProcessHeap(), 0, currentDS);
63 DSM_twCC = TWCC_SUCCESS;
68 DSM_twCC = TWCC_SEQERROR;
81 /* DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT */
82 TW_UINT16 TWAIN_IdentityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
88 TW_UINT16 twRC = TWRC_SUCCESS;
89 pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
91 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT\n");
95 if ((sane_get_devices (&device_list, SANE_FALSE) != SANE_STATUS_GOOD))
102 /* FIXME: the default device is not necessarily the first device. *
103 * Users should be able to choose the default device */
104 if (device_list && device_list[0])
106 pSourceIdentity->Id = DSM_sourceId ++;
107 strcpy (pSourceIdentity->ProductName, device_list[0]->name);
108 strcpy (pSourceIdentity->Manufacturer, device_list[0]->vendor);
109 strcpy (pSourceIdentity->ProductFamily, device_list[0]->model);
110 pSourceIdentity->ProtocolMajor = TWON_PROTOCOLMAJOR;
111 pSourceIdentity->ProtocolMinor = TWON_PROTOCOLMINOR;
114 DSM_twCC = TWCC_SUCCESS;
119 DSM_twCC = TWCC_NODS;
126 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
127 TW_UINT16 TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin, TW_MEMREF pData)
130 DSM_twCC = TWCC_NODS;
133 TW_UINT16 twRC = TWRC_SUCCESS;
134 pTW_IDENTITY pSourceIdentity;/* = (pTW_IDENTITY) pData;*/
137 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
139 status = sane_get_devices (&device_list, SANE_FALSE);
140 if (status == SANE_STATUS_GOOD)
144 pSourceIdentity->Id = DSM_sourceId ++;
145 strcpy (pSourceIdentity->ProductName, device_list[0]->name);
146 strcpy (pSourceIdentity->Manufacturer, device_list[0]->vendor);
147 strcpy (pSourceIdentity->ProductFamily, device_list[0]->model);
148 pSourceIdentity->ProtocolMajor = TWON_PROTOCOLMAJOR;
149 pSourceIdentity->ProtocolMinor = TWON_PROTOCOLMINOR;
151 DSM_currentDevice = 1;
153 DSM_twCC = TWCC_SUCCESS;
155 else if (status == SANE_STATUS_NO_MEM)
158 DSM_twCC = TWCC_LOWMEMORY;
162 WARN("sane_get_devices() failed: %s\n", sane_strstatus (status));
164 DSM_twCC = TWCC_NODS;
171 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
172 TW_UINT16 TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin, TW_MEMREF pData)
175 DSM_twCC = TWCC_SUCCESS;
176 return TWRC_ENDOFLIST;
178 TW_UINT16 twRC = TWRC_SUCCESS;
179 pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
181 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
183 if (device_list && device_list[DSM_currentDevice])
185 pSourceIdentity->Id = DSM_sourceId ++;
186 strcpy (pSourceIdentity->ProductName, device_list[DSM_currentDevice]->name);
187 strcpy (pSourceIdentity->Manufacturer, device_list[DSM_currentDevice]->vendor);
188 strcpy (pSourceIdentity->ProductFamily, device_list[DSM_currentDevice]->model);
189 pSourceIdentity->ProtocolMajor = TWON_PROTOCOLMAJOR;
190 pSourceIdentity->ProtocolMinor = TWON_PROTOCOLMINOR;
191 DSM_currentDevice ++;
194 DSM_twCC = TWCC_SUCCESS;
198 DSM_twCC = TWCC_SUCCESS;
199 twRC = TWRC_ENDOFLIST;
206 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
207 TW_UINT16 TWAIN_OpenDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
210 DSM_twCC = TWCC_NODS;
213 TW_UINT16 twRC = TWRC_SUCCESS, i = 0;
214 pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
218 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_OPENDS\n");
220 if (DSM_currentState != 3)
222 DSM_twCC = TWCC_SEQERROR;
227 (sane_get_devices (&device_list, SANE_FALSE) != SANE_STATUS_GOOD))
229 DSM_twCC = TWCC_NODS;
233 if (pIdentity->ProductName[0] != '\0')
235 /* Make sure the source to be open exists in the device list */
236 for (i = 0; device_list[i]; i ++)
238 if (strcmp (device_list[i]->name, pIdentity->ProductName) == 0)
245 /* the source is found in the device list */
246 newSource = HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS));
249 status = sane_open(device_list[i]->name,&newSource->deviceHandle);
250 if (status == SANE_STATUS_GOOD)
252 /* Assign name and id for the opened data source */
253 strcpy (pIdentity->ProductName, device_list[i]->name);
254 pIdentity->Id = DSM_sourceId ++;
255 /* add the data source to an internal active source list */
256 newSource->next = activeSources;
257 newSource->identity.Id = pIdentity->Id;
258 strcpy (newSource->identity.ProductName, pIdentity->ProductName);
259 newSource->currentState = 4; /*transition into state 4*/
260 newSource->twCC = TWCC_SUCCESS;
261 activeSources = newSource;
263 DSM_twCC = TWCC_SUCCESS;
268 DSM_twCC = TWCC_OPERATIONERROR;
274 DSM_twCC = TWCC_LOWMEMORY;
280 DSM_twCC = TWCC_NODS;
287 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
288 TW_UINT16 TWAIN_UserSelect (pTW_IDENTITY pOrigin, TW_MEMREF pData)
293 TW_UINT16 twRC = TWRC_SUCCESS;
295 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT\n");
297 /* FIXME: we should replace xscanimage with our own User Select UI */
298 system("xscanimage");
300 DSM_twCC = TWCC_SUCCESS;
305 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
306 TW_UINT16 TWAIN_CloseDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
311 TW_UINT16 twRC = TWRC_SUCCESS;
312 activeDS *currentDS = activeSources, *nextDS;
314 TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
316 if (DSM_currentState == 3)
319 DSM_initialized = FALSE;
321 DSM_currentState = 2;
323 /* If there are data sources still open, close them now. */
324 while (currentDS != NULL)
326 nextDS = currentDS->next;
327 sane_close (currentDS->deviceHandle);
328 HeapFree (GetProcessHeap(), 0, currentDS);
331 activeSources = NULL;
332 DSM_twCC = TWCC_SUCCESS;
337 DSM_twCC = TWCC_SEQERROR;
345 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
346 TW_UINT16 TWAIN_OpenDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
351 TW_UINT16 twRC = TWRC_SUCCESS;
353 SANE_Int version_code;
355 TRACE("DG_CONTROL/DAT_PARENT/MSG_OPENDSM\n");
357 if (DSM_currentState == 2)
359 if (!DSM_initialized)
361 DSM_initialized = TRUE;
362 status = sane_init (&version_code, NULL);
364 DSM_currentDevice = 0;
367 DSM_parentHWND = *(HWND*)pData;
368 DSM_currentState = 3; /* transition to state 3 */
369 DSM_twCC = TWCC_SUCCESS;
374 /* operation invoked in invalid state */
375 DSM_twCC = TWCC_SEQERROR;
383 /* DG_CONTROL/DAT_STATUS/MSG_GET */
384 TW_UINT16 TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin, TW_MEMREF pData)
386 pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
388 TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
390 pSourceStatus->ConditionCode = DSM_twCC;
391 DSM_twCC = TWCC_SUCCESS; /* clear the condition code */