Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ppapi / thunk / ppb_input_event_thunk.cc
blob4efbc1886ac3c030a5f037f1b5e2d4520d2d80ef
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/c/pp_errors.h"
6 #include "ppapi/thunk/enter.h"
7 #include "ppapi/thunk/ppb_input_event_api.h"
8 #include "ppapi/thunk/ppb_instance_api.h"
9 #include "ppapi/thunk/resource_creation_api.h"
10 #include "ppapi/thunk/thunk.h"
12 namespace ppapi {
13 namespace thunk {
15 namespace {
17 typedef EnterResource<PPB_InputEvent_API> EnterInputEvent;
19 // InputEvent ------------------------------------------------------------------
21 int32_t RequestInputEvents(PP_Instance instance, uint32_t event_classes) {
22 EnterInstance enter(instance);
23 if (enter.failed())
24 return enter.retval();
25 return enter.functions()->RequestInputEvents(instance, event_classes);
28 int32_t RequestFilteringInputEvents(PP_Instance instance,
29 uint32_t event_classes) {
30 EnterInstance enter(instance);
31 if (enter.failed())
32 return enter.retval();
33 return enter.functions()->RequestFilteringInputEvents(instance,
34 event_classes);
37 void ClearInputEventRequest(PP_Instance instance,
38 uint32_t event_classes) {
39 EnterInstance enter(instance);
40 if (enter.succeeded())
41 enter.functions()->ClearInputEventRequest(instance, event_classes);
44 PP_Bool IsInputEvent(PP_Resource resource) {
45 EnterInputEvent enter(resource, false);
46 return enter.succeeded() ? PP_TRUE : PP_FALSE;
49 PP_InputEvent_Type GetType(PP_Resource event) {
50 EnterInputEvent enter(event, true);
51 if (enter.failed())
52 return PP_INPUTEVENT_TYPE_UNDEFINED;
53 return enter.object()->GetType();
56 PP_TimeTicks GetTimeStamp(PP_Resource event) {
57 EnterInputEvent enter(event, true);
58 if (enter.failed())
59 return 0.0;
60 return enter.object()->GetTimeStamp();
63 uint32_t GetModifiers(PP_Resource event) {
64 EnterInputEvent enter(event, true);
65 if (enter.failed())
66 return 0;
67 return enter.object()->GetModifiers();
70 const PPB_InputEvent g_ppb_input_event_thunk = {
71 &RequestInputEvents,
72 &RequestFilteringInputEvents,
73 &ClearInputEventRequest,
74 &IsInputEvent,
75 &GetType,
76 &GetTimeStamp,
77 &GetModifiers
80 // Mouse -----------------------------------------------------------------------
82 PP_Resource CreateMouseInputEvent1_0(PP_Instance instance,
83 PP_InputEvent_Type type,
84 PP_TimeTicks time_stamp,
85 uint32_t modifiers,
86 PP_InputEvent_MouseButton mouse_button,
87 const PP_Point* mouse_position,
88 int32_t click_count) {
89 EnterResourceCreation enter(instance);
90 if (enter.failed())
91 return 0;
93 PP_Point mouse_movement = PP_MakePoint(0, 0);
94 return enter.functions()->CreateMouseInputEvent(instance, type, time_stamp,
95 modifiers, mouse_button,
96 mouse_position, click_count,
97 &mouse_movement);
100 PP_Resource CreateMouseInputEvent1_1(PP_Instance instance,
101 PP_InputEvent_Type type,
102 PP_TimeTicks time_stamp,
103 uint32_t modifiers,
104 PP_InputEvent_MouseButton mouse_button,
105 const PP_Point* mouse_position,
106 int32_t click_count,
107 const PP_Point* mouse_movement) {
108 EnterResourceCreation enter(instance);
109 if (enter.failed())
110 return 0;
111 return enter.functions()->CreateMouseInputEvent(instance, type, time_stamp,
112 modifiers, mouse_button,
113 mouse_position, click_count,
114 mouse_movement);
117 PP_Bool IsMouseInputEvent(PP_Resource resource) {
118 if (!IsInputEvent(resource))
119 return PP_FALSE; // Prevent warning log in GetType.
120 PP_InputEvent_Type type = GetType(resource);
121 return PP_FromBool(type == PP_INPUTEVENT_TYPE_MOUSEDOWN ||
122 type == PP_INPUTEVENT_TYPE_MOUSEUP ||
123 type == PP_INPUTEVENT_TYPE_MOUSEMOVE ||
124 type == PP_INPUTEVENT_TYPE_MOUSEENTER ||
125 type == PP_INPUTEVENT_TYPE_MOUSELEAVE ||
126 type == PP_INPUTEVENT_TYPE_CONTEXTMENU);
129 PP_InputEvent_MouseButton GetMouseButton(PP_Resource mouse_event) {
130 EnterInputEvent enter(mouse_event, true);
131 if (enter.failed())
132 return PP_INPUTEVENT_MOUSEBUTTON_NONE;
133 return enter.object()->GetMouseButton();
136 PP_Point GetMousePosition(PP_Resource mouse_event) {
137 EnterInputEvent enter(mouse_event, true);
138 if (enter.failed())
139 return PP_MakePoint(0, 0);
140 return enter.object()->GetMousePosition();
143 int32_t GetMouseClickCount(PP_Resource mouse_event) {
144 EnterInputEvent enter(mouse_event, true);
145 if (enter.failed())
146 return 0;
147 return enter.object()->GetMouseClickCount();
150 PP_Point GetMouseMovement(PP_Resource mouse_event) {
151 EnterInputEvent enter(mouse_event, true);
152 if (enter.failed())
153 return PP_MakePoint(0, 0);
154 return enter.object()->GetMouseMovement();
157 const PPB_MouseInputEvent_1_0 g_ppb_mouse_input_event_1_0_thunk = {
158 &CreateMouseInputEvent1_0,
159 &IsMouseInputEvent,
160 &GetMouseButton,
161 &GetMousePosition,
162 &GetMouseClickCount
165 const PPB_MouseInputEvent g_ppb_mouse_input_event_1_1_thunk = {
166 &CreateMouseInputEvent1_1,
167 &IsMouseInputEvent,
168 &GetMouseButton,
169 &GetMousePosition,
170 &GetMouseClickCount,
171 &GetMouseMovement
174 // Wheel -----------------------------------------------------------------------
176 PP_Resource CreateWheelInputEvent(PP_Instance instance,
177 PP_TimeTicks time_stamp,
178 uint32_t modifiers,
179 const PP_FloatPoint* wheel_delta,
180 const PP_FloatPoint* wheel_ticks,
181 PP_Bool scroll_by_page) {
182 EnterResourceCreation enter(instance);
183 if (enter.failed())
184 return 0;
185 return enter.functions()->CreateWheelInputEvent(instance, time_stamp,
186 modifiers, wheel_delta,
187 wheel_ticks, scroll_by_page);
190 PP_Bool IsWheelInputEvent(PP_Resource resource) {
191 if (!IsInputEvent(resource))
192 return PP_FALSE; // Prevent warning log in GetType.
193 PP_InputEvent_Type type = GetType(resource);
194 return PP_FromBool(type == PP_INPUTEVENT_TYPE_WHEEL);
197 PP_FloatPoint GetWheelDelta(PP_Resource wheel_event) {
198 EnterInputEvent enter(wheel_event, true);
199 if (enter.failed())
200 return PP_MakeFloatPoint(0.0f, 0.0f);
201 return enter.object()->GetWheelDelta();
204 PP_FloatPoint GetWheelTicks(PP_Resource wheel_event) {
205 EnterInputEvent enter(wheel_event, true);
206 if (enter.failed())
207 return PP_MakeFloatPoint(0.0f, 0.0f);
208 return enter.object()->GetWheelTicks();
211 PP_Bool GetWheelScrollByPage(PP_Resource wheel_event) {
212 EnterInputEvent enter(wheel_event, true);
213 if (enter.failed())
214 return PP_FALSE;
215 return enter.object()->GetWheelScrollByPage();
218 const PPB_WheelInputEvent g_ppb_wheel_input_event_thunk = {
219 &CreateWheelInputEvent,
220 &IsWheelInputEvent,
221 &GetWheelDelta,
222 &GetWheelTicks,
223 &GetWheelScrollByPage
226 // Keyboard --------------------------------------------------------------------
228 PP_Resource CreateKeyboardInputEvent(PP_Instance instance,
229 PP_InputEvent_Type type,
230 PP_TimeTicks time_stamp,
231 uint32_t modifiers,
232 uint32_t key_code,
233 struct PP_Var character_text) {
234 EnterResourceCreation enter(instance);
235 if (enter.failed())
236 return 0;
237 return enter.functions()->CreateKeyboardInputEvent(instance, type, time_stamp,
238 modifiers, key_code,
239 character_text);
242 PP_Bool IsKeyboardInputEvent(PP_Resource resource) {
243 if (!IsInputEvent(resource))
244 return PP_FALSE; // Prevent warning log in GetType.
245 PP_InputEvent_Type type = GetType(resource);
246 return PP_FromBool(type == PP_INPUTEVENT_TYPE_KEYDOWN ||
247 type == PP_INPUTEVENT_TYPE_KEYUP ||
248 type == PP_INPUTEVENT_TYPE_RAWKEYDOWN ||
249 type == PP_INPUTEVENT_TYPE_CHAR);
252 uint32_t GetKeyCode(PP_Resource key_event) {
253 EnterInputEvent enter(key_event, true);
254 if (enter.failed())
255 return 0;
256 return enter.object()->GetKeyCode();
259 PP_Var GetCharacterText(PP_Resource character_event) {
260 EnterInputEvent enter(character_event, true);
261 if (enter.failed())
262 return PP_MakeUndefined();
263 return enter.object()->GetCharacterText();
266 const PPB_KeyboardInputEvent g_ppb_keyboard_input_event_thunk = {
267 &CreateKeyboardInputEvent,
268 &IsKeyboardInputEvent,
269 &GetKeyCode,
270 &GetCharacterText
273 // _Dev interface.
275 PP_Bool SetUsbKeyCode(PP_Resource key_event, uint32_t usb_key_code) {
276 EnterInputEvent enter(key_event, true);
277 if (enter.failed())
278 return PP_FALSE;
279 return enter.object()->SetUsbKeyCode(usb_key_code);
282 uint32_t GetUsbKeyCode(PP_Resource key_event) {
283 EnterInputEvent enter(key_event, true);
284 if (enter.failed())
285 return 0;
286 return enter.object()->GetUsbKeyCode();
289 const PPB_KeyboardInputEvent_Dev g_ppb_keyboard_input_event_dev_thunk = {
290 &SetUsbKeyCode,
291 &GetUsbKeyCode,
294 // Composition -----------------------------------------------------------------
296 PP_Resource CreateIMEInputEvent(PP_Instance instance,
297 PP_InputEvent_Type type,
298 PP_TimeTicks time_stamp,
299 PP_Var text,
300 uint32_t segment_number,
301 const uint32_t segment_offsets[],
302 int32_t target_segment,
303 uint32_t selection_start,
304 uint32_t selection_end) {
305 EnterResourceCreation enter(instance);
306 if (enter.failed())
307 return 0;
308 return enter.functions()->CreateIMEInputEvent(instance, type, time_stamp,
309 text, segment_number,
310 segment_offsets,
311 target_segment,
312 selection_start,
313 selection_end);
316 PP_Bool IsIMEInputEvent(PP_Resource resource) {
317 if (!IsInputEvent(resource))
318 return PP_FALSE; // Prevent warning log in GetType.
319 PP_InputEvent_Type type = GetType(resource);
320 return PP_FromBool(type == PP_INPUTEVENT_TYPE_IME_COMPOSITION_START ||
321 type == PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE ||
322 type == PP_INPUTEVENT_TYPE_IME_COMPOSITION_END ||
323 type == PP_INPUTEVENT_TYPE_IME_TEXT);
326 PP_Var GetIMEText(PP_Resource ime_event) {
327 return GetCharacterText(ime_event);
330 uint32_t GetIMESegmentNumber(PP_Resource ime_event) {
331 EnterInputEvent enter(ime_event, true);
332 if (enter.failed())
333 return 0;
334 return enter.object()->GetIMESegmentNumber();
337 uint32_t GetIMESegmentOffset(PP_Resource ime_event, uint32_t index) {
338 EnterInputEvent enter(ime_event, true);
339 if (enter.failed())
340 return 0;
341 return enter.object()->GetIMESegmentOffset(index);
344 int32_t GetIMETargetSegment(PP_Resource ime_event) {
345 EnterInputEvent enter(ime_event, true);
346 if (enter.failed())
347 return -1;
348 return enter.object()->GetIMETargetSegment();
351 void GetIMESelection(PP_Resource ime_event, uint32_t* start, uint32_t* end) {
352 EnterInputEvent enter(ime_event, true);
353 if (enter.failed()) {
354 if (start)
355 *start = 0;
356 if (end)
357 *end = 0;
358 return;
360 enter.object()->GetIMESelection(start, end);
363 const PPB_IMEInputEvent_Dev_0_1 g_ppb_ime_input_event_0_1_thunk = {
364 &IsIMEInputEvent,
365 &GetIMEText,
366 &GetIMESegmentNumber,
367 &GetIMESegmentOffset,
368 &GetIMETargetSegment,
369 &GetIMESelection
372 const PPB_IMEInputEvent_Dev_0_2 g_ppb_ime_input_event_0_2_thunk = {
373 &CreateIMEInputEvent,
374 &IsIMEInputEvent,
375 &GetIMEText,
376 &GetIMESegmentNumber,
377 &GetIMESegmentOffset,
378 &GetIMETargetSegment,
379 &GetIMESelection
382 // Touch -----------------------------------------------------------------------
384 PP_Resource CreateTouchInputEvent(PP_Instance instance,
385 PP_InputEvent_Type type,
386 PP_TimeTicks time_stamp,
387 uint32_t modifiers) {
388 EnterResourceCreation enter(instance);
389 if (enter.failed())
390 return 0;
391 return enter.functions()->CreateTouchInputEvent(instance, type, time_stamp,
392 modifiers);
395 void AddTouchPoint(PP_Resource touch_event,
396 PP_TouchListType list,
397 const PP_TouchPoint* point) {
398 EnterInputEvent enter(touch_event, true);
399 if (enter.failed())
400 return;
401 return enter.object()->AddTouchPoint(list, *point);
404 PP_Bool IsTouchInputEvent(PP_Resource resource) {
405 if (!IsInputEvent(resource))
406 return PP_FALSE; // Prevent warning log in GetType.
407 PP_InputEvent_Type type = GetType(resource);
408 return PP_FromBool(type == PP_INPUTEVENT_TYPE_TOUCHSTART ||
409 type == PP_INPUTEVENT_TYPE_TOUCHMOVE ||
410 type == PP_INPUTEVENT_TYPE_TOUCHEND ||
411 type == PP_INPUTEVENT_TYPE_TOUCHCANCEL);
414 uint32_t GetTouchCount(PP_Resource touch_event, PP_TouchListType list) {
415 EnterInputEvent enter(touch_event, true);
416 if (enter.failed())
417 return 0;
418 return enter.object()->GetTouchCount(list);
421 struct PP_TouchPoint GetTouchByIndex(PP_Resource touch_event,
422 PP_TouchListType list,
423 uint32_t index) {
424 EnterInputEvent enter(touch_event, true);
425 if (enter.failed())
426 return PP_MakeTouchPoint();
427 return enter.object()->GetTouchByIndex(list, index);
430 struct PP_TouchPoint GetTouchById(PP_Resource touch_event,
431 PP_TouchListType list,
432 uint32_t id) {
433 EnterInputEvent enter(touch_event, true);
434 if (enter.failed())
435 return PP_MakeTouchPoint();
436 return enter.object()->GetTouchById(list, id);
439 const PPB_TouchInputEvent_1_0 g_ppb_touch_input_event_thunk = {
440 &CreateTouchInputEvent,
441 &AddTouchPoint,
442 &IsTouchInputEvent,
443 &GetTouchCount,
444 &GetTouchByIndex,
445 &GetTouchById
448 } // namespace
450 const PPB_InputEvent_1_0* GetPPB_InputEvent_1_0_Thunk() {
451 return &g_ppb_input_event_thunk;
454 const PPB_MouseInputEvent_1_0* GetPPB_MouseInputEvent_1_0_Thunk() {
455 return &g_ppb_mouse_input_event_1_0_thunk;
458 const PPB_MouseInputEvent_1_1* GetPPB_MouseInputEvent_1_1_Thunk() {
459 return &g_ppb_mouse_input_event_1_1_thunk;
462 const PPB_KeyboardInputEvent_1_0* GetPPB_KeyboardInputEvent_1_0_Thunk() {
463 return &g_ppb_keyboard_input_event_thunk;
466 const PPB_KeyboardInputEvent_Dev_0_1*
467 GetPPB_KeyboardInputEvent_Dev_0_1_Thunk() {
468 return &g_ppb_keyboard_input_event_dev_thunk;
471 const PPB_WheelInputEvent_1_0* GetPPB_WheelInputEvent_1_0_Thunk() {
472 return &g_ppb_wheel_input_event_thunk;
475 const PPB_IMEInputEvent_Dev_0_1* GetPPB_IMEInputEvent_Dev_0_1_Thunk() {
476 return &g_ppb_ime_input_event_0_1_thunk;
479 const PPB_IMEInputEvent_Dev_0_2* GetPPB_IMEInputEvent_Dev_0_2_Thunk() {
480 return &g_ppb_ime_input_event_0_2_thunk;
483 const PPB_TouchInputEvent_1_0* GetPPB_TouchInputEvent_1_0_Thunk() {
484 return &g_ppb_touch_input_event_thunk;
487 } // namespace thunk
488 } // namespace ppapi