serial_tracker.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef COMPONENTS_EXO_WAYLAND_SERIAL_TRACKER_H_
  5. #define COMPONENTS_EXO_WAYLAND_SERIAL_TRACKER_H_
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. struct wl_display;
  10. namespace exo {
  11. namespace wayland {
  12. class SerialTracker {
  13. public:
  14. enum EventType {
  15. POINTER_ENTER,
  16. POINTER_LEAVE,
  17. POINTER_BUTTON_DOWN,
  18. POINTER_BUTTON_UP,
  19. TOUCH_DOWN,
  20. TOUCH_UP,
  21. OTHER_EVENT,
  22. };
  23. explicit SerialTracker(struct wl_display* display);
  24. SerialTracker(const SerialTracker&) = delete;
  25. SerialTracker& operator=(const SerialTracker&) = delete;
  26. ~SerialTracker();
  27. // After shutdown, |GetNextSerial| returns 0.
  28. void Shutdown();
  29. uint32_t GetNextSerial(EventType type);
  30. // Get the serial number of the last {pointer,touch} pressed event, or nullopt
  31. // if the press has since been released.
  32. absl::optional<uint32_t> GetPointerDownSerial();
  33. absl::optional<uint32_t> GetTouchDownSerial();
  34. // Needed because wl_touch::cancel doesn't send a serial number, so we can't
  35. // test for it in GetNextSerial.
  36. void ResetTouchDownSerial();
  37. // If there exists a serial for key already, returns it. Or, it creates
  38. // a new serial, and returns it.
  39. uint32_t MaybeNextKeySerial();
  40. // Resets the stored key serial, so that next MaybeNextKeySerial() call will
  41. // generate a new serial.
  42. void ResetKeySerial();
  43. // Get the EventType for a serial number, or nullopt if the serial number was
  44. // never sent or is too old.
  45. absl::optional<EventType> GetEventType(uint32_t serial) const;
  46. private:
  47. struct wl_display* display_;
  48. // EventTypes are stored in a circular buffer, because serial numbers are
  49. // issued sequentially and we only want to store the most recent events.
  50. std::vector<EventType> events_;
  51. // [min_event_, max_event) is a half-open interval containing the range of
  52. // valid serial numbers. Note that as serial numbers are allowed to wrap
  53. // around the 32 bit space, we cannot assume that max_event_ >= min_event_.
  54. uint32_t min_event_ = 1;
  55. uint32_t max_event_ = 1;
  56. absl::optional<uint32_t> pointer_down_serial_;
  57. absl::optional<uint32_t> touch_down_serial_;
  58. absl::optional<uint32_t> key_serial_;
  59. };
  60. } // namespace wayland
  61. } // namespace exo
  62. #endif // COMPONENTS_EXO_WAYLAND_SERIAL_TRACKER_H_