base_event_utils.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/events/base_event_utils.h"
  5. #include "base/atomic_sequence_num.h"
  6. #include "base/check_op.h"
  7. #include "base/command_line.h"
  8. #include "base/lazy_instance.h"
  9. #include "base/time/time.h"
  10. #include "build/build_config.h"
  11. #include "build/chromeos_buildflags.h"
  12. #include "ui/events/event_constants.h"
  13. #include "ui/events/event_switches.h"
  14. namespace ui {
  15. namespace {
  16. #if BUILDFLAG(IS_CHROMEOS_ASH)
  17. const int kSystemKeyModifierMask = EF_ALT_DOWN | EF_COMMAND_DOWN;
  18. #elif BUILDFLAG(IS_APPLE)
  19. // Alt modifier is used to input extended characters on Mac.
  20. const int kSystemKeyModifierMask = EF_COMMAND_DOWN;
  21. #else
  22. const int kSystemKeyModifierMask = EF_ALT_DOWN;
  23. #endif
  24. } // namespace
  25. base::AtomicSequenceNumber g_next_event_id;
  26. uint32_t GetNextTouchEventId() {
  27. // Set the first touch event ID to 1 because we set id to 0 for other types
  28. // of events.
  29. uint32_t id = g_next_event_id.GetNext();
  30. if (id == 0)
  31. id = g_next_event_id.GetNext();
  32. DCHECK_NE(0U, id);
  33. return id;
  34. }
  35. bool IsSystemKeyModifier(int flags) {
  36. // AltGr modifier is used to type alternative keys on certain keyboard layouts
  37. // so we don't consider keys with the AltGr modifier as a system key.
  38. return (kSystemKeyModifierMask & flags) != 0 &&
  39. (EF_ALTGR_DOWN & flags) == 0;
  40. }
  41. base::LazyInstance<const base::TickClock*>::Leaky g_tick_clock =
  42. LAZY_INSTANCE_INITIALIZER;
  43. base::TimeTicks EventTimeForNow() {
  44. return g_tick_clock.Get() ? g_tick_clock.Get()->NowTicks()
  45. : base::TimeTicks::Now();
  46. }
  47. void SetEventTickClockForTesting(const base::TickClock* tick_clock) {
  48. g_tick_clock.Get() = tick_clock;
  49. }
  50. double EventTimeStampToSeconds(base::TimeTicks time_stamp) {
  51. return (time_stamp - base::TimeTicks()).InSecondsF();
  52. }
  53. base::TimeTicks EventTimeStampFromSeconds(double time_stamp_seconds) {
  54. return base::TimeTicks() + base::Seconds(time_stamp_seconds);
  55. }
  56. bool IsValidTimebase(base::TimeTicks now, base::TimeTicks timestamp) {
  57. int64_t delta = (now - timestamp).InMilliseconds();
  58. return delta >= 0 && delta <= 60 * 1000;
  59. }
  60. void ValidateEventTimeClock(base::TimeTicks* timestamp) {
  61. // Some fraction of devices, across all platforms provide bogus event
  62. // timestamps. See https://crbug.com/650338#c1. Correct timestamps which are
  63. // clearly bogus.
  64. // TODO(861855): Replace this with an approach that doesn't require an extra
  65. // read of the current time per event.
  66. base::TimeTicks now = EventTimeForNow();
  67. if (!IsValidTimebase(now, *timestamp))
  68. *timestamp = now;
  69. }
  70. } // namespace ui