Header modified...
[midnight-commander.git] / pc / key_nt.c
blobb36aea1e815aa0c650c95a170abba6447d5ee1a1
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 only"
24 #else
26 #include <windows.h>
27 #include <stdio.h>
28 #include "../src/mouse.h"
29 #include "../src/global.h"
30 #include "../src/main.h"
31 #include "../src/key.h"
32 #include "../vfs/vfs.h"
33 #include "../src/tty.h"
34 #include "trace_nt.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 } fkt_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 },
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 },
86 { ALT('+'), VK_ADD },
87 { ALT('-'), VK_SUBTRACT },
88 { ALT('\t'), VK_PAUSE }, /* Added to make Complete work press Pause */
90 { ESC_CHAR, VK_ESCAPE },
92 { 0, 0}
93 };
95 /* init_key - Called in main.c to initialize ourselves
96 Get handle to console input
98 void init_key (void)
100 win32APICALL_HANDLE (hConsoleInput, GetStdHandle (STD_INPUT_HANDLE));
103 int ctrl_pressed ()
105 if(dwSaved_ControlState & RIGHT_ALT_PRESSED) return 0;
106 /* The line above fixes the BUG with the AltGr Keys*/
107 return dwSaved_ControlState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED);
110 int shift_pressed ()
112 return dwSaved_ControlState & SHIFT_PRESSED;
115 int alt_pressed ()
117 return dwSaved_ControlState & (/* RIGHT_ALT_PRESSED |*/ LEFT_ALT_PRESSED );
120 static int VKtoCurses (int a_vkc)
122 int i;
124 for (i = 0; fkt_table[i].vkcode != 0; i++)
125 if (a_vkc == fkt_table[i].vkcode) {
126 return fkt_table[i].key_code;
128 return 0;
131 static int translate_key_code(int asc, int scan)
133 int c;
134 switch(scan){
135 case 106: /* KP_MULT*/
136 return ALT('*');
137 case 107: /* KP_PLUS*/
138 return ALT('+');
139 case 109: /* KP_MINUS*/
140 return ALT('-');
142 c = VKtoCurses (scan);
143 if (!asc && !c)
144 return 0;
145 if (asc && c)
146 return c;
147 if (!asc || asc=='\t' )
149 if (shift_pressed() && (c >= KEY_F(1)) && (c <= KEY_F(10)))
150 c += 10;
151 if (alt_pressed() && (c >= KEY_F(1)) && (c <= KEY_F(2)))
152 c += 10;
153 if (alt_pressed() && (c == KEY_F(7)))
154 c = ALT('?');
155 if (asc == '\t'){
156 if(ctrl_pressed())c = ALT('\t');
157 else c=asc;
159 return c;
161 if (ctrl_pressed())
162 return XCTRL(asc);
163 if (alt_pressed())
164 return ALT(asc);
165 if (asc == 13)
166 return 10;
167 return asc;
170 int get_key_code (int no_delay)
172 INPUT_RECORD ir; /* Input record */
173 DWORD dw; /* number of records actually read */
174 int ch, vkcode, j;
176 if (no_delay) {
177 /* Check if any input pending, otherwise return */
178 nodelay (stdscr, TRUE);
179 win32APICALL(PeekConsoleInput(hConsoleInput, &ir, 1, &dw));
180 if (!dw)
181 return 0;
184 do {
185 win32APICALL(ReadConsoleInput(hConsoleInput, &ir, 1, &dw));
186 switch (ir.EventType) {
187 case KEY_EVENT:
188 if (!ir.Event.KeyEvent.bKeyDown) /* Process key just once: when pressed */
189 break;
191 vkcode = ir.Event.KeyEvent.wVirtualKeyCode;
192 //#ifndef __MINGW32__
193 ch = ir.Event.KeyEvent.uChar.AsciiChar;
194 //#else
195 // ch = ir.Event.KeyEvent.AsciiChar;
196 //#endif
197 dwSaved_ControlState = ir.Event.KeyEvent.dwControlKeyState;
198 j = translate_key_code (ch, vkcode);
199 if (j)
200 return j;
201 break;
203 case MOUSE_EVENT:
204 /* Save event as a GPM-like event */
205 evSaved_Event.x = ir.Event.MouseEvent.dwMousePosition.X;
206 evSaved_Event.y = ir.Event.MouseEvent.dwMousePosition.Y+1;
207 evSaved_Event.buttons = ir.Event.MouseEvent.dwButtonState;
208 switch (ir.Event.MouseEvent.dwEventFlags) {
209 case 0:
210 evSaved_Event.type = GPM_DOWN | GPM_SINGLE;
211 break;
212 case MOUSE_MOVED:
213 evSaved_Event.type = GPM_MOVE;
214 break;
215 case DOUBLE_CLICK:
216 evSaved_Event.type = GPM_DOWN | GPM_DOUBLE;
217 break;
219 return 0;
221 } while (!no_delay);
222 return 0;
225 static int getch_with_delay (void)
227 int c;
229 while (1) {
230 /* Try to get a character */
231 c = get_key_code (0);
232 if (c != ERR)
233 break;
235 /* Success -> return the character */
236 return c;
239 /* Returns a character read from stdin with appropriate interpretation */
240 int get_event (Gpm_Event *event, int redo_event, int block)
242 int c;
243 static int flag; /* Return value from select */
244 static int dirty = 3;
246 if ((dirty == 1) || is_idle ()){
247 refresh ();
248 doupdate ();
249 dirty = 1;
250 } else
251 dirty++;
253 vfs_timeout_handler ();
255 c = block ? getch_with_delay () : get_key_code (1);
257 if (!c) {
258 /* Code is 0, so this is a Control key or mouse event */
259 return EV_NONE; /* FIXME: mouse not supported */
262 return c;
265 /* Returns a key press, mouse events are discarded */
266 int mi_getch ()
268 Gpm_Event ev;
269 int key;
271 while ((key = get_event (&ev, 0, 1)) == 0)
273 return key;
277 is_idle - A function to check if we're idle.
278 It checks for any waiting event (that can be a Key, Mouse event,
279 and other internal events like focus or menu)
281 int is_idle (void)
283 DWORD dw;
284 if (GetNumberOfConsoleInputEvents (hConsoleInput, &dw))
285 if (dw > 15)
286 return 0;
287 return 1;
290 /* get_modifier */
291 int get_modifier()
293 int retval = 0;
295 if (dwSaved_ControlState & LEFT_ALT_PRESSED) /* code is not clean, because we return Linux-like bitcodes*/
296 retval |= ALTL_PRESSED;
297 if (dwSaved_ControlState & RIGHT_ALT_PRESSED)
298 retval |= ALTR_PRESSED;
300 if (dwSaved_ControlState & RIGHT_CTRL_PRESSED ||
301 dwSaved_ControlState & LEFT_CTRL_PRESSED)
302 retval |= CONTROL_PRESSED;
304 if (dwSaved_ControlState & SHIFT_PRESSED)
305 retval |= SHIFT_PRESSED;
307 return retval;
310 /* void functions for UNIX compatibility */
311 void define_sequence (int code, char* vkcode, int action) {}
312 void channels_up() {}
313 void channels_down() {}
314 void init_key_input_fd (void) {}
315 void numeric_keypad_mode (void) {}
316 void application_keypad_mode (void) {}
318 /* mouse is not yet supported, sorry */
319 void init_mouse (void) {}
320 void shut_mouse (void) {}
322 #endif /* _OS_NT */