varhandlers.c 7.83 KB
Newer Older
1
2
3
4
5
6
#include <linux/ctype.h>
#include "spk_types.h"
#include "spk_priv.h"
#include "speakup.h"

static struct st_var_header var_headers[] = {
7
8
9
10
11
12
13
14
15
16
17
18
	{ "version", VERSION, VAR_PROC, NULL, NULL },
	{ "synth_name", SYNTH, VAR_PROC, NULL, NULL },
	{ "keymap", KEYMAP, VAR_PROC, NULL, NULL },
	{ "silent", SILENT, VAR_PROC, NULL, NULL },
	{ "punc_some", PUNC_SOME, VAR_PROC, NULL, NULL },
	{ "punc_most", PUNC_MOST, VAR_PROC, NULL, NULL },
	{ "punc_all", PUNC_ALL, VAR_PROC, NULL, NULL },
	{ "delimiters", DELIM, VAR_PROC, NULL, NULL },
	{ "repeats", REPEATS, VAR_PROC, NULL, NULL },
	{ "ex_num", EXNUMBER, VAR_PROC, NULL, NULL },
	{ "characters", CHARS, VAR_PROC, NULL, NULL },
	{ "synth_direct", SYNTH_DIRECT, VAR_PROC, NULL, NULL },
19
20
	{ "caps_start", CAPS_START, VAR_STRING, spk_str_caps_start, NULL },
	{ "caps_stop", CAPS_STOP, VAR_STRING, spk_str_caps_stop, NULL },
21
22
23
24
	{ "delay_time", DELAY, VAR_TIME, NULL, NULL },
	{ "trigger_time", TRIGGER, VAR_TIME, NULL, NULL },
	{ "jiffy_delta", JIFFY, VAR_TIME, NULL, NULL },
	{ "full_time", FULL, VAR_TIME, NULL, NULL },
25
26
27
28
	{ "spell_delay", SPELL_DELAY, VAR_NUM, &spk_spell_delay, NULL },
	{ "bleeps", BLEEPS, VAR_NUM, &spk_bleeps, NULL },
	{ "attrib_bleep", ATTRIB_BLEEP, VAR_NUM, &spk_attrib_bleep, NULL },
	{ "bleep_time", BLEEP_TIME, VAR_TIME, &spk_bleep_time, NULL },
29
	{ "cursor_time", CURSOR_TIME, VAR_TIME, NULL, NULL },
30
31
32
33
34
35
36
	{ "punc_level", PUNC_LEVEL, VAR_NUM, &spk_punc_level, NULL },
	{ "reading_punc", READING_PUNC, VAR_NUM, &spk_reading_punc, NULL },
	{ "say_control", SAY_CONTROL, VAR_NUM, &spk_say_ctrl, NULL },
	{ "say_word_ctl", SAY_WORD_CTL, VAR_NUM, &spk_say_word_ctl, NULL },
	{ "no_interrupt", NO_INTERRUPT, VAR_NUM, &spk_no_intr, NULL },
	{ "key_echo", KEY_ECHO, VAR_NUM, &spk_key_echo, NULL },
	{ "bell_pos", BELL_POS, VAR_NUM, &spk_bell_pos, NULL },
37
38
39
40
41
42
43
44
45
46
	{ "rate", RATE, VAR_NUM, NULL, NULL },
	{ "pitch", PITCH, VAR_NUM, NULL, NULL },
	{ "vol", VOL, VAR_NUM, NULL, NULL },
	{ "tone", TONE, VAR_NUM, NULL, NULL },
	{ "punct", PUNCT, VAR_NUM, NULL, NULL   },
	{ "voice", VOICE, VAR_NUM, NULL, NULL },
	{ "freq", FREQUENCY, VAR_NUM, NULL, NULL },
	{ "lang", LANG, VAR_NUM, NULL, NULL },
	{ "chartab", CHARTAB, VAR_PROC, NULL, NULL },
	{ "direct", DIRECT, VAR_NUM, NULL, NULL },
47
48
};

49
static struct st_var_header *var_ptrs[MAXVARS] = { NULL, NULL, NULL };
50
51

static struct punc_var_t punc_vars[] = {
52
53
54
55
56
57
58
	{ PUNC_SOME, 1 },
	{ PUNC_MOST, 2 },
	{ PUNC_ALL, 3 },
	{ DELIM, 4 },
	{ REPEATS, 5 },
	{ EXNUMBER, 6 },
	{ -1, -1 },
59
60
};

