Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / ppapi / shared_impl / ppb_input_event_shared.cc
blob4cd918d161851b9d782a5821f70b893de6ddb79d
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;
11 namespace ppapi {
13 InputEventData::InputEventData()
14 : is_filtered(false),
15 event_type(PP_INPUTEVENT_TYPE_UNDEFINED),
16 event_time_stamp(0.0),
17 event_modifiers(0),
18 mouse_button(PP_INPUTEVENT_MOUSEBUTTON_NONE),
19 mouse_position(PP_MakePoint(0, 0)),
20 mouse_click_count(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),
25 key_code(0),
26 usb_key_code(0),
27 character_text(),
28 composition_target_segment(-1),
29 composition_selection_start(0),
30 composition_selection_end(0),
31 touches(),
32 changed_touches(),
33 target_touches() {
36 InputEventData::~InputEventData() {
39 PPB_InputEvent_Shared::PPB_InputEvent_Shared(ResourceObjectType type,
40 PP_Instance instance,
41 const InputEventData& data)
42 : Resource(type, instance),
43 data_(data) {
46 PPB_InputEvent_API* PPB_InputEvent_Shared::AsPPB_InputEvent_API() {
47 return this;
50 const InputEventData& PPB_InputEvent_Shared::GetInputEventData() const {
51 return data_;
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;
104 return PP_TRUE;
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())
113 return 0;
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())
119 return 0;
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) {
128 if (start)
129 *start = data_.composition_selection_start;
130 if (end)
131 *end = data_.composition_selection_end;
134 void PPB_InputEvent_Shared::AddTouchPoint(PP_TouchListType list,
135 const PP_TouchPoint& point) {
136 switch (list) {
137 case PP_TOUCHLIST_TYPE_TOUCHES:
138 data_.touches.push_back(point);
139 break;
140 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
141 data_.changed_touches.push_back(point);
142 break;
143 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
144 data_.target_touches.push_back(point);
145 break;
146 default:
147 break;
151 uint32_t PPB_InputEvent_Shared::GetTouchCount(PP_TouchListType list) {
152 switch (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());
161 return 0;
164 PP_TouchPoint PPB_InputEvent_Shared::GetTouchByIndex(PP_TouchListType list,
165 uint32_t index) {
166 std::vector<PP_TouchPoint>* points;
167 switch (list) {
168 case PP_TOUCHLIST_TYPE_TOUCHES:
169 points = &data_.touches;
170 break;
171 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
172 points = &data_.changed_touches;
173 break;
174 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
175 points = &data_.target_touches;
176 break;
177 default:
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,
187 uint32_t id) {
188 const std::vector<PP_TouchPoint>* points;
189 switch (list) {
190 case PP_TOUCHLIST_TYPE_TOUCHES:
191 points = &data_.touches;
192 break;
193 case PP_TOUCHLIST_TYPE_CHANGEDTOUCHES:
194 points = &data_.changed_touches;
195 break;
196 case PP_TOUCHLIST_TYPE_TARGETTOUCHES:
197 points = &data_.target_touches;
198 break;
199 default:
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();
209 //static
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;
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();
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();
247 //static
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 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)
260 return 0;
262 InputEventData data;
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);
269 if (!text_str)
270 return 0;
271 data.character_text = text_str->value();
274 return (new PPB_InputEvent_Shared(type, instance, data))->GetReference();
277 //static
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,
283 uint32_t modifiers,
284 PP_InputEvent_MouseButton mouse_button,
285 const PP_Point* mouse_position,
286 int32_t click_count,
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)
293 return 0;
295 InputEventData data;
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();
307 //static
308 PP_Resource PPB_InputEvent_Shared::CreateWheelInputEvent(
309 ResourceObjectType type,
310 PP_Instance instance,
311 PP_TimeTicks time_stamp,
312 uint32_t modifiers,
313 const PP_FloatPoint* wheel_delta,
314 const PP_FloatPoint* wheel_ticks,
315 PP_Bool scroll_by_page) {
316 InputEventData data;
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();
327 // static
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)
338 return 0;
340 InputEventData data;
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();
347 } // namespace ppapi