4044cb2dcc7ea4cb2435c4c21c823d019d3a921e
[reactos.git] / dll / win32 / 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 "twain_i.h"
23
24 /* A helper function that looks up a destination identity in the active
25 source list */
26 static activeDS *TWAIN_LookupSource (const TW_IDENTITY *pDest)
27 {
28 activeDS *pSource;
29
30 for (pSource = activeSources; pSource; pSource = pSource->next)
31 if (pSource->identity.Id == pDest->Id)
32 break;
33 return pSource;
34 }
35
36 static TW_UINT16 TWAIN_SourceManagerHandler (
37 pTW_IDENTITY pOrigin,
38 TW_UINT16 DAT,
39 TW_UINT16 MSG,
40 TW_MEMREF pData)
41 {
42 TW_UINT16 twRC = TWRC_SUCCESS;
43
44 switch (DAT)
45 {
46 case DAT_IDENTITY:
47 switch (MSG)
48 {
49 case MSG_CLOSEDS:
50 twRC = TWAIN_CloseDS (pOrigin, pData);
51 break;
52
53 case MSG_GETDEFAULT:
54 twRC = TWAIN_IdentityGetDefault (pOrigin, pData);
55 break;
56
57 case MSG_GETFIRST:
58 twRC = TWAIN_IdentityGetFirst (pOrigin, pData);
59 break;
60
61 case MSG_GETNEXT:
62 twRC = TWAIN_IdentityGetNext (pOrigin, pData);
63 break;
64
65 case MSG_OPENDS:
66 twRC = TWAIN_OpenDS (pOrigin, pData);
67 break;
68
69 case MSG_USERSELECT:
70 twRC = TWAIN_UserSelect (pOrigin, pData);
71 break;
72
73 default:
74 /* Unrecognized operation triplet */
75 twRC = TWRC_FAILURE;
76 DSM_twCC = TWCC_BADPROTOCOL;
77 WARN("unrecognized operation triplet\n");
78 break;
79 }
80 break;
81
82 case DAT_PARENT:
83 switch (MSG)
84 {
85 case MSG_CLOSEDSM:
86 twRC = TWAIN_CloseDSM (pOrigin, pData);
87 break;
88
89 case MSG_OPENDSM:
90 twRC = TWAIN_OpenDSM (pOrigin, pData);
91 break;
92
93 default:
94 /* Unrecognized operation triplet */
95 twRC = TWRC_FAILURE;
96 DSM_twCC = TWCC_BADPROTOCOL;
97 WARN("unrecognized operation triplet\n");
98 }
99 break;
100
101 case DAT_STATUS:
102 if (MSG == MSG_GET) {
103 twRC = TWAIN_GetDSMStatus (pOrigin, pData);
104 } else {
105 twRC = TWRC_FAILURE;
106 DSM_twCC = TWCC_BADPROTOCOL;
107 WARN("unrecognized operation triplet\n");
108 }
109 break;
110
111 default:
112 twRC = TWRC_FAILURE;
113 DSM_twCC = TWCC_BADPROTOCOL;
114 WARN("unrecognized operation triplet\n");
115 break;
116 }
117
118 return twRC;
119 }
120
121
122 /* Main entry point for the TWAIN library */
123 TW_UINT16 WINAPI
124 DSM_Entry (pTW_IDENTITY pOrigin,
125 pTW_IDENTITY pDest,
126 TW_UINT32 DG,
127 TW_UINT16 DAT,
128 TW_UINT16 MSG,
129 TW_MEMREF pData)
130 {
131 TW_UINT16 twRC = TWRC_SUCCESS; /* Return Code */
132
133 TRACE("(DG=%d DAT=%d MSG=%d)\n", DG, DAT, MSG);
134
135 if (pDest)
136 {
137 activeDS *pSource = TWAIN_LookupSource (pDest);
138 /* This operation's destination is a source */
139
140 if (!pSource) {
141 ERR("No source associated with pDest %p\n", pDest);
142 DSM_twCC = TWCC_BADDEST;
143 return TWRC_FAILURE;
144 }
145 DSM_twCC = TWCC_SUCCESS;
146 TRACE("Forwarding %d/%d/%d/%p to DS.\n", DG, DAT, MSG, pData);
147 twRC = pSource->dsEntry(pOrigin, DG, DAT, MSG, pData);
148 TRACE("return value is %d\n", twRC);
149 return twRC;
150 }
151 switch (DG)
152 {
153 case DG_CONTROL:
154 twRC = TWAIN_SourceManagerHandler (pOrigin, DAT, MSG, pData);
155 break;
156 default:
157 FIXME("The DSM does not handle DG %d\n", DG);
158 DSM_twCC = TWCC_BADPROTOCOL;
159 twRC = TWRC_FAILURE;
160 }
161 return twRC;
162 }