OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/x/events_x_utils.h" | 5 #include "ui/events/x/events_x_utils.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <X11/extensions/XInput.h> | 9 #include <X11/extensions/XInput.h> |
10 #include <X11/extensions/XInput2.h> | 10 #include <X11/extensions/XInput2.h> |
11 #include <X11/XKBlib.h> | 11 #include <X11/XKBlib.h> |
12 #include <X11/Xlib.h> | 12 #include <X11/Xlib.h> |
13 #include <X11/Xutil.h> | 13 #include <X11/Xutil.h> |
14 #include <cmath> | 14 #include <cmath> |
15 | 15 |
16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/singleton.h" | 19 #include "base/memory/singleton.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "ui/display/display.h" | 21 #include "ui/display/display.h" |
22 #include "ui/display/screen.h" | 22 #include "ui/display/screen.h" |
| 23 #include "ui/events/base_event_utils.h" |
23 #include "ui/events/devices/x11/device_data_manager_x11.h" | 24 #include "ui/events/devices/x11/device_data_manager_x11.h" |
24 #include "ui/events/devices/x11/device_list_cache_x11.h" | 25 #include "ui/events/devices/x11/device_list_cache_x11.h" |
25 #include "ui/events/devices/x11/touch_factory_x11.h" | 26 #include "ui/events/devices/x11/touch_factory_x11.h" |
26 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 27 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
27 #include "ui/gfx/geometry/point.h" | 28 #include "ui/gfx/geometry/point.h" |
28 #include "ui/gfx/geometry/rect.h" | 29 #include "ui/gfx/geometry/rect.h" |
29 #include "ui/gfx/x/x11_atom_cache.h" | 30 #include "ui/gfx/x/x11_atom_cache.h" |
30 #include "ui/gfx/x/x11_types.h" | 31 #include "ui/gfx/x/x11_types.h" |
31 | 32 |
32 namespace { | 33 namespace { |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 end_time); | 306 end_time); |
306 return true; | 307 return true; |
307 } | 308 } |
308 | 309 |
309 int64_t g_last_seen_timestamp_ms = 0; | 310 int64_t g_last_seen_timestamp_ms = 0; |
310 // accumulated rollover time. | 311 // accumulated rollover time. |
311 int64_t g_rollover_ms = 0; | 312 int64_t g_rollover_ms = 0; |
312 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = | 313 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = |
313 LAZY_INSTANCE_INITIALIZER; | 314 LAZY_INSTANCE_INITIALIZER; |
314 | 315 |
315 // Takes Xlib Time and returns a time delta that is immune to timer rollover. | 316 // Takes Xlib Time and returns a time ticks that is immune to timer rollover. |
316 // This function is not thread safe as we do not use a lock. | 317 // This function is not thread safe as we do not use a lock. |
317 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { | 318 base::TimeTicks TimeTicksFromXEventTime(Time timestamp) { |
318 int64_t timestamp64 = timestamp; | 319 int64_t timestamp64 = timestamp; |
319 | 320 |
320 if (!timestamp) | 321 if (!timestamp) |
321 return base::TimeDelta(); | 322 return base::TimeTicks(); |
322 | 323 |
323 // If this is the first event that we get, assume the time stamp roll-over | 324 // If this is the first event that we get, assume the time stamp roll-over |
324 // might have happened before the process was started. | 325 // might have happened before the process was started. |
325 // Register a rollover if the distance between last timestamp and current one | 326 // Register a rollover if the distance between last timestamp and current one |
326 // is larger than half the width. This avoids false rollovers even in a case | 327 // is larger than half the width. This avoids false rollovers even in a case |
327 // where X server delivers reasonably close events out-of-order. | 328 // where X server delivers reasonably close events out-of-order. |
328 bool had_recent_rollover = | 329 bool had_recent_rollover = |
329 !g_last_seen_timestamp_ms || | 330 !g_last_seen_timestamp_ms || |
330 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); | 331 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); |
331 | 332 |
332 g_last_seen_timestamp_ms = timestamp64; | 333 g_last_seen_timestamp_ms = timestamp64; |
333 if (!had_recent_rollover) | 334 if (!had_recent_rollover) |
334 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); | 335 return base::TimeTicks() + |
| 336 base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); |
335 | 337 |
336 DCHECK(timestamp64 <= UINT32_MAX) | 338 DCHECK(timestamp64 <= UINT32_MAX) |
337 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; | 339 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; |
338 | 340 |
339 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr | 341 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr |
340 ? g_tick_clock.Get()->NowTicks() | 342 ? g_tick_clock.Get()->NowTicks() |
341 : base::TimeTicks::Now(); | 343 : base::TimeTicks::Now(); |
342 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); | 344 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); |
343 | 345 |
344 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); | 346 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); |
345 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); | 347 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); |
346 // If using a mock clock, all bets are off -- in some tests, actual X11 events | 348 // If using a mock clock, all bets are off -- in some tests, actual X11 events |
347 // come through with real timestamps. | 349 // come through with real timestamps. |
348 DCHECK(delta < 60 * 1000 || g_tick_clock.Get() != nullptr) | 350 DCHECK(delta < 60 * 1000 || g_tick_clock.Get() != nullptr) |
349 << "Unexpected X11 event time, now: " << now_ticks | 351 << "Unexpected X11 event time, now: " << now_ticks |
350 << " event at: " << timestamp; | 352 << " event at: " << timestamp; |
351 return base::TimeDelta::FromMilliseconds(now_ms - delta); | 353 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(now_ms - delta); |
352 } | 354 } |
353 | 355 |
354 } // namespace | 356 } // namespace |
355 | 357 |
356 namespace ui { | 358 namespace ui { |
357 | 359 |
358 EventType EventTypeFromXEvent(const XEvent& xev) { | 360 EventType EventTypeFromXEvent(const XEvent& xev) { |
359 // Allow the DeviceDataManager to block the event. If blocked return | 361 // Allow the DeviceDataManager to block the event. If blocked return |
360 // ET_UNKNOWN as the type so this event will not be further processed. | 362 // ET_UNKNOWN as the type so this event will not be further processed. |
361 // NOTE: During some events unittests there is no device data manager. | 363 // NOTE: During some events unittests there is no device data manager. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 case XI_KeyRelease: { | 520 case XI_KeyRelease: { |
519 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); | 521 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); |
520 return GetEventFlagsFromXGenericEvent(xev); | 522 return GetEventFlagsFromXGenericEvent(xev); |
521 } | 523 } |
522 } | 524 } |
523 } | 525 } |
524 } | 526 } |
525 return 0; | 527 return 0; |
526 } | 528 } |
527 | 529 |
528 base::TimeDelta EventTimeFromXEvent(const XEvent& xev) { | 530 base::TimeTicks EventTimeFromXEvent(const XEvent& xev) { |
529 switch (xev.type) { | 531 switch (xev.type) { |
530 case KeyPress: | 532 case KeyPress: |
531 case KeyRelease: | 533 case KeyRelease: |
532 return TimeDeltaFromXEventTime(xev.xkey.time); | 534 return TimeTicksFromXEventTime(xev.xkey.time); |
533 case ButtonPress: | 535 case ButtonPress: |
534 case ButtonRelease: | 536 case ButtonRelease: |
535 return TimeDeltaFromXEventTime(xev.xbutton.time); | 537 return TimeTicksFromXEventTime(xev.xbutton.time); |
536 break; | 538 break; |
537 case MotionNotify: | 539 case MotionNotify: |
538 return TimeDeltaFromXEventTime(xev.xmotion.time); | 540 return TimeTicksFromXEventTime(xev.xmotion.time); |
539 break; | 541 break; |
540 case EnterNotify: | 542 case EnterNotify: |
541 case LeaveNotify: | 543 case LeaveNotify: |
542 return TimeDeltaFromXEventTime(xev.xcrossing.time); | 544 return TimeTicksFromXEventTime(xev.xcrossing.time); |
543 break; | 545 break; |
544 case GenericEvent: { | 546 case GenericEvent: { |
545 double start, end; | 547 double start, end; |
546 double touch_timestamp; | 548 double touch_timestamp; |
547 if (GetGestureTimes(xev, &start, &end)) { | 549 if (GetGestureTimes(xev, &start, &end)) { |
548 // If the driver supports gesture times, use them. | 550 // If the driver supports gesture times, use them. |
549 return base::TimeDelta::FromMicroseconds(end * 1000000); | 551 return ui::EventTimeStampFromSeconds(end); |
550 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( | 552 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( |
551 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, | 553 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, |
552 &touch_timestamp)) { | 554 &touch_timestamp)) { |
553 return base::TimeDelta::FromMicroseconds(touch_timestamp * 1000000); | 555 return ui::EventTimeStampFromSeconds(touch_timestamp); |
554 } else { | 556 } else { |
555 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); | 557 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); |
556 return TimeDeltaFromXEventTime(xide->time); | 558 return TimeTicksFromXEventTime(xide->time); |
557 } | 559 } |
558 break; | 560 break; |
559 } | 561 } |
560 } | 562 } |
561 NOTREACHED(); | 563 NOTREACHED(); |
562 return base::TimeDelta(); | 564 return base::TimeTicks(); |
563 } | 565 } |
564 | 566 |
565 gfx::Point EventLocationFromXEvent(const XEvent& xev) { | 567 gfx::Point EventLocationFromXEvent(const XEvent& xev) { |
566 switch (xev.type) { | 568 switch (xev.type) { |
567 case EnterNotify: | 569 case EnterNotify: |
568 case LeaveNotify: | 570 case LeaveNotify: |
569 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); | 571 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); |
570 case ButtonPress: | 572 case ButtonPress: |
571 case ButtonRelease: | 573 case ButtonRelease: |
572 return gfx::Point(xev.xbutton.x, xev.xbutton.y); | 574 return gfx::Point(xev.xbutton.x, xev.xbutton.y); |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 } | 812 } |
811 | 813 |
812 void ResetTimestampRolloverCountersForTesting( | 814 void ResetTimestampRolloverCountersForTesting( |
813 std::unique_ptr<base::TickClock> tick_clock) { | 815 std::unique_ptr<base::TickClock> tick_clock) { |
814 g_last_seen_timestamp_ms = 0; | 816 g_last_seen_timestamp_ms = 0; |
815 g_rollover_ms = 0; | 817 g_rollover_ms = 0; |
816 g_tick_clock.Get() = std::move(tick_clock); | 818 g_tick_clock.Get() = std::move(tick_clock); |
817 } | 819 } |
818 | 820 |
819 } // namespace ui | 821 } // namespace ui |
OLD | NEW |