|
| 1 | +#include <stdlib.h> |
| 2 | +#include <string.h> |
| 3 | +#include <wlr/types/wlr_xdg_shell.h> |
| 4 | +#include <wlr/types/wlr_xdg_decoration_v1.h> |
| 5 | +#include <wlr/types/wlr_scene.h> |
| 6 | +#include <wlr/util/log.h> |
| 7 | + |
| 8 | +#include "server.h" |
| 9 | +#include "view.h" |
| 10 | +#include "xdg_shell.h" |
| 11 | + |
| 12 | +static void |
| 13 | +xdg_decoration_set_mode(struct gf_xdg_decoration *xdg_decoration) |
| 14 | +{ |
| 15 | + enum wlr_xdg_toplevel_decoration_v1_mode mode; |
| 16 | + if (xdg_decoration->server->xdg_decoration) { |
| 17 | + mode = WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE; |
| 18 | + } else { |
| 19 | + mode = WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE; |
| 20 | + } |
| 21 | + wlr_xdg_toplevel_decoration_v1_set_mode(xdg_decoration->wlr_decoration, mode); |
| 22 | +} |
| 23 | + |
| 24 | +static void |
| 25 | +xdg_decoration_handle_destroy(struct wl_listener *listener, void *data) |
| 26 | +{ |
| 27 | + struct gf_xdg_decoration *xdg_decoration = wl_container_of(listener, xdg_decoration, destroy); |
| 28 | + |
| 29 | + wl_list_remove(&xdg_decoration->destroy.link); |
| 30 | + wl_list_remove(&xdg_decoration->commit.link); |
| 31 | + wl_list_remove(&xdg_decoration->request_mode.link); |
| 32 | + free(xdg_decoration); |
| 33 | +} |
| 34 | + |
| 35 | +static void |
| 36 | +xdg_decoration_handle_commit(struct wl_listener *listener, void *data) |
| 37 | +{ |
| 38 | + struct gf_xdg_decoration *xdg_decoration = wl_container_of(listener, xdg_decoration, commit); |
| 39 | + |
| 40 | + if (xdg_decoration->wlr_decoration->toplevel->base->initial_commit) { |
| 41 | + xdg_decoration_set_mode(xdg_decoration); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +static void |
| 46 | +xdg_decoration_handle_request_mode(struct wl_listener *listener, void *data) |
| 47 | +{ |
| 48 | + struct gf_xdg_decoration *xdg_decoration = wl_container_of(listener, xdg_decoration, request_mode); |
| 49 | + |
| 50 | + if (xdg_decoration->wlr_decoration->toplevel->base->initialized) { |
| 51 | + xdg_decoration_set_mode(xdg_decoration); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void |
| 56 | +handle_xdg_toplevel_decoration(struct wl_listener *listener, void *data) |
| 57 | +{ |
| 58 | + struct gf_server *server = wl_container_of(listener, server, xdg_toplevel_decoration); |
| 59 | + struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration = data; |
| 60 | + |
| 61 | + struct gf_xdg_decoration *xdg_decoration = calloc(1, sizeof(*xdg_decoration)); |
| 62 | + if (!xdg_decoration) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + xdg_decoration->wlr_decoration = wlr_decoration; |
| 67 | + xdg_decoration->server = server; |
| 68 | + |
| 69 | + xdg_decoration->destroy.notify = xdg_decoration_handle_destroy; |
| 70 | + wl_signal_add(&wlr_decoration->events.destroy, &xdg_decoration->destroy); |
| 71 | + xdg_decoration->commit.notify = xdg_decoration_handle_commit; |
| 72 | + wl_signal_add(&wlr_decoration->toplevel->base->surface->events.commit, &xdg_decoration->commit); |
| 73 | + xdg_decoration->request_mode.notify = xdg_decoration_handle_request_mode; |
| 74 | + wl_signal_add(&wlr_decoration->events.request_mode, &xdg_decoration->request_mode); |
| 75 | +} |
| 76 | + |
| 77 | +static struct gf_view * |
| 78 | +popup_get_view(struct wlr_xdg_popup *popup) |
| 79 | +{ |
| 80 | + while (true) { |
| 81 | + if (popup->parent == NULL) { |
| 82 | + return NULL; |
| 83 | + } |
| 84 | + struct wlr_xdg_surface *xdg_surface = wlr_xdg_surface_try_from_wlr_surface(popup->parent); |
| 85 | + if (xdg_surface == NULL) { |
| 86 | + return NULL; |
| 87 | + } |
| 88 | + switch (xdg_surface->role) { |
| 89 | + case WLR_XDG_SURFACE_ROLE_TOPLEVEL: |
| 90 | + return xdg_surface->data; |
| 91 | + case WLR_XDG_SURFACE_ROLE_POPUP: |
| 92 | + popup = xdg_surface->popup; |
| 93 | + break; |
| 94 | + case WLR_XDG_SURFACE_ROLE_NONE: |
| 95 | + return NULL; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +static void |
| 101 | +popup_unconstrain(struct wlr_xdg_popup *popup) |
| 102 | +{ |
| 103 | + struct gf_view *view = popup_get_view(popup); |
| 104 | + if (view == NULL) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + struct gf_server *server = view->server; |
| 109 | + struct wlr_box *popup_box = &popup->current.geometry; |
| 110 | + |
| 111 | + struct wlr_output_layout *output_layout = server->output_layout; |
| 112 | + struct wlr_output *wlr_output = |
| 113 | + wlr_output_layout_output_at(output_layout, view->lx + popup_box->x, view->ly + popup_box->y); |
| 114 | + struct wlr_box output_box; |
| 115 | + wlr_output_layout_get_box(output_layout, wlr_output, &output_box); |
| 116 | + |
| 117 | + struct wlr_box output_toplevel_box = { |
| 118 | + .x = output_box.x - view->lx, |
| 119 | + .y = output_box.y - view->ly, |
| 120 | + .width = output_box.width, |
| 121 | + .height = output_box.height, |
| 122 | + }; |
| 123 | + |
| 124 | + wlr_xdg_popup_unconstrain_from_box(popup, &output_toplevel_box); |
| 125 | +} |
| 126 | + |
| 127 | +static struct gf_xdg_shell_view * |
| 128 | +xdg_shell_view_from_view(struct gf_view *view) |
| 129 | +{ |
| 130 | + return (struct gf_xdg_shell_view *) view; |
| 131 | +} |
| 132 | + |
| 133 | +static char * |
| 134 | +get_title(struct gf_view *view) |
| 135 | +{ |
| 136 | + struct gf_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); |
| 137 | + return xdg_shell_view->xdg_toplevel->title; |
| 138 | +} |
| 139 | + |
| 140 | +static void |
| 141 | +get_geometry(struct gf_view *view, int *width_out, int *height_out) |
| 142 | +{ |
| 143 | + struct gf_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); |
| 144 | + struct wlr_xdg_surface *xdg_surface = xdg_shell_view->xdg_toplevel->base; |
| 145 | + |
| 146 | + *width_out = xdg_surface->geometry.width; |
| 147 | + *height_out = xdg_surface->geometry.height; |
| 148 | +} |
| 149 | + |
| 150 | +static bool |
| 151 | +is_primary(struct gf_view *view) |
| 152 | +{ |
| 153 | + struct gf_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); |
| 154 | + struct wlr_xdg_toplevel *parent = xdg_shell_view->xdg_toplevel->parent; |
| 155 | + |
| 156 | + return parent == NULL; |
| 157 | +} |
| 158 | + |
| 159 | +static bool |
| 160 | +is_transient_for(struct gf_view *child, struct gf_view *parent) |
| 161 | +{ |
| 162 | + if (parent->type != GAMEFRAME_XDG_SHELL_VIEW) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + struct gf_xdg_shell_view *_child = xdg_shell_view_from_view(child); |
| 166 | + struct wlr_xdg_toplevel *xdg_toplevel = _child->xdg_toplevel; |
| 167 | + struct gf_xdg_shell_view *_parent = xdg_shell_view_from_view(parent); |
| 168 | + while (xdg_toplevel) { |
| 169 | + if (xdg_toplevel->parent == _parent->xdg_toplevel) { |
| 170 | + return true; |
| 171 | + } |
| 172 | + xdg_toplevel = xdg_toplevel->parent; |
| 173 | + } |
| 174 | + return false; |
| 175 | +} |
| 176 | + |
| 177 | +static void |
| 178 | +activate(struct gf_view *view, bool activate) |
| 179 | +{ |
| 180 | + struct gf_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); |
| 181 | + wlr_xdg_toplevel_set_activated(xdg_shell_view->xdg_toplevel, activate); |
| 182 | +} |
| 183 | + |
| 184 | +static void |
| 185 | +maximize(struct gf_view *view, int output_width, int output_height) |
| 186 | +{ |
| 187 | + struct gf_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); |
| 188 | + struct gf_server *server = view->server; |
| 189 | + int width = server->game_width > 0 ? server->game_width : output_width; |
| 190 | + int height = server->game_height > 0 ? server->game_height : output_height; |
| 191 | + wlr_xdg_toplevel_set_size(xdg_shell_view->xdg_toplevel, width, height); |
| 192 | + wlr_xdg_toplevel_set_maximized(xdg_shell_view->xdg_toplevel, true); |
| 193 | +} |
| 194 | + |
| 195 | +static void |
| 196 | +destroy(struct gf_view *view) |
| 197 | +{ |
| 198 | + struct gf_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); |
| 199 | + free(xdg_shell_view); |
| 200 | +} |
| 201 | + |
| 202 | +static void |
| 203 | +close(struct gf_view *view) |
| 204 | +{ |
| 205 | + struct gf_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); |
| 206 | + wlr_xdg_toplevel_send_close(xdg_shell_view->xdg_toplevel); |
| 207 | +} |
| 208 | + |
| 209 | +static void |
| 210 | +handle_xdg_toplevel_request_fullscreen(struct wl_listener *listener, void *data) |
| 211 | +{ |
| 212 | + struct gf_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, request_fullscreen); |
| 213 | + bool fullscreen = xdg_shell_view->xdg_toplevel->requested.fullscreen; |
| 214 | + |
| 215 | + if (!xdg_shell_view->xdg_toplevel->base->surface->mapped) { |
| 216 | + return; |
| 217 | + } |
| 218 | + |
| 219 | + struct wlr_box layout_box; |
| 220 | + wlr_output_layout_get_box(xdg_shell_view->view.server->output_layout, NULL, &layout_box); |
| 221 | + wlr_xdg_toplevel_set_size(xdg_shell_view->xdg_toplevel, layout_box.width, layout_box.height); |
| 222 | + wlr_xdg_toplevel_set_fullscreen(xdg_shell_view->xdg_toplevel, fullscreen); |
| 223 | + wlr_foreign_toplevel_handle_v1_set_fullscreen(xdg_shell_view->view.foreign_toplevel_handle, fullscreen); |
| 224 | +} |
| 225 | + |
| 226 | +static void |
| 227 | +handle_xdg_toplevel_unmap(struct wl_listener *listener, void *data) |
| 228 | +{ |
| 229 | + struct gf_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, unmap); |
| 230 | + struct gf_view *view = &xdg_shell_view->view; |
| 231 | + |
| 232 | + view_unmap(view); |
| 233 | +} |
| 234 | + |
| 235 | +static void |
| 236 | +handle_xdg_toplevel_map(struct wl_listener *listener, void *data) |
| 237 | +{ |
| 238 | + struct gf_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, map); |
| 239 | + struct gf_view *view = &xdg_shell_view->view; |
| 240 | + |
| 241 | + view_map(view, xdg_shell_view->xdg_toplevel->base->surface); |
| 242 | + |
| 243 | + if (xdg_shell_view->xdg_toplevel->title) |
| 244 | + wlr_foreign_toplevel_handle_v1_set_title(view->foreign_toplevel_handle, |
| 245 | + xdg_shell_view->xdg_toplevel->title); |
| 246 | + if (xdg_shell_view->xdg_toplevel->app_id) |
| 247 | + wlr_foreign_toplevel_handle_v1_set_app_id(view->foreign_toplevel_handle, |
| 248 | + xdg_shell_view->xdg_toplevel->app_id); |
| 249 | +} |
| 250 | + |
| 251 | +static void |
| 252 | +handle_xdg_toplevel_commit(struct wl_listener *listener, void *data) |
| 253 | +{ |
| 254 | + struct gf_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, commit); |
| 255 | + |
| 256 | + if (!xdg_shell_view->xdg_toplevel->base->initial_commit) { |
| 257 | + return; |
| 258 | + } |
| 259 | + |
| 260 | + wlr_xdg_toplevel_set_wm_capabilities(xdg_shell_view->xdg_toplevel, XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN); |
| 261 | + |
| 262 | + view_position(&xdg_shell_view->view); |
| 263 | +} |
| 264 | + |
| 265 | +static void |
| 266 | +handle_xdg_toplevel_destroy(struct wl_listener *listener, void *data) |
| 267 | +{ |
| 268 | + struct gf_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, destroy); |
| 269 | + struct gf_view *view = &xdg_shell_view->view; |
| 270 | + |
| 271 | + wl_list_remove(&xdg_shell_view->commit.link); |
| 272 | + wl_list_remove(&xdg_shell_view->map.link); |
| 273 | + wl_list_remove(&xdg_shell_view->unmap.link); |
| 274 | + wl_list_remove(&xdg_shell_view->destroy.link); |
| 275 | + wl_list_remove(&xdg_shell_view->request_fullscreen.link); |
| 276 | + xdg_shell_view->xdg_toplevel = NULL; |
| 277 | + |
| 278 | + view_destroy(view); |
| 279 | +} |
| 280 | + |
| 281 | +static const struct gf_view_impl xdg_shell_view_impl = { |
| 282 | + .get_title = get_title, |
| 283 | + .get_geometry = get_geometry, |
| 284 | + .is_primary = is_primary, |
| 285 | + .is_transient_for = is_transient_for, |
| 286 | + .activate = activate, |
| 287 | + .maximize = maximize, |
| 288 | + .destroy = destroy, |
| 289 | + .close = close, |
| 290 | +}; |
| 291 | + |
| 292 | +void |
| 293 | +handle_new_xdg_toplevel(struct wl_listener *listener, void *data) |
| 294 | +{ |
| 295 | + struct gf_server *server = wl_container_of(listener, server, new_xdg_toplevel); |
| 296 | + struct wlr_xdg_toplevel *toplevel = data; |
| 297 | + |
| 298 | + struct gf_xdg_shell_view *xdg_shell_view = calloc(1, sizeof(struct gf_xdg_shell_view)); |
| 299 | + if (!xdg_shell_view) { |
| 300 | + wlr_log(WLR_ERROR, "Failed to allocate XDG Shell view"); |
| 301 | + return; |
| 302 | + } |
| 303 | + |
| 304 | + view_init(&xdg_shell_view->view, server, GAMEFRAME_XDG_SHELL_VIEW, &xdg_shell_view_impl); |
| 305 | + xdg_shell_view->xdg_toplevel = toplevel; |
| 306 | + |
| 307 | + xdg_shell_view->commit.notify = handle_xdg_toplevel_commit; |
| 308 | + wl_signal_add(&toplevel->base->surface->events.commit, &xdg_shell_view->commit); |
| 309 | + xdg_shell_view->map.notify = handle_xdg_toplevel_map; |
| 310 | + wl_signal_add(&toplevel->base->surface->events.map, &xdg_shell_view->map); |
| 311 | + xdg_shell_view->unmap.notify = handle_xdg_toplevel_unmap; |
| 312 | + wl_signal_add(&toplevel->base->surface->events.unmap, &xdg_shell_view->unmap); |
| 313 | + xdg_shell_view->destroy.notify = handle_xdg_toplevel_destroy; |
| 314 | + wl_signal_add(&toplevel->events.destroy, &xdg_shell_view->destroy); |
| 315 | + xdg_shell_view->request_fullscreen.notify = handle_xdg_toplevel_request_fullscreen; |
| 316 | + wl_signal_add(&toplevel->events.request_fullscreen, &xdg_shell_view->request_fullscreen); |
| 317 | + |
| 318 | + toplevel->base->data = xdg_shell_view; |
| 319 | +} |
| 320 | + |
| 321 | +static void |
| 322 | +popup_handle_destroy(struct wl_listener *listener, void *data) |
| 323 | +{ |
| 324 | + struct gf_xdg_popup *popup = wl_container_of(listener, popup, destroy); |
| 325 | + wl_list_remove(&popup->destroy.link); |
| 326 | + wl_list_remove(&popup->commit.link); |
| 327 | + wl_list_remove(&popup->reposition.link); |
| 328 | + free(popup); |
| 329 | +} |
| 330 | + |
| 331 | +static void |
| 332 | +popup_handle_commit(struct wl_listener *listener, void *data) |
| 333 | +{ |
| 334 | + struct gf_xdg_popup *popup = wl_container_of(listener, popup, commit); |
| 335 | + |
| 336 | + if (popup->xdg_popup->base->initial_commit) { |
| 337 | + popup_unconstrain(popup->xdg_popup); |
| 338 | + } |
| 339 | +} |
| 340 | + |
| 341 | +static void |
| 342 | +popup_handle_reposition(struct wl_listener *listener, void *data) |
| 343 | +{ |
| 344 | + struct gf_xdg_popup *popup = wl_container_of(listener, popup, reposition); |
| 345 | + |
| 346 | + popup_unconstrain(popup->xdg_popup); |
| 347 | +} |
| 348 | + |
| 349 | +void |
| 350 | +handle_new_xdg_popup(struct wl_listener *listener, void *data) |
| 351 | +{ |
| 352 | + struct gf_server *server = wl_container_of(listener, server, new_xdg_popup); |
| 353 | + struct wlr_xdg_popup *wlr_popup = data; |
| 354 | + |
| 355 | + struct gf_view *view = popup_get_view(wlr_popup); |
| 356 | + if (view == NULL) { |
| 357 | + return; |
| 358 | + } |
| 359 | + |
| 360 | + struct wlr_scene_tree *parent_scene_tree = NULL; |
| 361 | + struct wlr_xdg_surface *parent = wlr_xdg_surface_try_from_wlr_surface(wlr_popup->parent); |
| 362 | + if (parent == NULL) { |
| 363 | + return; |
| 364 | + } |
| 365 | + switch (parent->role) { |
| 366 | + case WLR_XDG_SURFACE_ROLE_TOPLEVEL: |
| 367 | + parent_scene_tree = view->scene_tree; |
| 368 | + break; |
| 369 | + case WLR_XDG_SURFACE_ROLE_POPUP: |
| 370 | + parent_scene_tree = parent->data; |
| 371 | + break; |
| 372 | + case WLR_XDG_SURFACE_ROLE_NONE: |
| 373 | + return; |
| 374 | + } |
| 375 | + |
| 376 | + struct wlr_scene_tree *popup_tree = wlr_scene_xdg_surface_create(parent_scene_tree, wlr_popup->base); |
| 377 | + if (popup_tree == NULL) { |
| 378 | + wlr_log(WLR_ERROR, "Failed to create scene for popup"); |
| 379 | + return; |
| 380 | + } |
| 381 | + |
| 382 | + struct gf_xdg_popup *popup = calloc(1, sizeof(*popup)); |
| 383 | + if (popup == NULL) { |
| 384 | + wlr_log(WLR_ERROR, "Failed to allocate popup"); |
| 385 | + return; |
| 386 | + } |
| 387 | + popup->xdg_popup = wlr_popup; |
| 388 | + |
| 389 | + popup->destroy.notify = popup_handle_destroy; |
| 390 | + wl_signal_add(&wlr_popup->base->events.destroy, &popup->destroy); |
| 391 | + popup->commit.notify = popup_handle_commit; |
| 392 | + wl_signal_add(&wlr_popup->base->surface->events.commit, &popup->commit); |
| 393 | + popup->reposition.notify = popup_handle_reposition; |
| 394 | + wl_signal_add(&wlr_popup->events.reposition, &popup->reposition); |
| 395 | +} |
0 commit comments