Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
keyhelp.c
Go to the documentation of this file.
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  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22 
23 #include <linux/keyboard.h>
24 #include "spk_priv.h"
25 #include "speakup.h"
26 
27 #define MAXFUNCS 130
28 #define MAXKEYS 256
29 static const int num_key_names = MSG_KEYNAMES_END - MSG_KEYNAMES_START + 1;
30 static u_short key_offsets[MAXFUNCS], key_data[MAXKEYS];
31 static u_short masks[] = { 32, 16, 8, 4, 2, 1 };
32 
33 static short letter_offsets[26] = {
34  -1, -1, -1, -1, -1, -1, -1, -1,
35  -1, -1, -1, -1, -1, -1, -1, -1,
36  -1, -1, -1, -1, -1, -1, -1, -1,
37  -1, -1 };
38 
39 static u_char funcvals[] = {
57 };
58 
59 static u_char *state_tbl;
60 static int cur_item, nstates;
61 
62 static void build_key_data(void)
63 {
64  u_char *kp, counters[MAXFUNCS], ch, ch1;
65  u_short *p_key = key_data, key;
66  int i, offset = 1;
67  nstates = (int)(state_tbl[-1]);
68  memset(counters, 0, sizeof(counters));
69  memset(key_offsets, 0, sizeof(key_offsets));
70  kp = state_tbl + nstates + 1;
71  while (*kp++) {
72  /* count occurrences of each function */
73  for (i = 0; i < nstates; i++, kp++) {
74  if (!*kp)
75  continue;
76  if ((state_tbl[i]&16) != 0 && *kp == SPK_KEY)
77  continue;
78  counters[*kp]++;
79  }
80  }
81  for (i = 0; i < MAXFUNCS; i++) {
82  if (counters[i] == 0)
83  continue;
84  key_offsets[i] = offset;
85  offset += (counters[i]+1);
86  if (offset >= MAXKEYS)
87  break;
88  }
89 /* leave counters set so high keycodes come first.
90  * this is done so num pad and other extended keys maps are spoken before
91  * the alpha with speakup type mapping.
92  */
93  kp = state_tbl + nstates + 1;
94  while ((ch = *kp++)) {
95  for (i = 0; i < nstates; i++) {
96  ch1 = *kp++;
97  if (!ch1)
98  continue;
99  if ((state_tbl[i]&16) != 0 && ch1 == SPK_KEY)
100  continue;
101  key = (state_tbl[i] << 8) + ch;
102  counters[ch1]--;
103  offset = key_offsets[ch1];
104  if (!offset)
105  continue;
106  p_key = key_data + offset + counters[ch1];
107  *p_key = key;
108  }
109  }
110 }
111 
112 static void say_key(int key)
113 {
114  int i, state = key >> 8;
115  key &= 0xff;
116  for (i = 0; i < 6; i++) {
117  if (state & masks[i])
119  }
120  if ((key > 0) && (key <= num_key_names))
121  synth_printf(" %s\n", msg_get(MSG_KEYNAMES_START + (key - 1)));
122 }
123 
124 static int help_init(void)
125 {
126  char start = SPACE;
127  int i;
129 state_tbl = our_keys[0]+SHIFT_TBL_SIZE+2;
130  for (i = 0; i < num_funcs; i++) {
131  char *cur_funcname = msg_get(MSG_FUNCNAMES_START + i);
132  if (start == *cur_funcname)
133  continue;
134  start = *cur_funcname;
135  letter_offsets[(start&31)-1] = i;
136  }
137  return 0;
138 }
139 
141 {
142  int i, n;
143  char *name;
144  u_char func, *kp;
145  u_short *p_keys, val;
146  if (letter_offsets[0] == -1)
147  help_init();
148  if (type == KT_LATIN) {
149  if (ch == SPACE) {
152  return 1;
153  }
154  ch |= 32; /* lower case */
155  if (ch < 'a' || ch > 'z')
156  return -1;
157  if (letter_offsets[ch-'a'] == -1) {
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) <=
167  cur_item++;
168  else if (ch == 3 && cur_item > 0)
169  cur_item--;
170  else
171  return -1;
172  } else if (type == KT_SPKUP && ch == SPEAKUP_HELP && !special_handler) {
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  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 = msg_get(MSG_FUNCNAMES_START + i);
187  }
188  if (!name)
189  return -1;
190  kp = 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);
198  synth_printf("\n");
199  return 1;
200  }
201  name = msg_get(MSG_FUNCNAMES_START + cur_item);
202  func = funcvals[cur_item];
203  synth_printf("%s", name);
204  if (key_offsets[func] == 0) {
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)
213  say_key(val);
214  }
215  return 1;
216 }