keyhelp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <linux/keyboard.h>
  23. #include "spk_priv.h"
  24. #include "speakup.h"
  25. #define MAXFUNCS 130
  26. #define MAXKEYS 256
  27. static const int num_key_names = MSG_KEYNAMES_END - MSG_KEYNAMES_START + 1;
  28. static u_short key_offsets[MAXFUNCS], key_data[MAXKEYS];
  29. static u_short masks[] = { 32, 16, 8, 4, 2, 1 };
  30. static short letter_offsets[26] = {
  31. -1, -1, -1, -1, -1, -1, -1, -1,
  32. -1, -1, -1, -1, -1, -1, -1, -1,
  33. -1, -1, -1, -1, -1, -1, -1, -1,
  34. -1, -1 };
  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. static u_char *state_tbl;
  55. static int cur_item, nstates;
  56. static void build_key_data(void)
  57. {
  58. u_char *kp, counters[MAXFUNCS], ch, ch1;
  59. u_short *p_key = key_data, key;
  60. int i, offset = 1;
  61. nstates = (int)(state_tbl[-1]);
  62. memset(counters, 0, sizeof(counters));
  63. memset(key_offsets, 0, sizeof(key_offsets));
  64. kp = state_tbl + nstates + 1;
  65. while (*kp++) {
  66. /* count occurrences of each function */
  67. for (i = 0; i < nstates; i++, kp++) {
  68. if (!*kp)
  69. continue;
  70. if ((state_tbl[i]&16) != 0 && *kp == SPK_KEY)
  71. continue;
  72. counters[*kp]++;
  73. }
  74. }
  75. for (i = 0; i < MAXFUNCS; i++) {
  76. if (counters[i] == 0)
  77. continue;
  78. key_offsets[i] = offset;
  79. offset += (counters[i]+1);
  80. if (offset >= MAXKEYS)
  81. break;
  82. }
  83. /* leave counters set so high keycodes come first.
  84. * this is done so num pad and other extended keys maps are spoken before
  85. * the alpha with speakup type mapping.
  86. */
  87. kp = state_tbl + nstates + 1;
  88. while ((ch = *kp++)) {
  89. for (i = 0; i < nstates; i++) {
  90. ch1 = *kp++;
  91. if (!ch1)
  92. continue;
  93. if ((state_tbl[i]&16) != 0 && ch1 == SPK_KEY)
  94. continue;
  95. key = (state_tbl[i] << 8) + ch;
  96. counters[ch1]--;
  97. offset = key_offsets[ch1];
  98. if (!offset)
  99. continue;
  100. p_key = key_data + offset + counters[ch1];
  101. *p_key = key;
  102. }
  103. }
  104. }
  105. static void say_key(int key)
  106. {
  107. int i, state = key >> 8;
  108. key &= 0xff;
  109. for (i = 0; i < 6; i++) {
  110. if (state & masks[i])
  111. synth_printf(" %s", spk_msg_get(MSG_STATES_START + i));
  112. }
  113. if ((key > 0) && (key <= num_key_names))
  114. synth_printf(" %s\n",
  115. spk_msg_get(MSG_KEYNAMES_START + (key - 1)));
  116. }
  117. static int help_init(void)
  118. {
  119. char start = SPACE;
  120. int i;
  121. int num_funcs = MSG_FUNCNAMES_END - MSG_FUNCNAMES_START + 1;
  122. state_tbl = spk_our_keys[0]+SHIFT_TBL_SIZE+2;
  123. for (i = 0; i < num_funcs; i++) {
  124. char *cur_funcname = spk_msg_get(MSG_FUNCNAMES_START + i);
  125. if (start == *cur_funcname)
  126. continue;
  127. start = *cur_funcname;
  128. letter_offsets[(start&31)-1] = i;
  129. }
  130. return 0;
  131. }
  132. int spk_handle_help(struct vc_data *vc, u_char type, u_char ch, u_short key)
  133. {
  134. int i, n;
  135. char *name;
  136. u_char func, *kp;
  137. u_short *p_keys, val;
  138. if (letter_offsets[0] == -1)
  139. help_init();
  140. if (type == KT_LATIN) {
  141. if (ch == SPACE) {
  142. spk_special_handler = NULL;
  143. synth_printf("%s\n", spk_msg_get(MSG_LEAVING_HELP));
  144. return 1;
  145. }
  146. ch |= 32; /* lower case */
  147. if (ch < 'a' || ch > 'z')
  148. return -1;
  149. if (letter_offsets[ch-'a'] == -1) {
  150. synth_printf(spk_msg_get(MSG_NO_COMMAND), ch);
  151. synth_printf("\n");
  152. return 1;
  153. }
  154. cur_item = letter_offsets[ch-'a'];
  155. } else if (type == KT_CUR) {
  156. if (ch == 0
  157. && (MSG_FUNCNAMES_START + cur_item + 1) <=
  158. MSG_FUNCNAMES_END)
  159. cur_item++;
  160. else if (ch == 3 && cur_item > 0)
  161. cur_item--;
  162. else
  163. return -1;
  164. } else if (type == KT_SPKUP
  165. && ch == SPEAKUP_HELP
  166. && !spk_special_handler) {
  167. spk_special_handler = spk_handle_help;
  168. synth_printf("%s\n", spk_msg_get(MSG_HELP_INFO));
  169. build_key_data(); /* rebuild each time in case new mapping */
  170. return 1;
  171. } else {
  172. name = NULL;
  173. if ((type != KT_SPKUP) && (key > 0) && (key <= num_key_names)) {
  174. synth_printf("%s\n",
  175. spk_msg_get(MSG_KEYNAMES_START + key-1));
  176. return 1;
  177. }
  178. for (i = 0; funcvals[i] != 0 && !name; i++) {
  179. if (ch == funcvals[i])
  180. name = spk_msg_get(MSG_FUNCNAMES_START + i);
  181. }
  182. if (!name)
  183. return -1;
  184. kp = spk_our_keys[key]+1;
  185. for (i = 0; i < nstates; i++) {
  186. if (ch == kp[i])
  187. break;
  188. }
  189. key += (state_tbl[i] << 8);
  190. say_key(key);
  191. synth_printf(spk_msg_get(MSG_KEYDESC), name);
  192. synth_printf("\n");
  193. return 1;
  194. }
  195. name = spk_msg_get(MSG_FUNCNAMES_START + cur_item);
  196. func = funcvals[cur_item];
  197. synth_printf("%s", name);
  198. if (key_offsets[func] == 0) {
  199. synth_printf(" %s\n", spk_msg_get(MSG_IS_UNASSIGNED));
  200. return 1;
  201. }
  202. p_keys = key_data + key_offsets[func];
  203. for (n = 0; p_keys[n]; n++) {
  204. val = p_keys[n];
  205. if (n > 0)
  206. synth_printf("%s ", spk_msg_get(MSG_DISJUNCTION));
  207. say_key(val);
  208. }
  209. return 1;
  210. }