serial_tracker.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 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 "components/exo/wayland/serial_tracker.h"
  5. #include <wayland-server-core.h>
  6. namespace exo {
  7. namespace wayland {
  8. namespace {
  9. // Number of previous events to retain information about.
  10. constexpr uint32_t kMaxEventsTracked = 1024;
  11. } // namespace
  12. SerialTracker::SerialTracker(struct wl_display* display)
  13. : display_(display), events_(kMaxEventsTracked) {}
  14. SerialTracker::~SerialTracker() {}
  15. void SerialTracker::Shutdown() {
  16. display_ = nullptr;
  17. }
  18. uint32_t SerialTracker::GetNextSerial(EventType type) {
  19. if (!display_)
  20. return 0;
  21. uint32_t serial = wl_display_next_serial(display_);
  22. events_[serial % kMaxEventsTracked] = type;
  23. max_event_ = serial + 1;
  24. if ((max_event_ - min_event_) > kMaxEventsTracked)
  25. min_event_ = max_event_ - kMaxEventsTracked;
  26. switch (type) {
  27. case EventType::POINTER_BUTTON_DOWN:
  28. pointer_down_serial_ = serial;
  29. break;
  30. case EventType::POINTER_BUTTON_UP:
  31. pointer_down_serial_ = absl::nullopt;
  32. break;
  33. case EventType::TOUCH_DOWN:
  34. touch_down_serial_ = serial;
  35. break;
  36. case EventType::TOUCH_UP:
  37. touch_down_serial_ = absl::nullopt;
  38. break;
  39. default:
  40. break;
  41. }
  42. return serial;
  43. }
  44. absl::optional<SerialTracker::EventType> SerialTracker::GetEventType(
  45. uint32_t serial) const {
  46. if (max_event_ < min_event_) {
  47. // The valid range has partially overflowed the 32 bit space, so we should
  48. // only reject if the serial number is in neither the upper nor lower parts
  49. // of the space.
  50. if (!((serial < max_event_) || (serial >= min_event_)))
  51. return absl::nullopt;
  52. } else {
  53. // Normal, non-overflowed case. Reject the serial number if it isn't in the
  54. // interval.
  55. if (!((serial < max_event_) && (serial >= min_event_)))
  56. return absl::nullopt;
  57. }
  58. return events_[serial % kMaxEventsTracked];
  59. }
  60. absl::optional<uint32_t> SerialTracker::GetPointerDownSerial() {
  61. return pointer_down_serial_;
  62. }
  63. absl::optional<uint32_t> SerialTracker::GetTouchDownSerial() {
  64. return touch_down_serial_;
  65. }
  66. void SerialTracker::ResetTouchDownSerial() {
  67. touch_down_serial_ = absl::nullopt;
  68. }
  69. uint32_t SerialTracker::MaybeNextKeySerial() {
  70. if (!key_serial_.has_value())
  71. key_serial_ = GetNextSerial(OTHER_EVENT);
  72. return key_serial_.value();
  73. }
  74. void SerialTracker::ResetKeySerial() {
  75. key_serial_ = absl::nullopt;
  76. }
  77. } // namespace wayland
  78. } // namespace exo