wl_seat.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/wl_seat.h"
  5. #include <wayland-server-core.h>
  6. #include <wayland-server-protocol-core.h>
  7. #include "build/chromeos_buildflags.h"
  8. #include "components/exo/pointer.h"
  9. #include "components/exo/touch.h"
  10. #include "components/exo/wayland/serial_tracker.h"
  11. #include "components/exo/wayland/server_util.h"
  12. #include "components/exo/wayland/wayland_pointer_delegate.h"
  13. #include "components/exo/wayland/wayland_touch_delegate.h"
  14. #include "ui/base/buildflags.h"
  15. #if BUILDFLAG(IS_CHROMEOS_ASH)
  16. #include "components/exo/keyboard.h"
  17. #include "components/exo/wayland/wayland_keyboard_delegate.h"
  18. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  19. namespace exo {
  20. namespace wayland {
  21. namespace {
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // wl_pointer_interface:
  24. void pointer_set_cursor(wl_client* client,
  25. wl_resource* resource,
  26. uint32_t serial,
  27. wl_resource* surface_resource,
  28. int32_t hotspot_x,
  29. int32_t hotspot_y) {
  30. GetUserDataAs<Pointer>(resource)->SetCursor(
  31. surface_resource ? GetUserDataAs<Surface>(surface_resource) : nullptr,
  32. gfx::Point(hotspot_x, hotspot_y));
  33. }
  34. void pointer_release(wl_client* client, wl_resource* resource) {
  35. wl_resource_destroy(resource);
  36. }
  37. const struct wl_pointer_interface pointer_implementation = {pointer_set_cursor,
  38. pointer_release};
  39. #if BUILDFLAG(IS_CHROMEOS_ASH)
  40. ////////////////////////////////////////////////////////////////////////////////
  41. // wl_keyboard_interface:
  42. #if BUILDFLAG(USE_XKBCOMMON)
  43. void keyboard_release(wl_client* client, wl_resource* resource) {
  44. wl_resource_destroy(resource);
  45. }
  46. const struct wl_keyboard_interface keyboard_implementation = {keyboard_release};
  47. #endif // BUILDFLAG(USE_XKBCOMMON)
  48. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  49. ////////////////////////////////////////////////////////////////////////////////
  50. // wl_touch_interface:
  51. void touch_release(wl_client* client, wl_resource* resource) {
  52. wl_resource_destroy(resource);
  53. }
  54. const struct wl_touch_interface touch_implementation = {touch_release};
  55. ////////////////////////////////////////////////////////////////////////////////
  56. // wl_seat_interface:
  57. void seat_get_pointer(wl_client* client, wl_resource* resource, uint32_t id) {
  58. auto* data = GetUserDataAs<WaylandSeat>(resource);
  59. wl_resource* pointer_resource = wl_resource_create(
  60. client, &wl_pointer_interface, wl_resource_get_version(resource), id);
  61. SetImplementation(
  62. pointer_resource, &pointer_implementation,
  63. std::make_unique<Pointer>(
  64. new WaylandPointerDelegate(pointer_resource, data->serial_tracker),
  65. data->seat));
  66. }
  67. void seat_get_keyboard(wl_client* client, wl_resource* resource, uint32_t id) {
  68. #if BUILDFLAG(IS_CHROMEOS_ASH) && BUILDFLAG(USE_XKBCOMMON)
  69. auto* data = GetUserDataAs<WaylandSeat>(resource);
  70. uint32_t version = wl_resource_get_version(resource);
  71. wl_resource* keyboard_resource =
  72. wl_resource_create(client, &wl_keyboard_interface, version, id);
  73. auto keyboard =
  74. std::make_unique<Keyboard>(std::make_unique<WaylandKeyboardDelegate>(
  75. keyboard_resource, data->serial_tracker),
  76. data->seat);
  77. SetImplementation(keyboard_resource, &keyboard_implementation,
  78. std::move(keyboard));
  79. #else
  80. NOTIMPLEMENTED();
  81. #endif // BUILDFLAG(IS_CHROMEOS_ASH) && BUILDFLAG(USE_XKBCOMMON)
  82. }
  83. void seat_get_touch(wl_client* client, wl_resource* resource, uint32_t id) {
  84. auto* data = GetUserDataAs<WaylandSeat>(resource);
  85. wl_resource* touch_resource = wl_resource_create(
  86. client, &wl_touch_interface, wl_resource_get_version(resource), id);
  87. SetImplementation(
  88. touch_resource, &touch_implementation,
  89. std::make_unique<Touch>(
  90. new WaylandTouchDelegate(touch_resource, data->serial_tracker),
  91. data->seat));
  92. }
  93. void seat_release(wl_client* client, wl_resource* resource) {
  94. wl_resource_destroy(resource);
  95. }
  96. const struct wl_seat_interface seat_implementation = {
  97. seat_get_pointer, seat_get_keyboard, seat_get_touch, seat_release};
  98. } // namespace
  99. void bind_seat(wl_client* client, void* data, uint32_t version, uint32_t id) {
  100. wl_resource* resource = wl_resource_create(
  101. client, &wl_seat_interface, std::min(version, kWlSeatVersion), id);
  102. wl_resource_set_implementation(resource, &seat_implementation, data, nullptr);
  103. if (version >= WL_SEAT_NAME_SINCE_VERSION)
  104. wl_seat_send_name(resource, "default");
  105. uint32_t capabilities = WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH;
  106. #if BUILDFLAG(IS_CHROMEOS_ASH) && BUILDFLAG(USE_XKBCOMMON)
  107. capabilities |= WL_SEAT_CAPABILITY_KEYBOARD;
  108. #endif // BUILDFLAG(IS_CHROMEOS_ASH) && BUILDFLAG(USE_XKBCOMMON)
  109. wl_seat_send_capabilities(resource, capabilities);
  110. }
  111. } // namespace wayland
  112. } // namespace exo