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
24 static TW_UINT16 DSM_initialized
; /* whether Source Manager is initialized */
25 static TW_UINT32 DSM_sourceId
; /* source id generator */
26 static TW_UINT16 DSM_currentDevice
; /* keep track of device during enumeration */
33 static int nrdevices
= 0;
34 static struct all_devices
*devices
= NULL
;
37 twain_add_onedriver(const char *dsname
) {
40 TW_IDENTITY fakeOrigin
;
44 hmod
= LoadLibraryA(dsname
);
46 ERR("Failed to load TWAIN Source %s\n", dsname
);
49 dsEntry
= (DSENTRYPROC
)GetProcAddress(hmod
, "DS_Entry");
51 ERR("Failed to find DS_Entry() in TWAIN DS %s\n", dsname
);
54 /* Loop to do multiple detects, mostly for sane.ds and gphoto2.ds */
58 sourceId
.Id
= DSM_sourceId
;
59 sourceId
.ProtocolMajor
= TWON_PROTOCOLMAJOR
;
60 sourceId
.ProtocolMinor
= TWON_PROTOCOLMINOR
;
61 ret
= dsEntry (&fakeOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_GET
, &sourceId
);
62 if (ret
!= TWRC_SUCCESS
) {
63 ERR("Source->(DG_CONTROL,DAT_IDENTITY,MSG_GET) failed!\n");
66 TRACE("Manufacturer: %s\n", debugstr_a(sourceId
.Manufacturer
));
67 TRACE("ProductFamily: %s\n", debugstr_a(sourceId
.ProductFamily
));
68 TRACE("ProductName: %s\n", debugstr_a(sourceId
.ProductName
));
70 for (i
=0;i
<nrdevices
;i
++) {
71 if (!strcmp(sourceId
.ProductName
,devices
[i
].identity
.ProductName
))
77 devices
= HeapReAlloc(GetProcessHeap(), 0, devices
, sizeof(devices
[0])*(nrdevices
+1));
79 devices
= HeapAlloc(GetProcessHeap(), 0, sizeof(devices
[0]));
80 if ((devices
[nrdevices
].modname
= HeapAlloc(GetProcessHeap(), 0, strlen(dsname
) + 1)))
81 lstrcpyA(devices
[nrdevices
].modname
, dsname
);
82 devices
[nrdevices
].identity
= sourceId
;
89 static BOOL detectionrun
= FALSE
;
92 twain_autodetect(void) {
93 if (detectionrun
) return;
96 twain_add_onedriver("sane.ds");
97 twain_add_onedriver("gphoto2.ds");
99 twain_add_onedriver("c:\\windows\\Twain_32\\Largan\\sp503a.ds");
100 twain_add_onedriver("c:\\windows\\Twain_32\\vivicam10\\vivicam10.ds");
101 twain_add_onedriver("c:\\windows\\Twain_32\\ws30slim\\sp500a.ds");
105 /* DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS */
106 TW_UINT16
TWAIN_CloseDS (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
108 TW_UINT16 twRC
= TWRC_SUCCESS
;
109 pTW_IDENTITY pIdentity
= (pTW_IDENTITY
) pData
;
110 activeDS
*currentDS
= NULL
, *prevDS
= NULL
;
112 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_CLOSEDS\n");
114 for (currentDS
= activeSources
; currentDS
; currentDS
= currentDS
->next
) {
115 if (currentDS
->identity
.Id
== pIdentity
->Id
)
120 DSM_twCC
= TWCC_NODS
;
123 twRC
= currentDS
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_CLOSEDS
, pData
);
124 /* This causes crashes due to still open Windows, so leave out for now.
125 * FreeLibrary (currentDS->hmod);
128 prevDS
->next
= currentDS
->next
;
130 activeSources
= currentDS
->next
;
131 HeapFree (GetProcessHeap(), 0, currentDS
);
132 if (twRC
== TWRC_SUCCESS
)
133 DSM_twCC
= TWCC_SUCCESS
;
134 else /* FIXME: unclear how to get TWCC */
135 DSM_twCC
= TWCC_SEQERROR
;
139 /* DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT */
140 TW_UINT16
TWAIN_IdentityGetDefault (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
142 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
144 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETDEFAULT\n");
145 DSM_twCC
= TWCC_NODS
;
149 *pSourceIdentity
= devices
[0].identity
;
150 DSM_twCC
= TWCC_SUCCESS
;
154 /* DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST */
155 TW_UINT16
TWAIN_IdentityGetFirst (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
157 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
159 TRACE ("DG_CONTROL/DAT_IDENTITY/MSG_GETFIRST\n");
162 TRACE ("no entries found.\n");
163 DSM_twCC
= TWCC_NODS
;
166 DSM_currentDevice
= 0;
167 *pSourceIdentity
= devices
[DSM_currentDevice
++].identity
;
171 /* DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT */
172 TW_UINT16
TWAIN_IdentityGetNext (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
174 pTW_IDENTITY pSourceIdentity
= (pTW_IDENTITY
) pData
;
176 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_GETNEXT\n");
177 if (!nrdevices
|| (DSM_currentDevice
== nrdevices
)) {
178 DSM_twCC
= TWCC_SUCCESS
;
179 return TWRC_ENDOFLIST
;
181 *pSourceIdentity
= devices
[DSM_currentDevice
++].identity
;
185 /* DG_CONTROL/DAT_IDENTITY/MSG_OPENDS */
186 TW_UINT16
TWAIN_OpenDS (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
189 pTW_IDENTITY pIdentity
= (pTW_IDENTITY
) pData
;
191 const char *modname
= NULL
;
194 TRACE("DG_CONTROL/DAT_IDENTITY/MSG_OPENDS\n");
195 TRACE("pIdentity is %s\n", pIdentity
->ProductName
);
196 if (!DSM_initialized
) {
197 FIXME("seq error\n");
198 DSM_twCC
= TWCC_SEQERROR
;
204 DSM_twCC
= TWCC_NODS
;
208 if (pIdentity
->ProductName
[0] != '\0') {
209 /* Make sure the source to be opened exists in the device list */
210 for (i
= 0; i
<nrdevices
; i
++)
211 if (!strcmp (devices
[i
].identity
.ProductName
, pIdentity
->ProductName
))
215 } /* else use the first device */
217 /* the source is found in the device list */
218 newSource
= HeapAlloc (GetProcessHeap(), 0, sizeof (activeDS
));
220 DSM_twCC
= TWCC_LOWMEMORY
;
221 FIXME("Out of memory.\n");
224 hmod
= LoadLibraryA(devices
[i
].modname
);
226 ERR("Failed to load TWAIN Source %s\n", modname
);
227 DSM_twCC
= TWCC_OPERATIONERROR
;
228 HeapFree(GetProcessHeap(), 0, newSource
);
231 newSource
->hmod
= hmod
;
232 newSource
->dsEntry
= (DSENTRYPROC
)GetProcAddress(hmod
, "DS_Entry");
233 if (TWRC_SUCCESS
!= newSource
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_OPENDS
, pIdentity
)) {
234 DSM_twCC
= TWCC_OPERATIONERROR
;
235 HeapFree(GetProcessHeap(), 0, newSource
);
238 /* Assign name and id for the opened data source */
239 pIdentity
->Id
= DSM_sourceId
++;
240 /* add the data source to an internal active source list */
241 newSource
->next
= activeSources
;
242 newSource
->identity
.Id
= pIdentity
->Id
;
243 strcpy (newSource
->identity
.ProductName
, pIdentity
->ProductName
);
244 activeSources
= newSource
;
245 DSM_twCC
= TWCC_SUCCESS
;
249 /* DG_CONTROL/DAT_IDENTITY/MSG_USERSELECT */
250 TW_UINT16
TWAIN_UserSelect (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
252 pTW_IDENTITY selected
= (pTW_IDENTITY
)pData
;
255 DSM_twCC
= TWCC_OPERATIONERROR
;
258 *selected
= devices
[0].identity
;
259 DSM_twCC
= TWCC_SUCCESS
;
263 /* DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM */
264 TW_UINT16
TWAIN_CloseDSM (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
266 activeDS
*currentDS
= activeSources
, *nextDS
;
268 TRACE("DG_CONTROL/DAT_PARENT/MSG_CLOSEDSM\n");
272 DSM_initialized
= FALSE
;
274 /* If there are data sources still open, close them now. */
275 while (currentDS
!= NULL
)
277 nextDS
= currentDS
->next
;
278 currentDS
->dsEntry (pOrigin
, DG_CONTROL
, DAT_IDENTITY
, MSG_CLOSEDS
, pData
);
279 HeapFree (GetProcessHeap(), 0, currentDS
);
282 activeSources
= NULL
;
283 DSM_twCC
= TWCC_SUCCESS
;
286 DSM_twCC
= TWCC_SEQERROR
;
291 /* DG_CONTROL/DAT_PARENT/MSG_OPENDSM */
292 TW_UINT16
TWAIN_OpenDSM (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
294 TW_UINT16 twRC
= TWRC_SUCCESS
;
296 TRACE("DG_CONTROL/DAT_PARENT/MSG_OPENDSM\n");
297 if (!DSM_initialized
) {
298 DSM_currentDevice
= 0;
299 DSM_initialized
= TRUE
;
300 DSM_twCC
= TWCC_SUCCESS
;
303 /* operation invoked in invalid state */
304 DSM_twCC
= TWCC_SEQERROR
;
310 /* DG_CONTROL/DAT_STATUS/MSG_GET */
311 TW_UINT16
TWAIN_GetDSMStatus (pTW_IDENTITY pOrigin
, TW_MEMREF pData
)
313 pTW_STATUS pSourceStatus
= (pTW_STATUS
) pData
;
315 TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
317 pSourceStatus
->ConditionCode
= DSM_twCC
;
318 DSM_twCC
= TWCC_SUCCESS
; /* clear the condition code */