zwp_input_timestamps_manager.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2018 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/zwp_input_timestamps_manager.h"
  5. #include <input-timestamps-unstable-v1-server-protocol.h>
  6. #include <wayland-server-core.h>
  7. #include <wayland-server-protocol-core.h>
  8. #include "components/exo/keyboard.h"
  9. #include "components/exo/pointer.h"
  10. #include "components/exo/touch.h"
  11. #include "components/exo/wayland/server_util.h"
  12. #include "components/exo/wayland/wayland_input_delegate.h"
  13. #include "components/exo/wayland/wayland_keyboard_delegate.h"
  14. #include "components/exo/wayland/wayland_pointer_delegate.h"
  15. #include "components/exo/wayland/wayland_touch_delegate.h"
  16. namespace exo {
  17. namespace wayland {
  18. namespace {
  19. ////////////////////////////////////////////////////////////////////////////////
  20. // input_timestamps_v1 interface:
  21. class WaylandInputTimestamps : public WaylandInputDelegate::Observer {
  22. public:
  23. WaylandInputTimestamps(wl_resource* resource, WaylandInputDelegate* delegate)
  24. : resource_(resource), delegate_(delegate) {
  25. delegate_->AddObserver(this);
  26. }
  27. WaylandInputTimestamps(const WaylandInputTimestamps&) = delete;
  28. WaylandInputTimestamps& operator=(const WaylandInputTimestamps&) = delete;
  29. ~WaylandInputTimestamps() override {
  30. if (delegate_)
  31. delegate_->RemoveObserver(this);
  32. }
  33. // Overridden from WaylandInputDelegate::Observer:
  34. void OnDelegateDestroying(WaylandInputDelegate* delegate) override {
  35. DCHECK(delegate_ == delegate);
  36. delegate_ = nullptr;
  37. }
  38. void OnSendTimestamp(base::TimeTicks time_stamp) override {
  39. timespec ts = (time_stamp - base::TimeTicks()).ToTimeSpec();
  40. zwp_input_timestamps_v1_send_timestamp(
  41. resource_, static_cast<uint64_t>(ts.tv_sec) >> 32,
  42. ts.tv_sec & 0xffffffff, ts.tv_nsec);
  43. }
  44. private:
  45. wl_resource* const resource_;
  46. WaylandInputDelegate* delegate_;
  47. };
  48. void input_timestamps_destroy(struct wl_client* client,
  49. struct wl_resource* resource) {
  50. wl_resource_destroy(resource);
  51. }
  52. const struct zwp_input_timestamps_v1_interface input_timestamps_implementation =
  53. {input_timestamps_destroy};
  54. ////////////////////////////////////////////////////////////////////////////////
  55. // input_timestamps_manager_v1 interface:
  56. void input_timestamps_manager_destroy(struct wl_client* client,
  57. struct wl_resource* resource) {
  58. wl_resource_destroy(resource);
  59. }
  60. template <typename T, typename D>
  61. void input_timestamps_manager_get_timestamps(wl_client* client,
  62. wl_resource* resource,
  63. uint32_t id,
  64. wl_resource* input_resource) {
  65. wl_resource* input_timestamps_resource =
  66. wl_resource_create(client, &zwp_input_timestamps_v1_interface, 1, id);
  67. auto input_timestamps = std::make_unique<WaylandInputTimestamps>(
  68. input_timestamps_resource,
  69. static_cast<WaylandInputDelegate*>(
  70. static_cast<D*>(GetUserDataAs<T>(input_resource)->delegate())));
  71. SetImplementation(input_timestamps_resource, &input_timestamps_implementation,
  72. std::move(input_timestamps));
  73. }
  74. const struct zwp_input_timestamps_manager_v1_interface
  75. input_timestamps_manager_implementation = {
  76. input_timestamps_manager_destroy,
  77. input_timestamps_manager_get_timestamps<Keyboard,
  78. WaylandKeyboardDelegate>,
  79. input_timestamps_manager_get_timestamps<Pointer,
  80. WaylandPointerDelegate>,
  81. input_timestamps_manager_get_timestamps<Touch, WaylandTouchDelegate>};
  82. } // namespace
  83. void bind_input_timestamps_manager(wl_client* client,
  84. void* data,
  85. uint32_t version,
  86. uint32_t id) {
  87. wl_resource* resource = wl_resource_create(
  88. client, &zwp_input_timestamps_manager_v1_interface, 1, id);
  89. wl_resource_set_implementation(
  90. resource, &input_timestamps_manager_implementation, nullptr, nullptr);
  91. }
  92. } // namespace wayland
  93. } // namespace exo