oleaut32/tests: Use IsEqualIID instead of memcmp to compare REFIIDs.
[wine] / dlls / twain_32 / twain32_main.c
1 /*
2  * TWAIN32 functions
3  *
4  * Copyright 2000 Shi Quan He <shiquan@cyberdude.com>
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 <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "twain.h"
29 #include "twain_i.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(twain);
33
34 /* A helper function that looks up a destination identity in the active
35    source list */
36 static activeDS *TWAIN_LookupSource (const TW_IDENTITY *pDest)
37 {
38     activeDS *pSource;
39
40     for (pSource = activeSources; pSource; pSource = pSource->next)
41         if (pSource->identity.Id == pDest->Id)
42             break;
43     return pSource;
44 }
45
46 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
47 {
48     TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
49
50     switch (fdwReason)
51     {
52         case DLL_PROCESS_ATTACH:
53             DisableThreadLibraryCalls(hinstDLL);
54             DSM_currentState = 2;
55             break;
56
57         case DLL_PROCESS_DETACH:
58             DSM_currentState = 1;
59             break;
60     }
61
62     return TRUE;
63 }
64
65 static TW_UINT16 TWAIN_SourceManagerHandler (
66            pTW_IDENTITY pOrigin,
67            TW_UINT16   DAT,
68            TW_UINT16   MSG,
69            TW_MEMREF   pData)
70 {
71     TW_UINT16 twRC = TWRC_SUCCESS;
72
73     switch (DAT)
74     {
75         case DAT_IDENTITY:
76             switch (MSG)
77             {
78                 case MSG_CLOSEDS:
79                     twRC = TWAIN_CloseDS (pOrigin, pData);
80                     break;
81
82                 case MSG_GETDEFAULT:
83                     twRC = TWAIN_IdentityGetDefault (pOrigin, pData);
84                     break;
85
86                 case MSG_GETFIRST:
87                     twRC = TWAIN_IdentityGetFirst (pOrigin, pData);
88                     break;
89
90                 case MSG_GETNEXT:
91                     twRC = TWAIN_IdentityGetNext (pOrigin, pData);
92                     break;
93
94                 case MSG_OPENDS:
95                     twRC = TWAIN_OpenDS (pOrigin, pData);
96                     break;
97
98                 case MSG_USERSELECT:
99                     twRC = TWAIN_UserSelect (pOrigin, pData);
100                     break;
101
102                 default:
103                     /* Unrecognized operation triplet */
104                     twRC = TWRC_FAILURE;
105                     DSM_twCC = TWCC_BADPROTOCOL;
106                     WARN("unrecognized operation triplet\n");
107                     break;
108             }
109             break;
110
111         case DAT_PARENT:
112             switch (MSG)
113             {
114                 case MSG_CLOSEDSM:
115                     twRC = TWAIN_CloseDSM (pOrigin, pData);
116                     break;
117
118                 case MSG_OPENDSM:
119                     twRC = TWAIN_OpenDSM (pOrigin, pData);
120                     break;
121
122                 default:
123                     /* Unrecognized operation triplet */
124                     twRC = TWRC_FAILURE;
125                     DSM_twCC = TWCC_BADPROTOCOL;
126                     WARN("unrecognized operation triplet\n");
127             }
128             break;
129
130         case DAT_STATUS:
131             if (MSG == MSG_GET) {
132                 twRC = TWAIN_GetDSMStatus (pOrigin, pData);
133             } else {
134                 twRC = TWRC_FAILURE;
135                 DSM_twCC = TWCC_BADPROTOCOL;
136                 WARN("unrecognized operation triplet\n");
137             }
138             break;
139
140         default:
141             twRC = TWRC_FAILURE;
142             DSM_twCC = TWCC_BADPROTOCOL;
143             WARN("unrecognized operation triplet\n");
144             break;
145     }
146
147     return twRC;
148 }
149
150
151 /* Main entry point for the TWAIN library */
152 TW_UINT16 WINAPI
153 DSM_Entry (pTW_IDENTITY pOrigin,
154            pTW_IDENTITY pDest,
155            TW_UINT32    DG,
156            TW_UINT16    DAT,
157            TW_UINT16    MSG,
158            TW_MEMREF    pData)
159 {
160     TW_UINT16 twRC = TWRC_SUCCESS;  /* Return Code */
161
162     TRACE("(DG=%d DAT=%d MSG=%d)\n", DG, DAT, MSG);
163
164     if (pDest)
165     {
166         activeDS *pSource = TWAIN_LookupSource (pDest);
167         /* This operation's destination is a source */
168
169         if (!pSource) {
170             ERR("No source associated with pDest %p\n", pDest);
171             DSM_twCC = TWCC_BADDEST;
172             return TWRC_FAILURE;
173         }
174         DSM_twCC = TWCC_SUCCESS;
175         TRACE("Forwarding %d/%d/%d/%p to DS.\n", DG, DAT, MSG, pData);
176         twRC = pSource->dsEntry(pOrigin, DG, DAT, MSG, pData);
177         TRACE("return value is %d\n", twRC);
178         return twRC;
179     }
180     switch (DG)
181     {
182         case DG_CONTROL:
183             twRC = TWAIN_SourceManagerHandler (pOrigin, DAT, MSG, pData);
184             break;
185         default:
186             FIXME("The DSM does not handle DG %d\n", DG);
187             DSM_twCC = TWCC_BADPROTOCOL;
188             twRC = TWRC_FAILURE;
189     }
190     return twRC;
191 }