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
= sizeof(supported_caps
) / sizeof(supported_caps
[0]);
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
, sizeof(possible_values
) / sizeof(possible_values
[0]),
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
, sizeof(possible_values
) / sizeof(possible_values
[0]),
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
, sizeof(possible_values
) / sizeof(possible_values
[0]),
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
, sizeof(possible_values
) / sizeof(possible_values
[0]),
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 },
847 #define SUPPORTED_SIZE_COUNT (sizeof(supported_sizes) / sizeof(supported_sizes[0]))
849 static TW_UINT16
get_default_paper_size(const supported_size_t
*s
, int n
)
854 double width
, height
;
856 rc
= GetLocaleInfoA(LOCALE_USER_DEFAULT
, LOCALE_IPAPERSIZE
| LOCALE_RETURN_NUMBER
, (void *) &paper
, sizeof(paper
));
861 defsize
= TWSS_USLETTER
;
864 defsize
= TWSS_USLEGAL
;
877 if (get_width_height(&width
, &height
, TRUE
) != TWCC_SUCCESS
)
880 for (i
= 0; i
< n
; i
++)
881 if (s
[i
].size
== defsize
)
883 /* Sane's use of integers to store floats is a hair lossy; deal with it */
884 if (s
[i
].x
> (width
+ .01) || s
[i
].y
> (height
+ 0.01))
893 static TW_UINT16
get_current_paper_size(const supported_size_t
*s
, int n
)
896 double width
, height
;
897 double xdelta
, ydelta
;
899 if (get_width_height(&width
, &height
, FALSE
) != TWCC_SUCCESS
)
902 for (i
= 0; i
< n
; i
++)
904 /* Sane's use of integers to store floats results
905 * in a very small error; cope with that */
906 xdelta
= s
[i
].x
- width
;
907 ydelta
= s
[i
].y
- height
;
908 if (xdelta
< 0.01 && xdelta
> -0.01 &&
909 ydelta
< 0.01 && ydelta
> -0.01)
917 /* ICAP_SUPPORTEDSIZES */
918 static TW_UINT16
SANE_ICAPSupportedSizes (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
920 TW_UINT16 twCC
= TWCC_BADCAP
;
921 #ifdef SONAME_LIBSANE
923 static TW_UINT32 possible_values
[SUPPORTED_SIZE_COUNT
];
926 TW_UINT16 default_size
= get_default_paper_size(supported_sizes
, SUPPORTED_SIZE_COUNT
);
927 TW_UINT16 current_size
= get_current_paper_size(supported_sizes
, SUPPORTED_SIZE_COUNT
);
929 TRACE("ICAP_SUPPORTEDSIZES\n");
933 case MSG_QUERYSUPPORT
:
934 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
935 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
939 for (i
= 0; i
< sizeof(supported_sizes
) / sizeof(supported_sizes
[0]); i
++)
940 possible_values
[i
] = supported_sizes
[i
].size
;
941 twCC
= msg_get_enum(pCapability
, possible_values
, sizeof(possible_values
) / sizeof(possible_values
[0]),
942 TWTY_UINT16
, current_size
, default_size
);
943 WARN("Partial Stub: our supported size selection is a bit thin.\n");
947 twCC
= msg_set(pCapability
, &val
);
948 if (twCC
== TWCC_SUCCESS
)
949 for (i
= 1; i
< SUPPORTED_SIZE_COUNT
; i
++)
950 if (supported_sizes
[i
].size
== val
)
951 return set_width_height(supported_sizes
[i
].x
, supported_sizes
[i
].y
);
953 ERR("Unsupported size %d\n", val
);
958 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, default_size
);
963 for (i
= 1; i
< SUPPORTED_SIZE_COUNT
; i
++)
964 if (supported_sizes
[i
].size
== default_size
)
966 twCC
= set_width_height(supported_sizes
[i
].x
, supported_sizes
[i
].y
);
969 if (twCC
!= TWCC_SUCCESS
)
972 /* .. fall through intentional .. */
975 twCC
= set_onevalue(pCapability
, TWTY_UINT16
, current_size
);
979 #undef SUPPORTED_SIZE_COUNT
985 static TW_UINT16
SANE_CAPAutofeed (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
987 TW_UINT16 twCC
= TWCC_BADCAP
;
988 #ifdef SONAME_LIBSANE
993 TRACE("CAP_AUTOFEED\n");
995 if (sane_option_get_bool(activeDS
.deviceHandle
, "batch-scan", &autofeed
, NULL
) != SANE_STATUS_GOOD
)
1000 case MSG_QUERYSUPPORT
:
1001 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
1002 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
1006 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, autofeed
);
1010 twCC
= msg_set(pCapability
, &val
);
1011 if (twCC
== TWCC_SUCCESS
)
1014 autofeed
= SANE_TRUE
;
1016 autofeed
= SANE_FALSE
;
1018 status
= sane_option_set_bool(activeDS
.deviceHandle
, "batch-scan", autofeed
, NULL
);
1019 if (status
!= SANE_STATUS_GOOD
)
1021 ERR("Error %s: Could not set batch-scan to %d\n", psane_strstatus(status
), autofeed
);
1022 return sane_status_to_twcc(status
);
1027 case MSG_GETDEFAULT
:
1028 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, SANE_TRUE
);
1032 autofeed
= SANE_TRUE
;
1033 status
= sane_option_set_bool(activeDS
.deviceHandle
, "batch-scan", autofeed
, NULL
);
1034 if (status
!= SANE_STATUS_GOOD
)
1036 ERR("Error %s: Could not reset batch-scan to SANE_TRUE\n", psane_strstatus(status
));
1037 return sane_status_to_twcc(status
);
1039 /* .. fall through intentional .. */
1041 case MSG_GETCURRENT
:
1042 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, autofeed
);
1049 /* CAP_FEEDERENABLED */
1050 static TW_UINT16
SANE_CAPFeederEnabled (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
1052 TW_UINT16 twCC
= TWCC_BADCAP
;
1053 #ifdef SONAME_LIBSANE
1057 SANE_Char source
[64];
1059 TRACE("CAP_FEEDERENABLED\n");
1061 if (sane_option_get_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, sizeof(source
), NULL
) != SANE_STATUS_GOOD
)
1064 if (strcmp(source
, "Auto") == 0 || strcmp(source
, "ADF") == 0)
1071 case MSG_QUERYSUPPORT
:
1072 twCC
= set_onevalue(pCapability
, TWTY_INT32
,
1073 TWQC_GET
| TWQC_SET
| TWQC_GETDEFAULT
| TWQC_GETCURRENT
| TWQC_RESET
);
1077 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, enabled
);
1081 twCC
= msg_set(pCapability
, &val
);
1082 if (twCC
== TWCC_SUCCESS
)
1089 strcpy(source
, "ADF");
1090 status
= sane_option_set_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, NULL
);
1091 if (status
!= SANE_STATUS_GOOD
)
1093 strcpy(source
, "Auto");
1094 status
= sane_option_set_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, NULL
);
1096 if (status
!= SANE_STATUS_GOOD
)
1098 ERR("Error %s: Could not set source to either ADF or Auto\n", psane_strstatus(status
));
1099 return sane_status_to_twcc(status
);
1104 case MSG_GETDEFAULT
:
1105 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, TRUE
);
1109 strcpy(source
, "Auto");
1110 if (sane_option_set_str(activeDS
.deviceHandle
, SANE_NAME_SCAN_SOURCE
, source
, NULL
) == SANE_STATUS_GOOD
)
1112 twCC
= TWCC_SUCCESS
;
1113 /* .. fall through intentional .. */
1115 case MSG_GETCURRENT
:
1116 twCC
= set_onevalue(pCapability
, TWTY_BOOL
, enabled
);
1125 TW_UINT16
SANE_SaneCapability (pTW_CAPABILITY pCapability
, TW_UINT16 action
)
1127 TW_UINT16 twCC
= TWCC_CAPUNSUPPORTED
;
1129 TRACE("capability=%d action=%d\n", pCapability
->Cap
, action
);
1131 switch (pCapability
->Cap
)
1133 case CAP_SUPPORTEDCAPS
:
1134 if (action
== MSG_GET
)
1135 twCC
= TWAIN_GetSupportedCaps(pCapability
);
1137 twCC
= TWCC_BADVALUE
;
1141 twCC
= SANE_CAPXferCount (pCapability
, action
);
1144 case CAP_UICONTROLLABLE
:
1145 twCC
= SANE_CAPUiControllable (pCapability
, action
);
1149 twCC
= SANE_CAPAutofeed (pCapability
, action
);
1152 case CAP_FEEDERENABLED
:
1153 twCC
= SANE_CAPFeederEnabled (pCapability
, action
);
1156 case ICAP_PIXELTYPE
:
1157 twCC
= SANE_ICAPPixelType (pCapability
, action
);
1161 twCC
= SANE_ICAPUnits (pCapability
, action
);
1165 twCC
= SANE_ICAPBitDepth(pCapability
, action
);
1169 twCC
= SANE_ICAPXferMech (pCapability
, action
);
1172 case ICAP_PIXELFLAVOR
:
1173 twCC
= SANE_ICAPPixelFlavor (pCapability
, action
);
1176 case ICAP_COMPRESSION
:
1177 twCC
= SANE_ICAPCompression(pCapability
, action
);
1180 case ICAP_XRESOLUTION
:
1181 twCC
= SANE_ICAPResolution(pCapability
, action
, pCapability
->Cap
);
1184 case ICAP_YRESOLUTION
:
1185 twCC
= SANE_ICAPResolution(pCapability
, action
, pCapability
->Cap
);
1188 case ICAP_PHYSICALHEIGHT
:
1189 twCC
= SANE_ICAPPhysical(pCapability
, action
, pCapability
->Cap
);
1192 case ICAP_PHYSICALWIDTH
:
1193 twCC
= SANE_ICAPPhysical(pCapability
, action
, pCapability
->Cap
);
1196 case ICAP_SUPPORTEDSIZES
:
1197 twCC
= SANE_ICAPSupportedSizes (pCapability
, action
);
1200 case ICAP_PLANARCHUNKY
:
1201 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1205 FIXME("ICAP_BITORDER not implemented\n");
1210 /* Twain specifies that you should return a 0 in response to QUERYSUPPORT,
1211 * even if you don't formally support the capability */
1212 if (twCC
== TWCC_CAPUNSUPPORTED
&& action
== MSG_QUERYSUPPORT
)
1213 twCC
= set_onevalue(pCapability
, 0, TWTY_INT32
);
1215 if (twCC
== TWCC_CAPUNSUPPORTED
)
1216 TRACE("capability 0x%x/action=%d being reported as unsupported\n", pCapability
->Cap
, action
);
1221 #ifdef SONAME_LIBSANE
1222 TW_UINT16
SANE_SaneSetDefaults (void)
1226 memset(&cap
, 0, sizeof(cap
));
1227 cap
.Cap
= CAP_AUTOFEED
;
1228 cap
.ConType
= TWON_DONTCARE16
;
1230 if (SANE_SaneCapability(&cap
, MSG_RESET
) == TWCC_SUCCESS
)
1231 GlobalFree(cap
.hContainer
);
1233 memset(&cap
, 0, sizeof(cap
));
1234 cap
.Cap
= CAP_FEEDERENABLED
;
1235 cap
.ConType
= TWON_DONTCARE16
;
1237 if (SANE_SaneCapability(&cap
, MSG_RESET
) == TWCC_SUCCESS
)
1238 GlobalFree(cap
.hContainer
);
1240 memset(&cap
, 0, sizeof(cap
));
1241 cap
.Cap
= ICAP_SUPPORTEDSIZES
;
1242 cap
.ConType
= TWON_DONTCARE16
;
1244 if (SANE_SaneCapability(&cap
, MSG_RESET
) == TWCC_SUCCESS
)
1245 GlobalFree(cap
.hContainer
);
1247 return TWCC_SUCCESS
;