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" | |
17 #include "base/logging.h" | 16 #include "base/logging.h" |
18 #include "base/macros.h" | 17 #include "base/macros.h" |
19 #include "base/memory/singleton.h" | 18 #include "base/memory/singleton.h" |
20 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
21 #include "build/build_config.h" | 20 #include "build/build_config.h" |
22 #include "ui/display/display.h" | 21 #include "ui/display/display.h" |
23 #include "ui/display/screen.h" | 22 #include "ui/display/screen.h" |
24 #include "ui/events/base_event_utils.h" | 23 #include "ui/events/base_event_utils.h" |
25 #include "ui/events/devices/x11/device_data_manager_x11.h" | 24 #include "ui/events/devices/x11/device_data_manager_x11.h" |
26 #include "ui/events/devices/x11/device_list_cache_x11.h" | 25 #include "ui/events/devices/x11/device_list_cache_x11.h" |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 if (!end_time) | 302 if (!end_time) |
304 end_time = &end_time_; | 303 end_time = &end_time_; |
305 | 304 |
306 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, | 305 ui::DeviceDataManagerX11::GetInstance()->GetGestureTimes(xev, start_time, |
307 end_time); | 306 end_time); |
308 return true; | 307 return true; |
309 } | 308 } |
310 | 309 |
311 int64_t g_last_seen_timestamp_ms = 0; | 310 int64_t g_last_seen_timestamp_ms = 0; |
312 int64_t g_rollover_ms = 0; | 311 int64_t g_rollover_ms = 0; |
313 bool g_bogus_x11_timestamps = false; | |
314 base::LazyInstance<std::unique_ptr<base::TickClock>>::Leaky g_tick_clock = | |
315 LAZY_INSTANCE_INITIALIZER; | |
316 | 312 |
317 // Takes Xlib Time and returns a time delta that is immune to timer rollover. | 313 // Takes Xlib Time and returns a time delta that is immune to timer rollover. |
318 // This function is not thread safe as we do not use a lock. | 314 // This function is not thread safe as we do not use a lock. |
319 base::TimeTicks TimeTicksFromXEventTime(Time timestamp) { | 315 base::TimeTicks TimeTicksFromXEventTime(Time timestamp) { |
320 int64_t timestamp64 = timestamp; | 316 int64_t timestamp64 = timestamp; |
321 | 317 |
322 if (!timestamp) | 318 if (!timestamp) |
323 return base::TimeTicks(); | 319 return ui::EventTimeForNow(); |
324 | |
325 if (g_bogus_x11_timestamps) | |
326 return base::TimeTicks::Now(); | |
327 | 320 |
328 // If this is the first event that we get, assume the time stamp roll-over | 321 // If this is the first event that we get, assume the time stamp roll-over |
329 // might have happened before the process was started. | 322 // might have happened before the process was started. |
330 // Register a rollover if the distance between last timestamp and current one | 323 // Register a rollover if the distance between last timestamp and current one |
331 // is larger than half the width. This avoids false rollovers even in a case | 324 // is larger than half the width. This avoids false rollovers even in a case |
332 // where X server delivers reasonably close events out-of-order. | 325 // where X server delivers reasonably close events out-of-order. |
333 bool had_recent_rollover = | 326 bool had_recent_rollover = |
334 !g_last_seen_timestamp_ms || | 327 !g_last_seen_timestamp_ms || |
335 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); | 328 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); |
336 | 329 |
337 g_last_seen_timestamp_ms = timestamp64; | 330 g_last_seen_timestamp_ms = timestamp64; |
338 if (!had_recent_rollover) | 331 if (!had_recent_rollover) |
339 return base::TimeTicks() + | 332 return base::TimeTicks() + |
340 base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); | 333 base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); |
341 | 334 |
342 DCHECK(timestamp64 <= UINT32_MAX) | 335 DCHECK(timestamp64 <= UINT32_MAX) |
343 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; | 336 << "X11 Time does not roll over 32 bit, the below logic is likely wrong"; |
344 | 337 |
345 base::TimeTicks now_ticks = g_tick_clock.Get() != nullptr | 338 base::TimeTicks now_ticks = ui::EventTimeForNow(); |
346 ? g_tick_clock.Get()->NowTicks() | |
347 : base::TimeTicks::Now(); | |
348 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); | 339 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); |
349 | 340 |
350 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); | 341 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); |
351 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); | 342 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); |
352 if (!g_tick_clock.Get()) { | |
353 if (delta > 60 * 1000) { | |
354 // x11 timestamps don't seem to be using the same time base as TimeTicks, | |
355 // so ignore them altogether and always use current time instead. | |
356 delta = 0; | |
357 g_bogus_x11_timestamps = true; | |
358 LOG(WARNING) | |
359 << "Unexpected x11 timestamps, will use browser time instead."; | |
360 } | |
361 UMA_HISTOGRAM_BOOLEAN("Event.TimestampHasValidTimebase", | |
362 !g_bogus_x11_timestamps); | |
363 } | |
364 | |
365 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(now_ms - delta); | 343 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(now_ms - delta); |
366 } | 344 } |
367 | 345 |
368 } // namespace | 346 } // namespace |
369 | 347 |
370 namespace ui { | 348 namespace ui { |
371 | 349 |
372 EventType EventTypeFromXEvent(const XEvent& xev) { | 350 EventType EventTypeFromXEvent(const XEvent& xev) { |
373 // Allow the DeviceDataManager to block the event. If blocked return | 351 // Allow the DeviceDataManager to block the event. If blocked return |
374 // ET_UNKNOWN as the type so this event will not be further processed. | 352 // ET_UNKNOWN as the type so this event will not be further processed. |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 | 797 |
820 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, | 798 DeviceDataManagerX11::GetInstance()->GetFlingData(xev, vx, vy, vx_ordinal, |
821 vy_ordinal, is_cancel); | 799 vy_ordinal, is_cancel); |
822 return true; | 800 return true; |
823 } | 801 } |
824 | 802 |
825 void ResetTimestampRolloverCountersForTesting( | 803 void ResetTimestampRolloverCountersForTesting( |
826 std::unique_ptr<base::TickClock> tick_clock) { | 804 std::unique_ptr<base::TickClock> tick_clock) { |
827 g_last_seen_timestamp_ms = 0; | 805 g_last_seen_timestamp_ms = 0; |
828 g_rollover_ms = 0; | 806 g_rollover_ms = 0; |
829 g_bogus_x11_timestamps = false; | 807 SetEventTickClockForTesting(std::move(tick_clock)); |
830 g_tick_clock.Get() = std::move(tick_clock); | |
831 } | 808 } |
832 | 809 |
833 } // namespace ui | 810 } // namespace ui |
OLD | NEW |