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"
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_0
>() {
23 return PPB_KEYBOARD_INPUT_EVENT_INTERFACE_1_0
;
26 template <> const char* interface_name
<PPB_MouseInputEvent_1_1
>() {
27 return PPB_MOUSE_INPUT_EVENT_INTERFACE_1_1
;
30 template <> const char* interface_name
<PPB_WheelInputEvent_1_0
>() {
31 return PPB_WHEEL_INPUT_EVENT_INTERFACE_1_0
;
34 template <> const char* interface_name
<PPB_TouchInputEvent_1_0
>() {
35 return PPB_TOUCH_INPUT_EVENT_INTERFACE_1_0
;
38 template <> const char* interface_name
<PPB_IMEInputEvent_1_0
>() {
39 return PPB_IME_INPUT_EVENT_INTERFACE_1_0
;
44 // InputEvent ------------------------------------------------------------------
46 InputEvent::InputEvent() : Resource() {
49 InputEvent::InputEvent(PP_Resource input_event_resource
) : Resource() {
50 // Type check the input event before setting it.
51 if (!has_interface
<PPB_InputEvent_1_0
>())
53 if (get_interface
<PPB_InputEvent_1_0
>()->IsInputEvent(input_event_resource
)) {
54 Module::Get()->core()->AddRefResource(input_event_resource
);
55 PassRefFromConstructor(input_event_resource
);
59 InputEvent::~InputEvent() {
62 PP_InputEvent_Type
InputEvent::GetType() const {
63 if (!has_interface
<PPB_InputEvent_1_0
>())
64 return PP_INPUTEVENT_TYPE_UNDEFINED
;
65 return get_interface
<PPB_InputEvent_1_0
>()->GetType(pp_resource());
68 PP_TimeTicks
InputEvent::GetTimeStamp() const {
69 if (!has_interface
<PPB_InputEvent_1_0
>())
71 return get_interface
<PPB_InputEvent_1_0
>()->GetTimeStamp(pp_resource());
74 uint32_t InputEvent::GetModifiers() const {
75 if (!has_interface
<PPB_InputEvent_1_0
>())
77 return get_interface
<PPB_InputEvent_1_0
>()->GetModifiers(pp_resource());
80 // MouseInputEvent -------------------------------------------------------------
82 MouseInputEvent::MouseInputEvent() : InputEvent() {
85 MouseInputEvent::MouseInputEvent(const InputEvent
& event
) : InputEvent() {
86 // Type check the input event before setting it.
87 if (!has_interface
<PPB_MouseInputEvent_1_1
>())
89 if (get_interface
<PPB_MouseInputEvent_1_1
>()->IsMouseInputEvent(
90 event
.pp_resource())) {
91 Module::Get()->core()->AddRefResource(event
.pp_resource());
92 PassRefFromConstructor(event
.pp_resource());
96 MouseInputEvent::MouseInputEvent(const InstanceHandle
& instance
,
97 PP_InputEvent_Type type
,
98 PP_TimeTicks time_stamp
,
100 PP_InputEvent_MouseButton mouse_button
,
101 const Point
& mouse_position
,
103 const Point
& mouse_movement
) {
104 // Type check the input event before setting it.
105 if (!has_interface
<PPB_MouseInputEvent_1_1
>())
107 PassRefFromConstructor(get_interface
<PPB_MouseInputEvent_1_1
>()->Create(
108 instance
.pp_instance(), type
, time_stamp
, modifiers
, mouse_button
,
109 &mouse_position
.pp_point(), click_count
, &mouse_movement
.pp_point()));
112 PP_InputEvent_MouseButton
MouseInputEvent::GetButton() const {
113 if (!has_interface
<PPB_MouseInputEvent_1_1
>())
114 return PP_INPUTEVENT_MOUSEBUTTON_NONE
;
115 return get_interface
<PPB_MouseInputEvent_1_1
>()->GetButton(pp_resource());
118 Point
MouseInputEvent::GetPosition() const {
119 if (!has_interface
<PPB_MouseInputEvent_1_1
>())
121 return get_interface
<PPB_MouseInputEvent_1_1
>()->GetPosition(pp_resource());
124 int32_t MouseInputEvent::GetClickCount() const {
125 if (!has_interface
<PPB_MouseInputEvent_1_1
>())
127 return get_interface
<PPB_MouseInputEvent_1_1
>()->GetClickCount(pp_resource());
130 Point
MouseInputEvent::GetMovement() const {
131 if (!has_interface
<PPB_MouseInputEvent_1_1
>())
133 return get_interface
<PPB_MouseInputEvent_1_1
>()->GetMovement(pp_resource());
136 // WheelInputEvent -------------------------------------------------------------
138 WheelInputEvent::WheelInputEvent() : InputEvent() {
141 WheelInputEvent::WheelInputEvent(const InputEvent
& event
) : InputEvent() {
142 // Type check the input event before setting it.
143 if (!has_interface
<PPB_WheelInputEvent_1_0
>())
145 if (get_interface
<PPB_WheelInputEvent_1_0
>()->IsWheelInputEvent(
146 event
.pp_resource())) {
147 Module::Get()->core()->AddRefResource(event
.pp_resource());
148 PassRefFromConstructor(event
.pp_resource());
152 WheelInputEvent::WheelInputEvent(const InstanceHandle
& instance
,
153 PP_TimeTicks time_stamp
,
155 const FloatPoint
& wheel_delta
,
156 const FloatPoint
& wheel_ticks
,
157 bool scroll_by_page
) {
158 // Type check the input event before setting it.
159 if (!has_interface
<PPB_WheelInputEvent_1_0
>())
161 PassRefFromConstructor(get_interface
<PPB_WheelInputEvent_1_0
>()->Create(
162 instance
.pp_instance(), time_stamp
, modifiers
,
163 &wheel_delta
.pp_float_point(), &wheel_ticks
.pp_float_point(),
164 PP_FromBool(scroll_by_page
)));
167 FloatPoint
WheelInputEvent::GetDelta() const {
168 if (!has_interface
<PPB_WheelInputEvent_1_0
>())
170 return get_interface
<PPB_WheelInputEvent_1_0
>()->GetDelta(pp_resource());
173 FloatPoint
WheelInputEvent::GetTicks() const {
174 if (!has_interface
<PPB_WheelInputEvent_1_0
>())
176 return get_interface
<PPB_WheelInputEvent_1_0
>()->GetTicks(pp_resource());
179 bool WheelInputEvent::GetScrollByPage() const {
180 if (!has_interface
<PPB_WheelInputEvent_1_0
>())
183 get_interface
<PPB_WheelInputEvent_1_0
>()->GetScrollByPage(pp_resource()));
186 // KeyboardInputEvent ----------------------------------------------------------
188 KeyboardInputEvent::KeyboardInputEvent() : InputEvent() {
191 KeyboardInputEvent::KeyboardInputEvent(const InputEvent
& event
) : InputEvent() {
192 // Type check the input event before setting it.
193 if (!has_interface
<PPB_KeyboardInputEvent_1_0
>())
195 if (get_interface
<PPB_KeyboardInputEvent_1_0
>()->IsKeyboardInputEvent(
196 event
.pp_resource())) {
197 Module::Get()->core()->AddRefResource(event
.pp_resource());
198 PassRefFromConstructor(event
.pp_resource());
202 KeyboardInputEvent::KeyboardInputEvent(const InstanceHandle
& instance
,
203 PP_InputEvent_Type type
,
204 PP_TimeTicks time_stamp
,
207 const Var
& character_text
) {
208 // Type check the input event before setting it.
209 if (!has_interface
<PPB_KeyboardInputEvent_1_0
>())
211 PassRefFromConstructor(get_interface
<PPB_KeyboardInputEvent_1_0
>()->Create(
212 instance
.pp_instance(), type
, time_stamp
, modifiers
, key_code
,
213 character_text
.pp_var()));
216 uint32_t KeyboardInputEvent::GetKeyCode() const {
217 if (!has_interface
<PPB_KeyboardInputEvent_1_0
>())
219 return get_interface
<PPB_KeyboardInputEvent_1_0
>()->GetKeyCode(pp_resource());
222 Var
KeyboardInputEvent::GetCharacterText() const {
223 if (!has_interface
<PPB_KeyboardInputEvent_1_0
>())
226 get_interface
<PPB_KeyboardInputEvent_1_0
>()->GetCharacterText(
230 // TouchInputEvent ------------------------------------------------------------
231 TouchInputEvent::TouchInputEvent() : InputEvent() {
234 TouchInputEvent::TouchInputEvent(const InputEvent
& event
) : InputEvent() {
235 if (!has_interface
<PPB_TouchInputEvent_1_0
>())
237 // Type check the input event before setting it.
238 if (get_interface
<PPB_TouchInputEvent_1_0
>()->IsTouchInputEvent(
239 event
.pp_resource())) {
240 Module::Get()->core()->AddRefResource(event
.pp_resource());
241 PassRefFromConstructor(event
.pp_resource());
245 TouchInputEvent::TouchInputEvent(const InstanceHandle
& instance
,
246 PP_InputEvent_Type type
,
247 PP_TimeTicks time_stamp
,
248 uint32_t modifiers
) {
249 // Type check the input event before setting it.
250 if (!has_interface
<PPB_TouchInputEvent_1_0
>())
252 PassRefFromConstructor(get_interface
<PPB_TouchInputEvent_1_0
>()->Create(
253 instance
.pp_instance(), type
, time_stamp
, modifiers
));
256 void TouchInputEvent::AddTouchPoint(PP_TouchListType list
,
257 PP_TouchPoint point
) {
258 if (!has_interface
<PPB_TouchInputEvent_1_0
>())
260 get_interface
<PPB_TouchInputEvent_1_0
>()->AddTouchPoint(pp_resource(), list
,
264 uint32_t TouchInputEvent::GetTouchCount(PP_TouchListType list
) const {
265 if (!has_interface
<PPB_TouchInputEvent_1_0
>())
267 return get_interface
<PPB_TouchInputEvent_1_0
>()->GetTouchCount(pp_resource(),
271 TouchPoint
TouchInputEvent::GetTouchById(PP_TouchListType list
,
273 if (!has_interface
<PPB_TouchInputEvent_1_0
>())
275 return TouchPoint(get_interface
<PPB_TouchInputEvent_1_0
>()->
276 GetTouchById(pp_resource(), list
, id
));
279 TouchPoint
TouchInputEvent::GetTouchByIndex(PP_TouchListType list
,
280 uint32_t index
) const {
281 if (!has_interface
<PPB_TouchInputEvent_1_0
>())
283 return TouchPoint(get_interface
<PPB_TouchInputEvent_1_0
>()->
284 GetTouchByIndex(pp_resource(), list
, index
));
287 // IMEInputEvent -------------------------------------------------------
289 IMEInputEvent::IMEInputEvent() : InputEvent() {
292 IMEInputEvent::IMEInputEvent(const InputEvent
& event
) : InputEvent() {
293 if (has_interface
<PPB_IMEInputEvent_1_0
>()) {
294 if (get_interface
<PPB_IMEInputEvent_1_0
>()->IsIMEInputEvent(
295 event
.pp_resource())) {
296 Module::Get()->core()->AddRefResource(event
.pp_resource());
297 PassRefFromConstructor(event
.pp_resource());
302 IMEInputEvent::IMEInputEvent(
303 const InstanceHandle
& instance
,
304 PP_InputEvent_Type type
,
305 PP_TimeTicks time_stamp
,
307 const std::vector
<uint32_t>& segment_offsets
,
308 int32_t target_segment
,
309 const std::pair
<uint32_t, uint32_t>& selection
) : InputEvent() {
310 if (!has_interface
<PPB_IMEInputEvent_1_0
>())
313 PassRefFromConstructor(get_interface
<PPB_IMEInputEvent_1_0
>()->Create(
314 instance
.pp_instance(), type
, time_stamp
, text
.pp_var(),
315 segment_offsets
.empty() ? 0 : segment_offsets
.size() - 1,
316 segment_offsets
.empty() ? &dummy
: &segment_offsets
[0],
317 target_segment
, selection
.first
, selection
.second
));
321 Var
IMEInputEvent::GetText() const {
322 if (has_interface
<PPB_IMEInputEvent_1_0
>()) {
324 get_interface
<PPB_IMEInputEvent_1_0
>()->GetText(
330 uint32_t IMEInputEvent::GetSegmentNumber() const {
331 if (has_interface
<PPB_IMEInputEvent_1_0
>()) {
332 return get_interface
<PPB_IMEInputEvent_1_0
>()->GetSegmentNumber(
338 uint32_t IMEInputEvent::GetSegmentOffset(uint32_t index
) const {
339 if (has_interface
<PPB_IMEInputEvent_1_0
>()) {
340 return get_interface
<PPB_IMEInputEvent_1_0
>()->GetSegmentOffset(
341 pp_resource(), index
);
346 int32_t IMEInputEvent::GetTargetSegment() const {
347 if (has_interface
<PPB_IMEInputEvent_1_0
>()) {
348 return get_interface
<PPB_IMEInputEvent_1_0
>()->GetTargetSegment(
354 void IMEInputEvent::GetSelection(uint32_t* start
, uint32_t* end
) const {
355 if (has_interface
<PPB_IMEInputEvent_1_0
>()) {
356 get_interface
<PPB_IMEInputEvent_1_0
>()->GetSelection(pp_resource(),