msvfw32: Fix the size of previous compressed buffer.
[wine.git] / dlls / sane.ds / capability.c
blob7d3376f63e0b272dae1fdcc1546c3235a281f760
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 = 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);
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, sizeof(possible_values) / sizeof(possible_values[0]),
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, sizeof(possible_values) / sizeof(possible_values[0]),
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, 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");
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, sizeof(possible_values) / sizeof(possible_values[0]),
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 },
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)
851 DWORD paper;
852 int rc;
853 int defsize = -1;
854 double width, height;
855 int i;
856 rc = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER, (void *) &paper, sizeof(paper));
857 if (rc > 0)
858 switch (paper)
860 case 1:
861 defsize = TWSS_USLETTER;
862 break;
863 case 5:
864 defsize = TWSS_USLEGAL;
865 break;
866 case 8:
867 defsize = TWSS_A3;
868 break;
869 case 9:
870 defsize = TWSS_A4;
871 break;
874 if (defsize == -1)
875 return TWSS_NONE;
877 if (get_width_height(&width, &height, TRUE) != TWCC_SUCCESS)
878 return TWSS_NONE;
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))
885 return TWSS_NONE;
886 else
887 return s[i].size;
890 return TWSS_NONE;
893 static TW_UINT16 get_current_paper_size(const supported_size_t *s, int n)
895 int i;
896 double width, height;
897 double xdelta, ydelta;
899 if (get_width_height(&width, &height, FALSE) != TWCC_SUCCESS)
900 return TWSS_NONE;
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)
910 return s[i].size;
913 return TWSS_NONE;
915 #endif
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];
924 unsigned int i;
925 TW_UINT32 val;
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");
931 switch (action)
933 case MSG_QUERYSUPPORT:
934 twCC = set_onevalue(pCapability, TWTY_INT32,
935 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
936 break;
938 case MSG_GET:
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");
944 break;
946 case MSG_SET:
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);
954 twCC = TWCC_BADCAP;
955 break;
957 case MSG_GETDEFAULT:
958 twCC = set_onevalue(pCapability, TWTY_UINT16, default_size);
959 break;
961 case MSG_RESET:
962 twCC = TWCC_BADCAP;
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);
967 break;
969 if (twCC != TWCC_SUCCESS)
970 return twCC;
972 /* .. fall through intentional .. */
974 case MSG_GETCURRENT:
975 twCC = set_onevalue(pCapability, TWTY_UINT16, current_size);
976 break;
979 #undef SUPPORTED_SIZE_COUNT
980 #endif
981 return twCC;
984 /* CAP_AUTOFEED */
985 static TW_UINT16 SANE_CAPAutofeed (pTW_CAPABILITY pCapability, TW_UINT16 action)
987 TW_UINT16 twCC = TWCC_BADCAP;
988 #ifdef SONAME_LIBSANE
989 TW_UINT32 val;
990 SANE_Bool autofeed;
991 SANE_Status status;
993 TRACE("CAP_AUTOFEED\n");
995 if (sane_option_get_bool(activeDS.deviceHandle, "batch-scan", &autofeed, NULL) != SANE_STATUS_GOOD)
996 return TWCC_BADCAP;
998 switch (action)
1000 case MSG_QUERYSUPPORT:
1001 twCC = set_onevalue(pCapability, TWTY_INT32,
1002 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1003 break;
1005 case MSG_GET:
1006 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1007 break;
1009 case MSG_SET:
1010 twCC = msg_set(pCapability, &val);
1011 if (twCC == TWCC_SUCCESS)
1013 if (val)
1014 autofeed = SANE_TRUE;
1015 else
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);
1025 break;
1027 case MSG_GETDEFAULT:
1028 twCC = set_onevalue(pCapability, TWTY_BOOL, SANE_TRUE);
1029 break;
1031 case MSG_RESET:
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);
1043 break;
1045 #endif
1046 return twCC;
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
1054 TW_UINT32 val;
1055 TW_BOOL enabled;
1056 SANE_Status status;
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)
1062 return TWCC_BADCAP;
1064 if (strcmp(source, "Auto") == 0 || strcmp(source, "ADF") == 0)
1065 enabled = TRUE;
1066 else
1067 enabled = FALSE;
1069 switch (action)
1071 case MSG_QUERYSUPPORT:
1072 twCC = set_onevalue(pCapability, TWTY_INT32,
1073 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1074 break;
1076 case MSG_GET:
1077 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1078 break;
1080 case MSG_SET:
1081 twCC = msg_set(pCapability, &val);
1082 if (twCC == TWCC_SUCCESS)
1084 if (val)
1085 enabled = TRUE;
1086 else
1087 enabled = FALSE;
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);
1102 break;
1104 case MSG_GETDEFAULT:
1105 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
1106 break;
1108 case MSG_RESET:
1109 strcpy(source, "Auto");
1110 if (sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL) == SANE_STATUS_GOOD)
1111 enabled = TRUE;
1112 twCC = TWCC_SUCCESS;
1113 /* .. fall through intentional .. */
1115 case MSG_GETCURRENT:
1116 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1117 break;
1119 #endif
1120 return twCC;
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);
1136 else
1137 twCC = TWCC_BADVALUE;
1138 break;
1140 case CAP_XFERCOUNT:
1141 twCC = SANE_CAPXferCount (pCapability, action);
1142 break;
1144 case CAP_UICONTROLLABLE:
1145 twCC = SANE_CAPUiControllable (pCapability, action);
1146 break;
1148 case CAP_AUTOFEED:
1149 twCC = SANE_CAPAutofeed (pCapability, action);
1150 break;
1152 case CAP_FEEDERENABLED:
1153 twCC = SANE_CAPFeederEnabled (pCapability, action);
1154 break;
1156 case ICAP_PIXELTYPE:
1157 twCC = SANE_ICAPPixelType (pCapability, action);
1158 break;
1160 case ICAP_UNITS:
1161 twCC = SANE_ICAPUnits (pCapability, action);
1162 break;
1164 case ICAP_BITDEPTH:
1165 twCC = SANE_ICAPBitDepth(pCapability, action);
1166 break;
1168 case ICAP_XFERMECH:
1169 twCC = SANE_ICAPXferMech (pCapability, action);
1170 break;
1172 case ICAP_PIXELFLAVOR:
1173 twCC = SANE_ICAPPixelFlavor (pCapability, action);
1174 break;
1176 case ICAP_COMPRESSION:
1177 twCC = SANE_ICAPCompression(pCapability, action);
1178 break;
1180 case ICAP_XRESOLUTION:
1181 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1182 break;
1184 case ICAP_YRESOLUTION:
1185 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1186 break;
1188 case ICAP_PHYSICALHEIGHT:
1189 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1190 break;
1192 case ICAP_PHYSICALWIDTH:
1193 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1194 break;
1196 case ICAP_SUPPORTEDSIZES:
1197 twCC = SANE_ICAPSupportedSizes (pCapability, action);
1198 break;
1200 case ICAP_PLANARCHUNKY:
1201 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1202 break;
1204 case ICAP_BITORDER:
1205 FIXME("ICAP_BITORDER not implemented\n");
1206 break;
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);
1218 return twCC;
1221 #ifdef SONAME_LIBSANE
1222 TW_UINT16 SANE_SaneSetDefaults (void)
1224 TW_CAPABILITY cap;
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;
1249 #endif