gdi32/uniscribe: Ensure the cache is initialised.
[wine.git] / dlls / sane.ds / options.c
bloba4c4507360fee33189f9782a164488e223b7ebc4
1 /*
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
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include "sane_i.h"
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, &params );
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, &params );
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, &params ) ? 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 );
55 return rc;
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 );
65 return rc;
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 );
74 return rc;
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 );
84 return rc;
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)
94 if (opt.size < len)
95 rc = sane_option_get_value( opt.optno, val );
96 else
97 rc = TWCC_BADVALUE;
99 return rc;
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 );
110 return rc;
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)
120 TW_UINT32 i, j;
121 for(i=0; filter[i]; ++i)
123 if (!*categories)
125 for(j=0; filter[i][j]; ++j)
127 if (!wcscmp(value, filter[i][j]))
129 wcstombs(categories, value, buf_len);
130 return i;
134 categories += buf_len;
136 return 0;
139 TW_UINT16 sane_option_probe_str(const char* option_name, const WCHAR* const* filter[], char* opt_values, int buf_len)
141 WCHAR *p;
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);
155 return rc;
158 TW_UINT16 sane_option_get_scan_area( int *tlx, int *tly, int *brx, int *bry )
160 TW_UINT16 rc;
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 );
172 return rc;
175 TW_UINT16 sane_option_get_max_scan_area( int *tlx, int *tly, int *brx, int *bry )
177 TW_UINT16 rc;
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 );
189 return rc;
192 TW_UINT16 sane_option_set_scan_area( int tlx, int tly, int brx, int bry, BOOL *reload )
194 TW_UINT16 rc;
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;
205 return rc;
208 TW_FIX32 convert_sane_res_to_twain(int res)
210 TW_FIX32 value;
211 res = MulDiv( res, 10, 254 ); /* mm -> inch */
212 value.Whole = res / 65536;
213 value.Frac = res & 0xffff;
214 return value;
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 );