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.
4
5 #include "ppapi/shared_impl/ppb_input_event_shared.h"
6
7 #include "ppapi/shared_impl/ppapi_globals.h"
8 #include "ppapi/shared_impl/var.h"
9
10 using ppapi::thunk::PPB_InputEvent_API;
11
12 namespace ppapi {
13
InputEventData()14 InputEventData::InputEventData()
15 : is_filtered(false),
16 event_type(PP_INPUTEVENT_TYPE_UNDEFINED),
17 event_time_stamp(0.0),
18 event_modifiers(0),
19 mouse_button(PP_INPUTEVENT_MOUSEBUTTON_NONE),
20 mouse_position(PP_MakePoint(0, 0)),
21 mouse_click_count(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),
26 key_code(0),
27 code(),
28 character_text(),
29 composition_target_segment(-1),
30 composition_selection_start(0),
31 composition_selection_end(0),
32 touches(),
33 changed_touches(),
34 target_touches() {}
35
~InputEventData()36 InputEventData::~InputEventData() {}
37
PPB_InputEvent_Shared(ResourceObjectType type,PP_Instance instance,const InputEventData & data)38 PPB_InputEvent_Shared::PPB_InputEvent_Shared(ResourceObjectType type,
39 PP_Instance instance,
40 const InputEventData& data)
41 : Resource(type, instance), data_(data) {}
42
AsPPB_InputEvent_API()43 PPB_InputEvent_API* PPB_InputEvent_Shared::AsPPB_InputEvent_API() {
44 return this;
45 }
46
GetInputEventData() const47 const InputEventData& PPB_InputEvent_Shared::GetInputEventData() const {
48 return data_;
49 }
50
GetType()51 PP_InputEvent_Type PPB_InputEvent_Shared::GetType() { return data_.event_type; }
52
GetTimeStamp()53 PP_TimeTicks PPB_InputEvent_Shared::GetTimeStamp() {
54 return data_.event_time_stamp;
55 }
56
GetModifiers()57 uint32_t PPB_InputEvent_Shared::GetModifiers() { return data_.event_modifiers; }
58
GetMouseButton()59 PP_InputEvent_MouseButton PPB_InputEvent_Shared::GetMouseButton() {
60 return data_.mouse_button;
61 }
62
GetMousePosition()63 PP_Point PPB_InputEvent_Shared::GetMousePosition() {
64 return data_.mouse_position;
65 }
66
GetMouseClickCount()67 int32_t PPB_InputEvent_Shared::GetMouseClickCount() {
68 return data_.mouse_click_count;
69 }
70
GetMouseMovement()71 PP_Point PPB_InputEvent_Shared::GetMouseMovement() {
72 return data_.mouse_movement;
73 }
74
GetWheelDelta()75 PP_FloatPoint PPB_InputEvent_Shared::GetWheelDelta() {
76 return data_.wheel_delta;
77 }
78
GetWheelTicks()79 PP_FloatPoint PPB_InputEvent_Shared::GetWheelTicks() {
80 return data_.wheel_ticks;
81 }
82
GetWheelScrollByPage()83 PP_Bool PPB_InputEvent_Shared::GetWheelScrollByPage() {
84 return PP_FromBool(data_.wheel_scroll_by_page);
85 }
86
GetKeyCode()87 uint32_t PPB_InputEvent_Shared::GetKeyCode() { return data_.key_code; }
88
GetCharacterText()89 PP_Var PPB_InputEvent_Shared::GetCharacterText() {
90 return StringVar::StringToPPVar(data_.character_text);
91 }
92
GetCode()93 PP_Var PPB_InputEvent_Shared::GetCode() {
94 return StringVar::StringToPPVar(data_.code);
95 }
96
GetIMESegmentNumber()97 uint32_t PPB_InputEvent_Shared::GetIMESegmentNumber() {
98 if (data_.composition_segment_offsets.empty())
99 return 0;
100 return static_cast<uint32_t>(data_.composition_segment_offsets.size() - 1);
101 }
102
GetIMESegmentOffset(uint32_t index)103 uint32_t PPB_InputEvent_Shared::GetIMESegmentOffset(uint32_t index) {
104 if (index >= data_.composition_segment_offsets.size())
105 return 0;
106 return data_.composition_segment_offsets[index];
107 }
108
GetIMETargetSegment()109 int32_t PPB_InputEvent_Shared::GetIMETargetSegment() {
110 return data_.composition_target_segment;
111 }
112
GetIMESelection(uint32_t * start,uint32_t * end)113 void PPB_InputEvent_Shared::GetIMESelection(uint32_t* start, uint32_t* end) {
114 if (start)
115 *start = data_.composition_selection_start;
116 if (end)
117 *end = data_.composition_selection_end;
118 }
119
AddTouchPoint(PP_TouchListType list,const PP_TouchPoint & point)120 void PPB_InputEvent_Shared::AddTouchPoint(PP_TouchListType list,
121 const PP_TouchPoint& point) {
122 switch (list) {
123 case PP_TOUCHLIST_TYPE_TOUCHES:
124 data_.touches.push_back(point);
125 break;
126 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
127 data_.changed_touches.push_back(point);
128 break;
129 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
130 data_.target_touches.push_back(point);
131 break;
132 default:
133 break;
134 }
135 }
136
GetTouchCount(PP_TouchListType list)137 uint32_t PPB_InputEvent_Shared::GetTouchCount(PP_TouchListType list) {
138 switch (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());
145 }
146
147 return 0;
148 }
149
GetTouchByIndex(PP_TouchListType list,uint32_t index)150 PP_TouchPoint PPB_InputEvent_Shared::GetTouchByIndex(PP_TouchListType list,
151 uint32_t index) {
152 std::vector<PP_TouchPoint>* points;
153 switch (list) {
154 case PP_TOUCHLIST_TYPE_TOUCHES:
155 points = &data_.touches;
156 break;
157 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
158 points = &data_.changed_touches;
159 break;
160 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
161 points = &data_.target_touches;
162 break;
163 default:
164 return PP_MakeTouchPoint();
165 }
166 if (index >= points->size()) {
167 return PP_MakeTouchPoint();
168 }
169 return points->at(index);
170 }
171
GetTouchById(PP_TouchListType list,uint32_t id)172 PP_TouchPoint PPB_InputEvent_Shared::GetTouchById(PP_TouchListType list,
173 uint32_t id) {
174 const std::vector<PP_TouchPoint>* points;
175 switch (list) {
176 case PP_TOUCHLIST_TYPE_TOUCHES:
177 points = &data_.touches;
178 break;
179 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
180 points = &data_.changed_touches;
181 break;
182 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
183 points = &data_.target_touches;
184 break;
185 default:
186 return PP_MakeTouchPoint();
187 }
188 for (size_t i = 0; i < points->size(); i++) {
189 if (points->at(i).id == id)
190 return points->at(i);
191 }
192 return PP_MakeTouchPoint();
193 }
194
TraceInputLatency(PP_Bool has_damage)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,
198 0, NULL))
199 return PP_FALSE;
200 if (has_damage) {
201 PpapiGlobals::Get()->AddLatencyInfo(latency, pp_instance());
202 } else {
203 latency.AddLatencyNumber(
204 ui::INPUT_EVENT_LATENCY_TERMINATED_PLUGIN_COMPONENT, 0, 0);
205 }
206 return PP_TRUE;
207 }
208
209 // static
CreateIMEInputEvent(ResourceObjectType type,PP_Instance instance,PP_InputEvent_Type event_type,PP_TimeTicks time_stamp,struct PP_Var text,uint32_t segment_number,const uint32_t * segment_offsets,int32_t target_segment,uint32_t selection_start,uint32_t selection_end)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,
215 struct PP_Var text,
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)
225 return 0;
226
227 InputEventData data;
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);
232 if (!text_str)
233 return 0;
234 data.character_text = text_str->value();
235 }
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]);
240 }
241 data.composition_selection_start = selection_start;
242 data.composition_selection_end = selection_end;
243
244 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
245 }
246
247 // static
CreateKeyboardInputEvent(ResourceObjectType type,PP_Instance instance,PP_InputEvent_Type event_type,PP_TimeTicks time_stamp,uint32_t modifiers,uint32_t key_code,struct PP_Var character_text,struct PP_Var code)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,
253 uint32_t modifiers,
254 uint32_t key_code,
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)
261 return 0;
262
263 InputEventData data;
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);
270 if (!text_str)
271 return 0;
272 data.character_text = text_str->value();
273 }
274 if (code.type == PP_VARTYPE_STRING) {
275 StringVar* code_str = StringVar::FromPPVar(code);
276 if (!code_str)
277 return 0;
278 data.code = code_str->value();
279 }
280
281 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
282 }
283
284 // static
CreateMouseInputEvent(ResourceObjectType type,PP_Instance instance,PP_InputEvent_Type event_type,PP_TimeTicks time_stamp,uint32_t modifiers,PP_InputEvent_MouseButton mouse_button,const PP_Point * mouse_position,int32_t click_count,const PP_Point * mouse_movement)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,
290 uint32_t modifiers,
291 PP_InputEvent_MouseButton mouse_button,
292 const PP_Point* mouse_position,
293 int32_t click_count,
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)
300 return 0;
301
302 InputEventData data;
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;
310
311 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
312 }
313
314 // static
CreateWheelInputEvent(ResourceObjectType type,PP_Instance instance,PP_TimeTicks time_stamp,uint32_t modifiers,const PP_FloatPoint * wheel_delta,const PP_FloatPoint * wheel_ticks,PP_Bool scroll_by_page)315 PP_Resource PPB_InputEvent_Shared::CreateWheelInputEvent(
316 ResourceObjectType type,
317 PP_Instance instance,
318 PP_TimeTicks time_stamp,
319 uint32_t modifiers,
320 const PP_FloatPoint* wheel_delta,
321 const PP_FloatPoint* wheel_ticks,
322 PP_Bool scroll_by_page) {
323 InputEventData data;
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);
330
331 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
332 }
333
334 // static
CreateTouchInputEvent(ResourceObjectType type,PP_Instance instance,PP_InputEvent_Type event_type,PP_TimeTicks time_stamp,uint32_t modifiers)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)
345 return 0;
346
347 InputEventData data;
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();
352 }
353
354 } // namespace ppapi
355