Break out Chromoting plugin's cursor & mouse-lock handling.
[chromium-blink-merge.git] / remoting / client / plugin / pepper_input_handler.cc
blobfa80d467c135347565b68d2076d893ff143e72ac
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/var.h"
14 #include "remoting/proto/event.pb.h"
15 #include "ui/events/keycodes/dom4/keycode_converter.h"
17 namespace remoting {
19 namespace {
21 // Builds the Chromotocol lock states flags for the PPAPI |event|.
22 uint32_t MakeLockStates(const pp::InputEvent& event) {
23 uint32_t modifiers = event.GetModifiers();
24 uint32_t lock_states = 0;
26 if (modifiers & PP_INPUTEVENT_MODIFIER_CAPSLOCKKEY)
27 lock_states |= protocol::KeyEvent::LOCK_STATES_CAPSLOCK;
29 if (modifiers & PP_INPUTEVENT_MODIFIER_NUMLOCKKEY)
30 lock_states |= protocol::KeyEvent::LOCK_STATES_NUMLOCK;
32 return lock_states;
35 // Builds a protocol::KeyEvent from the supplied PPAPI event.
36 protocol::KeyEvent MakeKeyEvent(const pp::KeyboardInputEvent& pp_key_event) {
37 protocol::KeyEvent key_event;
38 std::string dom_code = pp_key_event.GetCode().AsString();
39 key_event.set_usb_keycode(
40 ui::KeycodeConverter::CodeToUsbKeycode(dom_code.c_str()));
41 key_event.set_pressed(pp_key_event.GetType() == PP_INPUTEVENT_TYPE_KEYDOWN);
42 key_event.set_lock_states(MakeLockStates(pp_key_event));
43 return key_event;
46 // Builds a protocol::MouseEvent from the supplied PPAPI event.
47 protocol::MouseEvent MakeMouseEvent(const pp::MouseInputEvent& pp_mouse_event,
48 bool set_deltas) {
49 protocol::MouseEvent mouse_event;
50 mouse_event.set_x(pp_mouse_event.GetPosition().x());
51 mouse_event.set_y(pp_mouse_event.GetPosition().y());
52 if (set_deltas) {
53 pp::Point delta = pp_mouse_event.GetMovement();
54 mouse_event.set_delta_x(delta.x());
55 mouse_event.set_delta_y(delta.y());
57 return mouse_event;
60 } // namespace
62 PepperInputHandler::PepperInputHandler()
63 : input_stub_(NULL),
64 has_focus_(false),
65 send_mouse_input_when_unfocused_(false),
66 send_mouse_move_deltas_(false),
67 wheel_delta_x_(0),
68 wheel_delta_y_(0),
69 wheel_ticks_x_(0),
70 wheel_ticks_y_(0) {
73 bool PepperInputHandler::HandleInputEvent(const pp::InputEvent& event) {
74 switch (event.GetType()) {
75 case PP_INPUTEVENT_TYPE_CONTEXTMENU: {
76 // We need to return true here or else we'll get a local (plugin) context
77 // menu instead of the mouseup event for the right click.
78 return true;
81 case PP_INPUTEVENT_TYPE_KEYDOWN:
82 case PP_INPUTEVENT_TYPE_KEYUP: {
83 if (!input_stub_)
84 return true;
85 pp::KeyboardInputEvent pp_key_event(event);
86 input_stub_->InjectKeyEvent(MakeKeyEvent(pp_key_event));
87 return true;
90 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
91 case PP_INPUTEVENT_TYPE_MOUSEUP: {
92 if (!has_focus_ && !send_mouse_input_when_unfocused_)
93 return false;
94 if (!input_stub_)
95 return true;
97 pp::MouseInputEvent pp_mouse_event(event);
98 protocol::MouseEvent mouse_event(
99 MakeMouseEvent(pp_mouse_event, send_mouse_move_deltas_));
100 switch (pp_mouse_event.GetButton()) {
101 case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
102 mouse_event.set_button(protocol::MouseEvent::BUTTON_LEFT);
103 break;
104 case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
105 mouse_event.set_button(protocol::MouseEvent::BUTTON_MIDDLE);
106 break;
107 case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
108 mouse_event.set_button(protocol::MouseEvent::BUTTON_RIGHT);
109 break;
110 case PP_INPUTEVENT_MOUSEBUTTON_NONE:
111 break;
113 if (mouse_event.has_button()) {
114 bool is_down = (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN);
115 mouse_event.set_button_down(is_down);
116 input_stub_->InjectMouseEvent(mouse_event);
119 return true;
122 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
123 case PP_INPUTEVENT_TYPE_MOUSEENTER:
124 case PP_INPUTEVENT_TYPE_MOUSELEAVE: {
125 if (!has_focus_ && !send_mouse_input_when_unfocused_)
126 return false;
127 if (!input_stub_)
128 return true;
130 pp::MouseInputEvent pp_mouse_event(event);
131 input_stub_->InjectMouseEvent(
132 MakeMouseEvent(pp_mouse_event, send_mouse_move_deltas_));
134 return true;
137 case PP_INPUTEVENT_TYPE_WHEEL: {
138 if (!has_focus_ && !send_mouse_input_when_unfocused_)
139 return false;
140 if (!input_stub_)
141 return true;
143 pp::WheelInputEvent pp_wheel_event(event);
145 // Ignore scroll-by-page events, for now.
146 if (pp_wheel_event.GetScrollByPage())
147 return true;
149 // Add this event to our accumulated sub-pixel deltas and clicks.
150 pp::FloatPoint delta = pp_wheel_event.GetDelta();
151 wheel_delta_x_ += delta.x();
152 wheel_delta_y_ += delta.y();
153 pp::FloatPoint ticks = pp_wheel_event.GetTicks();
154 wheel_ticks_x_ += ticks.x();
155 wheel_ticks_y_ += ticks.y();
157 // If there is at least a pixel's movement, emit an event. We don't
158 // ever expect to accumulate one tick's worth of scrolling without
159 // accumulating a pixel's worth at the same time, so this is safe.
160 int delta_x = static_cast<int>(wheel_delta_x_);
161 int delta_y = static_cast<int>(wheel_delta_y_);
162 if (delta_x != 0 || delta_y != 0) {
163 wheel_delta_x_ -= delta_x;
164 wheel_delta_y_ -= delta_y;
165 protocol::MouseEvent mouse_event;
166 mouse_event.set_wheel_delta_x(delta_x);
167 mouse_event.set_wheel_delta_y(delta_y);
169 // Always include the ticks in the event, even if insufficient pixel
170 // scrolling has accumulated for a single tick. This informs hosts
171 // that can't inject pixel-based scroll events that the client will
172 // accumulate them into tick-based scrolling, which gives a better
173 // overall experience than trying to do this host-side.
174 int ticks_x = static_cast<int>(wheel_ticks_x_);
175 int ticks_y = static_cast<int>(wheel_ticks_y_);
176 wheel_ticks_x_ -= ticks_x;
177 wheel_ticks_y_ -= ticks_y;
178 mouse_event.set_wheel_ticks_x(ticks_x);
179 mouse_event.set_wheel_ticks_y(ticks_y);
181 input_stub_->InjectMouseEvent(mouse_event);
183 return true;
186 case PP_INPUTEVENT_TYPE_CHAR:
187 // Consume but ignore character input events.
188 return true;
190 default: {
191 VLOG(0) << "Unhandled input event: " << event.GetType();
192 break;
196 return false;
199 void PepperInputHandler::DidChangeFocus(bool has_focus) {
200 has_focus_ = has_focus;
203 } // namespace remoting