blob: 537ff43574990741356543a6f02335886c611287 [file] [log] [blame]
Pierre Ossman407a5c32011-05-26 14:48:29 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
3 *
4 * This 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.
8 *
9 * This software 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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#include <windows.h>
21
Pierre Ossman2e9684f2014-07-21 16:46:22 +020022#define XK_MISCELLANY
23#define XK_XKB_KEYS
24#include <rfb/keysymdef.h>
25#include <rfb/XF86keysym.h>
26
27#include "keysym2ucs.h"
28
29#define NoSymbol 0
30
31// Missing in at least some versions of MinGW
32#ifndef MAPVK_VK_TO_CHAR
33#define MAPVK_VK_TO_CHAR 2
34#endif
35
Pierre Ossman69428fe2018-03-08 17:24:54 +010036int has_altgr;
37HKL current_layout = 0;
38
Pierre Ossman407a5c32011-05-26 14:48:29 +000039static HANDLE thread;
40static DWORD thread_id;
41
42static HHOOK hook = 0;
43static HWND target_wnd = 0;
44
45static int is_system_hotkey(int vkCode) {
46 switch (vkCode) {
47 case VK_LWIN:
48 case VK_RWIN:
49 case VK_SNAPSHOT:
50 return 1;
51 case VK_TAB:
52 if (GetAsyncKeyState(VK_MENU) & 0x8000)
53 return 1;
54 case VK_ESCAPE:
55 if (GetAsyncKeyState(VK_MENU) & 0x8000)
56 return 1;
57 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
58 return 1;
59 }
60 return 0;
61}
62
63static LRESULT CALLBACK keyboard_hook(int nCode, WPARAM wParam, LPARAM lParam)
64{
65 if (nCode >= 0) {
66 KBDLLHOOKSTRUCT* msgInfo = (KBDLLHOOKSTRUCT*)lParam;
67
68 // Grabbing everything seems to mess up some keyboard state that
69 // FLTK relies on, so just grab the keys that we normally cannot.
70 if (is_system_hotkey(msgInfo->vkCode)) {
71 PostMessage(target_wnd, wParam, msgInfo->vkCode,
72 (msgInfo->scanCode & 0xff) << 16 |
73 (msgInfo->flags & 0xff) << 24);
74 return 1;
75 }
76 }
77
78 return CallNextHookEx(hook, nCode, wParam, lParam);
79}
80
81static DWORD WINAPI keyboard_thread(LPVOID data)
82{
83 MSG msg;
84
85 target_wnd = (HWND)data;
86
87 // Make sure a message queue is created
88 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE | PM_NOYIELD);
89
90 hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_hook, GetModuleHandle(0), 0);
91 // If something goes wrong then there is not much we can do.
92 // Just sit around and wait for WM_QUIT...
93
94 while (GetMessage(&msg, NULL, 0, 0));
95
96 if (hook)
97 UnhookWindowsHookEx(hook);
98
99 target_wnd = 0;
100
101 return 0;
102}
103
104int win32_enable_lowlevel_keyboard(HWND hwnd)
105{
106 // Only one target at a time for now
107 if (thread != NULL) {
108 if (hwnd == target_wnd)
109 return 0;
110
111 return 1;
112 }
113
114 // We create a separate thread as it is crucial that hooks are processed
115 // in a timely manner.
116 thread = CreateThread(NULL, 0, keyboard_thread, hwnd, 0, &thread_id);
117 if (thread == NULL)
118 return 1;
119
120 return 0;
121}
122
123void win32_disable_lowlevel_keyboard(HWND hwnd)
124{
125 if (hwnd != target_wnd)
126 return;
127
128 PostThreadMessage(thread_id, WM_QUIT, 0, 0);
129
130 CloseHandle(thread);
131 thread = NULL;
132}
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200133
134static const int vkey_map[][3] = {
Pierre Ossmancc945562017-11-13 09:08:50 +0100135 { VK_CANCEL, NoSymbol, XK_Break },
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200136 { VK_BACK, XK_BackSpace, NoSymbol },
137 { VK_TAB, XK_Tab, NoSymbol },
138 { VK_CLEAR, XK_Clear, NoSymbol },
139 { VK_RETURN, XK_Return, XK_KP_Enter },
140 { VK_SHIFT, XK_Shift_L, NoSymbol },
141 { VK_CONTROL, XK_Control_L, XK_Control_R },
142 { VK_MENU, XK_Alt_L, XK_Alt_R },
143 { VK_PAUSE, XK_Pause, NoSymbol },
144 { VK_CAPITAL, XK_Caps_Lock, NoSymbol },
145 /* FIXME: IME keys */
146 { VK_ESCAPE, XK_Escape, NoSymbol },
147 { VK_PRIOR, XK_KP_Prior, XK_Prior },
148 { VK_NEXT, XK_KP_Next, XK_Next },
149 { VK_END, XK_KP_End, XK_End },
150 { VK_HOME, XK_KP_Home, XK_Home },
151 { VK_LEFT, XK_KP_Left, XK_Left },
152 { VK_UP, XK_KP_Up, XK_Up },
153 { VK_RIGHT, XK_KP_Right, XK_Right },
154 { VK_DOWN, XK_KP_Down, XK_Down },
Pierre Ossmana7d3dc72014-09-30 17:03:28 +0200155 { VK_SNAPSHOT, XK_Sys_Req, XK_Print },
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200156 { VK_INSERT, XK_KP_Insert, XK_Insert },
157 { VK_DELETE, XK_KP_Delete, XK_Delete },
158 { VK_LWIN, NoSymbol, XK_Super_L },
159 { VK_RWIN, NoSymbol, XK_Super_R },
160 { VK_APPS, NoSymbol, XK_Menu },
161 { VK_SLEEP, NoSymbol, XF86XK_Sleep },
162 { VK_NUMPAD0, XK_KP_0, NoSymbol },
163 { VK_NUMPAD1, XK_KP_1, NoSymbol },
164 { VK_NUMPAD2, XK_KP_2, NoSymbol },
165 { VK_NUMPAD3, XK_KP_3, NoSymbol },
166 { VK_NUMPAD4, XK_KP_4, NoSymbol },
167 { VK_NUMPAD5, XK_KP_5, NoSymbol },
168 { VK_NUMPAD6, XK_KP_6, NoSymbol },
169 { VK_NUMPAD7, XK_KP_7, NoSymbol },
170 { VK_NUMPAD8, XK_KP_8, NoSymbol },
171 { VK_NUMPAD9, XK_KP_9, NoSymbol },
172 { VK_MULTIPLY, XK_KP_Multiply, NoSymbol },
173 { VK_ADD, XK_KP_Add, NoSymbol },
174 { VK_SUBTRACT, XK_KP_Subtract, NoSymbol },
175 { VK_DIVIDE, NoSymbol, XK_KP_Divide },
176 /* VK_SEPARATOR and VK_DECIMAL left out on purpose. See further down. */
177 { VK_F1, XK_F1, NoSymbol },
178 { VK_F2, XK_F2, NoSymbol },
179 { VK_F3, XK_F3, NoSymbol },
180 { VK_F4, XK_F4, NoSymbol },
181 { VK_F5, XK_F5, NoSymbol },
182 { VK_F6, XK_F6, NoSymbol },
183 { VK_F7, XK_F7, NoSymbol },
184 { VK_F8, XK_F8, NoSymbol },
185 { VK_F9, XK_F9, NoSymbol },
186 { VK_F10, XK_F10, NoSymbol },
187 { VK_F11, XK_F11, NoSymbol },
188 { VK_F12, XK_F12, NoSymbol },
189 { VK_F13, XK_F13, NoSymbol },
190 { VK_F14, XK_F14, NoSymbol },
191 { VK_F15, XK_F15, NoSymbol },
192 { VK_F16, XK_F16, NoSymbol },
193 { VK_F17, XK_F17, NoSymbol },
194 { VK_F18, XK_F18, NoSymbol },
195 { VK_F19, XK_F19, NoSymbol },
196 { VK_F20, XK_F20, NoSymbol },
197 { VK_F21, XK_F21, NoSymbol },
198 { VK_F22, XK_F22, NoSymbol },
199 { VK_F23, XK_F23, NoSymbol },
200 { VK_F24, XK_F24, NoSymbol },
201 { VK_NUMLOCK, NoSymbol, XK_Num_Lock },
202 { VK_SCROLL, XK_Scroll_Lock, NoSymbol },
203 { VK_BROWSER_BACK, NoSymbol, XF86XK_Back },
204 { VK_BROWSER_FORWARD, NoSymbol, XF86XK_Forward },
205 { VK_BROWSER_REFRESH, NoSymbol, XF86XK_Refresh },
206 { VK_BROWSER_STOP, NoSymbol, XF86XK_Stop },
207 { VK_BROWSER_SEARCH, NoSymbol, XF86XK_Search },
208 { VK_BROWSER_FAVORITES, NoSymbol, XF86XK_Favorites },
209 { VK_BROWSER_HOME, NoSymbol, XF86XK_HomePage },
210 { VK_VOLUME_MUTE, NoSymbol, XF86XK_AudioMute },
211 { VK_VOLUME_DOWN, NoSymbol, XF86XK_AudioLowerVolume },
212 { VK_VOLUME_UP, NoSymbol, XF86XK_AudioRaiseVolume },
213 { VK_MEDIA_NEXT_TRACK, NoSymbol, XF86XK_AudioNext },
214 { VK_MEDIA_PREV_TRACK, NoSymbol, XF86XK_AudioPrev },
215 { VK_MEDIA_STOP, NoSymbol, XF86XK_AudioStop },
216 { VK_MEDIA_PLAY_PAUSE, NoSymbol, XF86XK_AudioPlay },
217 { VK_LAUNCH_MAIL, NoSymbol, XF86XK_Mail },
218 { VK_LAUNCH_APP2, NoSymbol, XF86XK_Calculator },
219};
220
221int win32_vkey_to_keysym(UINT vkey, int extended)
222{
223 int i;
224
225 BYTE state[256];
226 int ret;
227 WCHAR wstr[10];
228
229 // Start with keys that either don't generate a symbol, or
230 // generate the same symbol as some other key.
231 for (i = 0;i < sizeof(vkey_map)/sizeof(vkey_map[0]);i++) {
232 if (vkey == vkey_map[i][0]) {
233 if (extended)
234 return vkey_map[i][2];
235 else
236 return vkey_map[i][1];
237 }
238 }
239
240 // Windows is not consistent in which virtual key it uses for
241 // the numpad decimal key, and this is not likely to be fixed:
242 // http://blogs.msdn.com/michkap/archive/2006/09/13/752377.aspx
243 //
244 // To get X11 behaviour, we instead look at the text generated
245 // by they key.
246 if ((vkey == VK_DECIMAL) || (vkey == VK_SEPARATOR)) {
247 UINT ch;
248
249 ch = MapVirtualKey(vkey, MAPVK_VK_TO_CHAR);
250 switch (ch) {
251 case ',':
252 return XK_KP_Separator;
253 case '.':
254 return XK_KP_Decimal;
255 default:
256 return NoSymbol;
257 }
258 }
259
260 // MapVirtualKey() doesn't look at modifiers, so it is
261 // insufficient for mapping most keys to a symbol. ToUnicode()
262 // does what we want though. Unfortunately it keeps state, so
263 // we have to be careful around dead characters.
264
265 GetKeyboardState(state);
266
267 // Pressing Ctrl wreaks havoc with the symbol lookup, so turn
268 // that off. But AltGr shows up as Ctrl+Alt in Windows, so keep
269 // Ctrl if Alt is active.
270 if (!(state[VK_LCONTROL] & 0x80) || !(state[VK_RMENU] & 0x80))
271 state[VK_CONTROL] = state[VK_LCONTROL] = state[VK_RCONTROL] = 0;
272
273 // FIXME: Multi character results, like U+0644 U+0627
274 // on Arabic layout
275 ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0);
276
277 if (ret == 0) {
278 // Most Ctrl+Alt combinations will fail to produce a symbol, so
279 // try it again with Ctrl unconditionally disabled.
280 state[VK_CONTROL] = state[VK_LCONTROL] = state[VK_RCONTROL] = 0;
281 ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0);
282 }
283
284 if (ret == 1)
285 return ucs2keysym(wstr[0]);
286
287 if (ret == -1) {
288 WCHAR dead_char;
289
290 dead_char = wstr[0];
291
292 // Need to clear out the state that the dead key has caused.
293 // This is the recommended method by Microsoft's engineers:
294 // http://blogs.msdn.com/b/michkap/archive/2007/10/27/5717859.aspx
295 do {
296 ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0);
297 } while (ret < 0);
298
299 // Dead keys are represented by their spacing equivalent
300 // (or something similar depending on the layout)
301 return ucs2keysym(ucs2combining(dead_char));
302 }
303
304 return NoSymbol;
305}
Pierre Ossman69428fe2018-03-08 17:24:54 +0100306
307int win32_has_altgr(void)
308{
309 BYTE orig_state[256];
310 BYTE altgr_state[256];
311
312 if (current_layout == GetKeyboardLayout(0))
313 return has_altgr;
314
315 // Save current keyboard state so we can get things sane again after
316 // we're done
317 if (!GetKeyboardState(orig_state))
318 return 0;
319
320 // We press Ctrl+Alt (Windows fake AltGr) and then test every key
321 // to see if it produces a printable character. If so then we assume
322 // AltGr is used in the current layout.
323
324 has_altgr = 0;
325
326 memset(altgr_state, 0, sizeof(altgr_state));
327 altgr_state[VK_CONTROL] = 0x80;
328 altgr_state[VK_MENU] = 0x80;
329
330 for (UINT vkey = 0;vkey <= 0xff;vkey++) {
331 int ret;
332 WCHAR wstr[10];
333
334 // Need to skip this one as it is a bit magical and will trigger
335 // a false positive
336 if (vkey == VK_PACKET)
337 continue;
338
339 ret = ToUnicode(vkey, 0, altgr_state, wstr,
340 sizeof(wstr)/sizeof(wstr[0]), 0);
341 if (ret == 1) {
342 has_altgr = 1;
343 break;
344 }
345
346 if (ret == -1) {
347 // Dead key, need to clear out state before we proceed
348 do {
349 ret = ToUnicode(vkey, 0, altgr_state, wstr,
350 sizeof(wstr)/sizeof(wstr[0]), 0);
351 } while (ret < 0);
352 }
353 }
354
355 SetKeyboardState(orig_state);
356
357 current_layout = GetKeyboardLayout(0);
358
359 return has_altgr;
360}