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
23 #include "wine/debug.h"
26 static SANE_Status
sane_find_option(SANE_Handle h
, const char *option_name
,
27 const SANE_Option_Descriptor
**opt_p
, int *optno
, SANE_Value_Type type
)
31 const SANE_Option_Descriptor
*opt
;
34 /* Debian, in 32_net_backend_standard_fix.dpatch,
35 * forces a frontend (that's us) to reload options
36 * manually by invoking get_option_descriptor. */
37 opt
= psane_get_option_descriptor(h
, 0);
39 return SANE_STATUS_EOF
;
41 rc
= psane_control_option(h
, 0, SANE_ACTION_GET_VALUE
, &optcount
, NULL
);
42 if (rc
!= SANE_STATUS_GOOD
)
45 for (i
= 1; i
< optcount
; i
++)
47 opt
= psane_get_option_descriptor(h
, i
);
48 if (opt
&& (opt
->name
&& strcmp(opt
->name
, option_name
) == 0) &&
53 return SANE_STATUS_GOOD
;
56 return SANE_STATUS_EOF
;
59 SANE_Status
sane_option_get_int(SANE_Handle h
, const char *option_name
, SANE_Int
*val
)
63 const SANE_Option_Descriptor
*opt
;
65 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
66 if (rc
!= SANE_STATUS_GOOD
)
69 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, val
, NULL
);
72 SANE_Status
sane_option_set_int(SANE_Handle h
, const char *option_name
, SANE_Int val
, SANE_Int
*status
)
76 const SANE_Option_Descriptor
*opt
;
78 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
79 if (rc
!= SANE_STATUS_GOOD
)
82 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
85 SANE_Status
sane_option_get_bool(SANE_Handle h
, const char *option_name
, SANE_Bool
*val
, SANE_Int
*status
)
89 const SANE_Option_Descriptor
*opt
;
91 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_BOOL
);
92 if (rc
!= SANE_STATUS_GOOD
)
95 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, (void *) val
, status
);
98 SANE_Status
sane_option_set_bool(SANE_Handle h
, const char *option_name
, SANE_Bool val
, SANE_Int
*status
)
102 const SANE_Option_Descriptor
*opt
;
104 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_BOOL
);
105 if (rc
!= SANE_STATUS_GOOD
)
108 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
111 SANE_Status
sane_option_set_fixed(SANE_Handle h
, const char *option_name
, SANE_Fixed val
, SANE_Int
*status
)
115 const SANE_Option_Descriptor
*opt
;
117 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_FIXED
);
118 if (rc
!= SANE_STATUS_GOOD
)
121 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
124 SANE_Status
sane_option_get_str(SANE_Handle h
, const char *option_name
, SANE_String val
, size_t len
, SANE_Int
*status
)
128 const SANE_Option_Descriptor
*opt
;
130 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_STRING
);
131 if (rc
!= SANE_STATUS_GOOD
)
135 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, (void *) val
, status
);
137 return SANE_STATUS_NO_MEM
;
140 /* Important: SANE has the side effect of overwriting val with the returned value */
141 SANE_Status
sane_option_set_str(SANE_Handle h
, const char *option_name
, SANE_String val
, SANE_Int
*status
)
145 const SANE_Option_Descriptor
*opt
;
147 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_STRING
);
148 if (rc
!= SANE_STATUS_GOOD
)
151 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) val
, status
);
154 SANE_Status
sane_option_probe_resolution(SANE_Handle h
, const char *option_name
, SANE_Int
*minval
, SANE_Int
*maxval
, SANE_Int
*quant
)
158 const SANE_Option_Descriptor
*opt
;
160 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
161 if (rc
!= SANE_STATUS_GOOD
)
164 if (opt
->constraint_type
!= SANE_CONSTRAINT_RANGE
)
165 return SANE_STATUS_UNSUPPORTED
;
167 *minval
= opt
->constraint
.range
->min
;
168 *maxval
= opt
->constraint
.range
->max
;
169 *quant
= opt
->constraint
.range
->quant
;
174 SANE_Status
sane_option_probe_mode(SANE_Handle h
, SANE_String_Const
**choices
, char *current
, int current_size
)
178 const SANE_Option_Descriptor
*opt
;
179 rc
= sane_find_option(h
, "mode", &opt
, &optno
, SANE_TYPE_STRING
);
180 if (rc
!= SANE_STATUS_GOOD
)
183 if (choices
&& opt
->constraint_type
== SANE_CONSTRAINT_STRING_LIST
)
184 *choices
= (SANE_String_Const
*) opt
->constraint
.string_list
;
186 if (opt
->size
< current_size
)
187 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, current
, NULL
);
189 return SANE_STATUS_NO_MEM
;
193 SANE_Status
sane_option_probe_scan_area(SANE_Handle h
, const char *option_name
, SANE_Fixed
*val
,
194 SANE_Unit
*unit
, SANE_Fixed
*min
, SANE_Fixed
*max
, SANE_Fixed
*quant
)
198 const SANE_Option_Descriptor
*opt
;
200 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_FIXED
);
201 if (rc
!= SANE_STATUS_GOOD
)
207 *min
= opt
->constraint
.range
->min
;
209 *max
= opt
->constraint
.range
->max
;
211 *quant
= opt
->constraint
.range
->quant
;
214 rc
= psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, val
, NULL
);
219 TW_UINT16
sane_status_to_twcc(SANE_Status rc
)
223 case SANE_STATUS_GOOD
:
225 case SANE_STATUS_UNSUPPORTED
:
226 return TWCC_CAPUNSUPPORTED
;
227 case SANE_STATUS_JAMMED
:
228 return TWCC_PAPERJAM
;
229 case SANE_STATUS_NO_MEM
:
230 return TWCC_LOWMEMORY
;
231 case SANE_STATUS_ACCESS_DENIED
:
234 case SANE_STATUS_IO_ERROR
:
235 case SANE_STATUS_NO_DOCS
:
236 case SANE_STATUS_COVER_OPEN
:
237 case SANE_STATUS_EOF
:
238 case SANE_STATUS_INVAL
:
239 case SANE_STATUS_CANCELLED
:
240 case SANE_STATUS_DEVICE_BUSY
:
245 static void convert_double_fix32(double d
, TW_FIX32
*fix32
)
247 TW_INT32 value
= (TW_INT32
) (d
* 65536.0 + 0.5);
248 fix32
->Whole
= value
>> 16;
249 fix32
->Frac
= value
& 0x0000ffffL
;
252 BOOL
convert_sane_res_to_twain(double sane_res
, SANE_Unit unit
, TW_FIX32
*twain_res
, TW_UINT16 twtype
)
254 if (unit
!= SANE_UNIT_MM
)
257 if (twtype
!= TWUN_INCHES
)
260 convert_double_fix32((sane_res
/ 10.0) / 2.54, twain_res
);