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
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(twain);
34 #ifndef SANE_VALUE_SCAN_MODE_COLOR
35 #define SANE_VALUE_SCAN_MODE_COLOR SANE_I18N("Color")
37 #ifndef SANE_VALUE_SCAN_MODE_GRAY
38 #define SANE_VALUE_SCAN_MODE_GRAY SANE_I18N("Gray")
40 #ifndef SANE_VALUE_SCAN_MODE_LINEART
41 #define SANE_VALUE_SCAN_MODE_LINEART SANE_I18N("Lineart")
44 static TW_UINT16 get_onevalue(pTW_CAPABILITY pCapability, TW_UINT16 *type, TW_UINT32 *value)
46 if (pCapability->hContainer)
48 pTW_ONEVALUE pVal = GlobalLock (pCapability->hContainer);
53 *type = pVal->ItemType;
54 GlobalUnlock (pCapability->hContainer);
62 static TW_UINT16 set_onevalue(pTW_CAPABILITY pCapability, TW_UINT16 type, TW_UINT32 value)
64 pCapability->hContainer = GlobalAlloc (0, sizeof(TW_ONEVALUE));
66 if (pCapability->hContainer)
68 pTW_ONEVALUE pVal = GlobalLock (pCapability->hContainer);
71 pCapability->ConType = TWON_ONEVALUE;
72 pVal->ItemType = type;
74 GlobalUnlock (pCapability->hContainer);
78 return TWCC_LOWMEMORY;
81 static TW_UINT16 msg_set(pTW_CAPABILITY pCapability, TW_UINT32 *val)
83 if (pCapability->ConType == TWON_ONEVALUE)
84 return get_onevalue(pCapability, NULL, val);
86 FIXME("Partial Stub: MSG_SET only supports TW_ONEVALUE\n");
91 static TW_UINT16 msg_get_enum(pTW_CAPABILITY pCapability, const TW_UINT32 *values, int value_count,
92 TW_UINT16 type, TW_UINT32 current, TW_UINT32 default_value)
94 TW_ENUMERATION *enumv = NULL;
99 pCapability->ConType = TWON_ENUMERATION;
100 pCapability->hContainer = 0;
102 if (type == TWTY_INT16 || type == TWTY_UINT16)
103 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION, ItemList[value_count * sizeof(TW_UINT16)]));
105 if (type == TWTY_INT32 || type == TWTY_UINT32)
106 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION, ItemList[value_count * sizeof(TW_UINT32)]));
108 if (pCapability->hContainer)
109 enumv = GlobalLock(pCapability->hContainer);
112 return TWCC_LOWMEMORY;
114 enumv->ItemType = type;
115 enumv->NumItems = value_count;
117 p16 = (TW_UINT16 *) enumv->ItemList;
118 p32 = (TW_UINT32 *) enumv->ItemList;
119 for (i = 0; i < value_count; i++)
121 if (values[i] == current)
122 enumv->CurrentIndex = i;
123 if (values[i] == default_value)
124 enumv->DefaultIndex = i;
125 if (type == TWTY_INT16 || type == TWTY_UINT16)
127 if (type == TWTY_INT32 || type == TWTY_UINT32)
131 GlobalUnlock(pCapability->hContainer);
135 #ifdef SONAME_LIBSANE
136 static TW_UINT16 msg_get_range(pTW_CAPABILITY pCapability, TW_UINT16 type,
137 TW_UINT32 minval, TW_UINT32 maxval, TW_UINT32 step, TW_UINT32 def, TW_UINT32 current)
139 TW_RANGE *range = NULL;
141 pCapability->ConType = TWON_RANGE;
142 pCapability->hContainer = 0;
144 pCapability->hContainer = GlobalAlloc (0, sizeof(*range));
145 if (pCapability->hContainer)
146 range = GlobalLock(pCapability->hContainer);
149 return TWCC_LOWMEMORY;
151 range->ItemType = type;
152 range->MinValue = minval;
153 range->MaxValue = maxval;
154 range->StepSize = step;
155 range->DefaultValue = def;
156 range->CurrentValue = current;
158 GlobalUnlock(pCapability->hContainer);
163 static TW_UINT16 TWAIN_GetSupportedCaps(pTW_CAPABILITY pCapability)
166 static const UINT16 supported_caps[] = { CAP_SUPPORTEDCAPS, CAP_XFERCOUNT, CAP_UICONTROLLABLE,
167 CAP_AUTOFEED, CAP_FEEDERENABLED,
168 ICAP_XFERMECH, ICAP_PIXELTYPE, ICAP_UNITS, ICAP_BITDEPTH, ICAP_COMPRESSION, ICAP_PIXELFLAVOR,
169 ICAP_XRESOLUTION, ICAP_YRESOLUTION, ICAP_PHYSICALHEIGHT, ICAP_PHYSICALWIDTH, ICAP_SUPPORTEDSIZES };
171 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ARRAY, ItemList[sizeof(supported_caps)] ));
172 pCapability->ConType = TWON_ARRAY;
174 if (pCapability->hContainer)
178 a = GlobalLock (pCapability->hContainer);
179 a->ItemType = TWTY_UINT16;
180 a->NumItems = sizeof(supported_caps) / sizeof(supported_caps[0]);
181 u = (UINT16 *) a->ItemList;
182 for (i = 0; i < a->NumItems; i++)
183 u[i] = supported_caps[i];
184 GlobalUnlock (pCapability->hContainer);
188 return TWCC_LOWMEMORY;
193 static TW_UINT16 SANE_ICAPXferMech (pTW_CAPABILITY pCapability, TW_UINT16 action)
195 static const TW_UINT32 possible_values[] = { TWSX_NATIVE, TWSX_MEMORY };
197 TW_UINT16 twCC = TWCC_BADCAP;
199 TRACE("ICAP_XFERMECH\n");
203 case MSG_QUERYSUPPORT:
204 twCC = set_onevalue(pCapability, TWTY_INT32,
205 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
209 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
210 TWTY_UINT16, activeDS.capXferMech, TWSX_NATIVE);
214 twCC = msg_set(pCapability, &val);
215 if (twCC == TWCC_SUCCESS)
217 activeDS.capXferMech = (TW_UINT16) val;
218 FIXME("Partial Stub: XFERMECH set to %d, but ignored\n", val);
223 twCC = set_onevalue(pCapability, TWTY_UINT16, TWSX_NATIVE);
227 activeDS.capXferMech = TWSX_NATIVE;
228 /* .. fall through intentional .. */
231 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.capXferMech);
232 FIXME("Partial Stub: XFERMECH of %d not actually used\n", activeDS.capXferMech);
240 static TW_UINT16 SANE_CAPXferCount (pTW_CAPABILITY pCapability, TW_UINT16 action)
243 TW_UINT16 twCC = TWCC_BADCAP;
245 TRACE("CAP_XFERCOUNT\n");
249 case MSG_QUERYSUPPORT:
250 twCC = set_onevalue(pCapability, TWTY_INT32,
251 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
255 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
256 FIXME("Partial Stub: Reporting only support for transfer all\n");
260 twCC = msg_set(pCapability, &val);
261 if (twCC == TWCC_SUCCESS)
262 FIXME("Partial Stub: XFERCOUNT set to %d, but ignored\n", val);
266 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
270 /* .. fall through intentional .. */
273 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
279 #ifdef SONAME_LIBSANE
280 static BOOL pixeltype_to_sane_mode(TW_UINT16 pixeltype, SANE_String mode, int len)
282 SANE_String_Const m = NULL;
286 m = SANE_VALUE_SCAN_MODE_GRAY;
289 m = SANE_VALUE_SCAN_MODE_COLOR;
292 m = SANE_VALUE_SCAN_MODE_LINEART;
297 if (strlen(m) >= len)
302 static BOOL sane_mode_to_pixeltype(SANE_String_Const mode, TW_UINT16 *pixeltype)
304 if (strcmp(mode, SANE_VALUE_SCAN_MODE_LINEART) == 0)
305 *pixeltype = TWPT_BW;
306 else if (memcmp(mode, SANE_VALUE_SCAN_MODE_GRAY, strlen(SANE_VALUE_SCAN_MODE_GRAY)) == 0)
307 *pixeltype = TWPT_GRAY;
308 else if (strcmp(mode, SANE_VALUE_SCAN_MODE_COLOR) == 0)
309 *pixeltype = TWPT_RGB;
318 static TW_UINT16 SANE_ICAPPixelType (pTW_CAPABILITY pCapability, TW_UINT16 action)
320 TW_UINT16 twCC = TWCC_BADCAP;
321 #ifdef SONAME_LIBSANE
322 TW_UINT32 possible_values[3];
323 int possible_value_count;
327 SANE_String_Const *choices;
328 char current_mode[64];
329 TW_UINT16 current_pixeltype = TWPT_BW;
332 TRACE("ICAP_PIXELTYPE\n");
334 rc = sane_option_probe_mode(activeDS.deviceHandle, &choices, current_mode, sizeof(current_mode));
335 if (rc != SANE_STATUS_GOOD)
337 ERR("Unable to retrieve mode from sane, ICAP_PIXELTYPE unsupported\n");
341 sane_mode_to_pixeltype(current_mode, ¤t_pixeltype);
343 /* Sane does not support a concept of a default mode, so we simply cache
344 * the first mode we find */
345 if (! activeDS.PixelTypeSet)
347 activeDS.PixelTypeSet = TRUE;
348 activeDS.defaultPixelType = current_pixeltype;
353 case MSG_QUERYSUPPORT:
354 twCC = set_onevalue(pCapability, TWTY_INT32,
355 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
359 for (possible_value_count = 0; choices && *choices && possible_value_count < 3; choices++)
362 if (sane_mode_to_pixeltype(*choices, &pix))
363 possible_values[possible_value_count++] = pix;
365 twCC = msg_get_enum(pCapability, possible_values, possible_value_count,
366 TWTY_UINT16, current_pixeltype, activeDS.defaultPixelType);
370 twCC = msg_set(pCapability, &val);
371 if (twCC == TWCC_SUCCESS)
373 TRACE("Setting pixeltype to %d\n", val);
374 if (! pixeltype_to_sane_mode(val, mode, sizeof(mode)))
375 return TWCC_BADVALUE;
378 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
379 /* Some SANE devices use 'Grayscale' instead of the standard 'Gray' */
380 if (rc == SANE_STATUS_INVAL && strcmp(mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
382 strcpy(mode, "Grayscale");
383 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
385 if (rc != SANE_STATUS_GOOD)
386 return sane_status_to_twcc(rc);
387 if (status & SANE_INFO_RELOAD_PARAMS)
388 psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
393 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.defaultPixelType);
397 current_pixeltype = activeDS.defaultPixelType;
398 if (! pixeltype_to_sane_mode(current_pixeltype, mode, sizeof(mode)))
399 return TWCC_BADVALUE;
402 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
403 /* Some SANE devices use 'Grayscale' instead of the standard 'Gray' */
404 if (rc == SANE_STATUS_INVAL && strcmp(mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
406 strcpy(mode, "Grayscale");
407 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
409 if (rc != SANE_STATUS_GOOD)
410 return sane_status_to_twcc(rc);
411 if (status & SANE_INFO_RELOAD_PARAMS)
412 psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
414 /* .. fall through intentional .. */
417 twCC = set_onevalue(pCapability, TWTY_UINT16, current_pixeltype);
418 TRACE("Returning current pixeltype of %d\n", current_pixeltype);
427 static TW_UINT16 SANE_ICAPUnits (pTW_CAPABILITY pCapability, TW_UINT16 action)
430 TW_UINT16 twCC = TWCC_BADCAP;
432 TRACE("ICAP_UNITS\n");
436 case MSG_QUERYSUPPORT:
437 twCC = set_onevalue(pCapability, TWTY_INT32,
438 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
442 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
446 twCC = msg_set(pCapability, &val);
447 if (twCC == TWCC_SUCCESS)
449 if (val != TWUN_INCHES)
451 ERR("Sane supports only SANE_UNIT_DPI\n");
452 twCC = TWCC_BADVALUE;
459 /* .. fall through intentional .. */
462 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
470 static TW_UINT16 SANE_ICAPBitDepth(pTW_CAPABILITY pCapability, TW_UINT16 action)
472 TW_UINT16 twCC = TWCC_BADCAP;
473 #ifdef SONAME_LIBSANE
474 TW_UINT32 possible_values[1];
476 TRACE("ICAP_BITDEPTH\n");
478 possible_values[0] = activeDS.sane_param.depth;
482 case MSG_QUERYSUPPORT:
483 twCC = set_onevalue(pCapability, TWTY_INT32,
484 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
488 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
489 TWTY_UINT16, activeDS.sane_param.depth, activeDS.sane_param.depth);
493 /* .. Fall through intentional .. */
496 TRACE("Returning current bitdepth of %d\n", activeDS.sane_param.depth);
497 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.sane_param.depth);
504 /* CAP_UICONTROLLABLE */
505 static TW_UINT16 SANE_CAPUiControllable(pTW_CAPABILITY pCapability, TW_UINT16 action)
507 TW_UINT16 twCC = TWCC_BADCAP;
509 TRACE("CAP_UICONTROLLABLE\n");
513 case MSG_QUERYSUPPORT:
514 twCC = set_onevalue(pCapability, TWTY_INT32, TWQC_GET);
518 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
525 /* ICAP_COMPRESSION */
526 static TW_UINT16 SANE_ICAPCompression (pTW_CAPABILITY pCapability, TW_UINT16 action)
528 static const TW_UINT32 possible_values[] = { TWCP_NONE };
530 TW_UINT16 twCC = TWCC_BADCAP;
532 TRACE("ICAP_COMPRESSION\n");
536 case MSG_QUERYSUPPORT:
537 twCC = set_onevalue(pCapability, TWTY_INT32,
538 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
542 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
543 TWTY_UINT16, TWCP_NONE, TWCP_NONE);
544 FIXME("Partial stub: We don't attempt to support compression\n");
548 twCC = msg_set(pCapability, &val);
549 if (twCC == TWCC_SUCCESS)
550 FIXME("Partial Stub: COMPRESSION set to %d, but ignored\n", val);
554 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
558 /* .. fall through intentional .. */
561 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
567 /* ICAP_XRESOLUTION, ICAP_YRESOLUTION */
568 static TW_UINT16 SANE_ICAPResolution (pTW_CAPABILITY pCapability, TW_UINT16 action, TW_UINT16 cap)
570 TW_UINT16 twCC = TWCC_BADCAP;
571 #ifdef SONAME_LIBSANE
573 SANE_Int current_resolution;
574 TW_FIX32 *default_res;
575 const char *best_option_name;
576 SANE_Int minval, maxval, quantval;
580 TRACE("ICAP_%cRESOLUTION\n", cap == ICAP_XRESOLUTION ? 'X' : 'Y');
582 /* Some scanners support 'x-resolution', most seem to just support 'resolution' */
583 if (cap == ICAP_XRESOLUTION)
585 best_option_name = "x-resolution";
586 default_res = &activeDS.defaultXResolution;
590 best_option_name = "y-resolution";
591 default_res = &activeDS.defaultYResolution;
593 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, ¤t_resolution) != SANE_STATUS_GOOD)
595 best_option_name = "resolution";
596 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, ¤t_resolution) != SANE_STATUS_GOOD)
600 /* Sane does not support a concept of 'default' resolution, so we have to
601 * cache the resolution the very first time we load the scanner, and use that
603 if (cap == ICAP_XRESOLUTION && ! activeDS.XResolutionSet)
605 default_res->Whole = current_resolution;
606 default_res->Frac = 0;
607 activeDS.XResolutionSet = TRUE;
610 if (cap == ICAP_YRESOLUTION && ! activeDS.YResolutionSet)
612 default_res->Whole = current_resolution;
613 default_res->Frac = 0;
614 activeDS.YResolutionSet = TRUE;
619 case MSG_QUERYSUPPORT:
620 twCC = set_onevalue(pCapability, TWTY_INT32,
621 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
625 sane_rc = sane_option_probe_resolution(activeDS.deviceHandle, best_option_name, &minval, &maxval, &quantval);
626 if (sane_rc != SANE_STATUS_GOOD)
629 twCC = msg_get_range(pCapability, TWTY_FIX32,
630 minval, maxval, quantval == 0 ? 1 : quantval, default_res->Whole, current_resolution);
634 twCC = msg_set(pCapability, &val);
635 if (twCC == TWCC_SUCCESS)
638 memcpy(&f32, &val, sizeof(f32));
639 sane_rc = sane_option_set_int(activeDS.deviceHandle, best_option_name, f32.Whole, &set_status);
640 if (sane_rc != SANE_STATUS_GOOD)
642 FIXME("Status of %d not expected or handled\n", sane_rc);
645 else if (set_status == SANE_INFO_INEXACT)
646 twCC = TWCC_CHECKSTATUS;
651 twCC = set_onevalue(pCapability, TWTY_FIX32, default_res->Whole);
655 sane_rc = sane_option_set_int(activeDS.deviceHandle, best_option_name, default_res->Whole, NULL);
656 if (sane_rc != SANE_STATUS_GOOD)
659 /* .. fall through intentional .. */
662 twCC = set_onevalue(pCapability, TWTY_FIX32, current_resolution);
669 /* ICAP_PHYSICALHEIGHT, ICAP_PHYSICALWIDTH */
670 static TW_UINT16 SANE_ICAPPhysical (pTW_CAPABILITY pCapability, TW_UINT16 action, TW_UINT16 cap)
672 TW_UINT16 twCC = TWCC_BADCAP;
673 #ifdef SONAME_LIBSANE
675 char option_name[64];
676 SANE_Fixed lower, upper;
677 SANE_Unit lowerunit, upperunit;
680 TRACE("ICAP_PHYSICAL%s\n", cap == ICAP_PHYSICALHEIGHT? "HEIGHT" : "WIDTH");
682 sprintf(option_name, "tl-%c", cap == ICAP_PHYSICALHEIGHT ? 'y' : 'x');
683 status = sane_option_probe_scan_area(activeDS.deviceHandle, option_name, NULL, &lowerunit, &lower, NULL, NULL);
684 if (status != SANE_STATUS_GOOD)
685 return sane_status_to_twcc(status);
687 sprintf(option_name, "br-%c", cap == ICAP_PHYSICALHEIGHT ? 'y' : 'x');
688 status = sane_option_probe_scan_area(activeDS.deviceHandle, option_name, NULL, &upperunit, NULL, &upper, NULL);
689 if (status != SANE_STATUS_GOOD)
690 return sane_status_to_twcc(status);
692 if (upperunit != lowerunit)
695 if (! convert_sane_res_to_twain(SANE_UNFIX(upper) - SANE_UNFIX(lower), upperunit, &res, TWUN_INCHES))
700 case MSG_QUERYSUPPORT:
701 twCC = set_onevalue(pCapability, TWTY_INT32,
702 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
708 /* .. fall through intentional .. */
711 twCC = set_onevalue(pCapability, TWTY_FIX32, res.Whole | (res.Frac << 16));
718 /* ICAP_PIXELFLAVOR */
719 static TW_UINT16 SANE_ICAPPixelFlavor (pTW_CAPABILITY pCapability, TW_UINT16 action)
721 TW_UINT16 twCC = TWCC_BADCAP;
722 #ifdef SONAME_LIBSANE
723 static const TW_UINT32 possible_values[] = { TWPF_CHOCOLATE, TWPF_VANILLA };
725 TW_UINT32 flavor = activeDS.sane_param.depth == 1 ? TWPF_VANILLA : TWPF_CHOCOLATE;
727 TRACE("ICAP_PIXELFLAVOR\n");
731 case MSG_QUERYSUPPORT:
732 twCC = set_onevalue(pCapability, TWTY_INT32,
733 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
737 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
738 TWTY_UINT16, flavor, flavor);
742 twCC = msg_set(pCapability, &val);
743 if (twCC == TWCC_SUCCESS)
745 FIXME("Stub: PIXELFLAVOR set to %d, but ignored\n", val);
750 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
754 /* .. fall through intentional .. */
757 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
764 #ifdef SONAME_LIBSANE
765 static TW_UINT16 get_width_height(double *width, double *height, BOOL max)
769 SANE_Fixed tlx_current, tlx_min, tlx_max;
770 SANE_Fixed tly_current, tly_min, tly_max;
771 SANE_Fixed brx_current, brx_min, brx_max;
772 SANE_Fixed bry_current, bry_min, bry_max;
774 status = sane_option_probe_scan_area(activeDS.deviceHandle, "tl-x", &tlx_current, NULL, &tlx_min, &tlx_max, NULL);
775 if (status != SANE_STATUS_GOOD)
776 return sane_status_to_twcc(status);
778 status = sane_option_probe_scan_area(activeDS.deviceHandle, "tl-y", &tly_current, NULL, &tly_min, &tly_max, NULL);
779 if (status != SANE_STATUS_GOOD)
780 return sane_status_to_twcc(status);
782 status = sane_option_probe_scan_area(activeDS.deviceHandle, "br-x", &brx_current, NULL, &brx_min, &brx_max, NULL);
783 if (status != SANE_STATUS_GOOD)
784 return sane_status_to_twcc(status);
786 status = sane_option_probe_scan_area(activeDS.deviceHandle, "br-y", &bry_current, NULL, &bry_min, &bry_max, NULL);
787 if (status != SANE_STATUS_GOOD)
788 return sane_status_to_twcc(status);
791 *width = SANE_UNFIX(brx_max) - SANE_UNFIX(tlx_min);
793 *width = SANE_UNFIX(brx_current) - SANE_UNFIX(tlx_current);
796 *height = SANE_UNFIX(bry_max) - SANE_UNFIX(tly_min);
798 *height = SANE_UNFIX(bry_current) - SANE_UNFIX(tly_current);
800 return(TWCC_SUCCESS);
803 static TW_UINT16 set_one_coord(const char *name, double coord)
806 status = sane_option_set_fixed(activeDS.deviceHandle, name, SANE_FIX(coord), NULL);
807 if (status != SANE_STATUS_GOOD)
808 return sane_status_to_twcc(status);
812 static TW_UINT16 set_width_height(double width, double height)
814 TW_UINT16 rc = TWCC_SUCCESS;
815 rc = set_one_coord("tl-x", 0);
816 if (rc != TWCC_SUCCESS)
818 rc = set_one_coord("br-x", width);
819 if (rc != TWCC_SUCCESS)
821 rc = set_one_coord("tl-y", 0);
822 if (rc != TWCC_SUCCESS)
824 rc = set_one_coord("br-y", height);
836 static const supported_size_t supported_sizes[] =
839 { TWSS_A4, 210, 297 },
840 { TWSS_JISB5, 182, 257 },
841 { TWSS_USLETTER, 215.9, 279.4 },
842 { TWSS_USLEGAL, 215.9, 355.6 },
843 { TWSS_A5, 148, 210 },
844 { TWSS_B4, 250, 353 },
845 { TWSS_B6, 125, 176 },
846 { TWSS_USLEDGER, 215.9, 431.8 },
847 { TWSS_USEXECUTIVE, 184.15, 266.7 },
848 { TWSS_A3, 297, 420 },
850 #define SUPPORTED_SIZE_COUNT (sizeof(supported_sizes) / sizeof(supported_sizes[0]))
852 static TW_UINT16 get_default_paper_size(const supported_size_t *s, int n)
857 double width, height;
859 rc = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER, (void *) &paper, sizeof(paper));
864 defsize = TWSS_USLETTER;
867 defsize = TWSS_USLEGAL;
880 if (get_width_height(&width, &height, TRUE) != TWCC_SUCCESS)
883 for (i = 0; i < n; i++)
884 if (s[i].size == defsize)
886 /* Sane's use of integers to store floats is a hair lossy; deal with it */
887 if (s[i].x > (width + .01) || s[i].y > (height + 0.01))
896 static TW_UINT16 get_current_paper_size(const supported_size_t *s, int n)
899 double width, height;
900 double xdelta, ydelta;
902 if (get_width_height(&width, &height, FALSE) != TWCC_SUCCESS)
905 for (i = 0; i < n; i++)
907 /* Sane's use of integers to store floats results
908 * in a very small error; cope with that */
909 xdelta = s[i].x - width;
910 ydelta = s[i].y - height;
911 if (xdelta < 0.01 && xdelta > -0.01 &&
912 ydelta < 0.01 && ydelta > -0.01)
920 /* ICAP_SUPPORTEDSIZES */
921 static TW_UINT16 SANE_ICAPSupportedSizes (pTW_CAPABILITY pCapability, TW_UINT16 action)
923 TW_UINT16 twCC = TWCC_BADCAP;
924 #ifdef SONAME_LIBSANE
926 static TW_UINT32 possible_values[SUPPORTED_SIZE_COUNT];
929 TW_UINT16 default_size = get_default_paper_size(supported_sizes, SUPPORTED_SIZE_COUNT);
930 TW_UINT16 current_size = get_current_paper_size(supported_sizes, SUPPORTED_SIZE_COUNT);
932 TRACE("ICAP_SUPPORTEDSIZES\n");
936 case MSG_QUERYSUPPORT:
937 twCC = set_onevalue(pCapability, TWTY_INT32,
938 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
942 for (i = 0; i < sizeof(supported_sizes) / sizeof(supported_sizes[0]); i++)
943 possible_values[i] = supported_sizes[i].size;
944 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
945 TWTY_UINT16, current_size, default_size);
946 WARN("Partial Stub: our supported size selection is a bit thin.\n");
950 twCC = msg_set(pCapability, &val);
951 if (twCC == TWCC_SUCCESS)
952 for (i = 1; i < SUPPORTED_SIZE_COUNT; i++)
953 if (supported_sizes[i].size == val)
954 return set_width_height(supported_sizes[i].x, supported_sizes[i].y);
956 ERR("Unsupported size %d\n", val);
961 twCC = set_onevalue(pCapability, TWTY_UINT16, default_size);
966 for (i = 1; i < SUPPORTED_SIZE_COUNT; i++)
967 if (supported_sizes[i].size == default_size)
969 twCC = set_width_height(supported_sizes[i].x, supported_sizes[i].y);
972 if (twCC != TWCC_SUCCESS)
975 /* .. fall through intentional .. */
978 twCC = set_onevalue(pCapability, TWTY_UINT16, current_size);
982 #undef SUPPORTED_SIZE_COUNT
988 static TW_UINT16 SANE_CAPAutofeed (pTW_CAPABILITY pCapability, TW_UINT16 action)
990 TW_UINT16 twCC = TWCC_BADCAP;
991 #ifdef SONAME_LIBSANE
996 TRACE("CAP_AUTOFEED\n");
998 if (sane_option_get_bool(activeDS.deviceHandle, "batch-scan", &autofeed, NULL) != SANE_STATUS_GOOD)
1003 case MSG_QUERYSUPPORT:
1004 twCC = set_onevalue(pCapability, TWTY_INT32,
1005 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1009 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1013 twCC = msg_set(pCapability, &val);
1014 if (twCC == TWCC_SUCCESS)
1017 autofeed = SANE_TRUE;
1019 autofeed = SANE_FALSE;
1021 status = sane_option_set_bool(activeDS.deviceHandle, "batch-scan", autofeed, NULL);
1022 if (status != SANE_STATUS_GOOD)
1024 ERR("Error %s: Could not set batch-scan to %d\n", psane_strstatus(status), autofeed);
1025 return sane_status_to_twcc(status);
1030 case MSG_GETDEFAULT:
1031 twCC = set_onevalue(pCapability, TWTY_BOOL, SANE_TRUE);
1035 autofeed = SANE_TRUE;
1036 status = sane_option_set_bool(activeDS.deviceHandle, "batch-scan", autofeed, NULL);
1037 if (status != SANE_STATUS_GOOD)
1039 ERR("Error %s: Could not reset batch-scan to SANE_TRUE\n", psane_strstatus(status));
1040 return sane_status_to_twcc(status);
1042 /* .. fall through intentional .. */
1044 case MSG_GETCURRENT:
1045 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1052 /* CAP_FEEDERENABLED */
1053 static TW_UINT16 SANE_CAPFeederEnabled (pTW_CAPABILITY pCapability, TW_UINT16 action)
1055 TW_UINT16 twCC = TWCC_BADCAP;
1056 #ifdef SONAME_LIBSANE
1060 SANE_Char source[64];
1062 TRACE("CAP_FEEDERENABLED\n");
1064 if (sane_option_get_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, sizeof(source), NULL) != SANE_STATUS_GOOD)
1067 if (strcmp(source, "Auto") == 0 || strcmp(source, "ADF") == 0)
1074 case MSG_QUERYSUPPORT:
1075 twCC = set_onevalue(pCapability, TWTY_INT32,
1076 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1080 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1084 twCC = msg_set(pCapability, &val);
1085 if (twCC == TWCC_SUCCESS)
1092 strcpy(source, "ADF");
1093 status = sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL);
1094 if (status != SANE_STATUS_GOOD)
1096 strcpy(source, "Auto");
1097 status = sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL);
1099 if (status != SANE_STATUS_GOOD)
1101 ERR("Error %s: Could not set source to either ADF or Auto\n", psane_strstatus(status));
1102 return sane_status_to_twcc(status);
1107 case MSG_GETDEFAULT:
1108 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
1112 strcpy(source, "Auto");
1113 if (sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL) == SANE_STATUS_GOOD)
1115 twCC = TWCC_SUCCESS;
1116 /* .. fall through intentional .. */
1118 case MSG_GETCURRENT:
1119 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1128 TW_UINT16 SANE_SaneCapability (pTW_CAPABILITY pCapability, TW_UINT16 action)
1130 TW_UINT16 twCC = TWCC_CAPUNSUPPORTED;
1132 TRACE("capability=%d action=%d\n", pCapability->Cap, action);
1134 switch (pCapability->Cap)
1136 case CAP_SUPPORTEDCAPS:
1137 if (action == MSG_GET)
1138 twCC = TWAIN_GetSupportedCaps(pCapability);
1140 twCC = TWCC_BADVALUE;
1144 twCC = SANE_CAPXferCount (pCapability, action);
1147 case CAP_UICONTROLLABLE:
1148 twCC = SANE_CAPUiControllable (pCapability, action);
1152 twCC = SANE_CAPAutofeed (pCapability, action);
1155 case CAP_FEEDERENABLED:
1156 twCC = SANE_CAPFeederEnabled (pCapability, action);
1159 case ICAP_PIXELTYPE:
1160 twCC = SANE_ICAPPixelType (pCapability, action);
1164 twCC = SANE_ICAPUnits (pCapability, action);
1168 twCC = SANE_ICAPBitDepth(pCapability, action);
1172 twCC = SANE_ICAPXferMech (pCapability, action);
1175 case ICAP_PIXELFLAVOR:
1176 twCC = SANE_ICAPPixelFlavor (pCapability, action);
1179 case ICAP_COMPRESSION:
1180 twCC = SANE_ICAPCompression(pCapability, action);
1183 case ICAP_XRESOLUTION:
1184 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1187 case ICAP_YRESOLUTION:
1188 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1191 case ICAP_PHYSICALHEIGHT:
1192 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1195 case ICAP_PHYSICALWIDTH:
1196 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1199 case ICAP_SUPPORTEDSIZES:
1200 twCC = SANE_ICAPSupportedSizes (pCapability, action);
1203 case ICAP_PLANARCHUNKY:
1204 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1208 FIXME("ICAP_BITORDER not implemented\n");
1213 /* Twain specifies that you should return a 0 in response to QUERYSUPPORT,
1214 * even if you don't formally support the capability */
1215 if (twCC == TWCC_CAPUNSUPPORTED && action == MSG_QUERYSUPPORT)
1216 twCC = set_onevalue(pCapability, 0, TWTY_INT32);
1218 if (twCC == TWCC_CAPUNSUPPORTED)
1219 TRACE("capability 0x%x/action=%d being reported as unsupported\n", pCapability->Cap, action);
1224 TW_UINT16 SANE_SaneSetDefaults (void)
1228 memset(&cap, 0, sizeof(cap));
1229 cap.Cap = CAP_AUTOFEED;
1230 cap.ConType = TWON_DONTCARE16;
1232 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1233 GlobalFree(cap.hContainer);
1235 memset(&cap, 0, sizeof(cap));
1236 cap.Cap = CAP_FEEDERENABLED;
1237 cap.ConType = TWON_DONTCARE16;
1239 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1240 GlobalFree(cap.hContainer);
1242 memset(&cap, 0, sizeof(cap));
1243 cap.Cap = ICAP_SUPPORTEDSIZES;
1244 cap.ConType = TWON_DONTCARE16;
1246 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1247 GlobalFree(cap.hContainer);
1249 return TWCC_SUCCESS;