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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/event_executor.h"
#include <windows.h>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "remoting/host/capturer.h"
#include "remoting/host/clipboard.h"
#include "remoting/proto/event.pb.h"
#include "ui/base/keycodes/keyboard_codes.h"
namespace remoting {
namespace {
using protocol::ClipboardEvent;
using protocol::KeyEvent;
using protocol::MouseEvent;
// USB to XKB keycode map table.
#define USB_KEYMAP(usb, xkb, win, mac) {usb, win}
#include "remoting/host/usb_keycode_map.h"
#undef USB_KEYMAP
// A class to generate events on Windows.
class EventExecutorWin : public EventExecutor {
public:
EventExecutorWin(MessageLoop* message_loop,
base::MessageLoopProxy* ui_loop,
Capturer* capturer);
virtual ~EventExecutorWin() {}
// ClipboardStub interface.
virtual void InjectClipboardEvent(const ClipboardEvent& event) OVERRIDE;
// InputStub interface.
virtual void InjectKeyEvent(const KeyEvent& event) OVERRIDE;
virtual void InjectMouseEvent(const MouseEvent& event) OVERRIDE;
// EventExecutor interface.
virtual void OnSessionStarted(
scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE;
virtual void OnSessionFinished() OVERRIDE;
private:
HKL GetForegroundKeyboardLayout();
void HandleKey(const KeyEvent& event);
void HandleMouse(const MouseEvent& event);
MessageLoop* message_loop_;
base::MessageLoopProxy* ui_loop_;
Capturer* capturer_;
scoped_ptr<Clipboard> clipboard_;
DISALLOW_COPY_AND_ASSIGN(EventExecutorWin);
};
EventExecutorWin::EventExecutorWin(MessageLoop* message_loop,
base::MessageLoopProxy* ui_loop,
Capturer* capturer)
: message_loop_(message_loop),
ui_loop_(ui_loop),
capturer_(capturer),
clipboard_(Clipboard::Create()) {
}
void EventExecutorWin::InjectClipboardEvent(const ClipboardEvent& event) {
if (!ui_loop_->BelongsToCurrentThread()) {
ui_loop_->PostTask(
FROM_HERE,
base::Bind(&EventExecutorWin::InjectClipboardEvent,
base::Unretained(this),
event));
return;
}
clipboard_->InjectClipboardEvent(event);
}
void EventExecutorWin::InjectKeyEvent(const KeyEvent& event) {
if (MessageLoop::current() != message_loop_) {
message_loop_->PostTask(
FROM_HERE,
base::Bind(&EventExecutorWin::InjectKeyEvent, base::Unretained(this),
event));
return;
}
HandleKey(event);
}
void EventExecutorWin::InjectMouseEvent(const MouseEvent& event) {
if (MessageLoop::current() != message_loop_) {
message_loop_->PostTask(
FROM_HERE,
base::Bind(&EventExecutorWin::InjectMouseEvent, base::Unretained(this),
event));
return;
}
HandleMouse(event);
}
void EventExecutorWin::OnSessionStarted(
scoped_ptr<protocol::ClipboardStub> client_clipboard) {
if (!ui_loop_->BelongsToCurrentThread()) {
ui_loop_->PostTask(
FROM_HERE,
base::Bind(&EventExecutorWin::OnSessionStarted,
base::Unretained(this),
base::Passed(&client_clipboard)));
return;
}
clipboard_->Start(client_clipboard.Pass());
}
void EventExecutorWin::OnSessionFinished() {
if (!ui_loop_->BelongsToCurrentThread()) {
ui_loop_->PostTask(
FROM_HERE,
base::Bind(&EventExecutorWin::OnSessionFinished,
base::Unretained(this)));
return;
}
clipboard_->Stop();
}
HKL EventExecutorWin::GetForegroundKeyboardLayout() {
HKL layout = 0;
// Can return NULL if a window is losing focus.
HWND foreground = GetForegroundWindow();
if (foreground) {
// Can return 0 if the window no longer exists.
DWORD thread_id = GetWindowThreadProcessId(foreground, 0);
if (thread_id) {
// Can return 0 if the thread no longer exists, or if we're
// running on Windows Vista and the window is a command-prompt.
layout = GetKeyboardLayout(thread_id);
}
}
// If we couldn't determine a layout then use the system default.
if (!layout) {
SystemParametersInfo(SPI_GETDEFAULTINPUTLANG, 0, &layout, 0);
}
return layout;
}
void EventExecutorWin::HandleKey(const KeyEvent& event) {
// HostEventDispatcher should filter events missing the pressed field.
DCHECK(event.has_pressed());
// Reset the system idle suspend timeout.
SetThreadExecutionState(ES_SYSTEM_REQUIRED);
// The mapping between scancodes and VKEY values depends on the foreground
// window's current keyboard layout.
HKL layout = GetForegroundKeyboardLayout();
// Populate the a Windows INPUT structure for the event.
INPUT input;
memset(&input, 0, sizeof(input));
input.type = INPUT_KEYBOARD;
input.ki.time = 0;
input.ki.dwFlags = event.pressed() ? 0 : KEYEVENTF_KEYUP;
int scancode = kInvalidKeycode;
if (event.has_usb_keycode()) {
// If the event contains a USB-style code, map to a Windows scancode, and
// set a flag to have Windows look up the corresponding VK code.
input.ki.dwFlags |= KEYEVENTF_SCANCODE;
scancode = UsbKeycodeToNativeKeycode(event.usb_keycode());
VLOG(3) << "Converting USB keycode: " << std::hex << event.usb_keycode()
<< " to scancode: " << scancode << std::dec;
} else {
// If the event provides only a VKEY then use it, and map to the scancode.
input.ki.wVk = event.keycode();
scancode = MapVirtualKeyEx(event.keycode(), MAPVK_VK_TO_VSC_EX, layout);
VLOG(3) << "Converting VKEY: " << std::hex << event.keycode()
<< " to scancode: " << scancode << std::dec;
}
// Ignore events with no VK- or USB-keycode, or which can't be mapped.
if (scancode == kInvalidKeycode)
return;
// Windows scancodes are only 8-bit, so store the low-order byte into the
// event and set the extended flag if any high-order bits are set. The only
// high-order values we should see are 0xE0 or 0xE1. The extended bit usually
// distinguishes keys with the same meaning, e.g. left & right shift.
input.ki.wScan = scancode & 0xFF;
if ((scancode & 0xFF00) != 0x0000) {
input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
}
if (SendInput(1, &input, sizeof(INPUT)) == 0) {
LOG_GETLASTERROR(ERROR) << "Failed to inject a key event";
}
}
void EventExecutorWin::HandleMouse(const MouseEvent& event) {
// Reset the system idle suspend timeout.
SetThreadExecutionState(ES_SYSTEM_REQUIRED);
// TODO(garykac) Collapse mouse (x,y) and button events into a single
// input event when possible.
if (event.has_x() && event.has_y()) {
int x = event.x();
int y = event.y();
INPUT input;
input.type = INPUT_MOUSE;
input.mi.time = 0;
SkISize screen_size = capturer_->size_most_recent();
if ((screen_size.width() > 1) && (screen_size.height() > 1)) {
input.mi.dx = static_cast<int>((x * 65535) / (screen_size.width() - 1));
input.mi.dy = static_cast<int>((y * 65535) / (screen_size.height() - 1));
input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
if (SendInput(1, &input, sizeof(INPUT)) == 0) {
LOG_GETLASTERROR(ERROR) << "Failed to inject a mouse move event";
}
}
}
if (event.has_wheel_offset_x() && event.has_wheel_offset_y()) {
INPUT wheel;
wheel.type = INPUT_MOUSE;
wheel.mi.time = 0;
int dx = event.wheel_offset_x();
int dy = event.wheel_offset_y();
if (dx != 0) {
wheel.mi.mouseData = dx * WHEEL_DELTA;
wheel.mi.dwFlags = MOUSEEVENTF_HWHEEL;
if (SendInput(1, &wheel, sizeof(INPUT)) == 0) {
LOG_GETLASTERROR(ERROR) << "Failed to inject a mouse wheel(x) event";
}
}
if (dy != 0) {
wheel.mi.mouseData = dy * WHEEL_DELTA;
wheel.mi.dwFlags = MOUSEEVENTF_WHEEL;
if (SendInput(1, &wheel, sizeof(INPUT)) == 0) {
LOG_GETLASTERROR(ERROR) << "Failed to inject a mouse wheel(y) event";
}
}
}
if (event.has_button() && event.has_button_down()) {
INPUT button_event;
button_event.type = INPUT_MOUSE;
button_event.mi.time = 0;
button_event.mi.dx = 0;
button_event.mi.dy = 0;
MouseEvent::MouseButton button = event.button();
bool down = event.button_down();
if (button == MouseEvent::BUTTON_LEFT) {
button_event.mi.dwFlags =
down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
} else if (button == MouseEvent::BUTTON_MIDDLE) {
button_event.mi.dwFlags =
down ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
} else if (button == MouseEvent::BUTTON_RIGHT) {
button_event.mi.dwFlags =
down ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
} else {
button_event.mi.dwFlags =
down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
}
if (SendInput(1, &button_event, sizeof(INPUT)) == 0) {
LOG_GETLASTERROR(ERROR) << "Failed to inject a mouse button event";
}
}
}
} // namespace
scoped_ptr<EventExecutor> EventExecutor::Create(MessageLoop* message_loop,
base::MessageLoopProxy* ui_loop,
Capturer* capturer) {
return scoped_ptr<EventExecutor>(
new EventExecutorWin(message_loop, ui_loop, capturer));
}
} // namespace remoting
|