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