Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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