*** empty log message ***
[midnight-commander.git] / pc / key_nt.c
blob50f4efea62bf6e95512e2730fef46c60b38087a7
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 NATIVE_WIN32
23 #error "This file is for Win32 systems only"
24 #else
26 #include <stdio.h>
27 #include "../src/global.h"
28 #include "../src/mouse.h"
29 #include "../src/main.h"
30 #include "../src/key.h"
31 #include "../vfs/vfs.h"
32 #include "../src/tty.h"
33 #include "trace_nt.h"
35 /* Global variables */
36 int old_esc_mode = 0;
37 HANDLE hConsoleInput;
38 DWORD dwSaved_ControlState;
39 Gpm_Event evSaved_Event;
41 /* Unused variables */
42 int double_click_speed; /* they are here to keep linker happy */
43 int mou_auto_repeat;
44 int use_8th_bit_as_meta = 0;
46 /* Static Tables */
47 struct {
48 int key_code;
49 int vkcode;
50 } fkt_table [] = {
51 { KEY_F(1), VK_F1 },
52 { KEY_F(2), VK_F2 },
53 { KEY_F(3), VK_F3 },
54 { KEY_F(4), VK_F4 },
55 { KEY_F(5), VK_F5 },
56 { KEY_F(6), VK_F6 },
57 { KEY_F(7), VK_F7 },
58 { KEY_F(8), VK_F8 },
59 { KEY_F(9), VK_F9 },
60 { KEY_F(10), VK_F10 },
61 { KEY_F(11), VK_F11 },
62 { KEY_F(12), VK_F12 },
63 { KEY_F(13), VK_F13 },
64 { KEY_F(14), VK_F14 },
65 { KEY_F(15), VK_F15 },
66 { KEY_F(16), VK_F16 },
67 { KEY_F(17), VK_F17 },
68 { KEY_F(18), VK_F18 },
69 { KEY_F(19), VK_F19 },
70 { KEY_F(20), VK_F20 },
71 { KEY_IC, VK_INSERT },
72 { KEY_DC, VK_DELETE },
73 { KEY_BACKSPACE, VK_BACK },
75 { KEY_PPAGE, VK_PRIOR },
76 { KEY_NPAGE, VK_NEXT },
77 { KEY_LEFT, VK_LEFT },
78 { KEY_RIGHT, VK_RIGHT },
79 { KEY_UP, VK_UP },
80 { KEY_DOWN, VK_DOWN },
81 { KEY_HOME, VK_HOME },
82 { KEY_END, VK_END },
84 { ALT('*'), VK_MULTIPLY },
85 { ALT('+'), VK_ADD },
86 { ALT('-'), VK_SUBTRACT },
87 { ALT('\t'), VK_PAUSE }, /* Added to make Complete work press Pause */
89 { ESC_CHAR, VK_ESCAPE },
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 if(dwSaved_ControlState & RIGHT_ALT_PRESSED) return 0;
105 /* The line above fixes the BUG with the AltGr Keys*/
106 return dwSaved_ControlState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED);
109 int shift_pressed ()
111 return dwSaved_ControlState & SHIFT_PRESSED;
114 int alt_pressed ()
116 return dwSaved_ControlState & (/* RIGHT_ALT_PRESSED |*/ LEFT_ALT_PRESSED );
119 static int VKtoCurses (int a_vkc)
121 int i;
123 for (i = 0; fkt_table[i].vkcode != 0; i++)
124 if (a_vkc == fkt_table[i].vkcode) {
125 return fkt_table[i].key_code;
127 return 0;
130 static int translate_key_code(int asc, int scan)
132 int c;
133 switch(scan){
134 case 106: /* KP_MULT*/
135 return ALT('*');
136 case 107: /* KP_PLUS*/
137 return ALT('+');
138 case 109: /* KP_MINUS*/
139 return ALT('-');
141 c = VKtoCurses (scan);
142 if (!asc && !c)
143 return 0;
144 if (asc && c)
145 return c;
146 if (!asc || asc=='\t' )
148 if (shift_pressed() && (c >= KEY_F(1)) && (c <= KEY_F(10)))
149 c += 10;
150 if (alt_pressed() && (c >= KEY_F(1)) && (c <= KEY_F(2)))
151 c += 10;
152 if (alt_pressed() && (c == KEY_F(7)))
153 c = ALT('?');
154 if (asc == '\t'){
155 if(ctrl_pressed())c = ALT('\t');
156 else c=asc;
158 return c;
160 if (ctrl_pressed())
161 return XCTRL(asc);
162 if (alt_pressed())
163 return ALT(asc);
164 if (asc == 13)
165 return 10;
166 return asc;
169 int get_key_code (int no_delay)
171 INPUT_RECORD ir; /* Input record */
172 DWORD dw; /* number of records actually read */
173 int ch, vkcode, j;
175 if (no_delay) {
176 /* Check if any input pending, otherwise return */
177 nodelay (stdscr, TRUE);
178 win32APICALL(PeekConsoleInput(hConsoleInput, &ir, 1, &dw));
179 if (!dw)
180 return 0;
183 do {
184 win32APICALL(ReadConsoleInput(hConsoleInput, &ir, 1, &dw));
185 switch (ir.EventType) {
186 case KEY_EVENT:
187 if (!ir.Event.KeyEvent.bKeyDown) /* Process key just once: when pressed */
188 break;
190 vkcode = ir.Event.KeyEvent.wVirtualKeyCode;
191 //#ifndef __MINGW32__
192 ch = ir.Event.KeyEvent.uChar.AsciiChar;
193 //#else
194 // ch = ir.Event.KeyEvent.AsciiChar;
195 //#endif
196 dwSaved_ControlState = ir.Event.KeyEvent.dwControlKeyState;
197 j = translate_key_code (ch, vkcode);
198 if (j)
199 return j;
200 break;
202 case MOUSE_EVENT:
203 /* Save event as a GPM-like event */
204 evSaved_Event.x = ir.Event.MouseEvent.dwMousePosition.X;
205 evSaved_Event.y = ir.Event.MouseEvent.dwMousePosition.Y+1;
206 evSaved_Event.buttons = ir.Event.MouseEvent.dwButtonState;
207 switch (ir.Event.MouseEvent.dwEventFlags) {
208 case 0:
209 evSaved_Event.type = GPM_DOWN | GPM_SINGLE;
210 break;
211 case MOUSE_MOVED:
212 evSaved_Event.type = GPM_MOVE;
213 break;
214 case DOUBLE_CLICK:
215 evSaved_Event.type = GPM_DOWN | GPM_DOUBLE;
216 break;
218 return 0;
220 } while (!no_delay);
221 return 0;
224 static int getch_with_delay (void)
226 int c;
228 while (1) {
229 /* Try to get a character */
230 c = get_key_code (0);
231 if (c != ERR)
232 break;
234 /* Success -> return the character */
235 return c;
238 /* Returns a character read from stdin with appropriate interpretation */
239 int get_event (Gpm_Event *event, int redo_event, int block)
241 int c;
242 static int flag; /* Return value from select */
243 static int dirty = 3;
245 if ((dirty == 1) || is_idle ()){
246 refresh ();
247 doupdate ();
248 dirty = 1;
249 } else
250 dirty++;
252 vfs_timeout_handler ();
254 c = block ? getch_with_delay () : get_key_code (1);
256 if (!c) {
257 /* Code is 0, so this is a Control key or mouse event */
258 return EV_NONE; /* FIXME: mouse not supported */
261 return c;
264 /* Returns a key press, mouse events are discarded */
265 int mi_getch ()
267 Gpm_Event ev;
268 int key;
270 while ((key = get_event (&ev, 0, 1)) == 0)
272 return key;
276 is_idle - A function to check if we're idle.
277 It checks for any waiting event (that can be a Key, Mouse event,
278 and other internal events like focus or menu)
280 int is_idle (void)
282 DWORD dw;
283 if (GetNumberOfConsoleInputEvents (hConsoleInput, &dw))
284 if (dw > 15)
285 return 0;
286 return 1;
289 /* get_modifier */
290 int get_modifier()
292 int retval = 0;
294 if (dwSaved_ControlState & LEFT_ALT_PRESSED) /* code is not clean, because we return Linux-like bitcodes*/
295 retval |= ALTL_PRESSED;
296 if (dwSaved_ControlState & RIGHT_ALT_PRESSED)
297 retval |= ALTR_PRESSED;
299 if (dwSaved_ControlState & RIGHT_CTRL_PRESSED ||
300 dwSaved_ControlState & LEFT_CTRL_PRESSED)
301 retval |= CONTROL_PRESSED;
303 if (dwSaved_ControlState & SHIFT_PRESSED)
304 retval |= SHIFT_PRESSED;
306 return retval;
309 /* void functions for UNIX compatibility */
310 int define_sequence (int code, char* vkcode, int action) { return 1; }
311 void channels_up() {}
312 void channels_down() {}
313 void init_key_input_fd (void) {}
314 void numeric_keypad_mode (void) {}
315 void application_keypad_mode (void) {}
317 /* mouse is not yet supported, sorry */
318 void init_mouse (void) {}
319 void shut_mouse (void) {}
321 #endif /* NATIVE_WIN32 */