user32: Simplify some maximized MDI child checks.
[wine] / dlls / gphoto2.ds / capability.c
1 /*
2  * Copyright 2000 Corel Corporation
3  * Copyright 2006 Marcus Meissner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
22
23 #include "config.h"
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "twain.h"
30 #include "gphoto2_i.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(twain);
34
35 static TW_UINT16 GPHOTO2_ICAPXferMech (pTW_CAPABILITY,TW_UINT16);
36 static TW_UINT16 GPHOTO2_ICAPPixelType (pTW_CAPABILITY,TW_UINT16);
37 static TW_UINT16 GPHOTO2_ICAPPixelFlavor (pTW_CAPABILITY,TW_UINT16);
38 static TW_UINT16 GPHOTO2_ICAPBitDepth (pTW_CAPABILITY,TW_UINT16);
39 static TW_UINT16 GPHOTO2_ICAPUnits (pTW_CAPABILITY,TW_UINT16);
40
41 TW_UINT16 GPHOTO2_SaneCapability (pTW_CAPABILITY pCapability, TW_UINT16 action)
42 {
43     TW_UINT16 twCC = TWCC_SUCCESS;
44
45     TRACE("capability=%d action=%d\n", pCapability->Cap, action);
46
47     switch (pCapability->Cap)
48     {
49         case CAP_DEVICEEVENT:
50         case CAP_ALARMS:
51         case CAP_ALARMVOLUME:
52         case ACAP_AUDIOFILEFORMAT:
53         case ACAP_XFERMECH:
54         case ICAP_AUTOMATICBORDERDETECTION:
55         case ICAP_AUTOMATICDESKEW:
56         case ICAP_AUTODISCARDBLANKPAGES:
57         case ICAP_AUTOMATICROTATE:
58         case ICAP_FLIPROTATION:
59         case CAP_AUTOMATICCAPTURE:
60         case CAP_TIMEBEFOREFIRSTCAPTURE:
61         case CAP_TIMEBETWEENCAPTURES:
62         case CAP_AUTOSCAN:
63         case CAP_CLEARBUFFERS:
64         case CAP_MAXBATCHBUFFERS:
65         case ICAP_BARCODEDETECTIONENABLED:
66         case ICAP_SUPPORTEDBARCODETYPES:
67         case ICAP_BARCODEMAXSEARCHPRIORITIES:
68         case ICAP_BARCODESEARCHPRIORITIES:
69         case ICAP_BARCODESEARCHMODE:
70         case ICAP_BARCODEMAXRETRIES:
71         case ICAP_BARCODETIMEOUT:
72         case CAP_EXTENDEDCAPS:
73         case CAP_SUPPORTEDCAPS:
74         case ICAP_FILTER:
75         case ICAP_GAMMA:
76         case ICAP_PLANARCHUNKY:
77         case ICAP_BITORDERCODES:
78         case ICAP_CCITTKFACTOR:
79         case ICAP_JPEGPIXELTYPE:
80         /*case ICAP_JPEGQUALITY:*/
81         case ICAP_PIXELFLAVORCODES:
82         case ICAP_TIMEFILL:
83         case CAP_DEVICEONLINE:
84         case CAP_DEVICETIMEDATE:
85         case CAP_SERIALNUMBER:
86         case ICAP_EXPOSURETIME:
87         case ICAP_FLASHUSED2:
88         case ICAP_IMAGEFILTER:
89         case ICAP_LAMPSTATE:
90         case ICAP_LIGHTPATH:
91         case ICAP_NOISEFILTER:
92         case ICAP_OVERSCAN:
93         case ICAP_PHYSICALHEIGHT:
94         case ICAP_PHYSICALWIDTH:
95         case ICAP_ZOOMFACTOR:
96         case CAP_PRINTER:
97         case CAP_PRINTERENABLED:
98         case CAP_PRINTERINDEX:
99         case CAP_PRINTERMODE:
100         case CAP_PRINTERSTRING:
101         case CAP_PRINTERSUFFIX:
102         case CAP_AUTHOR:
103         case CAP_CAPTION:
104         case CAP_TIMEDATE:
105         case ICAP_AUTOBRIGHT:
106         case ICAP_BRIGHTNESS:
107         case ICAP_CONTRAST:
108         case ICAP_HIGHLIGHT:
109         case ICAP_ORIENTATION:
110         case ICAP_ROTATION:
111         case ICAP_SHADOW:
112         case ICAP_XSCALING:
113         case ICAP_YSCALING:
114         case ICAP_BITDEPTHREDUCTION:
115         case ICAP_BITORDER:
116         case ICAP_CUSTHALFTONE:
117         case ICAP_HALFTONES:
118         case ICAP_THRESHOLD:
119         case CAP_LANGUAGE:
120         case ICAP_FRAMES:
121         case ICAP_MAXFRAMES:
122         case ICAP_SUPPORTEDSIZES:
123         case CAP_AUTOFEED:
124         case CAP_CLEARPAGE:
125         case CAP_FEEDERALIGNMENT:
126         case CAP_FEEDERENABLED:
127         case CAP_FEEDERLOADED:
128         case CAP_FEEDERORDER:
129         case CAP_FEEDPAGE:
130         case CAP_PAPERBINDING:
131         case CAP_PAPERDETECTABLE:
132         case CAP_REACQUIREALLOWED:
133         case CAP_REWINDPAGE:
134         case ICAP_PATCHCODEDETECTIONENABLED:
135         case ICAP_SUPPORTEDPATCHCODETYPES:
136         case ICAP_PATCHCODEMAXSEARCHPRIORITIES:
137         case ICAP_PATCHCODESEARCHPRIORITIES:
138         case ICAP_PATCHCODESEARCHMODE:
139         case ICAP_PATCHCODEMAXRETRIES:
140         case ICAP_PATCHCODETIMEOUT:
141         case CAP_BATTERYMINUTES:
142         case CAP_BATTERYPERCENTAGE:
143         case CAP_POWERDOWNTIME:
144         case CAP_POWERSUPPLY:
145         case ICAP_XNATIVERESOLUTION:
146         case ICAP_XRESOLUTION:
147         case ICAP_YNATIVERESOLUTION:
148         case ICAP_YRESOLUTION:
149             twCC = TWCC_CAPUNSUPPORTED;
150             break;
151         case CAP_XFERCOUNT:
152             /* This is a required capability that every source need to
153                support but we haven't implemented yet. */
154             twCC = TWCC_SUCCESS;
155             break;
156         /*case ICAP_COMPRESSION:*/
157         case ICAP_IMAGEFILEFORMAT:
158         case ICAP_TILES:
159             twCC = TWCC_CAPUNSUPPORTED;
160             break;
161         case ICAP_XFERMECH:
162             twCC = GPHOTO2_ICAPXferMech (pCapability, action);
163             break;
164         case ICAP_PIXELTYPE:
165             twCC = GPHOTO2_ICAPPixelType (pCapability, action);
166             break;
167         case ICAP_PIXELFLAVOR:
168             twCC = GPHOTO2_ICAPPixelFlavor (pCapability, action);
169             break;
170         case ICAP_BITDEPTH:
171             twCC = GPHOTO2_ICAPBitDepth (pCapability, action);
172             break;
173         case ICAP_UNITS:
174             twCC = GPHOTO2_ICAPUnits (pCapability, action);
175             break;
176         case ICAP_UNDEFINEDIMAGESIZE:
177         case CAP_CAMERAPREVIEWUI:
178         case CAP_ENABLEDSUIONLY:
179         case CAP_INDICATORS:
180         case CAP_UICONTROLLABLE:
181             twCC = TWCC_CAPUNSUPPORTED;
182             break;
183
184         case ICAP_COMPRESSION:
185             twCC = TWCC_SUCCESS;
186             break;
187         default:
188             twCC = TWRC_FAILURE;
189             break;
190     }
191     return twCC;
192 }
193
194 static TW_BOOL GPHOTO2_OneValueSet32 (pTW_CAPABILITY pCapability, TW_UINT32 value)
195 {
196     pCapability->hContainer = (TW_HANDLE)GlobalAlloc (0, sizeof(TW_ONEVALUE));
197
198     TRACE("-> %ld\n", value);
199
200     if (pCapability->hContainer)
201     {
202         pTW_ONEVALUE pVal = GlobalLock ((HGLOBAL) pCapability->hContainer);
203         pVal->ItemType = TWTY_UINT32;
204         pVal->Item = value;
205         GlobalUnlock ((HGLOBAL) pCapability->hContainer);
206         pCapability->ConType = TWON_ONEVALUE;
207         return TRUE;
208     }
209     else
210         return FALSE;
211 }
212
213 static TW_BOOL GPHOTO2_OneValueSet16 (pTW_CAPABILITY pCapability, TW_UINT16 value)
214 {
215     pCapability->hContainer = (TW_HANDLE)GlobalAlloc (0, sizeof(TW_ONEVALUE));
216
217     TRACE("-> %d\n", value);
218
219     if (pCapability->hContainer)
220     {
221         pTW_ONEVALUE pVal = GlobalLock ((HGLOBAL) pCapability->hContainer);
222         pVal->ItemType = TWTY_UINT16;
223         pVal->Item = value;
224         GlobalUnlock ((HGLOBAL) pCapability->hContainer);
225         pCapability->ConType = TWON_ONEVALUE;
226         return TRUE;
227     }
228     else
229         return FALSE;
230 }
231
232 static TW_BOOL GPHOTO2_EnumSet16 (pTW_CAPABILITY pCapability, int nrofvalues, TW_UINT16 *values, int current, int def)
233 {
234     pTW_ENUMERATION pVal;
235     pCapability->hContainer = (TW_HANDLE)GlobalAlloc (0, sizeof(TW_ENUMERATION) + nrofvalues * sizeof(TW_UINT16));
236
237     if (!pCapability->hContainer)
238         return FALSE;
239
240     pVal = GlobalLock ((HGLOBAL) pCapability->hContainer);
241     pVal->ItemType = TWTY_UINT16;
242     pVal->NumItems = nrofvalues;
243     memcpy(pVal->ItemList, values, sizeof(TW_UINT16)*nrofvalues);
244     pVal->CurrentIndex = current;
245     pVal->DefaultIndex = def;
246     pCapability->ConType = TWON_ENUMERATION;
247     GlobalUnlock ((HGLOBAL) pCapability->hContainer);
248     return TRUE;
249 }
250
251 static TW_BOOL GPHOTO2_EnumGet16 (pTW_CAPABILITY pCapability, int *nrofvalues, TW_UINT16 **values)
252 {
253     pTW_ENUMERATION pVal = GlobalLock ((HGLOBAL) pCapability->hContainer);
254
255     if (!pVal)
256         return FALSE;
257     *nrofvalues = pVal->NumItems;
258     *values = HeapAlloc( GetProcessHeap(), 0, sizeof(TW_UINT16)*pVal->NumItems);
259     memcpy (*values, pVal->ItemList, sizeof(TW_UINT16)*(*nrofvalues));
260     FIXME("Current Index %ld, Default Index %ld\n", pVal->CurrentIndex, pVal->DefaultIndex);
261     GlobalUnlock ((HGLOBAL) pCapability->hContainer);
262     return TRUE;
263 }
264
265 static TW_BOOL GPHOTO2_OneValueGet32 (pTW_CAPABILITY pCapability, TW_UINT32 *pValue)
266 {
267     pTW_ONEVALUE pVal = GlobalLock ((HGLOBAL) pCapability->hContainer);
268
269     if (pVal)
270     {
271         *pValue = pVal->Item;
272         GlobalUnlock ((HGLOBAL) pCapability->hContainer);
273         return TRUE;
274     }
275     else
276         return FALSE;
277 }
278
279 static TW_BOOL GPHOTO2_OneValueGet16 (pTW_CAPABILITY pCapability, TW_UINT16 *pValue)
280 {
281     pTW_ONEVALUE pVal = GlobalLock ((HGLOBAL) pCapability->hContainer);
282
283     if (pVal)
284     {
285         *pValue = pVal->Item;
286         GlobalUnlock ((HGLOBAL) pCapability->hContainer);
287         return TRUE;
288     }
289     else
290         return FALSE;
291 }
292
293 /* ICAP_XFERMECH */
294 static TW_UINT16 GPHOTO2_ICAPXferMech (pTW_CAPABILITY pCapability, TW_UINT16 action)
295 {
296     TRACE("ICAP_XFERMECH, action %d\n", action);
297
298     switch (action)
299     {
300         case MSG_GET:
301             if (!GPHOTO2_OneValueSet32 (pCapability, activeDS.capXferMech))
302                 return TWCC_LOWMEMORY;
303             return TWCC_SUCCESS;
304         case MSG_SET:
305             if (pCapability->ConType == TWON_ONEVALUE)
306             {
307                 TW_UINT32 xfermechtemp = 0;
308
309                 if (!GPHOTO2_OneValueGet32 (pCapability, &xfermechtemp))
310                     return TWCC_LOWMEMORY;
311                 activeDS.capXferMech = xfermechtemp;
312                 TRACE("xfermech is %ld\n", xfermechtemp);
313                 return TWCC_SUCCESS;
314             }
315             else if (pCapability->ConType == TWON_ENUMERATION)
316             {
317
318             }
319             FIXME("GET FAILED\n");
320             break;
321         case MSG_GETCURRENT:
322             if (!GPHOTO2_OneValueSet32 (pCapability, activeDS.capXferMech))
323                 return TWCC_LOWMEMORY;
324             break;
325         case MSG_GETDEFAULT:
326             if (!GPHOTO2_OneValueSet32 (pCapability, TWSX_NATIVE))
327                 return TWCC_LOWMEMORY;
328             break;
329         case MSG_RESET:
330             activeDS.capXferMech = TWSX_NATIVE;
331             break;
332     }
333     return TWCC_SUCCESS;
334 }
335
336 /* ICAP_PIXELTYPE */
337 static TW_UINT16 GPHOTO2_ICAPPixelType (pTW_CAPABILITY pCapability, TW_UINT16 action)
338 {
339     TRACE("Action %d\n", action);
340
341     switch (action)
342     {
343         case MSG_GET:
344             if ((pCapability->ConType == TWON_DONTCARE16) ||
345                 (pCapability->ConType == TWON_ONEVALUE)
346             ) {
347                 if (!GPHOTO2_OneValueSet16 (pCapability, activeDS.pixeltype))
348                     return TWCC_LOWMEMORY;
349                 return TWCC_SUCCESS;
350             }
351             FIXME("Unknown container type %x in MSG_GET\n", pCapability->ConType);
352             return TWCC_CAPBADOPERATION;
353         case MSG_SET:
354             if (pCapability->ConType == TWON_ONEVALUE)
355             {
356                 TW_UINT16 pixeltype = 0;
357
358                 if (!GPHOTO2_OneValueGet16 (pCapability, &pixeltype))
359                     return TWCC_LOWMEMORY;
360                 activeDS.pixeltype = pixeltype;
361                 FIXME("pixeltype changed to %d!\n", pixeltype);
362                 return TWCC_SUCCESS;
363             }
364             FIXME("set not done\n");
365             if (pCapability->ConType == TWON_ENUMERATION) {
366                 TW_UINT16       *values = NULL;
367                 int i, nrofvalues = 0;
368                 
369                 if (!GPHOTO2_EnumGet16 (pCapability, &nrofvalues, &values))
370                     return TWCC_LOWMEMORY;
371                 for (i=0;i<nrofvalues;i++)
372                     FIXME("SET PixelType %d:%d\n", i, values[i]);
373                 HeapFree (GetProcessHeap(), 0, values);
374             }
375             break;
376         case MSG_GETCURRENT:
377             if (!GPHOTO2_OneValueSet16 (pCapability, activeDS.pixeltype)) {
378                 FIXME("Failed one value set in GETCURRENT, contype %d!\n", pCapability->ConType);
379                 return TWCC_LOWMEMORY;
380             }
381             break;
382         case MSG_GETDEFAULT:
383             if (!GPHOTO2_OneValueSet16 (pCapability, TWPT_RGB)) {
384                 FIXME("Failed onevalue set in GETDEFAULT!\n");
385                 return TWCC_LOWMEMORY;
386             }
387             break;
388         case MSG_RESET:
389             activeDS.pixeltype = TWPT_RGB;
390             break;
391     }
392     return TWCC_SUCCESS;
393 }
394
395 /* ICAP_PIXELFLAVOR */
396 static TW_UINT16 GPHOTO2_ICAPPixelFlavor (pTW_CAPABILITY pCapability, TW_UINT16 action)
397 {
398     TRACE("Action %d\n", action);
399
400     switch (action)
401     {
402         case MSG_GET:
403             if ((pCapability->ConType == TWON_DONTCARE16)       ||
404                 (pCapability->ConType == TWON_ONEVALUE)
405             ) {
406                 if (!GPHOTO2_OneValueSet16 (pCapability, TWPF_CHOCOLATE))
407                     return TWCC_LOWMEMORY;
408                 return TWCC_SUCCESS;
409             }
410             if (!pCapability->ConType) {
411                 TW_UINT16 arr[2];
412                 arr[0] = TWPF_CHOCOLATE;
413                 arr[1] = TWPF_VANILLA;
414
415                 if (!GPHOTO2_EnumSet16 (pCapability, 2, arr, 1, 1))
416                     return TWCC_LOWMEMORY;
417                 return TWCC_SUCCESS;
418             }
419             FIXME("MSG_GET container type %x unhandled\n", pCapability->ConType);
420             return TWCC_BADVALUE;
421         case MSG_SET:
422             if (pCapability->ConType == TWON_ONEVALUE)
423             {
424                 TW_UINT16 pixelflavor = 0;
425
426                 if (!GPHOTO2_OneValueGet16 (pCapability, &pixelflavor))
427                     return TWCC_LOWMEMORY;
428                 activeDS.pixelflavor = pixelflavor;
429                 FIXME("pixelflavor is %d\n", pixelflavor);
430             }
431             break;
432         case MSG_GETCURRENT:
433             if (!GPHOTO2_OneValueSet16 (pCapability, activeDS.pixelflavor))
434                 return TWCC_LOWMEMORY;
435             break;
436         case MSG_GETDEFAULT:
437             if (!GPHOTO2_OneValueSet16 (pCapability, TWPF_CHOCOLATE))
438                 return TWCC_LOWMEMORY;
439             break;
440         case MSG_RESET:
441             break;
442     }
443     return TWCC_SUCCESS;
444 }
445
446 /* ICAP_BITDEPTH */
447 static TW_UINT16 GPHOTO2_ICAPBitDepth (pTW_CAPABILITY pCapability, TW_UINT16 action)
448 {
449     TRACE("Action %d\n", action);
450
451     switch (action)
452     {
453         case MSG_GET:
454             if ((pCapability->ConType == TWON_DONTCARE16) ||
455                 (pCapability->ConType == TWON_ONEVALUE)
456             ) {
457                 if (!GPHOTO2_OneValueSet16 (pCapability, 24))
458                     return TWCC_LOWMEMORY;
459                 return TWCC_SUCCESS;
460             }
461             FIXME("MSG_GET container type %x unhandled\n", pCapability->ConType);
462             return TWCC_SUCCESS;
463         case MSG_SET:
464             if (pCapability->ConType == TWON_ONEVALUE) {
465                 TW_UINT16 bitdepth = 0;
466
467                 if (!GPHOTO2_OneValueGet16 (pCapability, &bitdepth))
468                     return TWCC_LOWMEMORY;
469                 if (bitdepth != 24)
470                     return TWCC_BADVALUE;
471                 return TWCC_SUCCESS;
472             }
473             if (pCapability->ConType == TWON_ENUMERATION)
474             {
475                 int i, nrofvalues = 0;
476                 TW_UINT16 *values = NULL;
477
478                 if (!GPHOTO2_EnumGet16 (pCapability, &nrofvalues, &values))
479                     return TWCC_LOWMEMORY;
480                 for (i=0;i<nrofvalues;i++)
481                     FIXME("SET: enum element %d = %d\n", i, values[i]);
482                 HeapFree (GetProcessHeap(), 0, values);
483                 return TWCC_SUCCESS;
484             }
485             FIXME("Unhandled container type %d in MSG_SET\n", pCapability->ConType);
486             break;
487         case MSG_GETCURRENT:
488             if (!GPHOTO2_OneValueSet16 (pCapability, 24))
489                 return TWCC_LOWMEMORY;
490             break;
491         case MSG_GETDEFAULT:
492             if (!GPHOTO2_OneValueSet16 (pCapability, 24))
493                 return TWCC_LOWMEMORY;
494             break;
495         case MSG_RESET:
496             break;
497     }
498     return TWCC_SUCCESS;
499 }
500
501 /* ICAP_UNITS */
502 static TW_UINT16 GPHOTO2_ICAPUnits (pTW_CAPABILITY pCapability, TW_UINT16 action)
503 {
504     TRACE("Action %d\n", action);
505
506     switch (action)
507     {
508         case MSG_GET:
509             if ((pCapability->ConType == TWON_DONTCARE16) ||
510                 (pCapability->ConType == TWON_ONEVALUE)
511             ) {
512                 if (!GPHOTO2_OneValueSet16 (pCapability, TWUN_PIXELS))
513                     return TWCC_LOWMEMORY;
514                 return TWCC_SUCCESS;
515             }
516             FIXME("MSG_GET container type %x unhandled\n", pCapability->ConType);
517             return TWCC_SUCCESS;
518         case MSG_SET:
519             if (pCapability->ConType == TWON_ONEVALUE) {
520                 TW_UINT16 units = 0;
521
522                 if (!GPHOTO2_OneValueGet16 (pCapability, &units))
523                     return TWCC_LOWMEMORY;
524                 FIXME("SET to type %d, stub.\n", units);
525                 return TWCC_SUCCESS;
526             }
527             if (pCapability->ConType == TWON_ENUMERATION)
528             {
529                 int i, nrofvalues = 0;
530                 TW_UINT16 *values = NULL;
531
532                 if (!GPHOTO2_EnumGet16 (pCapability, &nrofvalues, &values))
533                     return TWCC_LOWMEMORY;
534                 for (i=0;i<nrofvalues;i++)
535                     FIXME("SET: enum element %d = %d\n", i, values[i]);
536                 HeapFree (GetProcessHeap(), 0, values);
537                 return TWCC_SUCCESS;
538             }
539             FIXME("Unhandled container type %d in MSG_SET\n", pCapability->ConType);
540             break;
541         case MSG_GETCURRENT:
542             if (!GPHOTO2_OneValueSet16 (pCapability, TWUN_INCHES))
543                 return TWCC_LOWMEMORY;
544             break;
545         case MSG_GETDEFAULT:
546             if (!GPHOTO2_OneValueSet16 (pCapability, TWUN_PIXELS))
547                 return TWCC_LOWMEMORY;
548             break;
549         case MSG_RESET:
550             break;
551     }
552     return TWCC_SUCCESS;
553 }