Pierre Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 1 | /* 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 Ossman | 2e9684f | 2014-07-21 16:46:22 +0200 | [diff] [blame] | 22 | #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 Ossman | 407a5c3 | 2011-05-26 14:48:29 +0000 | [diff] [blame] | 36 | static HANDLE thread; |
| 37 | static DWORD thread_id; |
| 38 | |
| 39 | static HHOOK hook = 0; |
| 40 | static HWND target_wnd = 0; |
| 41 | |
| 42 | static 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 | |
| 60 | static 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 | |
| 78 | static 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 | |
| 101 | int 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 | |
| 120 | void 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 Ossman | 2e9684f | 2014-07-21 16:46:22 +0200 | [diff] [blame] | 130 | |
| 131 | static const int vkey_map[][3] = { |
| 132 | { VK_BACK, XK_BackSpace, NoSymbol }, |
| 133 | { VK_TAB, XK_Tab, NoSymbol }, |
| 134 | { VK_CLEAR, XK_Clear, NoSymbol }, |
| 135 | { VK_RETURN, XK_Return, XK_KP_Enter }, |
| 136 | { VK_SHIFT, XK_Shift_L, NoSymbol }, |
| 137 | { VK_CONTROL, XK_Control_L, XK_Control_R }, |
| 138 | { VK_MENU, XK_Alt_L, XK_Alt_R }, |
| 139 | { VK_PAUSE, XK_Pause, NoSymbol }, |
| 140 | { VK_CAPITAL, XK_Caps_Lock, NoSymbol }, |
| 141 | /* FIXME: IME keys */ |
| 142 | { VK_ESCAPE, XK_Escape, NoSymbol }, |
| 143 | { VK_PRIOR, XK_KP_Prior, XK_Prior }, |
| 144 | { VK_NEXT, XK_KP_Next, XK_Next }, |
| 145 | { VK_END, XK_KP_End, XK_End }, |
| 146 | { VK_HOME, XK_KP_Home, XK_Home }, |
| 147 | { VK_LEFT, XK_KP_Left, XK_Left }, |
| 148 | { VK_UP, XK_KP_Up, XK_Up }, |
| 149 | { VK_RIGHT, XK_KP_Right, XK_Right }, |
| 150 | { VK_DOWN, XK_KP_Down, XK_Down }, |
Pierre Ossman | a7d3dc7 | 2014-09-30 17:03:28 +0200 | [diff] [blame^] | 151 | { VK_SNAPSHOT, XK_Sys_Req, XK_Print }, |
Pierre Ossman | 2e9684f | 2014-07-21 16:46:22 +0200 | [diff] [blame] | 152 | { VK_INSERT, XK_KP_Insert, XK_Insert }, |
| 153 | { VK_DELETE, XK_KP_Delete, XK_Delete }, |
| 154 | { VK_LWIN, NoSymbol, XK_Super_L }, |
| 155 | { VK_RWIN, NoSymbol, XK_Super_R }, |
| 156 | { VK_APPS, NoSymbol, XK_Menu }, |
| 157 | { VK_SLEEP, NoSymbol, XF86XK_Sleep }, |
| 158 | { VK_NUMPAD0, XK_KP_0, NoSymbol }, |
| 159 | { VK_NUMPAD1, XK_KP_1, NoSymbol }, |
| 160 | { VK_NUMPAD2, XK_KP_2, NoSymbol }, |
| 161 | { VK_NUMPAD3, XK_KP_3, NoSymbol }, |
| 162 | { VK_NUMPAD4, XK_KP_4, NoSymbol }, |
| 163 | { VK_NUMPAD5, XK_KP_5, NoSymbol }, |
| 164 | { VK_NUMPAD6, XK_KP_6, NoSymbol }, |
| 165 | { VK_NUMPAD7, XK_KP_7, NoSymbol }, |
| 166 | { VK_NUMPAD8, XK_KP_8, NoSymbol }, |
| 167 | { VK_NUMPAD9, XK_KP_9, NoSymbol }, |
| 168 | { VK_MULTIPLY, XK_KP_Multiply, NoSymbol }, |
| 169 | { VK_ADD, XK_KP_Add, NoSymbol }, |
| 170 | { VK_SUBTRACT, XK_KP_Subtract, NoSymbol }, |
| 171 | { VK_DIVIDE, NoSymbol, XK_KP_Divide }, |
| 172 | /* VK_SEPARATOR and VK_DECIMAL left out on purpose. See further down. */ |
| 173 | { VK_F1, XK_F1, NoSymbol }, |
| 174 | { VK_F2, XK_F2, NoSymbol }, |
| 175 | { VK_F3, XK_F3, NoSymbol }, |
| 176 | { VK_F4, XK_F4, NoSymbol }, |
| 177 | { VK_F5, XK_F5, NoSymbol }, |
| 178 | { VK_F6, XK_F6, NoSymbol }, |
| 179 | { VK_F7, XK_F7, NoSymbol }, |
| 180 | { VK_F8, XK_F8, NoSymbol }, |
| 181 | { VK_F9, XK_F9, NoSymbol }, |
| 182 | { VK_F10, XK_F10, NoSymbol }, |
| 183 | { VK_F11, XK_F11, NoSymbol }, |
| 184 | { VK_F12, XK_F12, NoSymbol }, |
| 185 | { VK_F13, XK_F13, NoSymbol }, |
| 186 | { VK_F14, XK_F14, NoSymbol }, |
| 187 | { VK_F15, XK_F15, NoSymbol }, |
| 188 | { VK_F16, XK_F16, NoSymbol }, |
| 189 | { VK_F17, XK_F17, NoSymbol }, |
| 190 | { VK_F18, XK_F18, NoSymbol }, |
| 191 | { VK_F19, XK_F19, NoSymbol }, |
| 192 | { VK_F20, XK_F20, NoSymbol }, |
| 193 | { VK_F21, XK_F21, NoSymbol }, |
| 194 | { VK_F22, XK_F22, NoSymbol }, |
| 195 | { VK_F23, XK_F23, NoSymbol }, |
| 196 | { VK_F24, XK_F24, NoSymbol }, |
| 197 | { VK_NUMLOCK, NoSymbol, XK_Num_Lock }, |
| 198 | { VK_SCROLL, XK_Scroll_Lock, NoSymbol }, |
| 199 | { VK_BROWSER_BACK, NoSymbol, XF86XK_Back }, |
| 200 | { VK_BROWSER_FORWARD, NoSymbol, XF86XK_Forward }, |
| 201 | { VK_BROWSER_REFRESH, NoSymbol, XF86XK_Refresh }, |
| 202 | { VK_BROWSER_STOP, NoSymbol, XF86XK_Stop }, |
| 203 | { VK_BROWSER_SEARCH, NoSymbol, XF86XK_Search }, |
| 204 | { VK_BROWSER_FAVORITES, NoSymbol, XF86XK_Favorites }, |
| 205 | { VK_BROWSER_HOME, NoSymbol, XF86XK_HomePage }, |
| 206 | { VK_VOLUME_MUTE, NoSymbol, XF86XK_AudioMute }, |
| 207 | { VK_VOLUME_DOWN, NoSymbol, XF86XK_AudioLowerVolume }, |
| 208 | { VK_VOLUME_UP, NoSymbol, XF86XK_AudioRaiseVolume }, |
| 209 | { VK_MEDIA_NEXT_TRACK, NoSymbol, XF86XK_AudioNext }, |
| 210 | { VK_MEDIA_PREV_TRACK, NoSymbol, XF86XK_AudioPrev }, |
| 211 | { VK_MEDIA_STOP, NoSymbol, XF86XK_AudioStop }, |
| 212 | { VK_MEDIA_PLAY_PAUSE, NoSymbol, XF86XK_AudioPlay }, |
| 213 | { VK_LAUNCH_MAIL, NoSymbol, XF86XK_Mail }, |
| 214 | { VK_LAUNCH_APP2, NoSymbol, XF86XK_Calculator }, |
| 215 | }; |
| 216 | |
| 217 | int win32_vkey_to_keysym(UINT vkey, int extended) |
| 218 | { |
| 219 | int i; |
| 220 | |
| 221 | BYTE state[256]; |
| 222 | int ret; |
| 223 | WCHAR wstr[10]; |
| 224 | |
| 225 | // Start with keys that either don't generate a symbol, or |
| 226 | // generate the same symbol as some other key. |
| 227 | for (i = 0;i < sizeof(vkey_map)/sizeof(vkey_map[0]);i++) { |
| 228 | if (vkey == vkey_map[i][0]) { |
| 229 | if (extended) |
| 230 | return vkey_map[i][2]; |
| 231 | else |
| 232 | return vkey_map[i][1]; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Windows is not consistent in which virtual key it uses for |
| 237 | // the numpad decimal key, and this is not likely to be fixed: |
| 238 | // http://blogs.msdn.com/michkap/archive/2006/09/13/752377.aspx |
| 239 | // |
| 240 | // To get X11 behaviour, we instead look at the text generated |
| 241 | // by they key. |
| 242 | if ((vkey == VK_DECIMAL) || (vkey == VK_SEPARATOR)) { |
| 243 | UINT ch; |
| 244 | |
| 245 | ch = MapVirtualKey(vkey, MAPVK_VK_TO_CHAR); |
| 246 | switch (ch) { |
| 247 | case ',': |
| 248 | return XK_KP_Separator; |
| 249 | case '.': |
| 250 | return XK_KP_Decimal; |
| 251 | default: |
| 252 | return NoSymbol; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // MapVirtualKey() doesn't look at modifiers, so it is |
| 257 | // insufficient for mapping most keys to a symbol. ToUnicode() |
| 258 | // does what we want though. Unfortunately it keeps state, so |
| 259 | // we have to be careful around dead characters. |
| 260 | |
| 261 | GetKeyboardState(state); |
| 262 | |
| 263 | // Pressing Ctrl wreaks havoc with the symbol lookup, so turn |
| 264 | // that off. But AltGr shows up as Ctrl+Alt in Windows, so keep |
| 265 | // Ctrl if Alt is active. |
| 266 | if (!(state[VK_LCONTROL] & 0x80) || !(state[VK_RMENU] & 0x80)) |
| 267 | state[VK_CONTROL] = state[VK_LCONTROL] = state[VK_RCONTROL] = 0; |
| 268 | |
| 269 | // FIXME: Multi character results, like U+0644 U+0627 |
| 270 | // on Arabic layout |
| 271 | ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0); |
| 272 | |
| 273 | if (ret == 0) { |
| 274 | // Most Ctrl+Alt combinations will fail to produce a symbol, so |
| 275 | // try it again with Ctrl unconditionally disabled. |
| 276 | state[VK_CONTROL] = state[VK_LCONTROL] = state[VK_RCONTROL] = 0; |
| 277 | ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0); |
| 278 | } |
| 279 | |
| 280 | if (ret == 1) |
| 281 | return ucs2keysym(wstr[0]); |
| 282 | |
| 283 | if (ret == -1) { |
| 284 | WCHAR dead_char; |
| 285 | |
| 286 | dead_char = wstr[0]; |
| 287 | |
| 288 | // Need to clear out the state that the dead key has caused. |
| 289 | // This is the recommended method by Microsoft's engineers: |
| 290 | // http://blogs.msdn.com/b/michkap/archive/2007/10/27/5717859.aspx |
| 291 | do { |
| 292 | ret = ToUnicode(vkey, 0, state, wstr, sizeof(wstr)/sizeof(wstr[0]), 0); |
| 293 | } while (ret < 0); |
| 294 | |
| 295 | // Dead keys are represented by their spacing equivalent |
| 296 | // (or something similar depending on the layout) |
| 297 | return ucs2keysym(ucs2combining(dead_char)); |
| 298 | } |
| 299 | |
| 300 | return NoSymbol; |
| 301 | } |