dplayx: Introduce impl_from_IDirectPlayLobby3A().
[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 static TW_UINT16 TWAIN_SourceManagerHandler (
47            pTW_IDENTITY pOrigin,
48            TW_UINT16   DAT,
49            TW_UINT16   MSG,
50            TW_MEMREF   pData)
51 {
52     TW_UINT16 twRC = TWRC_SUCCESS;
53
54     switch (DAT)
55     {
56         case DAT_IDENTITY:
57             switch (MSG)
58             {
59                 case MSG_CLOSEDS:
60                     twRC = TWAIN_CloseDS (pOrigin, pData);
61                     break;
62
63                 case MSG_GETDEFAULT:
64                     twRC = TWAIN_IdentityGetDefault (pOrigin, pData);
65                     break;
66
67                 case MSG_GETFIRST:
68                     twRC = TWAIN_IdentityGetFirst (pOrigin, pData);
69                     break;
70
71                 case MSG_GETNEXT:
72                     twRC = TWAIN_IdentityGetNext (pOrigin, pData);
73                     break;
74
75                 case MSG_OPENDS:
76                     twRC = TWAIN_OpenDS (pOrigin, pData);
77                     break;
78
79                 case MSG_USERSELECT:
80                     twRC = TWAIN_UserSelect (pOrigin, pData);
81                     break;
82
83                 default:
84                     /* Unrecognized operation triplet */
85                     twRC = TWRC_FAILURE;
86                     DSM_twCC = TWCC_BADPROTOCOL;
87                     WARN("unrecognized operation triplet\n");
88                     break;
89             }
90             break;
91
92         case DAT_PARENT:
93             switch (MSG)
94             {
95                 case MSG_CLOSEDSM:
96                     twRC = TWAIN_CloseDSM (pOrigin, pData);
97                     break;
98
99                 case MSG_OPENDSM:
100                     twRC = TWAIN_OpenDSM (pOrigin, pData);
101                     break;
102
103                 default:
104                     /* Unrecognized operation triplet */
105                     twRC = TWRC_FAILURE;
106                     DSM_twCC = TWCC_BADPROTOCOL;
107                     WARN("unrecognized operation triplet\n");
108             }
109             break;
110
111         case DAT_STATUS:
112             if (MSG == MSG_GET) {
113                 twRC = TWAIN_GetDSMStatus (pOrigin, pData);
114             } else {
115                 twRC = TWRC_FAILURE;
116                 DSM_twCC = TWCC_BADPROTOCOL;
117                 WARN("unrecognized operation triplet\n");
118             }
119             break;
120
121         default:
122             twRC = TWRC_FAILURE;
123             DSM_twCC = TWCC_BADPROTOCOL;
124             WARN("unrecognized operation triplet\n");
125             break;
126     }
127
128     return twRC;
129 }
130
131
132 /* Main entry point for the TWAIN library */
133 TW_UINT16 WINAPI
134 DSM_Entry (pTW_IDENTITY pOrigin,
135            pTW_IDENTITY pDest,
136            TW_UINT32    DG,
137            TW_UINT16    DAT,
138            TW_UINT16    MSG,
139            TW_MEMREF    pData)
140 {
141     TW_UINT16 twRC = TWRC_SUCCESS;  /* Return Code */
142
143     TRACE("(DG=%d DAT=%d MSG=%d)\n", DG, DAT, MSG);
144
145     if (pDest)
146     {
147         activeDS *pSource = TWAIN_LookupSource (pDest);
148         /* This operation's destination is a source */
149
150         if (!pSource) {
151             ERR("No source associated with pDest %p\n", pDest);
152             DSM_twCC = TWCC_BADDEST;
153             return TWRC_FAILURE;
154         }
155         DSM_twCC = TWCC_SUCCESS;
156         TRACE("Forwarding %d/%d/%d/%p to DS.\n", DG, DAT, MSG, pData);
157         twRC = pSource->dsEntry(pOrigin, DG, DAT, MSG, pData);
158         TRACE("return value is %d\n", twRC);
159         return twRC;
160     }
161     switch (DG)
162     {
163         case DG_CONTROL:
164             twRC = TWAIN_SourceManagerHandler (pOrigin, DAT, MSG, pData);
165             break;
166         default:
167             FIXME("The DSM does not handle DG %d\n", DG);
168             DSM_twCC = TWCC_BADPROTOCOL;
169             twRC = TWRC_FAILURE;
170     }
171     return twRC;
172 }