summaryrefslogtreecommitdiffstats
path: root/remoting/host/input_injector_chromeos.cc
blob: 569119163b011d277edf97cc0ff2c1569d11dcec (plain)
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
// Copyright 2014 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/input_injector_chromeos.h"

#include <set>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "remoting/host/chromeos/point_transformer.h"
#include "remoting/host/clipboard.h"
#include "remoting/proto/internal.pb.h"
#include "ui/events/keycodes/dom3/dom_code.h"
#include "ui/events/keycodes/dom4/keycode_converter.h"
#include "ui/ozone/public/input_controller.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/system_input_injector.h"

namespace remoting {

using protocol::ClipboardEvent;
using protocol::KeyEvent;
using protocol::MouseEvent;
using protocol::TextEvent;

namespace {

ui::EventFlags MouseButtonToUIFlags(MouseEvent::MouseButton button) {
  switch (button) {
    case MouseEvent::BUTTON_LEFT:
      return ui::EF_LEFT_MOUSE_BUTTON;
    case MouseEvent::BUTTON_RIGHT:
      return ui::EF_RIGHT_MOUSE_BUTTON;
    case MouseEvent::BUTTON_MIDDLE:
      return ui::EF_MIDDLE_MOUSE_BUTTON;
    default:
      NOTREACHED();
      return ui::EF_NONE;
  }
}

}  // namespace

// This class is run exclusively on the UI thread of the browser process.
class InputInjectorChromeos::Core {
 public:
  Core(scoped_ptr<ui::SystemInputInjector> delegate_,
       ui::InputController* input_controller);

  // Mirrors the public InputInjectorChromeos interface.
  void InjectClipboardEvent(const ClipboardEvent& event);
  void InjectKeyEvent(const KeyEvent& event);
  void InjectTextEvent(const TextEvent& event);
  void InjectMouseEvent(const MouseEvent& event);
  void Start(scoped_ptr<protocol::ClipboardStub> client_clipboard);

 private:
  void HandleAutoRepeat(ui::DomCode dom_code, bool pressed);

  scoped_ptr<ui::SystemInputInjector> delegate_;
  ui::InputController* input_controller_;
  scoped_ptr<Clipboard> clipboard_;

  // Used to rotate the input coordinates appropriately based on the current
  // display rotation settings.
  scoped_ptr<PointTransformer> point_transformer_;

  // Used by HandleAutoRepeat().
  std::set<ui::DomCode> pressed_keys_;
  bool saved_auto_repeat_enabled_;

