You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win32.c 12KB

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