slgetkey.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Copyright (c) 1992, 1999, 2001, 2002 John E. Davis
  2. * This file is part of the S-Lang library.
  3. *
  4. * You may distribute under the terms of either the GNU General Public
  5. * License or the Perl Artistic License.
  6. */
  7. #include "slinclud.h"
  8. #include "slang.h"
  9. #include "_slang.h"
  10. unsigned int SLang_Input_Buffer_Len = 0;
  11. unsigned char SLang_Input_Buffer [SL_MAX_INPUT_BUFFER_LEN];
  12. int SLang_Abort_Char = 7;
  13. int SLang_Ignore_User_Abort = 0;
  14. /* This has the effect of mapping all characters in the range 128-169 to
  15. * ESC [ something
  16. */
  17. unsigned int SLang_getkey (void)
  18. {
  19. unsigned int imax;
  20. unsigned int ch;
  21. if (SLang_Input_Buffer_Len)
  22. {
  23. ch = (unsigned int) *SLang_Input_Buffer;
  24. SLang_Input_Buffer_Len--;
  25. imax = SLang_Input_Buffer_Len;
  26. SLMEMCPY ((char *) SLang_Input_Buffer,
  27. (char *) (SLang_Input_Buffer + 1), imax);
  28. }
  29. else if (SLANG_GETKEY_ERROR == (ch = _SLsys_getkey ())) return ch;
  30. #if _SLANG_MAP_VTXXX_8BIT
  31. # if !defined(IBMPC_SYSTEM)
  32. if (ch & 0x80)
  33. {
  34. unsigned char i;
  35. i = (unsigned char) (ch & 0x7F);
  36. if (i < ' ')
  37. {
  38. i += 64;
  39. SLang_ungetkey (i);
  40. ch = 27;
  41. }
  42. }
  43. # endif
  44. #endif
  45. return(ch);
  46. }
  47. int SLang_ungetkey_string (unsigned char *s, unsigned int n)
  48. {
  49. register unsigned char *bmax, *b, *b1;
  50. if (SLang_Input_Buffer_Len + n + 3 > SL_MAX_INPUT_BUFFER_LEN)
  51. return -1;
  52. b = SLang_Input_Buffer;
  53. bmax = (b - 1) + SLang_Input_Buffer_Len;
  54. b1 = bmax + n;
  55. while (bmax >= b) *b1-- = *bmax--;
  56. bmax = b + n;
  57. while (b < bmax) *b++ = *s++;
  58. SLang_Input_Buffer_Len += n;
  59. return 0;
  60. }
  61. int SLang_buffer_keystring (unsigned char *s, unsigned int n)
  62. {
  63. if (n + SLang_Input_Buffer_Len + 3 > SL_MAX_INPUT_BUFFER_LEN) return -1;
  64. SLMEMCPY ((char *) SLang_Input_Buffer + SLang_Input_Buffer_Len,
  65. (char *) s, n);
  66. SLang_Input_Buffer_Len += n;
  67. return 0;
  68. }
  69. int SLang_ungetkey (unsigned char ch)
  70. {
  71. return SLang_ungetkey_string(&ch, 1);
  72. }
  73. int SLang_input_pending (int tsecs)
  74. {
  75. int n;
  76. unsigned char c;
  77. if (SLang_Input_Buffer_Len) return (int) SLang_Input_Buffer_Len;
  78. n = _SLsys_input_pending (tsecs);
  79. if (n <= 0) return 0;
  80. c = (unsigned char) SLang_getkey ();
  81. SLang_ungetkey_string (&c, 1);
  82. return n;
  83. }
  84. void SLang_flush_input (void)
  85. {
  86. int quit = SLKeyBoard_Quit;
  87. SLang_Input_Buffer_Len = 0;
  88. SLKeyBoard_Quit = 0;
  89. while (_SLsys_input_pending (0) > 0)
  90. {
  91. (void) _SLsys_getkey ();
  92. /* Set this to 0 because _SLsys_getkey may stuff keyboard buffer if
  93. * key sends key sequence (OS/2, DOS, maybe VMS).
  94. */
  95. SLang_Input_Buffer_Len = 0;
  96. }
  97. SLKeyBoard_Quit = quit;
  98. }
  99. #ifdef IBMPC_SYSTEM
  100. static int Map_To_ANSI;
  101. int SLgetkey_map_to_ansi (int enable)
  102. {
  103. Map_To_ANSI = enable;
  104. return 0;
  105. }
  106. static int convert_scancode (unsigned int scan,
  107. unsigned int shift,
  108. int getkey,
  109. unsigned int *ret_key)
  110. {
  111. unsigned char buf[16];
  112. unsigned char *b;
  113. unsigned char end;
  114. int is_arrow;
  115. shift &= (_SLTT_KEY_ALT|_SLTT_KEY_SHIFT|_SLTT_KEY_CTRL);
  116. b = buf;
  117. if (_SLTT_KEY_ALT == shift)
  118. {
  119. shift = 0;
  120. *b++ = 27;
  121. }
  122. *b++ = 27;
  123. *b++ = '[';
  124. is_arrow = 0;
  125. end = '~';
  126. if (shift)
  127. {
  128. if (shift == _SLTT_KEY_CTRL)
  129. end = '^';
  130. else if (shift == _SLTT_KEY_SHIFT)
  131. end = '$';
  132. else shift = 0;
  133. }
  134. /* These mappings correspond to what rxvt produces under Linux */
  135. switch (scan & 0xFF)
  136. {
  137. default:
  138. return -1;
  139. case 0x47: /* home */
  140. *b++ = '1';
  141. break;
  142. case 0x48: /* up */
  143. end = 'A';
  144. is_arrow = 1;
  145. break;
  146. case 0x49: /* PgUp */
  147. *b++ = '5';
  148. break;
  149. case 0x4B: /* Left */
  150. end = 'D';
  151. is_arrow = 1;
  152. break;
  153. case 0x4D: /* Right */
  154. end = 'C';
  155. is_arrow = 1;
  156. break;
  157. case 0x4F: /* End */
  158. *b++ = '4';
  159. break;
  160. case 0x50: /* Down */
  161. end = 'B';
  162. is_arrow = 1;
  163. break;
  164. case 0x51: /* PgDn */
  165. *b++ = '6';
  166. break;
  167. case 0x52: /* Insert */
  168. *b++ = '2';
  169. break;
  170. case 0x53: /* Delete */
  171. *b++ = '3';
  172. break;
  173. case ';': /* F1 */
  174. *b++ = '1';
  175. *b++ = '1';
  176. break;
  177. case '<': /* F2 */
  178. *b++ = '1';
  179. *b++ = '2';
  180. break;
  181. case '=': /* F3 */
  182. *b++ = '1';
  183. *b++ = '3';
  184. break;
  185. case '>': /* F4 */
  186. *b++ = '1';
  187. *b++ = '4';
  188. break;
  189. case '?': /* F5 */
  190. *b++ = '1';
  191. *b++ = '5';
  192. break;
  193. case '@': /* F6 */
  194. *b++ = '1';
  195. *b++ = '7';
  196. break;
  197. case 'A': /* F7 */
  198. *b++ = '1';
  199. *b++ = '8';
  200. break;
  201. case 'B': /* F8 */
  202. *b++ = '1';
  203. *b++ = '9';
  204. break;
  205. case 'C': /* F9 */
  206. *b++ = '2';
  207. *b++ = '0';
  208. break;
  209. case 'D': /* F10 */
  210. *b++ = '2';
  211. *b++ = '1';
  212. break;
  213. case 0x57: /* F11 */
  214. *b++ = '2';
  215. *b++ = '3';
  216. break;
  217. case 0x58: /* F12 */
  218. *b++ = '2';
  219. *b++ = '4';
  220. break;
  221. }
  222. if (is_arrow && shift)
  223. {
  224. if (shift == _SLTT_KEY_CTRL)
  225. end &= 0x1F;
  226. else
  227. end |= 0x20;
  228. }
  229. *b++ = end;
  230. if (getkey)
  231. {
  232. (void) SLang_buffer_keystring (buf + 1, (unsigned int) (b - (buf + 1)));
  233. *ret_key = buf[0];
  234. return 0;
  235. }
  236. (void) SLang_buffer_keystring (buf, (unsigned int) (b - buf));
  237. return 0;
  238. }
  239. unsigned int _SLpc_convert_scancode (unsigned int scan,
  240. unsigned int shift,
  241. int getkey)
  242. {
  243. unsigned char buf[16];
  244. if (Map_To_ANSI)
  245. {
  246. if (0 == convert_scancode (scan, shift, getkey, &scan))
  247. return scan;
  248. }
  249. if (getkey)
  250. {
  251. buf[0] = scan & 0xFF;
  252. SLang_buffer_keystring (buf, 1);
  253. return (scan >> 8) & 0xFF;
  254. }
  255. buf[0] = (scan >> 8) & 0xFF;
  256. buf[1] = scan & 0xFF;
  257. (void) SLang_buffer_keystring (buf, 2);
  258. return 0;
  259. }
  260. #endif