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/ppapi_globals.h"
8 #include "ppapi/shared_impl/var.h"
10 using ppapi::thunk::PPB_InputEvent_API
;
14 InputEventData::InputEventData()
16 event_type(PP_INPUTEVENT_TYPE_UNDEFINED
),
17 event_time_stamp(0.0),
19 mouse_button(PP_INPUTEVENT_MOUSEBUTTON_NONE
),
20 mouse_position(PP_MakePoint(0, 0)),
22 mouse_movement(PP_MakePoint(0, 0)),
23 wheel_delta(PP_MakeFloatPoint(0.0f
, 0.0f
)),
24 wheel_ticks(PP_MakeFloatPoint(0.0f
, 0.0f
)),
25 wheel_scroll_by_page(false),
29 composition_target_segment(-1),
30 composition_selection_start(0),
31 composition_selection_end(0),
36 InputEventData::~InputEventData() {}
38 PPB_InputEvent_Shared::PPB_InputEvent_Shared(ResourceObjectType type
,
40 const InputEventData
& data
)
41 : Resource(type
, instance
), data_(data
) {}
43 PPB_InputEvent_API
* PPB_InputEvent_Shared::AsPPB_InputEvent_API() {
47 const InputEventData
& PPB_InputEvent_Shared::GetInputEventData() const {
51 PP_InputEvent_Type
PPB_InputEvent_Shared::GetType() { return data_
.event_type
; }
53 PP_TimeTicks
PPB_InputEvent_Shared::GetTimeStamp() {
54 return data_
.event_time_stamp
;
57 uint32_t PPB_InputEvent_Shared::GetModifiers() { return data_
.event_modifiers
; }
59 PP_InputEvent_MouseButton
PPB_InputEvent_Shared::GetMouseButton() {
60 return data_
.mouse_button
;
63 PP_Point
PPB_InputEvent_Shared::GetMousePosition() {
64 return data_
.mouse_position
;
67 int32_t PPB_InputEvent_Shared::GetMouseClickCount() {
68 return data_
.mouse_click_count
;
71 PP_Point
PPB_InputEvent_Shared::GetMouseMovement() {
72 return data_
.mouse_movement
;
75 PP_FloatPoint
PPB_InputEvent_Shared::GetWheelDelta() {
76 return data_
.wheel_delta
;
79 PP_FloatPoint
PPB_InputEvent_Shared::GetWheelTicks() {
80 return data_
.wheel_ticks
;
83 PP_Bool
PPB_InputEvent_Shared::GetWheelScrollByPage() {
84 return PP_FromBool(data_
.wheel_scroll_by_page
);
87 uint32_t PPB_InputEvent_Shared::GetKeyCode() { return data_
.key_code
; }
89 PP_Var
PPB_InputEvent_Shared::GetCharacterText() {
90 return StringVar::StringToPPVar(data_
.character_text
);
93 PP_Var
PPB_InputEvent_Shared::GetCode() {
94 return StringVar::StringToPPVar(data_
.code
);
97 uint32_t PPB_InputEvent_Shared::GetIMESegmentNumber() {
98 if (data_
.composition_segment_offsets
.empty())
100 return static_cast<uint32_t>(data_
.composition_segment_offsets
.size() - 1);
103 uint32_t PPB_InputEvent_Shared::GetIMESegmentOffset(uint32_t index
) {
104 if (index
>= data_
.composition_segment_offsets
.size())
106 return data_
.composition_segment_offsets
[index
];
109 int32_t PPB_InputEvent_Shared::GetIMETargetSegment() {
110 return data_
.composition_target_segment
;
113 void PPB_InputEvent_Shared::GetIMESelection(uint32_t* start
, uint32_t* end
) {
115 *start
= data_
.composition_selection_start
;
117 *end
= data_
.composition_selection_end
;
120 void PPB_InputEvent_Shared::AddTouchPoint(PP_TouchListType list
,
121 const PP_TouchPoint
& point
) {
123 case PP_TOUCHLIST_TYPE_TOUCHES
:
124 data_
.touches
.push_back(point
);
126 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
127 data_
.changed_touches
.push_back(point
);
129 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
130 data_
.target_touches
.push_back(point
);
137 uint32_t PPB_InputEvent_Shared::GetTouchCount(PP_TouchListType list
) {
139 case PP_TOUCHLIST_TYPE_TOUCHES
:
140 return static_cast<uint32_t>(data_
.touches
.size());
141 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
142 return static_cast<uint32_t>(data_
.changed_touches
.size());
143 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
144 return static_cast<uint32_t>(data_
.target_touches
.size());
150 PP_TouchPoint
PPB_InputEvent_Shared::GetTouchByIndex(PP_TouchListType list
,
152 std::vector
<PP_TouchPoint
>* points
;
154 case PP_TOUCHLIST_TYPE_TOUCHES
:
155 points
= &data_
.touches
;
157 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
158 points
= &data_
.changed_touches
;
160 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
161 points
= &data_
.target_touches
;
164 return PP_MakeTouchPoint();
166 if (index
>= points
->size()) {
167 return PP_MakeTouchPoint();
169 return points
->at(index
);
172 PP_TouchPoint
PPB_InputEvent_Shared::GetTouchById(PP_TouchListType list
,
174 const std::vector
<PP_TouchPoint
>* points
;
176 case PP_TOUCHLIST_TYPE_TOUCHES
:
177 points
= &data_
.touches
;
179 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES
:
180 points
= &data_
.changed_touches
;
182 case PP_TOUCHLIST_TYPE_TARGETTOUCHES
:
183 points
= &data_
.target_touches
;
186 return PP_MakeTouchPoint();
188 for (size_t i
= 0; i
< points
->size(); i
++) {
189 if (points
->at(i
).id
== id
)
190 return points
->at(i
);
192 return PP_MakeTouchPoint();
196 PP_Resource
PPB_InputEvent_Shared::CreateIMEInputEvent(
197 ResourceObjectType type
,
198 PP_Instance instance
,
199 PP_InputEvent_Type event_type
,
200 PP_TimeTicks time_stamp
,
202 uint32_t segment_number
,
203 const uint32_t* segment_offsets
,
204 int32_t target_segment
,
205 uint32_t selection_start
,
206 uint32_t selection_end
) {
207 if (event_type
!= PP_INPUTEVENT_TYPE_IME_COMPOSITION_START
&&
208 event_type
!= PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE
&&
209 event_type
!= PP_INPUTEVENT_TYPE_IME_COMPOSITION_END
&&
210 event_type
!= PP_INPUTEVENT_TYPE_IME_TEXT
)
214 data
.event_type
= event_type
;
215 data
.event_time_stamp
= time_stamp
;
216 if (text
.type
== PP_VARTYPE_STRING
) {
217 StringVar
* text_str
= StringVar::FromPPVar(text
);
220 data
.character_text
= text_str
->value();
222 data
.composition_target_segment
= target_segment
;
223 if (segment_number
!= 0) {
224 data
.composition_segment_offsets
.assign(
225 &segment_offsets
[0], &segment_offsets
[segment_number
+ 1]);
227 data
.composition_selection_start
= selection_start
;
228 data
.composition_selection_end
= selection_end
;
230 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
234 PP_Resource
PPB_InputEvent_Shared::CreateKeyboardInputEvent(
235 ResourceObjectType type
,
236 PP_Instance instance
,
237 PP_InputEvent_Type event_type
,
238 PP_TimeTicks time_stamp
,
241 struct PP_Var character_text
,
242 struct PP_Var code
) {
243 if (event_type
!= PP_INPUTEVENT_TYPE_RAWKEYDOWN
&&
244 event_type
!= PP_INPUTEVENT_TYPE_KEYDOWN
&&
245 event_type
!= PP_INPUTEVENT_TYPE_KEYUP
&&
246 event_type
!= PP_INPUTEVENT_TYPE_CHAR
)
250 data
.event_type
= event_type
;
251 data
.event_time_stamp
= time_stamp
;
252 data
.event_modifiers
= modifiers
;
253 data
.key_code
= key_code
;
254 if (character_text
.type
== PP_VARTYPE_STRING
) {
255 StringVar
* text_str
= StringVar::FromPPVar(character_text
);
258 data
.character_text
= text_str
->value();
260 if (code
.type
== PP_VARTYPE_STRING
) {
261 StringVar
* code_str
= StringVar::FromPPVar(code
);
264 data
.code
= code_str
->value();
267 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
271 PP_Resource
PPB_InputEvent_Shared::CreateMouseInputEvent(
272 ResourceObjectType type
,
273 PP_Instance instance
,
274 PP_InputEvent_Type event_type
,
275 PP_TimeTicks time_stamp
,
277 PP_InputEvent_MouseButton mouse_button
,
278 const PP_Point
* mouse_position
,
280 const PP_Point
* mouse_movement
) {
281 if (event_type
!= PP_INPUTEVENT_TYPE_MOUSEDOWN
&&
282 event_type
!= PP_INPUTEVENT_TYPE_MOUSEUP
&&
283 event_type
!= PP_INPUTEVENT_TYPE_MOUSEMOVE
&&
284 event_type
!= PP_INPUTEVENT_TYPE_MOUSEENTER
&&
285 event_type
!= PP_INPUTEVENT_TYPE_MOUSELEAVE
)
289 data
.event_type
= event_type
;
290 data
.event_time_stamp
= time_stamp
;
291 data
.event_modifiers
= modifiers
;
292 data
.mouse_button
= mouse_button
;
293 data
.mouse_position
= *mouse_position
;
294 data
.mouse_click_count
= click_count
;
295 data
.mouse_movement
= *mouse_movement
;
297 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
301 PP_Resource
PPB_InputEvent_Shared::CreateWheelInputEvent(
302 ResourceObjectType type
,
303 PP_Instance instance
,
304 PP_TimeTicks time_stamp
,
306 const PP_FloatPoint
* wheel_delta
,
307 const PP_FloatPoint
* wheel_ticks
,
308 PP_Bool scroll_by_page
) {
310 data
.event_type
= PP_INPUTEVENT_TYPE_WHEEL
;
311 data
.event_time_stamp
= time_stamp
;
312 data
.event_modifiers
= modifiers
;
313 data
.wheel_delta
= *wheel_delta
;
314 data
.wheel_ticks
= *wheel_ticks
;
315 data
.wheel_scroll_by_page
= PP_ToBool(scroll_by_page
);
317 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
321 PP_Resource
PPB_InputEvent_Shared::CreateTouchInputEvent(
322 ResourceObjectType type
,
323 PP_Instance instance
,
324 PP_InputEvent_Type event_type
,
325 PP_TimeTicks time_stamp
,
326 uint32_t modifiers
) {
327 if (event_type
!= PP_INPUTEVENT_TYPE_TOUCHSTART
&&
328 event_type
!= PP_INPUTEVENT_TYPE_TOUCHMOVE
&&
329 event_type
!= PP_INPUTEVENT_TYPE_TOUCHEND
&&
330 event_type
!= PP_INPUTEVENT_TYPE_TOUCHCANCEL
)
334 data
.event_type
= event_type
;
335 data
.event_time_stamp
= time_stamp
;
336 data
.event_modifiers
= modifiers
;
337 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();