sane.ds: Add stub support for ICAP_COMPRESSION.
[wine] / dlls / twain_32 / dsm_ctrl.c
1 /*
2  * TWAIN32 Source Manager
3  *
4  * Copyright 2000 Corel Corporation
5  * Copyright 2006 Marcus Meissner
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "windef.h"
31 #include "winbase.h"
32 #include "twain.h"
33 #include "twain_i.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(twain);
37
38 struct all_devices {
39         char            *modname;
40         TW_IDENTITY     identity;
41 };
42
43 static int nrdevices = 0;
44 static struct all_devices *devices = NULL;
45
46 static void
47 twain_add_onedriver(const char *dsname) {
48         HMODULE         hmod;
49         DSENTRYPROC     dsEntry;
50         TW_IDENTITY     fakeOrigin;
51         TW_IDENTITY     sourceId;
52         TW_UINT16       ret;
53
54         hmod = LoadLibraryA(dsname);
55         if (!hmod) {
56                 ERR("Failed to load TWAIN Source %s\n", dsname);
57                 return;
58         }
59         dsEntry = (DSENTRYPROC)GetProcAddress(hmod, "DS_Entry"); 
60         if (!dsEntry) {
61                 ERR("Failed to find DS_Entry() in TWAIN DS %s\n", dsname);
62                 return;
63         }
64         /* Loop to do multiple detects, mostly for sane.ds and gphoto2.ds */
65         do {
66                 int i;
67
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");
74                         return;
75                 }
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));
79
80                 for (i=0;i<nrdevices;i++) {
81                         if (!strcmp(sourceId.ProductName,devices[i].identity.ProductName))
82                                 break;
83                 }
84                 if (i < nrdevices)
85                         break;
86                 if (nrdevices)
87                         devices = realloc(devices, sizeof(devices[0])*(nrdevices+1));
88                 else
89                         devices = malloc(sizeof(devices[0]));
90                 devices[nrdevices].modname = strdup(dsname);
91                 devices[nrdevices].identity = sourceId;
92                 nrdevices++;
93                 DSM_sourceId++;
94         } while (1);
95         FreeLibrary (hmod);
96 }
97
98 static int detectionrun = 0;
99
100 static void
101 twain_autodetect(void) {
102         if (detectionrun) return;
103         detectionrun = 1;
104
105         twain_add_onedriver("gphoto2.ds");
106         twain_add_onedriver("sane.ds");
107 #if 0
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");
111 #endif
112 }
113
114 /* DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS */
115 TW_UINT16 TWAIN_CloseDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
116 {
117         TW_UINT16 twRC = TWRC_SUCCESS;
118         pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
119         activeDS *currentDS = NULL, *prevDS = NULL;
120
121         TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS\n");
122
123         for (currentDS = activeSources; currentDS; currentDS = currentDS->next) {
124                 if (currentDS->identity.Id == pIdentity->Id)
125                         break;
126                 prevDS = currentDS;
127         }
128         if (!currentDS) {
129                 DSM_twCC = TWCC_NODS;
130                 return TWRC_FAILURE;
131         }
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);
135          */
136         if (prevDS)
137                 prevDS->next = currentDS->next;
138         else
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;
145         return twRC;
146 }
147
148 /* DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT */
149 TW_UINT16 TWAIN_IdentityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
150 {
151         pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
152
153         TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT\n");
154         DSM_twCC = TWCC_NODS;
155         twain_autodetect();
156         if (!nrdevices)
157                 return TWRC_FAILURE;
158         *pSourceIdentity = devices[0].identity;
159         DSM_twCC = TWCC_SUCCESS;
160         return TWRC_SUCCESS;
161 }
162
163 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
164 TW_UINT16 TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin, TW_MEMREF pData)
165 {
166         pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
167
168         TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
169         twain_autodetect();
170         if (!nrdevices) {
171                 TRACE ("no entries found.\n");
172                 DSM_twCC = TWCC_NODS;
173                 return TWRC_FAILURE;
174         }
175         DSM_currentDevice = 0;
176         *pSourceIdentity = devices[DSM_currentDevice++].identity;
177         return TWRC_SUCCESS;
178 }
179
180 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
181 TW_UINT16 TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin, TW_MEMREF pData)
182 {
183         pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
184
185         TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
186         if (!nrdevices || (DSM_currentDevice == nrdevices)) {
187                 DSM_twCC = TWCC_SUCCESS;
188                 return TWRC_ENDOFLIST;
189         }
190         *pSourceIdentity = devices[DSM_currentDevice++].identity;
191         return TWRC_SUCCESS;
192 }
193
194 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
195 TW_UINT16 TWAIN_OpenDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
196 {
197         TW_UINT16 i = 0;
198         pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
199         activeDS *newSource;
200         const char *modname = NULL;
201         HMODULE hmod;
202
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;
208                 return TWRC_FAILURE;
209         }
210         twain_autodetect();
211         if (!nrdevices) {
212                 FIXME("no devs.\n");
213                 DSM_twCC = TWCC_NODS;
214                 return TWRC_FAILURE;
215         }
216
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))
221                                 break;
222                 if (i == nrdevices)
223                         i = 0;
224         } /* else use the first device */
225
226         /* the source is found in the device list */
227         newSource = HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS));
228         if (!newSource) {
229                 DSM_twCC = TWCC_LOWMEMORY;
230                 FIXME("Out of memory.\n");
231                 return TWRC_FAILURE;
232         }
233         hmod = LoadLibraryA(devices[i].modname);
234         if (!hmod) {
235                 ERR("Failed to load TWAIN Source %s\n", modname);
236                 DSM_twCC = TWCC_OPERATIONERROR;
237                 HeapFree(GetProcessHeap(), 0, newSource);
238                 return TWRC_FAILURE;
239         }
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);
245                 return TWRC_FAILURE;
246         }
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;
255         return TWRC_SUCCESS;
256 }
257
258 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
259 TW_UINT16 TWAIN_UserSelect (pTW_IDENTITY pOrigin, TW_MEMREF pData)
260 {
261         pTW_IDENTITY    selected = (pTW_IDENTITY)pData;
262
263         if (!nrdevices) {
264                 DSM_twCC = TWCC_OPERATIONERROR;
265                 return TWRC_FAILURE;
266         }
267         *selected = devices[0].identity;
268         DSM_twCC = TWCC_SUCCESS;
269         return TWRC_SUCCESS;
270 }
271
272 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
273 TW_UINT16 TWAIN_CloseDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
274 {
275     activeDS *currentDS = activeSources, *nextDS;
276
277     TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
278
279     if (DSM_currentState == 3)
280     {
281         DSM_initialized = FALSE;
282         DSM_currentState = 2;
283
284         /* If there are data sources still open, close them now. */
285         while (currentDS != NULL)
286         {
287             nextDS = currentDS->next;
288             currentDS->dsEntry (pOrigin, DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, pData);
289             HeapFree (GetProcessHeap(), 0, currentDS);
290             currentDS = nextDS;
291         }
292         activeSources = NULL;
293         DSM_twCC = TWCC_SUCCESS;
294         return TWRC_SUCCESS;
295     } else {
296         DSM_twCC = TWCC_SEQERROR;
297         return TWRC_FAILURE;
298     }
299 }
300
301 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
302 TW_UINT16 TWAIN_OpenDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
303 {
304         TW_UINT16 twRC = TWRC_SUCCESS;
305
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;
311                 }
312                 DSM_currentState = 3;
313                 DSM_twCC = TWCC_SUCCESS;
314                 twRC = TWRC_SUCCESS;
315         } else {
316                 /* operation invoked in invalid state */
317                 DSM_twCC = TWCC_SEQERROR;
318                 twRC = TWRC_FAILURE;
319         }
320         return twRC;
321 }
322
323 /* DG_CONTROL/DAT_STATUS/MSG_GET */
324 TW_UINT16 TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin, TW_MEMREF pData)
325 {
326         pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
327
328         TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
329
330         pSourceStatus->ConditionCode = DSM_twCC;
331         DSM_twCC = TWCC_SUCCESS;  /* clear the condition code */
332         return TWRC_SUCCESS;
333 }