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.

SInput.cxx 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- SInput.cxx
  19. //
  20. // A number of routines that accept VNC input event data and perform
  21. // the appropriate actions under Win32
  22. #define XK_MISCELLANY
  23. #define XK_LATIN1
  24. #define XK_CURRENCY
  25. #include <rfb/keysymdef.h>
  26. #include <tchar.h>
  27. #include <rfb_win32/SInput.h>
  28. #include <rfb_win32/MonitorInfo.h>
  29. #include <rfb_win32/Service.h>
  30. #include <rfb_win32/keymap.h>
  31. #include <rdr/Exception.h>
  32. #include <rfb/LogWriter.h>
  33. using namespace rfb;
  34. static LogWriter vlog("SInput");
  35. //
  36. // -=- Pointer implementation for Win32
  37. //
  38. static DWORD buttonDownMapping[8] = {
  39. MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_RIGHTDOWN,
  40. MOUSEEVENTF_WHEEL, MOUSEEVENTF_WHEEL, 0, 0, 0
  41. };
  42. static DWORD buttonUpMapping[8] = {
  43. MOUSEEVENTF_LEFTUP, MOUSEEVENTF_MIDDLEUP, MOUSEEVENTF_RIGHTUP,
  44. MOUSEEVENTF_WHEEL, MOUSEEVENTF_WHEEL, 0, 0, 0
  45. };
  46. static DWORD buttonDataMapping[8] = {
  47. 0, 0, 0, 120, (DWORD)-120, 0, 0, 0
  48. };
  49. win32::SPointer::SPointer()
  50. : last_buttonmask(0)
  51. {
  52. }
  53. void
  54. win32::SPointer::pointerEvent(const Point& pos, int buttonmask)
  55. {
  56. // - We are specifying absolute coordinates
  57. DWORD flags = MOUSEEVENTF_ABSOLUTE;
  58. // - Has the pointer moved since the last event?
  59. if (!last_position.equals(pos))
  60. flags |= MOUSEEVENTF_MOVE;
  61. // - If the system swaps left and right mouse buttons then we must
  62. // swap them here to negate the effect, so that we do the actual
  63. // action we mean to do
  64. if (::GetSystemMetrics(SM_SWAPBUTTON)) {
  65. bool leftDown = buttonmask & 1;
  66. bool rightDown = buttonmask & 4;
  67. buttonmask = (buttonmask & ~(1 | 4));
  68. if (leftDown) buttonmask |= 4;
  69. if (rightDown) buttonmask |= 1;
  70. }
  71. DWORD data = 0;
  72. for (int i = 0; i < 8; i++) {
  73. if ((buttonmask & (1<<i)) != (last_buttonmask & (1<<i))) {
  74. if (buttonmask & (1<<i)) {
  75. flags |= buttonDownMapping[i];
  76. if (buttonDataMapping[i]) {
  77. if (data) vlog.info("warning - two buttons set mouse_event data field");
  78. data = buttonDataMapping[i];
  79. }
  80. } else {
  81. flags |= buttonUpMapping[i];
  82. }
  83. }
  84. }
  85. last_position = pos;
  86. last_buttonmask = buttonmask;
  87. Rect primaryDisplay(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
  88. if (primaryDisplay.contains(pos)) {
  89. // mouse_event wants coordinates specified as a proportion of the
  90. // primary display's size, scaled to the range 0 to 65535
  91. Point scaled;
  92. scaled.x = (pos.x * 65535) / (primaryDisplay.width()-1);
  93. scaled.y = (pos.y * 65535) / (primaryDisplay.height()-1);
  94. ::mouse_event(flags, scaled.x, scaled.y, data, 0);
  95. } else {
  96. // The event lies outside the primary monitor. Under Win2K, we can just use
  97. // SendInput, which allows us to provide coordinates scaled to the virtual desktop.
  98. // SendInput is available on all multi-monitor-aware platforms.
  99. INPUT evt;
  100. evt.type = INPUT_MOUSE;
  101. Point vPos(pos.x-GetSystemMetrics(SM_XVIRTUALSCREEN),
  102. pos.y-GetSystemMetrics(SM_YVIRTUALSCREEN));
  103. evt.mi.dx = (vPos.x * 65535) / (GetSystemMetrics(SM_CXVIRTUALSCREEN)-1);
  104. evt.mi.dy = (vPos.y * 65535) / (GetSystemMetrics(SM_CYVIRTUALSCREEN)-1);
  105. evt.mi.dwFlags = flags | MOUSEEVENTF_VIRTUALDESK;
  106. evt.mi.dwExtraInfo = 0;
  107. evt.mi.mouseData = data;
  108. evt.mi.time = 0;
  109. if (SendInput(1, &evt, sizeof(evt)) != 1)
  110. throw rdr::SystemException("SendInput", GetLastError());
  111. }
  112. }
  113. //
  114. // -=- Keyboard implementation
  115. //
  116. BoolParameter rfb::win32::SKeyboard::deadKeyAware("DeadKeyAware",
  117. "Whether to assume the viewer has already interpreted dead key sequences "
  118. "into latin-1 characters", true);
  119. BoolParameter rfb::win32::SKeyboard::rawKeyboard("RawKeyboard",
  120. "Send keyboard events straight through and avoid mapping them to the "
  121. "current keyboard layout", false);
  122. // The keysymToAscii table transforms a couple of awkward keysyms into their
  123. // ASCII equivalents.
  124. struct keysymToAscii_t {
  125. rdr::U32 keysym;
  126. rdr::U8 ascii;
  127. };
  128. keysymToAscii_t keysymToAscii[] = {
  129. { XK_KP_Space, ' ' },
  130. { XK_KP_Equal, '=' },
  131. };
  132. rdr::U8 latin1DeadChars[] = {
  133. XK_grave, XK_acute, XK_asciicircum, XK_diaeresis, XK_degree, XK_cedilla,
  134. XK_asciitilde
  135. };
  136. struct latin1ToDeadChars_t {
  137. rdr::U8 latin1Char;
  138. rdr::U8 deadChar;
  139. rdr::U8 baseChar;
  140. };
  141. latin1ToDeadChars_t latin1ToDeadChars[] = {
  142. { XK_Agrave, XK_grave, XK_A },
  143. { XK_Egrave, XK_grave, XK_E },
  144. { XK_Igrave, XK_grave, XK_I },
  145. { XK_Ograve, XK_grave, XK_O },
  146. { XK_Ugrave, XK_grave, XK_U },
  147. { XK_agrave, XK_grave, XK_a },
  148. { XK_egrave, XK_grave, XK_e },
  149. { XK_igrave, XK_grave, XK_i },
  150. { XK_ograve, XK_grave, XK_o},
  151. { XK_ugrave, XK_grave, XK_u },
  152. { XK_Aacute, XK_acute, XK_A },
  153. { XK_Eacute, XK_acute, XK_E },
  154. { XK_Iacute, XK_acute, XK_I },
  155. { XK_Oacute, XK_acute, XK_O },
  156. { XK_Uacute, XK_acute, XK_U },
  157. { XK_Yacute, XK_acute, XK_Y },
  158. { XK_aacute, XK_acute, XK_a },
  159. { XK_eacute, XK_acute, XK_e },
  160. { XK_iacute, XK_acute, XK_i },
  161. { XK_oacute, XK_acute, XK_o},
  162. { XK_uacute, XK_acute, XK_u },
  163. { XK_yacute, XK_acute, XK_y },
  164. { XK_Acircumflex, XK_asciicircum, XK_A },
  165. { XK_Ecircumflex, XK_asciicircum, XK_E },
  166. { XK_Icircumflex, XK_asciicircum, XK_I },
  167. { XK_Ocircumflex, XK_asciicircum, XK_O },
  168. { XK_Ucircumflex, XK_asciicircum, XK_U },
  169. { XK_acircumflex, XK_asciicircum, XK_a },
  170. { XK_ecircumflex, XK_asciicircum, XK_e },
  171. { XK_icircumflex, XK_asciicircum, XK_i },
  172. { XK_ocircumflex, XK_asciicircum, XK_o},
  173. { XK_ucircumflex, XK_asciicircum, XK_u },
  174. { XK_Adiaeresis, XK_diaeresis, XK_A },
  175. { XK_Ediaeresis, XK_diaeresis, XK_E },
  176. { XK_Idiaeresis, XK_diaeresis, XK_I },
  177. { XK_Odiaeresis, XK_diaeresis, XK_O },
  178. { XK_Udiaeresis, XK_diaeresis, XK_U },
  179. { XK_adiaeresis, XK_diaeresis, XK_a },
  180. { XK_ediaeresis, XK_diaeresis, XK_e },
  181. { XK_idiaeresis, XK_diaeresis, XK_i },
  182. { XK_odiaeresis, XK_diaeresis, XK_o},
  183. { XK_udiaeresis, XK_diaeresis, XK_u },
  184. { XK_ydiaeresis, XK_diaeresis, XK_y },
  185. { XK_Aring, XK_degree, XK_A },
  186. { XK_aring, XK_degree, XK_a },
  187. { XK_Ccedilla, XK_cedilla, XK_C },
  188. { XK_ccedilla, XK_cedilla, XK_c },
  189. { XK_Atilde, XK_asciitilde, XK_A },
  190. { XK_Ntilde, XK_asciitilde, XK_N },
  191. { XK_Otilde, XK_asciitilde, XK_O },
  192. { XK_atilde, XK_asciitilde, XK_a },
  193. { XK_ntilde, XK_asciitilde, XK_n },
  194. { XK_otilde, XK_asciitilde, XK_o },
  195. };
  196. // doKeyboardEvent wraps the system keybd_event function and attempts to find
  197. // the appropriate scancode corresponding to the supplied virtual keycode.
  198. inline void doKeyboardEvent(BYTE vkCode, DWORD flags) {
  199. vlog.debug("vkCode 0x%x flags 0x%lx", vkCode, flags);
  200. keybd_event(vkCode, MapVirtualKey(vkCode, 0), flags, 0);
  201. }
  202. inline void doScanCodeEvent(BYTE scancode, bool down) {
  203. INPUT evt;
  204. evt.type = INPUT_KEYBOARD;
  205. evt.ki.wVk = 0;
  206. evt.ki.dwFlags = KEYEVENTF_SCANCODE;
  207. if (!down)
  208. evt.ki.dwFlags |= KEYEVENTF_KEYUP;
  209. if (scancode & 0x80) {
  210. evt.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
  211. scancode &= ~0x80;
  212. }
  213. evt.ki.wScan = scancode;
  214. evt.ki.dwExtraInfo = 0;
  215. evt.ki.time = 0;
  216. vlog.debug("SendInput ScanCode: 0x%x Flags: 0x%lx %s", scancode,
  217. evt.ki.dwFlags, down ? "Down" : "Up");
  218. // Windows has some bug where it doesn't look up scan code 0x45
  219. // properly, so we need to help it out
  220. if (evt.ki.wScan == 0x45) {
  221. evt.ki.dwFlags &= ~KEYEVENTF_SCANCODE;
  222. if (evt.ki.dwFlags & KEYEVENTF_EXTENDEDKEY)
  223. evt.ki.wVk = VK_NUMLOCK;
  224. else
  225. evt.ki.wVk = VK_PAUSE;
  226. }
  227. if (SendInput(1, &evt, sizeof(evt)) != 1)
  228. vlog.error("SendInput %lu", GetLastError());
  229. }
  230. // KeyStateModifier is a class which helps simplify generating a "fake" press
  231. // or release of shift, ctrl, alt, etc. An instance of the class is created
  232. // for every key which may need to be pressed or released. Then either press()
  233. // or release() may be called to make sure that the corresponding key is in the
  234. // right state. The destructor of the class automatically reverts to the
  235. // previous state.
  236. class KeyStateModifier {
  237. public:
  238. KeyStateModifier(int vkCode_, int flags_=0)
  239. : vkCode(vkCode_), flags(flags_), pressed(false), released(false)
  240. {}
  241. void press() {
  242. if (!(GetAsyncKeyState(vkCode) & 0x8000)) {
  243. doKeyboardEvent(vkCode, flags);
  244. pressed = true;
  245. }
  246. }
  247. void release() {
  248. if (GetAsyncKeyState(vkCode) & 0x8000) {
  249. doKeyboardEvent(vkCode, flags | KEYEVENTF_KEYUP);
  250. released = true;
  251. }
  252. }
  253. ~KeyStateModifier() {
  254. if (pressed) {
  255. doKeyboardEvent(vkCode, flags | KEYEVENTF_KEYUP);
  256. } else if (released) {
  257. doKeyboardEvent(vkCode, flags);
  258. }
  259. }
  260. int vkCode;
  261. int flags;
  262. bool pressed;
  263. bool released;
  264. };
  265. // doKeyEventWithModifiers() generates a key event having first "pressed" or
  266. // "released" the shift, ctrl or alt modifiers if necessary.
  267. void doKeyEventWithModifiers(BYTE vkCode, BYTE modifierState, bool down)
  268. {
  269. KeyStateModifier ctrl(VK_CONTROL);
  270. KeyStateModifier alt(VK_MENU);
  271. KeyStateModifier shift(VK_SHIFT);
  272. if (down) {
  273. if (modifierState & 2) ctrl.press();
  274. if (modifierState & 4) alt.press();
  275. if (modifierState & 1) {
  276. shift.press();
  277. } else {
  278. shift.release();
  279. }
  280. }
  281. doKeyboardEvent(vkCode, down ? 0 : KEYEVENTF_KEYUP);
  282. }
  283. win32::SKeyboard::SKeyboard()
  284. {
  285. for (unsigned int i = 0; i < sizeof(keymap) / sizeof(keymap_t); i++) {
  286. vkMap[keymap[i].keysym] = keymap[i].vk;
  287. extendedMap[keymap[i].keysym] = keymap[i].extended;
  288. }
  289. // Find dead characters for the current keyboard layout
  290. // XXX how could we handle the keyboard layout changing?
  291. BYTE keystate[256];
  292. memset(keystate, 0, 256);
  293. for (unsigned int j = 0; j < sizeof(latin1DeadChars); j++) {
  294. SHORT s = VkKeyScan(latin1DeadChars[j]);
  295. if (s != -1) {
  296. BYTE vkCode = LOBYTE(s);
  297. BYTE modifierState = HIBYTE(s);
  298. keystate[VK_SHIFT] = (modifierState & 1) ? 0x80 : 0;
  299. keystate[VK_CONTROL] = (modifierState & 2) ? 0x80 : 0;
  300. keystate[VK_MENU] = (modifierState & 4) ? 0x80 : 0;
  301. rdr::U8 chars[2];
  302. int nchars = ToAscii(vkCode, 0, keystate, (WORD*)&chars, 0);
  303. if (nchars < 0) {
  304. vlog.debug("Found dead key 0x%x '%c'",
  305. latin1DeadChars[j], latin1DeadChars[j]);
  306. deadChars.push_back(latin1DeadChars[j]);
  307. ToAscii(vkCode, 0, keystate, (WORD*)&chars, 0);
  308. }
  309. }
  310. }
  311. }
  312. void win32::SKeyboard::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down)
  313. {
  314. // If scan code is available use that directly as windows uses
  315. // compatible scancodes
  316. if (keycode && rawKeyboard) {
  317. // However NumLock incorrectly has the extended bit set
  318. if (keycode == 0x45)
  319. keycode = 0xc5;
  320. // And Pause uses NumLock's proper code, except when Control is
  321. // also pressed (i.e. when it is generating Break)
  322. if ((keycode == 0xc6) && !(GetAsyncKeyState(VK_CONTROL) & 0x8000))
  323. keycode = 0x45;
  324. // And PrintScreen uses a different code than Alt+PrintScreen (SysRq)
  325. if ((keycode == 0x54) && !(GetAsyncKeyState(VK_MENU) & 0x8000))
  326. keycode = 0xb7;
  327. if (down && (keycode == 0xd3) &&
  328. ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) &&
  329. ((GetAsyncKeyState(VK_MENU) & 0x8000) != 0))
  330. {
  331. rfb::win32::emulateCtrlAltDel();
  332. return;
  333. }
  334. doScanCodeEvent(keycode, down);
  335. return;
  336. }
  337. for (unsigned int i = 0; i < sizeof(keysymToAscii) / sizeof(keysymToAscii_t); i++) {
  338. if (keysymToAscii[i].keysym == keysym) {
  339. keysym = keysymToAscii[i].ascii;
  340. break;
  341. }
  342. }
  343. if ((keysym >= 32 && keysym <= 126) ||
  344. (keysym >= 160 && keysym <= 255))
  345. {
  346. // ordinary Latin-1 character
  347. if (deadKeyAware) {
  348. // Detect dead chars and generate the dead char followed by space so
  349. // that we'll end up with the original char.
  350. for (unsigned int i = 0; i < deadChars.size(); i++) {
  351. if (keysym == deadChars[i]) {
  352. SHORT dc = VkKeyScan(keysym);
  353. if (dc != -1) {
  354. if (down) {
  355. vlog.info("latin-1 dead key: 0x%x vkCode 0x%x mod 0x%x "
  356. "followed by space", keysym, LOBYTE(dc), HIBYTE(dc));
  357. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), true);
  358. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), false);
  359. doKeyEventWithModifiers(VK_SPACE, 0, true);
  360. doKeyEventWithModifiers(VK_SPACE, 0, false);
  361. }
  362. return;
  363. }
  364. }
  365. }
  366. }
  367. SHORT s = VkKeyScan(keysym);
  368. if (s == -1) {
  369. if (down) {
  370. // not a single keypress - try synthesizing dead chars.
  371. for (unsigned int j = 0;
  372. j < sizeof(latin1ToDeadChars) / sizeof(latin1ToDeadChars_t);
  373. j++) {
  374. if (keysym == latin1ToDeadChars[j].latin1Char) {
  375. for (unsigned int i = 0; i < deadChars.size(); i++) {
  376. if (deadChars[i] == latin1ToDeadChars[j].deadChar) {
  377. SHORT dc = VkKeyScan(latin1ToDeadChars[j].deadChar);
  378. SHORT bc = VkKeyScan(latin1ToDeadChars[j].baseChar);
  379. if (dc != -1 && bc != -1) {
  380. vlog.info("latin-1 key: 0x%x dead key vkCode 0x%x mod 0x%x "
  381. "followed by vkCode 0x%x mod 0x%x",
  382. keysym, LOBYTE(dc), HIBYTE(dc),
  383. LOBYTE(bc), HIBYTE(bc));
  384. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), true);
  385. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), false);
  386. doKeyEventWithModifiers(LOBYTE(bc), HIBYTE(bc), true);
  387. doKeyEventWithModifiers(LOBYTE(bc), HIBYTE(bc), false);
  388. return;
  389. }
  390. break;
  391. }
  392. }
  393. break;
  394. }
  395. }
  396. vlog.info("ignoring unrecognised Latin-1 keysym 0x%x",keysym);
  397. }
  398. return;
  399. }
  400. BYTE vkCode = LOBYTE(s);
  401. BYTE modifierState = HIBYTE(s);
  402. vlog.debug("latin-1 key: 0x%x vkCode 0x%x mod 0x%x down %d",
  403. keysym, vkCode, modifierState, down);
  404. doKeyEventWithModifiers(vkCode, modifierState, down);
  405. } else {
  406. // see if it's a recognised keyboard key, otherwise ignore it
  407. if (vkMap.find(keysym) == vkMap.end()) {
  408. vlog.info("ignoring unknown keysym 0x%x",keysym);
  409. return;
  410. }
  411. BYTE vkCode = vkMap[keysym];
  412. DWORD flags = 0;
  413. if (extendedMap[keysym]) flags |= KEYEVENTF_EXTENDEDKEY;
  414. if (!down) flags |= KEYEVENTF_KEYUP;
  415. vlog.debug("keyboard key: keysym 0x%x vkCode 0x%x ext %d down %d",
  416. keysym, vkCode, extendedMap[keysym], down);
  417. if (down && (vkCode == VK_DELETE) &&
  418. ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) &&
  419. ((GetAsyncKeyState(VK_MENU) & 0x8000) != 0))
  420. {
  421. rfb::win32::emulateCtrlAltDel();
  422. return;
  423. }
  424. doKeyboardEvent(vkCode, flags);
  425. }
  426. }