Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / cpp / input_event.cc
blob90f41fa2b8fbaeb99a51990a221a27dac8d3ac25
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 "ppapi/cpp/input_event.h"
7 #include "ppapi/cpp/instance_handle.h"
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/module_impl.h"
10 #include "ppapi/cpp/point.h"
11 #include "ppapi/cpp/touch_point.h"
12 #include "ppapi/cpp/var.h"
14 namespace pp {
16 namespace {
18 template <> const char* interface_name<PPB_InputEvent_1_0>() {
19 return PPB_INPUT_EVENT_INTERFACE_1_0;
22 template <> const char* interface_name<PPB_KeyboardInputEvent_1_2>() {
23 return PPB_KEYBOARD_INPUT_EVENT_INTERFACE_1_2;
26 template <> const char* interface_name<PPB_KeyboardInputEvent_1_0>() {
27 return PPB_KEYBOARD_INPUT_EVENT_INTERFACE_1_0;
30 template <> const char* interface_name<PPB_MouseInputEvent_1_1>() {
31 return PPB_MOUSE_INPUT_EVENT_INTERFACE_1_1;
34 template <> const char* interface_name<PPB_WheelInputEvent_1_0>() {
35 return PPB_WHEEL_INPUT_EVENT_INTERFACE_1_0;
38 template <> const char* interface_name<PPB_TouchInputEvent_1_0>() {
39 return PPB_TOUCH_INPUT_EVENT_INTERFACE_1_0;
42 template <> const char* interface_name<PPB_IMEInputEvent_1_0>() {
43 return PPB_IME_INPUT_EVENT_INTERFACE_1_0;
46 } // namespace
48 // InputEvent ------------------------------------------------------------------
50 InputEvent::InputEvent() : Resource() {
53 InputEvent::InputEvent(PP_Resource input_event_resource) : Resource() {
54 // Type check the input event before setting it.
55 if (!has_interface<PPB_InputEvent_1_0>())
56 return;
57 if (get_interface<PPB_InputEvent_1_0>()->IsInputEvent(input_event_resource)) {
58 Module::Get()->core()->AddRefResource(input_event_resource);
59 PassRefFromConstructor(input_event_resource);
63 InputEvent::~InputEvent() {
66 PP_InputEvent_Type InputEvent::GetType() const {
67 if (!has_interface<PPB_InputEvent_1_0>())
68 return PP_INPUTEVENT_TYPE_UNDEFINED;
69 return get_interface<PPB_InputEvent_1_0>()->GetType(pp_resource());
72 PP_TimeTicks InputEvent::GetTimeStamp() const {
73 if (!has_interface<PPB_InputEvent_1_0>())
74 return 0.0f;
75 return get_interface<PPB_InputEvent_1_0>()->GetTimeStamp(pp_resource());
78 uint32_t InputEvent::GetModifiers() const {
79 if (!has_interface<PPB_InputEvent_1_0>())
80 return 0;
81 return get_interface<PPB_InputEvent_1_0>()->GetModifiers(pp_resource());
84 // MouseInputEvent -------------------------------------------------------------
86 MouseInputEvent::MouseInputEvent() : InputEvent() {
89 MouseInputEvent::MouseInputEvent(const InputEvent& event) : InputEvent() {
90 // Type check the input event before setting it.
91 if (!has_interface<PPB_MouseInputEvent_1_1>())
92 return;
93 if (get_interface<PPB_MouseInputEvent_1_1>()->IsMouseInputEvent(
94 event.pp_resource())) {
95 Module::Get()->core()->AddRefResource(event.pp_resource());
96 PassRefFromConstructor(event.pp_resource());
100 MouseInputEvent::MouseInputEvent(const InstanceHandle& instance,
101 PP_InputEvent_Type type,
102 PP_TimeTicks time_stamp,
103 uint32_t modifiers,
104 PP_InputEvent_MouseButton mouse_button,
105 const Point& mouse_position,
106 int32_t click_count,
107 const Point& mouse_movement) {
108 // Type check the input event before setting it.
109 if (!has_interface<PPB_MouseInputEvent_1_1>())
110 return;
111 PassRefFromConstructor(get_interface<PPB_MouseInputEvent_1_1>()->Create(
112 instance.pp_instance(), type, time_stamp, modifiers, mouse_button,
113 &mouse_position.pp_point(), click_count, &mouse_movement.pp_point()));
116 PP_InputEvent_MouseButton MouseInputEvent::GetButton() const {
117 if (!has_interface<PPB_MouseInputEvent_1_1>())
118 return PP_INPUTEVENT_MOUSEBUTTON_NONE;
119 return get_interface<PPB_MouseInputEvent_1_1>()->GetButton(pp_resource());
122 Point MouseInputEvent::GetPosition() const {
123 if (!has_interface<PPB_MouseInputEvent_1_1>())
124 return Point();
125 return get_interface<PPB_MouseInputEvent_1_1>()->GetPosition(pp_resource());
128 int32_t MouseInputEvent::GetClickCount() const {
129 if (!has_interface<PPB_MouseInputEvent_1_1>())
130 return 0;
131 return get_interface<PPB_MouseInputEvent_1_1>()->GetClickCount(pp_resource());
134 Point MouseInputEvent::GetMovement() const {
135 if (!has_interface<PPB_MouseInputEvent_1_1>())
136 return Point();
137 return get_interface<PPB_MouseInputEvent_1_1>()->GetMovement(pp_resource());
140 // WheelInputEvent -------------------------------------------------------------
142 WheelInputEvent::WheelInputEvent() : InputEvent() {
145 WheelInputEvent::WheelInputEvent(const InputEvent& event) : InputEvent() {
146 // Type check the input event before setting it.
147 if (!has_interface<PPB_WheelInputEvent_1_0>())
148 return;
149 if (get_interface<PPB_WheelInputEvent_1_0>()->IsWheelInputEvent(
150 event.pp_resource())) {
151 Module::Get()->core()->AddRefResource(event.pp_resource());
152 PassRefFromConstructor(event.pp_resource());
156 WheelInputEvent::WheelInputEvent(const InstanceHandle& instance,
157 PP_TimeTicks time_stamp,
158 uint32_t modifiers,
159 const FloatPoint& wheel_delta,
160 const FloatPoint& wheel_ticks,
161 bool scroll_by_page) {
162 // Type check the input event before setting it.
163 if (!has_interface<PPB_WheelInputEvent_1_0>())
164 return;
165 PassRefFromConstructor(get_interface<PPB_WheelInputEvent_1_0>()->Create(
166 instance.pp_instance(), time_stamp, modifiers,
167 &wheel_delta.pp_float_point(), &wheel_ticks.pp_float_point(),
168 PP_FromBool(scroll_by_page)));
171 FloatPoint WheelInputEvent::GetDelta() const {
172 if (!has_interface<PPB_WheelInputEvent_1_0>())
173 return FloatPoint();
174 return get_interface<PPB_WheelInputEvent_1_0>()->GetDelta(pp_resource());
177 FloatPoint WheelInputEvent::GetTicks() const {
178 if (!has_interface<PPB_WheelInputEvent_1_0>())
179 return FloatPoint();
180 return get_interface<PPB_WheelInputEvent_1_0>()->GetTicks(pp_resource());
183 bool WheelInputEvent::GetScrollByPage() const {
184 if (!has_interface<PPB_WheelInputEvent_1_0>())
185 return false;
186 return PP_ToBool(
187 get_interface<PPB_WheelInputEvent_1_0>()->GetScrollByPage(pp_resource()));
190 // KeyboardInputEvent ----------------------------------------------------------
192 KeyboardInputEvent::KeyboardInputEvent() : InputEvent() {
195 KeyboardInputEvent::KeyboardInputEvent(const InputEvent& event) : InputEvent() {
196 PP_Bool is_keyboard_event = PP_FALSE;
198 if (has_interface<PPB_KeyboardInputEvent_1_2>()) {
199 is_keyboard_event =
200 get_interface<PPB_KeyboardInputEvent_1_2>()->IsKeyboardInputEvent(
201 event.pp_resource());
202 } else if (has_interface<PPB_KeyboardInputEvent_1_0>()) {
203 is_keyboard_event =
204 get_interface<PPB_KeyboardInputEvent_1_0>()->IsKeyboardInputEvent(
205 event.pp_resource());
208 if (PP_ToBool(is_keyboard_event)) {
209 Module::Get()->core()->AddRefResource(event.pp_resource());
210 PassRefFromConstructor(event.pp_resource());
214 KeyboardInputEvent::KeyboardInputEvent(const InstanceHandle& instance,
215 PP_InputEvent_Type type,
216 PP_TimeTicks time_stamp,
217 uint32_t modifiers,
218 uint32_t key_code,
219 const Var& character_text) {
220 if (has_interface<PPB_KeyboardInputEvent_1_2>()) {
221 PassRefFromConstructor(get_interface<PPB_KeyboardInputEvent_1_2>()->Create(
222 instance.pp_instance(), type, time_stamp, modifiers, key_code,
223 character_text.pp_var(), Var().pp_var()));
224 } else if (has_interface<PPB_KeyboardInputEvent_1_0>()) {
225 PassRefFromConstructor(get_interface<PPB_KeyboardInputEvent_1_0>()->Create(
226 instance.pp_instance(), type, time_stamp, modifiers, key_code,
227 character_text.pp_var()));
231 KeyboardInputEvent::KeyboardInputEvent(const InstanceHandle& instance,
232 PP_InputEvent_Type type,
233 PP_TimeTicks time_stamp,
234 uint32_t modifiers,
235 uint32_t key_code,
236 const Var& character_text,
237 const Var& code) {
238 if (has_interface<PPB_KeyboardInputEvent_1_2>()) {
239 PassRefFromConstructor(get_interface<PPB_KeyboardInputEvent_1_2>()->Create(
240 instance.pp_instance(), type, time_stamp, modifiers, key_code,
241 character_text.pp_var(), code.pp_var()));
242 } else if (has_interface<PPB_KeyboardInputEvent_1_0>()) {
243 PassRefFromConstructor(get_interface<PPB_KeyboardInputEvent_1_0>()->Create(
244 instance.pp_instance(), type, time_stamp, modifiers, key_code,
245 character_text.pp_var()));
249 uint32_t KeyboardInputEvent::GetKeyCode() const {
250 if (has_interface<PPB_KeyboardInputEvent_1_2>()) {
251 return get_interface<PPB_KeyboardInputEvent_1_2>()->GetKeyCode(
252 pp_resource());
253 } else if (has_interface<PPB_KeyboardInputEvent_1_0>()) {
254 return get_interface<PPB_KeyboardInputEvent_1_0>()->GetKeyCode(
255 pp_resource());
257 return 0;
260 Var KeyboardInputEvent::GetCharacterText() const {
261 if (has_interface<PPB_KeyboardInputEvent_1_2>()) {
262 return Var(PASS_REF,
263 get_interface<PPB_KeyboardInputEvent_1_2>()->GetCharacterText(
264 pp_resource()));
265 } else if (has_interface<PPB_KeyboardInputEvent_1_0>()) {
266 return Var(PASS_REF,
267 get_interface<PPB_KeyboardInputEvent_1_0>()->GetCharacterText(
268 pp_resource()));
270 return Var();
273 Var KeyboardInputEvent::GetCode() const {
274 if (has_interface<PPB_KeyboardInputEvent_1_2>()) {
275 return Var(PASS_REF,
276 get_interface<PPB_KeyboardInputEvent_1_2>()->GetCode(
277 pp_resource()));
279 return Var();
282 // TouchInputEvent ------------------------------------------------------------
283 TouchInputEvent::TouchInputEvent() : InputEvent() {
286 TouchInputEvent::TouchInputEvent(const InputEvent& event) : InputEvent() {
287 if (!has_interface<PPB_TouchInputEvent_1_0>())
288 return;
289 // Type check the input event before setting it.
290 if (get_interface<PPB_TouchInputEvent_1_0>()->IsTouchInputEvent(
291 event.pp_resource())) {
292 Module::Get()->core()->AddRefResource(event.pp_resource());
293 PassRefFromConstructor(event.pp_resource());
297 TouchInputEvent::TouchInputEvent(const InstanceHandle& instance,
298 PP_InputEvent_Type type,
299 PP_TimeTicks time_stamp,
300 uint32_t modifiers) {
301 // Type check the input event before setting it.
302 if (!has_interface<PPB_TouchInputEvent_1_0>())
303 return;
304 PassRefFromConstructor(get_interface<PPB_TouchInputEvent_1_0>()->Create(
305 instance.pp_instance(), type, time_stamp, modifiers));
308 void TouchInputEvent::AddTouchPoint(PP_TouchListType list,
309 PP_TouchPoint point) {
310 if (!has_interface<PPB_TouchInputEvent_1_0>())
311 return;
312 get_interface<PPB_TouchInputEvent_1_0>()->AddTouchPoint(pp_resource(), list,
313 &point);
316 uint32_t TouchInputEvent::GetTouchCount(PP_TouchListType list) const {
317 if (!has_interface<PPB_TouchInputEvent_1_0>())
318 return 0;
319 return get_interface<PPB_TouchInputEvent_1_0>()->GetTouchCount(pp_resource(),
320 list);
323 TouchPoint TouchInputEvent::GetTouchById(PP_TouchListType list,
324 uint32_t id) const {
325 if (!has_interface<PPB_TouchInputEvent_1_0>())
326 return TouchPoint();
327 return TouchPoint(get_interface<PPB_TouchInputEvent_1_0>()->
328 GetTouchById(pp_resource(), list, id));
331 TouchPoint TouchInputEvent::GetTouchByIndex(PP_TouchListType list,
332 uint32_t index) const {
333 if (!has_interface<PPB_TouchInputEvent_1_0>())
334 return TouchPoint();
335 return TouchPoint(get_interface<PPB_TouchInputEvent_1_0>()->
336 GetTouchByIndex(pp_resource(), list, index));
339 // IMEInputEvent -------------------------------------------------------
341 IMEInputEvent::IMEInputEvent() : InputEvent() {
344 IMEInputEvent::IMEInputEvent(const InputEvent& event) : InputEvent() {
345 if (has_interface<PPB_IMEInputEvent_1_0>()) {
346 if (get_interface<PPB_IMEInputEvent_1_0>()->IsIMEInputEvent(
347 event.pp_resource())) {
348 Module::Get()->core()->AddRefResource(event.pp_resource());
349 PassRefFromConstructor(event.pp_resource());
354 IMEInputEvent::IMEInputEvent(
355 const InstanceHandle& instance,
356 PP_InputEvent_Type type,
357 PP_TimeTicks time_stamp,
358 const Var& text,
359 const std::vector<uint32_t>& segment_offsets,
360 int32_t target_segment,
361 const std::pair<uint32_t, uint32_t>& selection) : InputEvent() {
362 if (!has_interface<PPB_IMEInputEvent_1_0>())
363 return;
364 uint32_t dummy = 0;
365 PassRefFromConstructor(get_interface<PPB_IMEInputEvent_1_0>()->Create(
366 instance.pp_instance(), type, time_stamp, text.pp_var(),
367 segment_offsets.empty() ? 0u :
368 static_cast<uint32_t>(segment_offsets.size() - 1),
369 segment_offsets.empty() ? &dummy : &segment_offsets[0],
370 target_segment, selection.first, selection.second));
374 Var IMEInputEvent::GetText() const {
375 if (has_interface<PPB_IMEInputEvent_1_0>()) {
376 return Var(PASS_REF,
377 get_interface<PPB_IMEInputEvent_1_0>()->GetText(
378 pp_resource()));
380 return Var();
383 uint32_t IMEInputEvent::GetSegmentNumber() const {
384 if (has_interface<PPB_IMEInputEvent_1_0>()) {
385 return get_interface<PPB_IMEInputEvent_1_0>()->GetSegmentNumber(
386 pp_resource());
388 return 0;
391 uint32_t IMEInputEvent::GetSegmentOffset(uint32_t index) const {
392 if (has_interface<PPB_IMEInputEvent_1_0>()) {
393 return get_interface<PPB_IMEInputEvent_1_0>()->GetSegmentOffset(
394 pp_resource(), index);
396 return 0;
399 int32_t IMEInputEvent::GetTargetSegment() const {
400 if (has_interface<PPB_IMEInputEvent_1_0>()) {
401 return get_interface<PPB_IMEInputEvent_1_0>()->GetTargetSegment(
402 pp_resource());
404 return 0;
407 void IMEInputEvent::GetSelection(uint32_t* start, uint32_t* end) const {
408 if (has_interface<PPB_IMEInputEvent_1_0>()) {
409 get_interface<PPB_IMEInputEvent_1_0>()->GetSelection(pp_resource(),
410 start,
411 end);
415 } // namespace pp