1 /*
2 * Copyright 2000 Corel Corporation
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
21
22 #include "config.h"
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <math.h>
27
28 #include "sane_i.h"
29 #include "winnls.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(twain);
33
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
43
44 static TW_UINT16 get_onevalue(pTW_CAPABILITY pCapability, TW_UINT16 *type, TW_UINT32 *value)
45 {
46 if (pCapability->hContainer)
47 {
48 pTW_ONEVALUE pVal = GlobalLock (pCapability->hContainer);
49 if (pVal)
50 {
51 *value = pVal->Item;
52 if (type)
53 *type = pVal->ItemType;
54 GlobalUnlock (pCapability->hContainer);
55 return TWCC_SUCCESS;
56 }
57 }
58 return TWCC_BUMMER;
59 }
60
61
62 static TW_UINT16 set_onevalue(pTW_CAPABILITY pCapability, TW_UINT16 type, TW_UINT32 value)
63 {
64 pCapability->hContainer = GlobalAlloc (0, sizeof(TW_ONEVALUE));
65
66 if (pCapability->hContainer)
67 {
68 pTW_ONEVALUE pVal = GlobalLock (pCapability->hContainer);
69 if (pVal)
70 {
71 pCapability->ConType = TWON_ONEVALUE;
72 pVal->ItemType = type;
73 pVal->Item = value;
74 GlobalUnlock (pCapability->hContainer);
75 return TWCC_SUCCESS;
76 }
77 }
78 return TWCC_LOWMEMORY;
79 }
80
81 static TW_UINT16 msg_set(pTW_CAPABILITY pCapability, TW_UINT32 *val)
82 {
83 if (pCapability->ConType == TWON_ONEVALUE)
84 return get_onevalue(pCapability, NULL, val);
85
86 FIXME("Partial Stub: MSG_SET only supports TW_ONEVALUE\n");
87 return TWCC_BADCAP;
88 }
89
90
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)
93 {
94 TW_ENUMERATION *enumv = NULL;
95 TW_UINT32 *p32;
96 TW_UINT16 *p16;
97 int i;
98
99 pCapability->ConType = TWON_ENUMERATION;
100 pCapability->hContainer = 0;
101
102 if (type == TWTY_INT16 || type == TWTY_UINT16)
103 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION, ItemList[value_count * sizeof(TW_UINT16)]));
104
105 if (type == TWTY_INT32 || type == TWTY_UINT32)
106 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ENUMERATION, ItemList[value_count * sizeof(TW_UINT32)]));
107
108 if (pCapability->hContainer)
109 enumv = GlobalLock(pCapability->hContainer);
110
111 if (! enumv)
112 return TWCC_LOWMEMORY;
113
114 enumv->ItemType = type;
115 enumv->NumItems = value_count;
116
117 p16 = (TW_UINT16 *) enumv->ItemList;
118 p32 = (TW_UINT32 *) enumv->ItemList;
119 for (i = 0; i < value_count; i++)
120 {
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];
129 }
130
131 GlobalUnlock(pCapability->hContainer);
132 return TWCC_SUCCESS;
133 }
134
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)
138 {
139 TW_RANGE *range = NULL;
140
141 pCapability->ConType = TWON_RANGE;
142 pCapability->hContainer = 0;
143
144 pCapability->hContainer = GlobalAlloc (0, sizeof(*range));
145 if (pCapability->hContainer)
146 range = GlobalLock(pCapability->hContainer);
147
148 if (! range)
149 return TWCC_LOWMEMORY;
150
151 range->ItemType = type;
152 range->MinValue = minval;
153 range->MaxValue = maxval;
154 range->StepSize = step;
155 range->DefaultValue = def;
156 range->CurrentValue = current;
157
158 GlobalUnlock(pCapability->hContainer);
159 return TWCC_SUCCESS;
160 }
161 #endif
162
163 static TW_UINT16 TWAIN_GetSupportedCaps(pTW_CAPABILITY pCapability)
164 {
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 };
170
171 pCapability->hContainer = GlobalAlloc (0, FIELD_OFFSET( TW_ARRAY, ItemList[sizeof(supported_caps)] ));
172 pCapability->ConType = TWON_ARRAY;
173
174 if (pCapability->hContainer)
175 {
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;
186 }
187 else
188 return TWCC_LOWMEMORY;
189 }
190
191
192 /* ICAP_XFERMECH */
193 static TW_UINT16 SANE_ICAPXferMech (pTW_CAPABILITY pCapability, TW_UINT16 action)
194 {
195 static const TW_UINT32 possible_values[] = { TWSX_NATIVE, TWSX_MEMORY };
196 TW_UINT32 val;
197 TW_UINT16 twCC = TWCC_BADCAP;
198
199 TRACE("ICAP_XFERMECH\n");
200
201 switch (action)
202 {
203 case MSG_QUERYSUPPORT:
204 twCC = set_onevalue(pCapability, TWTY_INT32,
205 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
206 break;
207
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;
212
213 case MSG_SET:
214 twCC = msg_set(pCapability, &val);
215 if (twCC == TWCC_SUCCESS)
216 {
217 activeDS.capXferMech = (TW_UINT16) val;
218 FIXME("Partial Stub: XFERMECH set to %d, but ignored\n", val);
219 }
220 break;
221
222 case MSG_GETDEFAULT:
223 twCC = set_onevalue(pCapability, TWTY_UINT16, TWSX_NATIVE);
224 break;
225
226 case MSG_RESET:
227 activeDS.capXferMech = TWSX_NATIVE;
228 /* .. fall through intentional .. */
229
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;
234 }
235 return twCC;
236 }
237
238
239 /* CAP_XFERCOUNT */
240 static TW_UINT16 SANE_CAPXferCount (pTW_CAPABILITY pCapability, TW_UINT16 action)
241 {
242 TW_UINT32 val;
243 TW_UINT16 twCC = TWCC_BADCAP;
244
245 TRACE("CAP_XFERCOUNT\n");
246
247 switch (action)
248 {
249 case MSG_QUERYSUPPORT:
250 twCC = set_onevalue(pCapability, TWTY_INT32,
251 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
252 break;
253
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;
258
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;
264
265 case MSG_GETDEFAULT:
266 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
267 break;
268
269 case MSG_RESET:
270 /* .. fall through intentional .. */
271
272 case MSG_GETCURRENT:
273 twCC = set_onevalue(pCapability, TWTY_INT16, -1);
274 break;
275 }
276 return twCC;
277 }
278
279 #ifdef SONAME_LIBSANE
280 static BOOL pixeltype_to_sane_mode(TW_UINT16 pixeltype, SANE_String mode, int len)
281 {
282 SANE_String_Const m = NULL;
283 switch (pixeltype)
284 {
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;
294 }
295 if (! m)
296 return FALSE;
297 if (strlen(m) >= len)
298 return FALSE;
299 strcpy(mode, m);
300 return TRUE;
301 }
302 static BOOL sane_mode_to_pixeltype(SANE_String_Const mode, TW_UINT16 *pixeltype)
303 {
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;
312
313 return TRUE;
314 }
315 #endif
316
317 /* ICAP_PIXELTYPE */
318 static TW_UINT16 SANE_ICAPPixelType (pTW_CAPABILITY pCapability, TW_UINT16 action)
319 {
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];
331
332 TRACE("ICAP_PIXELTYPE\n");
333
334 rc = sane_option_probe_mode(activeDS.deviceHandle, &choices, current_mode, sizeof(current_mode));
335 if (rc != SANE_STATUS_GOOD)
336 {
337 ERR("Unable to retrieve mode from sane, ICAP_PIXELTYPE unsupported\n");
338 return twCC;
339 }
340
341 sane_mode_to_pixeltype(current_mode, ¤t_pixeltype);
342
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)
346 {
347 activeDS.PixelTypeSet = TRUE;
348 activeDS.defaultPixelType = current_pixeltype;
349 }
350
351 switch (action)
352 {
353 case MSG_QUERYSUPPORT:
354 twCC = set_onevalue(pCapability, TWTY_INT32,
355 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
356 break;
357
358 case MSG_GET:
359 for (possible_value_count = 0; choices && *choices && possible_value_count < 3; choices++)
360 {
361 TW_UINT16 pix;
362 if (sane_mode_to_pixeltype(*choices, &pix))
363 possible_values[possible_value_count++] = pix;
364 }
365 twCC = msg_get_enum(pCapability, possible_values, possible_value_count,
366 TWTY_UINT16, current_pixeltype, activeDS.defaultPixelType);
367 break;
368
369 case MSG_SET:
370 twCC = msg_set(pCapability, &val);
371 if (twCC == TWCC_SUCCESS)
372 {
373 TRACE("Setting pixeltype to %d\n", val);
374 if (! pixeltype_to_sane_mode(val, mode, sizeof(mode)))
375 return TWCC_BADVALUE;
376
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)
381 {
382 strcpy(mode, "Grayscale");
383 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
384 }
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);
389 }
390 break;
391
392 case MSG_GETDEFAULT:
393 twCC = set_onevalue(pCapability, TWTY_UINT16, activeDS.defaultPixelType);
394 break;
395
396 case MSG_RESET:
397 current_pixeltype = activeDS.defaultPixelType;
398 if (! pixeltype_to_sane_mode(current_pixeltype, mode, sizeof(mode)))
399 return TWCC_BADVALUE;
400
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)
405 {
406 strcpy(mode, "Grayscale");
407 rc = sane_option_set_str(activeDS.deviceHandle, "mode", mode, &status);
408 }
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);
413
414 /* .. fall through intentional .. */
415
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;
420 }
421
422 #endif
423 return twCC;
424 }
425
426 /* ICAP_UNITS */
427 static TW_UINT16 SANE_ICAPUnits (pTW_CAPABILITY pCapability, TW_UINT16 action)
428 {
429 TW_UINT32 val;
430 TW_UINT16 twCC = TWCC_BADCAP;
431
432 TRACE("ICAP_UNITS\n");
433
434 switch (action)
435 {
436 case MSG_QUERYSUPPORT:
437 twCC = set_onevalue(pCapability, TWTY_INT32,
438 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
439 break;
440
441 case MSG_GET:
442 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
443 break;
444
445 case MSG_SET:
446 twCC = msg_set(pCapability, &val);
447 if (twCC == TWCC_SUCCESS)
448 {
449 if (val != TWUN_INCHES)
450 {
451 ERR("Sane supports only SANE_UNIT_DPI\n");
452 twCC = TWCC_BADVALUE;
453 }
454 }
455 break;
456
457 case MSG_GETDEFAULT:
458 case MSG_RESET:
459 /* .. fall through intentional .. */
460
461 case MSG_GETCURRENT:
462 twCC = set_onevalue(pCapability, TWTY_UINT16, TWUN_INCHES);
463 break;
464 }
465
466 return twCC;
467 }
468
469 /* ICAP_BITDEPTH */
470 static TW_UINT16 SANE_ICAPBitDepth(pTW_CAPABILITY pCapability, TW_UINT16 action)
471 {
472 TW_UINT16 twCC = TWCC_BADCAP;
473 #ifdef SONAME_LIBSANE
474 TW_UINT32 possible_values[1];
475
476 TRACE("ICAP_BITDEPTH\n");
477
478 possible_values[0] = activeDS.sane_param.depth;
479
480 switch (action)
481 {
482 case MSG_QUERYSUPPORT:
483 twCC = set_onevalue(pCapability, TWTY_INT32,
484 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
485 break;
486
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;
491
492 case MSG_GETDEFAULT:
493 /* .. Fall through intentional .. */
494
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;
499 }
500 #endif
501 return twCC;
502 }
503
504 /* CAP_UICONTROLLABLE */
505 static TW_UINT16 SANE_CAPUiControllable(pTW_CAPABILITY pCapability, TW_UINT16 action)
506 {
507 TW_UINT16 twCC = TWCC_BADCAP;
508
509 TRACE("CAP_UICONTROLLABLE\n");
510
511 switch (action)
512 {
513 case MSG_QUERYSUPPORT:
514 twCC = set_onevalue(pCapability, TWTY_INT32, TWQC_GET);
515 break;
516
517 case MSG_GET:
518 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
519 break;
520
521 }
522 return twCC;
523 }
524
525 /* ICAP_COMPRESSION */
526 static TW_UINT16 SANE_ICAPCompression (pTW_CAPABILITY pCapability, TW_UINT16 action)
527 {
528 static const TW_UINT32 possible_values[] = { TWCP_NONE };
529 TW_UINT32 val;
530 TW_UINT16 twCC = TWCC_BADCAP;
531
532 TRACE("ICAP_COMPRESSION\n");
533
534 switch (action)
535 {
536 case MSG_QUERYSUPPORT:
537 twCC = set_onevalue(pCapability, TWTY_INT32,
538 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
539 break;
540
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;
546
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;
552
553 case MSG_GETDEFAULT:
554 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
555 break;
556
557 case MSG_RESET:
558 /* .. fall through intentional .. */
559
560 case MSG_GETCURRENT:
561 twCC = set_onevalue(pCapability, TWTY_UINT16, TWCP_NONE);
562 break;
563 }
564 return twCC;
565 }
566
567 /* ICAP_XRESOLUTION, ICAP_YRESOLUTION */
568 static TW_UINT16 SANE_ICAPResolution (pTW_CAPABILITY pCapability, TW_UINT16 action, TW_UINT16 cap)
569 {
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;
579
580 TRACE("ICAP_%cRESOLUTION\n", cap == ICAP_XRESOLUTION ? 'X' : 'Y');
581
582 /* Some scanners support 'x-resolution', most seem to just support 'resolution' */
583 if (cap == ICAP_XRESOLUTION)
584 {
585 best_option_name = "x-resolution";
586 default_res = &activeDS.defaultXResolution;
587 }
588 else
589 {
590 best_option_name = "y-resolution";
591 default_res = &activeDS.defaultYResolution;
592 }
593 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, ¤t_resolution) != SANE_STATUS_GOOD)
594 {
595 best_option_name = "resolution";
596 if (sane_option_get_int(activeDS.deviceHandle, best_option_name, ¤t_resolution) != SANE_STATUS_GOOD)
597 return TWCC_BADCAP;
598 }
599
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)
604 {
605 default_res->Whole = current_resolution;
606 default_res->Frac = 0;
607 activeDS.XResolutionSet = TRUE;
608 }
609
610 if (cap == ICAP_YRESOLUTION && ! activeDS.YResolutionSet)
611 {
612 default_res->Whole = current_resolution;
613 default_res->Frac = 0;
614 activeDS.YResolutionSet = TRUE;
615 }
616
617 switch (action)
618 {
619 case MSG_QUERYSUPPORT:
620 twCC = set_onevalue(pCapability, TWTY_INT32,
621 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
622 break;
623
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;
632
633 case MSG_SET:
634 twCC = msg_set(pCapability, &val);
635 if (twCC == TWCC_SUCCESS)
636 {
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)
641 {
642 FIXME("Status of %d not expected or handled\n", sane_rc);
643 twCC = TWCC_BADCAP;
644 }
645 else if (set_status == SANE_INFO_INEXACT)
646 twCC = TWCC_CHECKSTATUS;
647 }
648 break;
649
650 case MSG_GETDEFAULT:
651 twCC = set_onevalue(pCapability, TWTY_FIX32, default_res->Whole);
652 break;
653
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;
658
659 /* .. fall through intentional .. */
660
661 case MSG_GETCURRENT:
662 twCC = set_onevalue(pCapability, TWTY_FIX32, current_resolution);
663 break;
664 }
665 #endif
666 return twCC;
667 }
668
669 /* ICAP_PHYSICALHEIGHT, ICAP_PHYSICALWIDTH */
670 static TW_UINT16 SANE_ICAPPhysical (pTW_CAPABILITY pCapability, TW_UINT16 action, TW_UINT16 cap)
671 {
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;
679
680 TRACE("ICAP_PHYSICAL%s\n", cap == ICAP_PHYSICALHEIGHT? "HEIGHT" : "WIDTH");
681
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);
686
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);
691
692 if (upperunit != lowerunit)
693 return TWCC_BADCAP;
694
695 if (! convert_sane_res_to_twain(SANE_UNFIX(upper) - SANE_UNFIX(lower), upperunit, &res, TWUN_INCHES))
696 return TWCC_BADCAP;
697
698 switch (action)
699 {
700 case MSG_QUERYSUPPORT:
701 twCC = set_onevalue(pCapability, TWTY_INT32,
702 TWQC_GET | TWQC_GETDEFAULT | TWQC_GETCURRENT );
703 break;
704
705 case MSG_GET:
706 case MSG_GETDEFAULT:
707
708 /* .. fall through intentional .. */
709
710 case MSG_GETCURRENT:
711 twCC = set_onevalue(pCapability, TWTY_FIX32, res.Whole | (res.Frac << 16));
712 break;
713 }
714 #endif
715 return twCC;
716 }
717
718 /* ICAP_PIXELFLAVOR */
719 static TW_UINT16 SANE_ICAPPixelFlavor (pTW_CAPABILITY pCapability, TW_UINT16 action)
720 {
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;
726
727 TRACE("ICAP_PIXELFLAVOR\n");
728
729 switch (action)
730 {
731 case MSG_QUERYSUPPORT:
732 twCC = set_onevalue(pCapability, TWTY_INT32,
733 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
734 break;
735
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;
740
741 case MSG_SET:
742 twCC = msg_set(pCapability, &val);
743 if (twCC == TWCC_SUCCESS)
744 {
745 FIXME("Stub: PIXELFLAVOR set to %d, but ignored\n", val);
746 }
747 break;
748
749 case MSG_GETDEFAULT:
750 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
751 break;
752
753 case MSG_RESET:
754 /* .. fall through intentional .. */
755
756 case MSG_GETCURRENT:
757 twCC = set_onevalue(pCapability, TWTY_UINT16, flavor);
758 break;
759 }
760 #endif
761 return twCC;
762 }
763
764 #ifdef SONAME_LIBSANE
765 static TW_UINT16 get_width_height(double *width, double *height, BOOL max)
766 {
767 SANE_Status status;
768
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;
773
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);
777
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);
781
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);
785
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);
789
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);
794
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);
799
800 return(TWCC_SUCCESS);
801 }
802
803 static TW_UINT16 set_one_coord(const char *name, double coord)
804 {
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;
810 }
811
812 static TW_UINT16 set_width_height(double width, double height)
813 {
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);
825
826 return rc;
827 }
828
829 typedef struct
830 {
831 TW_UINT32 size;
832 double x;
833 double y;
834 } supported_size_t;
835
836 static const supported_size_t supported_sizes[] =
837 {
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 },
849 };
850 #define SUPPORTED_SIZE_COUNT (sizeof(supported_sizes) / sizeof(supported_sizes[0]))
851
852 static TW_UINT16 get_default_paper_size(const supported_size_t *s, int n)
853 {
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)
862 {
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;
875 }
876
877 if (defsize == -1)
878 return TWSS_NONE;
879
880 if (get_width_height(&width, &height, TRUE) != TWCC_SUCCESS)
881 return TWSS_NONE;
882
883 for (i = 0; i < n; i++)
884 if (s[i].size == defsize)
885 {
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;
891 }
892
893 return TWSS_NONE;
894 }
895
896 static TW_UINT16 get_current_paper_size(const supported_size_t *s, int n)
897 {
898 int i;
899 double width, height;
900 double xdelta, ydelta;
901
902 if (get_width_height(&width, &height, FALSE) != TWCC_SUCCESS)
903 return TWSS_NONE;
904
905 for (i = 0; i < n; i++)
906 {
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;
914 }
915
916 return TWSS_NONE;
917 }
918 #endif
919
920 /* ICAP_SUPPORTEDSIZES */
921 static TW_UINT16 SANE_ICAPSupportedSizes (pTW_CAPABILITY pCapability, TW_UINT16 action)
922 {
923 TW_UINT16 twCC = TWCC_BADCAP;
924 #ifdef SONAME_LIBSANE
925
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);
931
932 TRACE("ICAP_SUPPORTEDSIZES\n");
933
934 switch (action)
935 {
936 case MSG_QUERYSUPPORT:
937 twCC = set_onevalue(pCapability, TWTY_INT32,
938 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
939 break;
940
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;
948
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);
955
956 ERR("Unsupported size %d\n", val);
957 twCC = TWCC_BADCAP;
958 break;
959
960 case MSG_GETDEFAULT:
961 twCC = set_onevalue(pCapability, TWTY_UINT16, default_size);
962 break;
963
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)
968 {
969 twCC = set_width_height(supported_sizes[i].x, supported_sizes[i].y);
970 break;
971 }
972 if (twCC != TWCC_SUCCESS)
973 return twCC;
974
975 /* .. fall through intentional .. */
976
977 case MSG_GETCURRENT:
978 twCC = set_onevalue(pCapability, TWTY_UINT16, current_size);
979 break;
980 }
981
982 #undef SUPPORTED_SIZE_COUNT
983 #endif
984 return twCC;
985 }
986
987 /* CAP_AUTOFEED */
988 static TW_UINT16 SANE_CAPAutofeed (pTW_CAPABILITY pCapability, TW_UINT16 action)
989 {
990 TW_UINT16 twCC = TWCC_BADCAP;
991 #ifdef SONAME_LIBSANE
992 TW_UINT32 val;
993 SANE_Bool autofeed;
994 SANE_Status status;
995
996 TRACE("CAP_AUTOFEED\n");
997
998 if (sane_option_get_bool(activeDS.deviceHandle, "batch-scan", &autofeed, NULL) != SANE_STATUS_GOOD)
999 return TWCC_BADCAP;
1000
1001 switch (action)
1002 {
1003 case MSG_QUERYSUPPORT:
1004 twCC = set_onevalue(pCapability, TWTY_INT32,
1005 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1006 break;
1007
1008 case MSG_GET:
1009 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1010 break;
1011
1012 case MSG_SET:
1013 twCC = msg_set(pCapability, &val);
1014 if (twCC == TWCC_SUCCESS)
1015 {
1016 if (val)
1017 autofeed = SANE_TRUE;
1018 else
1019 autofeed = SANE_FALSE;
1020
1021 status = sane_option_set_bool(activeDS.deviceHandle, "batch-scan", autofeed, NULL);
1022 if (status != SANE_STATUS_GOOD)
1023 {
1024 ERR("Error %s: Could not set batch-scan to %d\n", psane_strstatus(status), autofeed);
1025 return sane_status_to_twcc(status);
1026 }
1027 }
1028 break;
1029
1030 case MSG_GETDEFAULT:
1031 twCC = set_onevalue(pCapability, TWTY_BOOL, SANE_TRUE);
1032 break;
1033
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)
1038 {
1039 ERR("Error %s: Could not reset batch-scan to SANE_TRUE\n", psane_strstatus(status));
1040 return sane_status_to_twcc(status);
1041 }
1042 /* .. fall through intentional .. */
1043
1044 case MSG_GETCURRENT:
1045 twCC = set_onevalue(pCapability, TWTY_BOOL, autofeed);
1046 break;
1047 }
1048 #endif
1049 return twCC;
1050 }
1051
1052 /* CAP_FEEDERENABLED */
1053 static TW_UINT16 SANE_CAPFeederEnabled (pTW_CAPABILITY pCapability, TW_UINT16 action)
1054 {
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];
1061
1062 TRACE("CAP_FEEDERENABLED\n");
1063
1064 if (sane_option_get_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, sizeof(source), NULL) != SANE_STATUS_GOOD)
1065 return TWCC_BADCAP;
1066
1067 if (strcmp(source, "Auto") == 0 || strcmp(source, "ADF") == 0)
1068 enabled = TRUE;
1069 else
1070 enabled = FALSE;
1071
1072 switch (action)
1073 {
1074 case MSG_QUERYSUPPORT:
1075 twCC = set_onevalue(pCapability, TWTY_INT32,
1076 TWQC_GET | TWQC_SET | TWQC_GETDEFAULT | TWQC_GETCURRENT | TWQC_RESET );
1077 break;
1078
1079 case MSG_GET:
1080 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1081 break;
1082
1083 case MSG_SET:
1084 twCC = msg_set(pCapability, &val);
1085 if (twCC == TWCC_SUCCESS)
1086 {
1087 if (val)
1088 enabled = TRUE;
1089 else
1090 enabled = FALSE;
1091
1092 strcpy(source, "ADF");
1093 status = sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL);
1094 if (status != SANE_STATUS_GOOD)
1095 {
1096 strcpy(source, "Auto");
1097 status = sane_option_set_str(activeDS.deviceHandle, SANE_NAME_SCAN_SOURCE, source, NULL);
1098 }
1099 if (status != SANE_STATUS_GOOD)
1100 {
1101 ERR("Error %s: Could not set source to either ADF or Auto\n", psane_strstatus(status));
1102 return sane_status_to_twcc(status);
1103 }
1104 }
1105 break;
1106
1107 case MSG_GETDEFAULT:
1108 twCC = set_onevalue(pCapability, TWTY_BOOL, TRUE);
1109 break;
1110
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 .. */
1117
1118 case MSG_GETCURRENT:
1119 twCC = set_onevalue(pCapability, TWTY_BOOL, enabled);
1120 break;
1121 }
1122 #endif
1123 return twCC;
1124 }
1125
1126
1127
1128 TW_UINT16 SANE_SaneCapability (pTW_CAPABILITY pCapability, TW_UINT16 action)
1129 {
1130 TW_UINT16 twCC = TWCC_CAPUNSUPPORTED;
1131
1132 TRACE("capability=%d action=%d\n", pCapability->Cap, action);
1133
1134 switch (pCapability->Cap)
1135 {
1136 case CAP_SUPPORTEDCAPS:
1137 if (action == MSG_GET)
1138 twCC = TWAIN_GetSupportedCaps(pCapability);
1139 else
1140 twCC = TWCC_BADVALUE;
1141 break;
1142
1143 case CAP_XFERCOUNT:
1144 twCC = SANE_CAPXferCount (pCapability, action);
1145 break;
1146
1147 case CAP_UICONTROLLABLE:
1148 twCC = SANE_CAPUiControllable (pCapability, action);
1149 break;
1150
1151 case CAP_AUTOFEED:
1152 twCC = SANE_CAPAutofeed (pCapability, action);
1153 break;
1154
1155 case CAP_FEEDERENABLED:
1156 twCC = SANE_CAPFeederEnabled (pCapability, action);
1157 break;
1158
1159 case ICAP_PIXELTYPE:
1160 twCC = SANE_ICAPPixelType (pCapability, action);
1161 break;
1162
1163 case ICAP_UNITS:
1164 twCC = SANE_ICAPUnits (pCapability, action);
1165 break;
1166
1167 case ICAP_BITDEPTH:
1168 twCC = SANE_ICAPBitDepth(pCapability, action);
1169 break;
1170
1171 case ICAP_XFERMECH:
1172 twCC = SANE_ICAPXferMech (pCapability, action);
1173 break;
1174
1175 case ICAP_PIXELFLAVOR:
1176 twCC = SANE_ICAPPixelFlavor (pCapability, action);
1177 break;
1178
1179 case ICAP_COMPRESSION:
1180 twCC = SANE_ICAPCompression(pCapability, action);
1181 break;
1182
1183 case ICAP_XRESOLUTION:
1184 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1185 break;
1186
1187 case ICAP_YRESOLUTION:
1188 twCC = SANE_ICAPResolution(pCapability, action, pCapability->Cap);
1189 break;
1190
1191 case ICAP_PHYSICALHEIGHT:
1192 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1193 break;
1194
1195 case ICAP_PHYSICALWIDTH:
1196 twCC = SANE_ICAPPhysical(pCapability, action, pCapability->Cap);
1197 break;
1198
1199 case ICAP_SUPPORTEDSIZES:
1200 twCC = SANE_ICAPSupportedSizes (pCapability, action);
1201 break;
1202
1203 case ICAP_PLANARCHUNKY:
1204 FIXME("ICAP_PLANARCHUNKY not implemented\n");
1205 break;
1206
1207 case ICAP_BITORDER:
1208 FIXME("ICAP_BITORDER not implemented\n");
1209 break;
1210
1211 }
1212
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);
1217
1218 if (twCC == TWCC_CAPUNSUPPORTED)
1219 TRACE("capability 0x%x/action=%d being reported as unsupported\n", pCapability->Cap, action);
1220
1221 return twCC;
1222 }
1223
1224 TW_UINT16 SANE_SaneSetDefaults (void)
1225 {
1226 TW_CAPABILITY cap;
1227
1228 memset(&cap, 0, sizeof(cap));
1229 cap.Cap = CAP_AUTOFEED;
1230 cap.ConType = TWON_DONTCARE16;
1231
1232 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1233 GlobalFree(cap.hContainer);
1234
1235 memset(&cap, 0, sizeof(cap));
1236 cap.Cap = CAP_FEEDERENABLED;
1237 cap.ConType = TWON_DONTCARE16;
1238
1239 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1240 GlobalFree(cap.hContainer);
1241
1242 memset(&cap, 0, sizeof(cap));
1243 cap.Cap = ICAP_SUPPORTEDSIZES;
1244 cap.ConType = TWON_DONTCARE16;
1245
1246 if (SANE_SaneCapability(&cap, MSG_RESET) == TWCC_SUCCESS)
1247 GlobalFree(cap.hContainer);
1248
1249 return TWCC_SUCCESS;
1250 }
1251
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.