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();
195 PP_Bool
PPB_InputEvent_Shared::TraceInputLatency(PP_Bool has_damage
) {
196 ui::LatencyInfo latency
= data_
.latency_info
;
197 if (!latency
.FindLatency(ui::INPUT_EVENT_LATENCY_BEGIN_PLUGIN_COMPONENT
,
201 PpapiGlobals::Get()->AddLatencyInfo(latency
, pp_instance());
203 latency
.AddLatencyNumber(
204 ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT
, 0, 0);
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 struct PP_Var code
) {
257 if (event_type
!= PP_INPUTEVENT_TYPE_RAWKEYDOWN
&&
258 event_type
!= PP_INPUTEVENT_TYPE_KEYDOWN
&&
259 event_type
!= PP_INPUTEVENT_TYPE_KEYUP
&&
260 event_type
!= PP_INPUTEVENT_TYPE_CHAR
)
264 data
.event_type
= event_type
;
265 data
.event_time_stamp
= time_stamp
;
266 data
.event_modifiers
= modifiers
;
267 data
.key_code
= key_code
;
268 if (character_text
.type
== PP_VARTYPE_STRING
) {
269 StringVar
* text_str
= StringVar::FromPPVar(character_text
);
272 data
.character_text
= text_str
->value();
274 if (code
.type
== PP_VARTYPE_STRING
) {
275 StringVar
* code_str
= StringVar::FromPPVar(code
);
278 data
.code
= code_str
->value();
281 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
285 PP_Resource
PPB_InputEvent_Shared::CreateMouseInputEvent(
286 ResourceObjectType type
,
287 PP_Instance instance
,
288 PP_InputEvent_Type event_type
,
289 PP_TimeTicks time_stamp
,
291 PP_InputEvent_MouseButton mouse_button
,
292 const PP_Point
* mouse_position
,
294 const PP_Point
* mouse_movement
) {
295 if (event_type
!= PP_INPUTEVENT_TYPE_MOUSEDOWN
&&
296 event_type
!= PP_INPUTEVENT_TYPE_MOUSEUP
&&
297 event_type
!= PP_INPUTEVENT_TYPE_MOUSEMOVE
&&
298 event_type
!= PP_INPUTEVENT_TYPE_MOUSEENTER
&&
299 event_type
!= PP_INPUTEVENT_TYPE_MOUSELEAVE
)
303 data
.event_type
= event_type
;
304 data
.event_time_stamp
= time_stamp
;
305 data
.event_modifiers
= modifiers
;
306 data
.mouse_button
= mouse_button
;
307 data
.mouse_position
= *mouse_position
;
308 data
.mouse_click_count
= click_count
;
309 data
.mouse_movement
= *mouse_movement
;
311 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
315 PP_Resource
PPB_InputEvent_Shared::CreateWheelInputEvent(
316 ResourceObjectType type
,
317 PP_Instance instance
,
318 PP_TimeTicks time_stamp
,
320 const PP_FloatPoint
* wheel_delta
,
321 const PP_FloatPoint
* wheel_ticks
,
322 PP_Bool scroll_by_page
) {
324 data
.event_type
= PP_INPUTEVENT_TYPE_WHEEL
;
325 data
.event_time_stamp
= time_stamp
;
326 data
.event_modifiers
= modifiers
;
327 data
.wheel_delta
= *wheel_delta
;
328 data
.wheel_ticks
= *wheel_ticks
;
329 data
.wheel_scroll_by_page
= PP_ToBool(scroll_by_page
);
331 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();
335 PP_Resource
PPB_InputEvent_Shared::CreateTouchInputEvent(
336 ResourceObjectType type
,
337 PP_Instance instance
,
338 PP_InputEvent_Type event_type
,
339 PP_TimeTicks time_stamp
,
340 uint32_t modifiers
) {
341 if (event_type
!= PP_INPUTEVENT_TYPE_TOUCHSTART
&&
342 event_type
!= PP_INPUTEVENT_TYPE_TOUCHMOVE
&&
343 event_type
!= PP_INPUTEVENT_TYPE_TOUCHEND
&&
344 event_type
!= PP_INPUTEVENT_TYPE_TOUCHCANCEL
)
348 data
.event_type
= event_type
;
349 data
.event_time_stamp
= time_stamp
;
350 data
.event_modifiers
= modifiers
;
351 return (new PPB_InputEvent_Shared(type
, instance
, data
))->GetReference();