| 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 "base/metrics/histogram_macros.h" | 20 #include "base/metrics/histogram_macros.h" |
| 21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 22 #include "ui/display/display.h" | 22 #include "ui/display/display.h" |
| 23 #include "ui/display/screen.h" | 23 #include "ui/display/screen.h" |
| 24 #include "ui/events/base_event_utils.h" |
| 24 #include "ui/events/devices/x11/device_data_manager_x11.h" | 25 #include "ui/events/devices/x11/device_data_manager_x11.h" |
| 25 #include "ui/events/devices/x11/device_list_cache_x11.h" | 26 #include "ui/events/devices/x11/device_list_cache_x11.h" |
| 26 #include "ui/events/devices/x11/touch_factory_x11.h" | 27 #include "ui/events/devices/x11/touch_factory_x11.h" |
| 27 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 28 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
| 28 #include "ui/gfx/geometry/point.h" | 29 #include "ui/gfx/geometry/point.h" |
| 29 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
| 30 #include "ui/gfx/x/x11_atom_cache.h" | 31 #include "ui/gfx/x/x11_atom_cache.h" |
| 31 #include "ui/gfx/x/x11_types.h" | 32 #include "ui/gfx/x/x11_types.h" |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 if (!start_time) | 301 if (!start_time) |
| 301 start_time = &start_time_; | 302 start_time = &start_time_; |
| 302 if (!end_time) | 303 if (!end_time) |
| 303 end_time = &end_time_; | 304 end_time = &end_time_; |
| 304 | 305 |
| 305 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, | 306 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, |
| 306 end_time); | 307 end_time); |
| 307 return true; | 308 return true; |
| 308 } | 309 } |
| 309 | 310 |
| 310 namespace { | |
| 311 int64_t g_last_seen_timestamp_ms = 0; | 311 int64_t g_last_seen_timestamp_ms = 0; |
| 312 int64_t g_rollover_ms = 0; | 312 int64_t g_rollover_ms = 0; |
| 313 bool g_bogus_x11_timestamps = false; | 313 bool g_bogus_x11_timestamps = false; |
| 314 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = | 314 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = |
| 315 LAZY_INSTANCE_INITIALIZER; | 315 LAZY_INSTANCE_INITIALIZER; |
| 316 | 316 |
| 317 } // namespace | |
| 318 | |
| 319 // Takes Xlib Time and returns a time delta that is immune to timer rollover. | 317 // Takes Xlib Time and returns a time delta that is immune to timer rollover. |
| 320 // This function is not thread safe as we do not use a lock. | 318 // This function is not thread safe as we do not use a lock. |
| 321 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { | 319 base::TimeTicks TimeTicksFromXEventTime(Time timestamp) { |
| 322 int64_t timestamp64 = timestamp; | 320 int64_t timestamp64 = timestamp; |
| 323 | 321 |
| 324 if (!timestamp) | 322 if (!timestamp) |
| 325 return base::TimeDelta(); | 323 return base::TimeTicks(); |
| 326 | 324 |
| 327 if (g_bogus_x11_timestamps) { | 325 if (g_bogus_x11_timestamps) |
| 328 return base::TimeDelta::FromInternalValue( | 326 return base::TimeTicks::Now(); |
| 329 base::TimeTicks::Now().ToInternalValue()); | |
| 330 } | |
| 331 | 327 |
| 332 // If this is the first event that we get, assume the time stamp roll-over | 328 // If this is the first event that we get, assume the time stamp roll-over |
| 333 // might have happened before the process was started. | 329 // might have happened before the process was started. |
| 334 // Register a rollover if the distance between last timestamp and current one | 330 // Register a rollover if the distance between last timestamp and current one |
| 335 // is larger than half the width. This avoids false rollovers even in a case | 331 // is larger than half the width. This avoids false rollovers even in a case |
| 336 // where X server delivers reasonably close events out-of-order. | 332 // where X server delivers reasonably close events out-of-order. |
| 337 bool had_recent_rollover = | 333 bool had_recent_rollover = |
| 338 !g_last_seen_timestamp_ms || | 334 !g_last_seen_timestamp_ms || |
| 339 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); | 335 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); |
| 340 | 336 |
| 341 g_last_seen_timestamp_ms = timestamp64; | 337 g_last_seen_timestamp_ms = timestamp64; |
| 342 if (!had_recent_rollover) | 338 if (!had_recent_rollover) |
| 343 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); | 339 return base::TimeTicks() + |
| 340 base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); |
| 344 | 341 |
| 345 DCHECK(timestamp64 <= UINT32_MAX) | 342 DCHECK(timestamp64 <= UINT32_MAX) |
| 346 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; | 343 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; |
| 347 | 344 |
| 348 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr | 345 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr |
| 349 ? g_tick_clock.Get()->NowTicks() | 346 ? g_tick_clock.Get()->NowTicks() |
| 350 : base::TimeTicks::Now(); | 347 : base::TimeTicks::Now(); |
| 351 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); | 348 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); |
| 352 | 349 |
| 353 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); | 350 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); |
| 354 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); | 351 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); |
| 355 if (!g_tick_clock.Get()) { | 352 if (!g_tick_clock.Get()) { |
| 356 if (delta > 60 * 1000) { | 353 if (delta > 60 * 1000) { |
| 357 // x11 timestamps don't seem to be using the same time base as TimeTicks, | 354 // x11 timestamps don't seem to be using the same time base as TimeTicks, |
| 358 // so ignore them altogether and always use current time instead. | 355 // so ignore them altogether and always use current time instead. |
| 359 delta = 0; | 356 delta = 0; |
| 360 g_bogus_x11_timestamps = true; | 357 g_bogus_x11_timestamps = true; |
| 361 LOG(WARNING) | 358 LOG(WARNING) |
| 362 << "Unexpected x11 timestamps, will use browser time instead."; | 359 << "Unexpected x11 timestamps, will use browser time instead."; |
| 363 } | 360 } |
| 364 UMA_HISTOGRAM_BOOLEAN("Event.TimestampHasValidTimebase", | 361 UMA_HISTOGRAM_BOOLEAN("Event.TimestampHasValidTimebase", |
| 365 !g_bogus_x11_timestamps); | 362 !g_bogus_x11_timestamps); |
| 366 } | 363 } |
| 367 | 364 |
| 368 return base::TimeDelta::FromMilliseconds(now_ms - delta); | 365 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(now_ms - delta); |
| 369 } | 366 } |
| 370 | 367 |
| 371 } // namespace | 368 } // namespace |
| 372 | 369 |
| 373 namespace ui { | 370 namespace ui { |
| 374 | 371 |
| 375 EventType EventTypeFromXEvent(const XEvent& xev) { | 372 EventType EventTypeFromXEvent(const XEvent& xev) { |
| 376 // Allow the DeviceDataManager to block the event. If blocked return | 373 // Allow the DeviceDataManager to block the event. If blocked return |
| 377 // ET_UNKNOWN as the type so this event will not be further processed. | 374 // ET_UNKNOWN as the type so this event will not be further processed. |
| 378 // NOTE: During some events unittests there is no device data manager. | 375 // NOTE: During some events unittests there is no device data manager. |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 case XI_KeyRelease: { | 532 case XI_KeyRelease: { |
| 536 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); | 533 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); |
| 537 return GetEventFlagsFromXGenericEvent(xev); | 534 return GetEventFlagsFromXGenericEvent(xev); |
| 538 } | 535 } |
| 539 } | 536 } |
| 540 } | 537 } |
| 541 } | 538 } |
| 542 return 0; | 539 return 0; |
| 543 } | 540 } |
| 544 | 541 |
| 545 base::TimeDelta EventTimeFromXEvent(const XEvent& xev) { | 542 base::TimeTicks EventTimeFromXEvent(const XEvent& xev) { |
| 546 switch (xev.type) { | 543 switch (xev.type) { |
| 547 case KeyPress: | 544 case KeyPress: |
| 548 case KeyRelease: | 545 case KeyRelease: |
| 549 return TimeDeltaFromXEventTime(xev.xkey.time); | 546 return TimeTicksFromXEventTime(xev.xkey.time); |
| 550 case ButtonPress: | 547 case ButtonPress: |
| 551 case ButtonRelease: | 548 case ButtonRelease: |
| 552 return TimeDeltaFromXEventTime(xev.xbutton.time); | 549 return TimeTicksFromXEventTime(xev.xbutton.time); |
| 553 break; | 550 break; |
| 554 case MotionNotify: | 551 case MotionNotify: |
| 555 return TimeDeltaFromXEventTime(xev.xmotion.time); | 552 return TimeTicksFromXEventTime(xev.xmotion.time); |
| 556 break; | 553 break; |
| 557 case EnterNotify: | 554 case EnterNotify: |
| 558 case LeaveNotify: | 555 case LeaveNotify: |
| 559 return TimeDeltaFromXEventTime(xev.xcrossing.time); | 556 return TimeTicksFromXEventTime(xev.xcrossing.time); |
| 560 break; | 557 break; |
| 561 case GenericEvent: { | 558 case GenericEvent: { |
| 562 double start, end; | 559 double start, end; |
| 563 double touch_timestamp; | 560 double touch_timestamp; |
| 564 if (GetGestureTimes(xev, &start, &end)) { | 561 if (GetGestureTimes(xev, &start, &end)) { |
| 565 // If the driver supports gesture times, use them. | 562 // If the driver supports gesture times, use them. |
| 566 return base::TimeDelta::FromMicroseconds(end * 1000000); | 563 return ui::EventTimeStampFromSeconds(end); |
| 567 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( | 564 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( |
| 568 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, | 565 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, |
| 569 &touch_timestamp)) { | 566 &touch_timestamp)) { |
| 570 return base::TimeDelta::FromMicroseconds(touch_timestamp * 1000000); | 567 return ui::EventTimeStampFromSeconds(touch_timestamp); |
| 571 } else { | 568 } else { |
| 572 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); | 569 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); |
| 573 return TimeDeltaFromXEventTime(xide->time); | 570 return TimeTicksFromXEventTime(xide->time); |
| 574 } | 571 } |
| 575 break; | 572 break; |
| 576 } | 573 } |
| 577 } | 574 } |
| 578 NOTREACHED(); | 575 NOTREACHED(); |
| 579 return base::TimeDelta(); | 576 return base::TimeTicks(); |
| 580 } | 577 } |
| 581 | 578 |
| 582 gfx::Point EventLocationFromXEvent(const XEvent& xev) { | 579 gfx::Point EventLocationFromXEvent(const XEvent& xev) { |
| 583 switch (xev.type) { | 580 switch (xev.type) { |
| 584 case EnterNotify: | 581 case EnterNotify: |
| 585 case LeaveNotify: | 582 case LeaveNotify: |
| 586 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); | 583 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); |
| 587 case ButtonPress: | 584 case ButtonPress: |
| 588 case ButtonRelease: | 585 case ButtonRelease: |
| 589 return gfx::Point(xev.xbutton.x, xev.xbutton.y); | 586 return gfx::Point(xev.xbutton.x, xev.xbutton.y); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 | 825 |
| 829 void ResetTimestampRolloverCountersForTesting( | 826 void ResetTimestampRolloverCountersForTesting( |
| 830 std::unique_ptr<base::TickClock> tick_clock) { | 827 std::unique_ptr<base::TickClock> tick_clock) { |
| 831 g_last_seen_timestamp_ms = 0; | 828 g_last_seen_timestamp_ms = 0; |
| 832 g_rollover_ms = 0; | 829 g_rollover_ms = 0; |
| 833 g_bogus_x11_timestamps = false; | 830 g_bogus_x11_timestamps = false; |
| 834 g_tick_clock.Get() = std::move(tick_clock); | 831 g_tick_clock.Get() = std::move(tick_clock); |
| 835 } | 832 } |
| 836 | 833 |
| 837 } // namespace ui | 834 } // namespace ui |
| OLD | NEW |