urlmon: Don't create stgmed_obj for binding to object.
[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                 memcpy (&devices[nrdevices].identity, &sourceId, sizeof(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         memcpy (pSourceIdentity, &devices[0].identity, sizeof(TW_IDENTITY));
159         return TWRC_SUCCESS;
160 }
161
162 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
163 TW_UINT16 TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin, TW_MEMREF pData)
164 {
165         pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
166
167         TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
168         twain_autodetect();
169         if (!nrdevices) {
170                 TRACE ("no entries found.\n");
171                 DSM_twCC = TWCC_SUCCESS;
172                 return TWRC_ENDOFLIST;
173         }
174         DSM_currentDevice = 0;
175         memcpy (pSourceIdentity, &devices[DSM_currentDevice++].identity, sizeof(TW_IDENTITY));
176         return TWRC_SUCCESS;
177 }
178
179 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
180 TW_UINT16 TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin, TW_MEMREF pData)
181 {
182         pTW_IDENTITY pSourceIdentity = (pTW_IDENTITY) pData;
183
184         TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
185         if (!nrdevices || (DSM_currentDevice == nrdevices)) {
186                 DSM_twCC = TWCC_SUCCESS;
187                 return TWRC_ENDOFLIST;
188         }
189         memcpy (pSourceIdentity, &devices[DSM_currentDevice++].identity, sizeof(TW_IDENTITY));
190         return TWRC_SUCCESS;
191 }
192
193 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
194 TW_UINT16 TWAIN_OpenDS (pTW_IDENTITY pOrigin, TW_MEMREF pData)
195 {
196         TW_UINT16 i = 0;
197         pTW_IDENTITY pIdentity = (pTW_IDENTITY) pData;
198         activeDS *newSource;
199         const char *modname = NULL;
200         HMODULE hmod;
201
202         TRACE("DG_CONTROL/DAT_IDENTITY/MSG_OPENDS\n");
203         TRACE("pIdentity is %s\n", pIdentity->ProductName);
204         if (DSM_currentState != 3) {
205                 FIXME("seq errror\n");
206                 DSM_twCC = TWCC_SEQERROR;
207                 return TWRC_FAILURE;
208         }
209         twain_autodetect();
210         if (!nrdevices) {
211                 FIXME("no devs.\n");
212                 DSM_twCC = TWCC_NODS;
213                 return TWRC_FAILURE;
214         }
215
216         if (pIdentity->ProductName[0] != '\0') {
217                 /* Make sure the source to be opened exists in the device list */
218                 for (i = 0; i<nrdevices; i++)
219                         if (!strcmp (devices[i].identity.ProductName, pIdentity->ProductName))
220                                 break;
221                 if (i == nrdevices)
222                         i = 0;
223         } /* else use the first device */
224
225         /* the source is found in the device list */
226         newSource = HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS));
227         if (!newSource) {
228                 DSM_twCC = TWCC_LOWMEMORY;
229                 FIXME("Out of memory.\n");
230                 return TWRC_FAILURE;
231         }
232         hmod = LoadLibraryA(devices[i].modname);
233         if (!hmod) {
234                 ERR("Failed to load TWAIN Source %s\n", modname);
235                 DSM_twCC = TWCC_OPERATIONERROR;
236                 HeapFree(GetProcessHeap(), 0, newSource);
237                 return TWRC_FAILURE;
238         }
239         newSource->hmod = hmod; 
240         newSource->dsEntry = (DSENTRYPROC)GetProcAddress(hmod, "DS_Entry"); 
241         if (TWRC_SUCCESS != newSource->dsEntry (pOrigin, DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, pIdentity)) {
242                 DSM_twCC = TWCC_OPERATIONERROR;
243                 HeapFree(GetProcessHeap(), 0, newSource);
244                 return TWRC_FAILURE;
245         }
246         /* Assign name and id for the opened data source */
247         pIdentity->Id = DSM_sourceId ++;
248         /* add the data source to an internal active source list */
249         newSource->next = activeSources;
250         newSource->identity.Id = pIdentity->Id;
251         strcpy (newSource->identity.ProductName, pIdentity->ProductName);
252         activeSources = newSource;
253         DSM_twCC = TWCC_SUCCESS;
254         return TWRC_SUCCESS;
255 }
256
257 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
258 TW_UINT16 TWAIN_UserSelect (pTW_IDENTITY pOrigin, TW_MEMREF pData)
259 {
260         pTW_IDENTITY    selected = (pTW_IDENTITY)pData;
261
262         if (!nrdevices) {
263                 DSM_twCC = TWCC_OPERATIONERROR;
264                 return TWRC_FAILURE;
265         }
266         memcpy (selected, &devices[0].identity, sizeof(TW_IDENTITY));
267         DSM_twCC = TWCC_SUCCESS;
268         return TWRC_SUCCESS;
269 }
270
271 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
272 TW_UINT16 TWAIN_CloseDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
273 {
274     activeDS *currentDS = activeSources, *nextDS;
275
276     TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
277
278     if (DSM_currentState == 3)
279     {
280         DSM_initialized = FALSE;
281         DSM_currentState = 2;
282
283         /* If there are data sources still open, close them now. */
284         while (currentDS != NULL)
285         {
286             nextDS = currentDS->next;
287             currentDS->dsEntry (pOrigin, DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, pData);
288             HeapFree (GetProcessHeap(), 0, currentDS);
289             currentDS = nextDS;
290         }
291         activeSources = NULL;
292         DSM_twCC = TWCC_SUCCESS;
293         return TWRC_SUCCESS;
294     } else {
295         DSM_twCC = TWCC_SEQERROR;
296         return TWRC_FAILURE;
297     }
298 }
299
300 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
301 TW_UINT16 TWAIN_OpenDSM (pTW_IDENTITY pOrigin, TW_MEMREF pData)
302 {
303         TW_UINT16 twRC = TWRC_SUCCESS;
304
305         TRACE("DG_CONTROL/DAT_PARENT/MSG_OPENDSM\n");
306         if (DSM_currentState == 2) {
307                 if (!DSM_initialized) {
308                         DSM_currentDevice = 0;
309                         DSM_initialized = TRUE;
310                 }
311                 DSM_currentState = 3;
312                 DSM_twCC = TWCC_SUCCESS;
313                 twRC = TWRC_SUCCESS;
314         } else {
315                 /* operation invoked in invalid state */
316                 DSM_twCC = TWCC_SEQERROR;
317                 twRC = TWRC_FAILURE;
318         }
319         return twRC;
320 }
321
322 /* DG_CONTROL/DAT_STATUS/MSG_GET */
323 TW_UINT16 TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin, TW_MEMREF pData)
324 {
325         pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
326
327         TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
328
329         pSourceStatus->ConditionCode = DSM_twCC;
330         DSM_twCC = TWCC_SUCCESS;  /* clear the condition code */
331         return TWRC_SUCCESS;
332 }