gdiplus: Implement LineCapSquare in GdipWidenPath.
[wine/multimedia.git] / dlls / sane.ds / capability.c
blob138122063a1552da5a1d829ed5b8c61243ca7c88
1 /*
2 * Copyright 2000 Corel Corporation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
22 #include "config.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <math.h>
28 #include "sane_i.h"
29 #include "winnls.h"
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")
36 #endif
37 #ifndef SANE_VALUE_SCAN_MODE_GRAY
38 #define SANE_VALUE_SCAN_MODE_GRAY SANE_I18N("Gray")
39 #endif
40 #ifndef SANE_VALUE_SCAN_MODE_LINEART
41 #define SANE_VALUE_SCAN_MODE_LINEART SANE_I18N("Lineart")
42 #endif
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);
49 if (pVal)
51 *value = pVal->Item;
52 if (type)
53 *type = pVal->ItemType;
54 GlobalUnlock (pCapability->hContainer);
55 return TWCC_SUCCESS;
58 return TWCC_BUMMER;
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);
69 if (pVal)
71 pCapability->ConType = TWON_ONEVALUE;
72 pVal->ItemType = type;
73 pVal->Item = value;
74 GlobalUnlock (pCapability->hContainer);
75 return TWCC_SUCCESS;
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");
87 return TWCC_BADCAP;
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;
95 TW_UINT32 *p32;
96 TW_UINT16 *p16;
97 int i;
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);
111 if (! enumv)
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)
126 p16[i] = values[i];
127 if (type == TWTY_INT32 || type == TWTY_UINT32)
128 p32[i] = values[i];
131 GlobalUnlock(pCapability->hContainer);
132 return TWCC_SUCCESS;
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);
148 if (! range)
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);
159 return TWCC_SUCCESS;
161 #endif
163 static TW_UINT16 TWAIN_GetSupportedCaps(pTW_CAPABILITY pCapability)
165 TW_ARRAY *a;
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)
176 UINT16 *u;
177 int i;
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);
185 return TWCC_SUCCESS;
187 else
188 return TWCC_LOWMEMORY;
192 /* ICAP_XFERMECH */
193 static TW_UINT16 SANE_ICAPXferMech (pTW_CAPABILITY pCapability, TW_UINT16 action)
195 static const TW_UINT32 possible_values[] = { TWSX_NATIVE, TWSX_MEMORY };
196 TW_UINT32 val;
197 TW_UINT16 twCC = TWCC_BADCAP;
199 TRACE("ICAP_XFERMECH\n");
201 switch (action)
203 case MSG_QUERYSUPPORT:
204 twCC = set_onevalue(pCapability, TWTY_INT32,
205 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
206 break;
208 case MSG_GET:
209 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
210 TWTY_UINT16, activeDS.capXferMech, TWSX_NATIVE);
211 break;
213 case MSG_SET:
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);
220 break;
222 case MSG_GETDEFAULT:
223 twCC = set_onevalue(pCapability, TWTY_UINT16, TWSX_NATIVE);
224 break;
226 case MSG_RESET:
227 activeDS.capXferMech = TWSX_NATIVE;
228 /* .. fall through intentional .. */
230 case MSG_GETCURRENT:
231 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.capXferMech);
232 FIXME("Partial Stub: XFERMECH of %d not actually used\n", activeDS.capXferMech);
233 break;
235 return twCC;
239 /* CAP_XFERCOUNT */
240 static TW_UINT16 SANE_CAPXferCount (pTW_CAPABILITY pCapability, TW_UINT16 action)
242 TW_UINT32 val;
243 TW_UINT16 twCC = TWCC_BADCAP;
245 TRACE("CAP_XFERCOUNT\n");
247 switch (action)
249 case MSG_QUERYSUPPORT:
250 twCC = set_onevalue(pCapability, TWTY_INT32,
251 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
252 break;
254 case MSG_GET:
255 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
256 FIXME("Partial Stub: Reporting only support for transfer all\n");
257 break;
259 case MSG_SET:
260 twCC = msg_set(pCapability, &val);
261 if (twCC == TWCC_SUCCESS)
262 FIXME("Partial Stub: XFERCOUNT set to %d, but ignored\n", val);
263 break;
265 case MSG_GETDEFAULT:
266 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
267 break;
269 case MSG_RESET:
270 /* .. fall through intentional .. */
272 case MSG_GETCURRENT:
273 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
274 break;
276 return twCC;
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;
283 switch (pixeltype)
285 case TWPT_GRAY:
286 m = SANE_VALUE_SCAN_MODE_GRAY;
287 break;
288 case TWPT_RGB:
289 m = SANE_VALUE_SCAN_MODE_COLOR;
290 break;
291 case TWPT_BW:
292 m = SANE_VALUE_SCAN_MODE_LINEART;
293 break;
295 if (! m)
296 return FALSE;
297 if (strlen(m) >= len)
298 return FALSE;
299 strcpy(mode, m);
300 return TRUE;
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;
310 else
311 return FALSE;
313 return TRUE;
315 #endif
317 /* ICAP_PIXELTYPE */
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;
324 TW_UINT32 val;
325 SANE_Status rc;
326 SANE_Int status;
327 SANE_String_Const *choices;
328 char current_mode[64];
329 TW_UINT16 current_pixeltype = TWPT_BW;
330 SANE_Char mode[64];
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");
338 return twCC;
341 sane_mode_to_pixeltype(current_mode, &current_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;
351 switch (action)
353 case MSG_QUERYSUPPORT:
354 twCC = set_onevalue(pCapability, TWTY_INT32,
355 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
356 break;
358 case MSG_GET:
359 for (possible_value_count = 0; choices && *choices && possible_value_count < 3; choices++)
361 TW_UINT16 pix;
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);
367 break;
369 case MSG_SET:
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;
377 status = 0;
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);
390 break;
392 case MSG_GETDEFAULT:
393 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.defaultPixelType);
394 break;
396 case MSG_RESET:
397 current_pixeltype = activeDS.defaultPixelType;
398 if (! pixeltype_to_sane_mode(current_pixeltype, mode, sizeof(mode)))
399 return TWCC_BADVALUE;
401 status = 0;
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 .. */
416 case MSG_GETCURRENT:
417 twCC = set_onevalue(pCapability, TWTY_UINT16, current_pixeltype);
418 TRACE("Returning current pixeltype of %d\n", current_pixeltype);
419 break;
422 #endif
423 return twCC;
426 /* ICAP_UNITS */
427 static TW_UINT16 SANE_ICAPUnits (pTW_CAPABILITY pCapability, TW_UINT16 action)
429 TW_UINT32 val;
430 TW_UINT16 twCC = TWCC_BADCAP;
432 TRACE("ICAP_UNITS\n");
434 switch (action)
436 case MSG_QUERYSUPPORT:
437 twCC = set_onevalue(pCapability, TWTY_INT32,
438 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
439 break;
441 case MSG_GET:
442 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
443 break;
445 case MSG_SET:
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;
455 break;
457 case MSG_GETDEFAULT:
458 case MSG_RESET:
459 /* .. fall through intentional .. */
461 case MSG_GETCURRENT:
462 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
463 break;
466 return twCC;
469 /* ICAP_BITDEPTH */
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;
480 switch (action)
482 case MSG_QUERYSUPPORT:
483 twCC = set_onevalue(pCapability, TWTY_INT32,
484 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
485 break;
487 case MSG_GET:
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);
490 break;
492 case MSG_GETDEFAULT:
493 /* .. Fall through intentional .. */
495 case MSG_GETCURRENT:
496 TRACE("Returning current bitdepth of %d\n", activeDS.sane_param.depth);
497 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.sane_param.depth);
498 break;
500 #endif
501 return twCC;
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");
511 switch (action)
513 case MSG_QUERYSUPPORT:
514 twCC = set_onevalue(pCapability, TWTY_INT32, TWQC_GET);
515 break;
517 case MSG_GET:
518 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
519 break;
522 return twCC;
525 /* ICAP_COMPRESSION */
526 static TW_UINT16 SANE_ICAPCompression (pTW_CAPABILITY pCapability, TW_UINT16 action)
528 static const TW_UINT32 possible_values[] = { TWCP_NONE };
529 TW_UINT32 val;
530 TW_UINT16 twCC = TWCC_BADCAP;
532 TRACE("ICAP_COMPRESSION\n");
534 switch (action)
536 case MSG_QUERYSUPPORT:
537 twCC = set_onevalue(pCapability, TWTY_INT32,
538 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
539 break;
541 case MSG_GET:
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");
545 break;
547 case MSG_SET:
548 twCC = msg_set(pCapability, &val);
549 if (twCC == TWCC_SUCCESS)
550 FIXME("Partial Stub: COMPRESSION set to %d, but ignored\n", val);
551 break;
553 case MSG_GETDEFAULT:
554 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
555 break;
557 case MSG_RESET:
558 /* .. fall through intentional .. */
560 case MSG_GETCURRENT:
561 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
562 break;
564 return twCC;
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
572 TW_UINT32 val;
573 SANE_Int current_resolution;
574 TW_FIX32 *default_res;
575 const char *best_option_name;
576 SANE_Int minval, maxval, quantval;
577 SANE_Status sane_rc;
578 SANE_Int set_status;
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;
588 else
590 best_option_name = "y-resolution";
591 default_res = &activeDS.defaultYResolution;
593 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, &current_resolution) != SANE_STATUS_GOOD)
595 best_option_name = "resolution";
596 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, &current_resolution) != SANE_STATUS_GOOD)
597 return TWCC_BADCAP;
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
602 * as the default */
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;
617 switch (action)
619 case MSG_QUERYSUPPORT:
620 twCC = set_onevalue(pCapability, TWTY_INT32,
621 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
622 break;
624 case MSG_GET:
625 sane_rc = sane_option_probe_resolution(activeDS.deviceHandle, best_option_name, &minval, &maxval, &quantval);
626 if (sane_rc != SANE_STATUS_GOOD)
627 twCC = TWCC_BADCAP;
628 else
629 twCC = msg_get_range(pCapability, TWTY_FIX32,
630 minval, maxval, quantval == 0 ? 1 : quantval, default_res->Whole, current_resolution);
631 break;
633 case MSG_SET:
634 twCC = msg_set(pCapability, &val);
635 if (twCC == TWCC_SUCCESS)
637 TW_FIX32 f32;
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);
643 twCC = TWCC_BADCAP;
645 else if (set_status == SANE_INFO_INEXACT)
646 twCC = TWCC_CHECKSTATUS;
648 break;
650 case MSG_GETDEFAULT:
651 twCC = set_onevalue(pCapability, TWTY_FIX32, default_res->Whole);
652 break;
654 case MSG_RESET:
655 sane_rc = sane_option_set_int(activeDS.deviceHandle, best_option_name, default_res->Whole, NULL);
656 if (sane_rc != SANE_STATUS_GOOD)
657 return TWCC_BADCAP;
659 /* .. fall through intentional .. */
661 case MSG_GETCURRENT:
662 twCC = set_onevalue(pCapability, TWTY_FIX32, current_resolution);
663 break;
665 #endif
666 return twCC;
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
674 TW_FIX32 res;
675 char option_name[64];
676 SANE_Fixed lower, upper;
677 SANE_Unit lowerunit, upperunit;
678 SANE_Status status;
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)
693 return TWCC_BADCAP;
695 if (! convert_sane_res_to_twain(SANE_UNFIX(upper) - SANE_UNFIX(lower), upperunit, &res, TWUN_INCHES))
696 return TWCC_BADCAP;
698 switch (action)
700 case MSG_QUERYSUPPORT:
701 twCC = set_onevalue(pCapability, TWTY_INT32,
702 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
703 break;
705 case MSG_GET:
706 case MSG_GETDEFAULT:
708 /* .. fall through intentional .. */
710 case MSG_GETCURRENT:
711 twCC = set_onevalue(pCapability, TWTY_FIX32, res.Whole | (res.Frac << 16));
712 break;
714 #endif
715 return twCC;
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 };
724 TW_UINT32 val;
725 TW_UINT32 flavor = activeDS.sane_param.depth == 1 ? TWPF_VANILLA : TWPF_CHOCOLATE;
727 TRACE("ICAP_PIXELFLAVOR\n");
729 switch (action)
731 case MSG_QUERYSUPPORT:
732 twCC = set_onevalue(pCapability, TWTY_INT32,
733 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
734 break;
736 case MSG_GET:
737 twCC = msg_get_enum(pCapability, possible_values, sizeof(possible_values) / sizeof(possible_values[0]),
738 TWTY_UINT16, flavor, flavor);
739 break;
741 case MSG_SET:
742 twCC = msg_set(pCapability, &val);
743 if (twCC == TWCC_SUCCESS)
745 FIXME("Stub: PIXELFLAVOR set to %d, but ignored\n", val);
747 break;
749 case MSG_GETDEFAULT:
750 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
751 break;
753 case MSG_RESET:
754 /* .. fall through intentional .. */
756 case MSG_GETCURRENT:
757 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
758 break;
760 #endif
761 return twCC;
764 #ifdef SONAME_LIBSANE
765 static TW_UINT16 get_width_height(double *width, double *height, BOOL max)
767 SANE_Status status;
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);
790 if (max)
791 *width = SANE_UNFIX(brx_max) - SANE_UNFIX(tlx_min);
792 else
793 *width = SANE_UNFIX(brx_current) - SANE_UNFIX(tlx_current);
795 if (max)
796 *height = SANE_UNFIX(bry_max) - SANE_UNFIX(tly_min);
797 else
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)
805 SANE_Status status;
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);
809 return TWCC_SUCCESS;
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)
817 return rc;
818 rc = set_one_coord("br-x", width);
819 if (rc != TWCC_SUCCESS)
820 return rc;
821 rc = set_one_coord("tl-y", 0);
822 if (rc != TWCC_SUCCESS)
823 return rc;
824 rc = set_one_coord("br-y", height);
826 return rc;
829 typedef struct
831 TW_UINT32 size;
832 double x;
833 double y;
834 } supported_size_t;
836 static const supported_size_t supported_sizes[] =
838 { TWSS_NONE, 0, 0 },
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)
854 DWORD paper;
855 int rc;
856 int defsize = -1;
857 double width, height;
858 int i;
859 rc = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER, (void *) &paper, sizeof(paper));
860 if (rc > 0)
861 switch (paper)
863 case 1:
864 defsize = TWSS_USLETTER;
865 break;
866 case 5:
867 defsize = TWSS_USLEGAL;
868 break;
869 case 8:
870 defsize = TWSS_A3;
871 break;
872 case 9:
873 defsize = TWSS_A4;
874 break;
877 if (defsize == -1)
878 return TWSS_NONE;
880 if (get_width_height(&width, &height, TRUE) != TWCC_SUCCESS)
881 return TWSS_NONE;
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))
888 return TWSS_NONE;
889 else
890 return s[i].size;
893 return TWSS_NONE;
896 static TW_UINT16 get_current_paper_size(const supported_size_t *s, int n)
898 int i;
899 double width, height;
900 double xdelta, ydelta;
902 if (get_width_height(&width, &height, FALSE) != TWCC_SUCCESS)
903 return TWSS_NONE;
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)
913 return s[i].size;
916 return TWSS_NONE;
918 #endif
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];
927 int i;
928 TW_UINT32 val;
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");
934 switch (action)
936 case MSG_QUERYSUPPORT:
937 twCC = set_onevalue(pCapability, TWTY_INT32,
938 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
939 break;
941 case MSG_GET:
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");
947 break;
949 case MSG_SET:
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);
957 twCC = TWCC_BADCAP;
958 break;
960 case MSG_GETDEFAULT:
961 twCC = set_onevalue(pCapability, TWTY_UINT16, default_size);
962 break;
964 case MSG_RESET:
965 twCC = TWCC_BADCAP;
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);
970 break;
972 if (twCC != TWCC_SUCCESS)
973 return twCC;
975 /* .. fall through intentional .. */
977 case MSG_GETCURRENT:
978 twCC = set_onevalue(pCapability, TWTY_UINT16, current_size);
979 break;
982 #undef SUPPORTED_SIZE_COUNT
983 #endif
984 return twCC;
987 /* CAP_AUTOFEED */
988 static TW_UINT16 SANE_CAPAutofeed (pTW_CAPABILITY pCapability, TW_UINT16 action)
990 TW_UINT16 twCC = TWCC_BADCAP;
991 #ifdef SONAME_LIBSANE
992 TW_UINT32 val;
993 SANE_Bool autofeed;
994 SANE_Status status;
996 TRACE("CAP_AUTOFEED\n");
998 if (sane_option_get_bool(activeDS.deviceHandle, "batch-scan", &autofeed, NULL) != SANE_STATUS_GOOD)
999 return TWCC_BADCAP;
1001 switch (action)
1003 case MSG_QUERYSUPPORT:
1004 twCC = set_onevalue(pCapability, TWTY_INT32,
1005 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1006 break;
1008 case MSG_GET:
1009 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1010 break;
1012 case MSG_SET:
1013 twCC = msg_set(pCapability, &val);
1014 if (twCC == TWCC_SUCCESS)
1016 if (val)
1017 autofeed = SANE_TRUE;
1018 else
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);
1028 break;
1030 case MSG_GETDEFAULT:
1031 twCC = set_onevalue(pCapability, TWTY_BOOL, SANE_TRUE);
1032 break;
1034 case MSG_RESET:
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);
1046 break;
1048 #endif
1049 return twCC;
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
1057 TW_UINT32 val;
1058 TW_BOOL enabled;
1059 SANE_Status status;
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)
1065 return TWCC_BADCAP;
1067 if (strcmp(source, "Auto") == 0 || strcmp(source, "ADF") == 0)
1068 enabled = TRUE;
1069 else
1070 enabled = FALSE;
1072 switch (action)
1074 case MSG_QUERYSUPPORT:
1075 twCC = set_onevalue(pCapability, TWTY_INT32,
1076 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1077 break;
1079 case MSG_GET:
1080 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1081 break;
1083 case MSG_SET:
1084 twCC = msg_set(pCapability, &val);
1085 if (twCC == TWCC_SUCCESS)
1087 if (val)
1088 enabled = TRUE;
1089 else
1090 enabled = FALSE;
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);
1105 break;
1107 case MSG_GETDEFAULT:
1108 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
1109 break;
1111 case MSG_RESET:
1112 strcpy(source, "Auto");
1113 if (sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL) == SANE_STATUS_GOOD)
1114 enabled = TRUE;
1115 twCC = TWCC_SUCCESS;
1116 /* .. fall through intentional .. */
1118 case MSG_GETCURRENT:
1119 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1120 break;
1122 #endif
1123 return twCC;
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);
1139 else
1140 twCC = TWCC_BADVALUE;
1141 break;
1143 case CAP_XFERCOUNT:
1144 twCC = SANE_CAPXferCount (pCapability, action);
1145 break;
1147 case CAP_UICONTROLLABLE:
1148 twCC = SANE_CAPUiControllable (pCapability, action);
1149 break;
1151 case CAP_AUTOFEED:
1152 twCC = SANE_CAPAutofeed (pCapability, action);
1153 break;
1155 case CAP_FEEDERENABLED:
1156 twCC = SANE_CAPFeederEnabled (pCapability, action);
1157 break;
1159 case ICAP_PIXELTYPE:
1160 twCC = SANE_ICAPPixelType (pCapability, action);
1161 break;
1163 case ICAP_UNITS:
1164 twCC = SANE_ICAPUnits (pCapability, action);
1165 break;
1167 case ICAP_BITDEPTH:
1168 twCC = SANE_ICAPBitDepth(pCapability, action);
1169 break;
1171 case ICAP_XFERMECH:
1172 twCC = SANE_ICAPXferMech (pCapability, action);
1173 break;
1175 case ICAP_PIXELFLAVOR:
1176 twCC = SANE_ICAPPixelFlavor (pCapability, action);
1177 break;
1179 case ICAP_COMPRESSION:
1180 twCC = SANE_ICAPCompression(pCapability, action);
1181 break;
1183 case ICAP_XRESOLUTION:
1184 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1185 break;
1187 case ICAP_YRESOLUTION:
1188 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1189 break;
1191 case ICAP_PHYSICALHEIGHT:
1192 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1193 break;
1195 case ICAP_PHYSICALWIDTH:
1196 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1197 break;
1199 case ICAP_SUPPORTEDSIZES:
1200 twCC = SANE_ICAPSupportedSizes (pCapability, action);
1201 break;
1203 case ICAP_PLANARCHUNKY:
1204 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1205 break;
1207 case ICAP_BITORDER:
1208 FIXME("ICAP_BITORDER not implemented\n");
1209 break;
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);
1221 return twCC;
1224 TW_UINT16 SANE_SaneSetDefaults (void)
1226 TW_CAPABILITY cap;
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;