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