blob: 30c87f18cf0f661a3cc94dde38e5d7129b1c10a8 [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 Ossman407a5c32011-05-26 14:48:29 +000036static HANDLE thread;
37static DWORD thread_id;
38
39static HHOOK hook = 0;
40static HWND target_wnd = 0;
41
42static int is_system_hotkey(int vkCode) {
43 switch (vkCode) {
44 case VK_LWIN:
45 case VK_RWIN:
46 case VK_SNAPSHOT:
47 return 1;
48 case VK_TAB:
49 if (GetAsyncKeyState(VK_MENU) & 0x8000)
50 return 1;
51 case VK_ESCAPE:
52 if (GetAsyncKeyState(VK_MENU) & 0x8000)
53 return 1;
54 if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
55 return 1;
56 }
57 return 0;
58}
59
60static LRESULT CALLBACK keyboard_hook(int nCode, WPARAM wParam, LPARAM lParam)
61{
62 if (nCode >= 0) {
63 KBDLLHOOKSTRUCT* msgInfo = (KBDLLHOOKSTRUCT*)lParam;
64
65 // Grabbing everything seems to mess up some keyboard state that
66 // FLTK relies on, so just grab the keys that we normally cannot.
67 if (is_system_hotkey(msgInfo->vkCode)) {
68 PostMessage(target_wnd, wParam, msgInfo->vkCode,
69 (msgInfo->scanCode & 0xff) << 16 |
70 (msgInfo->flags & 0xff) << 24);
71 return 1;
72 }
73 }
74
75 return CallNextHookEx(hook, nCode, wParam, lParam);
76}
77
78static DWORD WINAPI keyboard_thread(LPVOID data)
79{
80 MSG msg;
81
82 target_wnd = (HWND)data;
83
84 // Make sure a message queue is created
85 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE | PM_NOYIELD);
86
87 hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_hook, GetModuleHandle(0), 0);
88 // If something goes wrong then there is not much we can do.
89 // Just sit around and wait for WM_QUIT...
90
91 while (GetMessage(&msg, NULL, 0, 0));
92
93 if (hook)
94 UnhookWindowsHookEx(hook);
95
96 target_wnd = 0;
97
98 return 0;
99}
100
101int win32_enable_lowlevel_keyboard(HWND hwnd)
102{
103 // Only one target at a time for now
104 if (thread != NULL) {
105 if (hwnd == target_wnd)
106 return 0;
107
108 return 1;
109 }
110
111 // We create a separate thread as it is crucial that hooks are processed
112 // in a timely manner.
113 thread = CreateThread(NULL, 0, keyboard_thread, hwnd, 0, &thread_id);
114 if (thread == NULL)
115 return 1;
116
117 return 0;
118}
119
120void win32_disable_lowlevel_keyboard(HWND hwnd)
121{
122 if (hwnd != target_wnd)
123 return;
124
125 PostThreadMessage(thread_id, WM_QUIT, 0, 0);
126
127 CloseHandle(thread);
128 thread = NULL;
129}
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200130
131static const int vkey_map[][3] = {
Pierre Ossmancc945562017-11-13 09:08:50 +0100132 { VK_CANCEL, NoSymbol, XK_Break },
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200133 { VK_BACK, XK_BackSpace, NoSymbol },
134 { VK_TAB, XK_Tab, NoSymbol },
135 { VK_CLEAR, XK_Clear, NoSymbol },
136 { VK_RETURN, XK_Return, XK_KP_Enter },
137 { VK_SHIFT, XK_Shift_L, NoSymbol },
138 { VK_CONTROL, XK_Control_L, XK_Control_R },
139 { VK_MENU, XK_Alt_L, XK_Alt_R },
140 { VK_PAUSE, XK_Pause, NoSymbol },
141 { VK_CAPITAL, XK_Caps_Lock, NoSymbol },
142 /* FIXME: IME keys */
143 { VK_ESCAPE, XK_Escape, NoSymbol },
144 { VK_PRIOR, XK_KP_Prior, XK_Prior },
145 { VK_NEXT, XK_KP_Next, XK_Next },
146 { VK_END, XK_KP_End, XK_End },
147 { VK_HOME, XK_KP_Home, XK_Home },
148 { VK_LEFT, XK_KP_Left, XK_Left },
149 { VK_UP, XK_KP_Up, XK_Up },
150 { VK_RIGHT, XK_KP_Right, XK_Right },
151 { VK_DOWN, XK_KP_Down, XK_Down },
Pierre Ossmana7d3dc72014-09-30 17:03:28 +0200152 { VK_SNAPSHOT, XK_Sys_Req, XK_Print },
Pierre Ossman2e9684f2014-07-21 16:46:22 +0200153 { VK_INSERT, XK_KP_Insert, XK_Insert },
154 { VK_DELETE, XK_KP_Delete, XK_Delete },
155 { VK_LWIN, NoSymbol, XK_Super_L },
156 { VK_RWIN, NoSymbol, XK_Super_R },
157 { VK_APPS, NoSymbol, XK_Menu },
158 { VK_SLEEP, NoSymbol, XF86XK_Sleep },
159 { VK_NUMPAD0, XK_KP_0, NoSymbol },
160 { VK_NUMPAD1, XK_KP_1, NoSymbol },
161 { VK_NUMPAD2, XK_KP_2, NoSymbol },
162 { VK_NUMPAD3, XK_KP_3, NoSymbol },
163 { VK_NUMPAD4, XK_KP_4, NoSymbol },
164 { VK_NUMPAD5, XK_KP_5, NoSymbol },
165 { VK_NUMPAD6, XK_KP_6, NoSymbol },
166 { VK_NUMPAD7, XK_KP_7, NoSymbol },
167 { VK_NUMPAD8, XK_KP_8, NoSymbol },
168 { VK_NUMPAD9, XK_KP_9, NoSymbol },
169 { VK_MULTIPLY, XK_KP_Multiply, NoSymbol },
170 { VK_ADD, XK_KP_Add, NoSymbol },
171 { VK_SUBTRACT, XK_KP_Subtract, NoSymbol },
172 { VK_DIVIDE, NoSymbol, XK_KP_Divide },
173 /* VK_SEPARATOR and VK_DECIMAL left out on purpose. See further down. */
174 { VK_F1, XK_F1, NoSymbol },
175 { VK_F2, XK_F2, NoSymbol },
176 { VK_F3, XK_F3, NoSymbol },
177 { VK_F4, XK_F4, NoSymbol },
178 { VK_F5, XK_F5, NoSymbol },
179 { VK_F6, XK_F6, NoSymbol },
180 { VK_F7, XK_F7, NoSymbol },
181 { VK_F8, XK_F8, NoSymbol },
182 { VK_F9, XK_F9, NoSymbol },
183 { VK_F10, XK_F10, NoSymbol },
184 { VK_F11, XK_F11, NoSymbol },
185 { VK_F12, XK_F12, NoSymbol },
186 { VK_F13, XK_F13, NoSymbol },
187 { VK_F14, XK_F14, NoSymbol },
188 { VK_F15, XK_F15, NoSymbol },
189 { VK_F16, XK_F16, NoSymbol },
190 { VK_F17, XK_F17, NoSymbol },
191 { VK_F18, XK_F18, NoSymbol },
192 { VK_F19, XK_F19, NoSymbol },
193 { VK_F20, XK_F20, NoSymbol },
194 { VK_F21, XK_F21, NoSymbol },
195 { VK_F22, XK_F22, NoSymbol },
196 { VK_F23, XK_F23, NoSymbol },
197 { VK_F24, XK_F24, NoSymbol },
198 { VK_NUMLOCK, NoSymbol, XK_Num_Lock },
199 { VK_SCROLL, XK_Scroll_Lock, NoSymbol },
200 { VK_BROWSER_BACK, NoSymbol, XF86XK_Back },
201 { VK_BROWSER_FORWARD, NoSymbol, XF86XK_Forward },
202 { VK_BROWSER_REFRESH, NoSymbol, XF86XK_Refresh },
203 { VK_BROWSER_STOP, NoSymbol, XF86XK_Stop },
204 { VK_BROWSER_SEARCH, NoSymbol, XF86XK_Search },
205 { VK_BROWSER_FAVORITES, NoSymbol, XF86XK_Favorites },
206 { VK_BROWSER_HOME, NoSymbol, XF86XK_HomePage },
207 { VK_VOLUME_MUTE, NoSymbol, XF86XK_AudioMute },
208 { VK_VOLUME_DOWN, NoSymbol, XF86XK_AudioLowerVolume },
209 { VK_VOLUME_UP, NoSymbol, XF86XK_AudioRaiseVolume },
210 { VK_MEDIA_NEXT_TRACK, NoSymbol, XF86XK_AudioNext },
211 { VK_MEDIA_PREV_TRACK, NoSymbol, XF86XK_AudioPrev },
212 { VK_MEDIA_STOP, NoSymbol, XF86XK_AudioStop },
213 { VK_MEDIA_PLAY_PAUSE, NoSymbol, XF86XK_AudioPlay },
214 { VK_LAUNCH_MAIL, NoSymbol, XF86XK_Mail },
215 { VK_LAUNCH_APP2, NoSymbol, XF86XK_Calculator },
216};
217
218int win32_vkey_to_keysym(UINT vkey, int extended)
219{
220 int i;
221
222 BYTE state[256];
223 int ret;
224 WCHAR wstr[10];
225
226 // Start with keys that either don't generate a symbol, or
227 // generate the same symbol as some other key.
228 for (i = 0;i < sizeof(vkey_map)/sizeof(vkey_map[0]);i++) {
229 if (vkey == vkey_map[i][0]) {
230 if (extended)
231 return vkey_map[i][2];
232 else
233 return vkey_map[i][1];
234 }
235 }
236
237 // Windows is not consistent in which virtual key it uses for
238 // the numpad decimal key, and this is not likely to be fixed:
239 // http://blogs.msdn.com/michkap/archive/2006/09/13/752377.aspx
240 //
241 // To get X11 behaviour, we instead look at the text generated
242 // by they key.
243 if ((vkey == VK_DECIMAL) || (vkey == VK_SEPARATOR)) {
244 UINT ch;
245
246 ch = MapVirtualKey(vkey, MAPVK_VK_TO_CHAR);
247 switch (ch) {
248 case ',':
249 return XK_KP_Separator;
250 case '.':
251 return XK_KP_Decimal;
252 default:
253 return NoSymbol;
254 }
255 }
256
257 // MapVirtualKey() doesn't look at modifiers, so it is
258 // insufficient for mapping most keys to a symbol. ToUnicode()
259 // does what we want though. Unfortunately it keeps state, so
260 // we have to be careful around dead characters.
261
262 GetKeyboardState(state);
263
264 // Pressing Ctrl wreaks havoc with the symbol lookup, so turn
265 // that off. But AltGr shows up as Ctrl+Alt in Windows, so keep
266 // Ctrl if Alt is active.
267 if (!(state[VK_LCONTROL] & 0x80) || !(state[VK_RMENU] & 0x80))
268 state[VK_CONTROL] = state[VK_LCONTROL] = state[VK_RCONTROL] = 0;
269
270 // FIXME: Multi character results, like U+0644 U+0627
271 // on Arabic layout
272 ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0);
273
274 if (ret == 0) {
275 // Most Ctrl+Alt combinations will fail to produce a symbol, so
276 // try it again with Ctrl unconditionally disabled.
277 state[VK_CONTROL] = state[VK_LCONTROL] = state[VK_RCONTROL] = 0;
278 ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0);
279 }
280
281 if (ret == 1)
282 return ucs2keysym(wstr[0]);
283
284 if (ret == -1) {
285 WCHAR dead_char;
286
287 dead_char = wstr[0];
288
289 // Need to clear out the state that the dead key has caused.
290 // This is the recommended method by Microsoft's engineers:
291 // http://blogs.msdn.com/b/michkap/archive/2007/10/27/5717859.aspx
292 do {
293 ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0);
294 } while (ret < 0);
295
296 // Dead keys are represented by their spacing equivalent
297 // (or something similar depending on the layout)
298 return ucs2keysym(ucs2combining(dead_char));
299 }
300
301 return NoSymbol;
302}