Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / client / plugin / pepper_input_handler.cc
blob01fec29419924043c715e0a714fe71c5e0d1ad41
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/client/plugin/pepper_input_handler.h"
7 #include "base/logging.h"
8 #include "ppapi/cpp/image_data.h"
9 #include "ppapi/cpp/input_event.h"
10 #include "ppapi/cpp/module_impl.h"
11 #include "ppapi/cpp/mouse_cursor.h"
12 #include "ppapi/cpp/point.h"
13 #include "ppapi/cpp/touch_point.h"
14 #include "ppapi/cpp/var.h"
15 #include "remoting/proto/event.pb.h"
16 #include "remoting/protocol/input_event_tracker.h"
17 #include "remoting/protocol/input_stub.h"
18 #include "ui/events/keycodes/dom/keycode_converter.h"
20 namespace remoting {
22 namespace {
24 void SetTouchEventType(PP_InputEvent_Type pp_type,
25 protocol::TouchEvent* touch_event) {
26 DCHECK(touch_event);
27 switch (pp_type) {
28 case PP_INPUTEVENT_TYPE_TOUCHSTART:
29 touch_event->set_event_type(protocol::TouchEvent::TOUCH_POINT_START);
30 return;
31 case PP_INPUTEVENT_TYPE_TOUCHMOVE:
32 touch_event->set_event_type(protocol::TouchEvent::TOUCH_POINT_MOVE);
33 return;
34 case PP_INPUTEVENT_TYPE_TOUCHEND:
35 touch_event->set_event_type(protocol::TouchEvent::TOUCH_POINT_END);
36 return;
37 case PP_INPUTEVENT_TYPE_TOUCHCANCEL:
38 touch_event->set_event_type(protocol::TouchEvent::TOUCH_POINT_CANCEL);
39 return;
40 default:
41 NOTREACHED() << "Unknown event type: " << pp_type;
42 return;
46 // Creates a protocol::TouchEvent instance from |pp_touch_event|.
47 // Note that only the changed touches are added to the TouchEvent.
48 protocol::TouchEvent MakeTouchEvent(const pp::TouchInputEvent& pp_touch_event) {
49 protocol::TouchEvent touch_event;
50 SetTouchEventType(pp_touch_event.GetType(), &touch_event);
51 DCHECK(touch_event.has_event_type());
53 for (uint32_t i = 0;
54 i < pp_touch_event.GetTouchCount(PP_TOUCHLIST_TYPE_CHANGEDTOUCHES);
55 ++i) {
56 pp::TouchPoint pp_point =
57 pp_touch_event.GetTouchByIndex(PP_TOUCHLIST_TYPE_CHANGEDTOUCHES, i);
58 protocol::TouchEventPoint* point = touch_event.add_touch_points();
59 point->set_id(pp_point.id());
60 point->set_x(pp_point.position().x());
61 point->set_y(pp_point.position().y());
62 point->set_radius_x(pp_point.radii().x());
63 point->set_radius_y(pp_point.radii().y());
64 point->set_angle(pp_point.rotation_angle());
67 return touch_event;
70 // Builds the Chromotocol lock states flags for the PPAPI |event|.
71 uint32_t MakeLockStates(const pp::InputEvent& event) {
72 uint32_t modifiers = event.GetModifiers();
73 uint32_t lock_states = 0;
75 if (modifiers & PP_INPUTEVENT_MODIFIER_CAPSLOCKKEY)
76 lock_states |= protocol::KeyEvent::LOCK_STATES_CAPSLOCK;
78 if (modifiers & PP_INPUTEVENT_MODIFIER_NUMLOCKKEY)
79 lock_states |= protocol::KeyEvent::LOCK_STATES_NUMLOCK;
81 return lock_states;
84 // Builds a protocol::KeyEvent from the supplied PPAPI event.
85 protocol::KeyEvent MakeKeyEvent(const pp::KeyboardInputEvent& pp_key_event) {
86 protocol::KeyEvent key_event;
87 std::string dom_code = pp_key_event.GetCode().AsString();
88 key_event.set_usb_keycode(
89 ui::KeycodeConverter::CodeToUsbKeycode(dom_code.c_str()));
90 key_event.set_pressed(pp_key_event.GetType() == PP_INPUTEVENT_TYPE_KEYDOWN);
91 key_event.set_lock_states(MakeLockStates(pp_key_event));
92 return key_event;
95 // Builds a protocol::MouseEvent from the supplied PPAPI event.
96 protocol::MouseEvent MakeMouseEvent(const pp::MouseInputEvent& pp_mouse_event,
97 bool set_deltas) {
98 protocol::MouseEvent mouse_event;
99 mouse_event.set_x(pp_mouse_event.GetPosition().x());
100 mouse_event.set_y(pp_mouse_event.GetPosition().y());
101 if (set_deltas) {
102 pp::Point delta = pp_mouse_event.GetMovement();
103 mouse_event.set_delta_x(delta.x());
104 mouse_event.set_delta_y(delta.y());
106 return mouse_event;
109 } // namespace
111 PepperInputHandler::PepperInputHandler(
112 protocol::InputEventTracker* input_tracker)
113 : input_stub_(nullptr),
114 input_tracker_(input_tracker),
115 has_focus_(false),
116 send_mouse_input_when_unfocused_(false),
117 send_mouse_move_deltas_(false),
118 wheel_delta_x_(0),
119 wheel_delta_y_(0),
120 wheel_ticks_x_(0),
121 wheel_ticks_y_(0),
122 detect_stuck_modifiers_(false) {
125 bool PepperInputHandler::HandleInputEvent(const pp::InputEvent& event) {
126 if (detect_stuck_modifiers_)
127 ReleaseAllIfModifiersStuck(event);
129 switch (event.GetType()) {
130 // Touch input cases.
131 case PP_INPUTEVENT_TYPE_TOUCHSTART:
132 case PP_INPUTEVENT_TYPE_TOUCHMOVE:
133 case PP_INPUTEVENT_TYPE_TOUCHEND:
134 case PP_INPUTEVENT_TYPE_TOUCHCANCEL: {
135 if (!input_stub_)
136 return true;
137 pp::TouchInputEvent pp_touch_event(event);
138 input_stub_->InjectTouchEvent(MakeTouchEvent(pp_touch_event));
139 return true;
142 case PP_INPUTEVENT_TYPE_CONTEXTMENU: {
143 // We need to return true here or else we'll get a local (plugin) context
144 // menu instead of the mouseup event for the right click.
145 return true;
148 case PP_INPUTEVENT_TYPE_KEYDOWN:
149 case PP_INPUTEVENT_TYPE_KEYUP: {
150 if (!input_stub_)
151 return true;
152 pp::KeyboardInputEvent pp_key_event(event);
153 input_stub_->InjectKeyEvent(MakeKeyEvent(pp_key_event));
154 return true;
157 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
158 case PP_INPUTEVENT_TYPE_MOUSEUP: {
159 if (!has_focus_ && !send_mouse_input_when_unfocused_)
160 return false;
161 if (!input_stub_)
162 return true;
164 pp::MouseInputEvent pp_mouse_event(event);
165 protocol::MouseEvent mouse_event(
166 MakeMouseEvent(pp_mouse_event, send_mouse_move_deltas_));
167 switch (pp_mouse_event.GetButton()) {
168 case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
169 mouse_event.set_button(protocol::MouseEvent::BUTTON_LEFT);
170 break;
171 case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
172 mouse_event.set_button(protocol::MouseEvent::BUTTON_MIDDLE);
173 break;
174 case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
175 mouse_event.set_button(protocol::MouseEvent::BUTTON_RIGHT);
176 break;
177 case PP_INPUTEVENT_MOUSEBUTTON_NONE:
178 break;
180 if (mouse_event.has_button()) {
181 bool is_down = (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN);
182 mouse_event.set_button_down(is_down);
183 input_stub_->InjectMouseEvent(mouse_event);
186 return true;
189 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
190 case PP_INPUTEVENT_TYPE_MOUSEENTER:
191 case PP_INPUTEVENT_TYPE_MOUSELEAVE: {
192 if (!has_focus_ && !send_mouse_input_when_unfocused_)
193 return false;
194 if (!input_stub_)
195 return true;
197 pp::MouseInputEvent pp_mouse_event(event);
198 input_stub_->InjectMouseEvent(
199 MakeMouseEvent(pp_mouse_event, send_mouse_move_deltas_));
201 return true;
204 case PP_INPUTEVENT_TYPE_WHEEL: {
205 if (!has_focus_ && !send_mouse_input_when_unfocused_)
206 return false;
207 if (!input_stub_)
208 return true;
210 pp::WheelInputEvent pp_wheel_event(event);
212 // Ignore scroll-by-page events, for now.
213 if (pp_wheel_event.GetScrollByPage())
214 return true;
216 // Add this event to our accumulated sub-pixel deltas and clicks.
217 pp::FloatPoint delta = pp_wheel_event.GetDelta();
218 wheel_delta_x_ += delta.x();
219 wheel_delta_y_ += delta.y();
220 pp::FloatPoint ticks = pp_wheel_event.GetTicks();
221 wheel_ticks_x_ += ticks.x();
222 wheel_ticks_y_ += ticks.y();
224 // If there is at least a pixel's movement, emit an event. We don't
225 // ever expect to accumulate one tick's worth of scrolling without
226 // accumulating a pixel's worth at the same time, so this is safe.
227 int delta_x = static_cast<int>(wheel_delta_x_);
228 int delta_y = static_cast<int>(wheel_delta_y_);
229 if (delta_x != 0 || delta_y != 0) {
230 wheel_delta_x_ -= delta_x;
231 wheel_delta_y_ -= delta_y;
232 protocol::MouseEvent mouse_event;
233 mouse_event.set_wheel_delta_x(delta_x);
234 mouse_event.set_wheel_delta_y(delta_y);
236 // Always include the ticks in the event, even if insufficient pixel
237 // scrolling has accumulated for a single tick. This informs hosts
238 // that can't inject pixel-based scroll events that the client will
239 // accumulate them into tick-based scrolling, which gives a better
240 // overall experience than trying to do this host-side.
241 int ticks_x = static_cast<int>(wheel_ticks_x_);
242 int ticks_y = static_cast<int>(wheel_ticks_y_);
243 wheel_ticks_x_ -= ticks_x;
244 wheel_ticks_y_ -= ticks_y;
245 mouse_event.set_wheel_ticks_x(ticks_x);
246 mouse_event.set_wheel_ticks_y(ticks_y);
248 input_stub_->InjectMouseEvent(mouse_event);
250 return true;
253 case PP_INPUTEVENT_TYPE_CHAR:
254 // Consume but ignore character input events.
255 return true;
257 default: {
258 VLOG(0) << "Unhandled input event: " << event.GetType();
259 break;
263 return false;
266 void PepperInputHandler::DidChangeFocus(bool has_focus) {
267 has_focus_ = has_focus;
270 void PepperInputHandler::ReleaseAllIfModifiersStuck(
271 const pp::InputEvent& event) {
272 switch (event.GetType()) {
273 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
274 case PP_INPUTEVENT_TYPE_MOUSEENTER:
275 case PP_INPUTEVENT_TYPE_MOUSELEAVE:
276 // Don't check modifiers on every mouse move event.
277 break;
279 case PP_INPUTEVENT_TYPE_KEYUP:
280 // PPAPI doesn't always set modifiers correctly on KEYUP events. See
281 // crbug.com/464791 for details.
282 break;
284 default: {
285 uint32_t modifiers = event.GetModifiers();
286 input_tracker_->ReleaseAllIfModifiersStuck(
287 (modifiers & PP_INPUTEVENT_MODIFIER_ALTKEY) != 0,
288 (modifiers & PP_INPUTEVENT_MODIFIER_CONTROLKEY) != 0,
289 (modifiers & PP_INPUTEVENT_MODIFIER_METAKEY) != 0,
290 (modifiers & PP_INPUTEVENT_MODIFIER_SHIFTKEY) != 0);
295 } // namespace remoting