zwp_keyboard_shortcuts_inhibit_manager.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2022 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_keyboard_shortcuts_inhibit_manager.h"
  5. #include <keyboard-shortcuts-inhibit-unstable-v1-server-protocol.h>
  6. #include "components/exo/surface.h"
  7. #include "components/exo/surface_observer.h"
  8. #include "components/exo/wayland/server_util.h"
  9. namespace exo::wayland {
  10. namespace {
  11. // Tracks the keyboard-shortcut-inhibitor setting, and notifies corresponding
  12. // Surface on unset.
  13. class KeyboardShortcutsInhibitor : public SurfaceObserver {
  14. public:
  15. explicit KeyboardShortcutsInhibitor(Surface* surface) : surface_(surface) {
  16. surface->SetKeyboardShortcutsInhibited(true);
  17. surface_->AddSurfaceObserver(this);
  18. }
  19. ~KeyboardShortcutsInhibitor() override {
  20. if (surface_) {
  21. surface_->RemoveSurfaceObserver(this);
  22. surface_->SetKeyboardShortcutsInhibited(false);
  23. }
  24. }
  25. void OnSurfaceDestroying(Surface* surface) override {
  26. DCHECK_EQ(surface, surface_);
  27. surface->RemoveSurfaceObserver(this);
  28. surface_ = nullptr;
  29. }
  30. private:
  31. Surface* surface_;
  32. };
  33. void keyboard_shortcuts_inhibitor_destroy(wl_client* client,
  34. wl_resource* resource) {
  35. wl_resource_destroy(resource);
  36. }
  37. constexpr struct zwp_keyboard_shortcuts_inhibitor_v1_interface
  38. keyboard_shortcuts_inhibitor_implementation = {
  39. keyboard_shortcuts_inhibitor_destroy,
  40. };
  41. void keyboard_shortcuts_inhibit_manager_destroy(wl_client* client,
  42. wl_resource* resource) {
  43. wl_resource_destroy(resource);
  44. }
  45. void keyboard_shortcuts_inhibit_manager_inhibit_shortcuts(
  46. wl_client* client,
  47. wl_resource* resource,
  48. uint32_t id,
  49. wl_resource* surface_resource,
  50. wl_resource* seat) {
  51. Surface* surface = GetUserDataAs<Surface>(surface_resource);
  52. if (surface->is_keyboard_shortcuts_inhibited()) {
  53. wl_resource_post_error(
  54. resource,
  55. ZWP_KEYBOARD_SHORTCUTS_INHIBIT_MANAGER_V1_ERROR_ALREADY_INHIBITED,
  56. "the associated surface has already been set to inhibit keyboard "
  57. "shortcuts");
  58. return;
  59. }
  60. uint32_t version = wl_resource_get_version(resource);
  61. wl_resource* keyboard_shortcuts_inhibitor_resource = wl_resource_create(
  62. client, &zwp_keyboard_shortcuts_inhibitor_v1_interface, version, id);
  63. SetImplementation(keyboard_shortcuts_inhibitor_resource,
  64. &keyboard_shortcuts_inhibitor_implementation,
  65. std::make_unique<KeyboardShortcutsInhibitor>(surface));
  66. }
  67. constexpr struct zwp_keyboard_shortcuts_inhibit_manager_v1_interface
  68. keyboard_shortcuts_inhibit_manager_implementation = {
  69. keyboard_shortcuts_inhibit_manager_destroy,
  70. keyboard_shortcuts_inhibit_manager_inhibit_shortcuts,
  71. };
  72. } // namespace
  73. void bind_keyboard_shortcuts_inhibit_manager(wl_client* client,
  74. void* data,
  75. uint32_t version,
  76. uint32_t id) {
  77. wl_resource* resource = wl_resource_create(
  78. client, &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, version,
  79. id);
  80. wl_resource_set_implementation(
  81. resource, &keyboard_shortcuts_inhibit_manager_implementation, data,
  82. nullptr);
  83. }
  84. } // namespace exo::wayland