Chromium Code Reviews| 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" |
| 27 #include "ui/events/event_utils.h" | |
| 26 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 28 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
| 27 #include "ui/gfx/geometry/point.h" | 29 #include "ui/gfx/geometry/point.h" |
| 28 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
| 29 #include "ui/gfx/x/x11_atom_cache.h" | 31 #include "ui/gfx/x/x11_atom_cache.h" |
| 30 #include "ui/gfx/x/x11_types.h" | 32 #include "ui/gfx/x/x11_types.h" |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 // Scroll amount for each wheelscroll event. 53 is also the value used for GTK+. | 36 // Scroll amount for each wheelscroll event. 53 is also the value used for GTK+. |
| 35 const int kWheelScrollAmount = 53; | 37 const int kWheelScrollAmount = 53; |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 if (!start_time) | 301 if (!start_time) |
| 300 start_time = &start_time_; | 302 start_time = &start_time_; |
| 301 if (!end_time) | 303 if (!end_time) |
| 302 end_time = &end_time_; | 304 end_time = &end_time_; |
| 303 | 305 |
| 304 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, | 306 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, |
| 305 end_time); | 307 end_time); |
| 306 return true; | 308 return true; |
| 307 } | 309 } |
| 308 | 310 |
| 311 namespace { | |
| 309 int64_t g_last_seen_timestamp_ms = 0; | 312 int64_t g_last_seen_timestamp_ms = 0; |
| 310 // accumulated rollover time. | |
| 311 int64_t g_rollover_ms = 0; | 313 int64_t g_rollover_ms = 0; |
| 314 bool g_bogus_x11_timestamps = false; | |
| 312 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = | 315 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = |
| 313 LAZY_INSTANCE_INITIALIZER; | 316 LAZY_INSTANCE_INITIALIZER; |
| 314 | 317 |
| 318 enum UMAEventTimestampValidity { | |
| 319 UMA_EVENT_TIMESTAMPS_HAVE_INVALID_TIMEBASE, | |
| 320 UMA_EVENT_TIMESTAMPS_HAVE_VALID_TIMEBASE | |
| 321 }; | |
| 322 | |
| 323 } // namespace | |
| 324 | |
| 315 // Takes Xlib Time and returns a time delta that is immune to timer rollover. | 325 // 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. | 326 // This function is not thread safe as we do not use a lock. |
| 317 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { | 327 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { |
| 318 int64_t timestamp64 = timestamp; | 328 int64_t timestamp64 = timestamp; |
| 319 | 329 |
| 320 if (!timestamp) | 330 if (!timestamp) |
| 321 return base::TimeDelta(); | 331 return base::TimeDelta(); |
| 322 | 332 |
| 333 if (g_bogus_x11_timestamps) | |
| 334 return ui::EventTimeForNow(); | |
| 335 | |
| 323 // If this is the first event that we get, assume the time stamp roll-over | 336 // If this is the first event that we get, assume the time stamp roll-over |
| 324 // might have happened before the process was started. | 337 // might have happened before the process was started. |
| 325 // Register a rollover if the distance between last timestamp and current one | 338 // 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 | 339 // 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. | 340 // where X server delivers reasonably close events out-of-order. |
| 328 bool had_recent_rollover = | 341 bool had_recent_rollover = |
| 329 !g_last_seen_timestamp_ms || | 342 !g_last_seen_timestamp_ms || |
| 330 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); | 343 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); |
| 331 | 344 |
| 332 g_last_seen_timestamp_ms = timestamp64; | 345 g_last_seen_timestamp_ms = timestamp64; |
| 333 if (!had_recent_rollover) | 346 if (!had_recent_rollover) |
| 334 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); | 347 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); |
| 335 | 348 |
| 336 DCHECK(timestamp64 <= UINT32_MAX) | 349 DCHECK(timestamp64 <= UINT32_MAX) |
| 337 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; | 350 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; |
| 338 | 351 |
| 339 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr | 352 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr |
| 340 ? g_tick_clock.Get()->NowTicks() | 353 ? g_tick_clock.Get()->NowTicks() |
| 341 : base::TimeTicks::Now(); | 354 : base::TimeTicks::Now(); |
| 342 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); | 355 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); |
| 343 | 356 |
| 344 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); | 357 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); |
| 345 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); | 358 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 | 359 if (!g_tick_clock.Get()) { |
| 347 // come through with real timestamps. | 360 if (delta > 60 * 1000) { |
| 348 DCHECK(delta < 60 * 1000 || g_tick_clock.Get() != nullptr) | 361 // x11 timestamps don't seem to be using the same time base as TimeTicks, |
| 349 << "Unexpected X11 event time, now: " << now_ticks | 362 // so ignore them altogether and always use current time instead. |
| 350 << " event at: " << timestamp; | 363 delta = 0; |
| 364 g_bogus_x11_timestamps = true; | |
| 365 LOG(WARNING) | |
| 366 << "Unexpected x11 timestamps, will use browser time instead."; | |
| 367 } | |
| 368 UMA_HISTOGRAM_BOOLEAN("Event.TimestampHasValidTimebase", | |
| 369 g_bogus_x11_timestamps | |
| 370 ? UMA_EVENT_TIMESTAMPS_HAVE_INVALID_TIMEBASE | |
| 371 : UMA_EVENT_TIMESTAMPS_HAVE_VALID_TIMEBASE); | |
|
Ilya Sherman
2016/05/23 18:36:33
nit: No need to use an enum in the C++ code -- you
| |
| 372 } | |
| 373 | |
| 351 return base::TimeDelta::FromMilliseconds(now_ms - delta); | 374 return base::TimeDelta::FromMilliseconds(now_ms - delta); |
| 352 } | 375 } |
| 353 | 376 |
| 354 } // namespace | 377 } // namespace |
| 355 | 378 |
| 356 namespace ui { | 379 namespace ui { |
| 357 | 380 |
| 358 EventType EventTypeFromXEvent(const XEvent& xev) { | 381 EventType EventTypeFromXEvent(const XEvent& xev) { |
| 359 // Allow the DeviceDataManager to block the event. If blocked return | 382 // Allow the DeviceDataManager to block the event. If blocked return |
| 360 // ET_UNKNOWN as the type so this event will not be further processed. | 383 // 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 | 829 |
| 807 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, | 830 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, |
| 808 vy_ordinal, is_cancel); | 831 vy_ordinal, is_cancel); |
| 809 return true; | 832 return true; |
| 810 } | 833 } |
| 811 | 834 |
| 812 void ResetTimestampRolloverCountersForTesting( | 835 void ResetTimestampRolloverCountersForTesting( |
| 813 std::unique_ptr<base::TickClock> tick_clock) { | 836 std::unique_ptr<base::TickClock> tick_clock) { |
| 814 g_last_seen_timestamp_ms = 0; | 837 g_last_seen_timestamp_ms = 0; |
| 815 g_rollover_ms = 0; | 838 g_rollover_ms = 0; |
| 839 g_bogus_x11_timestamps = false; | |
| 816 g_tick_clock.Get() = std::move(tick_clock); | 840 g_tick_clock.Get() = std::move(tick_clock); |
| 817 } | 841 } |
| 818 | 842 |
| 819 } // namespace ui | 843 } // namespace ui |
| OLD | NEW |