ntdll: Use a separate memory allocation for the kernel stack.
[wine.git] / dlls / sane.ds / options.c
blob52e73a9992508cf84751c7c1e1e8c03c83a03467
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);
63 if (rc == TWCC_SUCCESS) rc = sane_option_set_value( opt.optno, &val, needs_reload );
64 return rc;
67 TW_UINT16 sane_option_get_bool(const char *option_name, int *val)
69 struct option_descriptor opt;
70 TW_UINT16 rc = sane_find_option(option_name, TYPE_BOOL, &opt);
72 if (rc == TWCC_SUCCESS) rc = sane_option_get_value( opt.optno, val );
73 return rc;
76 TW_UINT16 sane_option_set_bool(const char *option_name, int val )
78 struct option_descriptor opt;
79 TW_UINT16 rc = sane_find_option(option_name, TYPE_BOOL, &opt);
81 if (rc == TWCC_SUCCESS) rc = sane_option_set_value( opt.optno, &val, NULL );
82 return rc;
85 TW_UINT16 sane_option_get_str(const char *option_name, char *val, int len)
87 struct option_descriptor opt;
88 TW_UINT16 rc = sane_find_option(option_name, TYPE_STRING, &opt);
90 if (rc == TWCC_SUCCESS)
92 if (opt.size < len)
93 rc = sane_option_get_value( opt.optno, val );
94 else
95 rc = TWCC_BADVALUE;
97 return rc;
100 /* Important: SANE has the side effect of overwriting val with the returned value */
101 TW_UINT16 sane_option_set_str(const char *option_name, char *val, BOOL *needs_reload)
103 struct option_descriptor opt;
104 TW_UINT16 rc = sane_find_option(option_name, TYPE_STRING, &opt);
106 if (rc == TWCC_SUCCESS) rc = sane_option_set_value( opt.optno, &val, needs_reload );
107 return rc;
110 TW_UINT16 sane_option_probe_resolution(const char *option_name, int *minval, int *maxval, int *quant)
112 struct option_descriptor opt;
113 TW_UINT16 rc = sane_find_option(option_name, TYPE_INT, &opt);
115 if (rc != TWCC_SUCCESS) return rc;
116 if (opt.constraint_type != CONSTRAINT_RANGE) return TWCC_CAPUNSUPPORTED;
118 *minval = opt.constraint.range.min;
119 *maxval = opt.constraint.range.max;
120 *quant = opt.constraint.range.quant;
121 return rc;
124 TW_UINT16 sane_option_probe_mode(TW_UINT16 *current, TW_UINT32 *choices, int *count)
126 WCHAR *p;
127 char buffer[256];
128 struct option_descriptor opt;
129 TW_UINT16 rc = sane_find_option("mode", TYPE_STRING, &opt);
131 if (rc != TWCC_SUCCESS) return rc;
132 if (opt.size > sizeof(buffer)) return TWCC_BADVALUE;
133 rc = sane_option_get_value( opt.optno, buffer );
134 if (rc != TWCC_SUCCESS) return rc;
136 if (!strcmp( buffer, "Lineart" )) *current = TWPT_BW;
137 else if (!strcmp( buffer, "Color" )) *current = TWPT_RGB;
138 else if (!strncmp( buffer, "Gray", 4 )) *current = TWPT_GRAY;
140 *count = 0;
141 if (opt.constraint_type == CONSTRAINT_STRING_LIST)
143 for (p = opt.constraint.strings; *p; p += lstrlenW(p) + 1)
145 if (!wcscmp( p, L"Lineart" )) choices[(*count)++] = TWPT_BW;
146 else if (!wcscmp( p, L"Color" )) choices[(*count)++] = TWPT_RGB;
147 else if (!wcsncmp( p, L"Gray", 4 )) choices[(*count)++] = TWPT_GRAY;
150 return rc;
153 TW_UINT16 sane_option_get_scan_area( int *tlx, int *tly, int *brx, int *bry )
155 TW_UINT16 rc;
156 struct option_descriptor opt;
158 if ((rc = sane_find_option( "tl-x", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
159 if ((rc = sane_option_get_value( opt.optno, tlx )) != TWCC_SUCCESS) return rc;
160 if ((rc = sane_find_option( "tl-y", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
161 if ((rc = sane_option_get_value( opt.optno, tly )) != TWCC_SUCCESS) return rc;
162 if ((rc = sane_find_option( "br-x", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
163 if ((rc = sane_option_get_value( opt.optno, brx )) != TWCC_SUCCESS) return rc;
164 if ((rc = sane_find_option( "br-y", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
165 if ((rc = sane_option_get_value( opt.optno, bry )) != TWCC_SUCCESS) return rc;
166 if (opt.unit != UNIT_MM) FIXME( "unsupported unit %u\n", opt.unit );
167 return rc;
170 TW_UINT16 sane_option_get_max_scan_area( int *tlx, int *tly, int *brx, int *bry )
172 TW_UINT16 rc;
173 struct option_descriptor opt;
175 if ((rc = sane_find_option( "tl-x", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
176 *tlx = opt.constraint.range.min;
177 if ((rc = sane_find_option( "tl-y", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
178 *tly = opt.constraint.range.min;
179 if ((rc = sane_find_option( "br-x", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
180 *brx = opt.constraint.range.max;
181 if ((rc = sane_find_option( "br-y", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
182 *bry = opt.constraint.range.max;
183 if (opt.unit != UNIT_MM) FIXME( "unsupported unit %u\n", opt.unit );
184 return rc;
187 TW_UINT16 sane_option_set_scan_area( int tlx, int tly, int brx, int bry, BOOL *reload )
189 TW_UINT16 rc;
190 struct option_descriptor opt;
192 if ((rc = sane_find_option( "tl-x", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
193 if ((rc = sane_option_set_value( opt.optno, &tlx, reload )) != TWCC_SUCCESS) return rc;
194 if ((rc = sane_find_option( "tl-y", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
195 if ((rc = sane_option_set_value( opt.optno, &tly, reload )) != TWCC_SUCCESS) return rc;
196 if ((rc = sane_find_option( "br-x", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
197 if ((rc = sane_option_set_value( opt.optno, &brx, reload )) != TWCC_SUCCESS) return rc;
198 if ((rc = sane_find_option( "br-y", TYPE_FIXED, &opt )) != TWCC_SUCCESS) return rc;
199 if ((rc = sane_option_set_value( opt.optno, &bry, reload )) != TWCC_SUCCESS) return rc;
200 return rc;
203 TW_FIX32 convert_sane_res_to_twain(int res)
205 TW_FIX32 value;
206 res = MulDiv( res, 10, 254 ); /* mm -> inch */
207 value.Whole = res / 65536;
208 value.Frac = res & 0xffff;
209 return value;
212 TW_UINT16 get_sane_params( struct frame_parameters *params )
214 return SANE_CALL( get_params, params );