Revert "Removing the avatar menu for the new multi profile style and show the avatar...
[chromium-blink-merge.git] / ppapi / cpp / input_event.cc
blobe4e000393008b9bd9f58a2594a051f49a9cdd9b9
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_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;
42 } // namespace
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>())
52 return;
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>())
70 return 0.0f;
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>())
76 return 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>())
88 return;
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,
99 uint32_t modifiers,
100 PP_InputEvent_MouseButton mouse_button,
101 const Point& mouse_position,
102 int32_t click_count,
103 const Point& mouse_movement) {
104 // Type check the input event before setting it.
105 if (!has_interface<PPB_MouseInputEvent_1_1>())
106 return;
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>())
120 return Point();
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>())
126 return 0;
127 return get_interface<PPB_MouseInputEvent_1_1>()->GetClickCount(pp_resource());
130 Point MouseInputEvent::GetMovement() const {
131 if (!has_interface<PPB_MouseInputEvent_1_1>())
132 return Point();
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>())
144 return;
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,
154 uint32_t modifiers,
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>())
160 return;
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>())
169 return FloatPoint();
170 return get_interface<PPB_WheelInputEvent_1_0>()->GetDelta(pp_resource());
173 FloatPoint WheelInputEvent::GetTicks() const {
174 if (!has_interface<PPB_WheelInputEvent_1_0>())
175 return FloatPoint();
176 return get_interface<PPB_WheelInputEvent_1_0>()->GetTicks(pp_resource());
179 bool WheelInputEvent::GetScrollByPage() const {
180 if (!has_interface<PPB_WheelInputEvent_1_0>())
181 return false;
182 return PP_ToBool(
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>())
194 return;
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,
205 uint32_t modifiers,
206 uint32_t key_code,
207 const Var& character_text) {
208 // Type check the input event before setting it.
209 if (!has_interface<PPB_KeyboardInputEvent_1_0>())
210 return;
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>())
218 return 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>())
224 return Var();
225 return Var(PASS_REF,
226 get_interface<PPB_KeyboardInputEvent_1_0>()->GetCharacterText(
227 pp_resource()));
230 // TouchInputEvent ------------------------------------------------------------
231 TouchInputEvent::TouchInputEvent() : InputEvent() {
234 TouchInputEvent::TouchInputEvent(const InputEvent& event) : InputEvent() {
235 if (!has_interface<PPB_TouchInputEvent_1_0>())
236 return;
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>())
251 return;
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>())
259 return;
260 get_interface<PPB_TouchInputEvent_1_0>()->AddTouchPoint(pp_resource(), list,
261 &point);
264 uint32_t TouchInputEvent::GetTouchCount(PP_TouchListType list) const {
265 if (!has_interface<PPB_TouchInputEvent_1_0>())
266 return 0;
267 return get_interface<PPB_TouchInputEvent_1_0>()->GetTouchCount(pp_resource(),
268 list);
271 TouchPoint TouchInputEvent::GetTouchById(PP_TouchListType list,
272 uint32_t id) const {
273 if (!has_interface<PPB_TouchInputEvent_1_0>())
274 return TouchPoint();
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>())
282 return TouchPoint();
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,
306 const Var& text,
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>())
311 return;
312 uint32_t dummy = 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>()) {
323 return Var(PASS_REF,
324 get_interface<PPB_IMEInputEvent_1_0>()->GetText(
325 pp_resource()));
327 return Var();
330 uint32_t IMEInputEvent::GetSegmentNumber() const {
331 if (has_interface<PPB_IMEInputEvent_1_0>()) {
332 return get_interface<PPB_IMEInputEvent_1_0>()->GetSegmentNumber(
333 pp_resource());
335 return 0;
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);
343 return 0;
346 int32_t IMEInputEvent::GetTargetSegment() const {
347 if (has_interface<PPB_IMEInputEvent_1_0>()) {
348 return get_interface<PPB_IMEInputEvent_1_0>()->GetTargetSegment(
349 pp_resource());
351 return 0;
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(),
357 start,
358 end);
362 } // namespace pp