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
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
31 #ifndef SANE_VALUE_SCAN_MODE_COLOR
32 #define SANE_VALUE_SCAN_MODE_COLOR SANE_I18N("Color")
34 #ifndef SANE_VALUE_SCAN_MODE_GRAY
35 #define SANE_VALUE_SCAN_MODE_GRAY SANE_I18N("Gray")
37 #ifndef SANE_VALUE_SCAN_MODE_LINEART
38 #define SANE_VALUE_SCAN_MODE_LINEART SANE_I18N("Lineart")
41 static TW_UINT16
get_onevalue(pTW_CAPABILITY pCapability
, TW_UINT16
*type
, TW_UINT32
*value
)
43 if (pCapability
->hContainer
)
45 pTW_ONEVALUE pVal
= GlobalLock (pCapability
->hContainer
);
50 *type
= pVal
->ItemType
;
51 GlobalUnlock (pCapability
->hContainer
);
59 static TW_UINT16
set_onevalue(pTW_CAPABILITY pCapability
, TW_UINT16 type
, TW_UINT32 value
)
61 pCapability
->hContainer
= GlobalAlloc (0, sizeof(TW_ONEVALUE
));
63 if (pCapability
->hContainer
)
65 pTW_ONEVALUE pVal
= GlobalLock (pCapability
->hContainer
);
68 pCapability
->ConType
= TWON_ONEVALUE
;
69 pVal
->ItemType
= type
;
71 GlobalUnlock (pCapability
->hContainer
);
75 return TWCC_LOWMEMORY
;
78 static TW_UINT16
msg_set(pTW_CAPABILITY pCapability
, TW_UINT32
*val
)
80 if (pCapability
->ConType
== TWON_ONEVALUE
)
81 return get_onevalue(pCapability
, NULL
, val
);
83 FIXME("Partial Stub: MSG_SET only supports TW_ONEVALUE\n");
88 static TW_UINT16
msg_get_enum(pTW_CAPABILITY pCapability
, const TW_UINT32
*values
, int value_count
,
89 TW_UINT16 type
, TW_UINT32 current
, TW_UINT32 default_value
)
91 TW_ENUMERATION
*enumv
= NULL
;
96 pCapability
->ConType
= TWON_ENUMERATION
;
97 pCapability
->hContainer
= 0;
99 if (type
== TWTY_INT16
|| type
== TWTY_UINT16
)
100 pCapability
->hContainer
= GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION
, ItemList
[value_count
* sizeof(TW_UINT16
)]));
102 if (type
== TWTY_INT32
|| type
== TWTY_UINT32
)
103 pCapability
->hContainer
= GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION
, ItemList
[value_count
* sizeof(TW_UINT32
)]));
105 if (pCapability
->hContainer
)
106 enumv
= GlobalLock(pCapability
->hContainer
);
109 return TWCC_LOWMEMORY
;
111 enumv
->ItemType
= type
;
112 enumv
->NumItems
= value_count
;
114 p16
= (TW_UINT16
*) enumv
->ItemList
;
115 p32
= (TW_UINT32
*) enumv
->ItemList
;
116 for (i
= 0; i
< value_count
; i
++)
118 if (values
[i
] == current
)
119 enumv
->CurrentIndex
= i
;
120 if (values
[i
] == default_value
)
121 enumv
->DefaultIndex
= i
;
122 if (type
== TWTY_INT16
|| type
== TWTY_UINT16
)
124 if (type
== TWTY_INT32
|| type
== TWTY_UINT32
)
128 GlobalUnlock(pCapability
->hContainer
);
132 #ifdef SONAME_LIBSANE
133 static TW_UINT16
msg_get_range(pTW_CAPABILITY pCapability
, TW_UINT16 type
,
134 TW_UINT32 minval
, TW_UINT32 maxval
, TW_UINT32 step
, TW_UINT32 def
, TW_UINT32 current
)
136 TW_RANGE
*range
= NULL
;
138 pCapability
->ConType
= TWON_RANGE
;
139 pCapability
->hContainer
= 0;
141 pCapability
->hContainer
= GlobalAlloc (0, sizeof(*range
));
142 if (pCapability
->hContainer
)
143 range
= GlobalLock(pCapability
->hContainer
);
146 return TWCC_LOWMEMORY
;
148 range
->ItemType
= type
;
149 range
->MinValue
= minval
;
150 range
->MaxValue
= maxval
;
151 range
->StepSize
= step
;
152 range
->DefaultValue
= def
;
153 range
->CurrentValue
= current
;
155 GlobalUnlock(pCapability
->hContainer
);
160 static TW_UINT16
TWAIN_GetSupportedCaps(pTW_CAPABILITY pCapability
)
163 static const UINT16 supported_caps
[] = { CAP_SUPPORTEDCAPS
, CAP_XFERCOUNT
, CAP_UICONTROLLABLE
,
164 CAP_AUTOFEED
, CAP_FEEDERENABLED
,
165 ICAP_XFERMECH
, ICAP_PIXELTYPE
, ICAP_UNITS
, ICAP_BITDEPTH
, ICAP_COMPRESSION
, ICAP_PIXELFLAVOR
,
166 ICAP_XRESOLUTION
, ICAP_YRESOLUTION
, ICAP_PHYSICALHEIGHT
, ICAP_PHYSICALWIDTH
, ICAP_SUPPORTEDSIZES
};
168 pCapability
->hContainer
= GlobalAlloc (0, FIELD_OFFSET( TW_ARRAY
, ItemList
[sizeof(supported_caps
)] ));
169 pCapability
->ConType
= TWON_ARRAY
;
171 if (pCapability
->hContainer
)
175 a
= GlobalLock (pCapability
->hContainer
);
176 a
->ItemType
= TWTY_UINT16
;
177 a
->NumItems
= ARRAY_SIZE(supported_caps
);
178 u
= (UINT16
*) a
->ItemList
;
179 for (i
= 0; i
< a
->NumItems
; i
++)
180 u
[i
] = supported_caps
[i
];
181 GlobalUnlock (pCapability
->hContainer
);
185 return TWCC_LOWMEMORY
;
190 static TW_UINT16
SANE_ICAPXferMech (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
192 static const TW_UINT32 possible_values
[] = { TWSX_NATIVE
, TWSX_MEMORY
};
194 TW_UINT16 twCC
= TWCC_BADCAP
;
196 TRACE("ICAP_XFERMECH\n");
200 case MSG_QUERYSUPPORT
:
201 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
202 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
206 twCC
= msg_get_enum(pCapability
, possible_values
, ARRAY_SIZE(possible_values
),
207 TWTY_UINT16
, activeDS
.capXferMech
, TWSX_NATIVE
);
211 twCC
= msg_set(pCapability
, &val
);
212 if (twCC
== TWCC_SUCCESS
)
214 activeDS
.capXferMech
= (TW_UINT16
) val
;
215 FIXME("Partial Stub: XFERMECH set to %d, but ignored\n", val
);
220 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, TWSX_NATIVE
);
224 activeDS
.capXferMech
= TWSX_NATIVE
;
225 /* .. fall through intentional .. */
228 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, activeDS
.capXferMech
);
229 FIXME("Partial Stub: XFERMECH of %d not actually used\n", activeDS
.capXferMech
);
237 static TW_UINT16
SANE_CAPXferCount (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
240 TW_UINT16 twCC
= TWCC_BADCAP
;
242 TRACE("CAP_XFERCOUNT\n");
246 case MSG_QUERYSUPPORT
:
247 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
248 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
252 twCC
= set_onevalue(pCapability
, TWTY_INT16
, -1);
253 FIXME("Partial Stub: Reporting only support for transfer all\n");
257 twCC
= msg_set(pCapability
, &val
);
258 if (twCC
== TWCC_SUCCESS
)
259 FIXME("Partial Stub: XFERCOUNT set to %d, but ignored\n", val
);
263 twCC
= set_onevalue(pCapability
, TWTY_INT16
, -1);
267 /* .. fall through intentional .. */
270 twCC
= set_onevalue(pCapability
, TWTY_INT16
, -1);
276 #ifdef SONAME_LIBSANE
277 static BOOL
pixeltype_to_sane_mode(TW_UINT16 pixeltype
, SANE_String mode
, int len
)
279 SANE_String_Const m
= NULL
;
283 m
= SANE_VALUE_SCAN_MODE_GRAY
;
286 m
= SANE_VALUE_SCAN_MODE_COLOR
;
289 m
= SANE_VALUE_SCAN_MODE_LINEART
;
294 if (strlen(m
) >= len
)
299 static BOOL
sane_mode_to_pixeltype(SANE_String_Const mode
, TW_UINT16
*pixeltype
)
301 if (strcmp(mode
, SANE_VALUE_SCAN_MODE_LINEART
) == 0)
302 *pixeltype
= TWPT_BW
;
303 else if (memcmp(mode
, SANE_VALUE_SCAN_MODE_GRAY
, strlen(SANE_VALUE_SCAN_MODE_GRAY
)) == 0)
304 *pixeltype
= TWPT_GRAY
;
305 else if (strcmp(mode
, SANE_VALUE_SCAN_MODE_COLOR
) == 0)
306 *pixeltype
= TWPT_RGB
;
315 static TW_UINT16
SANE_ICAPPixelType (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
317 TW_UINT16 twCC
= TWCC_BADCAP
;
318 #ifdef SONAME_LIBSANE
319 TW_UINT32 possible_values
[3];
320 int possible_value_count
;
324 SANE_String_Const
*choices
;
325 char current_mode
[64];
326 TW_UINT16 current_pixeltype
= TWPT_BW
;
329 TRACE("ICAP_PIXELTYPE\n");
331 rc
= sane_option_probe_mode(activeDS
.deviceHandle
, &choices
, current_mode
, sizeof(current_mode
));
332 if (rc
!= SANE_STATUS_GOOD
)
334 ERR("Unable to retrieve mode from sane, ICAP_PIXELTYPE unsupported\n");
338 sane_mode_to_pixeltype(current_mode
, ¤t_pixeltype
);
340 /* Sane does not support a concept of a default mode, so we simply cache
341 * the first mode we find */
342 if (! activeDS
.PixelTypeSet
)
344 activeDS
.PixelTypeSet
= TRUE
;
345 activeDS
.defaultPixelType
= current_pixeltype
;
350 case MSG_QUERYSUPPORT
:
351 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
352 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
356 for (possible_value_count
= 0; choices
&& *choices
&& possible_value_count
< 3; choices
++)
359 if (sane_mode_to_pixeltype(*choices
, &pix
))
360 possible_values
[possible_value_count
++] = pix
;
362 twCC
= msg_get_enum(pCapability
, possible_values
, possible_value_count
,
363 TWTY_UINT16
, current_pixeltype
, activeDS
.defaultPixelType
);
367 twCC
= msg_set(pCapability
, &val
);
368 if (twCC
== TWCC_SUCCESS
)
370 TRACE("Setting pixeltype to %d\n", val
);
371 if (! pixeltype_to_sane_mode(val
, mode
, sizeof(mode
)))
372 return TWCC_BADVALUE
;
375 rc
= sane_option_set_str(activeDS
.deviceHandle
, "mode", mode
, &status
);
376 /* Some SANE devices use 'Grayscale' instead of the standard 'Gray' */
377 if (rc
== SANE_STATUS_INVAL
&& strcmp(mode
, SANE_VALUE_SCAN_MODE_GRAY
) == 0)
379 strcpy(mode
, "Grayscale");
380 rc
= sane_option_set_str(activeDS
.deviceHandle
, "mode", mode
, &status
);
382 if (rc
!= SANE_STATUS_GOOD
)
383 return sane_status_to_twcc(rc
);
384 if (status
& SANE_INFO_RELOAD_PARAMS
)
385 psane_get_parameters (activeDS
.deviceHandle
, &activeDS
.sane_param
);
390 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, activeDS
.defaultPixelType
);
394 current_pixeltype
= activeDS
.defaultPixelType
;
395 if (! pixeltype_to_sane_mode(current_pixeltype
, mode
, sizeof(mode
)))
396 return TWCC_BADVALUE
;
399 rc
= sane_option_set_str(activeDS
.deviceHandle
, "mode", mode
, &status
);
400 /* Some SANE devices use 'Grayscale' instead of the standard 'Gray' */
401 if (rc
== SANE_STATUS_INVAL
&& strcmp(mode
, SANE_VALUE_SCAN_MODE_GRAY
) == 0)
403 strcpy(mode
, "Grayscale");
404 rc
= sane_option_set_str(activeDS
.deviceHandle
, "mode", mode
, &status
);
406 if (rc
!= SANE_STATUS_GOOD
)
407 return sane_status_to_twcc(rc
);
408 if (status
& SANE_INFO_RELOAD_PARAMS
)
409 psane_get_parameters (activeDS
.deviceHandle
, &activeDS
.sane_param
);
411 /* .. fall through intentional .. */
414 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, current_pixeltype
);
415 TRACE("Returning current pixeltype of %d\n", current_pixeltype
);
424 static TW_UINT16
SANE_ICAPUnits (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
427 TW_UINT16 twCC
= TWCC_BADCAP
;
429 TRACE("ICAP_UNITS\n");
433 case MSG_QUERYSUPPORT
:
434 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
435 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
439 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, TWUN_INCHES
);
443 twCC
= msg_set(pCapability
, &val
);
444 if (twCC
== TWCC_SUCCESS
)
446 if (val
!= TWUN_INCHES
)
448 ERR("Sane supports only SANE_UNIT_DPI\n");
449 twCC
= TWCC_BADVALUE
;
456 /* .. fall through intentional .. */
459 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, TWUN_INCHES
);
467 static TW_UINT16
SANE_ICAPBitDepth(pTW_CAPABILITY pCapability
, TW_UINT16 action
)
469 TW_UINT16 twCC
= TWCC_BADCAP
;
470 #ifdef SONAME_LIBSANE
471 TW_UINT32 possible_values
[1];
473 TRACE("ICAP_BITDEPTH\n");
475 possible_values
[0] = activeDS
.sane_param
.depth
;
479 case MSG_QUERYSUPPORT
:
480 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
481 TWQC_GET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
);
485 twCC
= msg_get_enum(pCapability
, possible_values
, ARRAY_SIZE(possible_values
),
486 TWTY_UINT16
, activeDS
.sane_param
.depth
, activeDS
.sane_param
.depth
);
490 /* .. Fall through intentional .. */
493 TRACE("Returning current bitdepth of %d\n", activeDS
.sane_param
.depth
);
494 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, activeDS
.sane_param
.depth
);
501 /* CAP_UICONTROLLABLE */
502 static TW_UINT16
SANE_CAPUiControllable(pTW_CAPABILITY pCapability
, TW_UINT16 action
)
504 TW_UINT16 twCC
= TWCC_BADCAP
;
506 TRACE("CAP_UICONTROLLABLE\n");
510 case MSG_QUERYSUPPORT
:
511 twCC
= set_onevalue(pCapability
, TWTY_INT32
, TWQC_GET
);
515 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, TRUE
);
522 /* ICAP_COMPRESSION */
523 static TW_UINT16
SANE_ICAPCompression (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
525 static const TW_UINT32 possible_values
[] = { TWCP_NONE
};
527 TW_UINT16 twCC
= TWCC_BADCAP
;
529 TRACE("ICAP_COMPRESSION\n");
533 case MSG_QUERYSUPPORT
:
534 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
535 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
539 twCC
= msg_get_enum(pCapability
, possible_values
, ARRAY_SIZE(possible_values
),
540 TWTY_UINT16
, TWCP_NONE
, TWCP_NONE
);
541 FIXME("Partial stub: We don't attempt to support compression\n");
545 twCC
= msg_set(pCapability
, &val
);
546 if (twCC
== TWCC_SUCCESS
)
547 FIXME("Partial Stub: COMPRESSION set to %d, but ignored\n", val
);
551 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, TWCP_NONE
);
555 /* .. fall through intentional .. */
558 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, TWCP_NONE
);
564 /* ICAP_XRESOLUTION, ICAP_YRESOLUTION */
565 static TW_UINT16
SANE_ICAPResolution (pTW_CAPABILITY pCapability
, TW_UINT16 action
, TW_UINT16 cap
)
567 TW_UINT16 twCC
= TWCC_BADCAP
;
568 #ifdef SONAME_LIBSANE
570 SANE_Int current_resolution
;
571 TW_FIX32
*default_res
;
572 const char *best_option_name
;
573 SANE_Int minval
, maxval
, quantval
;
577 TRACE("ICAP_%cRESOLUTION\n", cap
== ICAP_XRESOLUTION
? 'X' : 'Y');
579 /* Some scanners support 'x-resolution', most seem to just support 'resolution' */
580 if (cap
== ICAP_XRESOLUTION
)
582 best_option_name
= "x-resolution";
583 default_res
= &activeDS
.defaultXResolution
;
587 best_option_name
= "y-resolution";
588 default_res
= &activeDS
.defaultYResolution
;
590 if (sane_option_get_int(activeDS
.deviceHandle
, best_option_name
, ¤t_resolution
) != SANE_STATUS_GOOD
)
592 best_option_name
= "resolution";
593 if (sane_option_get_int(activeDS
.deviceHandle
, best_option_name
, ¤t_resolution
) != SANE_STATUS_GOOD
)
597 /* Sane does not support a concept of 'default' resolution, so we have to
598 * cache the resolution the very first time we load the scanner, and use that
600 if (cap
== ICAP_XRESOLUTION
&& ! activeDS
.XResolutionSet
)
602 default_res
->Whole
= current_resolution
;
603 default_res
->Frac
= 0;
604 activeDS
.XResolutionSet
= TRUE
;
607 if (cap
== ICAP_YRESOLUTION
&& ! activeDS
.YResolutionSet
)
609 default_res
->Whole
= current_resolution
;
610 default_res
->Frac
= 0;
611 activeDS
.YResolutionSet
= TRUE
;
616 case MSG_QUERYSUPPORT
:
617 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
618 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
622 sane_rc
= sane_option_probe_resolution(activeDS
.deviceHandle
, best_option_name
, &minval
, &maxval
, &quantval
);
623 if (sane_rc
!= SANE_STATUS_GOOD
)
626 twCC
= msg_get_range(pCapability
, TWTY_FIX32
,
627 minval
, maxval
, quantval
== 0 ? 1 : quantval
, default_res
->Whole
, current_resolution
);
631 twCC
= msg_set(pCapability
, &val
);
632 if (twCC
== TWCC_SUCCESS
)
635 memcpy(&f32
, &val
, sizeof(f32
));
636 sane_rc
= sane_option_set_int(activeDS
.deviceHandle
, best_option_name
, f32
.Whole
, &set_status
);
637 if (sane_rc
!= SANE_STATUS_GOOD
)
639 FIXME("Status of %d not expected or handled\n", sane_rc
);
642 else if (set_status
== SANE_INFO_INEXACT
)
643 twCC
= TWCC_CHECKSTATUS
;
648 twCC
= set_onevalue(pCapability
, TWTY_FIX32
, default_res
->Whole
);
652 sane_rc
= sane_option_set_int(activeDS
.deviceHandle
, best_option_name
, default_res
->Whole
, NULL
);
653 if (sane_rc
!= SANE_STATUS_GOOD
)
656 /* .. fall through intentional .. */
659 twCC
= set_onevalue(pCapability
, TWTY_FIX32
, current_resolution
);
666 /* ICAP_PHYSICALHEIGHT, ICAP_PHYSICALWIDTH */
667 static TW_UINT16
SANE_ICAPPhysical (pTW_CAPABILITY pCapability
, TW_UINT16 action
, TW_UINT16 cap
)
669 TW_UINT16 twCC
= TWCC_BADCAP
;
670 #ifdef SONAME_LIBSANE
672 char option_name
[64];
673 SANE_Fixed lower
, upper
;
674 SANE_Unit lowerunit
, upperunit
;
677 TRACE("ICAP_PHYSICAL%s\n", cap
== ICAP_PHYSICALHEIGHT
? "HEIGHT" : "WIDTH");
679 sprintf(option_name
, "tl-%c", cap
== ICAP_PHYSICALHEIGHT
? 'y' : 'x');
680 status
= sane_option_probe_scan_area(activeDS
.deviceHandle
, option_name
, NULL
, &lowerunit
, &lower
, NULL
, NULL
);
681 if (status
!= SANE_STATUS_GOOD
)
682 return sane_status_to_twcc(status
);
684 sprintf(option_name
, "br-%c", cap
== ICAP_PHYSICALHEIGHT
? 'y' : 'x');
685 status
= sane_option_probe_scan_area(activeDS
.deviceHandle
, option_name
, NULL
, &upperunit
, NULL
, &upper
, NULL
);
686 if (status
!= SANE_STATUS_GOOD
)
687 return sane_status_to_twcc(status
);
689 if (upperunit
!= lowerunit
)
692 if (! convert_sane_res_to_twain(SANE_UNFIX(upper
) - SANE_UNFIX(lower
), upperunit
, &res
, TWUN_INCHES
))
697 case MSG_QUERYSUPPORT
:
698 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
699 TWQC_GET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
);
705 /* .. fall through intentional .. */
708 twCC
= set_onevalue(pCapability
, TWTY_FIX32
, res
.Whole
| (res
.Frac
<< 16));
715 /* ICAP_PIXELFLAVOR */
716 static TW_UINT16
SANE_ICAPPixelFlavor (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
718 TW_UINT16 twCC
= TWCC_BADCAP
;
719 #ifdef SONAME_LIBSANE
720 static const TW_UINT32 possible_values
[] = { TWPF_CHOCOLATE
, TWPF_VANILLA
};
722 TW_UINT32 flavor
= activeDS
.sane_param
.depth
== 1 ? TWPF_VANILLA
: TWPF_CHOCOLATE
;
724 TRACE("ICAP_PIXELFLAVOR\n");
728 case MSG_QUERYSUPPORT
:
729 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
730 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
734 twCC
= msg_get_enum(pCapability
, possible_values
, ARRAY_SIZE(possible_values
),
735 TWTY_UINT16
, flavor
, flavor
);
739 twCC
= msg_set(pCapability
, &val
);
740 if (twCC
== TWCC_SUCCESS
)
742 FIXME("Stub: PIXELFLAVOR set to %d, but ignored\n", val
);
747 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, flavor
);
751 /* .. fall through intentional .. */
754 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, flavor
);
761 #ifdef SONAME_LIBSANE
762 static TW_UINT16
get_width_height(double *width
, double *height
, BOOL max
)
766 SANE_Fixed tlx_current
, tlx_min
, tlx_max
;
767 SANE_Fixed tly_current
, tly_min
, tly_max
;
768 SANE_Fixed brx_current
, brx_min
, brx_max
;
769 SANE_Fixed bry_current
, bry_min
, bry_max
;
771 status
= sane_option_probe_scan_area(activeDS
.deviceHandle
, "tl-x", &tlx_current
, NULL
, &tlx_min
, &tlx_max
, NULL
);
772 if (status
!= SANE_STATUS_GOOD
)
773 return sane_status_to_twcc(status
);
775 status
= sane_option_probe_scan_area(activeDS
.deviceHandle
, "tl-y", &tly_current
, NULL
, &tly_min
, &tly_max
, NULL
);
776 if (status
!= SANE_STATUS_GOOD
)
777 return sane_status_to_twcc(status
);
779 status
= sane_option_probe_scan_area(activeDS
.deviceHandle
, "br-x", &brx_current
, NULL
, &brx_min
, &brx_max
, NULL
);
780 if (status
!= SANE_STATUS_GOOD
)
781 return sane_status_to_twcc(status
);
783 status
= sane_option_probe_scan_area(activeDS
.deviceHandle
, "br-y", &bry_current
, NULL
, &bry_min
, &bry_max
, NULL
);
784 if (status
!= SANE_STATUS_GOOD
)
785 return sane_status_to_twcc(status
);
788 *width
= SANE_UNFIX(brx_max
) - SANE_UNFIX(tlx_min
);
790 *width
= SANE_UNFIX(brx_current
) - SANE_UNFIX(tlx_current
);
793 *height
= SANE_UNFIX(bry_max
) - SANE_UNFIX(tly_min
);
795 *height
= SANE_UNFIX(bry_current
) - SANE_UNFIX(tly_current
);
797 return(TWCC_SUCCESS
);
800 static TW_UINT16
set_one_coord(const char *name
, double coord
)
803 status
= sane_option_set_fixed(activeDS
.deviceHandle
, name
, SANE_FIX(coord
), NULL
);
804 if (status
!= SANE_STATUS_GOOD
)
805 return sane_status_to_twcc(status
);
809 static TW_UINT16
set_width_height(double width
, double height
)
811 TW_UINT16 rc
= TWCC_SUCCESS
;
812 rc
= set_one_coord("tl-x", 0);
813 if (rc
!= TWCC_SUCCESS
)
815 rc
= set_one_coord("br-x", width
);
816 if (rc
!= TWCC_SUCCESS
)
818 rc
= set_one_coord("tl-y", 0);
819 if (rc
!= TWCC_SUCCESS
)
821 rc
= set_one_coord("br-y", height
);
833 static const supported_size_t supported_sizes
[] =
836 { TWSS_A4
, 210, 297 },
837 { TWSS_JISB5
, 182, 257 },
838 { TWSS_USLETTER
, 215.9, 279.4 },
839 { TWSS_USLEGAL
, 215.9, 355.6 },
840 { TWSS_A5
, 148, 210 },
841 { TWSS_B4
, 250, 353 },
842 { TWSS_B6
, 125, 176 },
843 { TWSS_USLEDGER
, 215.9, 431.8 },
844 { TWSS_USEXECUTIVE
, 184.15, 266.7 },
845 { TWSS_A3
, 297, 420 },
848 static TW_UINT16
get_default_paper_size(const supported_size_t
*s
, int n
)
853 double width
, height
;
855 rc
= GetLocaleInfoA(LOCALE_USER_DEFAULT
, LOCALE_IPAPERSIZE
| LOCALE_RETURN_NUMBER
, (void *) &paper
, sizeof(paper
));
860 defsize
= TWSS_USLETTER
;
863 defsize
= TWSS_USLEGAL
;
876 if (get_width_height(&width
, &height
, TRUE
) != TWCC_SUCCESS
)
879 for (i
= 0; i
< n
; i
++)
880 if (s
[i
].size
== defsize
)
882 /* Sane's use of integers to store floats is a hair lossy; deal with it */
883 if (s
[i
].x
> (width
+ .01) || s
[i
].y
> (height
+ 0.01))
892 static TW_UINT16
get_current_paper_size(const supported_size_t
*s
, int n
)
895 double width
, height
;
896 double xdelta
, ydelta
;
898 if (get_width_height(&width
, &height
, FALSE
) != TWCC_SUCCESS
)
901 for (i
= 0; i
< n
; i
++)
903 /* Sane's use of integers to store floats results
904 * in a very small error; cope with that */
905 xdelta
= s
[i
].x
- width
;
906 ydelta
= s
[i
].y
- height
;
907 if (xdelta
< 0.01 && xdelta
> -0.01 &&
908 ydelta
< 0.01 && ydelta
> -0.01)
916 /* ICAP_SUPPORTEDSIZES */
917 static TW_UINT16
SANE_ICAPSupportedSizes (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
919 TW_UINT16 twCC
= TWCC_BADCAP
;
920 #ifdef SONAME_LIBSANE
922 static TW_UINT32 possible_values
[ARRAY_SIZE(supported_sizes
)];
925 TW_UINT16 default_size
= get_default_paper_size(supported_sizes
, ARRAY_SIZE(supported_sizes
));
926 TW_UINT16 current_size
= get_current_paper_size(supported_sizes
, ARRAY_SIZE(supported_sizes
));
928 TRACE("ICAP_SUPPORTEDSIZES\n");
932 case MSG_QUERYSUPPORT
:
933 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
934 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
938 for (i
= 0; i
< ARRAY_SIZE(supported_sizes
); i
++)
939 possible_values
[i
] = supported_sizes
[i
].size
;
940 twCC
= msg_get_enum(pCapability
, possible_values
, ARRAY_SIZE(possible_values
),
941 TWTY_UINT16
, current_size
, default_size
);
942 WARN("Partial Stub: our supported size selection is a bit thin.\n");
946 twCC
= msg_set(pCapability
, &val
);
947 if (twCC
== TWCC_SUCCESS
)
948 for (i
= 1; i
< ARRAY_SIZE(supported_sizes
); i
++)
949 if (supported_sizes
[i
].size
== val
)
950 return set_width_height(supported_sizes
[i
].x
, supported_sizes
[i
].y
);
952 ERR("Unsupported size %d\n", val
);
957 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, default_size
);
962 for (i
= 1; i
< ARRAY_SIZE(supported_sizes
); i
++)
963 if (supported_sizes
[i
].size
== default_size
)
965 twCC
= set_width_height(supported_sizes
[i
].x
, supported_sizes
[i
].y
);
968 if (twCC
!= TWCC_SUCCESS
)
971 /* .. fall through intentional .. */
974 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, current_size
);
983 static TW_UINT16
SANE_CAPAutofeed (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
985 TW_UINT16 twCC
= TWCC_BADCAP
;
986 #ifdef SONAME_LIBSANE
991 TRACE("CAP_AUTOFEED\n");
993 if (sane_option_get_bool(activeDS
.deviceHandle
, "batch-scan", &autofeed
, NULL
) != SANE_STATUS_GOOD
)
998 case MSG_QUERYSUPPORT
:
999 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
1000 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
1004 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, autofeed
);
1008 twCC
= msg_set(pCapability
, &val
);
1009 if (twCC
== TWCC_SUCCESS
)
1012 autofeed
= SANE_TRUE
;
1014 autofeed
= SANE_FALSE
;
1016 status
= sane_option_set_bool(activeDS
.deviceHandle
, "batch-scan", autofeed
, NULL
);
1017 if (status
!= SANE_STATUS_GOOD
)
1019 ERR("Error %s: Could not set batch-scan to %d\n", psane_strstatus(status
), autofeed
);
1020 return sane_status_to_twcc(status
);
1025 case MSG_GETDEFAULT
:
1026 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, SANE_TRUE
);
1030 autofeed
= SANE_TRUE
;
1031 status
= sane_option_set_bool(activeDS
.deviceHandle
, "batch-scan", autofeed
, NULL
);
1032 if (status
!= SANE_STATUS_GOOD
)
1034 ERR("Error %s: Could not reset batch-scan to SANE_TRUE\n", psane_strstatus(status
));
1035 return sane_status_to_twcc(status
);
1037 /* .. fall through intentional .. */
1039 case MSG_GETCURRENT
:
1040 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, autofeed
);
1047 /* CAP_FEEDERENABLED */
1048 static TW_UINT16
SANE_CAPFeederEnabled (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
1050 TW_UINT16 twCC
= TWCC_BADCAP
;
1051 #ifdef SONAME_LIBSANE
1055 SANE_Char source
[64];
1057 TRACE("CAP_FEEDERENABLED\n");
1059 if (sane_option_get_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, sizeof(source
), NULL
) != SANE_STATUS_GOOD
)
1062 if (strcmp(source
, "Auto") == 0 || strcmp(source
, "ADF") == 0)
1069 case MSG_QUERYSUPPORT
:
1070 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
1071 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
1075 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, enabled
);
1079 twCC
= msg_set(pCapability
, &val
);
1080 if (twCC
== TWCC_SUCCESS
)
1082 strcpy(source
, "ADF");
1083 status
= sane_option_set_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, NULL
);
1084 if (status
!= SANE_STATUS_GOOD
)
1086 strcpy(source
, "Auto");
1087 status
= sane_option_set_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, NULL
);
1089 if (status
!= SANE_STATUS_GOOD
)
1091 ERR("Error %s: Could not set source to either ADF or Auto\n", psane_strstatus(status
));
1092 return sane_status_to_twcc(status
);
1097 case MSG_GETDEFAULT
:
1098 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, TRUE
);
1102 strcpy(source
, "Auto");
1103 if (sane_option_set_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, NULL
) == SANE_STATUS_GOOD
)
1105 /* .. fall through intentional .. */
1107 case MSG_GETCURRENT
:
1108 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, enabled
);
1117 TW_UINT16
SANE_SaneCapability (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
1119 TW_UINT16 twCC
= TWCC_CAPUNSUPPORTED
;
1121 TRACE("capability=%d action=%d\n", pCapability
->Cap
, action
);
1123 switch (pCapability
->Cap
)
1125 case CAP_SUPPORTEDCAPS
:
1126 if (action
== MSG_GET
)
1127 twCC
= TWAIN_GetSupportedCaps(pCapability
);
1129 twCC
= TWCC_BADVALUE
;
1133 twCC
= SANE_CAPXferCount (pCapability
, action
);
1136 case CAP_UICONTROLLABLE
:
1137 twCC
= SANE_CAPUiControllable (pCapability
, action
);
1141 twCC
= SANE_CAPAutofeed (pCapability
, action
);
1144 case CAP_FEEDERENABLED
:
1145 twCC
= SANE_CAPFeederEnabled (pCapability
, action
);
1148 case ICAP_PIXELTYPE
:
1149 twCC
= SANE_ICAPPixelType (pCapability
, action
);
1153 twCC
= SANE_ICAPUnits (pCapability
, action
);
1157 twCC
= SANE_ICAPBitDepth(pCapability
, action
);
1161 twCC
= SANE_ICAPXferMech (pCapability
, action
);
1164 case ICAP_PIXELFLAVOR
:
1165 twCC
= SANE_ICAPPixelFlavor (pCapability
, action
);
1168 case ICAP_COMPRESSION
:
1169 twCC
= SANE_ICAPCompression(pCapability
, action
);
1172 case ICAP_XRESOLUTION
:
1173 twCC
= SANE_ICAPResolution(pCapability
, action
, pCapability
->Cap
);
1176 case ICAP_YRESOLUTION
:
1177 twCC
= SANE_ICAPResolution(pCapability
, action
, pCapability
->Cap
);
1180 case ICAP_PHYSICALHEIGHT
:
1181 twCC
= SANE_ICAPPhysical(pCapability
, action
, pCapability
->Cap
);
1184 case ICAP_PHYSICALWIDTH
:
1185 twCC
= SANE_ICAPPhysical(pCapability
, action
, pCapability
->Cap
);
1188 case ICAP_SUPPORTEDSIZES
:
1189 twCC
= SANE_ICAPSupportedSizes (pCapability
, action
);
1192 case ICAP_PLANARCHUNKY
:
1193 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1197 FIXME("ICAP_BITORDER not implemented\n");
1202 /* Twain specifies that you should return a 0 in response to QUERYSUPPORT,
1203 * even if you don't formally support the capability */
1204 if (twCC
== TWCC_CAPUNSUPPORTED
&& action
== MSG_QUERYSUPPORT
)
1205 twCC
= set_onevalue(pCapability
, 0, TWTY_INT32
);
1207 if (twCC
== TWCC_CAPUNSUPPORTED
)
1208 TRACE("capability 0x%x/action=%d being reported as unsupported\n", pCapability
->Cap
, action
);
1213 #ifdef SONAME_LIBSANE
1214 TW_UINT16
SANE_SaneSetDefaults (void)
1218 memset(&cap
, 0, sizeof(cap
));
1219 cap
.Cap
= CAP_AUTOFEED
;
1220 cap
.ConType
= TWON_DONTCARE16
;
1222 if (SANE_SaneCapability(&cap
, MSG_RESET
) == TWCC_SUCCESS
)
1223 GlobalFree(cap
.hContainer
);
1225 memset(&cap
, 0, sizeof(cap
));
1226 cap
.Cap
= CAP_FEEDERENABLED
;
1227 cap
.ConType
= TWON_DONTCARE16
;
1229 if (SANE_SaneCapability(&cap
, MSG_RESET
) == TWCC_SUCCESS
)
1230 GlobalFree(cap
.hContainer
);
1232 memset(&cap
, 0, sizeof(cap
));
1233 cap
.Cap
= ICAP_SUPPORTEDSIZES
;
1234 cap
.ConType
= TWON_DONTCARE16
;
1236 if (SANE_SaneCapability(&cap
, MSG_RESET
) == TWCC_SUCCESS
)
1237 GlobalFree(cap
.hContainer
);
1239 return TWCC_SUCCESS
;