61
int spk_chartab_get_value(char *keyword)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{
	int value = 0;

	if (!strcmp(keyword, "ALPHA"))
		value = ALPHA;
	else if (!strcmp(keyword, "B_CTL"))
		value = B_CTL;
	else if (!strcmp(keyword, "WDLM"))
		value = WDLM;
	else if (!strcmp(keyword, "A_PUNC"))
		value = A_PUNC;
	else if (!strcmp(keyword, "PUNC"))
		value = PUNC;
	else if (!strcmp(keyword, "NUM"))
		value = NUM;
	else if (!strcmp(keyword, "A_CAP"))
		value = A_CAP;
	else if (!strcmp(keyword, "B_CAPSYM"))
		value = B_CAPSYM;
	else if (!strcmp(keyword, "B_SYM"))
		value = B_SYM;
	return value;
}

void speakup_register_var(struct var_t *var)
{
	static char nothing[2] = "\0";
	int i;
	struct st_var_header *p_header;

	BUG_ON(!var || var->var_id < 0 || var->var_id >= MAXVARS);
93
	if (!var_ptrs[0]) {
94
95
96
97
98
99
100
101
102
103
104
105
		for (i = 0; i < MAXVARS; i++) {
			p_header = &var_headers[i];
			var_ptrs[p_header->var_id] = p_header;
			p_header->data = NULL;
		}
	}
	p_header = var_ptrs[var->var_id];
	if (p_header->data != NULL)
		return;
	p_header->data = var;
	switch (p_header->var_type) {
	case VAR_STRING:
106
		spk_set_string_var(nothing, p_header, 0);
107
108
109
		break;
	case VAR_NUM:
	case VAR_TIME:
110
		spk_set_num_var(0, p_header, E_DEFAULT);
111
112
113
114
115
116
117
118
119
		break;
	default:
		break;
	}
}

void speakup_unregister_var(enum var_id_t var_id)
{
	struct st_var_header *p_header;
120

121
122
123
124
125
	BUG_ON(var_id < 0 || var_id >= MAXVARS);
	p_header = var_ptrs[var_id];
	p_header->data = NULL;
}

126
struct st_var_header *spk_get_var_header(enum var_id_t var_id)
127
128
{
	struct st_var_header *p_header;
129

130
131
132
	if (var_id < 0 || var_id >= MAXVARS)
		return NULL;
	p_header = var_ptrs[var_id];
133
	if (!p_header->data)
134
135
136
137
		return NULL;
	return p_header;
}

138
struct st_var_header *spk_var_header_by_name(const char *name)
139
140
{
	int i;
141
142
143
144
145
146
147

	if (!name)
		return NULL;

	for (i = 0; i < MAXVARS; i++) {
		if (strcmp(name, var_ptrs[i]->name) == 0)
			return var_ptrs[i];
148
	}
149
	return NULL;
150
151
}

152
struct var_t *spk_get_var(enum var_id_t var_id)
153
154
{
	BUG_ON(var_id < 0 || var_id >= MAXVARS);
155
156
	BUG_ON(!var_ptrs[var_id]);
	return var_ptrs[var_id]->data;
157
}
158
EXPORT_SYMBOL_GPL(spk_get_var);
159

160
struct punc_var_t *spk_get_punc_var(enum var_id_t var_id)
161
162
163
164
165
{
	struct punc_var_t *rv = NULL;
	struct punc_var_t *where;

	where = punc_vars;
166
	while ((where->var_id != -1) && (!rv)) {
167
168
169
170
171
172
173
174
175
		if (where->var_id == var_id)
			rv = where;
		else
			where++;
	}
	return rv;
}

