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"
27 static SANE_Status
sane_find_option(SANE_Handle h
, const char *option_name
,
28 const SANE_Option_Descriptor
**opt_p
, int *optno
, SANE_Value_Type type
)
32 const SANE_Option_Descriptor
*opt
;
35 /* Debian, in 32_net_backend_standard_fix.dpatch,
36 * forces a frontend (that's us) to reload options
37 * manually by invoking get_option_descriptor. */
38 opt
= psane_get_option_descriptor(h
, 0);
40 return SANE_STATUS_EOF
;
42 rc
= psane_control_option(h
, 0, SANE_ACTION_GET_VALUE
, &optcount
, NULL
);
43 if (rc
!= SANE_STATUS_GOOD
)
46 for (i
= 1; i
< optcount
; i
++)
48 opt
= psane_get_option_descriptor(h
, i
);
49 if (opt
&& (opt
->name
&& strcmp(opt
->name
, option_name
) == 0) &&
54 return SANE_STATUS_GOOD
;
57 return SANE_STATUS_EOF
;
60 SANE_Status
sane_option_get_int(SANE_Handle h
, const char *option_name
, SANE_Int
*val
)
64 const SANE_Option_Descriptor
*opt
;
66 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
67 if (rc
!= SANE_STATUS_GOOD
)
70 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, val
, NULL
);
73 SANE_Status
sane_option_set_int(SANE_Handle h
, const char *option_name
, SANE_Int val
, SANE_Int
*status
)
77 const SANE_Option_Descriptor
*opt
;
79 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
80 if (rc
!= SANE_STATUS_GOOD
)
83 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
86 SANE_Status
sane_option_get_bool(SANE_Handle h
, const char *option_name
, SANE_Bool
*val
, SANE_Int
*status
)
90 const SANE_Option_Descriptor
*opt
;
92 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_BOOL
);
93 if (rc
!= SANE_STATUS_GOOD
)
96 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, (void *) val
, status
);
99 SANE_Status
sane_option_set_bool(SANE_Handle h
, const char *option_name
, SANE_Bool val
, SANE_Int
*status
)
103 const SANE_Option_Descriptor
*opt
;
105 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_BOOL
);
106 if (rc
!= SANE_STATUS_GOOD
)
109 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
112 SANE_Status
sane_option_set_fixed(SANE_Handle h
, const char *option_name
, SANE_Fixed val
, SANE_Int
*status
)
116 const SANE_Option_Descriptor
*opt
;
118 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_FIXED
);
119 if (rc
!= SANE_STATUS_GOOD
)
122 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) &val
, status
);
125 SANE_Status
sane_option_get_str(SANE_Handle h
, const char *option_name
, SANE_String val
, size_t len
, SANE_Int
*status
)
129 const SANE_Option_Descriptor
*opt
;
131 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_STRING
);
132 if (rc
!= SANE_STATUS_GOOD
)
136 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, (void *) val
, status
);
138 return SANE_STATUS_NO_MEM
;
141 /* Important: SANE has the side effect of overwriting val with the returned value */
142 SANE_Status
sane_option_set_str(SANE_Handle h
, const char *option_name
, SANE_String val
, SANE_Int
*status
)
146 const SANE_Option_Descriptor
*opt
;
148 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_STRING
);
149 if (rc
!= SANE_STATUS_GOOD
)
152 return psane_control_option(h
, optno
, SANE_ACTION_SET_VALUE
, (void *) val
, status
);
155 SANE_Status
sane_option_probe_resolution(SANE_Handle h
, const char *option_name
, SANE_Int
*minval
, SANE_Int
*maxval
, SANE_Int
*quant
)
159 const SANE_Option_Descriptor
*opt
;
161 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_INT
);
162 if (rc
!= SANE_STATUS_GOOD
)
165 if (opt
->constraint_type
!= SANE_CONSTRAINT_RANGE
)
166 return SANE_STATUS_UNSUPPORTED
;
168 *minval
= opt
->constraint
.range
->min
;
169 *maxval
= opt
->constraint
.range
->max
;
170 *quant
= opt
->constraint
.range
->quant
;
175 SANE_Status
sane_option_probe_mode(SANE_Handle h
, SANE_String_Const
**choices
, char *current
, int current_size
)
179 const SANE_Option_Descriptor
*opt
;
180 rc
= sane_find_option(h
, "mode", &opt
, &optno
, SANE_TYPE_STRING
);
181 if (rc
!= SANE_STATUS_GOOD
)
184 if (choices
&& opt
->constraint_type
== SANE_CONSTRAINT_STRING_LIST
)
185 *choices
= (SANE_String_Const
*) opt
->constraint
.string_list
;
187 if (opt
->size
< current_size
)
188 return psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, current
, NULL
);
190 return SANE_STATUS_NO_MEM
;
194 SANE_Status
sane_option_probe_scan_area(SANE_Handle h
, const char *option_name
, SANE_Fixed
*val
,
195 SANE_Unit
*unit
, SANE_Fixed
*min
, SANE_Fixed
*max
, SANE_Fixed
*quant
)
199 const SANE_Option_Descriptor
*opt
;
201 rc
= sane_find_option(h
, option_name
, &opt
, &optno
, SANE_TYPE_FIXED
);
202 if (rc
!= SANE_STATUS_GOOD
)
208 *min
= opt
->constraint
.range
->min
;
210 *max
= opt
->constraint
.range
->max
;
212 *quant
= opt
->constraint
.range
->quant
;
215 rc
= psane_control_option(h
, optno
, SANE_ACTION_GET_VALUE
, val
, NULL
);
220 TW_UINT16
sane_status_to_twcc(SANE_Status rc
)
224 case SANE_STATUS_GOOD
:
226 case SANE_STATUS_UNSUPPORTED
:
227 return TWCC_CAPUNSUPPORTED
;
228 case SANE_STATUS_JAMMED
:
229 return TWCC_PAPERJAM
;
230 case SANE_STATUS_NO_MEM
:
231 return TWCC_LOWMEMORY
;
232 case SANE_STATUS_ACCESS_DENIED
:
235 case SANE_STATUS_IO_ERROR
:
236 case SANE_STATUS_NO_DOCS
:
237 case SANE_STATUS_COVER_OPEN
:
238 case SANE_STATUS_EOF
:
239 case SANE_STATUS_INVAL
:
240 case SANE_STATUS_CANCELLED
:
241 case SANE_STATUS_DEVICE_BUSY
:
246 static void convert_double_fix32(double d
, TW_FIX32
*fix32
)
248 TW_INT32 value
= (TW_INT32
) (d
* 65536.0 + 0.5);
249 fix32
->Whole
= value
>> 16;
250 fix32
->Frac
= value
& 0x0000ffffL
;
253 BOOL
convert_sane_res_to_twain(double sane_res
, SANE_Unit unit
, TW_FIX32
*twain_res
, TW_UINT16 twtype
)
257 if (unit
!= SANE_UNIT_MM
)
260 if (twtype
!= TWUN_INCHES
)
263 d
= (sane_res
/ 10.0) / 2.54;
264 convert_double_fix32((sane_res
/ 10.0) / 2.54, twain_res
);