key.nt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Keyboard support routines.
  2. for Windows NT system.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. Bugs:
  15. Have trouble with non-US keyboards, "Alt-gr"+keys (API tells CTRL-ALT is pressed)
  16. */
  17. #include <config.h>
  18. #ifndef _OS_NT
  19. #error This file is for Win32 systems.
  20. #else
  21. #include <windows.h>
  22. #include <stdio.h>
  23. #include "mouse.h"
  24. #include "global.h"
  25. #include "main.h"
  26. #include "key.h"
  27. #include "../vfs/vfs.h"
  28. #include "tty.h"
  29. #include "util.debug.h"
  30. /* Global variables */
  31. int old_esc_mode = 0;
  32. HANDLE hConsoleInput;
  33. DWORD dwSaved_ControlState;
  34. Gpm_Event evSaved_Event;
  35. /* Unused variables */
  36. int double_click_speed; /* they are here to keep linker happy */
  37. int mou_auto_repeat;
  38. int use_8th_bit_as_meta = 0;
  39. /* Static Tables */
  40. struct {
  41. int key_code;
  42. int vkcode;
  43. } key_table [] = {
  44. { KEY_F(1), VK_F1 },
  45. { KEY_F(2), VK_F2 },
  46. { KEY_F(3), VK_F3 },
  47. { KEY_F(4), VK_F4 },
  48. { KEY_F(5), VK_F5 },
  49. { KEY_F(6), VK_F6 },
  50. { KEY_F(7), VK_F7 },
  51. { KEY_F(8), VK_F8 },
  52. { KEY_F(9), VK_F9 },
  53. { KEY_F(10), VK_F10 },
  54. { KEY_F(11), VK_F11 },
  55. { KEY_F(12), VK_F12 },
  56. { KEY_F(13), VK_F13 },
  57. { KEY_F(14), VK_F14 },
  58. { KEY_F(15), VK_F15 },
  59. { KEY_F(16), VK_F16 },
  60. { KEY_F(17), VK_F17 },
  61. { KEY_F(18), VK_F18 },
  62. { KEY_F(19), VK_F19 },
  63. { KEY_F(20), VK_F20 },
  64. { KEY_IC, VK_INSERT },
  65. { KEY_DC, VK_DELETE },
  66. { KEY_BACKSPACE, VK_BACK },
  67. { KEY_PPAGE, VK_PRIOR }, // Movement keys
  68. { KEY_NPAGE, VK_NEXT },
  69. { KEY_LEFT, VK_LEFT },
  70. { KEY_RIGHT, VK_RIGHT },
  71. { KEY_UP, VK_UP },
  72. { KEY_DOWN, VK_DOWN },
  73. { KEY_HOME, VK_HOME },
  74. { KEY_END, VK_END },
  75. { ALT('*'), VK_MULTIPLY }, // Numeric pad
  76. { ALT('+'), VK_ADD },
  77. { ALT('-'), VK_SUBTRACT },
  78. { ESC_CHAR, VK_ESCAPE }, /* ESC */
  79. { 0, 0}
  80. };
  81. /* init_key - Called in main.c to initialize ourselves
  82. Get handle to console input
  83. */
  84. void init_key (void)
  85. {
  86. win32APICALL_HANDLE (hConsoleInput, GetStdHandle (STD_INPUT_HANDLE));
  87. }
  88. int ctrl_pressed ()
  89. {
  90. return dwSaved_ControlState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED);
  91. }
  92. int shift_pressed ()
  93. {
  94. return dwSaved_ControlState & SHIFT_PRESSED;
  95. }
  96. int alt_pressed ()
  97. {
  98. return dwSaved_ControlState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED);
  99. }
  100. static int VKtoCurses (int a_vkc)
  101. {
  102. int i;
  103. for (i = 0; key_table[i].vkcode != 0; i++)
  104. if (a_vkc == key_table[i].vkcode) {
  105. return key_table[i].key_code;
  106. }
  107. return 0;
  108. }
  109. static int translate_key_code(int asc, int scan)
  110. {
  111. int c;
  112. c = VKtoCurses (scan);
  113. if (!asc && !c)
  114. return 0;
  115. if (asc && c)
  116. return c;
  117. if (!asc)
  118. {
  119. if (shift_pressed() && (c >= KEY_F(1)) && (c <= KEY_F(10)))
  120. c += 10;
  121. if (alt_pressed() && (c >= KEY_F(1)) && (c <= KEY_F(2)))
  122. c += 10;
  123. if (alt_pressed() && (c == KEY_F(7)))
  124. c = ALT('?');
  125. if (ctrl_pressed() && c == '\t')
  126. c = ALT('\t');
  127. return c;
  128. }
  129. if (ctrl_pressed())
  130. return XCTRL(asc);
  131. if (alt_pressed())
  132. return ALT(asc);
  133. if (asc == 13)
  134. return 10;
  135. return asc;
  136. }
  137. int get_key_code (int no_delay)
  138. {
  139. INPUT_RECORD ir; /* Input record */
  140. DWORD dw; /* number of records actually read */
  141. int ch, vkcode, j;
  142. if (no_delay) {
  143. /* Check if any input pending, otherwise return */
  144. nodelay (stdscr, TRUE);
  145. win32APICALL(PeekConsoleInput(hConsoleInput, &ir, 1, &dw));
  146. if (!dw)
  147. return 0;
  148. }
  149. do {
  150. win32APICALL(ReadConsoleInput(hConsoleInput, &ir, 1, &dw));
  151. switch (ir.EventType) {
  152. case KEY_EVENT:
  153. if (!ir.Event.KeyEvent.bKeyDown) /* Process key just once: when pressed */
  154. break;
  155. vkcode = ir.Event.KeyEvent.wVirtualKeyCode;
  156. ch = ir.Event.KeyEvent.uChar.AsciiChar;
  157. dwSaved_ControlState = ir.Event.KeyEvent.dwControlKeyState;
  158. j = translate_key_code (ch, vkcode);
  159. if (j)
  160. return j;
  161. break;
  162. case MOUSE_EVENT:
  163. // Save event as a GPM-like event
  164. evSaved_Event.x = ir.Event.MouseEvent.dwMousePosition.X;
  165. evSaved_Event.y = ir.Event.MouseEvent.dwMousePosition.Y+1;
  166. evSaved_Event.buttons = ir.Event.MouseEvent.dwButtonState;
  167. switch (ir.Event.MouseEvent.dwEventFlags) {
  168. case 0:
  169. evSaved_Event.type = GPM_DOWN | GPM_SINGLE;
  170. break;
  171. case MOUSE_MOVED:
  172. evSaved_Event.type = GPM_MOVE;
  173. break;
  174. case DOUBLE_CLICK:
  175. evSaved_Event.type = GPM_DOWN | GPM_DOUBLE;
  176. break;
  177. };
  178. return 0;
  179. }
  180. } while (!no_delay);
  181. return 0;
  182. }
  183. static int getch_with_delay (void)
  184. {
  185. int c;
  186. while (1) {
  187. /* Try to get a character */
  188. c = get_key_code (0);
  189. if (c != ERR)
  190. break;
  191. }
  192. /* Success -> return the character */
  193. return c;
  194. }
  195. /* Returns a character read from stdin with appropriate interpretation */
  196. int get_event (Gpm_Event *event, int redo_event, int block)
  197. {
  198. int c;
  199. static int flag; /* Return value from select */
  200. static int dirty = 3;
  201. if ((dirty == 1) || is_idle ()){
  202. refresh ();
  203. doupdate ();
  204. dirty = 1;
  205. } else
  206. dirty++;
  207. vfs_timeout_handler ();
  208. c = block ? getch_with_delay () : get_key_code (1);
  209. if (!c) {
  210. /* Code is 0, so this is a Control key or mouse event */
  211. return EV_NONE; /* FIXME: mouse not supported */
  212. }
  213. return c;
  214. }
  215. /* Returns a key press, mouse events are discarded */
  216. int mi_getch ()
  217. {
  218. Gpm_Event ev;
  219. int key;
  220. while ((key = get_event (&ev, 0, 1)) == 0)
  221. ;
  222. return key;
  223. }
  224. /*
  225. is_idle - A function to check if we're idle.
  226. It checks for any waiting event (that can be a Key, Mouse event,
  227. and other internal events like focus or menu)
  228. */
  229. int is_idle (void)
  230. {
  231. DWORD dw;
  232. if (GetNumberOfConsoleInputEvents (hConsoleInput, &dw))
  233. if (dw > 15)
  234. return 0;
  235. return 1;
  236. }
  237. /* get_modifier */
  238. int get_modifier()
  239. {
  240. int retval = 0;
  241. if (dwSaved_ControlState & LEFT_ALT_PRESSED) /* code is not clean, because we return Linux-like bitcodes*/
  242. retval |= ALTL_PRESSED;
  243. if (dwSaved_ControlState & RIGHT_ALT_PRESSED)
  244. retval |= ALTR_PRESSED;
  245. if (dwSaved_ControlState & RIGHT_CTRL_PRESSED ||
  246. dwSaved_ControlState & LEFT_CTRL_PRESSED)
  247. retval |= CONTROL_PRESSED;
  248. if (dwSaved_ControlState & SHIFT_PRESSED)
  249. retval |= SHIFT_PRESSED;
  250. return retval;
  251. }
  252. /* void functions for UNIX compatibility */
  253. void define_sequence (int code, char* vkcode, int action)
  254. {
  255. }
  256. void channels_up()
  257. {
  258. }
  259. void channels_down()
  260. {
  261. }
  262. void init_key_input_fd (void)
  263. {
  264. }
  265. /* mouse is not yet supported, sorry */
  266. void init_mouse (void) {}
  267. void shut_mouse (void) {}
  268. #endif /* _OS_NT */