Remove redundant check.
[wine] / dlls / twain / dsm_ctrl.c
1 /*
2  * TWAIN32 Source Manager
3  *
4  * Copyright 2000 Corel Corporation
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "twain.h"
31 #include "twain_i.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(twain);
35
36 /* DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS */
37 TW_UINT16 TWAIN_CloseDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
38 {
39 #ifndef HAVE_SANE
40     DSM_twCC = TWCC_NODS;
41     return TWRC_FAILURE;
42 #else
43     TW_UINT16 twRC = TWRC_SUCCESS;
44     pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
45     activeDS *currentDS = NULL, *prevDS = NULL;
46
47     TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS\n");
48
49     for (currentDS = activeSources; currentDS; currentDS = currentDS->next)
50     {
51         if (currentDS->identity.Id == pIdentity->Id)
52             break;
53         prevDS = currentDS;
54     }
55     if (currentDS)
56     {
57         /* Only valid to close a data source if it is in state 4 */
58         if (currentDS->currentState == 4)
59         {
60             sane_close (currentDS->deviceHandle);
61             /* remove the data source from active data source list */
62             if (prevDS)
63                 prevDS->next = currentDS->next;
64             else
65                 activeSources = currentDS->next;
66             HeapFree (GetProcessHeap(), 0, currentDS);
67             twRC = TWRC_SUCCESS;
68             DSM_twCC = TWCC_SUCCESS;
69         }
70         else
71         {
72             twRC = TWRC_FAILURE;
73             DSM_twCC = TWCC_SEQERROR;
74         }
75     }
76     else
77     {
78         twRC = TWRC_FAILURE;
79         DSM_twCC = TWCC_NODS;
80     }
81
82     return twRC;
83 #endif
84 }
85
86 /* DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT */
87 TW_UINT16 TWAIN_IdentityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
88 {
89 #ifndef HAVE_SANE
90     DSM_twCC = TWCC_NODS;
91     return TWRC_FAILURE;
92 #else
93     TW_UINT16 twRC = TWRC_SUCCESS;
94     pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
95
96     TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT\n");
97
98     if (!device_list)
99     {
100         if ((sane_get_devices (&device_list, SANE_FALSE) != SANE_STATUS_GOOD))
101         {
102             DSM_twCC = TWCC_NODS;
103             return TWRC_FAILURE;
104         }
105     }
106
107     /* FIXME: the default device is not necessarily the first device.  *
108      * Users should be able to choose the default device               */
109     if (device_list && device_list[0])
110     {
111         pSourceIdentity->Id = DSM_sourceId ++;
112         strcpy (pSourceIdentity->ProductName, device_list[0]->name);
113         strcpy (pSourceIdentity->Manufacturer, device_list[0]->vendor);
114         strcpy (pSourceIdentity->ProductFamily, device_list[0]->model);
115         pSourceIdentity->ProtocolMajor = TWON_PROTOCOLMAJOR;
116         pSourceIdentity->ProtocolMinor = TWON_PROTOCOLMINOR;
117
118         twRC = TWRC_SUCCESS;
119         DSM_twCC = TWCC_SUCCESS;
120     }
121     else
122     {
123         twRC = TWRC_FAILURE;
124         DSM_twCC = TWCC_NODS;
125     }
126
127     return twRC;
128 #endif
129 }
130
131 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
132 TW_UINT16 TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin, TW_MEMREF pData)
133 {
134 #ifndef HAVE_SANE
135     DSM_twCC = TWCC_NODS;
136     return TWRC_FAILURE;
137 #else
138     TW_UINT16 twRC = TWRC_SUCCESS;
139     pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
140     SANE_Status status;
141
142     TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
143
144     status = sane_get_devices (&device_list, SANE_FALSE);
145     if (status == SANE_STATUS_GOOD)
146     {
147         if (device_list[0])
148         {
149             pSourceIdentity->Id = DSM_sourceId ++;
150             strcpy (pSourceIdentity->ProductName, device_list[0]->name);
151             strcpy (pSourceIdentity->Manufacturer, device_list[0]->vendor);
152             strcpy (pSourceIdentity->ProductFamily, device_list[0]->model);
153             pSourceIdentity->ProtocolMajor = TWON_PROTOCOLMAJOR;
154             pSourceIdentity->ProtocolMinor = TWON_PROTOCOLMINOR;
155         }
156         DSM_currentDevice = 1;
157         twRC = TWRC_SUCCESS;
158         DSM_twCC = TWCC_SUCCESS;
159     }
160     else if (status == SANE_STATUS_NO_MEM)
161     {
162         twRC = TWRC_FAILURE;
163         DSM_twCC = TWCC_LOWMEMORY;
164     }
165     else
166     {
167         WARN("sane_get_devices() failed: %s\n", sane_strstatus (status));
168         twRC = TWRC_FAILURE;
169         DSM_twCC = TWCC_NODS;
170     }
171
172     return twRC;
173 #endif
174 }
175
176 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
177 TW_UINT16 TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin, TW_MEMREF pData)
178 {
179 #ifndef HAVE_SANE
180     DSM_twCC = TWCC_SUCCESS;
181     return TWRC_ENDOFLIST;
182 #else
183     TW_UINT16 twRC = TWRC_SUCCESS;
184     pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
185
186     TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
187
188     if (device_list && device_list[DSM_currentDevice] &&
189         device_list[DSM_currentDevice]->name &&
190         device_list[DSM_currentDevice]->vendor &&
191         device_list[DSM_currentDevice]->model)
192     {
193         pSourceIdentity->Id = DSM_sourceId ++;
194         strcpy (pSourceIdentity->ProductName, device_list[DSM_currentDevice]->name);
195         strcpy (pSourceIdentity->Manufacturer, device_list[DSM_currentDevice]->vendor);
196         strcpy (pSourceIdentity->ProductFamily, device_list[DSM_currentDevice]->model);
197         pSourceIdentity->ProtocolMajor = TWON_PROTOCOLMAJOR;
198         pSourceIdentity->ProtocolMinor = TWON_PROTOCOLMINOR;
199         DSM_currentDevice ++;
200
201         twRC = TWRC_SUCCESS;
202         DSM_twCC = TWCC_SUCCESS;
203     }
204     else
205     {
206         DSM_twCC = TWCC_SUCCESS;
207         twRC = TWRC_ENDOFLIST;
208     }
209
210     return twRC;
211 #endif
212 }
213
214 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
215 TW_UINT16 TWAIN_OpenDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
216 {
217 #ifndef HAVE_SANE
218     DSM_twCC = TWCC_NODS;
219     return TWRC_FAILURE;
220 #else
221     TW_UINT16 twRC = TWRC_SUCCESS, i = 0;
222     pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
223     activeDS *newSource;
224     SANE_Status status;
225
226     TRACE("DG_CONTROL/DAT_IDENTITY/MSG_OPENDS\n");
227
228     if (DSM_currentState != 3)
229     {
230         DSM_twCC = TWCC_SEQERROR;
231         return TWRC_FAILURE;
232     }
233
234     if (!device_list &&
235        (sane_get_devices (&device_list, SANE_FALSE) != SANE_STATUS_GOOD))
236     {
237         DSM_twCC = TWCC_NODS;
238         return TWRC_FAILURE;
239     }
240
241     if (pIdentity->ProductName[0] != '\0')
242     {
243         /* Make sure the source to be opened exists in the device list */
244         for (i = 0; device_list[i]; i ++)
245         {
246             if (strcmp (device_list[i]->name, pIdentity->ProductName) == 0)
247                 break;
248         }
249     }
250
251     if (device_list[i])
252     {
253         /* the source is found in the device list */
254         newSource = HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS));
255         if (newSource)
256         {
257             status = sane_open(device_list[i]->name,&newSource->deviceHandle);
258             if (status == SANE_STATUS_GOOD)
259             {
260                 /* Assign name and id for the opened data source */
261                 strcpy (pIdentity->ProductName, device_list[i]->name);
262                 pIdentity->Id = DSM_sourceId ++;
263                 /* add the data source to an internal active source list */
264                 newSource->next = activeSources;
265                 newSource->identity.Id = pIdentity->Id;
266                 strcpy (newSource->identity.ProductName, pIdentity->ProductName);
267                 newSource->currentState = 4; /*transition into state 4*/
268                 newSource->twCC = TWCC_SUCCESS;
269                 activeSources = newSource;
270                 twRC = TWRC_SUCCESS;
271                 DSM_twCC = TWCC_SUCCESS;
272             }
273             else
274             {
275                 twRC = TWRC_FAILURE;
276                 DSM_twCC = TWCC_OPERATIONERROR;
277             }
278         }
279         else
280         {
281             twRC = TWRC_FAILURE;
282             DSM_twCC = TWCC_LOWMEMORY;
283         }
284     }
285     else
286     {
287         twRC = TWRC_FAILURE;
288         DSM_twCC = TWCC_NODS;
289     }
290
291     return twRC;
292 #endif
293 }
294
295 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
296 TW_UINT16 TWAIN_UserSelect (pTW_IDENTITY pOrigin, TW_MEMREF pData)
297 {
298 #ifndef HAVE_SANE
299     return TWRC_SUCCESS;
300 #else
301     TW_UINT16 twRC = TWRC_SUCCESS;
302
303     TRACE("DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT\n");
304
305     /* FIXME: we should replace xscanimage with our own  User Select UI */
306     system("xscanimage");
307
308     DSM_twCC = TWCC_SUCCESS;
309     return twRC;
310 #endif
311 }
312
313 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
314 TW_UINT16 TWAIN_CloseDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
315 {
316 #ifndef HAVE_SANE
317     return TWRC_FAILURE;
318 #else
319     TW_UINT16 twRC = TWRC_SUCCESS;
320     activeDS *currentDS = activeSources, *nextDS;
321
322     TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
323
324     if (DSM_currentState == 3)
325     {
326         sane_exit ();
327         DSM_initialized = FALSE;
328         DSM_parentHWND = 0;
329         DSM_currentState = 2;
330
331         /* If there are data sources still open, close them now. */
332         while (currentDS != NULL)
333         {
334             nextDS = currentDS->next;
335             sane_close (currentDS->deviceHandle);
336             HeapFree (GetProcessHeap(), 0, currentDS);
337             currentDS = nextDS;
338         }
339         activeSources = NULL;
340         DSM_twCC = TWCC_SUCCESS;
341         twRC = TWRC_SUCCESS;
342     }
343     else
344     {
345         DSM_twCC = TWCC_SEQERROR;
346         twRC = TWRC_FAILURE;
347     }
348
349     return twRC;
350 #endif
351 }
352
353 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
354 TW_UINT16 TWAIN_OpenDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
355 {
356 #ifndef HAVE_SANE
357     return TWRC_FAILURE;
358 #else
359     TW_UINT16 twRC = TWRC_SUCCESS;
360     SANE_Status status;
361     SANE_Int version_code;
362
363     TRACE("DG_CONTROL/DAT_PARENT/MSG_OPENDSM\n");
364
365     if (DSM_currentState == 2)
366     {
367         if (!DSM_initialized)
368         {
369             DSM_initialized = TRUE;
370             status = sane_init (&version_code, NULL);
371             device_list = NULL;
372             DSM_currentDevice = 0;
373             DSM_sourceId = 0;
374         }
375         DSM_parentHWND = *(TW_HANDLE*)pData;
376         DSM_currentState = 3; /* transition to state 3 */
377         DSM_twCC = TWCC_SUCCESS;
378         twRC = TWRC_SUCCESS;
379     }
380     else
381     {
382         /* operation invoked in invalid state */
383         DSM_twCC = TWCC_SEQERROR;
384         twRC = TWRC_FAILURE;
385     }
386
387     return twRC;
388 #endif
389 }
390
391 /* DG_CONTROL/DAT_STATUS/MSG_GET */
392 TW_UINT16 TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin, TW_MEMREF pData)
393 {
394     pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
395
396     TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
397
398     pSourceStatus->ConditionCode = DSM_twCC;
399     DSM_twCC = TWCC_SUCCESS;  /* clear the condition code */
400
401     return TWRC_SUCCESS;
402 }