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
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
29 TW_UINT16
sane_option_get_value( int optno
, void *val
)
31 struct option_get_value_params params
= { optno
, val
};
33 return SANE_CALL( option_get_value
, ¶ms
);
36 TW_UINT16
sane_option_set_value( int optno
, void *val
, BOOL
*reload
)
38 struct option_set_value_params params
= { optno
, val
, reload
};
40 return SANE_CALL( option_set_value
, ¶ms
);
43 static TW_UINT16
sane_find_option( const char *name
, int type
, struct option_descriptor
*descr
)
45 struct option_find_descriptor_params params
= { name
, type
, descr
};
46 return SANE_CALL( option_find_descriptor
, ¶ms
) ? TWCC_CAPUNSUPPORTED
: TWCC_SUCCESS
;
49 TW_UINT16
sane_option_get_int(const char *option_name
, int *val
)
51 struct option_descriptor opt
;
52 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_INT
, &opt
);
54 if (rc
== TWCC_SUCCESS
) rc
= sane_option_get_value( opt
.optno
, val
);
58 TW_UINT16
sane_option_set_int(const char *option_name
, int val
, BOOL
*needs_reload
)
60 struct option_descriptor opt
;
61 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_INT
, &opt
);
62 if (!opt
.is_settable
) return TWCC_OPERATIONERROR
;
64 if (rc
== TWCC_SUCCESS
) rc
= sane_option_set_value( opt
.optno
, &val
, needs_reload
);
68 TW_UINT16
sane_option_get_bool(const char *option_name
, int *val
)
70 struct option_descriptor opt
;
71 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_BOOL
, &opt
);
73 if (rc
== TWCC_SUCCESS
) rc
= sane_option_get_value( opt
.optno
, val
);
77 TW_UINT16
sane_option_set_bool(const char *option_name
, int val
)
79 struct option_descriptor opt
;
80 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_BOOL
, &opt
);
81 if (!opt
.is_settable
) return TWCC_OPERATIONERROR
;
83 if (rc
== TWCC_SUCCESS
) rc
= sane_option_set_value( opt
.optno
, &val
, NULL
);
87 TW_UINT16
sane_option_get_str(const char *option_name
, char *val
, int len
)
89 struct option_descriptor opt
;
90 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_STRING
, &opt
);
92 if (rc
== TWCC_SUCCESS
)
95 rc
= sane_option_get_value( opt
.optno
, val
);
102 /* Important: SANE has the side effect of overwriting val with the returned value */
103 TW_UINT16
sane_option_set_str(const char *option_name
, char *val
, BOOL
*needs_reload
)
105 struct option_descriptor opt
;
106 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_STRING
, &opt
);
107 if (!opt
.is_settable
) return TWCC_OPERATIONERROR
;
109 if (rc
== TWCC_SUCCESS
) rc
= sane_option_set_value( opt
.optno
, val
, needs_reload
);
113 TW_UINT16
sane_option_probe_resolution(const char *option_name
, struct option_descriptor
*opt
)
115 return sane_find_option(option_name
, TYPE_INT
, opt
);
118 static TW_UINT32
sane_categorize_value(const WCHAR
* value
, const WCHAR
* const* filter
[], char* categories
, int buf_len
)
121 for(i
=0; filter
[i
]; ++i
)
125 for(j
=0; filter
[i
][j
]; ++j
)
127 if (!wcscmp(value
, filter
[i
][j
]))
129 wcstombs(categories
, value
, buf_len
);
134 categories
+= buf_len
;
139 TW_UINT16
sane_option_probe_str(const char* option_name
, const WCHAR
* const* filter
[], char* opt_values
, int buf_len
)
142 struct option_descriptor opt
;
143 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_STRING
, &opt
);
145 if (rc
!= TWCC_SUCCESS
) return rc
;
146 if (opt
.size
> buf_len
) return TWCC_BADVALUE
;
148 if (opt
.constraint_type
== CONSTRAINT_STRING_LIST
)
150 for (p
= opt
.constraint
.strings
; *p
; p
+= lstrlenW(p
) + 1)
152 sane_categorize_value(p
, filter
, opt_values
, buf_len
);
158 TW_UINT16
sane_option_get_scan_area( int *tlx
, int *tly
, int *brx
, int *bry
)
161 struct option_descriptor opt
;
163 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
164 if ((rc
= sane_option_get_value( opt
.optno
, tlx
)) != TWCC_SUCCESS
) return rc
;
165 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
166 if ((rc
= sane_option_get_value( opt
.optno
, tly
)) != TWCC_SUCCESS
) return rc
;
167 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
168 if ((rc
= sane_option_get_value( opt
.optno
, brx
)) != TWCC_SUCCESS
) return rc
;
169 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
170 if ((rc
= sane_option_get_value( opt
.optno
, bry
)) != TWCC_SUCCESS
) return rc
;
171 if (opt
.unit
!= UNIT_MM
) FIXME( "unsupported unit %u\n", opt
.unit
);
175 TW_UINT16
sane_option_get_max_scan_area( int *tlx
, int *tly
, int *brx
, int *bry
)
178 struct option_descriptor opt
;
180 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
181 *tlx
= opt
.constraint
.range
.min
;
182 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
183 *tly
= opt
.constraint
.range
.min
;
184 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
185 *brx
= opt
.constraint
.range
.max
;
186 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
187 *bry
= opt
.constraint
.range
.max
;
188 if (opt
.unit
!= UNIT_MM
) FIXME( "unsupported unit %u\n", opt
.unit
);
192 TW_UINT16
sane_option_set_scan_area( int tlx
, int tly
, int brx
, int bry
, BOOL
*reload
)
195 struct option_descriptor opt
;
197 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
198 if ((rc
= sane_option_set_value( opt
.optno
, &tlx
, reload
)) != TWCC_SUCCESS
) return rc
;
199 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
200 if ((rc
= sane_option_set_value( opt
.optno
, &tly
, reload
)) != TWCC_SUCCESS
) return rc
;
201 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
202 if ((rc
= sane_option_set_value( opt
.optno
, &brx
, reload
)) != TWCC_SUCCESS
) return rc
;
203 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
204 if ((rc
= sane_option_set_value( opt
.optno
, &bry
, reload
)) != TWCC_SUCCESS
) return rc
;
208 TW_FIX32
convert_sane_res_to_twain(int res
)
211 res
= MulDiv( res
, 10, 254 ); /* mm -> inch */
212 value
.Whole
= res
/ 65536;
213 value
.Frac
= res
& 0xffff;
217 int convert_twain_res_to_sane( TW_FIX32 res
)
219 return MulDiv( res
.Whole
* 65536 + res
.Frac
, 254, 10 ); /* inch -> mm */
222 TW_UINT16
get_sane_params( struct frame_parameters
*params
)
224 return SANE_CALL( get_params
, params
);