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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Copyright (C) 2002-2003 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. // * Force the windows headers to include all the SendInput stuff
  27. #define _WIN32_WINNT 0x401
  28. #include <rfb_win32/SInput.h>
  29. #include <rfb_win32/Service.h>
  30. #include <rfb/LogWriter.h>
  31. #include <rfb_win32/OSVersion.h>
  32. #include <rfb_win32/Win32Util.h>
  33. #include "keymap.h"
  34. using namespace rfb;
  35. static LogWriter vlog("SInput");
  36. typedef UINT (WINAPI *_SendInput_proto)(UINT, LPINPUT, int);
  37. static win32::DynamicFn<_SendInput_proto> _SendInput(_T("user32.dll"), "SendInput");
  38. //
  39. // -=- Pointer implementation for Win32
  40. //
  41. static DWORD buttonDownMapping[8] = {
  42. MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_RIGHTDOWN,
  43. MOUSEEVENTF_WHEEL, MOUSEEVENTF_WHEEL, 0, 0, 0
  44. };
  45. static DWORD buttonUpMapping[8] = {
  46. MOUSEEVENTF_LEFTUP, MOUSEEVENTF_MIDDLEUP, MOUSEEVENTF_RIGHTUP,
  47. MOUSEEVENTF_WHEEL, MOUSEEVENTF_WHEEL, 0, 0, 0
  48. };
  49. static DWORD buttonDataMapping[8] = {
  50. 0, 0, 0, 120, -120, 0, 0, 0
  51. };
  52. win32::SPointer::SPointer()
  53. : last_buttonmask(0)
  54. {
  55. }
  56. void
  57. win32::SPointer::pointerEvent(const Point& pos, rdr::U8 buttonmask)
  58. {
  59. // - We are specifying absolute coordinates
  60. DWORD flags = MOUSEEVENTF_ABSOLUTE;
  61. // - Has the pointer moved since the last event?
  62. if (!last_position.equals(pos))
  63. flags |= MOUSEEVENTF_MOVE;
  64. // - If the system swaps left and right mouse buttons then we must
  65. // swap them here to negate the effect, so that we do the actual
  66. // action we mean to do
  67. if (::GetSystemMetrics(SM_SWAPBUTTON)) {
  68. bool leftDown = buttonmask & 1;
  69. bool rightDown = buttonmask & 4;
  70. buttonmask = (buttonmask & ~(1 | 4));
  71. if (leftDown) buttonmask |= 4;
  72. if (rightDown) buttonmask |= 1;
  73. }
  74. DWORD data = 0;
  75. for (int i = 0; i < 8; i++) {
  76. if ((buttonmask & (1<<i)) != (last_buttonmask & (1<<i))) {
  77. if (buttonmask & (1<<i)) {
  78. flags |= buttonDownMapping[i];
  79. if (buttonDataMapping[i]) {
  80. if (data) vlog.info("warning - two buttons set mouse_event data field");
  81. data = buttonDataMapping[i];
  82. }
  83. } else {
  84. flags |= buttonUpMapping[i];
  85. }
  86. }
  87. }
  88. last_position = pos;
  89. last_buttonmask = buttonmask;
  90. Rect primaryDisplay(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
  91. if (primaryDisplay.contains(pos)) {
  92. // mouse_event wants coordinates specified as a proportion of the
  93. // primary display's size, scaled to the range 0 to 65535
  94. Point scaled;
  95. scaled.x = (pos.x * 65535) / (primaryDisplay.width()-1);
  96. scaled.y = (pos.y * 65535) / (primaryDisplay.height()-1);
  97. ::mouse_event(flags, scaled.x, scaled.y, data, 0);
  98. } else {
  99. // The event lies outside the primary monitor. Under Win2K, we can just use
  100. // SendInput, which allows us to provide coordinates scaled to the virtual desktop.
  101. // SendInput is available on all multi-monitor-aware platforms.
  102. #ifdef SM_CXVIRTUALSCREEN
  103. if (osVersion.isPlatformNT) {
  104. if (!_SendInput.isValid())
  105. throw rdr::Exception("SendInput not available");
  106. INPUT evt;
  107. evt.type = INPUT_MOUSE;
  108. Point vPos(pos.x-GetSystemMetrics(SM_XVIRTUALSCREEN),
  109. pos.y-GetSystemMetrics(SM_YVIRTUALSCREEN));
  110. evt.mi.dx = (vPos.x * 65535) / (GetSystemMetrics(SM_CXVIRTUALSCREEN)-1);
  111. evt.mi.dy = (vPos.y * 65535) / (GetSystemMetrics(SM_CYVIRTUALSCREEN)-1);
  112. evt.mi.dwFlags = flags | MOUSEEVENTF_VIRTUALDESK;
  113. evt.mi.dwExtraInfo = 0;
  114. evt.mi.mouseData = data;
  115. evt.mi.time = 0;
  116. if ((*_SendInput)(1, &evt, sizeof(evt)) != 1)
  117. throw rdr::SystemException("SendInput", GetLastError());
  118. } else {
  119. // Under Win9x, this is not addressable by either mouse_event or SendInput
  120. // *** STUPID KLUDGY HACK ***
  121. POINT cursorPos; GetCursorPos(&cursorPos);
  122. ULONG oldSpeed, newSpeed = 10;
  123. ULONG mouseInfo[3];
  124. if (flags & MOUSEEVENTF_MOVE) {
  125. flags &= ~MOUSEEVENTF_ABSOLUTE;
  126. SystemParametersInfo(SPI_GETMOUSE, 0, &mouseInfo, 0);
  127. SystemParametersInfo(SPI_GETMOUSESPEED, 0, &oldSpeed, 0);
  128. vlog.debug("SPI_GETMOUSE %d, %d, %d, speed %d", mouseInfo[0], mouseInfo[1], mouseInfo[2], oldSpeed);
  129. ULONG idealMouseInfo[] = {10, 0, 0};
  130. SystemParametersInfo(SPI_SETMOUSESPEED, 0, &newSpeed, 0);
  131. SystemParametersInfo(SPI_SETMOUSE, 0, &idealMouseInfo, 0);
  132. }
  133. ::mouse_event(flags, pos.x-cursorPos.x, pos.y-cursorPos.y, data, 0);
  134. if (flags & MOUSEEVENTF_MOVE) {
  135. SystemParametersInfo(SPI_SETMOUSE, 0, &mouseInfo, 0);
  136. SystemParametersInfo(SPI_SETMOUSESPEED, 0, &oldSpeed, 0);
  137. }
  138. }
  139. #endif
  140. }
  141. }
  142. //
  143. // -=- Keyboard implementation
  144. //
  145. BoolParameter rfb::win32::SKeyboard::deadKeyAware("DeadKeyAware",
  146. "Whether to assume the viewer has already interpreted dead key sequences "
  147. "into latin-1 characters", true);
  148. static bool oneShift;
  149. // The keysymToAscii table transforms a couple of awkward keysyms into their
  150. // ASCII equivalents.
  151. struct keysymToAscii_t {
  152. rdr::U32 keysym;
  153. rdr::U8 ascii;
  154. };
  155. keysymToAscii_t keysymToAscii[] = {
  156. { XK_KP_Space, ' ' },
  157. { XK_KP_Equal, '=' },
  158. };
  159. rdr::U8 latin1DeadChars[] = {
  160. XK_grave, XK_acute, XK_asciicircum, XK_diaeresis, XK_degree, XK_cedilla,
  161. XK_asciitilde
  162. };
  163. struct latin1ToDeadChars_t {
  164. rdr::U8 latin1Char;
  165. rdr::U8 deadChar;
  166. rdr::U8 baseChar;
  167. };
  168. latin1ToDeadChars_t latin1ToDeadChars[] = {
  169. { XK_Agrave, XK_grave, XK_A },
  170. { XK_Egrave, XK_grave, XK_E },
  171. { XK_Igrave, XK_grave, XK_I },
  172. { XK_Ograve, XK_grave, XK_O },
  173. { XK_Ugrave, XK_grave, XK_U },
  174. { XK_agrave, XK_grave, XK_a },
  175. { XK_egrave, XK_grave, XK_e },
  176. { XK_igrave, XK_grave, XK_i },
  177. { XK_ograve, XK_grave, XK_o},
  178. { XK_ugrave, XK_grave, XK_u },
  179. { XK_Aacute, XK_acute, XK_A },
  180. { XK_Eacute, XK_acute, XK_E },
  181. { XK_Iacute, XK_acute, XK_I },
  182. { XK_Oacute, XK_acute, XK_O },
  183. { XK_Uacute, XK_acute, XK_U },
  184. { XK_Yacute, XK_acute, XK_Y },
  185. { XK_aacute, XK_acute, XK_a },
  186. { XK_eacute, XK_acute, XK_e },
  187. { XK_iacute, XK_acute, XK_i },
  188. { XK_oacute, XK_acute, XK_o},
  189. { XK_uacute, XK_acute, XK_u },
  190. { XK_yacute, XK_acute, XK_y },
  191. { XK_Acircumflex, XK_asciicircum, XK_A },
  192. { XK_Ecircumflex, XK_asciicircum, XK_E },
  193. { XK_Icircumflex, XK_asciicircum, XK_I },
  194. { XK_Ocircumflex, XK_asciicircum, XK_O },
  195. { XK_Ucircumflex, XK_asciicircum, XK_U },
  196. { XK_acircumflex, XK_asciicircum, XK_a },
  197. { XK_ecircumflex, XK_asciicircum, XK_e },
  198. { XK_icircumflex, XK_asciicircum, XK_i },
  199. { XK_ocircumflex, XK_asciicircum, XK_o},
  200. { XK_ucircumflex, XK_asciicircum, XK_u },
  201. { XK_Adiaeresis, XK_diaeresis, XK_A },
  202. { XK_Ediaeresis, XK_diaeresis, XK_E },
  203. { XK_Idiaeresis, XK_diaeresis, XK_I },
  204. { XK_Odiaeresis, XK_diaeresis, XK_O },
  205. { XK_Udiaeresis, XK_diaeresis, XK_U },
  206. { XK_adiaeresis, XK_diaeresis, XK_a },
  207. { XK_ediaeresis, XK_diaeresis, XK_e },
  208. { XK_idiaeresis, XK_diaeresis, XK_i },
  209. { XK_odiaeresis, XK_diaeresis, XK_o},
  210. { XK_udiaeresis, XK_diaeresis, XK_u },
  211. { XK_ydiaeresis, XK_diaeresis, XK_y },
  212. { XK_Aring, XK_degree, XK_A },
  213. { XK_aring, XK_degree, XK_a },
  214. { XK_Ccedilla, XK_cedilla, XK_C },
  215. { XK_ccedilla, XK_cedilla, XK_c },
  216. { XK_Atilde, XK_asciitilde, XK_A },
  217. { XK_Ntilde, XK_asciitilde, XK_N },
  218. { XK_Otilde, XK_asciitilde, XK_O },
  219. { XK_atilde, XK_asciitilde, XK_a },
  220. { XK_ntilde, XK_asciitilde, XK_n },
  221. { XK_otilde, XK_asciitilde, XK_o },
  222. };
  223. // doKeyboardEvent wraps the system keybd_event function and attempts to find
  224. // the appropriate scancode corresponding to the supplied virtual keycode.
  225. inline void doKeyboardEvent(BYTE vkCode, DWORD flags) {
  226. vlog.debug("vkCode 0x%x flags 0x%x", vkCode, flags);
  227. keybd_event(vkCode, MapVirtualKey(vkCode, 0), flags, 0);
  228. }
  229. // KeyStateModifier is a class which helps simplify generating a "fake" press
  230. // or release of shift, ctrl, alt, etc. An instance of the class is created
  231. // for every key which may need to be pressed or released. Then either press()
  232. // or release() may be called to make sure that the corresponding key is in the
  233. // right state. The destructor of the class automatically reverts to the
  234. // previous state.
  235. class KeyStateModifier {
  236. public:
  237. KeyStateModifier(int vkCode_, int flags_=0)
  238. : vkCode(vkCode_), flags(flags_), pressed(false), released(false)
  239. {}
  240. void press() {
  241. if (!(GetAsyncKeyState(vkCode) & 0x8000)) {
  242. doKeyboardEvent(vkCode, flags);
  243. pressed = true;
  244. }
  245. }
  246. void release() {
  247. if (GetAsyncKeyState(vkCode) & 0x8000) {
  248. doKeyboardEvent(vkCode, flags | KEYEVENTF_KEYUP);
  249. released = true;
  250. }
  251. }
  252. ~KeyStateModifier() {
  253. if (pressed) {
  254. doKeyboardEvent(vkCode, flags | KEYEVENTF_KEYUP);
  255. } else if (released) {
  256. doKeyboardEvent(vkCode, flags);
  257. }
  258. }
  259. int vkCode;
  260. int flags;
  261. bool pressed;
  262. bool released;
  263. };
  264. // doKeyEventWithModifiers() generates a key event having first "pressed" or
  265. // "released" the shift, ctrl or alt modifiers if necessary.
  266. void doKeyEventWithModifiers(BYTE vkCode, BYTE modifierState, bool down)
  267. {
  268. KeyStateModifier ctrl(VK_CONTROL);
  269. KeyStateModifier alt(VK_MENU);
  270. KeyStateModifier shift(VK_SHIFT);
  271. if (down) {
  272. if (modifierState & 2) ctrl.press();
  273. if (modifierState & 4) alt.press();
  274. if (modifierState & 1) {
  275. shift.press();
  276. } else {
  277. shift.release();
  278. }
  279. }
  280. doKeyboardEvent(vkCode, down ? 0 : KEYEVENTF_KEYUP);
  281. }
  282. win32::SKeyboard::SKeyboard()
  283. {
  284. oneShift = rfb::win32::osVersion.isPlatformWindows;
  285. for (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 (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, bool down)
  313. {
  314. for (int i = 0; i < sizeof(keysymToAscii) / sizeof(keysymToAscii_t); i++) {
  315. if (keysymToAscii[i].keysym == keysym) {
  316. keysym = keysymToAscii[i].ascii;
  317. break;
  318. }
  319. }
  320. if ((keysym >= 32 && keysym <= 126) ||
  321. (keysym >= 160 && keysym <= 255))
  322. {
  323. // ordinary Latin-1 character
  324. if (deadKeyAware) {
  325. // Detect dead chars and generate the dead char followed by space so
  326. // that we'll end up with the original char.
  327. for (int i = 0; i < deadChars.size(); i++) {
  328. if (keysym == deadChars[i]) {
  329. SHORT dc = VkKeyScan(keysym);
  330. if (dc != -1) {
  331. if (down) {
  332. vlog.info("latin-1 dead key: 0x%x vkCode 0x%x mod 0x%x "
  333. "followed by space", keysym, LOBYTE(dc), HIBYTE(dc));
  334. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), true);
  335. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), false);
  336. doKeyEventWithModifiers(VK_SPACE, 0, true);
  337. doKeyEventWithModifiers(VK_SPACE, 0, false);
  338. }
  339. return;
  340. }
  341. }
  342. }
  343. }
  344. SHORT s = VkKeyScan(keysym);
  345. if (s == -1) {
  346. if (down) {
  347. // not a single keypress - try synthesizing dead chars.
  348. for (int j = 0;
  349. j < sizeof(latin1ToDeadChars) / sizeof(latin1ToDeadChars_t);
  350. j++) {
  351. if (keysym == latin1ToDeadChars[j].latin1Char) {
  352. for (int i = 0; i < deadChars.size(); i++) {
  353. if (deadChars[i] == latin1ToDeadChars[j].deadChar) {
  354. SHORT dc = VkKeyScan(latin1ToDeadChars[j].deadChar);
  355. SHORT bc = VkKeyScan(latin1ToDeadChars[j].baseChar);
  356. if (dc != -1 && bc != -1) {
  357. vlog.info("latin-1 key: 0x%x dead key vkCode 0x%x mod 0x%x "
  358. "followed by vkCode 0x%x mod 0x%x",
  359. keysym, LOBYTE(dc), HIBYTE(dc),
  360. LOBYTE(bc), HIBYTE(bc));
  361. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), true);
  362. doKeyEventWithModifiers(LOBYTE(dc), HIBYTE(dc), false);
  363. doKeyEventWithModifiers(LOBYTE(bc), HIBYTE(bc), true);
  364. doKeyEventWithModifiers(LOBYTE(bc), HIBYTE(bc), false);
  365. return;
  366. }
  367. break;
  368. }
  369. }
  370. break;
  371. }
  372. }
  373. vlog.info("ignoring unrecognised Latin-1 keysym 0x%x",keysym);
  374. }
  375. return;
  376. }
  377. BYTE vkCode = LOBYTE(s);
  378. BYTE modifierState = HIBYTE(s);
  379. vlog.debug("latin-1 key: 0x%x vkCode 0x%x mod 0x%x down %d",
  380. keysym, vkCode, modifierState, down);
  381. doKeyEventWithModifiers(vkCode, modifierState, down);
  382. } else {
  383. // see if it's a recognised keyboard key, otherwise ignore it
  384. if (vkMap.find(keysym) == vkMap.end()) {
  385. vlog.info("ignoring unknown keysym 0x%x",keysym);
  386. return;
  387. }
  388. BYTE vkCode = vkMap[keysym];
  389. DWORD flags = 0;
  390. if (extendedMap[keysym]) flags |= KEYEVENTF_EXTENDEDKEY;
  391. if (!down) flags |= KEYEVENTF_KEYUP;
  392. vlog.debug("keyboard key: keysym 0x%x vkCode 0x%x ext %d down %d",
  393. keysym, vkCode, extendedMap[keysym], down);
  394. if (down && (vkCode == VK_DELETE) &&
  395. ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) &&
  396. ((GetAsyncKeyState(VK_MENU) & 0x8000) != 0))
  397. {
  398. rfb::win32::emulateCtrlAltDel();
  399. return;
  400. }
  401. doKeyboardEvent(vkCode, flags);
  402. }
  403. }