include: Specify calling convention explicitly in idl files where needed.
[wine/hramrach.git] / dlls / sane.ds / capability.c
blob20fd2c3b50aa6d13b61b74530450f4930f634173
1 /*
2 * Copyright 2000 Corel Corporation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
22 #include "config.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <math.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "twain.h"
31 #include "sane_i.h"
32 #include "winnls.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(twain);
37 #ifndef SANE_VALUE_SCAN_MODE_COLOR
38 #define SANE_VALUE_SCAN_MODE_COLOR SANE_I18N("Color")
39 #endif
40 #ifndef SANE_VALUE_SCAN_MODE_GRAY
41 #define SANE_VALUE_SCAN_MODE_GRAY SANE_I18N("Gray")
42 #endif
43 #ifndef SANE_VALUE_SCAN_MODE_LINEART
44 #define SANE_VALUE_SCAN_MODE_LINEART SANE_I18N("Lineart")
45 #endif
47 static TW_UINT16 get_onevalue(pTW_CAPABILITY pCapability, TW_UINT16 *type, TW_UINT32 *value)
49 if (pCapability->hContainer)
51 pTW_ONEVALUE pVal = GlobalLock (pCapability->hContainer);
52 if (pVal)
54 *value = pVal->Item;
55 if (type)
56 *type = pVal->ItemType;
57 GlobalUnlock (pCapability->hContainer);
58 return TWCC_SUCCESS;
61 return TWCC_BUMMER;
65 static TW_UINT16 set_onevalue(pTW_CAPABILITY pCapability, TW_UINT16 type, TW_UINT32 value)
67 pCapability->hContainer = GlobalAlloc (0, sizeof(TW_ONEVALUE));
69 if (pCapability->hContainer)
71 pTW_ONEVALUE pVal = GlobalLock (pCapability->hContainer);
72 if (pVal)
74 pCapability->ConType = TWON_ONEVALUE;
75 pVal->ItemType = type;
76 pVal->Item = value;
77 GlobalUnlock (pCapability->hContainer);
78 return TWCC_SUCCESS;
81 return TWCC_LOWMEMORY;
84 static TW_UINT16 msg_set(pTW_CAPABILITY pCapability, TW_UINT32 *val)
86 if (pCapability->ConType == TWON_ONEVALUE)
87 return get_onevalue(pCapability, NULL, val);
89 FIXME("Partial Stub: MSG_SET only supports TW_ONEVALUE\n");
90 return TWCC_BADCAP;
94 static TW_UINT16 msg_get_enum(pTW_CAPABILITY pCapability, const TW_UINT32 *values, int value_count,
95 TW_UINT16 type, TW_UINT32 current, TW_UINT32 default_value)
97 TW_ENUMERATION *enumv = NULL;
98 TW_UINT32 *p32;
99 TW_UINT16 *p16;
100 int i;
102 pCapability->ConType = TWON_ENUMERATION;
103 pCapability->hContainer = 0;
105 if (type == TWTY_INT16 || type == TWTY_UINT16)
106 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION, ItemList[value_count * sizeof(TW_UINT16)]));
108 if (type == TWTY_INT32 || type == TWTY_UINT32)
109 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION, ItemList[value_count * sizeof(TW_UINT32)]));
111 if (pCapability->hContainer)
112 enumv = GlobalLock(pCapability->hContainer);
114 if (! enumv)
115 return TWCC_LOWMEMORY;
117 enumv->ItemType = type;
118 enumv->NumItems = value_count;
120 p16 = (TW_UINT16 *) enumv->ItemList;
121 p32 = (TW_UINT32 *) enumv->ItemList;
122 for (i = 0; i < value_count; i++)
124 if (values[i] == current)
125 enumv->CurrentIndex = i;
126 if (values[i] == default_value)
127 enumv->DefaultIndex = i;
128 if (type == TWTY_INT16 || type == TWTY_UINT16)
129 p16[i] = values[i];
130 if (type == TWTY_INT32 || type == TWTY_UINT32)
131 p32[i] = values[i];
134 GlobalUnlock(pCapability->hContainer);
135 return TWCC_SUCCESS;
138 #ifdef SONAME_LIBSANE
139 static TW_UINT16 msg_get_range(pTW_CAPABILITY pCapability, TW_UINT16 type,
140 TW_UINT32 minval, TW_UINT32 maxval, TW_UINT32 step, TW_UINT32 def, TW_UINT32 current)
142 TW_RANGE *range = NULL;
144 pCapability->ConType = TWON_RANGE;
145 pCapability->hContainer = 0;
147 pCapability->hContainer = GlobalAlloc (0, sizeof(*range));
148 if (pCapability->hContainer)
149 range = GlobalLock(pCapability->hContainer);
151 if (! range)
152 return TWCC_LOWMEMORY;
154 range->ItemType = type;
155 range->MinValue = minval;
156 range->MaxValue = maxval;
157 range->StepSize = step;
158 range->DefaultValue = def;
159 range->CurrentValue = current;
161 GlobalUnlock(pCapability->hContainer);
162 return TWCC_SUCCESS;
164 #endif
166 static TW_UINT16 TWAIN_GetSupportedCaps(pTW_CAPABILITY pCapability)
168 TW_ARRAY *a;
169 static const UINT16 supported_caps[] = { CAP_SUPPORTEDCAPS, CAP_XFERCOUNT, CAP_UICONTROLLABLE,
170 CAP_AUTOFEED, CAP_FEEDERENABLED,
171 ICAP_XFERMECH, ICAP_PIXELTYPE, ICAP_UNITS, ICAP_BITDEPTH, ICAP_COMPRESSION, ICAP_PIXELFLAVOR,
172 ICAP_XRESOLUTION, ICAP_YRESOLUTION, ICAP_PHYSICALHEIGHT, ICAP_PHYSICALWIDTH, ICAP_SUPPORTEDSIZES };
174 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ARRAY, ItemList[sizeof(supported_caps)] ));
175 pCapability->ConType = TWON_ARRAY;
177 if (pCapability->hContainer)
179 UINT16 *u;
180 int i;
181 a = GlobalLock (pCapability->hContainer);
182 a->ItemType = TWTY_UINT16;
183 a->NumItems = sizeof(supported_caps) / sizeof(supported_caps[0]);
184 u = (UINT16 *) a->ItemList;
185 for (i = 0; i < a->NumItems; i++)
186 u[i] = supported_caps[i];
187 GlobalUnlock (pCapability->hContainer);
188 return TWCC_SUCCESS;
190 else
191 return TWCC_LOWMEMORY;
195 /* ICAP_XFERMECH */
196 static TW_UINT16 SANE_ICAPXferMech (pTW_CAPABILITY pCapability, TW_UINT16 action)
198 static const TW_UINT32 possible_values[] = { TWSX_NATIVE, TWSX_MEMORY };
199 TW_UINT32 val;
200 TW_UINT16 twCC = TWCC_BADCAP;
202 TRACE("ICAP_XFERMECH\n");
204 switch (action)
206 case MSG_QUERYSUPPORT:
207 twCC = set_onevalue(pCapability, TWTY_INT32,
208 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
209 break;
211 case MSG_GET:
212 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
213 TWTY_UINT16, activeDS.capXferMech, TWSX_NATIVE);
214 break;
216 case MSG_SET:
217 twCC = msg_set(pCapability, &val);
218 if (twCC == TWCC_SUCCESS)
220 activeDS.capXferMech = (TW_UINT16) val;
221 FIXME("Partial Stub: XFERMECH set to %d, but ignored\n", val);
223 break;
225 case MSG_GETDEFAULT:
226 twCC = set_onevalue(pCapability, TWTY_UINT16, TWSX_NATIVE);
227 break;
229 case MSG_RESET:
230 activeDS.capXferMech = TWSX_NATIVE;
231 /* .. fall through intentional .. */
233 case MSG_GETCURRENT:
234 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.capXferMech);
235 FIXME("Partial Stub: XFERMECH of %d not actually used\n", activeDS.capXferMech);
236 break;
238 return twCC;
242 /* CAP_XFERCOUNT */
243 static TW_UINT16 SANE_CAPXferCount (pTW_CAPABILITY pCapability, TW_UINT16 action)
245 TW_UINT32 val;
246 TW_UINT16 twCC = TWCC_BADCAP;
248 TRACE("CAP_XFERCOUNT\n");
250 switch (action)
252 case MSG_QUERYSUPPORT:
253 twCC = set_onevalue(pCapability, TWTY_INT32,
254 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
255 break;
257 case MSG_GET:
258 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
259 FIXME("Partial Stub: Reporting only support for transfer all\n");
260 break;
262 case MSG_SET:
263 twCC = msg_set(pCapability, &val);
264 if (twCC == TWCC_SUCCESS)
265 FIXME("Partial Stub: XFERCOUNT set to %d, but ignored\n", val);
266 break;
268 case MSG_GETDEFAULT:
269 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
270 break;
272 case MSG_RESET:
273 /* .. fall through intentional .. */
275 case MSG_GETCURRENT:
276 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
277 break;
279 return twCC;
282 #ifdef SONAME_LIBSANE
283 static BOOL pixeltype_to_sane_mode(TW_UINT16 pixeltype, SANE_String mode, int len)
285 SANE_String_Const m = NULL;
286 switch (pixeltype)
288 case TWPT_GRAY:
289 m = SANE_VALUE_SCAN_MODE_GRAY;
290 break;
291 case TWPT_RGB:
292 m = SANE_VALUE_SCAN_MODE_COLOR;
293 break;
294 case TWPT_BW:
295 m = SANE_VALUE_SCAN_MODE_LINEART;
296 break;
298 if (! m)
299 return FALSE;
300 if (strlen(m) >= len)
301 return FALSE;
302 strcpy(mode, m);
303 return TRUE;
305 static BOOL sane_mode_to_pixeltype(SANE_String_Const mode, TW_UINT16 *pixeltype)
307 if (strcmp(mode, SANE_VALUE_SCAN_MODE_LINEART) == 0)
308 *pixeltype = TWPT_BW;
309 else if (memcmp(mode, SANE_VALUE_SCAN_MODE_GRAY, strlen(SANE_VALUE_SCAN_MODE_GRAY)) == 0)
310 *pixeltype = TWPT_GRAY;
311 else if (strcmp(mode, SANE_VALUE_SCAN_MODE_COLOR) == 0)
312 *pixeltype = TWPT_RGB;
313 else
314 return FALSE;
316 return TRUE;
318 #endif
320 /* ICAP_PIXELTYPE */
321 static TW_UINT16 SANE_ICAPPixelType (pTW_CAPABILITY pCapability, TW_UINT16 action)
323 TW_UINT16 twCC = TWCC_BADCAP;
324 #ifdef SONAME_LIBSANE
325 TW_UINT32 possible_values[3];
326 int possible_value_count;
327 TW_UINT32 val;
328 SANE_Status rc;
329 SANE_Int status;
330 SANE_String_Const *choices;
331 char current_mode[64];
332 TW_UINT16 current_pixeltype = TWPT_BW;
333 SANE_Char mode[64];
335 TRACE("ICAP_PIXELTYPE\n");
337 rc = sane_option_probe_mode(activeDS.deviceHandle, &choices, current_mode, sizeof(current_mode));
338 if (rc != SANE_STATUS_GOOD)
340 ERR("Unable to retrieve mode from sane, ICAP_PIXELTYPE unsupported\n");
341 return twCC;
344 sane_mode_to_pixeltype(current_mode, &current_pixeltype);
346 /* Sane does not support a concept of a default mode, so we simply cache
347 * the first mode we find */
348 if (! activeDS.PixelTypeSet)
350 activeDS.PixelTypeSet = TRUE;
351 activeDS.defaultPixelType = current_pixeltype;
354 switch (action)
356 case MSG_QUERYSUPPORT:
357 twCC = set_onevalue(pCapability, TWTY_INT32,
358 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
359 break;
361 case MSG_GET:
362 for (possible_value_count = 0; choices && *choices && possible_value_count < 3; choices++)
364 TW_UINT16 pix;
365 if (sane_mode_to_pixeltype(*choices, &pix))
366 possible_values[possible_value_count++] = pix;
368 twCC = msg_get_enum(pCapability, possible_values, possible_value_count,
369 TWTY_UINT16, current_pixeltype, activeDS.defaultPixelType);
370 break;
372 case MSG_SET:
373 twCC = msg_set(pCapability, &val);
374 if (twCC == TWCC_SUCCESS)
376 TRACE("Setting pixeltype to %d\n", val);
377 if (! pixeltype_to_sane_mode(val, mode, sizeof(mode)))
378 return TWCC_BADVALUE;
380 status = 0;
381 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
382 /* Some SANE devices use 'Grayscale' instead of the standard 'Gray' */
383 if (rc == SANE_STATUS_INVAL && strcmp(mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
385 strcpy(mode, "Grayscale");
386 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
388 if (rc != SANE_STATUS_GOOD)
389 return sane_status_to_twcc(rc);
390 if (status & SANE_INFO_RELOAD_PARAMS)
391 psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
393 break;
395 case MSG_GETDEFAULT:
396 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.defaultPixelType);
397 break;
399 case MSG_RESET:
400 current_pixeltype = activeDS.defaultPixelType;
401 if (! pixeltype_to_sane_mode(current_pixeltype, mode, sizeof(mode)))
402 return TWCC_BADVALUE;
404 status = 0;
405 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
406 /* Some SANE devices use 'Grayscale' instead of the standard 'Gray' */
407 if (rc == SANE_STATUS_INVAL && strcmp(mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
409 strcpy(mode, "Grayscale");
410 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
412 if (rc != SANE_STATUS_GOOD)
413 return sane_status_to_twcc(rc);
414 if (status & SANE_INFO_RELOAD_PARAMS)
415 psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
417 /* .. fall through intentional .. */
419 case MSG_GETCURRENT:
420 twCC = set_onevalue(pCapability, TWTY_UINT16, current_pixeltype);
421 TRACE("Returning current pixeltype of %d\n", current_pixeltype);
422 break;
425 #endif
426 return twCC;
429 /* ICAP_UNITS */
430 static TW_UINT16 SANE_ICAPUnits (pTW_CAPABILITY pCapability, TW_UINT16 action)
432 TW_UINT32 val;
433 TW_UINT16 twCC = TWCC_BADCAP;
435 TRACE("ICAP_UNITS\n");
437 switch (action)
439 case MSG_QUERYSUPPORT:
440 twCC = set_onevalue(pCapability, TWTY_INT32,
441 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
442 break;
444 case MSG_GET:
445 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
446 break;
448 case MSG_SET:
449 twCC = msg_set(pCapability, &val);
450 if (twCC == TWCC_SUCCESS)
452 if (val != TWUN_INCHES)
454 ERR("Sane supports only SANE_UNIT_DPI\n");
455 twCC = TWCC_BADVALUE;
458 break;
460 case MSG_GETDEFAULT:
461 case MSG_RESET:
462 /* .. fall through intentional .. */
464 case MSG_GETCURRENT:
465 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
466 break;
469 return twCC;
472 /* ICAP_BITDEPTH */
473 static TW_UINT16 SANE_ICAPBitDepth(pTW_CAPABILITY pCapability, TW_UINT16 action)
475 TW_UINT16 twCC = TWCC_BADCAP;
476 #ifdef SONAME_LIBSANE
477 TW_UINT32 possible_values[1];
479 TRACE("ICAP_BITDEPTH\n");
481 possible_values[0] = activeDS.sane_param.depth;
483 switch (action)
485 case MSG_QUERYSUPPORT:
486 twCC = set_onevalue(pCapability, TWTY_INT32,
487 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
488 break;
490 case MSG_GET:
491 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
492 TWTY_UINT16, activeDS.sane_param.depth, activeDS.sane_param.depth);
493 break;
495 case MSG_GETDEFAULT:
496 /* .. Fall through intentional .. */
498 case MSG_GETCURRENT:
499 TRACE("Returning current bitdepth of %d\n", activeDS.sane_param.depth);
500 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.sane_param.depth);
501 break;
503 #endif
504 return twCC;
507 /* CAP_UICONTROLLABLE */
508 static TW_UINT16 SANE_CAPUiControllable(pTW_CAPABILITY pCapability, TW_UINT16 action)
510 TW_UINT16 twCC = TWCC_BADCAP;
512 TRACE("CAP_UICONTROLLABLE\n");
514 switch (action)
516 case MSG_QUERYSUPPORT:
517 twCC = set_onevalue(pCapability, TWTY_INT32, TWQC_GET);
518 break;
520 case MSG_GET:
521 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
522 break;
525 return twCC;
528 /* ICAP_COMPRESSION */
529 static TW_UINT16 SANE_ICAPCompression (pTW_CAPABILITY pCapability, TW_UINT16 action)
531 static const TW_UINT32 possible_values[] = { TWCP_NONE };
532 TW_UINT32 val;
533 TW_UINT16 twCC = TWCC_BADCAP;
535 TRACE("ICAP_COMPRESSION\n");
537 switch (action)
539 case MSG_QUERYSUPPORT:
540 twCC = set_onevalue(pCapability, TWTY_INT32,
541 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
542 break;
544 case MSG_GET:
545 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
546 TWTY_UINT16, TWCP_NONE, TWCP_NONE);
547 FIXME("Partial stub: We don't attempt to support compression\n");
548 break;
550 case MSG_SET:
551 twCC = msg_set(pCapability, &val);
552 if (twCC == TWCC_SUCCESS)
553 FIXME("Partial Stub: COMPRESSION set to %d, but ignored\n", val);
554 break;
556 case MSG_GETDEFAULT:
557 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
558 break;
560 case MSG_RESET:
561 /* .. fall through intentional .. */
563 case MSG_GETCURRENT:
564 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
565 break;
567 return twCC;
570 /* ICAP_XRESOLUTION, ICAP_YRESOLUTION */
571 static TW_UINT16 SANE_ICAPResolution (pTW_CAPABILITY pCapability, TW_UINT16 action, TW_UINT16 cap)
573 TW_UINT16 twCC = TWCC_BADCAP;
574 #ifdef SONAME_LIBSANE
575 TW_UINT32 val;
576 SANE_Int current_resolution;
577 TW_FIX32 *default_res;
578 const char *best_option_name;
579 SANE_Int minval, maxval, quantval;
580 SANE_Status sane_rc;
581 SANE_Int set_status;
583 TRACE("ICAP_%cRESOLUTION\n", cap == ICAP_XRESOLUTION ? 'X' : 'Y');
585 /* Some scanners support 'x-resolution', most seem to just support 'resolution' */
586 if (cap == ICAP_XRESOLUTION)
588 best_option_name = "x-resolution";
589 default_res = &activeDS.defaultXResolution;
591 else
593 best_option_name = "y-resolution";
594 default_res = &activeDS.defaultYResolution;
596 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, &current_resolution) != SANE_STATUS_GOOD)
598 best_option_name = "resolution";
599 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, &current_resolution) != SANE_STATUS_GOOD)
600 return TWCC_BADCAP;
603 /* Sane does not support a concept of 'default' resolution, so we have to
604 * cache the resolution the very first time we load the scanner, and use that
605 * as the default */
606 if (cap == ICAP_XRESOLUTION && ! activeDS.XResolutionSet)
608 default_res->Whole = current_resolution;
609 default_res->Frac = 0;
610 activeDS.XResolutionSet = TRUE;
613 if (cap == ICAP_YRESOLUTION && ! activeDS.YResolutionSet)
615 default_res->Whole = current_resolution;
616 default_res->Frac = 0;
617 activeDS.YResolutionSet = TRUE;
620 switch (action)
622 case MSG_QUERYSUPPORT:
623 twCC = set_onevalue(pCapability, TWTY_INT32,
624 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
625 break;
627 case MSG_GET:
628 sane_rc = sane_option_probe_resolution(activeDS.deviceHandle, best_option_name, &minval, &maxval, &quantval);
629 if (sane_rc != SANE_STATUS_GOOD)
630 twCC = TWCC_BADCAP;
631 else
632 twCC = msg_get_range(pCapability, TWTY_FIX32,
633 minval, maxval, quantval == 0 ? 1 : quantval, default_res->Whole, current_resolution);
634 break;
636 case MSG_SET:
637 twCC = msg_set(pCapability, &val);
638 if (twCC == TWCC_SUCCESS)
640 TW_FIX32 f32;
641 memcpy(&f32, &val, sizeof(f32));
642 sane_rc = sane_option_set_int(activeDS.deviceHandle, best_option_name, f32.Whole, &set_status);
643 if (sane_rc != SANE_STATUS_GOOD)
645 FIXME("Status of %d not expected or handled\n", sane_rc);
646 twCC = TWCC_BADCAP;
648 else if (set_status == SANE_INFO_INEXACT)
649 twCC = TWCC_CHECKSTATUS;
651 break;
653 case MSG_GETDEFAULT:
654 twCC = set_onevalue(pCapability, TWTY_FIX32, default_res->Whole);
655 break;
657 case MSG_RESET:
658 sane_rc = sane_option_set_int(activeDS.deviceHandle, best_option_name, default_res->Whole, NULL);
659 if (sane_rc != SANE_STATUS_GOOD)
660 return TWCC_BADCAP;
662 /* .. fall through intentional .. */
664 case MSG_GETCURRENT:
665 twCC = set_onevalue(pCapability, TWTY_FIX32, current_resolution);
666 break;
668 #endif
669 return twCC;
672 /* ICAP_PHYSICALHEIGHT, ICAP_PHYSICALWIDTH */
673 static TW_UINT16 SANE_ICAPPhysical (pTW_CAPABILITY pCapability, TW_UINT16 action, TW_UINT16 cap)
675 TW_UINT16 twCC = TWCC_BADCAP;
676 #ifdef SONAME_LIBSANE
677 TW_FIX32 res;
678 char option_name[64];
679 SANE_Fixed lower, upper;
680 SANE_Unit lowerunit, upperunit;
681 SANE_Status status;
683 TRACE("ICAP_PHYSICAL%s\n", cap == ICAP_PHYSICALHEIGHT? "HEIGHT" : "WIDTH");
685 sprintf(option_name, "tl-%c", cap == ICAP_PHYSICALHEIGHT ? 'y' : 'x');
686 status = sane_option_probe_scan_area(activeDS.deviceHandle, option_name, NULL, &lowerunit, &lower, NULL, NULL);
687 if (status != SANE_STATUS_GOOD)
688 return sane_status_to_twcc(status);
690 sprintf(option_name, "br-%c", cap == ICAP_PHYSICALHEIGHT ? 'y' : 'x');
691 status = sane_option_probe_scan_area(activeDS.deviceHandle, option_name, NULL, &upperunit, NULL, &upper, NULL);
692 if (status != SANE_STATUS_GOOD)
693 return sane_status_to_twcc(status);
695 if (upperunit != lowerunit)
696 return TWCC_BADCAP;
698 if (! convert_sane_res_to_twain(SANE_UNFIX(upper) - SANE_UNFIX(lower), upperunit, &res, TWUN_INCHES))
699 return TWCC_BADCAP;
701 switch (action)
703 case MSG_QUERYSUPPORT:
704 twCC = set_onevalue(pCapability, TWTY_INT32,
705 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
706 break;
708 case MSG_GET:
709 case MSG_GETDEFAULT:
711 /* .. fall through intentional .. */
713 case MSG_GETCURRENT:
714 twCC = set_onevalue(pCapability, TWTY_FIX32, res.Whole | (res.Frac << 16));
715 break;
717 #endif
718 return twCC;
721 /* ICAP_PIXELFLAVOR */
722 static TW_UINT16 SANE_ICAPPixelFlavor (pTW_CAPABILITY pCapability, TW_UINT16 action)
724 TW_UINT16 twCC = TWCC_BADCAP;
725 #ifdef SONAME_LIBSANE
726 static const TW_UINT32 possible_values[] = { TWPF_CHOCOLATE, TWPF_VANILLA };
727 TW_UINT32 val;
728 TW_UINT32 flavor = activeDS.sane_param.depth == 1 ? TWPF_VANILLA : TWPF_CHOCOLATE;
730 TRACE("ICAP_PIXELFLAVOR\n");
732 switch (action)
734 case MSG_QUERYSUPPORT:
735 twCC = set_onevalue(pCapability, TWTY_INT32,
736 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
737 break;
739 case MSG_GET:
740 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
741 TWTY_UINT16, flavor, flavor);
742 break;
744 case MSG_SET:
745 twCC = msg_set(pCapability, &val);
746 if (twCC == TWCC_SUCCESS)
748 FIXME("Stub: PIXELFLAVOR set to %d, but ignored\n", val);
750 break;
752 case MSG_GETDEFAULT:
753 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
754 break;
756 case MSG_RESET:
757 /* .. fall through intentional .. */
759 case MSG_GETCURRENT:
760 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
761 break;
763 #endif
764 return twCC;
767 #ifdef SONAME_LIBSANE
768 static TW_UINT16 get_width_height(double *width, double *height, BOOL max)
770 SANE_Status status;
772 SANE_Fixed tlx_current, tlx_min, tlx_max;
773 SANE_Fixed tly_current, tly_min, tly_max;
774 SANE_Fixed brx_current, brx_min, brx_max;
775 SANE_Fixed bry_current, bry_min, bry_max;
777 status = sane_option_probe_scan_area(activeDS.deviceHandle, "tl-x", &tlx_current, NULL, &tlx_min, &tlx_max, NULL);
778 if (status != SANE_STATUS_GOOD)
779 return sane_status_to_twcc(status);
781 status = sane_option_probe_scan_area(activeDS.deviceHandle, "tl-y", &tly_current, NULL, &tly_min, &tly_max, NULL);
782 if (status != SANE_STATUS_GOOD)
783 return sane_status_to_twcc(status);
785 status = sane_option_probe_scan_area(activeDS.deviceHandle, "br-x", &brx_current, NULL, &brx_min, &brx_max, NULL);
786 if (status != SANE_STATUS_GOOD)
787 return sane_status_to_twcc(status);
789 status = sane_option_probe_scan_area(activeDS.deviceHandle, "br-y", &bry_current, NULL, &bry_min, &bry_max, NULL);
790 if (status != SANE_STATUS_GOOD)
791 return sane_status_to_twcc(status);
793 if (max)
794 *width = SANE_UNFIX(brx_max) - SANE_UNFIX(tlx_min);
795 else
796 *width = SANE_UNFIX(brx_current) - SANE_UNFIX(tlx_current);
798 if (max)
799 *height = SANE_UNFIX(bry_max) - SANE_UNFIX(tly_min);
800 else
801 *height = SANE_UNFIX(bry_current) - SANE_UNFIX(tly_current);
803 return(TWCC_SUCCESS);
806 static TW_UINT16 set_one_coord(const char *name, double coord)
808 SANE_Status status;
809 status = sane_option_set_fixed(activeDS.deviceHandle, name, SANE_FIX(coord), NULL);
810 if (status != SANE_STATUS_GOOD)
811 return sane_status_to_twcc(status);
812 return TWCC_SUCCESS;
815 static TW_UINT16 set_width_height(double width, double height)
817 TW_UINT16 rc = TWCC_SUCCESS;
818 rc = set_one_coord("tl-x", 0);
819 if (rc != TWCC_SUCCESS)
820 return rc;
821 rc = set_one_coord("br-x", width);
822 if (rc != TWCC_SUCCESS)
823 return rc;
824 rc = set_one_coord("tl-y", 0);
825 if (rc != TWCC_SUCCESS)
826 return rc;
827 rc = set_one_coord("br-y", height);
829 return rc;
832 typedef struct
834 TW_UINT32 size;
835 double x;
836 double y;
837 } supported_size_t;
839 static const supported_size_t supported_sizes[] =
841 { TWSS_NONE, 0, 0 },
842 { TWSS_A4, 210, 297 },
843 { TWSS_JISB5, 182, 257 },
844 { TWSS_USLETTER, 215.9, 279.4 },
845 { TWSS_USLEGAL, 215.9, 355.6 },
846 { TWSS_A5, 148, 210 },
847 { TWSS_B4, 250, 353 },
848 { TWSS_B6, 125, 176 },
849 { TWSS_USLEDGER, 215.9, 431.8 },
850 { TWSS_USEXECUTIVE, 184.15, 266.7 },
851 { TWSS_A3, 297, 420 },
853 #define SUPPORTED_SIZE_COUNT (sizeof(supported_sizes) / sizeof(supported_sizes[0]))
855 static TW_UINT16 get_default_paper_size(const supported_size_t *s, int n)
857 DWORD paper;
858 int rc;
859 int defsize = -1;
860 double width, height;
861 int i;
862 rc = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER, (void *) &paper, sizeof(paper));
863 if (rc > 0)
864 switch (paper)
866 case 1:
867 defsize = TWSS_USLETTER;
868 break;
869 case 5:
870 defsize = TWSS_USLEGAL;
871 break;
872 case 8:
873 defsize = TWSS_A3;
874 break;
875 case 9:
876 defsize = TWSS_A4;
877 break;
880 if (defsize == -1)
881 return TWSS_NONE;
883 if (get_width_height(&width, &height, TRUE) != TWCC_SUCCESS)
884 return TWSS_NONE;
886 for (i = 0; i < n; i++)
887 if (s[i].size == defsize)
889 /* Sane's use of integers to store floats is a hair lossy; deal with it */
890 if (s[i].x > (width + .01) || s[i].y > (height + 0.01))
891 return TWSS_NONE;
892 else
893 return s[i].size;
896 return TWSS_NONE;
899 static TW_UINT16 get_current_paper_size(const supported_size_t *s, int n)
901 int i;
902 double width, height;
903 double xdelta, ydelta;
905 if (get_width_height(&width, &height, FALSE) != TWCC_SUCCESS)
906 return TWSS_NONE;
908 for (i = 0; i < n; i++)
910 /* Sane's use of integers to store floats results
911 * in a very small error; cope with that */
912 xdelta = s[i].x - width;
913 ydelta = s[i].y - height;
914 if (xdelta < 0.01 && xdelta > -0.01 &&
915 ydelta < 0.01 && ydelta > -0.01)
916 return s[i].size;
919 return TWSS_NONE;
921 #endif
923 /* ICAP_SUPPORTEDSIZES */
924 static TW_UINT16 SANE_ICAPSupportedSizes (pTW_CAPABILITY pCapability, TW_UINT16 action)
926 TW_UINT16 twCC = TWCC_BADCAP;
927 #ifdef SONAME_LIBSANE
929 static TW_UINT32 possible_values[SUPPORTED_SIZE_COUNT];
930 int i;
931 TW_UINT32 val;
932 TW_UINT16 default_size = get_default_paper_size(supported_sizes, SUPPORTED_SIZE_COUNT);
933 TW_UINT16 current_size = get_current_paper_size(supported_sizes, SUPPORTED_SIZE_COUNT);
935 TRACE("ICAP_SUPPORTEDSIZES\n");
937 switch (action)
939 case MSG_QUERYSUPPORT:
940 twCC = set_onevalue(pCapability, TWTY_INT32,
941 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
942 break;
944 case MSG_GET:
945 for (i = 0; i < sizeof(supported_sizes) / sizeof(supported_sizes[0]); i++)
946 possible_values[i] = supported_sizes[i].size;
947 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
948 TWTY_UINT16, current_size, default_size);
949 WARN("Partial Stub: our supported size selection is a bit thin.\n");
950 break;
952 case MSG_SET:
953 twCC = msg_set(pCapability, &val);
954 if (twCC == TWCC_SUCCESS)
955 for (i = 1; i < SUPPORTED_SIZE_COUNT; i++)
956 if (supported_sizes[i].size == val)
957 return set_width_height(supported_sizes[i].x, supported_sizes[i].y);
959 ERR("Unsupported size %d\n", val);
960 twCC = TWCC_BADCAP;
961 break;
963 case MSG_GETDEFAULT:
964 twCC = set_onevalue(pCapability, TWTY_UINT16, default_size);
965 break;
967 case MSG_RESET:
968 twCC = TWCC_BADCAP;
969 for (i = 1; i < SUPPORTED_SIZE_COUNT; i++)
970 if (supported_sizes[i].size == default_size)
972 twCC = set_width_height(supported_sizes[i].x, supported_sizes[i].y);
973 break;
975 if (twCC != TWCC_SUCCESS)
976 return twCC;
978 /* .. fall through intentional .. */
980 case MSG_GETCURRENT:
981 twCC = set_onevalue(pCapability, TWTY_UINT16, current_size);
982 break;
985 #undef SUPPORTED_SIZE_COUNT
986 #endif
987 return twCC;
990 /* CAP_AUTOFEED */
991 static TW_UINT16 SANE_CAPAutofeed (pTW_CAPABILITY pCapability, TW_UINT16 action)
993 TW_UINT16 twCC = TWCC_BADCAP;
994 #ifdef SONAME_LIBSANE
995 TW_UINT32 val;
996 SANE_Bool autofeed;
997 SANE_Status status;
999 TRACE("CAP_AUTOFEED\n");
1001 if (sane_option_get_bool(activeDS.deviceHandle, "batch-scan", &autofeed, NULL) != SANE_STATUS_GOOD)
1002 return TWCC_BADCAP;
1004 switch (action)
1006 case MSG_QUERYSUPPORT:
1007 twCC = set_onevalue(pCapability, TWTY_INT32,
1008 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1009 break;
1011 case MSG_GET:
1012 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1013 break;
1015 case MSG_SET:
1016 twCC = msg_set(pCapability, &val);
1017 if (twCC == TWCC_SUCCESS)
1019 if (val)
1020 autofeed = SANE_TRUE;
1021 else
1022 autofeed = SANE_FALSE;
1024 status = sane_option_set_bool(activeDS.deviceHandle, "batch-scan", autofeed, NULL);
1025 if (status != SANE_STATUS_GOOD)
1027 ERR("Error %s: Could not set batch-scan to %d\n", psane_strstatus(status), autofeed);
1028 return sane_status_to_twcc(status);
1031 break;
1033 case MSG_GETDEFAULT:
1034 twCC = set_onevalue(pCapability, TWTY_BOOL, SANE_TRUE);
1035 break;
1037 case MSG_RESET:
1038 autofeed = SANE_TRUE;
1039 status = sane_option_set_bool(activeDS.deviceHandle, "batch-scan", autofeed, NULL);
1040 if (status != SANE_STATUS_GOOD)
1042 ERR("Error %s: Could not reset batch-scan to SANE_TRUE\n", psane_strstatus(status));
1043 return sane_status_to_twcc(status);
1045 /* .. fall through intentional .. */
1047 case MSG_GETCURRENT:
1048 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1049 break;
1051 #endif
1052 return twCC;
1055 /* CAP_FEEDERENABLED */
1056 static TW_UINT16 SANE_CAPFeederEnabled (pTW_CAPABILITY pCapability, TW_UINT16 action)
1058 TW_UINT16 twCC = TWCC_BADCAP;
1059 #ifdef SONAME_LIBSANE
1060 TW_UINT32 val;
1061 TW_BOOL enabled;
1062 SANE_Status status;
1063 SANE_Char source[64];
1065 TRACE("CAP_FEEDERENABLED\n");
1067 if (sane_option_get_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, sizeof(source), NULL) != SANE_STATUS_GOOD)
1068 return TWCC_BADCAP;
1070 if (strcmp(source, "Auto") == 0 || strcmp(source, "ADF") == 0)
1071 enabled = TRUE;
1072 else
1073 enabled = FALSE;
1075 switch (action)
1077 case MSG_QUERYSUPPORT:
1078 twCC = set_onevalue(pCapability, TWTY_INT32,
1079 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1080 break;
1082 case MSG_GET:
1083 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1084 break;
1086 case MSG_SET:
1087 twCC = msg_set(pCapability, &val);
1088 if (twCC == TWCC_SUCCESS)
1090 if (val)
1091 enabled = TRUE;
1092 else
1093 enabled = FALSE;
1095 strcpy(source, "ADF");
1096 status = sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL);
1097 if (status != SANE_STATUS_GOOD)
1099 strcpy(source, "Auto");
1100 status = sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL);
1102 if (status != SANE_STATUS_GOOD)
1104 ERR("Error %s: Could not set source to either ADF or Auto\n", psane_strstatus(status));
1105 return sane_status_to_twcc(status);
1108 break;
1110 case MSG_GETDEFAULT:
1111 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
1112 break;
1114 case MSG_RESET:
1115 strcpy(source, "Auto");
1116 if (sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL) == SANE_STATUS_GOOD)
1117 enabled = TRUE;
1118 twCC = TWCC_SUCCESS;
1119 /* .. fall through intentional .. */
1121 case MSG_GETCURRENT:
1122 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1123 break;
1125 #endif
1126 return twCC;
1131 TW_UINT16 SANE_SaneCapability (pTW_CAPABILITY pCapability, TW_UINT16 action)
1133 TW_UINT16 twCC = TWCC_CAPUNSUPPORTED;
1135 TRACE("capability=%d action=%d\n", pCapability->Cap, action);
1137 switch (pCapability->Cap)
1139 case CAP_SUPPORTEDCAPS:
1140 if (action == MSG_GET)
1141 twCC = TWAIN_GetSupportedCaps(pCapability);
1142 else
1143 twCC = TWCC_BADVALUE;
1144 break;
1146 case CAP_XFERCOUNT:
1147 twCC = SANE_CAPXferCount (pCapability, action);
1148 break;
1150 case CAP_UICONTROLLABLE:
1151 twCC = SANE_CAPUiControllable (pCapability, action);
1152 break;
1154 case CAP_AUTOFEED:
1155 twCC = SANE_CAPAutofeed (pCapability, action);
1156 break;
1158 case CAP_FEEDERENABLED:
1159 twCC = SANE_CAPFeederEnabled (pCapability, action);
1160 break;
1162 case ICAP_PIXELTYPE:
1163 twCC = SANE_ICAPPixelType (pCapability, action);
1164 break;
1166 case ICAP_UNITS:
1167 twCC = SANE_ICAPUnits (pCapability, action);
1168 break;
1170 case ICAP_BITDEPTH:
1171 twCC = SANE_ICAPBitDepth(pCapability, action);
1172 break;
1174 case ICAP_XFERMECH:
1175 twCC = SANE_ICAPXferMech (pCapability, action);
1176 break;
1178 case ICAP_PIXELFLAVOR:
1179 twCC = SANE_ICAPPixelFlavor (pCapability, action);
1180 break;
1182 case ICAP_COMPRESSION:
1183 twCC = SANE_ICAPCompression(pCapability, action);
1184 break;
1186 case ICAP_XRESOLUTION:
1187 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1188 break;
1190 case ICAP_YRESOLUTION:
1191 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1192 break;
1194 case ICAP_PHYSICALHEIGHT:
1195 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1196 break;
1198 case ICAP_PHYSICALWIDTH:
1199 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1200 break;
1202 case ICAP_SUPPORTEDSIZES:
1203 twCC = SANE_ICAPSupportedSizes (pCapability, action);
1204 break;
1206 case ICAP_PLANARCHUNKY:
1207 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1208 break;
1210 case ICAP_BITORDER:
1211 FIXME("ICAP_BITORDER not implemented\n");
1212 break;
1216 /* Twain specifies that you should return a 0 in response to QUERYSUPPORT,
1217 * even if you don't formally support the capability */
1218 if (twCC == TWCC_CAPUNSUPPORTED && action == MSG_QUERYSUPPORT)
1219 twCC = set_onevalue(pCapability, 0, TWTY_INT32);
1221 if (twCC == TWCC_CAPUNSUPPORTED)
1222 TRACE("capability 0x%x/action=%d being reported as unsupported\n", pCapability->Cap, action);
1224 return twCC;
1227 TW_UINT16 SANE_SaneSetDefaults (void)
1229 TW_CAPABILITY cap;
1231 memset(&cap, 0, sizeof(cap));
1232 cap.Cap = CAP_AUTOFEED;
1233 cap.ConType = TWON_DONTCARE16;
1235 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1236 GlobalFree(cap.hContainer);
1238 memset(&cap, 0, sizeof(cap));
1239 cap.Cap = CAP_FEEDERENABLED;
1240 cap.ConType = TWON_DONTCARE16;
1242 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1243 GlobalFree(cap.hContainer);
1245 memset(&cap, 0, sizeof(cap));
1246 cap.Cap = ICAP_SUPPORTEDSIZES;
1247 cap.ConType = TWON_DONTCARE16;
1249 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1250 GlobalFree(cap.hContainer);
1252 return TWCC_SUCCESS;