Linux 4.2.1
[linux/fpc-iii.git] / drivers / staging / speakup / varhandlers.c
blob1b0d1c08741f9a1d05dd0606f20c1f55f8069e7e
1 #include <linux/ctype.h>
2 #include "spk_types.h"
3 #include "spk_priv.h"
4 #include "speakup.h"
6 static struct st_var_header var_headers[] = {
7 { "version", VERSION, VAR_PROC, NULL, NULL },
8 { "synth_name", SYNTH, VAR_PROC, NULL, NULL },
9 { "keymap", KEYMAP, VAR_PROC, NULL, NULL },
10 { "silent", SILENT, VAR_PROC, NULL, NULL },
11 { "punc_some", PUNC_SOME, VAR_PROC, NULL, NULL },
12 { "punc_most", PUNC_MOST, VAR_PROC, NULL, NULL },
13 { "punc_all", PUNC_ALL, VAR_PROC, NULL, NULL },
14 { "delimiters", DELIM, VAR_PROC, NULL, NULL },
15 { "repeats", REPEATS, VAR_PROC, NULL, NULL },
16 { "ex_num", EXNUMBER, VAR_PROC, NULL, NULL },
17 { "characters", CHARS, VAR_PROC, NULL, NULL },
18 { "synth_direct", SYNTH_DIRECT, VAR_PROC, NULL, NULL },
19 { "caps_start", CAPS_START, VAR_STRING, spk_str_caps_start, NULL },
20 { "caps_stop", CAPS_STOP, VAR_STRING, spk_str_caps_stop, NULL },
21 { "delay_time", DELAY, VAR_TIME, NULL, NULL },
22 { "trigger_time", TRIGGER, VAR_TIME, NULL, NULL },
23 { "jiffy_delta", JIFFY, VAR_TIME, NULL, NULL },
24 { "full_time", FULL, VAR_TIME, NULL, NULL },
25 { "spell_delay", SPELL_DELAY, VAR_NUM, &spk_spell_delay, NULL },
26 { "bleeps", BLEEPS, VAR_NUM, &spk_bleeps, NULL },
27 { "attrib_bleep", ATTRIB_BLEEP, VAR_NUM, &spk_attrib_bleep, NULL },
28 { "bleep_time", BLEEP_TIME, VAR_TIME, &spk_bleep_time, NULL },
29 { "cursor_time", CURSOR_TIME, VAR_TIME, NULL, NULL },
30 { "punc_level", PUNC_LEVEL, VAR_NUM, &spk_punc_level, NULL },
31 { "reading_punc", READING_PUNC, VAR_NUM, &spk_reading_punc, NULL },
32 { "say_control", SAY_CONTROL, VAR_NUM, &spk_say_ctrl, NULL },
33 { "say_word_ctl", SAY_WORD_CTL, VAR_NUM, &spk_say_word_ctl, NULL },
34 { "no_interrupt", NO_INTERRUPT, VAR_NUM, &spk_no_intr, NULL },
35 { "key_echo", KEY_ECHO, VAR_NUM, &spk_key_echo, NULL },
36 { "bell_pos", BELL_POS, VAR_NUM, &spk_bell_pos, NULL },
37 { "rate", RATE, VAR_NUM, NULL, NULL },
38 { "pitch", PITCH, VAR_NUM, NULL, NULL },
39 { "vol", VOL, VAR_NUM, NULL, NULL },
40 { "tone", TONE, VAR_NUM, NULL, NULL },
41 { "punct", PUNCT, VAR_NUM, NULL, NULL },
42 { "voice", VOICE, VAR_NUM, NULL, NULL },
43 { "freq", FREQUENCY, VAR_NUM, NULL, NULL },
44 { "lang", LANG, VAR_NUM, NULL, NULL },
45 { "chartab", CHARTAB, VAR_PROC, NULL, NULL },
46 { "direct", DIRECT, VAR_NUM, NULL, NULL },
49 static struct st_var_header *var_ptrs[MAXVARS] = { NULL, NULL, NULL };
51 static struct punc_var_t punc_vars[] = {
52 { PUNC_SOME, 1 },
53 { PUNC_MOST, 2 },
54 { PUNC_ALL, 3 },
55 { DELIM, 4 },
56 { REPEATS, 5 },
57 { EXNUMBER, 6 },
58 { -1, -1 },
61 int spk_chartab_get_value(char *keyword)
63 int value = 0;
65 if (!strcmp(keyword, "ALPHA"))
66 value = ALPHA;
67 else if (!strcmp(keyword, "B_CTL"))
68 value = B_CTL;
69 else if (!strcmp(keyword, "WDLM"))
70 value = WDLM;
71 else if (!strcmp(keyword, "A_PUNC"))
72 value = A_PUNC;
73 else if (!strcmp(keyword, "PUNC"))
74 value = PUNC;
75 else if (!strcmp(keyword, "NUM"))
76 value = NUM;
77 else if (!strcmp(keyword, "A_CAP"))
78 value = A_CAP;
79 else if (!strcmp(keyword, "B_CAPSYM"))
80 value = B_CAPSYM;
81 else if (!strcmp(keyword, "B_SYM"))
82 value = B_SYM;
83 return value;
86 void speakup_register_var(struct var_t *var)
88 static char nothing[2] = "\0";
89 int i;
90 struct st_var_header *p_header;
92 BUG_ON(!var || var->var_id < 0 || var->var_id >= MAXVARS);
93 if (var_ptrs[0] == NULL) {
94 for (i = 0; i < MAXVARS; i++) {
95 p_header = &var_headers[i];
96 var_ptrs[p_header->var_id] = p_header;
97 p_header->data = NULL;
100 p_header = var_ptrs[var->var_id];
101 if (p_header->data != NULL)
102 return;
103 p_header->data = var;
104 switch (p_header->var_type) {
105 case VAR_STRING:
106 spk_set_string_var(nothing, p_header, 0);
107 break;
108 case VAR_NUM:
109 case VAR_TIME:
110 spk_set_num_var(0, p_header, E_DEFAULT);
111 break;
112 default:
113 break;
117 void speakup_unregister_var(enum var_id_t var_id)
119 struct st_var_header *p_header;
121 BUG_ON(var_id < 0 || var_id >= MAXVARS);
122 p_header = var_ptrs[var_id];
123 p_header->data = NULL;
126 struct st_var_header *spk_get_var_header(enum var_id_t var_id)
128 struct st_var_header *p_header;
130 if (var_id < 0 || var_id >= MAXVARS)
131 return NULL;
132 p_header = var_ptrs[var_id];
133 if (p_header->data == NULL)
134 return NULL;
135 return p_header;
138 struct st_var_header *spk_var_header_by_name(const char *name)
140 int i;
142 if (!name)
143 return NULL;
145 for (i = 0; i < MAXVARS; i++) {
146 if (strcmp(name, var_ptrs[i]->name) == 0)
147 return var_ptrs[i];
149 return NULL;
152 struct var_t *spk_get_var(enum var_id_t var_id)
154 BUG_ON(var_id < 0 || var_id >= MAXVARS);
155 BUG_ON(!var_ptrs[var_id]);
156 return var_ptrs[var_id]->data;
158 EXPORT_SYMBOL_GPL(spk_get_var);
160 struct punc_var_t *spk_get_punc_var(enum var_id_t var_id)
162 struct punc_var_t *rv = NULL;
163 struct punc_var_t *where;
165 where = punc_vars;
166 while ((where->var_id != -1) && (rv == NULL)) {
167 if (where->var_id == var_id)
168 rv = where;
169 else
170 where++;
172 return rv;
175 /* handlers for setting vars */
176 int spk_set_num_var(int input, struct st_var_header *var, int how)
178 int val;
179 short ret = 0;
180 int *p_val = var->p_val;
181 int l;
182 char buf[32];
183 char *cp;
184 struct var_t *var_data = var->data;
186 if (var_data == NULL)
187 return -ENODATA;
189 if (how == E_NEW_DEFAULT) {
190 if (input < var_data->u.n.low || input > var_data->u.n.high)
191 return -ERANGE;
192 var_data->u.n.default_val = input;
193 return 0;
195 if (how == E_DEFAULT) {
196 val = var_data->u.n.default_val;
197 ret = -ERESTART;
198 } else {
199 if (how == E_SET)
200 val = input;
201 else
202 val = var_data->u.n.value;
203 if (how == E_INC)
204 val += input;
205 else if (how == E_DEC)
206 val -= input;
207 if (val < var_data->u.n.low || val > var_data->u.n.high)
208 return -ERANGE;
210 var_data->u.n.value = val;
211 if (var->var_type == VAR_TIME && p_val != NULL) {
212 *p_val = msecs_to_jiffies(val);
213 return ret;
215 if (p_val != NULL)
216 *p_val = val;
217 if (var->var_id == PUNC_LEVEL) {
218 spk_punc_mask = spk_punc_masks[val];
219 return ret;
221 if (var_data->u.n.multiplier != 0)
222 val *= var_data->u.n.multiplier;
223 val += var_data->u.n.offset;
224 if (var->var_id < FIRST_SYNTH_VAR || synth == NULL)
225 return ret;
226 if (synth->synth_adjust != NULL) {
227 int status = synth->synth_adjust(var);
229 return (status != 0) ? status : ret;
231 if (!var_data->u.n.synth_fmt)
232 return ret;
233 if (var->var_id == PITCH)
234 cp = spk_pitch_buff;
235 else
236 cp = buf;
237 if (!var_data->u.n.out_str)
238 l = sprintf(cp, var_data->u.n.synth_fmt, (int)val);
239 else
240 l = sprintf(cp,
241 var_data->u.n.synth_fmt, var_data->u.n.out_str[val]);
242 synth_printf("%s", cp);
243 return ret;
246 int spk_set_string_var(const char *page, struct st_var_header *var, int len)
248 struct var_t *var_data = var->data;
250 if (var_data == NULL)
251 return -ENODATA;
252 if (len > MAXVARLEN)
253 return -E2BIG;
254 if (!len) {
255 if (!var_data->u.s.default_val)
256 return 0;
257 if (!var->p_val)
258 var->p_val = var_data->u.s.default_val;
259 if (var->p_val != var_data->u.s.default_val)
260 strcpy((char *)var->p_val, var_data->u.s.default_val);
261 return -ERESTART;
262 } else if (var->p_val)
263 strcpy((char *)var->p_val, page);
264 else
265 return -E2BIG;
266 return 0;
269 /* spk_set_mask_bits sets or clears the punc/delim/repeat bits,
270 * if input is null uses the defaults.
271 * values for how: 0 clears bits of chars supplied,
272 * 1 clears allk, 2 sets bits for chars */
273 int spk_set_mask_bits(const char *input, const int which, const int how)
275 u_char *cp;
276 short mask = spk_punc_info[which].mask;
278 if (how&1) {
279 for (cp = (u_char *)spk_punc_info[3].value; *cp; cp++)
280 spk_chartab[*cp] &= ~mask;
282 cp = (u_char *)input;
283 if (!cp)
284 cp = spk_punc_info[which].value;
285 else {
286 for (; *cp; cp++) {
287 if (*cp < SPACE)
288 break;
289 if (mask < PUNC) {
290 if (!(spk_chartab[*cp]&PUNC))
291 break;
292 } else if (spk_chartab[*cp]&B_NUM)
293 break;
295 if (*cp)
296 return -EINVAL;
297 cp = (u_char *)input;
299 if (how&2) {
300 for (; *cp; cp++)
301 if (*cp > SPACE)
302 spk_chartab[*cp] |= mask;
303 } else {
304 for (; *cp; cp++)
305 if (*cp > SPACE)
306 spk_chartab[*cp] &= ~mask;
308 return 0;
311 char *spk_strlwr(char *s)
313 char *p;
315 if (s == NULL)
316 return NULL;
318 for (p = s; *p; p++)
319 *p = tolower(*p);
320 return s;
323 char *spk_s2uchar(char *start, char *dest)
325 int val = 0;
327 val = simple_strtoul(skip_spaces(start), &start, 10);
328 if (*start == ',')
329 start++;
330 *dest = (u_char)val;
331 return start;