2 * Copyright 2009 Jeremy White <jwhite@codeweavers.com> for CodeWeavers
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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
29 static SANE_Status
sane_find_option(SANE_Handle h
, const char *option_name
,
30 const SANE_Option_Descriptor
**opt_p
, int *optno
, SANE_Value_Type type
)
34 const SANE_Option_Descriptor
*opt
;
37 /* Debian, in 32_net_backend_standard_fix.dpatch,
38 * forces a frontend (that's us) to reload options
39 * manually by invoking get_option_descriptor. */
40 opt
= psane_get_option_descriptor(h
, 0);
42 return SANE_STATUS_EOF
;
44 rc
= psane_control_option(h
, 0, SANE_ACTION_GET_VALUE
, &optcount
, NULL
);
45 if (rc
!= SANE_STATUS_GOOD
)
48 for (i
= 1; i
< optcount
; i
++)
50 opt
= psane_get_option_descriptor(h
, i
);
51 if (opt
&& (opt
->name
&& strcmp(opt
->name
, option_name
) == 0) &&
56 return SANE_STATUS_GOOD
;
59 return SANE_STATUS_EOF
;
62 SANE_Status
sane_option_get_int(SANE_Handle h
, const char *option_name
, SANE_Int
*val
)
66 const SANE_Option_Descriptor
*opt
;
68 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
69 if (rc
!= SANE_STATUS_GOOD
)
72 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, val
, NULL
);
75 SANE_Status
sane_option_set_int(SANE_Handle h
, const char *option_name
, SANE_Int val
, SANE_Int
*status
)
79 const SANE_Option_Descriptor
*opt
;
81 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
82 if (rc
!= SANE_STATUS_GOOD
)
85 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
88 SANE_Status
sane_option_get_bool(SANE_Handle h
, const char *option_name
, SANE_Bool
*val
, SANE_Int
*status
)
92 const SANE_Option_Descriptor
*opt
;
94 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_BOOL
);
95 if (rc
!= SANE_STATUS_GOOD
)
98 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, (void *) val
, status
);
101 SANE_Status
sane_option_set_bool(SANE_Handle h
, const char *option_name
, SANE_Bool val
, SANE_Int
*status
)
105 const SANE_Option_Descriptor
*opt
;
107 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_BOOL
);
108 if (rc
!= SANE_STATUS_GOOD
)
111 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
114 SANE_Status
sane_option_set_fixed(SANE_Handle h
, const char *option_name
, SANE_Fixed val
, SANE_Int
*status
)
118 const SANE_Option_Descriptor
*opt
;
120 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_FIXED
);
121 if (rc
!= SANE_STATUS_GOOD
)
124 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
127 SANE_Status
sane_option_get_str(SANE_Handle h
, const char *option_name
, SANE_String val
, size_t len
, SANE_Int
*status
)
131 const SANE_Option_Descriptor
*opt
;
133 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_STRING
);
134 if (rc
!= SANE_STATUS_GOOD
)
138 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, (void *) val
, status
);
140 return SANE_STATUS_NO_MEM
;
143 /* Important: SANE has the side effect of of overwriting val with the returned value */
144 SANE_Status
sane_option_set_str(SANE_Handle h
, const char *option_name
, SANE_String val
, SANE_Int
*status
)
148 const SANE_Option_Descriptor
*opt
;
150 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_STRING
);
151 if (rc
!= SANE_STATUS_GOOD
)
154 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) val
, status
);
157 SANE_Status
sane_option_probe_resolution(SANE_Handle h
, const char *option_name
, SANE_Int
*minval
, SANE_Int
*maxval
, SANE_Int
*quant
)
161 const SANE_Option_Descriptor
*opt
;
163 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
164 if (rc
!= SANE_STATUS_GOOD
)
167 if (opt
->constraint_type
!= SANE_CONSTRAINT_RANGE
)
168 return SANE_STATUS_UNSUPPORTED
;
170 *minval
= opt
->constraint
.range
->min
;
171 *maxval
= opt
->constraint
.range
->max
;
172 *quant
= opt
->constraint
.range
->quant
;
177 SANE_Status
sane_option_probe_mode(SANE_Handle h
, SANE_String_Const
**choices
, char *current
, int current_size
)
181 const SANE_Option_Descriptor
*opt
;
182 rc
= sane_find_option(h
, "mode", &opt
, &optno
, SANE_TYPE_STRING
);
183 if (rc
!= SANE_STATUS_GOOD
)
186 if (choices
&& opt
->constraint_type
== SANE_CONSTRAINT_STRING_LIST
)
187 *choices
= (SANE_String_Const
*) opt
->constraint
.string_list
;
189 if (opt
->size
< current_size
)
190 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, current
, NULL
);
192 return SANE_STATUS_NO_MEM
;
196 SANE_Status
sane_option_probe_scan_area(SANE_Handle h
, const char *option_name
, SANE_Fixed
*val
,
197 SANE_Unit
*unit
, SANE_Fixed
*min
, SANE_Fixed
*max
, SANE_Fixed
*quant
)
201 const SANE_Option_Descriptor
*opt
;
203 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_FIXED
);
204 if (rc
!= SANE_STATUS_GOOD
)
210 *min
= opt
->constraint
.range
->min
;
212 *max
= opt
->constraint
.range
->max
;
214 *quant
= opt
->constraint
.range
->quant
;
217 rc
= psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, val
, NULL
);
222 TW_UINT16
sane_status_to_twcc(SANE_Status rc
)
226 case SANE_STATUS_GOOD
:
228 case SANE_STATUS_UNSUPPORTED
:
229 return TWCC_CAPUNSUPPORTED
;
230 case SANE_STATUS_JAMMED
:
231 return TWCC_PAPERJAM
;
232 case SANE_STATUS_NO_MEM
:
233 return TWCC_LOWMEMORY
;
234 case SANE_STATUS_ACCESS_DENIED
:
237 case SANE_STATUS_IO_ERROR
:
238 case SANE_STATUS_NO_DOCS
:
239 case SANE_STATUS_COVER_OPEN
:
240 case SANE_STATUS_EOF
:
241 case SANE_STATUS_INVAL
:
242 case SANE_STATUS_CANCELLED
:
243 case SANE_STATUS_DEVICE_BUSY
:
248 static void convert_double_fix32(double d
, TW_FIX32
*fix32
)
250 TW_INT32 value
= (TW_INT32
) (d
* 65536.0 + 0.5);
251 fix32
->Whole
= value
>> 16;
252 fix32
->Frac
= value
& 0x0000ffffL
;
255 BOOL
convert_sane_res_to_twain(double sane_res
, SANE_Unit unit
, TW_FIX32
*twain_res
, TW_UINT16 twtype
)
259 if (unit
!= SANE_UNIT_MM
)
262 if (twtype
!= TWUN_INCHES
)
265 d
= (sane_res
/ 10.0) / 2.54;
266 convert_double_fix32((sane_res
/ 10.0) / 2.54, twain_res
);