Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SInput.cxx 15KB

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