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 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 return true; | 305 return true; |
306 } | 306 } |
307 | 307 |
308 int64_t g_last_seen_timestamp_ms = 0; | 308 int64_t g_last_seen_timestamp_ms = 0; |
309 // accumulated rollover time. | 309 // accumulated rollover time. |
310 int64_t g_rollover_ms = 0; | 310 int64_t g_rollover_ms = 0; |
311 std::unique_ptr<base::TickClock> g_tick_clock; | 311 std::unique_ptr<base::TickClock> g_tick_clock; |
312 | 312 |
313 // 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. |
314 // 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. |
315 base::TimeDelta TimeDeltaFromXEventTime(Time timestamp) { | 315 base::TimeTicks TimeTicksFromXEventTime(Time timestamp) { |
316 int64_t timestamp64 = timestamp; | 316 int64_t timestamp64 = timestamp; |
317 | 317 |
318 if (!timestamp) | 318 if (!timestamp) |
319 return base::TimeDelta(); | 319 return base::TimeTicks(); |
320 | 320 |
321 // 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 |
322 // might have happened before the process was started. | 322 // might have happened before the process was started. |
323 // 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 |
324 // 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 |
325 // where X server delivers reasonably close events out-of-order. | 325 // where X server delivers reasonably close events out-of-order. |
326 bool had_recent_rollover = | 326 bool had_recent_rollover = |
327 !g_last_seen_timestamp_ms || | 327 !g_last_seen_timestamp_ms || |
328 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); | 328 g_last_seen_timestamp_ms - timestamp64 > (UINT32_MAX >> 1); |
329 | 329 |
330 g_last_seen_timestamp_ms = timestamp64; | 330 g_last_seen_timestamp_ms = timestamp64; |
331 if (!had_recent_rollover) | 331 if (!had_recent_rollover) |
332 return base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); | 332 return base::TimeTicks() + |
| 333 base::TimeDelta::FromMilliseconds(g_rollover_ms + timestamp); |
333 | 334 |
334 DCHECK(timestamp64 <= UINT32_MAX) | 335 DCHECK(timestamp64 <= UINT32_MAX) |
335 << "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"; |
336 | 337 |
337 base::TimeTicks now_ticks = | 338 base::TimeTicks now_ticks = |
338 g_tick_clock ? g_tick_clock->NowTicks() : base::TimeTicks::Now(); | 339 g_tick_clock ? g_tick_clock->NowTicks() : base::TimeTicks::Now(); |
339 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); | 340 int64_t now_ms = (now_ticks - base::TimeTicks()).InMilliseconds(); |
340 | 341 |
341 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); | 342 g_rollover_ms = now_ms & ~static_cast<int64_t>(UINT32_MAX); |
342 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); | 343 uint32_t delta = static_cast<uint32_t>(now_ms - timestamp); |
343 // If using a mock clock, all bets are off -- in some tests, actual X11 events | 344 // If using a mock clock, all bets are off -- in some tests, actual X11 events |
344 // come through with real timestamps. | 345 // come through with real timestamps. |
345 DCHECK(delta < 60 * 1000 || g_tick_clock) | 346 DCHECK(delta < 60 * 1000 || g_tick_clock) |
346 << "Unexpected X11 event time, now: " << now_ticks | 347 << "Unexpected X11 event time, now: " << now_ticks |
347 << " event at: " << timestamp; | 348 << " event at: " << timestamp; |
348 return base::TimeDelta::FromMilliseconds(now_ms - delta); | 349 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(now_ms - delta); |
349 } | 350 } |
350 | 351 |
351 } // namespace | 352 } // namespace |
352 | 353 |
353 namespace ui { | 354 namespace ui { |
354 | 355 |
355 EventType EventTypeFromXEvent(const XEvent& xev) { | 356 EventType EventTypeFromXEvent(const XEvent& xev) { |
356 // Allow the DeviceDataManager to block the event. If blocked return | 357 // Allow the DeviceDataManager to block the event. If blocked return |
357 // ET_UNKNOWN as the type so this event will not be further processed. | 358 // ET_UNKNOWN as the type so this event will not be further processed. |
358 // NOTE: During some events unittests there is no device data manager. | 359 // NOTE: During some events unittests there is no device data manager. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 case XI_KeyRelease: { | 516 case XI_KeyRelease: { |
516 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); | 517 XModifierStateWatcher::GetInstance()->UpdateStateFromXEvent(xev); |
517 return GetEventFlagsFromXGenericEvent(xev); | 518 return GetEventFlagsFromXGenericEvent(xev); |
518 } | 519 } |
519 } | 520 } |
520 } | 521 } |
521 } | 522 } |
522 return 0; | 523 return 0; |
523 } | 524 } |
524 | 525 |
525 base::TimeDelta EventTimeFromXEvent(const XEvent& xev) { | 526 base::TimeTicks EventTimeFromXEvent(const XEvent& xev) { |
526 switch (xev.type) { | 527 switch (xev.type) { |
527 case KeyPress: | 528 case KeyPress: |
528 case KeyRelease: | 529 case KeyRelease: |
529 return TimeDeltaFromXEventTime(xev.xkey.time); | 530 return TimeTicksFromXEventTime(xev.xkey.time); |
530 case ButtonPress: | 531 case ButtonPress: |
531 case ButtonRelease: | 532 case ButtonRelease: |
532 return TimeDeltaFromXEventTime(xev.xbutton.time); | 533 return TimeTicksFromXEventTime(xev.xbutton.time); |
533 break; | 534 break; |
534 case MotionNotify: | 535 case MotionNotify: |
535 return TimeDeltaFromXEventTime(xev.xmotion.time); | 536 return TimeTicksFromXEventTime(xev.xmotion.time); |
536 break; | 537 break; |
537 case EnterNotify: | 538 case EnterNotify: |
538 case LeaveNotify: | 539 case LeaveNotify: |
539 return TimeDeltaFromXEventTime(xev.xcrossing.time); | 540 return TimeTicksFromXEventTime(xev.xcrossing.time); |
540 break; | 541 break; |
541 case GenericEvent: { | 542 case GenericEvent: { |
542 double start, end; | 543 double start, end; |
543 double touch_timestamp; | 544 double touch_timestamp; |
544 if (GetGestureTimes(xev, &start, &end)) { | 545 if (GetGestureTimes(xev, &start, &end)) { |
545 // If the driver supports gesture times, use them. | 546 // If the driver supports gesture times, use them. |
546 return base::TimeDelta::FromMicroseconds(end * 1000000); | 547 return base::TimeTicks() + |
| 548 base::TimeDelta::FromMicroseconds(end * 1000000); |
547 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( | 549 } else if (DeviceDataManagerX11::GetInstance()->GetEventData( |
548 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, | 550 xev, DeviceDataManagerX11::DT_TOUCH_RAW_TIMESTAMP, |
549 &touch_timestamp)) { | 551 &touch_timestamp)) { |
550 return base::TimeDelta::FromMicroseconds(touch_timestamp * 1000000); | 552 return base::TimeTicks() + |
| 553 base::TimeDelta::FromMicroseconds(touch_timestamp * 1000000); |
551 } else { | 554 } else { |
552 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); | 555 XIDeviceEvent* xide = static_cast<XIDeviceEvent*>(xev.xcookie.data); |
553 return TimeDeltaFromXEventTime(xide->time); | 556 return TimeTicksFromXEventTime(xide->time); |
554 } | 557 } |
555 break; | 558 break; |
556 } | 559 } |
557 } | 560 } |
558 NOTREACHED(); | 561 NOTREACHED(); |
559 return base::TimeDelta(); | 562 return base::TimeTicks(); |
560 } | 563 } |
561 | 564 |
562 gfx::Point EventLocationFromXEvent(const XEvent& xev) { | 565 gfx::Point EventLocationFromXEvent(const XEvent& xev) { |
563 switch (xev.type) { | 566 switch (xev.type) { |
564 case EnterNotify: | 567 case EnterNotify: |
565 case LeaveNotify: | 568 case LeaveNotify: |
566 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); | 569 return gfx::Point(xev.xcrossing.x, xev.xcrossing.y); |
567 case ButtonPress: | 570 case ButtonPress: |
568 case ButtonRelease: | 571 case ButtonRelease: |
569 return gfx::Point(xev.xbutton.x, xev.xbutton.y); | 572 return gfx::Point(xev.xbutton.x, xev.xbutton.y); |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 } | 810 } |
808 | 811 |
809 void ResetTimestampRolloverCountersForTesting( | 812 void ResetTimestampRolloverCountersForTesting( |
810 std::unique_ptr<base::TickClock> tick_clock) { | 813 std::unique_ptr<base::TickClock> tick_clock) { |
811 g_last_seen_timestamp_ms = 0; | 814 g_last_seen_timestamp_ms = 0; |
812 g_rollover_ms = 0; | 815 g_rollover_ms = 0; |
813 g_tick_clock = std::move(tick_clock); | 816 g_tick_clock = std::move(tick_clock); |
814 } | 817 } |
815 | 818 |
816 } // namespace ui | 819 } // namespace ui |
OLD | NEW |