kernel: Show the exe name instead of wine-[kp]thread in ps and top.
[wine] / dlls / twain / twain32_main.c
1 /*
2  * TWAIN32 functions
3  *
4  * Copyright 2000 Shi Quan He <shiquan@cyberdude.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "twain.h"
28 #include "twain_i.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(twain);
32
33 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
34 {
35     TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
36
37     switch (fdwReason)
38     {
39         case DLL_PROCESS_ATTACH:
40             DisableThreadLibraryCalls(hinstDLL);
41             DSM_currentState = 2;
42             DSM_instance = hinstDLL;
43             break;
44
45         case DLL_PROCESS_DETACH:
46             DSM_currentState = 1;
47             break;
48     }
49
50     return TRUE;
51 }
52
53 TW_UINT16 TWAIN_SourceManagerHandler (
54            pTW_IDENTITY pOrigin,
55            TW_UINT16   DAT,
56            TW_UINT16   MSG,
57            TW_MEMREF   pData)
58 {
59     TW_UINT16 twRC = TWRC_SUCCESS;
60
61     switch (DAT)
62     {
63         case DAT_IDENTITY:
64             switch (MSG)
65             {
66                 case MSG_CLOSEDS:
67                     twRC = TWAIN_CloseDS (pOrigin, pData);
68                     break;
69
70                 case MSG_GETDEFAULT:
71                     twRC = TWAIN_IdentityGetDefault (pOrigin, pData);
72                     break;
73
74                 case MSG_GETFIRST:
75                     twRC = TWAIN_IdentityGetFirst (pOrigin, pData);
76                     break;
77
78                 case MSG_GETNEXT:
79                     twRC = TWAIN_IdentityGetNext (pOrigin, pData);
80                     break;
81
82                 case MSG_OPENDS:
83                     twRC = TWAIN_OpenDS (pOrigin, pData);
84                     break;
85
86                 case MSG_USERSELECT:
87                     twRC = TWAIN_UserSelect (pOrigin, pData);
88                     break;
89
90                 default:
91                     /* Unrecognized operation triplet */
92                     twRC = TWRC_FAILURE;
93                     DSM_twCC = TWCC_BADPROTOCOL;
94                     WARN("unrecognized operation triplet\n");
95                     break;
96             }
97             break;
98
99         case DAT_PARENT:
100             switch (MSG)
101             {
102                 case MSG_CLOSEDSM:
103                     twRC = TWAIN_CloseDSM (pOrigin, pData);
104                     break;
105
106                 case MSG_OPENDSM:
107                     twRC = TWAIN_OpenDSM (pOrigin, pData);
108                     break;
109
110                 default:
111                     /* Unrecognized operation triplet */
112                     twRC = TWRC_FAILURE;
113                     DSM_twCC = TWCC_BADPROTOCOL;
114                     WARN("unrecognized operation triplet\n");
115             }
116             break;
117
118         case DAT_STATUS:
119             if (MSG == MSG_GET)
120             {
121                 twRC = TWAIN_GetDSMStatus (pOrigin, pData);
122             }
123             else
124             {
125                 twRC = TWRC_FAILURE;
126                 DSM_twCC = TWCC_BADPROTOCOL;
127                 WARN("unrecognized operation triplet\n");
128             }
129             break;
130
131         default:
132             twRC = TWRC_FAILURE;
133             DSM_twCC = TWCC_BADPROTOCOL;
134             WARN("unrecognized operation triplet\n");
135             break;
136     }
137
138     return twRC;
139 }
140
141 TW_UINT16 TWAIN_SourceControlHandler (
142            pTW_IDENTITY pOrigin,
143            pTW_IDENTITY pDest,
144            TW_UINT16    DAT,
145            TW_UINT16    MSG,
146            TW_MEMREF    pData)
147 {
148     TW_UINT16 twRC = TWRC_SUCCESS;
149
150     switch (DAT)
151     {
152         case DAT_CAPABILITY:
153             switch (MSG)
154             {
155                 case MSG_GET:
156                     twRC = TWAIN_CapabilityGet (pOrigin, pDest, pData);
157                     break;
158                 case MSG_GETCURRENT:
159                     twRC = TWAIN_CapabilityGetCurrent (pOrigin, pDest, pData);
160                     break;
161                 case MSG_GETDEFAULT:
162                     twRC = TWAIN_CapabilityGetDefault (pOrigin, pDest, pData);
163                     break;
164                 case MSG_QUERYSUPPORT:
165                     twRC = TWAIN_CapabilityQuerySupport (pOrigin, pDest, pData);
166                     break;
167                 case MSG_RESET:
168                     twRC = TWAIN_CapabilityReset (pOrigin, pDest, pData);
169                     break;
170                 case MSG_SET:
171                     twRC = TWAIN_CapabilitySet (pOrigin, pDest, pData);
172                     break;
173                 default:
174                     twRC = TWRC_FAILURE;
175                     WARN("unrecognized opertion triplet\n");
176             }
177             break;
178
179         case DAT_CUSTOMDSDATA:
180             switch (MSG)
181             {
182                 case MSG_GET:
183                     twRC = TWAIN_CustomDSDataGet (pOrigin, pDest, pData);
184                     break;
185                 case MSG_SET:
186                     twRC = TWAIN_CustomDSDataSet (pOrigin, pDest, pData);
187                     break;
188                 default:
189                     break;
190             }
191             break;
192
193         case DAT_FILESYSTEM:
194             switch (MSG)
195             {
196                 /*case MSG_AUTOMATICCAPTUREDIRECTORY:
197                     twRC = TWAIN_AutomaticCaptureDirectory
198                                (pOrigin, pDest, pData);
199                     break;*/
200                 case MSG_CHANGEDIRECTORY:
201                     twRC = TWAIN_ChangeDirectory (pOrigin, pDest, pData);
202                     break;
203                 /*case MSG_COPY:
204                     twRC = TWAIN_FileSystemCopy (pOrigin, pDest, pData);
205                     break;*/
206                 case MSG_CREATEDIRECTORY:
207                     twRC = TWAIN_CreateDirectory (pOrigin, pDest, pData);
208                     break;
209                 case MSG_DELETE:
210                     twRC = TWAIN_FileSystemDelete (pOrigin, pDest, pData);
211                     break;
212                 case MSG_FORMATMEDIA:
213                     twRC = TWAIN_FormatMedia (pOrigin, pDest, pData);
214                     break;
215                 case MSG_GETCLOSE:
216                     twRC = TWAIN_FileSystemGetClose (pOrigin, pDest, pData);
217                     break;
218                 case MSG_GETFIRSTFILE:
219                     twRC = TWAIN_FileSystemGetFirstFile
220                                (pOrigin, pDest, pData);
221                     break;
222                 case MSG_GETINFO:
223                     twRC = TWAIN_FileSystemGetInfo (pOrigin, pDest, pData);
224                     break;
225                 case MSG_GETNEXTFILE:
226                     twRC = TWAIN_FileSystemGetNextFile
227                                (pOrigin, pDest, pData);
228                     break;
229                 case MSG_RENAME:
230                     twRC = TWAIN_FileSystemRename (pOrigin, pDest, pData);
231                     break;
232                 default:
233                     twRC = TWRC_FAILURE;
234                     break;
235             }
236             break;
237
238         case DAT_EVENT:
239             if (MSG == MSG_PROCESSEVENT)
240                 twRC = TWAIN_ProcessEvent (pOrigin, pDest, pData);
241             else
242                 twRC = TWRC_FAILURE;
243             break;
244
245         case DAT_PASSTHRU:
246             if (MSG == MSG_PASSTHRU)
247                 twRC = TWAIN_PassThrough (pOrigin, pDest, pData);
248             else
249                 twRC = TWRC_FAILURE;
250             break;
251
252         case DAT_PENDINGXFERS:
253             switch (MSG)
254             {
255                 case MSG_ENDXFER:
256                     twRC = TWAIN_PendingXfersEndXfer (pOrigin, pDest, pData);
257                     break;
258                 case MSG_GET:
259                     twRC = TWAIN_PendingXfersGet (pOrigin, pDest, pData);
260                     break;
261                 case MSG_RESET:
262                     twRC = TWAIN_PendingXfersReset (pOrigin, pDest, pData);
263                     break;
264                 /*case MSG_STOPFEEDER:
265                     twRC = TWAIN_PendingXfersStopFeeder
266                                (pOrigin, pDest, pData);
267                     break;*/
268                 default:
269                     twRC = TWRC_FAILURE;
270             }
271             break;
272
273         case DAT_SETUPFILEXFER:
274             switch (MSG)
275             {
276                 case MSG_GET:
277                     twRC = TWAIN_SetupFileXferGet (pOrigin, pDest, pData);
278                     break;
279                 case MSG_GETDEFAULT:
280                     twRC = TWAIN_SetupFileXferGetDefault
281                                (pOrigin, pDest, pData);
282                     break;
283                 case MSG_RESET:
284                     twRC = TWAIN_SetupFileXferReset (pOrigin, pDest, pData);
285                     break;
286                 case MSG_SET:
287                     twRC = TWAIN_SetupFileXferSet (pOrigin, pDest, pData);
288                     break;
289                 default:
290                     twRC = TWRC_FAILURE;
291                     break;
292             }
293             break;
294
295         /*case DAT_SETUPFILEXFER2:
296             switch (MSG)
297             {
298                 case MSG_GET:
299                     twRC = TWAIN_SetupFileXfer2Get (pOrigin, pDest, pData);
300                     break;
301                 case MSG_GETDEFAULT:
302                     twRC = TWAIN_SetupFileXfer2GetDefault
303                                (pOrigin, pDest, pData);
304                     break;
305                 case MSG_RESET:
306                     twRC = TWAIN_SetupFileXfer2Reset (pOrigin, pDest, pData);
307                     break;
308                 case MSG_SET:
309                     twRC = TWAIN_SetupFileXfer2Set (pOrigin, pDest, pData);
310                     break;
311             }
312             break;*/
313         case DAT_SETUPMEMXFER:
314             if (MSG == MSG_GET)
315                 twRC = TWAIN_SetupMemXferGet (pOrigin, pDest, pData);
316             else
317                 twRC = TWRC_FAILURE;
318             break;
319
320         case DAT_STATUS:
321             if (MSG == MSG_GET)
322                 twRC = TWAIN_GetDSStatus (pOrigin, pDest, pData);
323             else
324                 twRC = TWRC_FAILURE;
325             break;
326
327         case DAT_USERINTERFACE:
328             switch (MSG)
329             {
330                 case MSG_DISABLEDS:
331                     twRC = TWAIN_DisableDSUserInterface
332                                (pOrigin, pDest, pData);
333                     break;
334                 case MSG_ENABLEDS:
335                     twRC = TWAIN_EnableDSUserInterface
336                                (pOrigin, pDest, pData);
337                     break;
338                 case MSG_ENABLEDSUIONLY:
339                     twRC = TWAIN_EnableDSUIOnly (pOrigin, pDest, pData);
340                     break;
341                 default:
342                     twRC = TWRC_FAILURE;
343                     break;
344             }
345             break;
346
347         case DAT_XFERGROUP:
348             switch (MSG)
349             {
350                 case MSG_GET:
351                     twRC = TWAIN_XferGroupGet (pOrigin, pDest, pData);
352                     break;
353                 case MSG_SET:
354                     twRC = TWAIN_XferGroupSet (pOrigin, pDest, pData);
355                     break;
356                 default:
357                     twRC = TWRC_FAILURE;
358                     break;
359             }
360             break;
361
362         default:
363             twRC = TWRC_FAILURE;
364             break;
365     }
366
367     return twRC;
368 }
369
370 TW_UINT16 TWAIN_ControlGroupHandler (
371            pTW_IDENTITY pOrigin,
372            pTW_IDENTITY pDest,
373            TW_UINT16    DAT,
374            TW_UINT16    MSG,
375            TW_MEMREF    pData)
376 {
377     TW_UINT16 twRC = TWRC_SUCCESS;
378
379     if (pDest)
380     {
381         /* This operation's destination is a source */
382         twRC = TWAIN_SourceControlHandler (pOrigin, pDest, DAT, MSG, pData);
383     }
384     else
385     {
386         /* This operation's destination is the Source Manager */
387         twRC = TWAIN_SourceManagerHandler (pOrigin, DAT, MSG, pData);
388     }
389
390     return twRC;
391 }
392
393 TW_UINT16 TWAIN_ImageGroupHandler (
394            pTW_IDENTITY pOrigin,
395            pTW_IDENTITY pDest,
396            TW_UINT16    DAT,
397            TW_UINT16    MSG,
398            TW_MEMREF    pData)
399 {
400     TW_UINT16 twRC = TWRC_SUCCESS;
401
402     if (pDest == NULL)
403     {
404         DSM_twCC = TWCC_BADDEST;
405         return TWRC_FAILURE;
406     }
407
408     switch (DAT)
409     {
410         case DAT_CIECOLOR:
411             if (MSG == MSG_GET)
412                 twRC = TWAIN_CIEColorGet (pOrigin, pDest, pData);
413             else
414                 twRC = TWRC_FAILURE;
415             break;
416
417         case DAT_EXTIMAGEINFO:
418             if (MSG == MSG_GET)
419                 twRC = TWAIN_ExtImageInfoGet (pOrigin, pDest, pData);
420             else
421                 twRC = TWRC_FAILURE;
422             break;
423
424         case DAT_GRAYRESPONSE:
425             switch (MSG)
426             {
427                 case MSG_RESET:
428                     twRC = TWAIN_GrayResponseReset (pOrigin, pDest, pData);
429                     break;
430                 case MSG_SET:
431                     twRC = TWAIN_GrayResponseSet (pOrigin, pDest, pData);
432                     break;
433                 default:
434                     twRC = TWRC_FAILURE;
435                     DSM_twCC = TWCC_BADPROTOCOL;
436                     WARN("unrecognized operation triplet\n");
437                     break;
438             }
439             break;
440         case DAT_IMAGEFILEXFER:
441             if (MSG == MSG_GET)
442                 twRC = TWAIN_ImageFileXferGet (pOrigin, pDest, pData);
443             else
444                 twRC = TWRC_FAILURE;
445             break;
446
447         case DAT_IMAGEINFO:
448             if (MSG == MSG_GET)
449                 twRC = TWAIN_ImageInfoGet (pOrigin, pDest, pData);
450             else
451                 twRC = TWRC_FAILURE;
452             break;
453
454         case DAT_IMAGELAYOUT:
455             switch (MSG)
456             {
457                 case MSG_GET:
458                     twRC = TWAIN_ImageLayoutGet (pOrigin, pDest, pData);
459                     break;
460                 case MSG_GETDEFAULT:
461                     twRC = TWAIN_ImageLayoutGetDefault (pOrigin, pDest, pData);
462                     break;
463                 case MSG_RESET:
464                     twRC = TWAIN_ImageLayoutReset (pOrigin, pDest, pData);
465                     break;
466                 case MSG_SET:
467                     twRC = TWAIN_ImageLayoutSet (pOrigin, pDest, pData);
468                     break;
469                 default:
470                     twRC = TWRC_FAILURE;
471                     DSM_twCC = TWCC_BADPROTOCOL;
472                     WARN("unrecognized operation triplet\n");
473                     break;
474             }
475             break;
476
477         case DAT_IMAGEMEMXFER:
478             if (MSG == MSG_GET)
479                 twRC = TWAIN_ImageMemXferGet (pOrigin, pDest, pData);
480             else
481                 twRC = TWRC_FAILURE;
482             break;
483
484         case DAT_IMAGENATIVEXFER:
485             if (MSG == MSG_GET)
486                 twRC = TWAIN_ImageNativeXferGet (pOrigin, pDest, pData);
487             else
488                 twRC = TWRC_FAILURE;
489             break;
490
491         case DAT_JPEGCOMPRESSION:
492             switch (MSG)
493             {
494                 case MSG_GET:
495                     twRC = TWAIN_JPEGCompressionGet (pOrigin, pDest, pData);
496                     break;
497                 case MSG_GETDEFAULT:
498                     twRC = TWAIN_JPEGCompressionGetDefault
499                                (pOrigin, pDest, pData);
500                     break;
501                 case MSG_RESET:
502                     twRC = TWAIN_JPEGCompressionReset (pOrigin, pDest, pData);
503                     break;
504                 case MSG_SET:
505                     twRC = TWAIN_JPEGCompressionSet (pOrigin, pDest, pData);
506                     break;
507                 default:
508                     twRC = TWRC_FAILURE;
509                     DSM_twCC = TWCC_BADPROTOCOL;
510                     WARN("unrecognized operation triplet\n");
511                     break;
512             }
513             break;
514
515         case DAT_PALETTE8:
516             switch (MSG)
517             {
518                 case MSG_GET:
519                     twRC = TWAIN_Palette8Get (pOrigin, pDest, pData);
520                     break;
521                 case MSG_GETDEFAULT:
522                     twRC = TWAIN_Palette8GetDefault (pOrigin, pDest, pData);
523                     break;
524                 case MSG_RESET:
525                     twRC = TWAIN_Palette8Reset (pOrigin, pDest, pData);
526                     break;
527                 case MSG_SET:
528                     twRC = TWAIN_Palette8Set (pOrigin, pDest, pData);
529                     break;
530                 default:
531                     twRC = TWRC_FAILURE;
532                     DSM_twCC = TWCC_BADPROTOCOL;
533                     WARN("unrecognized operation triplet\n");
534             }
535             break;
536
537         case DAT_RGBRESPONSE:
538             switch (MSG)
539             {
540                 case MSG_RESET:
541                     twRC = TWAIN_RGBResponseReset (pOrigin, pDest, pData);
542                     break;
543                 case MSG_SET:
544                     twRC = TWAIN_RGBResponseSet (pOrigin, pDest, pData);
545                     break;
546                 default:
547                     twRC = TWRC_FAILURE;
548                     DSM_twCC = TWCC_BADPROTOCOL;
549                     WARN("unrecognized operation triplet\n");
550                     break;
551             }
552             break;
553
554         default:
555             twRC = TWRC_FAILURE;
556             DSM_twCC = TWCC_BADPROTOCOL;
557             WARN("unrecognized operation triplet\n");
558     }
559     return twRC;
560 }
561
562 TW_UINT16 TWAIN_AudioGroupHandler (
563            pTW_IDENTITY pOrigin,
564            pTW_IDENTITY pDest,
565            TW_UINT16    DAT,
566            TW_UINT16    MSG,
567            TW_MEMREF    pData)
568 {
569     TW_UINT16 twRC = TWRC_FAILURE;
570
571     switch (DAT)
572     {
573         case DAT_AUDIOFILEXFER:
574             if (MSG == MSG_GET)
575                 twRC = TWAIN_AudioFileXferGet (pOrigin, pDest, pData);
576             break;
577
578         case DAT_AUDIOINFO:
579             if (MSG == MSG_GET)
580                 twRC = TWAIN_AudioInfoGet (pOrigin, pDest, pData);
581             break;
582
583         case DAT_AUDIONATIVEXFER:
584             if (MSG == MSG_GET)
585                 twRC = TWAIN_AudioNativeXferGet (pOrigin, pDest, pData);
586             break;
587
588         default:
589             DSM_twCC = TWCC_BADPROTOCOL;
590             twRC = TWRC_FAILURE;
591             break;
592     }
593
594     return twRC;
595 }
596
597 /* Main entry point for the TWAIN library */
598 TW_UINT16 WINAPI
599 DSM_Entry (pTW_IDENTITY pOrigin,
600            pTW_IDENTITY pDest,
601            TW_UINT32    DG,
602            TW_UINT16    DAT,
603            TW_UINT16    MSG,
604            TW_MEMREF    pData)
605 {
606     TW_UINT16 twRC = TWRC_SUCCESS;  /* Return Code */
607
608     TRACE("(DG=%ld DAT=%d MSG=%d)\n", DG, DAT, MSG);
609
610     switch (DG)
611     {
612         case DG_CONTROL:
613             twRC = TWAIN_ControlGroupHandler (pOrigin,pDest,DAT,MSG,pData);
614             break;
615         case DG_IMAGE:
616             twRC = TWAIN_ImageGroupHandler (pOrigin,pDest,DAT,MSG,pData);
617             break;
618         case DG_AUDIO:
619             twRC = TWAIN_AudioGroupHandler (pOrigin,pDest,DAT,MSG,pData);
620             break;
621         default:
622             DSM_twCC = TWCC_BADPROTOCOL;
623             twRC = TWRC_FAILURE;
624     }
625
626     return twRC;
627 }
628
629 /* A helper function that looks up a destination identity in the active
630    source list */
631 activeDS *TWAIN_LookupSource (pTW_IDENTITY pDest)
632 {
633     activeDS *pSource;
634
635     for (pSource = activeSources; pSource; pSource = pSource->next)
636         if (pSource->identity.Id == pDest->Id)
637             break;
638
639     return pSource;
640 }