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