Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / ppapi / shared_impl / ppb_input_event_shared.cc
blob5cf83b5ac4afb3ab83cec75b5c0a4bdcf86e44f7
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;
12 namespace ppapi {
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() {}
36 InputEventData::~InputEventData() {}
38 PPB_InputEvent_Shared::PPB_InputEvent_Shared(ResourceObjectType type,
39 PP_Instance instance,
40 const InputEventData& data)
41 : Resource(type, instance), data_(data) {}
43 PPB_InputEvent_API* PPB_InputEvent_Shared::AsPPB_InputEvent_API() {
44 return this;
47 const InputEventData& PPB_InputEvent_Shared::GetInputEventData() const {
48 return data_;
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())
99 return 0;
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())
105 return 0;
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) {
114 if (start)
115 *start = data_.composition_selection_start;
116 if (end)
117 *end = data_.composition_selection_end;
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;
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());
147 return 0;
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();
166 if (index >= points->size()) {
167 return PP_MakeTouchPoint();
169 return points->at(index);
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();
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 // static
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,
201 struct PP_Var text,
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)
211 return 0;
213 InputEventData data;
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);
218 if (!text_str)
219 return 0;
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();
233 // static
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,
239 uint32_t modifiers,
240 uint32_t key_code,
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)
247 return 0;
249 InputEventData data;
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);
256 if (!text_str)
257 return 0;
258 data.character_text = text_str->value();
260 if (code.type == PP_VARTYPE_STRING) {
261 StringVar* code_str = StringVar::FromPPVar(code);
262 if (!code_str)
263 return 0;
264 data.code = code_str->value();
267 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
270 // static
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,
276 uint32_t modifiers,
277 PP_InputEvent_MouseButton mouse_button,
278 const PP_Point* mouse_position,
279 int32_t click_count,
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)
286 return 0;
288 InputEventData data;
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();
300 // static
301 PP_Resource PPB_InputEvent_Shared::CreateWheelInputEvent(
302 ResourceObjectType type,
303 PP_Instance instance,
304 PP_TimeTicks time_stamp,
305 uint32_t modifiers,
306 const PP_FloatPoint* wheel_delta,
307 const PP_FloatPoint* wheel_ticks,
308 PP_Bool scroll_by_page) {
309 InputEventData data;
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();
320 // static
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)
331 return 0;
333 InputEventData data;
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();
340 } // namespace ppapi