push 297972334720102b8c295c34d3cd5b0b9aafc558
[wine/hacks.git] / dlls / sane.ds / options.c
blob98d83dcf48903a23e6ed3e3a1451ee6173476af5
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 "config.h"
21 #include <stdlib.h>
22 #include "twain.h"
23 #include "sane_i.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(twain);
28 #ifdef SONAME_LIBSANE
29 static SANE_Status sane_find_option(SANE_Handle h, const char *option_name,
30 const SANE_Option_Descriptor **opt_p, int *optno, SANE_Value_Type type)
32 SANE_Status rc;
33 SANE_Int optcount;
34 const SANE_Option_Descriptor *opt;
35 int i;
37 /* Debian, in 32_net_backend_standard_fix.dpatch,
38 * forces a frontend (that's us) to reload options
39 * manually by invoking get_option_descriptor. */
40 opt = psane_get_option_descriptor(h, 0);
41 if (! opt)
42 return SANE_STATUS_EOF;
44 rc = psane_control_option(h, 0, SANE_ACTION_GET_VALUE, &optcount, NULL);
45 if (rc != SANE_STATUS_GOOD)
46 return rc;
48 for (i = 1; i < optcount; i++)
50 opt = psane_get_option_descriptor(h, i);
51 if (opt && (opt->name && strcmp(opt->name, option_name) == 0) &&
52 opt->type == type)
54 *opt_p = opt;
55 *optno = i;
56 return SANE_STATUS_GOOD;
59 return SANE_STATUS_EOF;
62 SANE_Status sane_option_get_int(SANE_Handle h, const char *option_name, SANE_Int *val)
64 SANE_Status rc;
65 int optno;
66 const SANE_Option_Descriptor *opt;
68 rc = sane_find_option(h, option_name, &opt, &optno, SANE_TYPE_INT);
69 if (rc != SANE_STATUS_GOOD)
70 return rc;
72 return psane_control_option(h, optno, SANE_ACTION_GET_VALUE, val, NULL);
75 SANE_Status sane_option_set_int(SANE_Handle h, const char *option_name, SANE_Int val, SANE_Int *status)
77 SANE_Status rc;
78 int optno;
79 const SANE_Option_Descriptor *opt;
81 rc = sane_find_option(h, option_name, &opt, &optno, SANE_TYPE_INT);
82 if (rc != SANE_STATUS_GOOD)
83 return rc;
85 return psane_control_option(h, optno, SANE_ACTION_SET_VALUE, (void *) &val, status);
88 /* Important: SANE has the side effect of of overwriting val with the returned value */
89 SANE_Status sane_option_set_str(SANE_Handle h, const char *option_name, SANE_String val, SANE_Int *status)
91 SANE_Status rc;
92 int optno;
93 const SANE_Option_Descriptor *opt;
95 rc = sane_find_option(h, option_name, &opt, &optno, SANE_TYPE_STRING);
96 if (rc != SANE_STATUS_GOOD)
97 return rc;
99 return psane_control_option(h, optno, SANE_ACTION_SET_VALUE, (void *) val, status);
102 SANE_Status sane_option_probe_resolution(SANE_Handle h, const char *option_name, SANE_Int *minval, SANE_Int *maxval, SANE_Int *quant)
104 SANE_Status rc;
105 int optno;
106 const SANE_Option_Descriptor *opt;
108 rc = sane_find_option(h, option_name, &opt, &optno, SANE_TYPE_INT);
109 if (rc != SANE_STATUS_GOOD)
110 return rc;
112 if (opt->constraint_type != SANE_CONSTRAINT_RANGE)
113 return SANE_STATUS_UNSUPPORTED;
115 *minval = opt->constraint.range->min;
116 *maxval = opt->constraint.range->max;
117 *quant = opt->constraint.range->quant;
119 return rc;
122 SANE_Status sane_option_probe_mode(SANE_Handle h, SANE_String_Const **choices, char *current, int current_size)
124 SANE_Status rc;
125 int optno;
126 const SANE_Option_Descriptor *opt;
127 rc = sane_find_option(h, "mode", &opt, &optno, SANE_TYPE_STRING);
128 if (rc != SANE_STATUS_GOOD)
129 return rc;
131 if (choices && opt->constraint_type == SANE_CONSTRAINT_STRING_LIST)
132 *choices = (SANE_String_Const *) opt->constraint.string_list;
134 if (opt->size < current_size)
135 return psane_control_option(h, optno, SANE_ACTION_GET_VALUE, current, NULL);
136 else
137 return SANE_STATUS_NO_MEM;
140 #endif