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