keyhelp.c 5.9 KB

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