OLD | NEW |
| (Empty) |
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 "ui/base/event.h" | |
6 | |
7 #if defined(USE_X11) | |
8 #include <X11/Xlib.h> | |
9 #endif | |
10 | |
11 #include <cstring> | |
12 | |
13 #include "ui/base/keycodes/keyboard_code_conversion.h" | |
14 #include "ui/gfx/point3.h" | |
15 #include "ui/gfx/interpolated_transform.h" | |
16 #include "ui/gfx/transform.h" | |
17 | |
18 #if defined(USE_X11) | |
19 #include "ui/base/keycodes/keyboard_code_conversion_x.h" | |
20 #endif | |
21 | |
22 namespace { | |
23 | |
24 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { | |
25 #if defined(USE_X11) | |
26 XEvent* copy = new XEvent; | |
27 *copy = *event; | |
28 return copy; | |
29 #elif defined(OS_WIN) | |
30 return event; | |
31 #else | |
32 NOTREACHED() << | |
33 "Don't know how to copy base::NativeEvent for this platform"; | |
34 return NULL; | |
35 #endif | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 namespace ui { | |
41 | |
42 Event::~Event() { | |
43 #if defined(USE_X11) | |
44 if (delete_native_event_) | |
45 delete native_event_; | |
46 #endif | |
47 } | |
48 | |
49 bool Event::HasNativeEvent() const { | |
50 base::NativeEvent null_event; | |
51 std::memset(&null_event, 0, sizeof(null_event)); | |
52 return !!std::memcmp(&native_event_, &null_event, sizeof(null_event)); | |
53 } | |
54 | |
55 Event::Event(EventType type, int flags) | |
56 : type_(type), | |
57 time_stamp_(base::Time::NowFromSystemTime() - base::Time()), | |
58 flags_(flags), | |
59 delete_native_event_(false) { | |
60 Init(); | |
61 } | |
62 | |
63 Event::Event(const base::NativeEvent& native_event, | |
64 EventType type, | |
65 int flags) | |
66 : type_(type), | |
67 time_stamp_(EventTimeFromNative(native_event)), | |
68 flags_(flags), | |
69 delete_native_event_(false) { | |
70 InitWithNativeEvent(native_event); | |
71 } | |
72 | |
73 Event::Event(const Event& copy) | |
74 : native_event_(copy.native_event_), | |
75 type_(copy.type_), | |
76 time_stamp_(copy.time_stamp_), | |
77 flags_(copy.flags_), | |
78 delete_native_event_(false) { | |
79 } | |
80 | |
81 void Event::Init() { | |
82 std::memset(&native_event_, 0, sizeof(native_event_)); | |
83 } | |
84 | |
85 void Event::InitWithNativeEvent(const base::NativeEvent& native_event) { | |
86 native_event_ = native_event; | |
87 } | |
88 | |
89 LocatedEvent::~LocatedEvent() { | |
90 } | |
91 | |
92 LocatedEvent::LocatedEvent(const base::NativeEvent& native_event) | |
93 : Event(native_event, | |
94 EventTypeFromNative(native_event), | |
95 EventFlagsFromNative(native_event)), | |
96 location_(EventLocationFromNative(native_event)), | |
97 root_location_(location_) { | |
98 } | |
99 | |
100 LocatedEvent::LocatedEvent(EventType type, | |
101 const gfx::Point& location, | |
102 const gfx::Point& root_location, | |
103 int flags) | |
104 : Event(type, flags), | |
105 location_(location), | |
106 root_location_(root_location) { | |
107 } | |
108 | |
109 void LocatedEvent::UpdateForRootTransform(const Transform& root_transform) { | |
110 // Transform has to be done at root level. | |
111 DCHECK_EQ(root_location_.x(), location_.x()); | |
112 DCHECK_EQ(root_location_.y(), location_.y()); | |
113 gfx::Point3f p(location_); | |
114 root_transform.TransformPointReverse(p); | |
115 root_location_ = location_ = p.AsPoint(); | |
116 } | |
117 | |
118 MouseEvent::MouseEvent(const base::NativeEvent& native_event) | |
119 : LocatedEvent(native_event) { | |
120 if (type() == ET_MOUSE_PRESSED) | |
121 SetClickCount(GetRepeatCount(*this)); | |
122 } | |
123 | |
124 MouseEvent::MouseEvent(EventType type, | |
125 const gfx::Point& location, | |
126 const gfx::Point& root_location, | |
127 int flags) | |
128 : LocatedEvent(type, location, root_location, flags) { | |
129 } | |
130 | |
131 // static | |
132 bool MouseEvent::IsRepeatedClickEvent( | |
133 const MouseEvent& event1, | |
134 const MouseEvent& event2) { | |
135 // These values match the Windows defaults. | |
136 static const int kDoubleClickTimeMS = 500; | |
137 static const int kDoubleClickWidth = 4; | |
138 static const int kDoubleClickHeight = 4; | |
139 | |
140 if (event1.type() != ET_MOUSE_PRESSED || | |
141 event2.type() != ET_MOUSE_PRESSED) | |
142 return false; | |
143 | |
144 // Compare flags, but ignore EF_IS_DOUBLE_CLICK to allow triple clicks. | |
145 if ((event1.flags() & ~EF_IS_DOUBLE_CLICK) != | |
146 (event2.flags() & ~EF_IS_DOUBLE_CLICK)) | |
147 return false; | |
148 | |
149 base::TimeDelta time_difference = event2.time_stamp() - event1.time_stamp(); | |
150 | |
151 if (time_difference.InMilliseconds() > kDoubleClickTimeMS) | |
152 return false; | |
153 | |
154 if (abs(event2.x() - event1.x()) > kDoubleClickWidth / 2) | |
155 return false; | |
156 | |
157 if (abs(event2.y() - event1.y()) > kDoubleClickHeight / 2) | |
158 return false; | |
159 | |
160 return true; | |
161 } | |
162 | |
163 // static | |
164 int MouseEvent::GetRepeatCount(const MouseEvent& event) { | |
165 int click_count = 1; | |
166 if (last_click_event_) { | |
167 if (IsRepeatedClickEvent(*last_click_event_, event)) | |
168 click_count = last_click_event_->GetClickCount() + 1; | |
169 delete last_click_event_; | |
170 } | |
171 last_click_event_ = new MouseEvent(event.native_event()); | |
172 if (click_count > 3) | |
173 click_count = 3; | |
174 last_click_event_->SetClickCount(click_count); | |
175 return click_count; | |
176 } | |
177 | |
178 // static | |
179 MouseEvent* MouseEvent::last_click_event_ = NULL; | |
180 | |
181 int MouseEvent::GetClickCount() const { | |
182 if (type() != ET_MOUSE_PRESSED) | |
183 return 0; | |
184 | |
185 if (flags() & EF_IS_TRIPLE_CLICK) | |
186 return 3; | |
187 else if (flags() & EF_IS_DOUBLE_CLICK) | |
188 return 2; | |
189 else | |
190 return 1; | |
191 } | |
192 | |
193 void MouseEvent::SetClickCount(int click_count) { | |
194 if (type() != ET_MOUSE_PRESSED) | |
195 return; | |
196 | |
197 DCHECK(click_count > 0); | |
198 DCHECK(click_count <= 3); | |
199 | |
200 int f = flags(); | |
201 switch (click_count) { | |
202 case 1: | |
203 f &= ~EF_IS_DOUBLE_CLICK; | |
204 f &= ~EF_IS_TRIPLE_CLICK; | |
205 break; | |
206 case 2: | |
207 f |= EF_IS_DOUBLE_CLICK; | |
208 f &= ~EF_IS_TRIPLE_CLICK; | |
209 break; | |
210 case 3: | |
211 f &= ~EF_IS_DOUBLE_CLICK; | |
212 f |= EF_IS_TRIPLE_CLICK; | |
213 break; | |
214 } | |
215 set_flags(f); | |
216 } | |
217 | |
218 TouchEventImpl::TouchEventImpl(const base::NativeEvent& native_event) | |
219 : LocatedEvent(native_event), | |
220 touch_id_(ui::GetTouchId(native_event)), | |
221 radius_x_(GetTouchRadiusX(native_event)), | |
222 radius_y_(GetTouchRadiusY(native_event)), | |
223 rotation_angle_(GetTouchAngle(native_event)), | |
224 force_(GetTouchForce(native_event)) { | |
225 } | |
226 | |
227 TouchEventImpl::TouchEventImpl(EventType type, | |
228 const gfx::Point& location, | |
229 int touch_id, | |
230 base::TimeDelta time_stamp) | |
231 : LocatedEvent(type, location, location, 0), | |
232 touch_id_(touch_id), | |
233 radius_x_(0.0f), | |
234 radius_y_(0.0f), | |
235 rotation_angle_(0.0f), | |
236 force_(0.0f) { | |
237 set_time_stamp(time_stamp); | |
238 } | |
239 | |
240 TouchEventImpl::~TouchEventImpl() { | |
241 } | |
242 | |
243 void TouchEventImpl::UpdateForRootTransform(const Transform& root_transform) { | |
244 LocatedEvent::UpdateForRootTransform(root_transform); | |
245 gfx::Point3f scale; | |
246 InterpolatedTransform::FactorTRS(root_transform, NULL, NULL, &scale); | |
247 if (scale.x()) | |
248 radius_x_ /= scale.x(); | |
249 if (scale.y()) | |
250 radius_y_ /= scale.y(); | |
251 } | |
252 | |
253 EventType TouchEventImpl::GetEventType() const { | |
254 return type(); | |
255 } | |
256 | |
257 gfx::Point TouchEventImpl::GetLocation() const { | |
258 return location(); | |
259 } | |
260 | |
261 int TouchEventImpl::GetTouchId() const { | |
262 return touch_id_; | |
263 } | |
264 | |
265 int TouchEventImpl::GetEventFlags() const { | |
266 return flags(); | |
267 } | |
268 | |
269 base::TimeDelta TouchEventImpl::GetTimestamp() const { | |
270 return time_stamp(); | |
271 } | |
272 | |
273 float TouchEventImpl::RadiusX() const { | |
274 return radius_x_; | |
275 } | |
276 | |
277 float TouchEventImpl::RadiusY() const { | |
278 return radius_y_; | |
279 } | |
280 | |
281 float TouchEventImpl::RotationAngle() const { | |
282 return rotation_angle_; | |
283 } | |
284 | |
285 float TouchEventImpl::Force() const { | |
286 return force_; | |
287 } | |
288 | |
289 KeyEvent::KeyEvent(const base::NativeEvent& native_event, bool is_char) | |
290 : Event(native_event, | |
291 EventTypeFromNative(native_event), | |
292 EventFlagsFromNative(native_event)), | |
293 key_code_(KeyboardCodeFromNative(native_event)), | |
294 is_char_(is_char), | |
295 character_(0), | |
296 unmodified_character_(0) { | |
297 } | |
298 | |
299 KeyEvent::KeyEvent(EventType type, | |
300 KeyboardCode key_code, | |
301 int flags) | |
302 : Event(type, flags), | |
303 key_code_(key_code), | |
304 is_char_(false), | |
305 character_(GetCharacterFromKeyCode(key_code, flags)), | |
306 unmodified_character_(0) { | |
307 } | |
308 | |
309 uint16 KeyEvent::GetCharacter() const { | |
310 if (character_) | |
311 return character_; | |
312 | |
313 #if defined(OS_WIN) | |
314 return (native_event().message == WM_CHAR) ? key_code_ : | |
315 GetCharacterFromKeyCode(key_code_, flags()); | |
316 #elif defined(USE_X11) | |
317 if (!native_event()) | |
318 return GetCharacterFromKeyCode(key_code_, flags()); | |
319 | |
320 DCHECK(native_event()->type == KeyPress || | |
321 native_event()->type == KeyRelease); | |
322 | |
323 uint16 ch = 0; | |
324 if (!IsControlDown()) | |
325 ch = GetCharacterFromXEvent(native_event()); | |
326 return ch ? ch : GetCharacterFromKeyCode(key_code_, flags()); | |
327 #else | |
328 NOTIMPLEMENTED(); | |
329 return 0; | |
330 #endif | |
331 } | |
332 | |
333 uint16 KeyEvent::GetUnmodifiedCharacter() const { | |
334 if (unmodified_character_) | |
335 return unmodified_character_; | |
336 | |
337 #if defined(OS_WIN) | |
338 // Looks like there is no way to get unmodified character on Windows. | |
339 return (native_event().message == WM_CHAR) ? key_code_ : | |
340 GetCharacterFromKeyCode(key_code_, flags() & EF_SHIFT_DOWN); | |
341 #elif defined(USE_X11) | |
342 if (!native_event()) | |
343 return GetCharacterFromKeyCode(key_code_, flags() & EF_SHIFT_DOWN); | |
344 | |
345 DCHECK(native_event()->type == KeyPress || | |
346 native_event()->type == KeyRelease); | |
347 | |
348 static const unsigned int kIgnoredModifiers = ControlMask | LockMask | | |
349 Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask; | |
350 | |
351 XKeyEvent copy = native_event()->xkey; // bit-wise copy is safe. | |
352 // We can't use things like (native_event()->xkey.state & ShiftMask), as it | |
353 // may mask out bits used by X11 internally. | |
354 copy.state &= ~kIgnoredModifiers; | |
355 uint16 ch = GetCharacterFromXEvent(reinterpret_cast<XEvent*>(©)); | |
356 return ch ? ch : GetCharacterFromKeyCode(key_code_, flags() & EF_SHIFT_DOWN); | |
357 #else | |
358 NOTIMPLEMENTED(); | |
359 return 0; | |
360 #endif | |
361 } | |
362 | |
363 KeyEvent* KeyEvent::Copy() { | |
364 KeyEvent* copy = new KeyEvent(::CopyNativeEvent(native_event()), is_char()); | |
365 #if defined(USE_X11) | |
366 copy->set_delete_native_event(true); | |
367 #endif | |
368 return copy; | |
369 } | |
370 | |
371 TranslatedKeyEvent::TranslatedKeyEvent(const base::NativeEvent& native_event, | |
372 bool is_char) | |
373 : KeyEvent(native_event, is_char) { | |
374 set_type(type() == ET_KEY_PRESSED ? | |
375 ET_TRANSLATED_KEY_PRESS : ET_TRANSLATED_KEY_RELEASE); | |
376 } | |
377 | |
378 TranslatedKeyEvent::TranslatedKeyEvent(bool is_press, | |
379 KeyboardCode key_code, | |
380 int flags) | |
381 : KeyEvent((is_press ? ET_TRANSLATED_KEY_PRESS : ET_TRANSLATED_KEY_RELEASE), | |
382 key_code, | |
383 flags) { | |
384 } | |
385 | |
386 void TranslatedKeyEvent::ConvertToKeyEvent() { | |
387 set_type(type() == ET_TRANSLATED_KEY_PRESS ? | |
388 ET_KEY_PRESSED : ET_KEY_RELEASED); | |
389 } | |
390 | |
391 ScrollEvent::ScrollEvent(const base::NativeEvent& native_event) | |
392 : MouseEvent(native_event) { | |
393 if (type() == ET_SCROLL) { | |
394 GetScrollOffsets(native_event, &x_offset_, &y_offset_); | |
395 double start, end; | |
396 GetGestureTimes(native_event, &start, &end); | |
397 } else if (type() == ET_SCROLL_FLING_START) { | |
398 bool is_cancel; | |
399 GetFlingData(native_event, &x_offset_, &y_offset_, &is_cancel); | |
400 } | |
401 } | |
402 | |
403 GestureEventImpl::GestureEventImpl(EventType type, | |
404 int x, | |
405 int y, | |
406 int flags, | |
407 base::Time time_stamp, | |
408 const GestureEventDetails& details, | |
409 unsigned int touch_ids_bitfield) | |
410 : LocatedEvent(type, gfx::Point(x, y), gfx::Point(x, y), flags), | |
411 details_(details), | |
412 touch_ids_bitfield_(touch_ids_bitfield) { | |
413 set_time_stamp(base::TimeDelta::FromSeconds(time_stamp.ToDoubleT())); | |
414 } | |
415 | |
416 GestureEventImpl::~GestureEventImpl() { | |
417 } | |
418 | |
419 int GestureEventImpl::GetLowestTouchId() const { | |
420 if (touch_ids_bitfield_ == 0) | |
421 return -1; | |
422 int i = -1; | |
423 // Find the index of the least significant 1 bit | |
424 while (!(1 << ++i & touch_ids_bitfield_)); | |
425 return i; | |
426 } | |
427 | |
428 } // namespace ui | |
OLD | NEW |