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

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