projects/linux·blob master · refs

216 lines · 5.6 KiB

1// SPDX-License-Identifier: GPL-2.0+
2/* speakup_keyhelp.c
3 * help module for speakup
4 *
5 *written by David Borowski.
6 *
7 * Copyright (C) 2003 David Borowski.
8 */
9
10#include <linux/keyboard.h>
11#include <linux/ctype.h>
12#include "spk_priv.h"
13#include "speakup.h"
14
15#define MAXFUNCS 130
16#define MAXKEYS 256
17static const int num_key_names = MSG_KEYNAMES_END - MSG_KEYNAMES_START + 1;
18static u16 key_offsets[MAXFUNCS], key_data[MAXKEYS];
19static u16 masks[] = { 32, 16, 8, 4, 2, 1 };
20
21static short letter_offsets[26] = {
22 -1, -1, -1, -1, -1, -1, -1, -1,
23 -1, -1, -1, -1, -1, -1, -1, -1,
24 -1, -1, -1, -1, -1, -1, -1, -1,
25 -1, -1 };
26
27static u_char funcvals[] = {
28 ATTRIB_BLEEP_DEC, ATTRIB_BLEEP_INC, BLEEPS_DEC, BLEEPS_INC,
29 SAY_FIRST_CHAR, SAY_LAST_CHAR, SAY_CHAR, SAY_CHAR_NUM,
30 SAY_NEXT_CHAR, SAY_PHONETIC_CHAR, SAY_PREV_CHAR, SPEAKUP_PARKED,
31 SPEAKUP_CUT, EDIT_DELIM, EDIT_EXNUM, EDIT_MOST,
32 EDIT_REPEAT, EDIT_SOME, SPEAKUP_GOTO, BOTTOM_EDGE,
33 LEFT_EDGE, RIGHT_EDGE, TOP_EDGE, SPEAKUP_HELP,
34 SAY_LINE, SAY_NEXT_LINE, SAY_PREV_LINE, SAY_LINE_INDENT,
35 SPEAKUP_PASTE, PITCH_DEC, PITCH_INC, PUNCT_DEC,
36 PUNCT_INC, PUNC_LEVEL_DEC, PUNC_LEVEL_INC, SPEAKUP_QUIET,
37 RATE_DEC, RATE_INC, READING_PUNC_DEC, READING_PUNC_INC,
38 SAY_ATTRIBUTES, SAY_FROM_LEFT, SAY_FROM_TOP, SAY_POSITION,
39 SAY_SCREEN, SAY_TO_BOTTOM, SAY_TO_RIGHT, SPK_KEY,
40 SPK_LOCK, SPEAKUP_OFF, SPEECH_KILL, SPELL_DELAY_DEC,
41 SPELL_DELAY_INC, SPELL_WORD, SPELL_PHONETIC, TONE_DEC,
42 TONE_INC, VOICE_DEC, VOICE_INC, VOL_DEC,
43 VOL_INC, CLEAR_WIN, SAY_WIN, SET_WIN,
44 ENABLE_WIN, SAY_WORD, SAY_NEXT_WORD, SAY_PREV_WORD, 0
45};
46
47static u_char *state_tbl;
48static int cur_item, nstates;
49
50static void build_key_data(void)
51{
52 u_char *kp, counters[MAXFUNCS], ch, ch1;
53 u16 *p_key, key;
54 int i, offset = 1;
55
56 nstates = (int)(state_tbl[-1]);
57 memset(counters, 0, sizeof(counters));
58 memset(key_offsets, 0, sizeof(key_offsets));
59 kp = state_tbl + nstates + 1;
60 while (*kp++) {
61 /* count occurrences of each function */
62 for (i = 0; i < nstates; i++, kp++) {
63 if (!*kp)
64 continue;
65 if ((state_tbl[i] & 16) != 0 && *kp == SPK_KEY)
66 continue;
67 counters[*kp]++;
68 }
69 }
70 for (i = 0; i < MAXFUNCS; i++) {
71 if (counters[i] == 0)
72 continue;
73 key_offsets[i] = offset;
74 offset += (counters[i] + 1);
75 if (offset >= MAXKEYS)
76 break;
77 }
78/* leave counters set so high keycodes come first.
79 * this is done so num pad and other extended keys maps are spoken before
80 * the alpha with speakup type mapping.
81 */
82 kp = state_tbl + nstates + 1;
83 while ((ch = *kp++)) {
84 for (i = 0; i < nstates; i++) {
85 ch1 = *kp++;
86 if (!ch1)
87 continue;
88 if ((state_tbl[i] & 16) != 0 && ch1 == SPK_KEY)
89 continue;
90 key = (state_tbl[i] << 8) + ch;
91 counters[ch1]--;
92 offset = key_offsets[ch1];
93 if (!offset)
94 continue;
95 p_key = key_data + offset + counters[ch1];
96 *p_key = key;
97 }
98 }
99}
100
101static void say_key(int key)
102{
103 int i, state = key >> 8;
104
105 key &= 0xff;
106 for (i = 0; i < 6; i++) {
107 if (state & masks[i])
108 synth_printf(" %s", spk_msg_get(MSG_STATES_START + i));
109 }
110 if ((key > 0) && (key <= num_key_names))
111 synth_printf(" %s\n",
112 spk_msg_get(MSG_KEYNAMES_START + (key - 1)));
113}
114
115static void help_init(void)
116{
117 char start = SPACE;
118 int i;
119 int num_funcs = MSG_FUNCNAMES_END - MSG_FUNCNAMES_START + 1;
120
121 state_tbl = spk_our_keys[0] + SHIFT_TBL_SIZE + 2;
122 for (i = 0; i < num_funcs; i++) {
123 char *cur_funcname = spk_msg_get(MSG_FUNCNAMES_START + i);
124 char first_letter;
125
126 first_letter = tolower(*cur_funcname);
127
128 /* Accept only 'a'..'z' to index letter_offsets[] safely */
129 if (first_letter < 'a' || first_letter > 'z')
130 continue;
131
132 if (start == first_letter)
133 continue;
134 start = first_letter;
135 letter_offsets[(start & 31) - 1] = i;
136 }
137}
138
139int spk_handle_help(struct vc_data *vc, u_char type, u_char ch, u16 key)
140{
141 int i, n;
142 char *name;
143 u_char func, *kp;
144 u16 *p_keys, val;
145
146 if (letter_offsets[0] == -1)
147 help_init();
148 if (type == KT_LATIN) {
149 if (ch == SPACE) {
150 spk_special_handler = NULL;
151 synth_printf("%s\n", spk_msg_get(MSG_LEAVING_HELP));
152 return 1;
153 }
154 ch = tolower(ch);
155 if (ch < 'a' || ch > 'z')
156 return -1;
157 if (letter_offsets[ch - 'a'] == -1) {
158 synth_printf(spk_msg_get(MSG_NO_COMMAND), ch);
159 synth_printf("\n");
160 return 1;
161 }
162 cur_item = letter_offsets[ch - 'a'];
163 } else if (type == KT_CUR) {
164 if (ch == 0 &&
165 (MSG_FUNCNAMES_START + cur_item + 1) <= MSG_FUNCNAMES_END)
166 cur_item++;
167 else if (ch == 3 && cur_item > 0)
168 cur_item--;
169 else
170 return -1;
171 } else if (type == KT_SPKUP && ch == SPEAKUP_HELP &&
172 !spk_special_handler) {
173 spk_special_handler = spk_handle_help;
174 synth_printf("%s\n", spk_msg_get(MSG_HELP_INFO));
175 build_key_data(); /* rebuild each time in case new mapping */
176 return 1;
177 } else {
178 name = NULL;
179 if ((type != KT_SPKUP) && (key > 0) && (key <= num_key_names)) {
180 synth_printf("%s\n",
181 spk_msg_get(MSG_KEYNAMES_START + key - 1));
182 return 1;
183 }
184 for (i = 0; funcvals[i] != 0 && !name; i++) {
185 if (ch == funcvals[i])
186 name = spk_msg_get(MSG_FUNCNAMES_START + i);
187 }
188 if (!name)
189 return -1;
190 kp = spk_our_keys[key] + 1;
191 for (i = 0; i < nstates; i++) {
192 if (ch == kp[i])
193 break;
194 }
195 key += (state_tbl[i] << 8);
196 say_key(key);
197 synth_printf(spk_msg_get(MSG_KEYDESC), name);
198 synth_printf("\n");
199 return 1;
200 }
201 name = spk_msg_get(MSG_FUNCNAMES_START + cur_item);
202 func = funcvals[cur_item];
203 synth_printf("%s", name);
204 if (key_offsets[func] == 0) {
205 synth_printf(" %s\n", spk_msg_get(MSG_IS_UNASSIGNED));
206 return 1;
207 }
208 p_keys = key_data + key_offsets[func];
209 for (n = 0; p_keys[n]; n++) {
210 val = p_keys[n];
211 if (n > 0)
212 synth_printf("%s ", spk_msg_get(MSG_DISJUNCTION));
213 say_key(val);
214 }
215 return 1;
216}