wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / sane.ds / capability.c
blob9157cae596370480a7a1d8d52a0fe81e5cfdb73c
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 #include "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <math.h>
25 #include "sane_i.h"
26 #include "winnls.h"
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")
33 #endif
34 #ifndef SANE_VALUE_SCAN_MODE_GRAY
35 #define SANE_VALUE_SCAN_MODE_GRAY SANE_I18N("Gray")
36 #endif
37 #ifndef SANE_VALUE_SCAN_MODE_LINEART
38 #define SANE_VALUE_SCAN_MODE_LINEART SANE_I18N("Lineart")
39 #endif
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);
46 if (pVal)
48 *value = pVal->Item;
49 if (type)
50 *type = pVal->ItemType;
51 GlobalUnlock (pCapability->hContainer);
52 return TWCC_SUCCESS;
55 return TWCC_BUMMER;
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);
66 if (pVal)
68 pCapability->ConType = TWON_ONEVALUE;
69 pVal->ItemType = type;
70 pVal->Item = value;
71 GlobalUnlock (pCapability->hContainer);
72 return TWCC_SUCCESS;
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");
84 return TWCC_BADCAP;
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;
92 TW_UINT32 *p32;
93 TW_UINT16 *p16;
94 int i;
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);
108 if (! enumv)
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)
123 p16[i] = values[i];
124 if (type == TWTY_INT32 || type == TWTY_UINT32)
125 p32[i] = values[i];
128 GlobalUnlock(pCapability->hContainer);
129 return TWCC_SUCCESS;
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);
145 if (! range)
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);
156 return TWCC_SUCCESS;
158 #endif
160 static TW_UINT16 TWAIN_GetSupportedCaps(pTW_CAPABILITY pCapability)
162 TW_ARRAY *a;
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)
173 UINT16 *u;
174 TW_UINT32 i;
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);
182 return TWCC_SUCCESS;
184 else
185 return TWCC_LOWMEMORY;
189 /* ICAP_XFERMECH */
190 static TW_UINT16 SANE_ICAPXferMech (pTW_CAPABILITY pCapability, TW_UINT16 action)
192 static const TW_UINT32 possible_values[] = { TWSX_NATIVE, TWSX_MEMORY };
193 TW_UINT32 val;
194 TW_UINT16 twCC = TWCC_BADCAP;
196 TRACE("ICAP_XFERMECH\n");
198 switch (action)
200 case MSG_QUERYSUPPORT:
201 twCC = set_onevalue(pCapability, TWTY_INT32,
202 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
203 break;
205 case MSG_GET:
206 twCC = msg_get_enum(pCapability, possible_values, ARRAY_SIZE(possible_values),
207 TWTY_UINT16, activeDS.capXferMech, TWSX_NATIVE);
208 break;
210 case MSG_SET:
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);
217 break;
219 case MSG_GETDEFAULT:
220 twCC = set_onevalue(pCapability, TWTY_UINT16, TWSX_NATIVE);
221 break;
223 case MSG_RESET:
224 activeDS.capXferMech = TWSX_NATIVE;
225 /* .. fall through intentional .. */
227 case MSG_GETCURRENT:
228 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.capXferMech);
229 FIXME("Partial Stub: XFERMECH of %d not actually used\n", activeDS.capXferMech);
230 break;
232 return twCC;
236 /* CAP_XFERCOUNT */
237 static TW_UINT16 SANE_CAPXferCount (pTW_CAPABILITY pCapability, TW_UINT16 action)
239 TW_UINT32 val;
240 TW_UINT16 twCC = TWCC_BADCAP;
242 TRACE("CAP_XFERCOUNT\n");
244 switch (action)
246 case MSG_QUERYSUPPORT:
247 twCC = set_onevalue(pCapability, TWTY_INT32,
248 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
249 break;
251 case MSG_GET:
252 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
253 FIXME("Partial Stub: Reporting only support for transfer all\n");
254 break;
256 case MSG_SET:
257 twCC = msg_set(pCapability, &val);
258 if (twCC == TWCC_SUCCESS)
259 FIXME("Partial Stub: XFERCOUNT set to %d, but ignored\n", val);
260 break;
262 case MSG_GETDEFAULT:
263 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
264 break;
266 case MSG_RESET:
267 /* .. fall through intentional .. */
269 case MSG_GETCURRENT:
270 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
271 break;
273 return twCC;
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;
280 switch (pixeltype)
282 case TWPT_GRAY:
283 m = SANE_VALUE_SCAN_MODE_GRAY;
284 break;
285 case TWPT_RGB:
286 m = SANE_VALUE_SCAN_MODE_COLOR;
287 break;
288 case TWPT_BW:
289 m = SANE_VALUE_SCAN_MODE_LINEART;
290 break;
292 if (! m)
293 return FALSE;
294 if (strlen(m) >= len)
295 return FALSE;
296 strcpy(mode, m);
297 return TRUE;
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;
307 else
308 return FALSE;
310 return TRUE;
312 #endif
314 /* ICAP_PIXELTYPE */
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;
321 TW_UINT32 val;
322 SANE_Status rc;
323 SANE_Int status;
324 SANE_String_Const *choices;
325 char current_mode[64];
326 TW_UINT16 current_pixeltype = TWPT_BW;
327 SANE_Char mode[64];
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");
335 return twCC;
338 sane_mode_to_pixeltype(current_mode, &current_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;
348 switch (action)
350 case MSG_QUERYSUPPORT:
351 twCC = set_onevalue(pCapability, TWTY_INT32,
352 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
353 break;
355 case MSG_GET:
356 for (possible_value_count = 0; choices && *choices && possible_value_count < 3; choices++)
358 TW_UINT16 pix;
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);
364 break;
366 case MSG_SET:
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;
374 status = 0;
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);
387 break;
389 case MSG_GETDEFAULT:
390 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.defaultPixelType);
391 break;
393 case MSG_RESET:
394 current_pixeltype = activeDS.defaultPixelType;
395 if (! pixeltype_to_sane_mode(current_pixeltype, mode, sizeof(mode)))
396 return TWCC_BADVALUE;
398 status = 0;
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 .. */
413 case MSG_GETCURRENT:
414 twCC = set_onevalue(pCapability, TWTY_UINT16, current_pixeltype);
415 TRACE("Returning current pixeltype of %d\n", current_pixeltype);
416 break;
419 #endif
420 return twCC;
423 /* ICAP_UNITS */
424 static TW_UINT16 SANE_ICAPUnits (pTW_CAPABILITY pCapability, TW_UINT16 action)
426 TW_UINT32 val;
427 TW_UINT16 twCC = TWCC_BADCAP;
429 TRACE("ICAP_UNITS\n");
431 switch (action)
433 case MSG_QUERYSUPPORT:
434 twCC = set_onevalue(pCapability, TWTY_INT32,
435 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
436 break;
438 case MSG_GET:
439 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
440 break;
442 case MSG_SET:
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;
452 break;
454 case MSG_GETDEFAULT:
455 case MSG_RESET:
456 /* .. fall through intentional .. */
458 case MSG_GETCURRENT:
459 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
460 break;
463 return twCC;
466 /* ICAP_BITDEPTH */
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;
477 switch (action)
479 case MSG_QUERYSUPPORT:
480 twCC = set_onevalue(pCapability, TWTY_INT32,
481 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
482 break;
484 case MSG_GET:
485 twCC = msg_get_enum(pCapability, possible_values, ARRAY_SIZE(possible_values),
486 TWTY_UINT16, activeDS.sane_param.depth, activeDS.sane_param.depth);
487 break;
489 case MSG_GETDEFAULT:
490 /* .. Fall through intentional .. */
492 case MSG_GETCURRENT:
493 TRACE("Returning current bitdepth of %d\n", activeDS.sane_param.depth);
494 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.sane_param.depth);
495 break;
497 #endif
498 return twCC;
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");
508 switch (action)
510 case MSG_QUERYSUPPORT:
511 twCC = set_onevalue(pCapability, TWTY_INT32, TWQC_GET);
512 break;
514 case MSG_GET:
515 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
516 break;
519 return twCC;
522 /* ICAP_COMPRESSION */
523 static TW_UINT16 SANE_ICAPCompression (pTW_CAPABILITY pCapability, TW_UINT16 action)
525 static const TW_UINT32 possible_values[] = { TWCP_NONE };
526 TW_UINT32 val;
527 TW_UINT16 twCC = TWCC_BADCAP;
529 TRACE("ICAP_COMPRESSION\n");
531 switch (action)
533 case MSG_QUERYSUPPORT:
534 twCC = set_onevalue(pCapability, TWTY_INT32,
535 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
536 break;
538 case MSG_GET:
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");
542 break;
544 case MSG_SET:
545 twCC = msg_set(pCapability, &val);
546 if (twCC == TWCC_SUCCESS)
547 FIXME("Partial Stub: COMPRESSION set to %d, but ignored\n", val);
548 break;
550 case MSG_GETDEFAULT:
551 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
552 break;
554 case MSG_RESET:
555 /* .. fall through intentional .. */
557 case MSG_GETCURRENT:
558 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
559 break;
561 return twCC;
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
569 TW_UINT32 val;
570 SANE_Int current_resolution;
571 TW_FIX32 *default_res;
572 const char *best_option_name;
573 SANE_Int minval, maxval, quantval;
574 SANE_Status sane_rc;
575 SANE_Int set_status;
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;
585 else
587 best_option_name = "y-resolution";
588 default_res = &activeDS.defaultYResolution;
590 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, &current_resolution) != SANE_STATUS_GOOD)
592 best_option_name = "resolution";
593 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, &current_resolution) != SANE_STATUS_GOOD)
594 return TWCC_BADCAP;
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
599 * as the default */
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;
614 switch (action)
616 case MSG_QUERYSUPPORT:
617 twCC = set_onevalue(pCapability, TWTY_INT32,
618 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
619 break;
621 case MSG_GET:
622 sane_rc = sane_option_probe_resolution(activeDS.deviceHandle, best_option_name, &minval, &maxval, &quantval);
623 if (sane_rc != SANE_STATUS_GOOD)
624 twCC = TWCC_BADCAP;
625 else
626 twCC = msg_get_range(pCapability, TWTY_FIX32,
627 minval, maxval, quantval == 0 ? 1 : quantval, default_res->Whole, current_resolution);
628 break;
630 case MSG_SET:
631 twCC = msg_set(pCapability, &val);
632 if (twCC == TWCC_SUCCESS)
634 TW_FIX32 f32;
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);
640 twCC = TWCC_BADCAP;
642 else if (set_status == SANE_INFO_INEXACT)
643 twCC = TWCC_CHECKSTATUS;
645 break;
647 case MSG_GETDEFAULT:
648 twCC = set_onevalue(pCapability, TWTY_FIX32, default_res->Whole);
649 break;
651 case MSG_RESET:
652 sane_rc = sane_option_set_int(activeDS.deviceHandle, best_option_name, default_res->Whole, NULL);
653 if (sane_rc != SANE_STATUS_GOOD)
654 return TWCC_BADCAP;
656 /* .. fall through intentional .. */
658 case MSG_GETCURRENT:
659 twCC = set_onevalue(pCapability, TWTY_FIX32, current_resolution);
660 break;
662 #endif
663 return twCC;
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
671 TW_FIX32 res;
672 char option_name[64];
673 SANE_Fixed lower, upper;
674 SANE_Unit lowerunit, upperunit;
675 SANE_Status status;
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)
690 return TWCC_BADCAP;
692 if (! convert_sane_res_to_twain(SANE_UNFIX(upper) - SANE_UNFIX(lower), upperunit, &res, TWUN_INCHES))
693 return TWCC_BADCAP;
695 switch (action)
697 case MSG_QUERYSUPPORT:
698 twCC = set_onevalue(pCapability, TWTY_INT32,
699 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
700 break;
702 case MSG_GET:
703 case MSG_GETDEFAULT:
705 /* .. fall through intentional .. */
707 case MSG_GETCURRENT:
708 twCC = set_onevalue(pCapability, TWTY_FIX32, res.Whole | (res.Frac << 16));
709 break;
711 #endif
712 return twCC;
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 };
721 TW_UINT32 val;
722 TW_UINT32 flavor = activeDS.sane_param.depth == 1 ? TWPF_VANILLA : TWPF_CHOCOLATE;
724 TRACE("ICAP_PIXELFLAVOR\n");
726 switch (action)
728 case MSG_QUERYSUPPORT:
729 twCC = set_onevalue(pCapability, TWTY_INT32,
730 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
731 break;
733 case MSG_GET:
734 twCC = msg_get_enum(pCapability, possible_values, ARRAY_SIZE(possible_values),
735 TWTY_UINT16, flavor, flavor);
736 break;
738 case MSG_SET:
739 twCC = msg_set(pCapability, &val);
740 if (twCC == TWCC_SUCCESS)
742 FIXME("Stub: PIXELFLAVOR set to %d, but ignored\n", val);
744 break;
746 case MSG_GETDEFAULT:
747 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
748 break;
750 case MSG_RESET:
751 /* .. fall through intentional .. */
753 case MSG_GETCURRENT:
754 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
755 break;
757 #endif
758 return twCC;
761 #ifdef SONAME_LIBSANE
762 static TW_UINT16 get_width_height(double *width, double *height, BOOL max)
764 SANE_Status status;
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);
787 if (max)
788 *width = SANE_UNFIX(brx_max) - SANE_UNFIX(tlx_min);
789 else
790 *width = SANE_UNFIX(brx_current) - SANE_UNFIX(tlx_current);
792 if (max)
793 *height = SANE_UNFIX(bry_max) - SANE_UNFIX(tly_min);
794 else
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)
802 SANE_Status status;
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);
806 return TWCC_SUCCESS;
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)
814 return rc;
815 rc = set_one_coord("br-x", width);
816 if (rc != TWCC_SUCCESS)
817 return rc;
818 rc = set_one_coord("tl-y", 0);
819 if (rc != TWCC_SUCCESS)
820 return rc;
821 rc = set_one_coord("br-y", height);
823 return rc;
826 typedef struct
828 TW_UINT32 size;
829 double x;
830 double y;
831 } supported_size_t;
833 static const supported_size_t supported_sizes[] =
835 { TWSS_NONE, 0, 0 },
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)
850 DWORD paper;
851 int rc;
852 int defsize = -1;
853 double width, height;
854 int i;
855 rc = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER, (void *) &paper, sizeof(paper));
856 if (rc > 0)
857 switch (paper)
859 case 1:
860 defsize = TWSS_USLETTER;
861 break;
862 case 5:
863 defsize = TWSS_USLEGAL;
864 break;
865 case 8:
866 defsize = TWSS_A3;
867 break;
868 case 9:
869 defsize = TWSS_A4;
870 break;
873 if (defsize == -1)
874 return TWSS_NONE;
876 if (get_width_height(&width, &height, TRUE) != TWCC_SUCCESS)
877 return TWSS_NONE;
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))
884 return TWSS_NONE;
885 else
886 return s[i].size;
889 return TWSS_NONE;
892 static TW_UINT16 get_current_paper_size(const supported_size_t *s, int n)
894 int i;
895 double width, height;
896 double xdelta, ydelta;
898 if (get_width_height(&width, &height, FALSE) != TWCC_SUCCESS)
899 return TWSS_NONE;
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)
909 return s[i].size;
912 return TWSS_NONE;
914 #endif
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)];
923 unsigned int i;
924 TW_UINT32 val;
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");
930 switch (action)
932 case MSG_QUERYSUPPORT:
933 twCC = set_onevalue(pCapability, TWTY_INT32,
934 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
935 break;
937 case MSG_GET:
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");
943 break;
945 case MSG_SET:
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);
953 twCC = TWCC_BADCAP;
954 break;
956 case MSG_GETDEFAULT:
957 twCC = set_onevalue(pCapability, TWTY_UINT16, default_size);
958 break;
960 case MSG_RESET:
961 twCC = TWCC_BADCAP;
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);
966 break;
968 if (twCC != TWCC_SUCCESS)
969 return twCC;
971 /* .. fall through intentional .. */
973 case MSG_GETCURRENT:
974 twCC = set_onevalue(pCapability, TWTY_UINT16, current_size);
975 break;
978 #endif
979 return twCC;
982 /* CAP_AUTOFEED */
983 static TW_UINT16 SANE_CAPAutofeed (pTW_CAPABILITY pCapability, TW_UINT16 action)
985 TW_UINT16 twCC = TWCC_BADCAP;
986 #ifdef SONAME_LIBSANE
987 TW_UINT32 val;
988 SANE_Bool autofeed;
989 SANE_Status status;
991 TRACE("CAP_AUTOFEED\n");
993 if (sane_option_get_bool(activeDS.deviceHandle, "batch-scan", &autofeed, NULL) != SANE_STATUS_GOOD)
994 return TWCC_BADCAP;
996 switch (action)
998 case MSG_QUERYSUPPORT:
999 twCC = set_onevalue(pCapability, TWTY_INT32,
1000 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1001 break;
1003 case MSG_GET:
1004 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1005 break;
1007 case MSG_SET:
1008 twCC = msg_set(pCapability, &val);
1009 if (twCC == TWCC_SUCCESS)
1011 if (val)
1012 autofeed = SANE_TRUE;
1013 else
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);
1023 break;
1025 case MSG_GETDEFAULT:
1026 twCC = set_onevalue(pCapability, TWTY_BOOL, SANE_TRUE);
1027 break;
1029 case MSG_RESET:
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);
1041 break;
1043 #endif
1044 return twCC;
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
1052 TW_UINT32 val;
1053 TW_BOOL enabled;
1054 SANE_Status status;
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)
1060 return TWCC_BADCAP;
1062 if (strcmp(source, "Auto") == 0 || strcmp(source, "ADF") == 0)
1063 enabled = TRUE;
1064 else
1065 enabled = FALSE;
1067 switch (action)
1069 case MSG_QUERYSUPPORT:
1070 twCC = set_onevalue(pCapability, TWTY_INT32,
1071 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1072 break;
1074 case MSG_GET:
1075 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1076 break;
1078 case MSG_SET:
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);
1095 break;
1097 case MSG_GETDEFAULT:
1098 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
1099 break;
1101 case MSG_RESET:
1102 strcpy(source, "Auto");
1103 if (sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL) == SANE_STATUS_GOOD)
1104 enabled = TRUE;
1105 /* .. fall through intentional .. */
1107 case MSG_GETCURRENT:
1108 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1109 break;
1111 #endif
1112 return twCC;
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);
1128 else
1129 twCC = TWCC_BADVALUE;
1130 break;
1132 case CAP_XFERCOUNT:
1133 twCC = SANE_CAPXferCount (pCapability, action);
1134 break;
1136 case CAP_UICONTROLLABLE:
1137 twCC = SANE_CAPUiControllable (pCapability, action);
1138 break;
1140 case CAP_AUTOFEED:
1141 twCC = SANE_CAPAutofeed (pCapability, action);
1142 break;
1144 case CAP_FEEDERENABLED:
1145 twCC = SANE_CAPFeederEnabled (pCapability, action);
1146 break;
1148 case ICAP_PIXELTYPE:
1149 twCC = SANE_ICAPPixelType (pCapability, action);
1150 break;
1152 case ICAP_UNITS:
1153 twCC = SANE_ICAPUnits (pCapability, action);
1154 break;
1156 case ICAP_BITDEPTH:
1157 twCC = SANE_ICAPBitDepth(pCapability, action);
1158 break;
1160 case ICAP_XFERMECH:
1161 twCC = SANE_ICAPXferMech (pCapability, action);
1162 break;
1164 case ICAP_PIXELFLAVOR:
1165 twCC = SANE_ICAPPixelFlavor (pCapability, action);
1166 break;
1168 case ICAP_COMPRESSION:
1169 twCC = SANE_ICAPCompression(pCapability, action);
1170 break;
1172 case ICAP_XRESOLUTION:
1173 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1174 break;
1176 case ICAP_YRESOLUTION:
1177 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1178 break;
1180 case ICAP_PHYSICALHEIGHT:
1181 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1182 break;
1184 case ICAP_PHYSICALWIDTH:
1185 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1186 break;
1188 case ICAP_SUPPORTEDSIZES:
1189 twCC = SANE_ICAPSupportedSizes (pCapability, action);
1190 break;
1192 case ICAP_PLANARCHUNKY:
1193 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1194 break;
1196 case ICAP_BITORDER:
1197 FIXME("ICAP_BITORDER not implemented\n");
1198 break;
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);
1210 return twCC;
1213 #ifdef SONAME_LIBSANE
1214 TW_UINT16 SANE_SaneSetDefaults (void)
1216 TW_CAPABILITY cap;
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;
1241 #endif