Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / remoting / client / plugin / pepper_input_handler.cc
blobe4bbac272e61f251119bbe67308771408e589231
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/dom4/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) {
124 bool PepperInputHandler::HandleInputEvent(const pp::InputEvent& event) {
125 ReleaseAllIfModifiersStuck(event);
127 switch (event.GetType()) {
128 // Touch input cases.
129 case PP_INPUTEVENT_TYPE_TOUCHSTART:
130 case PP_INPUTEVENT_TYPE_TOUCHMOVE:
131 case PP_INPUTEVENT_TYPE_TOUCHEND:
132 case PP_INPUTEVENT_TYPE_TOUCHCANCEL: {
133 if (!input_stub_)
134 return true;
135 pp::TouchInputEvent pp_touch_event(event);
136 input_stub_->InjectTouchEvent(MakeTouchEvent(pp_touch_event));
137 return true;
140 case PP_INPUTEVENT_TYPE_CONTEXTMENU: {
141 // We need to return true here or else we'll get a local (plugin) context
142 // menu instead of the mouseup event for the right click.
143 return true;
146 case PP_INPUTEVENT_TYPE_KEYDOWN:
147 case PP_INPUTEVENT_TYPE_KEYUP: {
148 if (!input_stub_)
149 return true;
150 pp::KeyboardInputEvent pp_key_event(event);
151 input_stub_->InjectKeyEvent(MakeKeyEvent(pp_key_event));
152 return true;
155 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
156 case PP_INPUTEVENT_TYPE_MOUSEUP: {
157 if (!has_focus_ && !send_mouse_input_when_unfocused_)
158 return false;
159 if (!input_stub_)
160 return true;
162 pp::MouseInputEvent pp_mouse_event(event);
163 protocol::MouseEvent mouse_event(
164 MakeMouseEvent(pp_mouse_event, send_mouse_move_deltas_));
165 switch (pp_mouse_event.GetButton()) {
166 case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
167 mouse_event.set_button(protocol::MouseEvent::BUTTON_LEFT);
168 break;
169 case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
170 mouse_event.set_button(protocol::MouseEvent::BUTTON_MIDDLE);
171 break;
172 case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
173 mouse_event.set_button(protocol::MouseEvent::BUTTON_RIGHT);
174 break;
175 case PP_INPUTEVENT_MOUSEBUTTON_NONE:
176 break;
178 if (mouse_event.has_button()) {
179 bool is_down = (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN);
180 mouse_event.set_button_down(is_down);
181 input_stub_->InjectMouseEvent(mouse_event);
184 return true;
187 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
188 case PP_INPUTEVENT_TYPE_MOUSEENTER:
189 case PP_INPUTEVENT_TYPE_MOUSELEAVE: {
190 if (!has_focus_ && !send_mouse_input_when_unfocused_)
191 return false;
192 if (!input_stub_)
193 return true;
195 pp::MouseInputEvent pp_mouse_event(event);
196 input_stub_->InjectMouseEvent(
197 MakeMouseEvent(pp_mouse_event, send_mouse_move_deltas_));
199 return true;
202 case PP_INPUTEVENT_TYPE_WHEEL: {
203 if (!has_focus_ && !send_mouse_input_when_unfocused_)
204 return false;
205 if (!input_stub_)
206 return true;
208 pp::WheelInputEvent pp_wheel_event(event);
210 // Ignore scroll-by-page events, for now.
211 if (pp_wheel_event.GetScrollByPage())
212 return true;
214 // Add this event to our accumulated sub-pixel deltas and clicks.
215 pp::FloatPoint delta = pp_wheel_event.GetDelta();
216 wheel_delta_x_ += delta.x();
217 wheel_delta_y_ += delta.y();
218 pp::FloatPoint ticks = pp_wheel_event.GetTicks();
219 wheel_ticks_x_ += ticks.x();
220 wheel_ticks_y_ += ticks.y();
222 // If there is at least a pixel's movement, emit an event. We don't
223 // ever expect to accumulate one tick's worth of scrolling without
224 // accumulating a pixel's worth at the same time, so this is safe.
225 int delta_x = static_cast<int>(wheel_delta_x_);
226 int delta_y = static_cast<int>(wheel_delta_y_);
227 if (delta_x != 0 || delta_y != 0) {
228 wheel_delta_x_ -= delta_x;
229 wheel_delta_y_ -= delta_y;
230 protocol::MouseEvent mouse_event;
231 mouse_event.set_wheel_delta_x(delta_x);
232 mouse_event.set_wheel_delta_y(delta_y);
234 // Always include the ticks in the event, even if insufficient pixel
235 // scrolling has accumulated for a single tick. This informs hosts
236 // that can't inject pixel-based scroll events that the client will
237 // accumulate them into tick-based scrolling, which gives a better
238 // overall experience than trying to do this host-side.
239 int ticks_x = static_cast<int>(wheel_ticks_x_);
240 int ticks_y = static_cast<int>(wheel_ticks_y_);
241 wheel_ticks_x_ -= ticks_x;
242 wheel_ticks_y_ -= ticks_y;
243 mouse_event.set_wheel_ticks_x(ticks_x);
244 mouse_event.set_wheel_ticks_y(ticks_y);
246 input_stub_->InjectMouseEvent(mouse_event);
248 return true;
251 case PP_INPUTEVENT_TYPE_CHAR:
252 // Consume but ignore character input events.
253 return true;
255 default: {
256 VLOG(0) << "Unhandled input event: " << event.GetType();
257 break;
261 return false;
264 void PepperInputHandler::DidChangeFocus(bool has_focus) {
265 has_focus_ = has_focus;
268 void PepperInputHandler::ReleaseAllIfModifiersStuck(
269 const pp::InputEvent& event) {
270 switch (event.GetType()) {
271 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
272 case PP_INPUTEVENT_TYPE_MOUSEENTER:
273 case PP_INPUTEVENT_TYPE_MOUSELEAVE:
274 // Don't check modifiers on every mouse move event.
275 break;
277 case PP_INPUTEVENT_TYPE_KEYUP:
278 // PPAPI doesn't always set modifiers correctly on KEYUP events. See
279 // crbug.com/464791 for details.
280 break;
282 default: {
283 uint32_t modifiers = event.GetModifiers();
284 input_tracker_->ReleaseAllIfModifiersStuck(
285 (modifiers & PP_INPUTEVENT_MODIFIER_ALTKEY) != 0,
286 (modifiers & PP_INPUTEVENT_MODIFIER_CONTROLKEY) != 0,
287 (modifiers & PP_INPUTEVENT_MODIFIER_METAKEY) != 0,
288 (modifiers & PP_INPUTEVENT_MODIFIER_SHIFTKEY) != 0);
293 } // namespace remoting