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> |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 if (!start_time) | 299 if (!start_time) |
| 300 start_time = &start_time_; | 300 start_time = &start_time_; |
| 301 if (!end_time) | 301 if (!end_time) |
| 302 end_time = &end_time_; | 302 end_time = &end_time_; |
| 303 | 303 |
| 304 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, | 304 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, |
| 305 end_time); | 305 end_time); |
| 306 return true; | 306 return true; |
| 307 } | 307 } |
| 308 | 308 |
| 309 namespace { | |
| 309 int64_t g_last_seen_timestamp_ms = 0; | 310 int64_t g_last_seen_timestamp_ms = 0; |
| 310 // accumulated rollover time. | |
| 311 int64_t g_rollover_ms = 0; | 311 int64_t g_rollover_ms = 0; |
| 312 bool g_bogus_x11_timestamps = false; | |
| 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; |
| 315 } // namespace | |
| 314 | 316 |
| 315 // 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. |
| 316 // 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. |
| 317 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { | 319 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { |
| 318 int64_t timestamp64 = timestamp; | 320 int64_t timestamp64 = timestamp; |
| 319 | 321 |
| 320 if (!timestamp) | 322 if (!timestamp) |
| 321 return base::TimeDelta(); | 323 return base::TimeDelta(); |
| 322 | 324 |
| 325 if (g_bogus_x11_timestamps) { | |
| 326 return base::TimeDelta::FromMilliseconds( | |
| 327 (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds()); | |
|
majidvp
2016/05/17 13:27:53
Please use ui::EventTimeForNow() instead.
I don't
caseq
2016/05/20 23:41:33
Done.
| |
| 328 } | |
| 329 | |
| 323 // If this is the first event that we get, assume the time stamp roll-over | 330 // If this is the first event that we get, assume the time stamp roll-over |
| 324 // might have happened before the process was started. | 331 // might have happened before the process was started. |
| 325 // Register a rollover if the distance between last timestamp and current one | 332 // 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 | 333 // 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. | 334 // where X server delivers reasonably close events out-of-order. |
| 328 bool had_recent_rollover = | 335 bool had_recent_rollover = |
| 329 !g_last_seen_timestamp_ms || | 336 !g_last_seen_timestamp_ms || |
| 330 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); | 337 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); |
| 331 | 338 |
| 332 g_last_seen_timestamp_ms = timestamp64; | 339 g_last_seen_timestamp_ms = timestamp64; |
| 333 if (!had_recent_rollover) | 340 if (!had_recent_rollover) |
| 334 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); | 341 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); |
| 335 | 342 |
| 336 DCHECK(timestamp64 <= UINT32_MAX) | 343 DCHECK(timestamp64 <= UINT32_MAX) |
| 337 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; | 344 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; |
| 338 | 345 |
| 339 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr | 346 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr |
| 340 ? g_tick_clock.Get()->NowTicks() | 347 ? g_tick_clock.Get()->NowTicks() |
| 341 : base::TimeTicks::Now(); | 348 : base::TimeTicks::Now(); |
| 342 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); | 349 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); |
| 343 | 350 |
| 344 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); | 351 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); |
| 345 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); | 352 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 | 353 if (delta > 60 * 1000 && !g_tick_clock.Get()) { |
| 347 // come through with real timestamps. | 354 // x11 timestamps don't seem to be using the same time base as TimeTicks, |
| 348 DCHECK(delta < 60 * 1000 || g_tick_clock.Get() != nullptr) | 355 // so ignore them altogether and always use current time instead. |
| 349 << "Unexpected X11 event time, now: " << now_ticks | 356 delta = 0; |
| 350 << " event at: " << timestamp; | 357 g_bogus_x11_timestamps = true; |
| 358 LOG(WARNING) << "Unexpected x11 timestamps, will use browser time instead."; | |
|
majidvp
2016/05/17 13:27:53
I wonder if it makes sense to also collect UMA met
caseq
2016/05/20 23:41:33
Done.
| |
| 359 } | |
| 351 return base::TimeDelta::FromMilliseconds(now_ms - delta); | 360 return base::TimeDelta::FromMilliseconds(now_ms - delta); |
| 352 } | 361 } |
| 353 | 362 |
| 354 } // namespace | 363 } // namespace |
| 355 | 364 |
| 356 namespace ui { | 365 namespace ui { |
| 357 | 366 |
| 358 EventType EventTypeFromXEvent(const XEvent& xev) { | 367 EventType EventTypeFromXEvent(const XEvent& xev) { |
| 359 // Allow the DeviceDataManager to block the event. If blocked return | 368 // Allow the DeviceDataManager to block the event. If blocked return |
| 360 // ET_UNKNOWN as the type so this event will not be further processed. | 369 // 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 | 815 |
| 807 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, | 816 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, |
| 808 vy_ordinal, is_cancel); | 817 vy_ordinal, is_cancel); |
| 809 return true; | 818 return true; |
| 810 } | 819 } |
| 811 | 820 |
| 812 void ResetTimestampRolloverCountersForTesting( | 821 void ResetTimestampRolloverCountersForTesting( |
| 813 std::unique_ptr<base::TickClock> tick_clock) { | 822 std::unique_ptr<base::TickClock> tick_clock) { |
| 814 g_last_seen_timestamp_ms = 0; | 823 g_last_seen_timestamp_ms = 0; |
| 815 g_rollover_ms = 0; | 824 g_rollover_ms = 0; |
| 825 g_bogus_x11_timestamps = false; | |
| 816 g_tick_clock.Get() = std::move(tick_clock); | 826 g_tick_clock.Get() = std::move(tick_clock); |
| 817 } | 827 } |
| 818 | 828 |
| 819 } // namespace ui | 829 } // namespace ui |
| OLD | NEW |