/* handlers for setting vars */
176
int spk_set_num_var(int input, struct st_var_header *var, int how)
177
178
179
180
181
182
183
{
	int val;
	int *p_val = var->p_val;
	int l;
	char buf[32];
	char *cp;
	struct var_t *var_data = var->data;
184

185
	if (!var_data)
186
		return -ENODATA;
187

188
189
190
	val = var_data->u.n.value;
	switch (how) {
	case E_NEW_DEFAULT:
191
		if (input < var_data->u.n.low || input > var_data->u.n.high)
192
193
194
			return -ERANGE;
		var_data->u.n.default_val = input;
		return 0;
195
	case E_DEFAULT:
196
		val = var_data->u.n.default_val;
197
198
199
200
201
202
203
204
205
206
		break;
	case E_SET:
		val = input;
		break;
	case E_INC:
		val += input;
		break;
	case E_DEC:
		val -= input;
		break;
207
	}
208
209
210
211

	if (val < var_data->u.n.low || val > var_data->u.n.high)
		return -ERANGE;

212
213
214
	var_data->u.n.value = val;
	if (var->var_type == VAR_TIME && p_val != NULL) {
		*p_val = msecs_to_jiffies(val);
215
		return 0;
216
217
218
219
	}
	if (p_val != NULL)
		*p_val = val;
	if (var->var_id == PUNC_LEVEL) {
220
		spk_punc_mask = spk_punc_masks[val];
221
		return 0;
222
223
224
225
	}
	if (var_data->u.n.multiplier != 0)
		val *= var_data->u.n.multiplier;
	val += var_data->u.n.offset;
226
	if (var->var_id < FIRST_SYNTH_VAR || !synth)
227
228
229
		return 0;
	if (synth->synth_adjust)
		return synth->synth_adjust(var);
230

231
	if (!var_data->u.n.synth_fmt)
232
		return 0;
233
	if (var->var_id == PITCH)
234
		cp = spk_pitch_buff;
235
236
237
238
239
	else
		cp = buf;
	if (!var_data->u.n.out_str)
		l = sprintf(cp, var_data->u.n.synth_fmt, (int)val);
	else
240
		l = sprintf(cp,	var_data->u.n.synth_fmt, var_data->u.n.out_str[val]);
241
	synth_printf("%s", cp);
242
	return 0;
243
244
}

245
int spk_set_string_var(const char *page, struct st_var_header *var, int len)
246
247
{
	struct var_t *var_data = var->data;
248

249
	if (!var_data)
250
		return -ENODATA;
251
	if (len > MAXVARLEN)
252
		return -E2BIG;
253
	if (!len) {
254
255
		if (!var_data->u.s.default_val)
			return 0;
256
257
258
259
		if (!var->p_val)
			var->p_val = var_data->u.s.default_val;
		if (var->p_val != var_data->u.s.default_val)
			strcpy((char *)var->p_val, var_data->u.s.default_val);
260
		return -ERESTART;
261
262
	} else if (var->p_val)
		strcpy((char *)var->p_val, page);
263
	else
264
265
		return -E2BIG;
	return 0;
266
267
}

268
269
/*
 * spk_set_mask_bits sets or clears the punc/delim/repeat bits,
270
271
 * if input is null uses the defaults.
 * values for how: 0 clears bits of chars supplied,
272
273
 * 1 clears allk, 2 sets bits for chars
 */
274
int spk_set_mask_bits(const char *input, const int which, const int how)
275
276
{
	u_char *cp;
277
	short mask = spk_punc_info[which].mask;
278

279
	if (how & 1) {
280
		for (cp = (u_char *)spk_punc_info[3].value; *cp; cp++)
281
282
283
			spk_chartab[*cp] &= ~mask;
	}
	cp = (u_char *)input;
284
	if (!cp)
285
		cp = spk_punc_info[which].value;
286
	else {
287
		for (; *cp; cp++) {
288
289
290
			if (*cp < SPACE)
				break;
			if (mask < PUNC) {
291
				if (!(spk_chartab[*cp] & PUNC))
292
					break;
293
			} else if (spk_chartab[*cp] & B_NUM)
294
295
296
297
298
299
				break;
		}
		if (*cp)
			return -EINVAL;
		cp = (u_char *)input;
	}
300
	if (how & 2) {
301
		for (; *cp; cp++)
302
303
304
			if (*cp > SPACE)
				spk_chartab[*cp] |= mask;
	} else {
305
		for (; *cp; cp++)
306
307
308
309
310
311
			if (*cp > SPACE)
				spk_chartab[*cp] &= ~mask;
	}
	return 0;
}

312
char *spk_strlwr(char *s)
313
314
{
	char *p;
315

316
	if (!s)
317
318
319
320
321
322
323
		return NULL;

	for (p = s; *p; p++)
		*p = tolower(*p);
	return s;
}

324
char *spk_s2uchar(char *start, char *dest)
325
{
326
	int val;
327

328
	val = simple_strtoul(skip_spaces(start), &start, 10);
329
330
331
332
333
	if (*start == ',')
		start++;
	*dest = (u_char)val;
	return start;
}