  DISALLOW_COPY_AND_ASSIGN(Core);
};

InputInjectorChromeos::Core::Core(scoped_ptr<ui::SystemInputInjector> delegate,
                                  ui::InputController* input_controller)
    : delegate_(delegate.Pass()),
      input_controller_(input_controller),
      saved_auto_repeat_enabled_(false) {
  DCHECK(delegate_);
  DCHECK(input_controller_);
}

void InputInjectorChromeos::Core::InjectClipboardEvent(
    const ClipboardEvent& event) {
  clipboard_->InjectClipboardEvent(event);
}

void InputInjectorChromeos::Core::InjectKeyEvent(const KeyEvent& event) {
  DCHECK(event.has_pressed());
  DCHECK(event.has_usb_keycode());

  ui::DomCode dom_code =
      ui::KeycodeConverter::UsbKeycodeToDomCode(event.usb_keycode());

  // Ignore events which can't be mapped.
  if (dom_code != ui::DomCode::NONE) {
    HandleAutoRepeat(dom_code, event.pressed());
    delegate_->InjectKeyPress(dom_code, event.pressed());
  }
}

// Disables auto-repeat as long as keys are pressed to avoid duplicated
// key-presses if network congestion delays the key-up event from the client.
void InputInjectorChromeos::Core::HandleAutoRepeat(ui::DomCode dom_code,
                                                   bool pressed) {
  if (pressed) {
    if (pressed_keys_.find(dom_code) != pressed_keys_.end()) {
      // Key is already held down, so lift the key up to ensure this repeated
      // press takes effect.
      // TODO(kelvinp): Fix this code to inject auto-repeated key presses as
      // the expected behavior of "down down down ... up" as opposed to current
      // implementation "down up down ... up".
      delegate_->InjectKeyPress(dom_code, false);
    }

    if (pressed_keys_.empty()) {
      // Disable auto-repeat, if necessary, when any key is pressed.
      saved_auto_repeat_enabled_ = input_controller_->IsAutoRepeatEnabled();
      if (saved_auto_repeat_enabled_) {
        input_controller_->SetAutoRepeatEnabled(false);
      }
    }
    pressed_keys_.insert(dom_code);
  } else {
    pressed_keys_.erase(dom_code);
    if (pressed_keys_.empty()) {
      // Re-enable auto-repeat, if necessary, when all keys are released.
      if (saved_auto_repeat_enabled_) {
        input_controller_->SetAutoRepeatEnabled(true);
      }
    }
  }
}

void InputInjectorChromeos::Core::InjectTextEvent(const TextEvent& event) {
  // Chrome OS only supports It2Me, which is not supported on mobile clients, so
  // we don't need to implement text events.
  NOTIMPLEMENTED();
}

void InputInjectorChromeos::Core::InjectMouseEvent(const MouseEvent& event) {
  if (event.has_button() && event.has_button_down()) {
    delegate_->InjectMouseButton(MouseButtonToUIFlags(event.button()),
                                 event.button_down());
  } else if (event.has_wheel_delta_y() || event.has_wheel_delta_x()) {
    delegate_->InjectMouseWheel(event.wheel_delta_x(), event.wheel_delta_y());
  } else {
    DCHECK(event.has_x() && event.has_y());
    delegate_->MoveCursorTo(point_transformer_->ToScreenCoordinates(
        gfx::PointF(event.x(), event.y())));
  }
}

void InputInjectorChromeos::Core::Start(
    scoped_ptr<protocol::ClipboardStub> client_clipboard) {
  clipboard_.reset(Clipboard::Create());
  clipboard_->Start(client_clipboard.Pass());
  point_transformer_.reset(new PointTransformer());
}

InputInjectorChromeos::InputInjectorChromeos(
    scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : input_task_runner_(task_runner) {
  ui::OzonePlatform* ozone_platform = ui::OzonePlatform::GetInstance();
  core_.reset(new Core(ozone_platform->CreateSystemInputInjector(),
                       ozone_platform->GetInputController()));
}

InputInjectorChromeos::~InputInjectorChromeos() {
  input_task_runner_->DeleteSoon(FROM_HERE, core_.release());
}

void InputInjectorChromeos::InjectClipboardEvent(const ClipboardEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE, base::Bind(&Core::InjectClipboardEvent,
                            base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::InjectKeyEvent(const KeyEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE,
      base::Bind(&Core::InjectKeyEvent, base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::InjectTextEvent(const TextEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE,
      base::Bind(&Core::InjectTextEvent, base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::InjectMouseEvent(const MouseEvent& event) {
  input_task_runner_->PostTask(
      FROM_HERE, base::Bind(&Core::InjectMouseEvent,
                            base::Unretained(core_.get()), event));
}

void InputInjectorChromeos::Start(
    scoped_ptr<protocol::ClipboardStub> client_clipboard) {
  input_task_runner_->PostTask(
      FROM_HERE, base::Bind(&Core::Start, base::Unretained(core_.get()),
                            base::Passed(&client_clipboard)));
}

// static
scoped_ptr<InputInjector> InputInjector::Create(
    scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
    scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
  // The Ozone input injector must be called on the UI task runner of the
  // browser process.
  return make_scoped_ptr(new InputInjectorChromeos(ui_task_runner));
}

}  // namespace remoting