zcr_stylus_tools.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/zcr_stylus_tools.h"
  5. #include <stylus-tools-unstable-v1-server-protocol.h>
  6. #include <wayland-server-core.h>
  7. #include <wayland-server-protocol-core.h>
  8. #include "components/exo/surface.h"
  9. #include "components/exo/surface_observer.h"
  10. #include "components/exo/wayland/server_util.h"
  11. namespace exo {
  12. namespace wayland {
  13. namespace {
  14. // A property key containing a boolean set to true if the stylus_tool
  15. // object is associated with surface object.
  16. DEFINE_UI_CLASS_PROPERTY_KEY(bool, kSurfaceHasStylusToolKey, false)
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // stylus_tool interface:
  19. class StylusTool : public SurfaceObserver {
  20. public:
  21. explicit StylusTool(Surface* surface) : surface_(surface) {
  22. surface_->AddSurfaceObserver(this);
  23. surface_->SetProperty(kSurfaceHasStylusToolKey, true);
  24. }
  25. StylusTool(const StylusTool&) = delete;
  26. StylusTool& operator=(const StylusTool&) = delete;
  27. ~StylusTool() override {
  28. if (surface_) {
  29. surface_->RemoveSurfaceObserver(this);
  30. surface_->SetProperty(kSurfaceHasStylusToolKey, false);
  31. }
  32. }
  33. void SetStylusOnly() { surface_->SetStylusOnly(); }
  34. // Overridden from SurfaceObserver:
  35. void OnSurfaceDestroying(Surface* surface) override {
  36. surface->RemoveSurfaceObserver(this);
  37. surface_ = nullptr;
  38. }
  39. private:
  40. Surface* surface_;
  41. };
  42. void stylus_tool_destroy(wl_client* client, wl_resource* resource) {
  43. wl_resource_destroy(resource);
  44. }
  45. void stylus_tool_set_stylus_only(wl_client* client, wl_resource* resource) {
  46. GetUserDataAs<StylusTool>(resource)->SetStylusOnly();
  47. }
  48. const struct zcr_stylus_tool_v1_interface stylus_tool_implementation = {
  49. stylus_tool_destroy, stylus_tool_set_stylus_only};
  50. ////////////////////////////////////////////////////////////////////////////////
  51. // stylus_tools interface:
  52. void stylus_tools_destroy(wl_client* client, wl_resource* resource) {
  53. wl_resource_destroy(resource);
  54. }
  55. void stylus_tools_get_stylus_tool(wl_client* client,
  56. wl_resource* resource,
  57. uint32_t id,
  58. wl_resource* surface_resource) {
  59. Surface* surface = GetUserDataAs<Surface>(surface_resource);
  60. if (surface->GetProperty(kSurfaceHasStylusToolKey)) {
  61. wl_resource_post_error(
  62. resource, ZCR_STYLUS_TOOLS_V1_ERROR_STYLUS_TOOL_EXISTS,
  63. "a stylus_tool object for that surface already exists");
  64. return;
  65. }
  66. wl_resource* stylus_tool_resource =
  67. wl_resource_create(client, &zcr_stylus_tool_v1_interface, 1, id);
  68. SetImplementation(stylus_tool_resource, &stylus_tool_implementation,
  69. std::make_unique<StylusTool>(surface));
  70. }
  71. const struct zcr_stylus_tools_v1_interface stylus_tools_implementation = {
  72. stylus_tools_destroy, stylus_tools_get_stylus_tool};
  73. } // namespace
  74. void bind_stylus_tools(wl_client* client,
  75. void* data,
  76. uint32_t version,
  77. uint32_t id) {
  78. wl_resource* resource =
  79. wl_resource_create(client, &zcr_stylus_tools_v1_interface, 1, id);
  80. wl_resource_set_implementation(resource, &stylus_tools_implementation, data,
  81. nullptr);
  82. }
  83. } // namespace wayland
  84. } // namespace exo