wl_shell.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_shell.h"
  5. #include <wayland-server-core.h>
  6. #include <wayland-server-protocol-core.h>
  7. #include "ash/public/cpp/shell_window_ids.h"
  8. #include "base/bind.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #include "components/exo/display.h"
  11. #include "components/exo/shell_surface.h"
  12. #include "components/exo/wayland/server_util.h"
  13. namespace exo {
  14. namespace wayland {
  15. namespace {
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // wl_shell_surface_interface:
  18. void shell_surface_pong(wl_client* client,
  19. wl_resource* resource,
  20. uint32_t serial) {
  21. NOTIMPLEMENTED();
  22. }
  23. void shell_surface_move(wl_client* client,
  24. wl_resource* resource,
  25. wl_resource* seat_resource,
  26. uint32_t serial) {
  27. GetUserDataAs<ShellSurface>(resource)->StartMove();
  28. }
  29. void shell_surface_resize(wl_client* client,
  30. wl_resource* resource,
  31. wl_resource* seat_resource,
  32. uint32_t serial,
  33. uint32_t edges) {
  34. NOTIMPLEMENTED();
  35. }
  36. void shell_surface_set_toplevel(wl_client* client, wl_resource* resource) {
  37. GetUserDataAs<ShellSurface>(resource)->SetEnabled(true);
  38. }
  39. void shell_surface_set_transient(wl_client* client,
  40. wl_resource* resource,
  41. wl_resource* parent_resource,
  42. int x,
  43. int y,
  44. uint32_t flags) {
  45. ShellSurface* shell_surface = GetUserDataAs<ShellSurface>(resource);
  46. if (shell_surface->GetEnabled())
  47. return;
  48. if (flags & WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
  49. shell_surface->SetContainer(ash::kShellWindowId_SystemModalContainer);
  50. shell_surface->SetActivatable(false);
  51. }
  52. shell_surface->SetEnabled(true);
  53. }
  54. void shell_surface_set_fullscreen(wl_client* client,
  55. wl_resource* resource,
  56. uint32_t method,
  57. uint32_t framerate,
  58. wl_resource* output_resource) {
  59. ShellSurface* shell_surface = GetUserDataAs<ShellSurface>(resource);
  60. if (shell_surface->GetEnabled())
  61. return;
  62. shell_surface->SetEnabled(true);
  63. shell_surface->SetFullscreen(true);
  64. }
  65. void shell_surface_set_popup(wl_client* client,
  66. wl_resource* resource,
  67. wl_resource* seat_resource,
  68. uint32_t serial,
  69. wl_resource* parent_resource,
  70. int32_t x,
  71. int32_t y,
  72. uint32_t flags) {
  73. NOTIMPLEMENTED();
  74. }
  75. void shell_surface_set_maximized(wl_client* client,
  76. wl_resource* resource,
  77. wl_resource* output_resource) {
  78. ShellSurface* shell_surface = GetUserDataAs<ShellSurface>(resource);
  79. if (shell_surface->GetEnabled())
  80. return;
  81. shell_surface->SetEnabled(true);
  82. shell_surface->Maximize();
  83. }
  84. void shell_surface_set_title(wl_client* client,
  85. wl_resource* resource,
  86. const char* title) {
  87. GetUserDataAs<ShellSurface>(resource)->SetTitle(
  88. std::u16string(base::UTF8ToUTF16(title)));
  89. }
  90. void shell_surface_set_class(wl_client* client,
  91. wl_resource* resource,
  92. const char* clazz) {
  93. GetUserDataAs<ShellSurface>(resource)->SetApplicationId(clazz);
  94. }
  95. const struct wl_shell_surface_interface shell_surface_implementation = {
  96. shell_surface_pong, shell_surface_move,
  97. shell_surface_resize, shell_surface_set_toplevel,
  98. shell_surface_set_transient, shell_surface_set_fullscreen,
  99. shell_surface_set_popup, shell_surface_set_maximized,
  100. shell_surface_set_title, shell_surface_set_class};
  101. ////////////////////////////////////////////////////////////////////////////////
  102. // wl_shell_interface:
  103. uint32_t HandleShellSurfaceConfigureCallback(
  104. wl_resource* resource,
  105. const gfx::Rect& bounds,
  106. chromeos::WindowStateType state_type,
  107. bool resizing,
  108. bool activated,
  109. const gfx::Vector2d& origin_offset) {
  110. wl_shell_surface_send_configure(resource, WL_SHELL_SURFACE_RESIZE_NONE,
  111. bounds.width(), bounds.height());
  112. wl_client_flush(wl_resource_get_client(resource));
  113. return 0;
  114. }
  115. void shell_get_shell_surface(wl_client* client,
  116. wl_resource* resource,
  117. uint32_t id,
  118. wl_resource* surface) {
  119. std::unique_ptr<ShellSurface> shell_surface =
  120. GetUserDataAs<Display>(resource)->CreateShellSurface(
  121. GetUserDataAs<Surface>(surface));
  122. if (!shell_surface) {
  123. wl_resource_post_error(resource, WL_SHELL_ERROR_ROLE,
  124. "surface has already been assigned a role");
  125. return;
  126. }
  127. wl_resource* shell_surface_resource =
  128. wl_resource_create(client, &wl_shell_surface_interface, 1, id);
  129. // Shell surfaces are initially disabled and needs to be explicitly mapped
  130. // before they are enabled and can become visible.
  131. shell_surface->SetEnabled(false);
  132. shell_surface->SetSecurityDelegate(GetSecurityDelegate(client));
  133. shell_surface->set_configure_callback(
  134. base::BindRepeating(&HandleShellSurfaceConfigureCallback,
  135. base::Unretained(shell_surface_resource)));
  136. shell_surface->set_surface_destroyed_callback(base::BindOnce(
  137. &wl_resource_destroy, base::Unretained(shell_surface_resource)));
  138. SetImplementation(shell_surface_resource, &shell_surface_implementation,
  139. std::move(shell_surface));
  140. }
  141. const struct wl_shell_interface shell_implementation = {
  142. shell_get_shell_surface};
  143. } // namespace
  144. void bind_shell(wl_client* client, void* data, uint32_t version, uint32_t id) {
  145. wl_resource* resource =
  146. wl_resource_create(client, &wl_shell_interface, 1, id);
  147. wl_resource_set_implementation(resource, &shell_implementation, data,
  148. nullptr);
  149. }
  150. } // namespace wayland
  151. } // namespace exo