| 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 "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "ui/display/display.h" | 22 #include "ui/display/display.h" |
| 22 #include "ui/display/screen.h" | 23 #include "ui/display/screen.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" |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 if (!start_time) | 300 if (!start_time) |
| 300 start_time = &start_time_; | 301 start_time = &start_time_; |
| 301 if (!end_time) | 302 if (!end_time) |
| 302 end_time = &end_time_; | 303 end_time = &end_time_; |
| 303 | 304 |
| 304 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, | 305 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, |
| 305 end_time); | 306 end_time); |
| 306 return true; | 307 return true; |
| 307 } | 308 } |
| 308 | 309 |
| 310 namespace { |
| 309 int64_t g_last_seen_timestamp_ms = 0; | 311 int64_t g_last_seen_timestamp_ms = 0; |
| 310 // accumulated rollover time. | |
| 311 int64_t g_rollover_ms = 0; | 312 int64_t g_rollover_ms = 0; |
| 313 bool g_bogus_x11_timestamps = false; |
| 312 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = | 314 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = |
| 313 LAZY_INSTANCE_INITIALIZER; | 315 LAZY_INSTANCE_INITIALIZER; |
| 314 | 316 |
| 317 } // namespace |
| 318 |
| 315 // Takes Xlib Time and returns a time delta that is immune to timer rollover. | 319 // Takes Xlib Time and returns a time delta that is immune to timer rollover. |
| 316 // This function is not thread safe as we do not use a lock. | 320 // This function is not thread safe as we do not use a lock. |
| 317 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { | 321 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { |
| 318 int64_t timestamp64 = timestamp; | 322 int64_t timestamp64 = timestamp; |
| 319 | 323 |
| 320 if (!timestamp) | 324 if (!timestamp) |
| 321 return base::TimeDelta(); | 325 return base::TimeDelta(); |
| 322 | 326 |
| 327 if (g_bogus_x11_timestamps) { |
| 328 return base::TimeDelta::FromInternalValue( |
| 329 base::TimeTicks::Now().ToInternalValue()); |
| 330 } |
| 331 |
| 323 // If this is the first event that we get, assume the time stamp roll-over | 332 // If this is the first event that we get, assume the time stamp roll-over |
| 324 // might have happened before the process was started. | 333 // might have happened before the process was started. |
| 325 // Register a rollover if the distance between last timestamp and current one | 334 // 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 | 335 // 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. | 336 // where X server delivers reasonably close events out-of-order. |
| 328 bool had_recent_rollover = | 337 bool had_recent_rollover = |
| 329 !g_last_seen_timestamp_ms || | 338 !g_last_seen_timestamp_ms || |
| 330 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); | 339 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); |
| 331 | 340 |
| 332 g_last_seen_timestamp_ms = timestamp64; | 341 g_last_seen_timestamp_ms = timestamp64; |
| 333 if (!had_recent_rollover) | 342 if (!had_recent_rollover) |
| 334 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); | 343 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); |
| 335 | 344 |
| 336 DCHECK(timestamp64 <= UINT32_MAX) | 345 DCHECK(timestamp64 <= UINT32_MAX) |
| 337 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; | 346 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; |
| 338 | 347 |
| 339 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr | 348 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr |
| 340 ? g_tick_clock.Get()->NowTicks() | 349 ? g_tick_clock.Get()->NowTicks() |
| 341 : base::TimeTicks::Now(); | 350 : base::TimeTicks::Now(); |
| 342 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); | 351 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); |
| 343 | 352 |
| 344 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); | 353 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); |
| 345 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); | 354 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 | 355 if (!g_tick_clock.Get()) { |
| 347 // come through with real timestamps. | 356 if (delta > 60 * 1000) { |
| 348 DCHECK(delta < 60 * 1000 || g_tick_clock.Get() != nullptr) | 357 // x11 timestamps don't seem to be using the same time base as TimeTicks, |
| 349 << "Unexpected X11 event time, now: " << now_ticks | 358 // so ignore them altogether and always use current time instead. |
| 350 << " event at: " << timestamp; | 359 delta = 0; |
| 360 g_bogus_x11_timestamps = true; |
| 361 LOG(WARNING) |
| 362 << "Unexpected x11 timestamps, will use browser time instead."; |
| 363 } |
| 364 UMA_HISTOGRAM_BOOLEAN("Event.TimestampHasValidTimebase", |
| 365 !g_bogus_x11_timestamps); |
| 366 } |
| 367 |
| 351 return base::TimeDelta::FromMilliseconds(now_ms - delta); | 368 return base::TimeDelta::FromMilliseconds(now_ms - delta); |
| 352 } | 369 } |
| 353 | 370 |
| 354 } // namespace | 371 } // namespace |
| 355 | 372 |
| 356 namespace ui { | 373 namespace ui { |
| 357 | 374 |
| 358 EventType EventTypeFromXEvent(const XEvent& xev) { | 375 EventType EventTypeFromXEvent(const XEvent& xev) { |
| 359 // Allow the DeviceDataManager to block the event. If blocked return | 376 // Allow the DeviceDataManager to block the event. If blocked return |
| 360 // ET_UNKNOWN as the type so this event will not be further processed. | 377 // ET_UNKNOWN as the type so this event will not be further processed. |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 806 | 823 |
| 807 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, | 824 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, |
| 808 vy_ordinal, is_cancel); | 825 vy_ordinal, is_cancel); |
| 809 return true; | 826 return true; |
| 810 } | 827 } |
| 811 | 828 |
| 812 void ResetTimestampRolloverCountersForTesting( | 829 void ResetTimestampRolloverCountersForTesting( |
| 813 std::unique_ptr<base::TickClock> tick_clock) { | 830 std::unique_ptr<base::TickClock> tick_clock) { |
| 814 g_last_seen_timestamp_ms = 0; | 831 g_last_seen_timestamp_ms = 0; |
| 815 g_rollover_ms = 0; | 832 g_rollover_ms = 0; |
| 833 g_bogus_x11_timestamps = false; |
| 816 g_tick_clock.Get() = std::move(tick_clock); | 834 g_tick_clock.Get() = std::move(tick_clock); |
| 817 } | 835 } |
| 818 | 836 |
| 819 } // namespace ui | 837 } // namespace ui |
| OLD | NEW |