Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / remoting / host / input_injector_x11.cc
blob86a0432f074666046d4cd24e14c47dfd8dd7d846
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/input_injector.h"
7 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XTest.h>
9 #include <X11/Xlib.h>
10 #include <X11/XKBlib.h>
12 #include <set>
14 #include "base/basictypes.h"
15 #include "base/bind.h"
16 #include "base/compiler_specific.h"
17 #include "base/location.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/strings/utf_string_conversion_utils.h"
20 #include "remoting/base/logging.h"
21 #if defined(OS_CHROMEOS)
22 #include "remoting/host/chromeos/point_transformer.h"
23 #endif
24 #include "remoting/host/clipboard.h"
25 #include "remoting/host/linux/unicode_to_keysym.h"
26 #include "remoting/proto/internal.pb.h"
27 #include "remoting/protocol/usb_key_codes.h"
28 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
29 #include "ui/events/keycodes/dom/keycode_converter.h"
31 namespace remoting {
33 namespace {
35 using protocol::ClipboardEvent;
36 using protocol::KeyEvent;
37 using protocol::TextEvent;
38 using protocol::MouseEvent;
39 using protocol::TouchEvent;
41 bool FindKeycodeForKeySym(Display* display,
42 KeySym key_sym,
43 uint32_t* keycode,
44 uint32_t* modifiers) {
45 *keycode = XKeysymToKeycode(display, key_sym);
47 const uint32_t kModifiersToTry[] = {
49 ShiftMask,
50 Mod2Mask,
51 Mod3Mask,
52 Mod4Mask,
53 ShiftMask | Mod2Mask,
54 ShiftMask | Mod3Mask,
55 ShiftMask | Mod4Mask,
58 // TODO(sergeyu): Is there a better way to find modifiers state?
59 for (size_t i = 0; i < arraysize(kModifiersToTry); ++i) {
60 unsigned long key_sym_with_mods;
61 if (XkbLookupKeySym(display, *keycode, kModifiersToTry[i], nullptr,
62 &key_sym_with_mods) &&
63 key_sym_with_mods == key_sym) {
64 *modifiers = kModifiersToTry[i];
65 return true;
69 return false;
72 // Finds a keycode and set of modifiers that generate character with the
73 // specified |code_point|.
74 bool FindKeycodeForUnicode(Display* display,
75 uint32_t code_point,
76 uint32_t* keycode,
77 uint32_t* modifiers) {
78 std::vector<uint32_t> keysyms;
79 GetKeySymsForUnicode(code_point, &keysyms);
81 for (std::vector<uint32_t>::iterator it = keysyms.begin();
82 it != keysyms.end(); ++it) {
83 if (FindKeycodeForKeySym(display, *it, keycode, modifiers)) {
84 return true;
88 return false;
91 bool IsModifierKey(int usb_keycode) {
92 return usb_keycode == kUsbLeftControl ||
93 usb_keycode == kUsbLeftShift ||
94 usb_keycode == kUsbLeftAlt ||
95 usb_keycode == kUsbLeftOs ||
96 usb_keycode == kUsbRightControl ||
97 usb_keycode == kUsbRightShift ||
98 usb_keycode == kUsbRightAlt ||
99 usb_keycode == kUsbRightOs;
102 // Pixel-to-wheel-ticks conversion ratio used by GTK.
103 // From third_party/WebKit/Source/web/gtk/WebInputEventFactory.cpp .
104 const float kWheelTicksPerPixel = 3.0f / 160.0f;
106 // A class to generate events on X11.
107 class InputInjectorX11 : public InputInjector {
108 public:
109 explicit InputInjectorX11(
110 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
111 ~InputInjectorX11() override;
113 bool Init();
115 // Clipboard stub interface.
116 void InjectClipboardEvent(const ClipboardEvent& event) override;
118 // InputStub interface.
119 void InjectKeyEvent(const KeyEvent& event) override;
120 void InjectTextEvent(const TextEvent& event) override;
121 void InjectMouseEvent(const MouseEvent& event) override;
122 void InjectTouchEvent(const TouchEvent& event) override;
124 // InputInjector interface.
125 void Start(scoped_ptr<protocol::ClipboardStub> client_clipboard) override;
127 private:
128 // The actual implementation resides in InputInjectorX11::Core class.
129 class Core : public base::RefCountedThreadSafe<Core> {
130 public:
131 explicit Core(scoped_refptr<base::SingleThreadTaskRunner> task_runner);
133 bool Init();
135 // Mirrors the ClipboardStub interface.
136 void InjectClipboardEvent(const ClipboardEvent& event);
138 // Mirrors the InputStub interface.
139 void InjectKeyEvent(const KeyEvent& event);
140 void InjectTextEvent(const TextEvent& event);
141 void InjectMouseEvent(const MouseEvent& event);
143 // Mirrors the InputInjector interface.
144 void Start(scoped_ptr<protocol::ClipboardStub> client_clipboard);
146 void Stop();
148 private:
149 friend class base::RefCountedThreadSafe<Core>;
150 virtual ~Core();
152 void InitClipboard();
154 // Queries whether keyboard auto-repeat is globally enabled. This is used
155 // to decide whether to temporarily disable then restore this setting. If
156 // auto-repeat has already been disabled, this class should leave it
157 // untouched.
158 bool IsAutoRepeatEnabled();
160 // Enables or disables keyboard auto-repeat globally.
161 void SetAutoRepeatEnabled(bool enabled);
163 void InjectScrollWheelClicks(int button, int count);
164 // Compensates for global button mappings and resets the XTest device
165 // mapping.
166 void InitMouseButtonMap();
167 int MouseButtonToX11ButtonNumber(MouseEvent::MouseButton button);
168 int HorizontalScrollWheelToX11ButtonNumber(int dx);
169 int VerticalScrollWheelToX11ButtonNumber(int dy);
171 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
173 std::set<int> pressed_keys_;
174 webrtc::DesktopVector latest_mouse_position_;
175 float wheel_ticks_x_;
176 float wheel_ticks_y_;
178 // X11 graphics context.
179 Display* display_;
180 Window root_window_;
182 int test_event_base_;
183 int test_error_base_;
185 // Number of buttons we support.
186 // Left, Right, Middle, VScroll Up/Down, HScroll Left/Right.
187 static const int kNumPointerButtons = 7;
189 int pointer_button_map_[kNumPointerButtons];
191 #if defined(OS_CHROMEOS)
192 PointTransformer point_transformer_;
193 #endif
195 scoped_ptr<Clipboard> clipboard_;
197 bool saved_auto_repeat_enabled_;
199 DISALLOW_COPY_AND_ASSIGN(Core);
202 scoped_refptr<Core> core_;
204 DISALLOW_COPY_AND_ASSIGN(InputInjectorX11);
207 InputInjectorX11::InputInjectorX11(
208 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
209 core_ = new Core(task_runner);
212 InputInjectorX11::~InputInjectorX11() {
213 core_->Stop();
216 bool InputInjectorX11::Init() {
217 return core_->Init();
220 void InputInjectorX11::InjectClipboardEvent(const ClipboardEvent& event) {
221 core_->InjectClipboardEvent(event);
224 void InputInjectorX11::InjectKeyEvent(const KeyEvent& event) {
225 core_->InjectKeyEvent(event);
228 void InputInjectorX11::InjectTextEvent(const TextEvent& event) {
229 core_->InjectTextEvent(event);
232 void InputInjectorX11::InjectMouseEvent(const MouseEvent& event) {
233 core_->InjectMouseEvent(event);
236 void InputInjectorX11::InjectTouchEvent(const TouchEvent& event) {
237 NOTIMPLEMENTED() << "Raw touch event injection not implemented for X11.";
240 void InputInjectorX11::Start(
241 scoped_ptr<protocol::ClipboardStub> client_clipboard) {
242 core_->Start(client_clipboard.Pass());
245 InputInjectorX11::Core::Core(
246 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
247 : task_runner_(task_runner),
248 latest_mouse_position_(-1, -1),
249 wheel_ticks_x_(0.0f),
250 wheel_ticks_y_(0.0f),
251 display_(XOpenDisplay(nullptr)),
252 root_window_(BadValue),
253 saved_auto_repeat_enabled_(false) {
256 bool InputInjectorX11::Core::Init() {
257 CHECK(display_);
259 if (!task_runner_->BelongsToCurrentThread())
260 task_runner_->PostTask(FROM_HERE, base::Bind(&Core::InitClipboard, this));
262 root_window_ = RootWindow(display_, DefaultScreen(display_));
263 if (root_window_ == BadValue) {
264 LOG(ERROR) << "Unable to get the root window";
265 return false;
268 // TODO(ajwong): Do we want to check the major/minor version at all for XTest?
269 int major = 0;
270 int minor = 0;
271 if (!XTestQueryExtension(display_, &test_event_base_, &test_error_base_,
272 &major, &minor)) {
273 LOG(ERROR) << "Server does not support XTest.";
274 return false;
276 InitMouseButtonMap();
277 return true;
280 void InputInjectorX11::Core::InjectClipboardEvent(
281 const ClipboardEvent& event) {
282 if (!task_runner_->BelongsToCurrentThread()) {
283 task_runner_->PostTask(
284 FROM_HERE, base::Bind(&Core::InjectClipboardEvent, this, event));
285 return;
288 // |clipboard_| will ignore unknown MIME-types, and verify the data's format.
289 clipboard_->InjectClipboardEvent(event);
292 void InputInjectorX11::Core::InjectKeyEvent(const KeyEvent& event) {
293 // HostEventDispatcher should filter events missing the pressed field.
294 if (!event.has_pressed() || !event.has_usb_keycode())
295 return;
297 if (!task_runner_->BelongsToCurrentThread()) {
298 task_runner_->PostTask(FROM_HERE,
299 base::Bind(&Core::InjectKeyEvent, this, event));
300 return;
303 int keycode =
304 ui::KeycodeConverter::UsbKeycodeToNativeKeycode(event.usb_keycode());
306 VLOG(3) << "Converting USB keycode: " << std::hex << event.usb_keycode()
307 << " to keycode: " << keycode << std::dec;
309 // Ignore events which can't be mapped.
310 if (keycode == ui::KeycodeConverter::InvalidNativeKeycode())
311 return;
313 if (event.pressed()) {
314 if (pressed_keys_.find(keycode) != pressed_keys_.end()) {
315 // Ignore repeats for modifier keys.
316 if (IsModifierKey(event.usb_keycode()))
317 return;
318 // Key is already held down, so lift the key up to ensure this repeated
319 // press takes effect.
320 XTestFakeKeyEvent(display_, keycode, False, CurrentTime);
323 if (pressed_keys_.empty()) {
324 // Disable auto-repeat, if necessary, to avoid triggering auto-repeat
325 // if network congestion delays the key-up event from the client.
326 saved_auto_repeat_enabled_ = IsAutoRepeatEnabled();
327 if (saved_auto_repeat_enabled_)
328 SetAutoRepeatEnabled(false);
330 pressed_keys_.insert(keycode);
331 } else {
332 pressed_keys_.erase(keycode);
333 if (pressed_keys_.empty()) {
334 // Re-enable auto-repeat, if necessary, when all keys are released.
335 if (saved_auto_repeat_enabled_)
336 SetAutoRepeatEnabled(true);
340 XTestFakeKeyEvent(display_, keycode, event.pressed(), CurrentTime);
341 XFlush(display_);
344 void InputInjectorX11::Core::InjectTextEvent(const TextEvent& event) {
345 if (!task_runner_->BelongsToCurrentThread()) {
346 task_runner_->PostTask(FROM_HERE,
347 base::Bind(&Core::InjectTextEvent, this, event));
348 return;
351 // Release all keys before injecting text event. This is necessary to avoid
352 // any interference with the currently pressed keys. E.g. if Shift is pressed
353 // when TextEvent is received.
354 for (int key : pressed_keys_) {
355 XTestFakeKeyEvent(display_, key, False, CurrentTime);
357 pressed_keys_.clear();
359 const std::string text = event.text();
360 for (int32 index = 0; index < static_cast<int32>(text.size()); ++index) {
361 uint32_t code_point;
362 if (!base::ReadUnicodeCharacter(
363 text.c_str(), text.size(), &index, &code_point)) {
364 continue;
367 uint32_t keycode;
368 uint32_t modifiers;
369 if (!FindKeycodeForUnicode(display_, code_point, &keycode, &modifiers))
370 continue;
372 XkbLockModifiers(display_, XkbUseCoreKbd, modifiers, modifiers);
374 XTestFakeKeyEvent(display_, keycode, True, CurrentTime);
375 XTestFakeKeyEvent(display_, keycode, False, CurrentTime);
377 XkbLockModifiers(display_, XkbUseCoreKbd, modifiers, 0);
380 XFlush(display_);
383 InputInjectorX11::Core::~Core() {
384 CHECK(pressed_keys_.empty());
387 void InputInjectorX11::Core::InitClipboard() {
388 DCHECK(task_runner_->BelongsToCurrentThread());
389 clipboard_ = Clipboard::Create();
392 bool InputInjectorX11::Core::IsAutoRepeatEnabled() {
393 XKeyboardState state;
394 if (!XGetKeyboardControl(display_, &state)) {
395 LOG(ERROR) << "Failed to get keyboard auto-repeat status, assuming ON.";
396 return true;
398 return state.global_auto_repeat == AutoRepeatModeOn;
401 void InputInjectorX11::Core::SetAutoRepeatEnabled(bool mode) {
402 XKeyboardControl control;
403 control.auto_repeat_mode = mode ? AutoRepeatModeOn : AutoRepeatModeOff;
404 XChangeKeyboardControl(display_, KBAutoRepeatMode, &control);
407 void InputInjectorX11::Core::InjectScrollWheelClicks(int button, int count) {
408 if (button < 0) {
409 LOG(WARNING) << "Ignoring unmapped scroll wheel button";
410 return;
412 for (int i = 0; i < count; i++) {
413 // Generate a button-down and a button-up to simulate a wheel click.
414 XTestFakeButtonEvent(display_, button, true, CurrentTime);
415 XTestFakeButtonEvent(display_, button, false, CurrentTime);
419 void InputInjectorX11::Core::InjectMouseEvent(const MouseEvent& event) {
420 if (!task_runner_->BelongsToCurrentThread()) {
421 task_runner_->PostTask(FROM_HERE,
422 base::Bind(&Core::InjectMouseEvent, this, event));
423 return;
426 if (event.has_delta_x() &&
427 event.has_delta_y() &&
428 (event.delta_x() != 0 || event.delta_y() != 0)) {
429 latest_mouse_position_.set(-1, -1);
430 VLOG(3) << "Moving mouse by " << event.delta_x() << "," << event.delta_y();
431 XTestFakeRelativeMotionEvent(display_,
432 event.delta_x(), event.delta_y(),
433 CurrentTime);
435 } else if (event.has_x() && event.has_y()) {
436 // Injecting a motion event immediately before a button release results in
437 // a MotionNotify even if the mouse position hasn't changed, which confuses
438 // apps which assume MotionNotify implies movement. See crbug.com/138075.
439 bool inject_motion = true;
440 webrtc::DesktopVector new_mouse_position(event.x(), event.y());
441 #if defined(OS_CHROMEOS)
442 // Interim hack to handle display rotation on Chrome OS.
443 // TODO(kelvin): Remove this when Chrome OS has completely migrated to
444 // Ozone (crbug.com/439287).
445 gfx::PointF screen_location = point_transformer_.ToScreenCoordinates(
446 gfx::PointF(event.x(), event.y()));
447 new_mouse_position.set(screen_location.x(), screen_location.y());
448 #endif
449 if (event.has_button() && event.has_button_down() && !event.button_down()) {
450 if (new_mouse_position.equals(latest_mouse_position_))
451 inject_motion = false;
454 if (inject_motion) {
455 latest_mouse_position_.set(std::max(0, new_mouse_position.x()),
456 std::max(0, new_mouse_position.y()));
458 VLOG(3) << "Moving mouse to " << latest_mouse_position_.x()
459 << "," << latest_mouse_position_.y();
460 XTestFakeMotionEvent(display_, DefaultScreen(display_),
461 latest_mouse_position_.x(),
462 latest_mouse_position_.y(),
463 CurrentTime);
467 if (event.has_button() && event.has_button_down()) {
468 int button_number = MouseButtonToX11ButtonNumber(event.button());
470 if (button_number < 0) {
471 LOG(WARNING) << "Ignoring unknown button type: " << event.button();
472 return;
475 VLOG(3) << "Button " << event.button()
476 << " received, sending "
477 << (event.button_down() ? "down " : "up ")
478 << button_number;
479 XTestFakeButtonEvent(display_, button_number, event.button_down(),
480 CurrentTime);
483 // Older client plugins always send scroll events in pixels, which
484 // must be accumulated host-side. Recent client plugins send both
485 // pixels and ticks with every scroll event, allowing the host to
486 // choose the best model on a per-platform basis. Since we can only
487 // inject ticks on Linux, use them if available.
488 int ticks_y = 0;
489 if (event.has_wheel_ticks_y()) {
490 ticks_y = event.wheel_ticks_y();
491 } else if (event.has_wheel_delta_y()) {
492 wheel_ticks_y_ += event.wheel_delta_y() * kWheelTicksPerPixel;
493 ticks_y = static_cast<int>(wheel_ticks_y_);
494 wheel_ticks_y_ -= ticks_y;
496 if (ticks_y != 0) {
497 InjectScrollWheelClicks(VerticalScrollWheelToX11ButtonNumber(ticks_y),
498 abs(ticks_y));
501 int ticks_x = 0;
502 if (event.has_wheel_ticks_x()) {
503 ticks_x = event.wheel_ticks_x();
504 } else if (event.has_wheel_delta_x()) {
505 wheel_ticks_x_ += event.wheel_delta_x() * kWheelTicksPerPixel;
506 ticks_x = static_cast<int>(wheel_ticks_x_);
507 wheel_ticks_x_ -= ticks_x;
509 if (ticks_x != 0) {
510 InjectScrollWheelClicks(HorizontalScrollWheelToX11ButtonNumber(ticks_x),
511 abs(ticks_x));
514 XFlush(display_);
517 void InputInjectorX11::Core::InitMouseButtonMap() {
518 // TODO(rmsousa): Run this on global/device mapping change events.
520 // Do not touch global pointer mapping, since this may affect the local user.
521 // Instead, try to work around it by reversing the mapping.
522 // Note that if a user has a global mapping that completely disables a button
523 // (by assigning 0 to it), we won't be able to inject it.
524 int num_buttons = XGetPointerMapping(display_, nullptr, 0);
525 scoped_ptr<unsigned char[]> pointer_mapping(new unsigned char[num_buttons]);
526 num_buttons = XGetPointerMapping(display_, pointer_mapping.get(),
527 num_buttons);
528 for (int i = 0; i < kNumPointerButtons; i++) {
529 pointer_button_map_[i] = -1;
531 for (int i = 0; i < num_buttons; i++) {
532 // Reverse the mapping.
533 if (pointer_mapping[i] > 0 && pointer_mapping[i] <= kNumPointerButtons)
534 pointer_button_map_[pointer_mapping[i] - 1] = i + 1;
536 for (int i = 0; i < kNumPointerButtons; i++) {
537 if (pointer_button_map_[i] == -1)
538 LOG(ERROR) << "Global pointer mapping does not support button " << i + 1;
541 int opcode, event, error;
542 if (!XQueryExtension(display_, "XInputExtension", &opcode, &event, &error)) {
543 // If XInput is not available, we're done. But it would be very unusual to
544 // have a server that supports XTest but not XInput, so log it as an error.
545 LOG(ERROR) << "X Input extension not available: " << error;
546 return;
549 // Make sure the XTEST XInput pointer device mapping is trivial. It should be
550 // safe to reset this mapping, as it won't affect the user's local devices.
551 // In fact, the reason why we do this is because an old gnome-settings-daemon
552 // may have mistakenly applied left-handed preferences to the XTEST device.
553 XID device_id = 0;
554 bool device_found = false;
555 int num_devices;
556 XDeviceInfo* devices;
557 devices = XListInputDevices(display_, &num_devices);
558 for (int i = 0; i < num_devices; i++) {
559 XDeviceInfo* device_info = &devices[i];
560 if (device_info->use == IsXExtensionPointer &&
561 strcmp(device_info->name, "Virtual core XTEST pointer") == 0) {
562 device_id = device_info->id;
563 device_found = true;
564 break;
567 XFreeDeviceList(devices);
569 if (!device_found) {
570 HOST_LOG << "Cannot find XTest device.";
571 return;
574 XDevice* device = XOpenDevice(display_, device_id);
575 if (!device) {
576 LOG(ERROR) << "Cannot open XTest device.";
577 return;
580 int num_device_buttons =
581 XGetDeviceButtonMapping(display_, device, nullptr, 0);
582 scoped_ptr<unsigned char[]> button_mapping(new unsigned char[num_buttons]);
583 for (int i = 0; i < num_device_buttons; i++) {
584 button_mapping[i] = i + 1;
586 error = XSetDeviceButtonMapping(display_, device, button_mapping.get(),
587 num_device_buttons);
588 if (error != Success)
589 LOG(ERROR) << "Failed to set XTest device button mapping: " << error;
591 XCloseDevice(display_, device);
594 int InputInjectorX11::Core::MouseButtonToX11ButtonNumber(
595 MouseEvent::MouseButton button) {
596 switch (button) {
597 case MouseEvent::BUTTON_LEFT:
598 return pointer_button_map_[0];
600 case MouseEvent::BUTTON_RIGHT:
601 return pointer_button_map_[2];
603 case MouseEvent::BUTTON_MIDDLE:
604 return pointer_button_map_[1];
606 case MouseEvent::BUTTON_UNDEFINED:
607 default:
608 return -1;
612 int InputInjectorX11::Core::HorizontalScrollWheelToX11ButtonNumber(int dx) {
613 return (dx > 0 ? pointer_button_map_[5] : pointer_button_map_[6]);
616 int InputInjectorX11::Core::VerticalScrollWheelToX11ButtonNumber(int dy) {
617 // Positive y-values are wheel scroll-up events (button 4), negative y-values
618 // are wheel scroll-down events (button 5).
619 return (dy > 0 ? pointer_button_map_[3] : pointer_button_map_[4]);
622 void InputInjectorX11::Core::Start(
623 scoped_ptr<protocol::ClipboardStub> client_clipboard) {
624 if (!task_runner_->BelongsToCurrentThread()) {
625 task_runner_->PostTask(
626 FROM_HERE,
627 base::Bind(&Core::Start, this, base::Passed(&client_clipboard)));
628 return;
631 InitMouseButtonMap();
633 clipboard_->Start(client_clipboard.Pass());
636 void InputInjectorX11::Core::Stop() {
637 if (!task_runner_->BelongsToCurrentThread()) {
638 task_runner_->PostTask(FROM_HERE, base::Bind(&Core::Stop, this));
639 return;
642 clipboard_.reset();
645 } // namespace
647 // static
648 scoped_ptr<InputInjector> InputInjector::Create(
649 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
650 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
651 scoped_ptr<InputInjectorX11> injector(
652 new InputInjectorX11(main_task_runner));
653 if (!injector->Init())
654 return nullptr;
655 return injector.Pass();
658 // static
659 bool InputInjector::SupportsTouchEvents() {
660 return false;
663 } // namespace remoting