From 355b9dbff5b81054ab8c8690084527bba5cda012 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 25 Oct 2021 18:29:24 +0200 Subject: [PATCH 1/3] scene: add wlr_scene_xdg_popup_create This allows compositors to easily add an xdg_popup to the scene-graph while retaining the ability to unconstraint the popup and decide its final position. Compositors can handle new popups with the wlr_xdg_shell.new_surface event, get the parent scene-graph node via wlr_xdg_popup.parent.data, create a new scene-graph node via wlr_scene_xdg_popup_tree_create, and unconstraint the popup if they want to. --- include/wlr/types/wlr_scene.h | 8 +++ types/meson.build | 1 + types/scene/xdg_shell.c | 105 ++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 types/scene/xdg_shell.c diff --git a/include/wlr/types/wlr_scene.h b/include/wlr/types/wlr_scene.h index 538b894125..baed996296 100644 --- a/include/wlr/types/wlr_scene.h +++ b/include/wlr/types/wlr_scene.h @@ -25,6 +25,7 @@ struct wlr_output; struct wlr_output_layout; +struct wlr_xdg_popup; enum wlr_scene_node_type { WLR_SCENE_NODE_ROOT, @@ -298,4 +299,11 @@ bool wlr_scene_attach_output_layout(struct wlr_scene *scene, struct wlr_scene_node *wlr_scene_subsurface_tree_create( struct wlr_scene_node *parent, struct wlr_surface *surface); +/** + * Add a node displaying an xdg_popup and all of its sub-surfaces to the + * scene-graph. + */ +struct wlr_scene_node *wlr_scene_xdg_popup_create( + struct wlr_scene_node *parent, struct wlr_xdg_popup *popup); + #endif diff --git a/types/meson.build b/types/meson.build index 89e834cec1..1694ac94a5 100644 --- a/types/meson.build +++ b/types/meson.build @@ -10,6 +10,7 @@ wlr_files += files( 'scene/subsurface_tree.c', 'scene/wlr_scene.c', 'scene/output_layout.c', + 'scene/xdg_shell.c', 'seat/wlr_seat_keyboard.c', 'seat/wlr_seat_pointer.c', 'seat/wlr_seat_touch.c', diff --git a/types/scene/xdg_shell.c b/types/scene/xdg_shell.c new file mode 100644 index 0000000000..c2fd63f421 --- /dev/null +++ b/types/scene/xdg_shell.c @@ -0,0 +1,105 @@ +#include +#include +#include + +struct wlr_scene_xdg_popup { + struct wlr_scene_tree *tree; + struct wlr_xdg_popup *popup; + struct wlr_scene_node *surface_node; + + struct wl_listener tree_destroy; + struct wl_listener popup_destroy; + struct wl_listener popup_map; + struct wl_listener popup_unmap; + struct wl_listener popup_configure; +}; + +static void scene_popup_handle_tree_destroy(struct wl_listener *listener, + void *data) { + struct wlr_scene_xdg_popup *scene_popup = + wl_container_of(listener, scene_popup, tree_destroy); + // tree and surface_node will be cleaned up by scene_node_finish + wl_list_remove(&scene_popup->tree_destroy.link); + wl_list_remove(&scene_popup->popup_destroy.link); + free(scene_popup); +} + +static void scene_popup_handle_popup_destroy(struct wl_listener *listener, + void *data) { + struct wlr_scene_xdg_popup *scene_popup = + wl_container_of(listener, scene_popup, popup_destroy); + wlr_scene_node_destroy(&scene_popup->tree->node); +} + +static void scene_popup_handle_popup_map(struct wl_listener *listener, + void *data) { + struct wlr_scene_xdg_popup *scene_popup = + wl_container_of(listener, scene_popup, popup_map); + wlr_scene_node_set_enabled(&scene_popup->tree->node, true); +} + +static void scene_popup_handle_popup_unmap(struct wl_listener *listener, + void *data) { + struct wlr_scene_xdg_popup *scene_popup = + wl_container_of(listener, scene_popup, popup_unmap); + wlr_scene_node_set_enabled(&scene_popup->tree->node, false); +} + +static void scene_popup_update_position( + struct wlr_scene_xdg_popup *scene_popup) { + double sx, sy; + wlr_xdg_popup_get_position(scene_popup->popup, &sx, &sy); + wlr_scene_node_set_position(&scene_popup->tree->node, sx, sy); +} + +static void scene_popup_handle_popup_configure(struct wl_listener *listener, + void *data) { + struct wlr_scene_xdg_popup *scene_popup = + wl_container_of(listener, scene_popup, popup_configure); + scene_popup_update_position(scene_popup); +} + +struct wlr_scene_node *wlr_scene_xdg_popup_create( + struct wlr_scene_node *parent, struct wlr_xdg_popup *popup) { + struct wlr_scene_xdg_popup *scene_popup = calloc(1, sizeof(*scene_popup)); + if (scene_popup == NULL) { + return NULL; + } + + scene_popup->popup = popup; + + scene_popup->tree = wlr_scene_tree_create(parent); + if (scene_popup->tree == NULL) { + free(scene_popup); + return NULL; + } + + scene_popup->surface_node = wlr_scene_subsurface_tree_create( + &scene_popup->tree->node, popup->base->surface); + if (scene_popup->surface_node == NULL) { + wlr_scene_node_destroy(&scene_popup->tree->node); + free(scene_popup); + return NULL; + } + + scene_popup->tree_destroy.notify = scene_popup_handle_tree_destroy; + wl_signal_add(&scene_popup->tree->node.events.destroy, + &scene_popup->tree_destroy); + + scene_popup->popup_destroy.notify = scene_popup_handle_popup_destroy; + wl_signal_add(&popup->base->events.destroy, &scene_popup->popup_destroy); + + scene_popup->popup_map.notify = scene_popup_handle_popup_map; + wl_signal_add(&popup->base->events.map, &scene_popup->popup_map); + + scene_popup->popup_unmap.notify = scene_popup_handle_popup_unmap; + wl_signal_add(&popup->base->events.unmap, &scene_popup->popup_unmap); + + scene_popup->popup_configure.notify = scene_popup_handle_popup_configure; + wl_signal_add(&popup->base->events.configure, &scene_popup->popup_configure); + + wlr_scene_node_set_enabled(&scene_popup->tree->node, popup->base->mapped); + scene_popup_update_position(scene_popup); + + return &scene_popup->tree->node; +} From 96c86e4aeb6ee4f231f07ffab4c6b5e9bcdf3c8b Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Tue, 12 Oct 2021 20:48:26 +0200 Subject: [PATCH 2/3] tinywl: use wlr_scene --- tinywl/tinywl.c | 336 ++++++++++++++++++++++-------------------------- 1 file changed, 157 insertions(+), 179 deletions(-) diff --git a/tinywl/tinywl.c b/tinywl/tinywl.c index b11549beac..4bf1374d1c 100644 --- a/tinywl/tinywl.c +++ b/tinywl/tinywl.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200112L +#include #include #include #include @@ -7,16 +8,15 @@ #include #include #include -#include #include #include #include #include #include -#include #include #include #include +#include #include #include #include @@ -33,7 +33,7 @@ enum tinywl_cursor_mode { struct tinywl_server { struct wl_display *wl_display; struct wlr_backend *backend; - struct wlr_renderer *renderer; + struct wlr_scene *scene; struct wlr_xdg_shell *xdg_shell; struct wl_listener new_xdg_surface; @@ -74,15 +74,26 @@ struct tinywl_view { struct wl_list link; struct tinywl_server *server; struct wlr_xdg_surface *xdg_surface; + struct wlr_scene_node *scene_node; struct wl_listener map; struct wl_listener unmap; struct wl_listener destroy; + struct wl_listener new_popup; struct wl_listener request_move; struct wl_listener request_resize; - bool mapped; int x, y; }; +struct tinywl_popup { + struct tinywl_view *view; + struct wlr_xdg_popup *xdg_popup; + struct wlr_scene_node *scene_node; + struct wl_listener map; + struct wl_listener unmap; + struct wl_listener destroy; + struct wl_listener new_popup; +}; + struct tinywl_keyboard { struct wl_list link; struct tinywl_server *server; @@ -116,6 +127,7 @@ static void focus_view(struct tinywl_view *view, struct wlr_surface *surface) { } struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); /* Move the view to the front */ + wlr_scene_node_raise_to_top(view->scene_node); wl_list_remove(&view->link); wl_list_insert(&server->views, &view->link); /* Activate the new surface */ @@ -164,14 +176,9 @@ static bool handle_keybinding(struct tinywl_server *server, xkb_keysym_t sym) { if (wl_list_length(&server->views) < 2) { break; } - struct tinywl_view *current_view = wl_container_of( - server->views.next, current_view, link); struct tinywl_view *next_view = wl_container_of( - current_view->link.next, next_view, link); + server->views.prev, next_view, link); focus_view(next_view, next_view->xdg_surface->surface); - /* Move the previous view to the end of the list */ - wl_list_remove(¤t_view->link); - wl_list_insert(server->views.prev, ¤t_view->link); break; default: return false; @@ -197,7 +204,8 @@ static void keyboard_handle_key( bool handled = false; uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); - if ((modifiers & WLR_MODIFIER_ALT) && event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { + if ((modifiers & WLR_MODIFIER_ALT) && + event->state == WL_KEYBOARD_KEY_STATE_PRESSED) { /* If alt is held down and this button was _pressed_, we attempt to * process it as a compositor keybinding. */ for (int i = 0; i < nsyms; i++) { @@ -308,52 +316,32 @@ static void seat_request_set_selection(struct wl_listener *listener, void *data) wlr_seat_set_selection(server->seat, event->source, event->serial); } -static bool view_at(struct tinywl_view *view, - double lx, double ly, struct wlr_surface **surface, - double *sx, double *sy) { - /* - * XDG toplevels may have nested surfaces, such as popup windows for context - * menus or tooltips. This function tests if any of those are underneath the - * coordinates lx and ly (in output Layout Coordinates). If so, it sets the - * surface pointer to that wlr_surface and the sx and sy coordinates to the - * coordinates relative to that surface's top-left corner. - */ - double view_sx = lx - view->x; - double view_sy = ly - view->y; - - double _sx, _sy; - struct wlr_surface *_surface = NULL; - _surface = wlr_xdg_surface_surface_at( - view->xdg_surface, view_sx, view_sy, &_sx, &_sy); - - if (_surface != NULL) { - *sx = _sx; - *sy = _sy; - *surface = _surface; - return true; - } - - return false; -} - static struct tinywl_view *desktop_view_at( struct tinywl_server *server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy) { - /* This iterates over all of our surfaces and attempts to find one under the - * cursor. This relies on server->views being ordered from top-to-bottom. */ - struct tinywl_view *view; - wl_list_for_each(view, &server->views, link) { - if (view_at(view, lx, ly, surface, sx, sy)) { - return view; - } + /* This returns the topmost node in the scene at the given layout coords. + * we only care about surface nodes as we are specifically looking for a + * surface in the surface tree of a tinywl_view. */ + struct wlr_scene_node *node = wlr_scene_node_at(&server->scene->node, + lx, ly, sx, sy); + if (node == NULL || node->type != WLR_SCENE_NODE_SURFACE) { + return NULL; + } + *surface = wlr_scene_surface_from_node(node)->surface; + /* Find the node corresponding to the tinywl_view at the root of this + * surface tree, it is the only one for which we set the data field. */ + while (node != NULL && node->data == NULL) { + node = node->parent; } - return NULL; + return node->data; } static void process_cursor_move(struct tinywl_server *server, uint32_t time) { /* Move the grabbed view to the new position. */ - server->grabbed_view->x = server->cursor->x - server->grab_x; - server->grabbed_view->y = server->cursor->y - server->grab_y; + struct tinywl_view *view = server->grabbed_view; + view->x = server->cursor->x - server->grab_x; + view->y = server->cursor->y - server->grab_y; + wlr_scene_node_set_position(view->scene_node, view->x, view->y); } static void process_cursor_resize(struct tinywl_server *server, uint32_t time) { @@ -402,6 +390,7 @@ static void process_cursor_resize(struct tinywl_server *server, uint32_t time) { wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box); view->x = new_left - geo_box.x; view->y = new_top - geo_box.y; + wlr_scene_node_set_position(view->scene_node, view->x, view->y); int new_width = new_right - new_left; int new_height = new_bottom - new_top; @@ -528,74 +517,12 @@ static void server_cursor_frame(struct wl_listener *listener, void *data) { wlr_seat_pointer_notify_frame(server->seat); } -/* Used to move all of the data necessary to render a surface from the top-level - * frame handler to the per-surface render function. */ -struct render_data { - struct wlr_output *output; - struct wlr_renderer *renderer; - struct tinywl_view *view; - struct timespec *when; -}; - -static void render_surface(struct wlr_surface *surface, +// TODO: We should avoid sending the frame done event twice if a surface +// appears on multiple outputs. +// https://github.com/swaywm/wlroots/issues/3210 +static void send_frame_done(struct wlr_surface *surface, int sx, int sy, void *data) { - /* This function is called for every surface that needs to be rendered. */ - struct render_data *rdata = data; - struct tinywl_view *view = rdata->view; - struct wlr_output *output = rdata->output; - - /* We first obtain a wlr_texture, which is a GPU resource. wlroots - * automatically handles negotiating these with the client. The underlying - * resource could be an opaque handle passed from the client, or the client - * could have sent a pixel buffer which we copied to the GPU, or a few other - * means. You don't have to worry about this, wlroots takes care of it. */ - struct wlr_texture *texture = wlr_surface_get_texture(surface); - if (texture == NULL) { - return; - } - - /* The view has a position in layout coordinates. If you have two displays, - * one next to the other, both 1080p, a view on the rightmost display might - * have layout coordinates of 2000,100. We need to translate that to - * output-local coordinates, or (2000 - 1920). */ - double ox = 0, oy = 0; - wlr_output_layout_output_coords( - view->server->output_layout, output, &ox, &oy); - ox += view->x + sx, oy += view->y + sy; - - /* We also have to apply the scale factor for HiDPI outputs. This is only - * part of the puzzle, TinyWL does not fully support HiDPI. */ - struct wlr_box box = { - .x = ox * output->scale, - .y = oy * output->scale, - .width = surface->current.width * output->scale, - .height = surface->current.height * output->scale, - }; - - /* - * Those familiar with OpenGL are also familiar with the role of matricies - * in graphics programming. We need to prepare a matrix to render the view - * with. wlr_matrix_project_box is a helper which takes a box with a desired - * x, y coordinates, width and height, and an output geometry, then - * prepares an orthographic projection and multiplies the necessary - * transforms to produce a model-view-projection matrix. - * - * Naturally you can do this any way you like, for example to make a 3D - * compositor. - */ - float matrix[9]; - enum wl_output_transform transform = - wlr_output_transform_invert(surface->current.transform); - wlr_matrix_project_box(matrix, &box, transform, 0, - output->transform_matrix); - - /* This takes our matrix, the texture, and an alpha, and performs the actual - * rendering on the GPU. */ - wlr_render_texture_with_matrix(rdata->renderer, texture, matrix, 1); - - /* This lets the client know that we've displayed that frame and it can - * prepare another one now if it likes. */ - wlr_surface_send_frame_done(surface, rdata->when); + wlr_surface_send_frame_done(surface, data); } static void output_frame(struct wl_listener *listener, void *data) { @@ -603,56 +530,23 @@ static void output_frame(struct wl_listener *listener, void *data) { * generally at the output's refresh rate (e.g. 60Hz). */ struct tinywl_output *output = wl_container_of(listener, output, frame); - struct wlr_renderer *renderer = output->server->renderer; - - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - /* wlr_output_attach_render makes the OpenGL context current. */ - if (!wlr_output_attach_render(output->wlr_output, NULL)) { - return; - } - /* The "effective" resolution can change if you rotate your outputs. */ - int width, height; - wlr_output_effective_resolution(output->wlr_output, &width, &height); - /* Begin the renderer (calls glViewport and some other GL sanity checks) */ - wlr_renderer_begin(renderer, width, height); - - float color[4] = {0.3, 0.3, 0.3, 1.0}; - wlr_renderer_clear(renderer, color); - - /* Each subsequent window we render is rendered on top of the last. Because - * our view list is ordered front-to-back, we iterate over it backwards. */ - struct tinywl_view *view; - wl_list_for_each_reverse(view, &output->server->views, link) { - if (!view->mapped) { - /* An unmapped view should not be rendered. */ - continue; + bool found = false; + struct wlr_scene_output *scene_output; + wl_list_for_each(scene_output, &output->server->scene->outputs, link) { + if (scene_output->output == output->wlr_output) { + found = true; + break; } - struct render_data rdata = { - .output = output->wlr_output, - .view = view, - .renderer = renderer, - .when = &now, - }; - /* This calls our render_surface function for each surface among the - * xdg_surface's toplevel and popups. */ - wlr_xdg_surface_for_each_surface(view->xdg_surface, - render_surface, &rdata); } + assert(found); - /* Hardware cursors are rendered by the GPU on a separate plane, and can be - * moved around without re-rendering what's beneath them - which is more - * efficient. However, not all hardware supports hardware cursors. For this - * reason, wlroots provides a software fallback, which we ask it to render - * here. wlr_cursor handles configuring hardware vs software cursors for you, - * and this function is a no-op when hardware cursors are in use. */ - wlr_output_render_software_cursors(output->wlr_output, NULL); + /* Render the scene if needed and commit the output */ + wlr_scene_output_commit(scene_output); - /* Conclude rendering and swap the buffers, showing the final frame - * on-screen. */ - wlr_renderer_end(renderer); - wlr_output_commit(output->wlr_output); + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + wlr_scene_output_for_each_surface(scene_output, send_frame_done, &now); } static void server_new_output(struct wl_listener *listener, void *data) { @@ -698,26 +592,98 @@ static void server_new_output(struct wl_listener *listener, void *data) { wlr_output_layout_add_auto(server->output_layout, wlr_output); } -static void xdg_surface_map(struct wl_listener *listener, void *data) { +static void xdg_popup_map(struct wl_listener *listener, void *data) { + struct tinywl_popup *popup = wl_container_of(listener, popup, map); + + popup->scene_node = wlr_scene_subsurface_tree_create( + popup->view->scene_node, popup->xdg_popup->base->surface); + double sx, sy; + wlr_xdg_popup_get_position(popup->xdg_popup, &sx, &sy); + wlr_scene_node_set_position(popup->scene_node, sx, sy); +} + +static void xdg_popup_unmap(struct wl_listener *listener, void *data) { + struct tinywl_popup *popup = wl_container_of(listener, popup, unmap); + wlr_scene_node_destroy(popup->scene_node); +} + +static void xdg_popup_destroy(struct wl_listener *listener, void *data) { + struct tinywl_popup *popup = wl_container_of(listener, popup, destroy); + + wl_list_remove(&popup->map.link); + wl_list_remove(&popup->unmap.link); + wl_list_remove(&popup->destroy.link); + wl_list_remove(&popup->new_popup.link); + + free(popup); +} + +static void tinywl_popup_create(struct tinywl_view *view, + struct wlr_xdg_popup *xdg_popup); + +static void xdg_popup_new_popup(struct wl_listener *listener, void *data) { + struct tinywl_view *view = wl_container_of(listener, view, new_popup); + struct wlr_xdg_popup *xdg_popup = data; + tinywl_popup_create(view, xdg_popup); +} + +static void tinywl_popup_create(struct tinywl_view *view, + struct wlr_xdg_popup *xdg_popup) { + struct tinywl_popup *popup = calloc(1, sizeof(struct tinywl_popup)); + popup->view = view; + popup->xdg_popup = xdg_popup; + + popup->map.notify = xdg_popup_map; + wl_signal_add(&xdg_popup->base->events.map, &popup->map); + popup->unmap.notify = xdg_popup_unmap; + wl_signal_add(&xdg_popup->base->events.unmap, &popup->unmap); + popup->destroy.notify = xdg_popup_destroy; + wl_signal_add(&xdg_popup->base->events.destroy, &popup->destroy); + popup->new_popup.notify = xdg_popup_new_popup; + wl_signal_add(&xdg_popup->base->events.new_popup, &popup->new_popup); +} + +static void xdg_toplevel_map(struct wl_listener *listener, void *data) { /* Called when the surface is mapped, or ready to display on-screen. */ struct tinywl_view *view = wl_container_of(listener, view, map); - view->mapped = true; + + view->scene_node = wlr_scene_subsurface_tree_create( + &view->server->scene->node, view->xdg_surface->surface); + view->scene_node->data = view; + + wl_list_insert(&view->server->views, &view->link); + focus_view(view, view->xdg_surface->surface); } -static void xdg_surface_unmap(struct wl_listener *listener, void *data) { +static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) { /* Called when the surface is unmapped, and should no longer be shown. */ struct tinywl_view *view = wl_container_of(listener, view, unmap); - view->mapped = false; + + wlr_scene_node_destroy(view->scene_node); } -static void xdg_surface_destroy(struct wl_listener *listener, void *data) { +static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) { /* Called when the surface is destroyed and should never be shown again. */ struct tinywl_view *view = wl_container_of(listener, view, destroy); wl_list_remove(&view->link); + + wl_list_remove(&view->map.link); + wl_list_remove(&view->unmap.link); + wl_list_remove(&view->destroy.link); + wl_list_remove(&view->new_popup.link); + wl_list_remove(&view->request_move.link); + wl_list_remove(&view->request_resize.link); + free(view); } +static void xdg_toplevel_new_popup(struct wl_listener *listener, void *data) { + struct tinywl_view *view = wl_container_of(listener, view, new_popup); + struct wlr_xdg_popup *xdg_popup = data; + tinywl_popup_create(view, xdg_popup); +} + static void begin_interactive(struct tinywl_view *view, enum tinywl_cursor_mode mode, uint32_t edges) { /* This function sets up an interactive move or resize operation, where the @@ -740,8 +706,10 @@ static void begin_interactive(struct tinywl_view *view, struct wlr_box geo_box; wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box); - double border_x = (view->x + geo_box.x) + ((edges & WLR_EDGE_RIGHT) ? geo_box.width : 0); - double border_y = (view->y + geo_box.y) + ((edges & WLR_EDGE_BOTTOM) ? geo_box.height : 0); + double border_x = (view->x + geo_box.x) + + ((edges & WLR_EDGE_RIGHT) ? geo_box.width : 0); + double border_y = (view->y + geo_box.y) + + ((edges & WLR_EDGE_BOTTOM) ? geo_box.height : 0); server->grab_x = server->cursor->x - border_x; server->grab_y = server->cursor->y - border_y; @@ -783,6 +751,8 @@ static void server_new_xdg_surface(struct wl_listener *listener, void *data) { wl_container_of(listener, server, new_xdg_surface); struct wlr_xdg_surface *xdg_surface = data; if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) { + /* New popups are handled by listening to the new_popup + * event on the parent toplevel/popup */ return; } @@ -793,12 +763,14 @@ static void server_new_xdg_surface(struct wl_listener *listener, void *data) { view->xdg_surface = xdg_surface; /* Listen to the various events it can emit */ - view->map.notify = xdg_surface_map; + view->map.notify = xdg_toplevel_map; wl_signal_add(&xdg_surface->events.map, &view->map); - view->unmap.notify = xdg_surface_unmap; + view->unmap.notify = xdg_toplevel_unmap; wl_signal_add(&xdg_surface->events.unmap, &view->unmap); - view->destroy.notify = xdg_surface_destroy; + view->destroy.notify = xdg_toplevel_destroy; wl_signal_add(&xdg_surface->events.destroy, &view->destroy); + view->new_popup.notify = xdg_toplevel_new_popup; + wl_signal_add(&xdg_surface->events.new_popup, &view->new_popup); /* cotd */ struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel; @@ -807,8 +779,6 @@ static void server_new_xdg_surface(struct wl_listener *listener, void *data) { view->request_resize.notify = xdg_toplevel_request_resize; wl_signal_add(&toplevel->events.request_resize, &view->request_resize); - /* Add it to the list of views. */ - wl_list_insert(&server->views, &view->link); } int main(int argc, char *argv[]) { @@ -844,8 +814,8 @@ int main(int argc, char *argv[]) { /* If we don't provide a renderer, autocreate makes a GLES2 renderer for us. * The renderer is responsible for defining the various pixel formats it * supports for shared memory, this configures that for clients. */ - server.renderer = wlr_backend_get_renderer(server.backend); - wlr_renderer_init_wl_display(server.renderer, server.wl_display); + struct wlr_renderer *renderer = wlr_backend_get_renderer(server.backend); + wlr_renderer_init_wl_display(renderer, server.wl_display); /* This creates some hands-off wlroots interfaces. The compositor is * necessary for clients to allocate surfaces and the data device manager @@ -853,7 +823,7 @@ int main(int argc, char *argv[]) { * to dig your fingers in and play with their behavior if you want. Note that * the clients cannot set the selection directly without compositor approval, * see the handling of the request_set_selection event below.*/ - wlr_compositor_create(server.wl_display, server.renderer); + wlr_compositor_create(server.wl_display, renderer); wlr_data_device_manager_create(server.wl_display); /* Creates an output layout, which a wlroots utility for working with an @@ -866,9 +836,17 @@ int main(int argc, char *argv[]) { server.new_output.notify = server_new_output; wl_signal_add(&server.backend->events.new_output, &server.new_output); - /* Set up our list of views and the xdg-shell. The xdg-shell is a Wayland - * protocol which is used for application windows. For more detail on - * shells, refer to my article: + /* Create a scene graph. This is a wlroots abstraction that handles all + * rendering and damage tracking. All the compositor author needs to do + * is add things that should be rendered to the scene graph at the proper + * positions and then call wlr_scene_output_commit() to render a frame if + * necessary. + */ + server.scene = wlr_scene_create(); + wlr_scene_attach_output_layout(server.scene, server.output_layout); + + /* Set up the xdg-shell. The xdg-shell is a Wayland protocol which is used + * for application windows. For more detail on shells, refer to my article: * * https://drewdevault.com/2018/07/29/Wayland-shells.html */ From 346e648c52d7e7fa358cf50e5340a299bf9ed26b Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Wed, 27 Oct 2021 17:14:26 +0200 Subject: [PATCH 3/3] tinywl: use wlr_scene_xdg_popup_create() --- tinywl/tinywl.c | 99 +++++++++++++------------------------------------ 1 file changed, 25 insertions(+), 74 deletions(-) diff --git a/tinywl/tinywl.c b/tinywl/tinywl.c index 4bf1374d1c..13272d34da 100644 --- a/tinywl/tinywl.c +++ b/tinywl/tinywl.c @@ -78,22 +78,11 @@ struct tinywl_view { struct wl_listener map; struct wl_listener unmap; struct wl_listener destroy; - struct wl_listener new_popup; struct wl_listener request_move; struct wl_listener request_resize; int x, y; }; -struct tinywl_popup { - struct tinywl_view *view; - struct wlr_xdg_popup *xdg_popup; - struct wlr_scene_node *scene_node; - struct wl_listener map; - struct wl_listener unmap; - struct wl_listener destroy; - struct wl_listener new_popup; -}; - struct tinywl_keyboard { struct wl_list link; struct tinywl_server *server; @@ -592,57 +581,6 @@ static void server_new_output(struct wl_listener *listener, void *data) { wlr_output_layout_add_auto(server->output_layout, wlr_output); } -static void xdg_popup_map(struct wl_listener *listener, void *data) { - struct tinywl_popup *popup = wl_container_of(listener, popup, map); - - popup->scene_node = wlr_scene_subsurface_tree_create( - popup->view->scene_node, popup->xdg_popup->base->surface); - double sx, sy; - wlr_xdg_popup_get_position(popup->xdg_popup, &sx, &sy); - wlr_scene_node_set_position(popup->scene_node, sx, sy); -} - -static void xdg_popup_unmap(struct wl_listener *listener, void *data) { - struct tinywl_popup *popup = wl_container_of(listener, popup, unmap); - wlr_scene_node_destroy(popup->scene_node); -} - -static void xdg_popup_destroy(struct wl_listener *listener, void *data) { - struct tinywl_popup *popup = wl_container_of(listener, popup, destroy); - - wl_list_remove(&popup->map.link); - wl_list_remove(&popup->unmap.link); - wl_list_remove(&popup->destroy.link); - wl_list_remove(&popup->new_popup.link); - - free(popup); -} - -static void tinywl_popup_create(struct tinywl_view *view, - struct wlr_xdg_popup *xdg_popup); - -static void xdg_popup_new_popup(struct wl_listener *listener, void *data) { - struct tinywl_view *view = wl_container_of(listener, view, new_popup); - struct wlr_xdg_popup *xdg_popup = data; - tinywl_popup_create(view, xdg_popup); -} - -static void tinywl_popup_create(struct tinywl_view *view, - struct wlr_xdg_popup *xdg_popup) { - struct tinywl_popup *popup = calloc(1, sizeof(struct tinywl_popup)); - popup->view = view; - popup->xdg_popup = xdg_popup; - - popup->map.notify = xdg_popup_map; - wl_signal_add(&xdg_popup->base->events.map, &popup->map); - popup->unmap.notify = xdg_popup_unmap; - wl_signal_add(&xdg_popup->base->events.unmap, &popup->unmap); - popup->destroy.notify = xdg_popup_destroy; - wl_signal_add(&xdg_popup->base->events.destroy, &popup->destroy); - popup->new_popup.notify = xdg_popup_new_popup; - wl_signal_add(&xdg_popup->base->events.new_popup, &popup->new_popup); -} - static void xdg_toplevel_map(struct wl_listener *listener, void *data) { /* Called when the surface is mapped, or ready to display on-screen. */ struct tinywl_view *view = wl_container_of(listener, view, map); @@ -671,19 +609,12 @@ static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&view->map.link); wl_list_remove(&view->unmap.link); wl_list_remove(&view->destroy.link); - wl_list_remove(&view->new_popup.link); wl_list_remove(&view->request_move.link); wl_list_remove(&view->request_resize.link); free(view); } -static void xdg_toplevel_new_popup(struct wl_listener *listener, void *data) { - struct tinywl_view *view = wl_container_of(listener, view, new_popup); - struct wlr_xdg_popup *xdg_popup = data; - tinywl_popup_create(view, xdg_popup); -} - static void begin_interactive(struct tinywl_view *view, enum tinywl_cursor_mode mode, uint32_t edges) { /* This function sets up an interactive move or resize operation, where the @@ -744,23 +675,45 @@ static void xdg_toplevel_request_resize( begin_interactive(view, TINYWL_CURSOR_RESIZE, event->edges); } +static struct wlr_xdg_surface *xdg_popup_get_toplevel( + struct wlr_xdg_popup *popup) { + struct wlr_xdg_surface *surface = popup->base; + while (true) { + switch (surface->role) { + case WLR_XDG_SURFACE_ROLE_POPUP: + if (!wlr_surface_is_xdg_surface(surface->popup->parent)) { + return NULL; + } + surface = wlr_xdg_surface_from_wlr_surface(surface->popup->parent); + break; + case WLR_XDG_SURFACE_ROLE_TOPLEVEL: + return surface; + case WLR_XDG_SURFACE_ROLE_NONE: + return NULL; + } + } +} + static void server_new_xdg_surface(struct wl_listener *listener, void *data) { /* This event is raised when wlr_xdg_shell receives a new xdg surface from a * client, either a toplevel (application window) or popup. */ struct tinywl_server *server = wl_container_of(listener, server, new_xdg_surface); struct wlr_xdg_surface *xdg_surface = data; - if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) { - /* New popups are handled by listening to the new_popup - * event on the parent toplevel/popup */ + if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { + struct wlr_xdg_surface *toplevel = xdg_popup_get_toplevel(xdg_surface->popup); + struct tinywl_view *view = toplevel->data; + wlr_scene_xdg_popup_create(view->scene_node, xdg_surface->popup); return; } + assert(xdg_surface-> role == WLR_XDG_SURFACE_ROLE_TOPLEVEL); /* Allocate a tinywl_view for this surface */ struct tinywl_view *view = calloc(1, sizeof(struct tinywl_view)); view->server = server; view->xdg_surface = xdg_surface; + xdg_surface->data = view; /* Listen to the various events it can emit */ view->map.notify = xdg_toplevel_map; @@ -769,8 +722,6 @@ static void server_new_xdg_surface(struct wl_listener *listener, void *data) { wl_signal_add(&xdg_surface->events.unmap, &view->unmap); view->destroy.notify = xdg_toplevel_destroy; wl_signal_add(&xdg_surface->events.destroy, &view->destroy); - view->new_popup.notify = xdg_toplevel_new_popup; - wl_signal_add(&xdg_surface->events.new_popup, &view->new_popup); /* cotd */ struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel;