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/shared_impl/ppb_input_event_shared.h"
7 #include "ppapi/shared_impl/var.h"
9 using ppapi::thunk::PPB_InputEvent_API
;
13 InputEventData::InputEventData()
15 event_type(PP_INPUTEVENT_TYPE_UNDEFINED
),
16 event_time_stamp(0.0),
18 mouse_button(PP_INPUTEVENT_MOUSEBUTTON_NONE
),
19 mouse_position(PP_MakePoint(0, 0)),
21 mouse_movement(PP_MakePoint(0, 0)),
22 wheel_delta(PP_MakeFloatPoint(0.0f
, 0.0f
)),
23 wheel_ticks(PP_MakeFloatPoint(0.0f
, 0.0f
)),
24 wheel_scroll_by_page(false),
28 composition_target_segment(-1),
29 composition_selection_start(0),
30 composition_selection_end(0),
36 InputEventData::~InputEventData() {
39 PPB_InputEvent_Shared::PPB_InputEvent_Shared(ResourceObjectType type
,
41 const InputEventData
& data
)
42 : Resource(type
, instance
),
46 PPB_InputEvent_API
* PPB_InputEvent_Shared::AsPPB_InputEvent_API() {
50 const InputEventData
& PPB_InputEvent_Shared::GetInputEventData() const {
54 PP_InputEvent_Type
PPB_InputEvent_Shared::GetType() {
55 return data_
.event_type
;
58 PP_TimeTicks
PPB_InputEvent_Shared::GetTimeStamp() {
59 return data_
.event_time_stamp
;
62 uint32_t PPB_InputEvent_Shared::GetModifiers() {
63 return data_
.event_modifiers
;
66 PP_InputEvent_MouseButton
PPB_InputEvent_Shared::GetMouseButton() {
67 return data_
.mouse_button
;
70 PP_Point
PPB_InputEvent_Shared::GetMousePosition() {
71 return data_
.mouse_position
;
74 int32_t PPB_InputEvent_Shared::GetMouseClickCount() {
75 return data_
.mouse_click_count
;
78 PP_Point
PPB_InputEvent_Shared::GetMouseMovement() {
79 return data_
.mouse_movement
;
82 PP_FloatPoint
PPB_InputEvent_Shared::GetWheelDelta() {
83 return data_
.wheel_delta
;
86 PP_FloatPoint
PPB_InputEvent_Shared::GetWheelTicks() {
87 return data_
.wheel_ticks
;
90 PP_Bool
PPB_InputEvent_Shared::GetWheelScrollByPage() {
91 return PP_FromBool(data_
.wheel_scroll_by_page
);
94 uint32_t PPB_InputEvent_Shared::GetKeyCode() {
95 return data_
.key_code
;
98 PP_Var
PPB_InputEvent_Shared::GetCharacterText() {
99 return StringVar::StringToPPVar(data_
.character_text
);
102 PP_Bool
PPB_InputEvent_Shared::SetUsbKeyCode(uint32_t usb_key_code
) {
103 data_
.usb_key_code
= usb_key_code
;
107 uint32_t PPB_InputEvent_Shared::GetUsbKeyCode() {
108 return data_
.usb_key_code
;
111 uint32_t PPB_InputEvent_Shared::GetIMESegmentNumber() {
112 if (data_
.composition_segment_offsets
.empty())
114 return static_cast<uint32_t>(data_
.composition_segment_offsets
.size() - 1);
117 uint32_t PPB_InputEvent_Shared::GetIMESegmentOffset(uint32_t index
) {
118 if (index
>= data_
.composition_segment_offsets
.size())
120 return data_
.composition_segment_offsets
[index
];
123 int32_t PPB_InputEvent_Shared::GetIMETargetSegment() {
124 return data_
.composition_target_segment
;
127 void PPB_InputEvent_Shared::GetIMESelection(uint32_t* start
, uint32_t* end
) {
129 *start
= data_
.composition_selection_start
;
131 *end
= data_
.composition_selection_end
;
134 void PPB_InputEvent_Shared::AddTouchPoint(PP_TouchListType list
,
135 const PP_TouchPoint
& point
) {
137 case PP_TOUCHLIST_TYPE_TOUCHES
:
138 data_
.touches
.push_back(point
);
140 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
141 data_
.changed_touches
.push_back(point
);
143 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
144 data_
.target_touches
.push_back(point
);
151 uint32_t PPB_InputEvent_Shared::GetTouchCount(PP_TouchListType list
) {
153 case PP_TOUCHLIST_TYPE_TOUCHES
:
154 return static_cast<uint32_t>(data_
.touches
.size());
155 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
156 return static_cast<uint32_t>(data_
.changed_touches
.size());
157 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
158 return static_cast<uint32_t>(data_
.target_touches
.size());
164 PP_TouchPoint
PPB_InputEvent_Shared::GetTouchByIndex(PP_TouchListType list
,
166 std::vector
<PP_TouchPoint
>* points
;
168 case PP_TOUCHLIST_TYPE_TOUCHES
:
169 points
= &data_
.touches
;
171 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
172 points
= &data_
.changed_touches
;
174 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
175 points
= &data_
.target_touches
;
178 return PP_MakeTouchPoint();
180 if (index
>= points
->size()) {
181 return PP_MakeTouchPoint();
183 return points
->at(index
);
186 PP_TouchPoint
PPB_InputEvent_Shared::GetTouchById(PP_TouchListType list
,
188 const std::vector
<PP_TouchPoint
>* points
;
190 case PP_TOUCHLIST_TYPE_TOUCHES
:
191 points
= &data_
.touches
;
193 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
194 points
= &data_
.changed_touches
;
196 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
197 points
= &data_
.target_touches
;
200 return PP_MakeTouchPoint();
202 for (size_t i
= 0; i
< points
->size(); i
++) {
203 if (points
->at(i
).id
== id
)
204 return points
->at(i
);
206 return PP_MakeTouchPoint();
210 PP_Resource
PPB_InputEvent_Shared::CreateIMEInputEvent(
211 ResourceObjectType type
,
212 PP_Instance instance
,
213 PP_InputEvent_Type event_type
,
214 PP_TimeTicks time_stamp
,
216 uint32_t segment_number
,
217 const uint32_t* segment_offsets
,
218 int32_t target_segment
,
219 uint32_t selection_start
,
220 uint32_t selection_end
) {
221 if (event_type
!= PP_INPUTEVENT_TYPE_IME_COMPOSITION_START
&&
222 event_type
!= PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE
&&
223 event_type
!= PP_INPUTEVENT_TYPE_IME_COMPOSITION_END
&&
224 event_type
!= PP_INPUTEVENT_TYPE_IME_TEXT
)
228 data
.event_type
= event_type
;
229 data
.event_time_stamp
= time_stamp
;
230 if (text
.type
== PP_VARTYPE_STRING
) {
231 StringVar
* text_str
= StringVar::FromPPVar(text
);
234 data
.character_text
= text_str
->value();
236 data
.composition_target_segment
= target_segment
;
237 if (segment_number
!= 0) {
238 data
.composition_segment_offsets
.assign(
239 &segment_offsets
[0], &segment_offsets
[segment_number
+ 1]);
241 data
.composition_selection_start
= selection_start
;
242 data
.composition_selection_end
= selection_end
;
244 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
248 PP_Resource
PPB_InputEvent_Shared::CreateKeyboardInputEvent(
249 ResourceObjectType type
,
250 PP_Instance instance
,
251 PP_InputEvent_Type event_type
,
252 PP_TimeTicks time_stamp
,
255 struct PP_Var character_text
) {
256 if (event_type
!= PP_INPUTEVENT_TYPE_RAWKEYDOWN
&&
257 event_type
!= PP_INPUTEVENT_TYPE_KEYDOWN
&&
258 event_type
!= PP_INPUTEVENT_TYPE_KEYUP
&&
259 event_type
!= PP_INPUTEVENT_TYPE_CHAR
)
263 data
.event_type
= event_type
;
264 data
.event_time_stamp
= time_stamp
;
265 data
.event_modifiers
= modifiers
;
266 data
.key_code
= key_code
;
267 if (character_text
.type
== PP_VARTYPE_STRING
) {
268 StringVar
* text_str
= StringVar::FromPPVar(character_text
);
271 data
.character_text
= text_str
->value();
274 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
278 PP_Resource
PPB_InputEvent_Shared::CreateMouseInputEvent(
279 ResourceObjectType type
,
280 PP_Instance instance
,
281 PP_InputEvent_Type event_type
,
282 PP_TimeTicks time_stamp
,
284 PP_InputEvent_MouseButton mouse_button
,
285 const PP_Point
* mouse_position
,
287 const PP_Point
* mouse_movement
) {
288 if (event_type
!= PP_INPUTEVENT_TYPE_MOUSEDOWN
&&
289 event_type
!= PP_INPUTEVENT_TYPE_MOUSEUP
&&
290 event_type
!= PP_INPUTEVENT_TYPE_MOUSEMOVE
&&
291 event_type
!= PP_INPUTEVENT_TYPE_MOUSEENTER
&&
292 event_type
!= PP_INPUTEVENT_TYPE_MOUSELEAVE
)
296 data
.event_type
= event_type
;
297 data
.event_time_stamp
= time_stamp
;
298 data
.event_modifiers
= modifiers
;
299 data
.mouse_button
= mouse_button
;
300 data
.mouse_position
= *mouse_position
;
301 data
.mouse_click_count
= click_count
;
302 data
.mouse_movement
= *mouse_movement
;
304 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
308 PP_Resource
PPB_InputEvent_Shared::CreateWheelInputEvent(
309 ResourceObjectType type
,
310 PP_Instance instance
,
311 PP_TimeTicks time_stamp
,
313 const PP_FloatPoint
* wheel_delta
,
314 const PP_FloatPoint
* wheel_ticks
,
315 PP_Bool scroll_by_page
) {
317 data
.event_type
= PP_INPUTEVENT_TYPE_WHEEL
;
318 data
.event_time_stamp
= time_stamp
;
319 data
.event_modifiers
= modifiers
;
320 data
.wheel_delta
= *wheel_delta
;
321 data
.wheel_ticks
= *wheel_ticks
;
322 data
.wheel_scroll_by_page
= PP_ToBool(scroll_by_page
);
324 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
328 PP_Resource
PPB_InputEvent_Shared::CreateTouchInputEvent(
329 ResourceObjectType type
,
330 PP_Instance instance
,
331 PP_InputEvent_Type event_type
,
332 PP_TimeTicks time_stamp
,
333 uint32_t modifiers
) {
334 if (event_type
!= PP_INPUTEVENT_TYPE_TOUCHSTART
&&
335 event_type
!= PP_INPUTEVENT_TYPE_TOUCHMOVE
&&
336 event_type
!= PP_INPUTEVENT_TYPE_TOUCHEND
&&
337 event_type
!= PP_INPUTEVENT_TYPE_TOUCHCANCEL
)
341 data
.event_type
= event_type
;
342 data
.event_time_stamp
= time_stamp
;
343 data
.event_modifiers
= modifiers
;
344 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();