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 10KB

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