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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 case XI_KeyRelease: { | 531 case XI_KeyRelease: { |
535 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); | 532 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); |
536 return GetEventFlagsFromXGenericEvent(xev); | 533 return GetEventFlagsFromXGenericEvent(xev); |
537 } | 534 } |
538 } | 535 } |
539 } | 536 } |
540 } | 537 } |
541 return 0; | 538 return 0; |
542 } | 539 } |
543 | 540 |
544 base::TimeDelta EventTimeFromXEvent(const XEvent& xev) { | 541 base::TimeTicks EventTimeFromXEvent(const XEvent& xev) { |
545 switch (xev.type) { | 542 switch (xev.type) { |
546 case KeyPress: | 543 case KeyPress: |
547 case KeyRelease: | 544 case KeyRelease: |
548 return TimeDeltaFromXEventTime(xev.xkey.time); | 545 return TimeTicksFromXEventTime(xev.xkey.time); |
549 case ButtonPress: | 546 case ButtonPress: |
550 case ButtonRelease: | 547 case ButtonRelease: |
551 return TimeDeltaFromXEventTime(xev.xbutton.time); | 548 return TimeTicksFromXEventTime(xev.xbutton.time); |
552 break; | 549 break; |
553 case MotionNotify: | 550 case MotionNotify: |
554 return TimeDeltaFromXEventTime(xev.xmotion.time); | 551 return TimeTicksFromXEventTime(xev.xmotion.time); |
555 break; | 552 break; |
556 case EnterNotify: | 553 case EnterNotify: |
557 case LeaveNotify: | 554 case LeaveNotify: |
558 return TimeDeltaFromXEventTime(xev.xcrossing.time); | 555 return TimeTicksFromXEventTime(xev.xcrossing.time); |
559 break; | 556 break; |
560 case GenericEvent: { | 557 case GenericEvent: { |
561 double start, end; | 558 double start, end; |
562 double touch_timestamp; | 559 double touch_timestamp; |
563 if (GetGestureTimes(xev, &start, &end)) { | 560 if (GetGestureTimes(xev, &start, &end)) { |
564 // If the driver supports gesture times, use them. | 561 // If the driver supports gesture times, use them. |
565 return base::TimeDelta::FromMicroseconds(end * 1000000); | 562 return ui::EventTimeStampFromSeconds(end); |
566 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( | 563 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( |
567 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, | 564 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, |
568 &touch_timestamp)) { | 565 &touch_timestamp)) { |
569 return base::TimeDelta::FromMicroseconds(touch_timestamp * 1000000); | 566 return ui::EventTimeStampFromSeconds(touch_timestamp); |
570 } else { | 567 } else { |
571 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); | 568 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); |
572 return TimeDeltaFromXEventTime(xide->time); | 569 return TimeTicksFromXEventTime(xide->time); |
573 } | 570 } |
574 break; | 571 break; |
575 } | 572 } |
576 } | 573 } |
577 NOTREACHED(); | 574 NOTREACHED(); |
578 return base::TimeDelta(); | 575 return base::TimeTicks(); |
579 } | 576 } |
580 | 577 |
581 gfx::Point EventLocationFromXEvent(const XEvent& xev) { | 578 gfx::Point EventLocationFromXEvent(const XEvent& xev) { |
582 switch (xev.type) { | 579 switch (xev.type) { |
583 case EnterNotify: | 580 case EnterNotify: |
584 case LeaveNotify: | 581 case LeaveNotify: |
585 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); | 582 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); |
586 case ButtonPress: | 583 case ButtonPress: |
587 case ButtonRelease: | 584 case ButtonRelease: |
588 return gfx::Point(xev.xbutton.x, xev.xbutton.y); | 585 return gfx::Point(xev.xbutton.x, xev.xbutton.y); |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 | 824 |
828 void ResetTimestampRolloverCountersForTesting( | 825 void ResetTimestampRolloverCountersForTesting( |
829 std::unique_ptr<base::TickClock> tick_clock) { | 826 std::unique_ptr<base::TickClock> tick_clock) { |
830 g_last_seen_timestamp_ms = 0; | 827 g_last_seen_timestamp_ms = 0; |
831 g_rollover_ms = 0; | 828 g_rollover_ms = 0; |
832 g_bogus_x11_timestamps = false; | 829 g_bogus_x11_timestamps = false; |
833 g_tick_clock.Get() = std::move(tick_clock); | 830 g_tick_clock.Get() = std::move(tick_clock); |
834 } | 831 } |
835 | 832 |
836 } // namespace ui | 833 } // namespace ui |
OLD | NEW |