neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

commit f35e69e3b98ca6eed40d8632b2b0c509882dc2bc
parent fe85c08ec5bd659e02f35561e383520fb24cd682
Author: cowmonk <cowmonk@based.pt>
Date:   Tue, 24 Nov 2026 14:16:05 -0700

refactored MICE neuswc patches

Diffstat:
Mlibswc/compositor.c | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mlibswc/compositor.h | 25++++++++++++++++++-------
Mlibswc/data_device.c | 2+-
Mlibswc/decor.c | 9++++++++-
Mlibswc/layer_shell.c | 12++++++------
Mlibswc/swc.h | 44++++++++++++++++++++++++++++++++++++++++----
Dlibswc/swc.h.orig | 773-------------------------------------------------------------------------------
Mlibswc/window.c | 20+++++++++++++++-----
Dlibswc/window.c.orig | 772-------------------------------------------------------------------------------
Mlibswc/window.h | 1-
Mlibswc/xdg_shell.c | 18+++++++++---------
Dlibswc/xdg_shell.c.orig | 1075-------------------------------------------------------------------------------
Dpatches/borderAbleToGoRoundDecor.patch | 159-------------------------------------------------------------------------------
Dpatches/borderSharedRoutine.patch | 240-------------------------------------------------------------------------------
Apatches/compositor.patch | 3448+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dpatches/cursorShapes.patch | 477-------------------------------------------------------------------------------
Dpatches/dragAndDrop.patch | 515-------------------------------------------------------------------------------
Dpatches/dragModifiersChooseAction.patch | 221-------------------------------------------------------------------------------
Dpatches/extentsFromAttachedBuffer.patch | 78------------------------------------------------------------------------------
Dpatches/focusFromDecoration.patch | 211-------------------------------------------------------------------------------
Apatches/input.patch | 1755+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dpatches/keyboardResetTellsModifiers.patch | 43-------------------------------------------
Dpatches/keymapAtRuntime.patch | 129-------------------------------------------------------------------------------
Apatches/launch.patch | 425+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dpatches/launchEscapeKeys.patch | 238-------------------------------------------------------------------------------
Dpatches/launchRestoresConsole.patch | 232-------------------------------------------------------------------------------
Dpatches/layerPopupsOnScreen.patch | 241-------------------------------------------------------------------------------
Dpatches/layerZoneZeroUsableArea.patch | 127-------------------------------------------------------------------------------
Dpatches/managerTransients.patch | 208-------------------------------------------------------------------------------
Dpatches/nestedBackend.patch | 2052-------------------------------------------------------------------------------
Dpatches/netlinkKernelEvents.patch | 18------------------
Dpatches/neuswc-buffer-release.patch | 33---------------------------------
Dpatches/neuswc-configure.patch | 86-------------------------------------------------------------------------------
Dpatches/neuswc-cursor-frame.patch | 15---------------
Dpatches/neuswc-fullscreen.patch | 81-------------------------------------------------------------------------------
Dpatches/panelRedockOnResize.patch | 83-------------------------------------------------------------------------------
Dpatches/popupGrabDismiss.patch | 115-------------------------------------------------------------------------------
Dpatches/popupGrabKeyboardFocus.patch | 64----------------------------------------------------------------
Dpatches/popupsFitScreenKeysOnDemand.patch | 127-------------------------------------------------------------------------------
Dpatches/popupsOverParent.patch | 45---------------------------------------------
Dpatches/seatFeedInput.patch | 113-------------------------------------------------------------------------------
Dpatches/selectionHandler.patch | 557-------------------------------------------------------------------------------
Apatches/shell.patch | 1099+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dpatches/tiledRaise.patch | 14--------------
Dpatches/videoBackendsAtRuntime.patch | 665-------------------------------------------------------------------------------
Dpatches/xdgDialog.patch | 322-------------------------------------------------------------------------------
Dpatches/xdgMinMaxSize.patch | 40----------------------------------------
47 files changed, 6929 insertions(+), 10214 deletions(-)

diff --git a/libswc/compositor.c b/libswc/compositor.c @@ -107,6 +107,8 @@ handle_button(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state); static void perform_update(void *data); +static void +schedule_updates(uint32_t screens); static struct pointer_handler pointer_handler = { .motion = handle_motion, @@ -373,6 +375,93 @@ draw_border(struct wld_renderer *renderer, const struct compositor_view *view, pixman_region32_fini(&outer); } +/* a backdrop's area: the view's extents and the margin around them */ +static pixman_box32_t +backdrop_box(const struct compositor_view *view) +{ + int32_t m = (int32_t)view->backdrop.margin; + + return (pixman_box32_t){ view->extents.x1 - m, view->extents.y1 - m, view->extents.x2 + m, + view->extents.y2 + m }; +} + +/* what lies behind a view with a backdrop is read through its whole area, + * so a change anywhere in it repaints all of it, and that may reach another + * backdrop's area in turn */ +static void +backdrop_damage(void) +{ + struct compositor_view *view; + bool grew = true; + + while (grew) { + grew = false; + wl_list_for_each (view, &compositor.views, link) { + pixman_box32_t box = backdrop_box(view); + + if (!view->visible || !view->backdrop.hook) + continue; + if (pixman_region32_contains_rectangle(&compositor.damage, &box) == PIXMAN_REGION_PART) { + pixman_region32_union_rect(&compositor.damage, &compositor.damage, box.x1, box.y1, + (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1)); + grew = true; + } + } + } +} + +static void +backdrop_call(struct target *target, struct compositor_view *view, pixman_region32_t *damage) +{ + const struct swc_rectangle *tg = &target->view->geometry; + pixman_box32_t box = backdrop_box(view), *boxes; + pixman_region32_t area; + struct swc_rectangle rects[64]; + struct swc_backdrop b = { 0 }; + int n; + + pixman_region32_init_rect(&area, box.x1, box.y1, (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1)); + pixman_region32_intersect_rect(&area, &area, tg->x, tg->y, tg->width, tg->height); + box = *pixman_region32_extents(&area); + pixman_region32_intersect(&area, &area, damage); + pixman_region32_subtract(&area, &area, &view->clip); + boxes = pixman_region32_rectangles(&area, &n); + if (n && (b.pixels = wld_target_map(swc.backend->renderer, &b.pitch, &b.format))) { + b.area = (struct swc_rectangle){ box.x1 - tg->x, box.y1 - tg->y, (uint32_t)(box.x2 - box.x1), + (uint32_t)(box.y2 - box.y1) }; + /* more pieces than fit are given as the one box around them */ + if (n > 64) { + box = *pixman_region32_extents(&area); + boxes = &box; + n = 1; + } + for (int i = 0; i < n; i++) + rects[i] = (struct swc_rectangle){ boxes[i].x1 - tg->x, boxes[i].y1 - tg->y, + (uint32_t)(boxes[i].x2 - boxes[i].x1), + (uint32_t)(boxes[i].y2 - boxes[i].y1) }; + b.rects = rects; + b.nrects = n; + view->backdrop.hook(view->backdrop.data, &b); + wld_target_unmap(swc.backend->renderer); + } + pixman_region32_fini(&area); +} + +void +compositor_view_set_backdrop(struct compositor_view *view, uint32_t margin, + void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data) +{ + pixman_box32_t box; + + view->backdrop.margin = margin; + view->backdrop.hook = hook; + view->backdrop.data = data; + box = backdrop_box(view); + pixman_region32_union_rect(&compositor.damage, &compositor.damage, box.x1, box.y1, + (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1)); + schedule_updates(view->base.screens); +} + static void repaint_view(struct target *target, struct compositor_view *view, pixman_region32_t *damage) @@ -386,6 +475,8 @@ repaint_view(struct target *target, struct compositor_view *view, if (!view->base.buffer) { return; } + if (view->backdrop.hook) + backdrop_call(target, view, damage); buf_w = view->base.buffer->width; buf_h = view->base.buffer->height; @@ -1165,12 +1256,12 @@ restack_view_for_layer(struct compositor_view *view, bool raise) continue; } - if (other->stack_layer > view->stack_layer) { + if (other->z > view->z) { insert_after = &other->link; continue; } - if (other->stack_layer == view->stack_layer) { + if (other->z == view->z) { found_same = true; if (raise) { insert_after = other->link.prev; @@ -1311,6 +1402,9 @@ raise_transients(struct compositor_view *view) } } for (unsigned i = 0; i < n; i++) { + /* a transient stays in its parent's layer, or it would be found + * among views of another */ + children[i]->z = view->z; wl_list_remove(&children[i]->link); wl_list_insert(view->link.prev, &children[i]->link); damage_view(children[i]); @@ -1342,18 +1436,17 @@ raise_window(struct compositor_view *view) continue; } - if (other->stack_layer > STACK_LAYER_NORMAL || + if (other->z > view->z || other->always_top) { insert_after = &other->link; continue; } - if (other->stack_layer < STACK_LAYER_NORMAL) { + if (other->z < view->z) { break; } - if (other->window && (view->window->mode != WINDOW_MODE_TILED || - other->window->mode == WINDOW_MODE_TILED)) { + if (other->window) { top_window = other; break; } @@ -1382,17 +1475,17 @@ raise_window(struct compositor_view *view) void raise_window_top(struct compositor_view *view) { - view->stack_layer = STACK_LAYER_OVERLAY; + view->z = STACK_LAYER_OVERLAY; restack_view_for_layer(view, true); damage_view(view); schedule_updates(view->base.screens); } void -compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, +compositor_view_set_z(struct compositor_view *view, int32_t layer, bool raise) { - if (view->stack_layer == layer) { + if (view->z == layer) { if (raise) { restack_view_for_layer(view, true); damage_view(view); @@ -1402,7 +1495,7 @@ compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, } damage_view(view); - view->stack_layer = layer; + view->z = layer; restack_view_for_layer(view, raise); damage_view(view); schedule_updates(view->base.screens); @@ -1539,7 +1632,7 @@ compositor_create_view(struct surface *surface) view->visible = false; view->always_top = false; view->no_input = false; - view->stack_layer = STACK_LAYER_NORMAL; + view->z = STACK_LAYER_NORMAL; view->extents.x1 = 0; view->extents.y1 = 0; view->extents.x2 = 0; @@ -1900,6 +1993,7 @@ update_screen(struct screen *screen) return; } + backdrop_damage(); pixman_region32_init(&damage); pixman_region32_intersect_rect(&damage, &compositor.damage, geom->x, geom->y, geom->width, geom->height); diff --git a/libswc/compositor.h b/libswc/compositor.h @@ -24,6 +24,7 @@ #ifndef SWC_COMPOSITOR_H #define SWC_COMPOSITOR_H +#include "swc.h" #include "view.h" #include <pixman.h> @@ -86,10 +87,17 @@ struct compositor_view { bool no_input; /* Global stacking layer for this view */ - uint32_t stack_layer; + /* depth: a higher one is drawn over a lower, see swc_window_set_z */ + int32_t z; /* The box that the surface covers (including it's border). */ pixman_box32_t extents; + /* see swc_window_set_backdrop */ + struct { + uint32_t margin; + void (*hook)(void *data, const struct swc_backdrop *backdrop); + void *data; + } backdrop; /* The region that is covered by opaque regions of surfaces above this * surface. */ @@ -190,18 +198,21 @@ compositor_render_to_shm(struct screen *screen); void raise_window(struct compositor_view *view); void +compositor_view_set_backdrop(struct compositor_view *view, uint32_t margin, + void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data); +void raise_window_top(struct compositor_view *view); enum compositor_stack_layer { - STACK_LAYER_BACKGROUND = 0, - STACK_LAYER_BOTTOM = 1, - STACK_LAYER_NORMAL = 2, - STACK_LAYER_TOP = 3, - STACK_LAYER_OVERLAY = 4, + STACK_LAYER_BACKGROUND = SWC_Z_BACKGROUND, + STACK_LAYER_BOTTOM = SWC_Z_BOTTOM, + STACK_LAYER_NORMAL = SWC_Z_WINDOW, + STACK_LAYER_TOP = SWC_Z_TOP, + STACK_LAYER_OVERLAY = SWC_Z_OVERLAY, }; void -compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, +compositor_view_set_z(struct compositor_view *view, int32_t layer, bool raise); #endif diff --git a/libswc/data_device.c b/libswc/data_device.c @@ -304,7 +304,7 @@ start_drag(struct wl_client *client, struct wl_resource *resource, if (icon_resource && (drag.icon = compositor_create_view(wl_resource_get_user_data(icon_resource)))) { drag.icon->no_input = true; drag.icon->always_top = true; - compositor_view_set_stack_layer(drag.icon, STACK_LAYER_OVERLAY, true); + compositor_view_set_z(drag.icon, STACK_LAYER_OVERLAY, true); view_move(&drag.icon->base, x, y); compositor_view_show(drag.icon); drag.icon_destroy.notify = handle_drag_icon_destroy; diff --git a/libswc/decor.c b/libswc/decor.c @@ -731,7 +731,14 @@ decor_view_set(struct compositor_view *view, const struct swc_decor *decor) owned_string = strdup(text.string); } - if (font_name) { + /* a font is opened once per name: a decor that changes only its + * colours or parts keeps the face it has */ + if (font_name && view->decor.font && streq(view->decor.font_name, font_name)) { + font = view->decor.font; + owned_font_name = view->decor.font_name; + view->decor.font = NULL; + view->decor.font_name = NULL; + } else if (font_name) { owned_font_name = strdup(font_name); if (owned_font_name && font_context) { font = wld_font_open_name(font_context, font_name); diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c @@ -156,22 +156,22 @@ update_usable_geometry(struct layer_surface *surface) static void restack_layer(struct layer_surface *surface) { - uint32_t stack_layer = STACK_LAYER_NORMAL; + int32_t z = STACK_LAYER_NORMAL; bool always_top = false; switch (surface->current.layer) { case ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND: - stack_layer = STACK_LAYER_BACKGROUND; + z = STACK_LAYER_BACKGROUND; break; case ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM: - stack_layer = STACK_LAYER_BOTTOM; + z = STACK_LAYER_BOTTOM; break; case ZWLR_LAYER_SHELL_V1_LAYER_TOP: - stack_layer = STACK_LAYER_TOP; + z = STACK_LAYER_TOP; always_top = true; break; case ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY: - stack_layer = STACK_LAYER_OVERLAY; + z = STACK_LAYER_OVERLAY; always_top = true; break; default: @@ -179,7 +179,7 @@ restack_layer(struct layer_surface *surface) } surface->view->always_top = always_top; - compositor_view_set_stack_layer(surface->view, stack_layer, true); + compositor_view_set_z(surface->view, z, true); } /* zone 0 surfaces keep clear of reserved zones, others span the screen */ diff --git a/libswc/swc.h b/libswc/swc.h @@ -364,6 +364,13 @@ struct swc_window_handler { * Called when the window size changes. */ void (*size_changed)(void *data); + + /** + * Called when the window asks to be shown fullscreen, or to stop being. + * + * The window manager decides, see swc_window_set_fullscreen. + */ + void (*fullscreen)(void *data, bool fullscreen); }; struct swc_window { @@ -387,11 +394,40 @@ void swc_window_set_handler(struct swc_window *window, const struct swc_window_handler *handler, void *data); -/* Deliver xdg_toplevel fullscreen requests using the window handler's data. */ -#define SWC_WINDOW_FULLSCREEN_REQUEST 1 +/* what lies behind a window while the screen is composited: the target's + * pixels as drawn so far, pitch bytes a row in a WLD format, the window's + * area with the margin asked for (in those pixels, clipped to them), and the + * parts of it being repainted now, which every caller may read and write */ +struct swc_backdrop { + void *pixels; + uint32_t pitch, format; + struct swc_rectangle area; + const struct swc_rectangle *rects; + int nrects; +}; + +/* calls hook right before the window is drawn with what lies behind it, so + * a window manager can read it or change it: glass, a shade, a lens. the + * whole area and margin is drawn again whenever any of it changes, so the + * pixels are always complete there. a NULL hook takes it away */ +void +swc_window_set_backdrop(struct swc_window *window, uint32_t margin, + void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data); + +/* depths: a view at a higher one is drawn over one at a lower. the + * shells' layers stand at these, a window at SWC_Z_WINDOW until its manager + * says otherwise, and every depth between them is the manager's: floating + * windows over tiled ones, a fullscreen window over the panels. a window is + * raised to the front of the views at its depth, and its transients share + * it */ +#define SWC_Z_BACKGROUND (-3000) +#define SWC_Z_BOTTOM (-2000) +#define SWC_Z_WINDOW 0 +#define SWC_Z_TOP 2000 +#define SWC_Z_OVERLAY 3000 + void -swc_window_set_fullscreen_handler(struct swc_window *window, - void (*handler)(void *data, bool fullscreen)); +swc_window_set_z(struct swc_window *window, int32_t z); /** * Request that the specified window close. diff --git a/libswc/swc.h.orig b/libswc/swc.h.orig @@ -1,773 +0,0 @@ -/* swc: libswc/swc.h - * - * Copyright (c) 2013 Michael Forney - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#ifndef SWC_H -#define SWC_H - -#include <stdbool.h> -#include <stdint.h> -#include <sys/types.h> - -#ifdef __cplusplus -extern "C" { -#endif - -struct libinput_device; -struct wl_display; -struct wl_event_loop; -struct wld_buffer; - -/** - * Get the current cursor position. - * - * The returned coordinates are in compositor-global space, in wl_fixed_t - * (24.8) fixed-point units, but exposed as raw int32_t to avoid needing - * wayland headers. - * - */ -bool -swc_cursor_position(int32_t *x, int32_t *y); - -/** - * Set the cursor position. - * - * The coordinates are in compositor-global space, exposed as raw int32_t - * to avoid needing wayland headers. This has the same effect as the - * cursor being moved there by an input device. - */ -bool -swc_cursor_set_position(int32_t x, int32_t y); - -/** - * Feed a button event to the seat, through the bindings. - * button is a linux input code. - */ -bool -swc_button(uint32_t button, uint32_t state); - -/** - * Feed a key event to the seat. key is an evdev code. - */ -bool -swc_key(uint32_t key, uint32_t state); - -/** - * Find the key and level (0 bare, 1 shift) that produce keysym. - */ -bool -swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level); - -/** - * Send a pointer button event to the currently focused client. - * - * This is intended for window managers which intercept button events (for - * example for mouse chords) but want normal clicks to still reach clients. - */ -void -swc_pointer_send_button(uint32_t time, uint32_t button, uint32_t state); - -/** - * Send a pointer axis event to the currently focused client. - * - * This is intended for window managers which intercept axis events (for - * example for mouse chords) but want normal scrolling to still reach clients. - * - * value120 uses the wl_pointer "120 units" convention. - */ -void -swc_pointer_send_axis(uint32_t time, uint32_t axis, int32_t value120); - -/* Cursor control (compositor-internal cursor) */ -enum swc_cursor_kind { - SWC_CURSOR_DEFAULT = 0, - SWC_CURSOR_BOX = 1, - SWC_CURSOR_CROSS = 2, - SWC_CURSOR_SIGHT = 3, - SWC_CURSOR_UP = 4, - SWC_CURSOR_DOWN = 5, -}; - -enum swc_cursor_mode { - /* Allow clients to set their own cursors (I-beam, resize, etc). */ - SWC_CURSOR_MODE_CLIENT = 0, - /* Force compositor cursor; ignore client wl_pointer.set_cursor. */ - SWC_CURSOR_MODE_COMPOSITOR = 1, -}; - -/** - * Override the compositor's internal cursor. - * - * this is intended for window managers to show mode cursors - * (move/resize/select) like the ones in hevel If a client has set its own - * cursor surface, swc may ignore the override. - */ -void -swc_set_cursor(enum swc_cursor_kind kind); - -/** - * Control whether client cursor surfaces are honored. - */ -void -swc_set_cursor_mode(enum swc_cursor_mode mode); - -/** - * set a custom argb8888 cursor image for a given kind - * - * `argb8888` is a pointer to `width*height` pixels in ARGB8888 order. - * the caller has to keep the pixel memory alive for as long as it may be used - */ -void -swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888, - uint32_t width, uint32_t height, int32_t hotspot_x, - int32_t hotspot_y); - -void -swc_clear_cursor_image(enum swc_cursor_kind kind); - -/** - * draw [or update] a simple box overlay - * - * box is defined by two diagonally opposite corners in compositor-global - * coordinates. this draws only the border. Call swc_overlay_clear() to remove - * it - */ -void -swc_overlay_set_box(int32_t x1, int32_t y1, int32_t x2, int32_t y2, - uint32_t color, uint32_t border_width); - -/** - * Clear the current overlay, if any. - */ -void -swc_overlay_clear(void); - -/** - * Set the compositor zoom level. - * - * 1.0 = normal, >1.0 = zoomed in, <1.0 = zoomed out - * Uses software (pixman) scaling. - */ -void -swc_set_zoom(float level); - -/** - * Get the current zoom level. - */ -float -swc_get_zoom(void); - -/* Rectangles {{{ */ - -struct swc_rectangle { - int32_t x, y; - uint32_t width, height; -}; - -/* }}} */ - -/* Screens {{{ */ - -struct swc_screen_handler { - /** - * Called when the screen is about to be destroyed. - * - * After this is called, the screen is no longer valid. - */ - void (*destroy)(void *data); - - /** - * Called when the total area of the screen has changed. - */ - void (*geometry_changed)(void *data); - - /** - * Called when the geometry of the screen available for laying out windows - * has changed. - * - * A window manager should respond by making sure all visible windows are - * within this area. - */ - void (*usable_geometry_changed)(void *data); - - /** - * Called when the pointer enters the screen. - */ - void (*entered)(void *data); -}; - -struct swc_screen { - /** - * The total area of the screen. - */ - struct swc_rectangle geometry; - - /** - * The area of the screen available for placing windows. - */ - struct swc_rectangle usable_geometry; -}; - -/** - * Set the handler associated with this screen. - */ -void -swc_screen_set_handler(struct swc_screen *screen, - const struct swc_screen_handler *handler, void *data); - -/* }}} */ - -/* Windows {{{ */ - -struct swc_window_handler { - /** - * Called when the window is about to be destroyed. - * - * After this is called, the window is no longer valid. - */ - void (*destroy)(void *data); - - /** - * Called when the window's title changes. - */ - void (*title_changed)(void *data); - - /** - * Called when the window's application identifier changes. - */ - void (*app_id_changed)(void *data); - - /** - * Called when the window's parent changes. - * - * This can occur when the window becomes a transient for another window, or - * becomes a toplevel window. - */ - void (*parent_changed)(void *data); - - /** - * Called when the pointer enters the window. - */ - void (*entered)(void *data); - - /** - * Called when the window wants to initiate an interactive move, but the - * window is not in stacked mode. - * - * The window manager may respond by changing the window's mode, after which - * the interactive move will be honored. - */ - void (*move)(void *data); - - /** - * Called when the window wants to initiate an interactive resize, but the - * window is not in stacked mode. - * - * The window manager may respond by changing the window's mode, after which - * the interactive resize will be honored. - */ - void (*resize)(void *data); - - /** - * Called when the window starts or stops blocking its parent. - */ - void (*modal_changed)(void *data); - - /** - * Called when the window size changes. - */ - void (*size_changed)(void *data); -}; - -struct swc_window { - char *title; - char *app_id; - - struct swc_window *parent; - /* the window says it blocks its parent */ - bool modal; - uint32_t motion_throttle_ms; - uint32_t min_width; - uint32_t min_height; - uint32_t max_width; - uint32_t max_height; -}; - -/** - * Set the handler associated with this window. - */ -void -swc_window_set_handler(struct swc_window *window, - const struct swc_window_handler *handler, void *data); - -/** - * Request that the specified window close. - */ -void -swc_window_close(struct swc_window *window); - -/** - * Make the specified window visible. - */ -void -swc_window_show(struct swc_window *window); - -/** - * Make the specified window hidden. - */ -void -swc_window_hide(struct swc_window *window); - -/** - * Set the keyboard focus to the specified window. - * - * If window is NULL, the keyboard will have no focus. - */ -void -swc_window_focus(struct swc_window *window); - -/** - * Sets the window to stacked mode. - * - * A window in this mode has its size specified by the client. The window's - * viewport will be adjusted to the size of the buffer attached by the - * client. - * - * Use of this mode is required to allow interactive moving and resizing. - */ -void -swc_window_set_stacked(struct swc_window *window); - -/** - * Sets the window to tiled mode. - * - * A window in this mode has its size specified by the window manager. - * Additionally, swc will configure the window to operate in a tiled or - * maximized state in order to prevent the window from drawing shadows. - * - * It is invalid to interactively move or resize a window in tiled mode. - */ -void -swc_window_set_tiled(struct swc_window *window); - -/** - * Sets the window to fullscreen mode. - */ -void -swc_window_set_fullscreen(struct swc_window *window, struct swc_screen *screen); - -/** - * Make window a transient of parent, or of nothing when NULL. - */ -void -swc_window_set_parent(struct swc_window *window, struct swc_window *parent); - -/** - * An attached transient moves with its parent. - */ -void -swc_window_set_attached(struct swc_window *window, bool attached); - -/** - * Raise the window with its transients. - */ -void -swc_window_raise(struct swc_window *window); - -/** - * Set the window's position. - * - * The x and y coordinates refer to the top-left corner of the actual contents - * of the window and should be adjusted for the border size. - */ -void -swc_window_set_position(struct swc_window *window, int32_t x, int32_t y); - -/** - * Set the window's size. - * - * The width and height refer to the dimension of the actual contents of the - * window and should be adjusted for the border size. - */ -void -swc_window_set_size(struct swc_window *window, uint32_t width, uint32_t height); - -/** - * Set the window's size and position. - * - * This is a convenience function that is equivalent to calling - * swc_window_set_size and then swc_window_set_position. - */ -void -swc_window_set_geometry(struct swc_window *window, - const struct swc_rectangle *geometry); - -/** - * Get the window's current geometry in compositor-global coordinates. - */ -bool -swc_window_get_geometry(const struct swc_window *window, - struct swc_rectangle *geometry); - -/** - * Get the pid of the client that owns this window - * - * returns pid, or 0 if unavailable - */ -pid_t -swc_window_get_pid(struct swc_window *window); - -/** - * Set the window's border color and width. - * - * NOTE: The window's geometry remains unchanged, and should be updated if a - * fixed top-left corner of the border is desired. - * - * info from dalem: unsure how much double borders break! - */ -void -swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, - uint32_t inner_border_width, uint32_t outer_border_color, - uint32_t outer_border_width); - -/** - * What the border goes around. - */ -enum swc_border_placement { - /** - * The client area only. - */ - SWC_BORDER_CLIENT, - /** - * The client area and its decor. - */ - SWC_BORDER_FRAME, -}; - -/** - * Choose what the border goes around. The default is SWC_BORDER_CLIENT. - */ -void -swc_window_set_border_placement(struct swc_window *window, - enum swc_border_placement placement); - -/*window decor things*/ - -/** - * Select which decor edge a text slot is rendered on. - */ -enum swc_decor_edge { - SWC_DECOR_EDGE_TOP, - SWC_DECOR_EDGE_RIGHT, - SWC_DECOR_EDGE_BOTTOM, - SWC_DECOR_EDGE_LEFT, -}; - -/** - * Choose text alignment within the selected decor edge. - * - * for top and bottom edges, alignment is horizontal - * for left and right edges, alignment is vertical - */ -enum swc_decor_align { - SWC_DECOR_ALIGN_START, - SWC_DECOR_ALIGN_CENTER, - SWC_DECOR_ALIGN_END, -}; - -/** - * Describe a window decor text slot. - * - * if enabled is false, no text is drawn. - * - * the wm supplies some string to be rendered in this decor slot. swc - * copies the string when swc_window_set_decor() is called, so caller doesn't - * need to keep it after the call returns. - * - * for top and bottom edges, text is drawn horizontally - * for left and right edges, text is drawn as stacked glyphs, one glyph - * per row. - * - * the font field accepts some fontconfig pattern such as "sans-serif:size=10". - * if font is NULL, a default font is used. - */ -struct swc_decor_text { - bool enabled; - enum swc_decor_edge edge; - enum swc_decor_align align; - const char *string; - uint32_t color; - uint32_t padding; - int32_t offset_x, offset_y; - const char *font; -}; - -/** - * Describes one wm-supplied decor pixel block. - * - * swc copies the pixel data when swc_window_set_decor() is called, so caller - * doesn't need to keep it after the call returns. - * - * Pixel data is expected to be premultiplied ARGB8888 with the provided - * stride. - */ -struct swc_decor_part { - uint32_t width, height; - uint32_t stride; - const void *data; -}; - -/** - * Describes optional pixel blocks for the outer frame of a decor. - * - * corner parts are drawn once at their matching corner - * edge parts are tiled to fill the remaining edge area between corners. - */ -struct swc_decor_parts { - struct swc_decor_part top_left; - struct swc_decor_part top; - struct swc_decor_part top_right; - struct swc_decor_part left; - struct swc_decor_part right; - struct swc_decor_part bottom_left; - struct swc_decor_part bottom; - struct swc_decor_part bottom_right; -}; - -/** - * Describe decor around a window's edges. - * - * The edge sizes extend outward from the window content geometry. - * if you put 0 it won't be visible. - * - * The title field controls an optional text slot rendered on one edge. - */ -struct swc_decor { - uint32_t color; - uint32_t top, right, bottom, left; - const struct swc_decor_parts *parts; - struct swc_decor_text title; -}; - -/** - * Set window decor around the window. - * - * the dimensions are independent for each edge and extend outward from the - * window content geometry. - * - * swc copies any decor text string needed by the configuration. - * - * passing NULL disables window decor. - */ -void -swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor); - -/** - * Begin an interactive move of the specified window. - */ -void -swc_window_begin_move(struct swc_window *window); - -/** - * End an interactive move of the specified window. - */ -void -swc_window_end_move(struct swc_window *window); - -enum { - SWC_WINDOW_EDGE_AUTO = 0, - SWC_WINDOW_EDGE_TOP = (1 << 0), - SWC_WINDOW_EDGE_BOTTOM = (1 << 1), - SWC_WINDOW_EDGE_LEFT = (1 << 2), - SWC_WINDOW_EDGE_RIGHT = (1 << 3) -}; - -/** - * Begin an interactive resize of the specified window. - */ -void -swc_window_begin_resize(struct swc_window *window, uint32_t edges); - -/** - * End an interactive resize of the specified window. - */ -void -swc_window_end_resize(struct swc_window *window); - -/** - * returns the topmost window at any given compositor global coordinates - * - * returns null if there is no window at that point - */ -struct swc_window * -swc_window_at(int32_t x, int32_t y); - -/** - * move a window in the stacking order by one step - * - * direction < 0 moves the window towards the front (higher) - * direction > 0 moves the window towards the back (lower) - */ -void -swc_window_stack(struct swc_window *window, int32_t direction); - -/* }}} */ - -/* Keyboard repeat rate (characters per second) and delay (ms) definitions. - * Exposed as extern so compositors can set them. - */ -extern int32_t swc_repeat_rate, swc_repeat_delay; - -/** - * Change the keyboard layout while running: names as XKB's, any of them - * NULL for its default. False, the old layout kept, when they make no - * keymap. - */ -bool -swc_set_keyboard_layout(const char *layout, const char *variant, - const char *options); - -/** - * Change the repeat rate (characters per second) and delay (ms), which - * clients are told again. - */ -void -swc_set_keyboard_repeat(int32_t rate, int32_t delay); - -/* Bindings {{{ */ - -enum { - SWC_MOD_CTRL = 1 << 0, - SWC_MOD_ALT = 1 << 1, - SWC_MOD_LOGO = 1 << 2, - SWC_MOD_SHIFT = 1 << 3, - SWC_MOD_ANY = ~0 -}; - -/** - * Drag actions, as in wl_data_device_manager.dnd_action. - */ -enum swc_drag_action { - SWC_DRAG_COPY = 1, - SWC_DRAG_MOVE = 2, - SWC_DRAG_ASK = 4, -}; - -/** - * Set the modifiers that ask for an action during a drag, all held. - * 0 disables it. Defaults: Ctrl copy, Shift move. - */ -void -swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers); - -enum swc_binding_type { - SWC_BINDING_KEY, - SWC_BINDING_BUTTON, -}; - -typedef void (*swc_binding_handler)(void *data, uint32_t time, uint32_t value, - uint32_t state); -typedef void (*swc_axis_binding_handler)(void *data, uint32_t time, - uint32_t axis, int32_t value120); - -/** - * Register a new input binding. - * - * Returns 0 on success, negative error code otherwise. - */ -int -swc_add_binding(enum swc_binding_type type, uint32_t modifiers, uint32_t value, - swc_binding_handler handler, void *data); - -/** - * Unregister a registered input binding. - * - */ -void -swc_remove_binding(enum swc_binding_type type, uint32_t modifiers, - uint32_t value); - -/** - * register a new pointer axis binding - * - * this will intercept axis events from clients; use swc_pointer_send_axis() - * from the handler to forward events when appropriate - */ -int -swc_add_axis_binding(uint32_t modifiers, uint32_t axis, - swc_axis_binding_handler handler, void *data); - -/* }}} */ - -/** - * This is a user-provided structure that swc will use to notify the display - * server of new windows, screens and input devices. - */ -struct swc_manager { - /** - * Called when a new screen is created. - */ - void (*new_screen)(struct swc_screen *screen); - - /** - * Called when a new window is created. - */ - void (*new_window)(struct swc_window *window); - - /** - * Called when a new input device is detected. - */ - void (*new_device)(struct libinput_device *device); - - /** - * Called when the session gets activated (for example, startup or VT - * switch). - */ - void (*activate)(void); - - /** - * Called when the session gets deactivated. - */ - void (*deactivate)(void); -}; - -/** - * Initializes the compositor using the specified display, event_loop, and - * manager. - */ -bool -swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, - const struct swc_manager *manager); - -/** - * Stops the compositor, releasing any used resources. - */ -void -swc_finalize(void); - -#ifdef __cplusplus -} -#endif - -#endif - -/* vim: set fdm=marker : */ diff --git a/libswc/window.c b/libswc/window.c @@ -195,10 +195,22 @@ swc_window_set_handler(struct swc_window *base, } EXPORT void -swc_window_set_fullscreen_handler(struct swc_window *base, - void (*handler)(void *, bool)) +swc_window_set_backdrop(struct swc_window *base, uint32_t margin, + void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data) { - INTERNAL(base)->fullscreen = handler; + struct window *window = INTERNAL(base); + + if (window->view) + compositor_view_set_backdrop(window->view, margin, hook, data); +} + +EXPORT void +swc_window_set_z(struct swc_window *base, int32_t z) +{ + struct window *window = INTERNAL(base); + + if (window->view) + compositor_view_set_z(window->view, z, true); } EXPORT void @@ -585,7 +597,6 @@ window_initialize(struct window *window, const struct window_impl *impl, window->base.min_height = 0; window->base.max_width = 0; window->base.max_height = 0; - window->fullscreen = NULL; window->managed = false; window->mode = WINDOW_MODE_STACKED; window->move.pending = false; @@ -643,7 +654,6 @@ window_unmanage(struct window *window) window->handler->destroy(window->handler_data); } window->handler = &null_handler; - window->fullscreen = NULL; window->managed = false; } diff --git a/libswc/window.c.orig b/libswc/window.c.orig @@ -1,772 +0,0 @@ -/* swc: libswc/window.c - * - * Copyright (c) 2013 Michael Forney - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "window.h" -#include "compositor.h" -#include "event.h" -#include "internal.h" -#include "keyboard.h" -#include "seat.h" -#include "surface.h" -#include "swc.h" -#include "util.h" -#include "view.h" - -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> - -#define INTERNAL(w) ((struct window *)(w)) - -static const uint32_t def_motion_throttle_ms = 16; - -static const struct swc_window_handler null_handler; - -static bool -should_throttle_motion(uint32_t throttle_ms, uint32_t *last_time, uint32_t time) -{ - if (!throttle_ms) { - return false; - } - - if (*last_time && time - *last_time < throttle_ms) { - return true; - } - - *last_time = time; - return false; -} - -static uint32_t -clamp_dimension(int32_t value, uint32_t min, uint32_t max) -{ - if (value < 0) { - value = 0; - } - - if (min && value < min) { - value = min; - } - - if (max) { - if (min && max < min) { - max = min; - } - - if (value > max) { - value = max; - } - } - - if (value > UINT32_MAX) { - value = UINT32_MAX; - } - - return value; -} - -static void -clamp_window_size(const struct window *window, uint32_t *width, - uint32_t *height) -{ - *width = - clamp_dimension(*width, window->base.min_width, window->base.max_width); - *height = clamp_dimension(*height, window->base.min_height, - window->base.max_height); -} - -static void -handle_window_enter(struct wl_listener *listener, void *data) -{ - struct event *event = data; - struct input_focus_event_data *event_data = event->data; - struct window *window; - - if (event->type != INPUT_FOCUS_EVENT_CHANGED) { - return; - } - - if (!event_data->new || !(window = event_data->new->window)) { - return; - } - - if (window->handler->entered) { - window->handler->entered(window->handler_data); - } -} - -struct wl_listener window_enter_listener = { - .notify = handle_window_enter, -}; - -static void -begin_interaction(struct window_pointer_interaction *interaction, - struct button *button) -{ - if (button) { - /* Store the serial of the button press so we are able to cancel the - * interaction if the window changes from stacked mode. */ - interaction->serial = button->press.serial; - interaction->original_handler = button->handler; - button->handler = &interaction->handler; - } else { - interaction->original_handler = NULL; - } - - interaction->active = true; - wl_list_insert(&swc.seat->pointer->handlers, &interaction->handler.link); -} - -static void -end_interaction(struct window_pointer_interaction *interaction, - struct button *button) -{ - if (!interaction->active) { - return; - } - - if (interaction->original_handler) { - if (!button) { - button = pointer_get_button(swc.seat->pointer, interaction->serial); - - if (!button) { - WARNING("No button with serial %u\n", interaction->serial); - goto remove; - } - } - - interaction->original_handler->button(interaction->original_handler, - get_time(), button, - WL_POINTER_BUTTON_STATE_RELEASED); - } - -remove: - interaction->active = false; - wl_list_remove(&interaction->handler.link); -} - -static void -flush(struct window *window) -{ - if (window->move.pending) { - if (window->impl->move) { - window->impl->move(window, window->move.x, window->move.y); - } - - view_move(&window->view->base, window->move.x, window->move.y); - window->move.pending = false; - } -} - -EXPORT void -swc_window_set_handler(struct swc_window *base, - const struct swc_window_handler *handler, void *data) -{ - struct window *window = INTERNAL(base); - - window->handler = handler; - window->handler_data = data; -} - -EXPORT void -swc_window_close(struct swc_window *base) -{ - struct window *window = INTERNAL(base); - - if (window->impl->close) { - window->impl->close(window); - } -} - -EXPORT void -swc_window_show(struct swc_window *window) -{ - compositor_view_show(INTERNAL(window)->view); -} - -EXPORT void -swc_window_hide(struct swc_window *window) -{ - compositor_view_hide(INTERNAL(window)->view); -} - -EXPORT void -swc_window_focus(struct swc_window *base) -{ - struct window *window = INTERNAL(base); - struct compositor_view *new = window ? window->view : NULL, - *old = swc.seat->keyboard->focus.view; - - if (new == old) { - return; - } - - /* Focus the new window before unfocusing the old one in case both are X11 - * windows so the xwl_window implementation can handle this transition - * correctly. */ - if (window && window->impl->focus) { - window->impl->focus(window); - } - if (old && old->window && old->window->impl->unfocus) { - old->window->impl->unfocus(old->window); - } - - keyboard_set_focus(swc.seat->keyboard, new); -} - -EXPORT void -swc_window_set_parent(struct swc_window *base, struct swc_window *parent) -{ - window_set_parent(INTERNAL(base), parent ? INTERNAL(parent) : NULL); -} - -EXPORT void -swc_window_set_attached(struct swc_window *base, bool attached) -{ - struct window *window = INTERNAL(base); - - if (window->view) { - window->view->attached = attached; - } -} - -EXPORT void -swc_window_raise(struct swc_window *base) -{ - struct window *window = INTERNAL(base); - - if (window->view) { - raise_window(window->view); - } -} - -EXPORT void -swc_window_set_stacked(struct swc_window *base) -{ - struct window *window = INTERNAL(base); - - flush(window); - window->configure.pending = false; - window->configure.width = 0; - window->configure.height = 0; - if (window->impl->set_mode) { - window->impl->set_mode(window, WINDOW_MODE_STACKED); - } - window->mode = WINDOW_MODE_STACKED; -} - -EXPORT void -swc_window_set_tiled(struct swc_window *base) -{ - struct window *window = INTERNAL(base); - - end_interaction(&window->move.interaction, NULL); - end_interaction(&window->resize.interaction, NULL); - if (window->impl->set_mode) { - window->impl->set_mode(window, WINDOW_MODE_TILED); - } - window->mode = WINDOW_MODE_TILED; -} - -EXPORT void -swc_window_set_fullscreen(struct swc_window *base, struct swc_screen *screen) -{ - struct window *window = INTERNAL(base); - - struct swc_rectangle geom; - swc_window_get_geometry(base, &geom); - - if (window->mode != WINDOW_MODE_FULLSCREEN) { - window->prev.geom = geom; - window->prev.mode = window->mode; - swc_window_set_geometry(base, &screen->usable_geometry); - - if (window->impl->set_mode) { - window->impl->set_mode(window, WINDOW_MODE_FULLSCREEN); - } - window->mode = WINDOW_MODE_FULLSCREEN; - } - - else { - swc_window_set_geometry(base, &window->prev.geom); - window->mode = window->prev.mode; - } -} - -EXPORT void -swc_window_set_position(struct swc_window *base, int32_t x, int32_t y) -{ - struct window *window = INTERNAL(base); - struct swc_rectangle *geometry = &window->view->base.geometry; - - if (x == geometry->x && y == geometry->y) { - window->move.pending = false; - return; - } - - window->move.x = x; - window->move.y = y; - window->move.pending = true; - - /* If we don't have a configure pending, perform the move now. */ - if (!window->configure.pending) { - flush(window); - } -} - -EXPORT void -swc_window_set_size(struct swc_window *base, uint32_t width, uint32_t height) -{ - struct window *window = INTERNAL(base); - struct swc_rectangle *geom = &window->view->base.geometry; - - clamp_window_size(window, &width, &height); - - if ((window->configure.pending && width == window->configure.width && - height == window->configure.height) || - (!window->configure.pending && width == geom->width && - height == geom->height)) { - return; - } - - window->impl->configure(window, width, height); - - if (window->mode == WINDOW_MODE_TILED) { - window->configure.width = width; - window->configure.height = height; - window->configure.pending = true; - } -} - -EXPORT void -swc_window_set_geometry(struct swc_window *window, - const struct swc_rectangle *geometry) -{ - swc_window_set_size(window, geometry->width, geometry->height); - swc_window_set_position(window, geometry->x, geometry->y); -} - -EXPORT bool -swc_window_get_geometry(const struct swc_window *base, - struct swc_rectangle *geometry) -{ - struct window *window = INTERNAL((struct swc_window *)base); - - if (!window || !geometry) { - return false; - } - - *geometry = window->view->base.geometry; - return true; -} - -EXPORT void -swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, - uint32_t inner_border_width, uint32_t outer_border_color, - uint32_t outer_border_width) -{ - struct compositor_view *view = INTERNAL(window)->view; - - compositor_view_set_border_color(view, outer_border_color, - inner_border_color); - compositor_view_set_border_width(view, outer_border_width, - inner_border_width); -} - -EXPORT void -swc_window_set_border_placement(struct swc_window *window, - enum swc_border_placement placement) -{ - compositor_view_set_border_frame(INTERNAL(window)->view, - placement == SWC_BORDER_FRAME); -} - -EXPORT void -swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor) -{ - struct compositor_view *view = INTERNAL(window)->view; - - compositor_view_set_decor(view, decor); -} - -EXPORT void -swc_window_begin_move(struct swc_window *window) -{ - window_begin_move(INTERNAL(window), NULL); -} - -EXPORT void -swc_window_end_move(struct swc_window *window) -{ - end_interaction(&INTERNAL(window)->move.interaction, NULL); -} - -EXPORT void -swc_window_begin_resize(struct swc_window *window, uint32_t edges) -{ - window_begin_resize(INTERNAL(window), edges, NULL); -} - -EXPORT void -swc_window_end_resize(struct swc_window *window) -{ - end_interaction(&INTERNAL(window)->resize.interaction, NULL); -} - -static bool -move_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, - wl_fixed_t fy) -{ - struct window *window = - wl_container_of(handler, window, move.interaction.handler); - - if (should_throttle_motion(window->base.motion_throttle_ms, - &window->move.last_time, time)) { - return true; - } - - int32_t x = wl_fixed_to_int(fx) + window->move.offset.x, - y = wl_fixed_to_int(fy) + window->move.offset.y; - - view_move(&window->view->base, x, y); - return true; -} - -static bool -resize_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, - wl_fixed_t fy) -{ - struct window *window = - wl_container_of(handler, window, resize.interaction.handler); - const struct swc_rectangle *geometry = &window->view->base.geometry; - uint32_t width = geometry->width, height = geometry->height; - - if (should_throttle_motion(window->base.motion_throttle_ms, - &window->resize.last_time, time)) { - return true; - } - - if (window->resize.edges & SWC_WINDOW_EDGE_LEFT) { - width -= wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; - } else if (window->resize.edges & SWC_WINDOW_EDGE_RIGHT) { - width = wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; - } - - if (window->resize.edges & SWC_WINDOW_EDGE_TOP) { - height -= wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; - } else if (window->resize.edges & SWC_WINDOW_EDGE_BOTTOM) { - height = wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; - } - - clamp_window_size(window, &width, &height); - window->impl->configure(window, width, height); - - return true; -} - -static bool -handle_button(struct pointer_handler *handler, uint32_t time, - struct button *button, uint32_t state) -{ - struct window_pointer_interaction *interaction = - wl_container_of(handler, interaction, handler); - - if (state != WL_POINTER_BUTTON_STATE_RELEASED || - !interaction->original_handler) { - return false; - } - - end_interaction(interaction, button); - return true; -} - -static void -handle_attach(struct view_handler *handler) -{ - struct window *window = wl_container_of(handler, window, view_handler); - - if (window->configure.acknowledged) { - flush(window); - } - window->configure.pending = false; -} - -static void -handle_resize(struct view_handler *handler, uint32_t old_width, - uint32_t old_height) -{ - struct window *window = wl_container_of(handler, window, view_handler); - - if (window->resize.interaction.active && - window->resize.edges & (SWC_WINDOW_EDGE_TOP | SWC_WINDOW_EDGE_LEFT)) { - const struct swc_rectangle *geometry = &window->view->base.geometry; - int32_t x = geometry->x, y = geometry->y; - - if (window->resize.edges & SWC_WINDOW_EDGE_LEFT) { - x += old_width - geometry->width; - } - if (window->resize.edges & SWC_WINDOW_EDGE_TOP) { - y += old_height - geometry->height; - } - - view_move(&window->view->base, x, y); - } - - if (window->handler->size_changed) { - window->handler->size_changed(window->handler_data); - } -} - -static const struct view_handler_impl view_handler_impl = { - .attach = handle_attach, - .resize = handle_resize, -}; - -bool -window_initialize(struct window *window, const struct window_impl *impl, - struct surface *surface) -{ - DEBUG("Initializing window, %p\n", window); - - window->base.title = NULL; - window->base.app_id = NULL; - window->base.parent = NULL; - - if (surface->view) { - window->view = compositor_view(surface->view); - if (!window->view || window->view->window) { - return false; - } - } else { - if (!(window->view = compositor_create_view(surface))) { - return false; - } - } - - window->impl = impl; - window->handler = &null_handler; - window->view_handler.impl = &view_handler_impl; - window->view->window = window; - window->base.motion_throttle_ms = def_motion_throttle_ms; - window->base.min_width = 0; - window->base.min_height = 0; - window->base.max_width = 0; - window->base.max_height = 0; - window->managed = false; - window->mode = WINDOW_MODE_STACKED; - window->move.pending = false; - window->move.last_time = 0; - window->move.interaction.active = false; - window->move.interaction.handler = (struct pointer_handler){ - .motion = move_motion, - .button = handle_button, - }; - window->configure.pending = false; - window->configure.width = 0; - window->configure.height = 0; - window->resize.interaction.active = false; - window->resize.interaction.handler = (struct pointer_handler){ - .motion = resize_motion, - .button = handle_button, - }; - window->resize.last_time = 0; - - wl_list_insert(&window->view->base.handlers, &window->view_handler.link); - - return true; -} - -void -window_finalize(struct window *window) -{ - DEBUG("Finalizing window, %p\n", window); - - window_unmanage(window); - compositor_view_destroy(window->view); - free(window->base.title); - free(window->base.app_id); -} - -void -window_manage(struct window *window) -{ - if (window->managed) { - return; - } - - swc.manager->new_window(&window->base); - window->managed = true; -} - -void -window_unmanage(struct window *window) -{ - if (!window->managed) { - return; - } - - if (window->handler->destroy) { - window->handler->destroy(window->handler_data); - } - window->handler = &null_handler; - window->managed = false; -} - -void -window_set_title(struct window *window, const char *title, size_t length) -{ - free(window->base.title); - window->base.title = strndup(title, length); - - if (window->handler->title_changed) { - window->handler->title_changed(window->handler_data); - } -} - -void -window_set_app_id(struct window *window, const char *app_id) -{ - free(window->base.app_id); - window->base.app_id = strdup(app_id); - - if (window->handler->app_id_changed) { - window->handler->app_id_changed(window->handler_data); - } -} - -void -window_set_modal(struct window *window, bool modal) -{ - if (window->base.modal == modal) { - return; - } - window->base.modal = modal; - if (window->handler->modal_changed) { - window->handler->modal_changed(window->handler_data); - } -} - -void -window_set_parent(struct window *window, struct window *parent) -{ - struct swc_window *base = parent ? &parent->base : NULL; - - if (window->base.parent == base || window == parent) { - return; - } - - compositor_view_set_parent(window->view, parent ? parent->view : NULL); - window->base.parent = base; - if (parent) { - raise_window(parent->view); - } - - if (window->handler->parent_changed) { - window->handler->parent_changed(window->handler_data); - } -} - -void -window_begin_move(struct window *window, struct button *button) -{ - if (window->mode != WINDOW_MODE_STACKED && window->handler->move) { - window->handler->move(window->handler_data); - } - - if (window->mode != WINDOW_MODE_STACKED || - window->move.interaction.active) { - return; - } - - struct swc_rectangle *geometry = &window->view->base.geometry; - int32_t px = wl_fixed_to_int(swc.seat->pointer->x), - py = wl_fixed_to_int(swc.seat->pointer->y); - - begin_interaction(&window->move.interaction, button); - window->move.last_time = 0; - window->move.offset.x = geometry->x - px; - window->move.offset.y = geometry->y - py; -} - -void -window_begin_resize(struct window *window, uint32_t edges, - struct button *button) -{ - if (window->mode != WINDOW_MODE_STACKED && window->handler->resize) { - window->handler->resize(window->handler_data); - } - - if (window->mode != WINDOW_MODE_STACKED || - window->resize.interaction.active) { - return; - } - - struct swc_rectangle *geometry = &window->view->base.geometry; - int32_t px = wl_fixed_to_int(swc.seat->pointer->x), - py = wl_fixed_to_int(swc.seat->pointer->y); - - begin_interaction(&window->resize.interaction, button); - window->resize.last_time = 0; - - if (!edges) { - edges |= (px < geometry->x + geometry->width / 2) - ? SWC_WINDOW_EDGE_LEFT - : SWC_WINDOW_EDGE_RIGHT; - edges |= (py < geometry->y + geometry->height / 2) - ? SWC_WINDOW_EDGE_TOP - : SWC_WINDOW_EDGE_BOTTOM; - } - - window->resize.offset.x = - geometry->x - px + - ((edges & SWC_WINDOW_EDGE_RIGHT) ? geometry->width : 0); - window->resize.offset.y = - geometry->y - py + - ((edges & SWC_WINDOW_EDGE_BOTTOM) ? geometry->height : 0); - window->resize.edges = edges; -} - -EXPORT pid_t -swc_window_get_pid(struct swc_window *base) -{ - struct window *window = INTERNAL(base); - struct surface *surface; - struct wl_client *client; - pid_t pid; - uid_t uid; - gid_t gid; - - if (!window || !window->view || !window->view->surface) { - return 0; - } - - surface = window->view->surface; - if (!surface->resource) { - return 0; - } - - client = wl_resource_get_client(surface->resource); - wl_client_get_credentials(client, &pid, &uid, &gid); - - return pid; -} diff --git a/libswc/window.h b/libswc/window.h @@ -47,7 +47,6 @@ struct window { const struct window_impl *impl; const struct swc_window_handler *handler; void *handler_data; - void (*fullscreen)(void *, bool); struct compositor_view *view; struct view_handler view_handler; diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c @@ -356,7 +356,9 @@ send_state(struct xdg_toplevel *toplevel) struct window *window = &toplevel->window; struct swc_rectangle *geometry = &window->view->base.geometry; - if (window->configure.pending) + /* a window not yet mapped has no geometry: it keeps the size it was + * last asked for rather than being told to choose its own */ + if (window->configure.pending || !geometry->width || !geometry->height) send_configure(toplevel, window->configure.width, window->configure.height); else send_configure(toplevel, geometry->width, geometry->height); @@ -533,8 +535,8 @@ set_fullscreen(struct wl_client *client, struct wl_resource *resource, struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); struct window *window = &toplevel->window; - if (window->fullscreen) - window->fullscreen(window->handler_data, true); + if (window->handler->fullscreen) + window->handler->fullscreen(window->handler_data, true); } static void @@ -543,8 +545,8 @@ unset_fullscreen(struct wl_client *client, struct wl_resource *resource) struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); struct window *window = &toplevel->window; - if (window->fullscreen) - window->fullscreen(window->handler_data, false); + if (window->handler->fullscreen) + window->handler->fullscreen(window->handler_data, false); } static void @@ -760,10 +762,8 @@ xdg_popup_place(struct xdg_popup *popup, struct compositor_view *parent_view) compositor_view_set_parent(popup->view, parent_view); /* above the parent, in a layer of normal at least */ popup->view->always_top = parent_view->always_top; - compositor_view_set_stack_layer(popup->view, - parent_view->stack_layer > STACK_LAYER_NORMAL ? parent_view->stack_layer - : STACK_LAYER_NORMAL, - true); + compositor_view_set_z(popup->view, parent_view->z > STACK_LAYER_NORMAL ? parent_view->z : STACK_LAYER_NORMAL, + true); view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, parent_view->base.geometry.y + rect.y); xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, diff --git a/libswc/xdg_shell.c.orig b/libswc/xdg_shell.c.orig @@ -1,1075 +0,0 @@ -/* swc: libswc/xdg_shell.c - * - * Copyright (c) 2014, 2018 Michael Forney - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "xdg_shell.h" -#include "compositor.h" -#include "internal.h" -#include "pointer.h" -#include "screen.h" -#include "seat.h" -#include "surface.h" -#include "util.h" -#include "window.h" - -#include "xdg-shell-server-protocol.h" -#include <assert.h> -#include <stdlib.h> -#include <wayland-server.h> - -struct xdg_surface { - struct wl_resource *resource, *role; - struct surface *surface; - struct wl_listener surface_destroy_listener, role_destroy_listener; - uint32_t configure_serial; -}; - -struct xdg_positioner { - int32_t width, height; - int32_t anchor_x, anchor_y; - int32_t anchor_width, anchor_height; - enum xdg_positioner_anchor anchor; - enum xdg_positioner_gravity gravity; - enum xdg_positioner_constraint_adjustment constraint; - int32_t offset_x, offset_y; -}; - -struct xdg_toplevel { - struct window window; - struct wl_resource *resource; - struct wl_array states; - struct xdg_surface *xdg_surface; -}; - -struct xdg_popup { - struct wl_resource *resource; - struct xdg_surface *xdg_surface; - struct xdg_positioner positioner; - struct compositor_view *view; - struct wl_list grab_link; /* in grabs while it holds a grab */ - bool grabbing; -}; - -/* popups holding a grab, newest last. a press elsewhere dismisses them newest first */ -static struct wl_list grabs = {&grabs, &grabs}; - -static bool popup_grab_button(struct pointer_handler *handler, uint32_t time, - struct button *button, uint32_t state); - -static struct pointer_handler popup_grab_handler = { - .button = popup_grab_button, -}; - -static void -grab_end(struct xdg_popup *popup) -{ - if (!popup->grabbing) { - return; - } - wl_list_remove(&popup->grab_link); - popup->grabbing = false; - if (wl_list_empty(&grabs)) { - wl_list_remove(&popup_grab_handler.link); - wl_list_init(&popup_grab_handler.link); - } -} - -static bool -popup_grab_button(struct pointer_handler *handler, uint32_t time, - struct button *button, uint32_t state) -{ - struct xdg_popup *top, *popup, *tmp; - struct wl_client *focus = swc.seat->pointer->focus.client; - - if (state != WL_POINTER_BUTTON_STATE_PRESSED || wl_list_empty(&grabs)) { - return false; - } - top = wl_container_of(grabs.prev, top, grab_link); - if (focus && focus == wl_resource_get_client(top->resource)) { - return false; - } - wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { - xdg_popup_send_popup_done(popup->resource); - grab_end(popup); - } - return true; -} - -/* Keyboard focus went to client: a menu is a grab, and a grab ends when focus - * leaves what grabbed it, as menus do on every desktop. A press elsewhere - * is the other way it ends (popup_grab_button). */ -void -xdg_popups_focus_changed(struct wl_client *client) -{ - struct xdg_popup *top, *popup, *tmp; - - if (wl_list_empty(&grabs)) { - return; - } - top = wl_container_of(grabs.prev, top, grab_link); - if (client && client == wl_resource_get_client(top->resource)) { - return; - } - wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { - xdg_popup_send_popup_done(popup->resource); - grab_end(popup); - } -} - -/* xdg_positioner */ -static void -destroy_positioner(struct wl_resource *resource) -{ - struct xdg_positioner *positioner = wl_resource_get_user_data(resource); - - free(positioner); -} - -static void -set_size(struct wl_client *client, struct wl_resource *resource, int32_t width, - int32_t height) -{ - struct xdg_positioner *positioner = wl_resource_get_user_data(resource); - - if (width <= 0 || height <= 0) { - wl_resource_post_error(resource, XDG_POSITIONER_ERROR_INVALID_INPUT, - "invalid size"); - return; - } - positioner->width = width; - positioner->height = height; -} - -static void -set_anchor_rect(struct wl_client *client, struct wl_resource *resource, - int32_t x, int32_t y, int32_t width, int32_t height) -{ - struct xdg_positioner *positioner = wl_resource_get_user_data(resource); - - if (width <= 0 || height <= 0) { - wl_resource_post_error(resource, XDG_POSITIONER_ERROR_INVALID_INPUT, - "invalid anchor size"); - return; - } - positioner->anchor_x = x; - positioner->anchor_y = y; - positioner->anchor_width = width; - positioner->anchor_height = height; -} - -static void -set_anchor(struct wl_client *client, struct wl_resource *resource, - uint32_t anchor) -{ - struct xdg_positioner *positioner = wl_resource_get_user_data(resource); - - positioner->anchor = anchor; -} - -static void -set_gravity(struct wl_client *client, struct wl_resource *resource, - uint32_t gravity) -{ - struct xdg_positioner *positioner = wl_resource_get_user_data(resource); - - positioner->gravity = gravity; -} - -static void -set_constraint_adjustment(struct wl_client *client, - struct wl_resource *resource, uint32_t constraint) -{ - struct xdg_positioner *positioner = wl_resource_get_user_data(resource); - - positioner->constraint = constraint; -} - -static void -set_offset(struct wl_client *client, struct wl_resource *resource, int32_t x, - int32_t y) -{ - struct xdg_positioner *positioner = wl_resource_get_user_data(resource); - - positioner->offset_x = x; - positioner->offset_y = y; -} - -static const struct xdg_positioner_interface positioner_impl = { - .destroy = destroy_resource, - .set_size = set_size, - .set_anchor_rect = set_anchor_rect, - .set_anchor = set_anchor, - .set_gravity = set_gravity, - .set_constraint_adjustment = set_constraint_adjustment, - .set_offset = set_offset, -}; - -static struct swc_rectangle -calculate_position(struct xdg_positioner *positioner) -{ - struct swc_rectangle r = { - .x = positioner->offset_x, - .y = positioner->offset_y, - .width = positioner->width, - .height = positioner->height, - }; - - switch (positioner->anchor) { - case XDG_POSITIONER_ANCHOR_TOP: - case XDG_POSITIONER_ANCHOR_TOP_LEFT: - case XDG_POSITIONER_ANCHOR_TOP_RIGHT: - r.y += positioner->anchor_y; - break; - case XDG_POSITIONER_ANCHOR_BOTTOM: - case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT: - case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT: - r.y += positioner->anchor_y + positioner->anchor_height; - break; - default: - r.y += positioner->anchor_y + positioner->anchor_height / 2; - } - switch (positioner->anchor) { - case XDG_POSITIONER_ANCHOR_LEFT: - case XDG_POSITIONER_ANCHOR_TOP_LEFT: - case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT: - r.x += positioner->anchor_x; - break; - case XDG_POSITIONER_ANCHOR_RIGHT: - case XDG_POSITIONER_ANCHOR_TOP_RIGHT: - case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT: - r.x += positioner->anchor_x + positioner->anchor_width; - break; - default: - r.x += positioner->anchor_x + positioner->anchor_width / 2; - } - - switch (positioner->gravity) { - case XDG_POSITIONER_GRAVITY_TOP: - case XDG_POSITIONER_GRAVITY_TOP_LEFT: - case XDG_POSITIONER_GRAVITY_TOP_RIGHT: - r.y -= r.height; - break; - case XDG_POSITIONER_GRAVITY_BOTTOM: - case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT: - case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT: - break; - default: - r.y -= r.height / 2; - } - switch (positioner->gravity) { - case XDG_POSITIONER_GRAVITY_LEFT: - case XDG_POSITIONER_GRAVITY_TOP_LEFT: - case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT: - r.x -= r.width; - break; - case XDG_POSITIONER_GRAVITY_RIGHT: - case XDG_POSITIONER_GRAVITY_TOP_RIGHT: - case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT: - break; - default: - r.x -= r.width / 2; - } - - return r; -} - -/* xdg_toplevel */ -static void -destroy_toplevel(struct wl_resource *resource) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - - window_finalize(&toplevel->window); - free(toplevel); -} - -static bool -add_state(struct xdg_toplevel *toplevel, uint32_t state) -{ - uint32_t *current_state; - - wl_array_for_each(current_state, &toplevel->states) - { - if (*current_state == state) { - return false; - } - } - - if (!(current_state = wl_array_add(&toplevel->states, sizeof(state)))) { - WARNING("xdg_toplevel: Failed to allocate new state\n"); - return false; - } - - *current_state = state; - return true; -} - -static bool -remove_state(struct xdg_toplevel *toplevel, uint32_t state) -{ - uint32_t *current_state; - - wl_array_for_each(current_state, &toplevel->states) - { - if (*current_state == state) { - array_remove(&toplevel->states, current_state, sizeof(state)); - return true; - } - } - - return false; -} - -static void -send_configure(struct xdg_toplevel *toplevel, int32_t width, int32_t height) -{ - uint32_t serial = wl_display_next_serial(swc.display); - - xdg_toplevel_send_configure(toplevel->resource, width, height, - &toplevel->states); - xdg_surface_send_configure(toplevel->xdg_surface->resource, serial); - toplevel->xdg_surface->configure_serial = serial; -} - -static void -send_state(struct xdg_toplevel *toplevel) -{ - struct window *window = &toplevel->window; - struct swc_rectangle *geometry = &window->view->base.geometry; - - if (window->configure.pending) - send_configure(toplevel, window->configure.width, window->configure.height); - else - send_configure(toplevel, geometry->width, geometry->height); -} - -static void -configure(struct window *window, uint32_t width, uint32_t height) -{ - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); - - window->configure.acknowledged = false; - send_configure(toplevel, width, height); -} - -static void -focus(struct window *window) -{ - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); - - add_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); - send_state(toplevel); -} - -static void -unfocus(struct window *window) -{ - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); - - remove_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); - send_state(toplevel); -} - -static void -close_(struct window *window) -{ - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); - - xdg_toplevel_send_close(toplevel->resource); -} - -static void -set_mode(struct window *window, unsigned mode) -{ - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); - - switch (window->mode) { - case WINDOW_MODE_TILED: - remove_state(toplevel, XDG_TOPLEVEL_STATE_MAXIMIZED); - break; - case WINDOW_MODE_FULLSCREEN: - remove_state(toplevel, XDG_TOPLEVEL_STATE_FULLSCREEN); - break; - } - - switch (mode) { - case WINDOW_MODE_TILED: - add_state(toplevel, XDG_TOPLEVEL_STATE_MAXIMIZED); - break; - case WINDOW_MODE_FULLSCREEN: - add_state(toplevel, XDG_TOPLEVEL_STATE_FULLSCREEN); - break; - } - - send_state(toplevel); -} - -static const struct window_impl toplevel_window_impl = { - .configure = configure, - .focus = focus, - .unfocus = unfocus, - .close = close_, - .set_mode = set_mode, -}; - -static void -set_parent(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *parent_resource) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource), - *parent = NULL; - - if (parent_resource) { - parent = wl_resource_get_user_data(parent_resource); - } - window_set_parent(&toplevel->window, parent ? &parent->window : NULL); -} - -static void -set_title(struct wl_client *client, struct wl_resource *resource, - const char *title) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - window_set_title(&toplevel->window, title, -1); -} - -static void -set_app_id(struct wl_client *client, struct wl_resource *resource, - const char *app_id) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - window_set_app_id(&toplevel->window, app_id); -} - -static void -show_window_menu(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *seat, uint32_t serial, int32_t x, - int32_t y) -{ -} - -static void -move(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *seat, uint32_t serial) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - struct button *button; - - button = pointer_get_button(swc.seat->pointer, serial); - if (button) { - window_begin_move(&toplevel->window, button); - } -} - -static void -resize(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *seat, uint32_t serial, uint32_t edges) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - struct button *button; - - button = pointer_get_button(swc.seat->pointer, serial); - if (button) { - window_begin_resize(&toplevel->window, edges, button); - } -} - -static void -set_max_size(struct wl_client *client, struct wl_resource *resource, - int32_t width, int32_t height) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - - (void)client; - /* 0 is no limit */ - toplevel->window.base.max_width = width > 0 ? (uint32_t)width : 0; - toplevel->window.base.max_height = height > 0 ? (uint32_t)height : 0; -} - -static void -set_min_size(struct wl_client *client, struct wl_resource *resource, - int32_t width, int32_t height) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - - (void)client; - toplevel->window.base.min_width = width > 0 ? (uint32_t)width : 1; - toplevel->window.base.min_height = height > 0 ? (uint32_t)height : 1; -} - -static void -set_maximized(struct wl_client *client, struct wl_resource *resource) -{ -} - -static void -unset_maximized(struct wl_client *client, struct wl_resource *resource) -{ -} - -static void -set_fullscreen(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *output) -{ -} - -static void -unset_fullscreen(struct wl_client *client, struct wl_resource *resource) -{ -} - -static void -set_minimized(struct wl_client *client, struct wl_resource *resource) -{ -} - -static const struct xdg_toplevel_interface toplevel_impl = { - .destroy = destroy_resource, - .set_parent = set_parent, - .set_title = set_title, - .set_app_id = set_app_id, - .show_window_menu = show_window_menu, - .move = move, - .resize = resize, - .set_max_size = set_max_size, - .set_min_size = set_min_size, - .set_maximized = set_maximized, - .unset_maximized = unset_maximized, - .set_fullscreen = set_fullscreen, - .unset_fullscreen = unset_fullscreen, - .set_minimized = set_minimized, -}; - -struct window * -xdg_toplevel_window(struct wl_resource *resource) -{ - struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); - - return toplevel ? &toplevel->window : NULL; -} - -static struct xdg_toplevel * -xdg_toplevel_new(struct wl_client *client, uint32_t version, uint32_t id, - struct xdg_surface *xdg_surface) -{ - struct xdg_toplevel *toplevel; - - toplevel = malloc(sizeof(*toplevel)); - if (!toplevel) { - goto error0; - } - toplevel->xdg_surface = xdg_surface; - toplevel->resource = - wl_resource_create(client, &xdg_toplevel_interface, version, id); - if (!toplevel->resource) { - goto error1; - } - if (!surface_set_role(xdg_surface->surface, toplevel->resource)) { - goto error2; - } - if (!window_initialize(&toplevel->window, &toplevel_window_impl, - xdg_surface->surface)) { - goto error2; - } - wl_array_init(&toplevel->states); - wl_resource_set_implementation(toplevel->resource, &toplevel_impl, toplevel, - &destroy_toplevel); - window_manage(&toplevel->window); - - return toplevel; - -error2: - wl_resource_destroy(toplevel->resource); - free(toplevel); -error1: -error0: - return NULL; -} - -/* xdg_popup */ -static void -destroy_popup(struct wl_resource *resource) -{ - struct xdg_popup *popup = wl_resource_get_user_data(resource); - - grab_end(popup); - compositor_view_destroy(popup->view); - free(popup); -} - -static void -grab(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *seat, uint32_t serial) -{ - struct xdg_popup *popup = wl_resource_get_user_data(resource); - - if (popup->grabbing) { - return; - } - if (wl_list_empty(&grabs)) { - /* before bindings and clients */ - wl_list_insert(&swc.seat->pointer->handlers, &popup_grab_handler.link); - } - wl_list_insert(grabs.prev, &popup->grab_link); - popup->grabbing = true; -} - -static const struct xdg_popup_interface popup_impl = { - .destroy = destroy_resource, - .grab = grab, -}; - -/* mirror an anchor or gravity, the enums share values */ -static uint32_t -mirror_y(uint32_t v) -{ - static const uint32_t to[] = {0, 2, 1, 3, 4, 6, 5, 8, 7}; - - return v < 9 ? to[v] : v; -} - -static uint32_t -mirror_x(uint32_t v) -{ - static const uint32_t to[] = {0, 1, 2, 4, 3, 7, 8, 5, 6}; - - return v < 9 ? to[v] : v; -} - -/* screen at x, y, else the first */ -static struct swc_rectangle * -screen_at(int32_t x, int32_t y) -{ - struct screen *screen, *first = NULL; - - wl_list_for_each (screen, &swc.screens, link) { - struct swc_rectangle *g = &screen->base.geometry; - - if (!first) { - first = screen; - } - if (x >= g->x && y >= g->y && x < g->x + (int32_t)g->width && y < g->y + (int32_t)g->height) { - return g; - } - } - return first ? &first->base.geometry : NULL; -} - -/* popup position kept on screen: flip, then slide, as the positioner allows */ -static struct swc_rectangle -constrained_position(struct xdg_positioner *positioner, int32_t px, int32_t py) -{ - struct swc_rectangle r = calculate_position(positioner), f; - struct swc_rectangle *g = screen_at(px + r.x, py + r.y); - struct xdg_positioner flipped; - - if (!g) { - return r; - } - if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y && - py + r.y + r.height > g->y + (int32_t)g->height) { - flipped = *positioner; - flipped.anchor = mirror_y(flipped.anchor); - flipped.gravity = mirror_y(flipped.gravity); - flipped.offset_y = -flipped.offset_y; - f = calculate_position(&flipped); - if (py + f.y >= g->y) { - r.y = f.y; - } - } - if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X && - px + r.x + r.width > g->x + (int32_t)g->width) { - flipped = *positioner; - flipped.anchor = mirror_x(flipped.anchor); - flipped.gravity = mirror_x(flipped.gravity); - flipped.offset_x = -flipped.offset_x; - f = calculate_position(&flipped); - if (px + f.x >= g->x) { - r.x = f.x; - } - } - if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y) { - if (py + r.y + r.height > g->y + (int32_t)g->height) { - r.y = g->y + (int32_t)g->height - r.height - py; - } - if (py + r.y < g->y) { - r.y = g->y - py; - } - } - if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X) { - if (px + r.x + r.width > g->x + (int32_t)g->width) { - r.x = g->x + (int32_t)g->width - r.width - px; - } - if (px + r.x < g->x) { - r.x = g->x - px; - } - } - /* cut what still does not fit and tell the client */ - if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y && - py + r.y + r.height > g->y + (int32_t)g->height) { - r.height = g->y + (int32_t)g->height - py - r.y; - } - if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X && - px + r.x + r.width > g->x + (int32_t)g->width) { - r.width = g->x + (int32_t)g->width - px - r.x; - } - return r; -} - -/* place popup against its parent and tell the client */ -static bool -xdg_popup_place(struct xdg_popup *popup, struct compositor_view *parent_view) -{ - uint32_t serial = wl_display_next_serial(swc.display); - struct swc_rectangle rect; - - if (!parent_view) { - return false; - } - rect = constrained_position(&popup->positioner, parent_view->base.geometry.x, - parent_view->base.geometry.y); - compositor_view_set_parent(popup->view, parent_view); - /* above the parent, in a layer of normal at least */ - popup->view->always_top = parent_view->always_top; - compositor_view_set_stack_layer(popup->view, - parent_view->stack_layer > STACK_LAYER_NORMAL ? parent_view->stack_layer - : STACK_LAYER_NORMAL, - true); - view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, - parent_view->base.geometry.y + rect.y); - xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, - rect.height); - xdg_surface_send_configure(popup->xdg_surface->resource, serial); - return true; -} - -void -xdg_popup_set_parent(struct wl_resource *resource, struct surface *parent) -{ - struct xdg_popup *popup; - - if (!resource || !wl_resource_instance_of(resource, &xdg_popup_interface, &popup_impl)) { - return; - } - popup = wl_resource_get_user_data(resource); - xdg_popup_place(popup, compositor_view(parent->view)); -} - -/* a popup with no parent waits for one, as layer surface popups do */ -static struct xdg_popup * -xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, - struct xdg_surface *xdg_surface, struct xdg_surface *parent, - struct xdg_positioner *positioner) -{ - struct xdg_popup *popup; - struct compositor_view *parent_view = - parent ? compositor_view(parent->surface->view) : NULL; - - if (parent && !parent_view) { - goto error0; - } - popup = malloc(sizeof(*popup)); - if (!popup) { - goto error0; - } - popup->xdg_surface = xdg_surface; - popup->positioner = *positioner; - popup->grabbing = false; - popup->resource = - wl_resource_create(client, &xdg_popup_interface, version, id); - if (!popup->resource) { - goto error1; - } - if (!surface_set_role(xdg_surface->surface, popup->resource)) { - goto error2; - } - popup->view = compositor_create_view(xdg_surface->surface); - if (!popup->view) { - goto error3; - } - wl_resource_set_implementation(popup->resource, &popup_impl, popup, - &destroy_popup); - if (parent_view) { - xdg_popup_place(popup, parent_view); - } - - return popup; - -error3: - wl_resource_destroy(popup->resource); -error2: - free(popup); -error1: -error0: - return NULL; -} - -/* xdg_surface */ -static void -get_toplevel(struct wl_client *client, struct wl_resource *resource, - uint32_t id) -{ - struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); - struct xdg_toplevel *toplevel; - - if (xdg_surface->role) { - wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, - "surface already has a role"); - return; - } - if (xdg_surface->surface->role) { - wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, - "surface already has a role"); - return; - } - toplevel = xdg_toplevel_new(client, wl_resource_get_version(resource), id, - xdg_surface); - if (!toplevel) { - wl_client_post_no_memory(client); - return; - } - xdg_surface->role = toplevel->resource; - wl_resource_add_destroy_listener(xdg_surface->role, - &xdg_surface->role_destroy_listener); -} - -static void -get_popup(struct wl_client *client, struct wl_resource *resource, uint32_t id, - struct wl_resource *parent_resource, - struct wl_resource *positioner_resource) -{ - struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); - struct xdg_surface *parent = - parent_resource ? wl_resource_get_user_data(parent_resource) : NULL; - struct xdg_positioner *positioner = - wl_resource_get_user_data(positioner_resource); - struct xdg_popup *popup; - - if (xdg_surface->role) { - wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, - "surface already has a role"); - return; - } - if (xdg_surface->surface->role) { - wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, - "surface already has a role"); - return; - } - popup = xdg_popup_new(client, wl_resource_get_version(resource), id, - xdg_surface, parent, positioner); - if (!popup) { - wl_client_post_no_memory(client); - return; - } - xdg_surface->role = popup->resource; - wl_resource_add_destroy_listener(xdg_surface->role, - &xdg_surface->role_destroy_listener); -} - -static void -ack_configure(struct wl_client *client, struct wl_resource *resource, - uint32_t serial) -{ - struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); - struct window *window; - - if (!xdg_surface->role) { - return; - } - window = wl_resource_get_user_data(xdg_surface->role); - if (window && serial == xdg_surface->configure_serial) { - window->configure.acknowledged = true; - } -} - -static void -set_window_geometry(struct wl_client *client, struct wl_resource *resource, - int32_t x, int32_t y, int32_t width, int32_t height) -{ - (void)client; - struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); - struct surface *surface = xdg_surface->surface; - - if (width <= 0 || height <= 0) { - surface->has_window_geometry = false; - return; - } - - surface->has_window_geometry = true; - surface->window_x = x; - surface->window_y = y; - surface->window_width = width; - surface->window_height = height; - if (!surface->window_geometry_applied && surface->view && - (x != 0 || y != 0)) { - struct swc_rectangle *geom = &surface->view->geometry; - view_move(surface->view, geom->x - x, geom->y - y); - surface->window_geometry_applied = true; - } -} - -static const struct xdg_surface_interface xdg_surface_impl = { - .destroy = destroy_resource, - .get_toplevel = get_toplevel, - .get_popup = get_popup, - .ack_configure = ack_configure, - .set_window_geometry = set_window_geometry, -}; - -static void -handle_surface_destroy(struct wl_listener *listener, void *data) -{ - struct xdg_surface *xdg_surface = - wl_container_of(listener, xdg_surface, surface_destroy_listener); - - wl_resource_destroy(xdg_surface->resource); -} - -static void -handle_role_destroy(struct wl_listener *listener, void *data) -{ - struct xdg_surface *xdg_surface = - wl_container_of(listener, xdg_surface, role_destroy_listener); - - xdg_surface->role = NULL; -} - -static void -destroy_xdg_surface(struct wl_resource *resource) -{ - struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); - - wl_list_remove(&xdg_surface->surface_destroy_listener.link); - if (xdg_surface->role) { - wl_resource_destroy(xdg_surface->role); - } - free(xdg_surface); -} - -static struct xdg_surface * -xdg_surface_new(struct wl_client *client, uint32_t version, uint32_t id, - struct surface *surface) -{ - struct xdg_surface *xdg_surface; - - xdg_surface = malloc(sizeof(*xdg_surface)); - if (!xdg_surface) { - goto error0; - } - xdg_surface->resource = - wl_resource_create(client, &xdg_surface_interface, version, id); - if (!xdg_surface->resource) { - goto error1; - } - xdg_surface->surface = surface; - xdg_surface->surface_destroy_listener.notify = &handle_surface_destroy; - xdg_surface->role = NULL; - xdg_surface->role_destroy_listener.notify = &handle_role_destroy; - wl_resource_add_destroy_listener(surface->resource, - &xdg_surface->surface_destroy_listener); - wl_resource_set_implementation(xdg_surface->resource, &xdg_surface_impl, - xdg_surface, destroy_xdg_surface); - - return xdg_surface; - -error1: - free(xdg_surface); -error0: - return NULL; -} - -/* xdg_shell */ -static void -create_positioner(struct wl_client *client, struct wl_resource *resource, - uint32_t id) -{ - struct xdg_positioner *positioner; - struct wl_resource *positioner_resource; - uint32_t version; - - positioner = calloc(1, sizeof(*positioner)); - if (!positioner) { - goto error0; - } - - version = wl_resource_get_version(resource); - positioner_resource = - wl_resource_create(client, &xdg_positioner_interface, version, id); - if (!positioner_resource) { - goto error1; - } - wl_resource_set_implementation(positioner_resource, &positioner_impl, - positioner, &destroy_positioner); - return; - -error1: - free(positioner); -error0: - wl_resource_post_no_memory(resource); -} - -static void -get_xdg_surface(struct wl_client *client, struct wl_resource *resource, - uint32_t id, struct wl_resource *surface_resource) -{ - struct xdg_surface *xdg_surface; - struct surface *surface = wl_resource_get_user_data(surface_resource); - - xdg_surface = - xdg_surface_new(client, wl_resource_get_version(resource), id, surface); - if (!xdg_surface) { - wl_client_post_no_memory(client); - } -} - -static void -pong(struct wl_client *client, struct wl_resource *resource, uint32_t serial) -{ -} - -static const struct xdg_wm_base_interface wm_base_impl = { - .destroy = destroy_resource, - .create_positioner = create_positioner, - .get_xdg_surface = get_xdg_surface, - .pong = pong, -}; - -static void -bind_wm_base(struct wl_client *client, void *data, uint32_t version, - uint32_t id) -{ - struct wl_resource *resource; - - resource = wl_resource_create(client, &xdg_wm_base_interface, version, id); - if (!resource) { - wl_client_post_no_memory(client); - return; - } - wl_resource_set_implementation(resource, &wm_base_impl, NULL, NULL); -} - -struct wl_global * -xdg_shell_create(struct wl_display *display) -{ - return wl_global_create(display, &xdg_wm_base_interface, 1, NULL, - &bind_wm_base); -} diff --git a/patches/borderAbleToGoRoundDecor.patch b/patches/borderAbleToGoRoundDecor.patch @@ -1,159 +0,0 @@ -From 7354520a94d1a196f4318bdc3ad7610625f40111 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:57:41 -0300 -Subject: [PATCH 17/19] window: border placement option - ---- - libswc/compositor.c | 45 ++++++++++++++++++++++++++++++++++++++++----- - libswc/compositor.h | 5 +++++ - libswc/swc.h | 21 +++++++++++++++++++++ - libswc/window.c | 8 ++++++++ - 4 files changed, 74 insertions(+), 5 deletions(-) - -diff --git a/libswc/compositor.c b/libswc/compositor.c -index f4c8373..c1cec76 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -325,7 +325,16 @@ view_buffer_is_opaque(struct compositor_view *view) - static struct swc_rectangle - frame_of(const struct compositor_view *view) - { -- return view->base.geometry; -+ const struct swc_rectangle *geom = &view->base.geometry; -+ struct swc_rectangle frame = *geom; -+ -+ if (view->border.frame) { -+ frame.x -= (int32_t)view->decor.left; -+ frame.y -= (int32_t)view->decor.top; -+ frame.width += view->decor.left + view->decor.right; -+ frame.height += view->decor.top + view->decor.bottom; -+ } -+ return frame; - } - - /* inner ring on the frame, outer ring on the inner, damaged parts only */ -@@ -644,10 +653,10 @@ update_extents(struct compositor_view *view, const struct wld_buffer *buffer) - int64_t decor_y1 = geom_y - (int64_t)view->decor.top; - int64_t decor_x2 = geom_x + geom_w + (int64_t)view->decor.right; - int64_t decor_y2 = geom_y + geom_h + (int64_t)view->decor.bottom; -- int64_t border_x1 = geom_x - total_border; -- int64_t border_y1 = geom_y - total_border; -- int64_t border_x2 = geom_x + geom_w + total_border; -- int64_t border_y2 = geom_y + geom_h + total_border; -+ int64_t border_x1 = (view->border.frame ? decor_x1 : geom_x) - total_border; -+ int64_t border_y1 = (view->border.frame ? decor_y1 : geom_y) - total_border; -+ int64_t border_x2 = (view->border.frame ? decor_x2 : geom_x + geom_w) + total_border; -+ int64_t border_y2 = (view->border.frame ? decor_y2 : geom_y + geom_h) + total_border; - - int64_t buffer_x1 = geom_x - view->buffer_offset_x; - int64_t buffer_y1 = geom_y - view->buffer_offset_y; -@@ -1673,6 +1682,32 @@ compositor_view_set_border_width(struct compositor_view *view, - update(&view->base); - } - -+void -+compositor_view_set_border_frame(struct compositor_view *view, bool frame) -+{ -+ pixman_box32_t old_extents; -+ -+ if (view->border.frame == frame) { -+ return; -+ } -+ -+ old_extents = view->extents; -+ view->border.frame = frame; -+ update_extents(view, view->base.buffer); -+ -+ /* damage the old border */ -+ if (view->visible) { -+ pixman_region32_t old_region; -+ -+ pixman_region32_init_with_extents(&old_region, &old_extents); -+ pixman_region32_union(&compositor.damage, &compositor.damage, -+ &old_region); -+ pixman_region32_fini(&old_region); -+ } -+ -+ update(&view->base); -+} -+ - void - compositor_view_set_border_color(struct compositor_view *view, - uint32_t outcolor, uint32_t incolor) -diff --git a/libswc/compositor.h b/libswc/compositor.h -index ddb4275..2de7845 100644 ---- a/libswc/compositor.h -+++ b/libswc/compositor.h -@@ -106,6 +106,9 @@ struct compositor_view { - uint32_t incolor; - - bool damaged_border2; -+ -+ /* border goes around the decor too */ -+ bool frame; - } border; - - struct { -@@ -156,6 +159,8 @@ void - compositor_view_set_border_color(struct compositor_view *view, - uint32_t outcolor, uint32_t incolor); - void -+compositor_view_set_border_frame(struct compositor_view *view, bool frame); -+void - compositor_view_set_border_width(struct compositor_view *view, - uint32_t outwidth, uint32_t inwidth); - void -diff --git a/libswc/swc.h b/libswc/swc.h -index 6477b27..bd23d6a 100644 ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -449,6 +449,27 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, - uint32_t inner_border_width, uint32_t outer_border_color, - uint32_t outer_border_width); - -+/** -+ * What the border goes around. -+ */ -+enum swc_border_placement { -+ /** -+ * The client area only. -+ */ -+ SWC_BORDER_CLIENT, -+ /** -+ * The client area and its decor. -+ */ -+ SWC_BORDER_FRAME, -+}; -+ -+/** -+ * Choose what the border goes around. The default is SWC_BORDER_CLIENT. -+ */ -+void -+swc_window_set_border_placement(struct swc_window *window, -+ enum swc_border_placement placement); -+ - /*window decor things*/ - - /** -diff --git a/libswc/window.c b/libswc/window.c -index 34bed2c..80c410e 100644 ---- a/libswc/window.c -+++ b/libswc/window.c -@@ -393,6 +393,14 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, - inner_border_width); - } - -+EXPORT void -+swc_window_set_border_placement(struct swc_window *window, -+ enum swc_border_placement placement) -+{ -+ compositor_view_set_border_frame(INTERNAL(window)->view, -+ placement == SWC_BORDER_FRAME); -+} -+ - EXPORT void - swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor) - { --- -2.56.1 - diff --git a/patches/borderSharedRoutine.patch b/patches/borderSharedRoutine.patch @@ -1,240 +0,0 @@ -From 4953c3c65838779ea8c9e62d0d50e503afaf608b Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:57:20 -0300 -Subject: [PATCH 16/19] compositor: one routine draws the border - ---- - libswc/compositor.c | 158 +++++++++++++++----------------------------- - 1 file changed, 53 insertions(+), 105 deletions(-) - -diff --git a/libswc/compositor.c b/libswc/compositor.c -index b7b9eed..f4c8373 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -321,17 +321,58 @@ view_buffer_is_opaque(struct compositor_view *view) - return view->buffer_opaque; - } - -+/* what the border goes around: the client, or with border.frame the decor too */ -+static struct swc_rectangle -+frame_of(const struct compositor_view *view) -+{ -+ return view->base.geometry; -+} -+ -+/* inner ring on the frame, outer ring on the inner, damaged parts only */ -+static void -+draw_border(struct wld_renderer *renderer, const struct compositor_view *view, -+ const pixman_region32_t *damage, const struct swc_rectangle *target) -+{ -+ struct swc_rectangle frame = frame_of(view); -+ int32_t in = (int32_t)view->border.inwidth; -+ int32_t all = in + (int32_t)view->border.outwidth; -+ pixman_region32_t frame_region, inner, outer; -+ -+ if (!all) { -+ return; -+ } -+ pixman_region32_init_rect(&frame_region, frame.x, frame.y, frame.width, -+ frame.height); -+ pixman_region32_init_rect(&inner, frame.x - in, frame.y - in, -+ frame.width + 2 * in, frame.height + 2 * in); -+ pixman_region32_init_rect(&outer, frame.x - all, frame.y - all, -+ frame.width + 2 * all, frame.height + 2 * all); -+ pixman_region32_subtract(&outer, &outer, &inner); -+ pixman_region32_subtract(&inner, &inner, &frame_region); -+ pixman_region32_intersect(&outer, &outer, (pixman_region32_t *)damage); -+ pixman_region32_intersect(&inner, &inner, (pixman_region32_t *)damage); -+ pixman_region32_translate(&outer, -target->x, -target->y); -+ pixman_region32_translate(&inner, -target->x, -target->y); -+ if (pixman_region32_not_empty(&outer)) { -+ wld_fill_region(renderer, view->border.outcolor, &outer); -+ } -+ if (pixman_region32_not_empty(&inner)) { -+ wld_fill_region(renderer, view->border.incolor, &inner); -+ } -+ pixman_region32_fini(&frame_region); -+ pixman_region32_fini(&inner); -+ pixman_region32_fini(&outer); -+} -+ - static void - repaint_view(struct target *target, struct compositor_view *view, - pixman_region32_t *damage) - { -- pixman_region32_t geom_region, buffer_region, border_region, view_damage, -- buffer_damage, border_damage; -+ pixman_region32_t buffer_region, view_damage, buffer_damage; - const struct swc_rectangle *geom = &view->base.geometry, - *target_geom = &target->view->geometry; - int32_t buf_x, buf_y; - uint32_t buf_w, buf_h; -- int64_t total_border; - - if (!view->base.buffer) { - return; -@@ -342,29 +383,17 @@ repaint_view(struct target *target, struct compositor_view *view, - buf_x = geom->x - view->buffer_offset_x; - buf_y = geom->y - view->buffer_offset_y; - -- total_border = -- (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; -- pixman_region32_init_rect(&geom_region, geom->x, geom->y, geom->width, -- geom->height); - if (view->window) { - pixman_region32_init_rect(&buffer_region, geom->x, geom->y, geom->width, - geom->height); - } else { - pixman_region32_init_rect(&buffer_region, buf_x, buf_y, buf_w, buf_h); - } -- pixman_region32_init_rect(&border_region, -- geom->x - (int32_t)total_border, -- geom->y - (int32_t)total_border, -- geom->width + (uint32_t)(2 * total_border), -- geom->height + (uint32_t)(2 * total_border)); -- pixman_region32_subtract(&border_region, &border_region, &geom_region); - pixman_region32_init_with_extents(&view_damage, &view->extents); - pixman_region32_init(&buffer_damage); -- pixman_region32_init(&border_damage); - - pixman_region32_intersect(&view_damage, &view_damage, damage); - pixman_region32_subtract(&view_damage, &view_damage, &view->clip); -- pixman_region32_intersect(&border_damage, &view_damage, &border_region); - pixman_region32_intersect(&buffer_damage, &view_damage, &buffer_region); - - if (pixman_region32_not_empty(&buffer_damage)) { -@@ -406,45 +435,10 @@ repaint_view(struct target *target, struct compositor_view *view, - } - } - -+ draw_border(swc.backend->renderer, view, &view_damage, target_geom); - pixman_region32_fini(&view_damage); - pixman_region32_fini(&buffer_damage); -- -- pixman_region32_t in_rect; -- pixman_region32_init_rect(&in_rect, -- geom->x - view->border.inwidth, -- geom->y - view->border.inwidth, -- geom->width + (2 * view->border.inwidth), -- geom->height + (2 * view->border.inwidth)); -- -- pixman_region32_t out_border; -- pixman_region32_init(&out_border); -- pixman_region32_subtract(&out_border, &border_damage, &in_rect); -- -- pixman_region32_t in_border; -- pixman_region32_init(&in_border); -- pixman_region32_subtract(&in_border, &in_rect, &geom_region); -- pixman_region32_intersect(&in_border, &in_border, &border_damage); -- -- pixman_region32_fini(&geom_region); - pixman_region32_fini(&buffer_region); -- pixman_region32_fini(&border_region); -- -- /* Draw border */ -- if (view->border.outwidth > 0 && pixman_region32_not_empty(&out_border)) { -- pixman_region32_translate(&out_border, -target_geom->x, -- -target_geom->y); -- wld_fill_region(swc.backend->renderer, view->border.outcolor, &out_border); -- } -- -- if (view->border.inwidth > 0 && pixman_region32_not_empty(&in_border)) { -- pixman_region32_translate(&in_border, -target_geom->x, -target_geom->y); -- wld_fill_region(swc.backend->renderer, view->border.incolor, &in_border); -- } -- -- pixman_region32_fini(&border_damage); -- pixman_region32_fini(&in_rect); -- pixman_region32_fini(&out_border); -- pixman_region32_fini(&in_border); - - if ((view->decor.top || view->decor.right || view->decor.bottom || - view->decor.left)) { -@@ -646,14 +640,14 @@ update_extents(struct compositor_view *view, const struct wld_buffer *buffer) - int64_t geom_w = view->base.geometry.width; - int64_t geom_h = view->base.geometry.height; - -- int64_t border_x1 = geom_x - total_border; -- int64_t border_y1 = geom_y - total_border; -- int64_t border_x2 = geom_x + geom_w + total_border; -- int64_t border_y2 = geom_y + geom_h + total_border; - int64_t decor_x1 = geom_x - (int64_t)view->decor.left; - int64_t decor_y1 = geom_y - (int64_t)view->decor.top; - int64_t decor_x2 = geom_x + geom_w + (int64_t)view->decor.right; - int64_t decor_y2 = geom_y + geom_h + (int64_t)view->decor.bottom; -+ int64_t border_x1 = geom_x - total_border; -+ int64_t border_y1 = geom_y - total_border; -+ int64_t border_x2 = geom_x + geom_w + total_border; -+ int64_t border_y2 = geom_y + geom_h + total_border; - - int64_t buffer_x1 = geom_x - view->buffer_offset_x; - int64_t buffer_y1 = geom_y - view->buffer_offset_y; -@@ -2189,61 +2183,15 @@ compositor_render_to_shm(struct screen *screen) - pixman_region32_fini(&source_region); - } - -- if ((view->border.outwidth > 0 || view->border.inwidth > 0) && -- view->base.buffer) { -- pixman_region32_t view_region, view_damage, border_damage; -- const struct swc_rectangle *geom = &view->base.geometry; -- const struct swc_rectangle *target_geom = &screen->base.geometry; -+ if (view->base.buffer) { -+ pixman_region32_t view_damage; - -- pixman_region32_init_rect(&view_region, geom->x, geom->y, -- geom->width, geom->height); - pixman_region32_init_with_extents(&view_damage, &view->extents); -- pixman_region32_init(&border_damage); -- - pixman_region32_intersect(&view_damage, &view_damage, &damage); - pixman_region32_subtract(&view_damage, &view_damage, &view->clip); -- pixman_region32_subtract(&border_damage, &view_damage, -- &view_region); -- -- pixman_region32_t in_rect; -- pixman_region32_init_rect(&in_rect, -- geom->x - view->border.inwidth, -- geom->y - view->border.inwidth, -- geom->width + (2 * view->border.inwidth), -- geom->height + -- (2 * view->border.inwidth)); -- -- pixman_region32_t out_border; -- pixman_region32_init(&out_border); -- pixman_region32_subtract(&out_border, &border_damage, &in_rect); -- -- pixman_region32_t in_border; -- pixman_region32_init(&in_border); -- pixman_region32_subtract(&in_border, &in_rect, &view_region); -- pixman_region32_intersect(&in_border, &in_border, &border_damage); -- -- if (view->border.outwidth > 0 && -- pixman_region32_not_empty(&out_border)) { -- pixman_region32_translate(&out_border, -target_geom->x, -- -target_geom->y); -- wld_fill_region(swc.shm->renderer, view->border.outcolor, -- &out_border); -- } -- -- if (view->border.inwidth > 0 && -- pixman_region32_not_empty(&in_border)) { -- pixman_region32_translate(&in_border, -target_geom->x, -- -target_geom->y); -- wld_fill_region(swc.shm->renderer, view->border.incolor, -- &in_border); -- } -- -- pixman_region32_fini(&border_damage); -- pixman_region32_fini(&view_region); -+ draw_border(swc.shm->renderer, view, &view_damage, -+ &screen->base.geometry); - pixman_region32_fini(&view_damage); -- pixman_region32_fini(&in_rect); -- pixman_region32_fini(&out_border); -- pixman_region32_fini(&in_border); - } - - if (view->decor.top || view->decor.right || view->decor.bottom || --- -2.56.1 - diff --git a/patches/compositor.patch b/patches/compositor.patch @@ -0,0 +1,3448 @@ +Subject: backends, z order, backdrops + +diff --git a/libswc/backend.h b/libswc/backend.h +index 65432bc..8d0e7bf 100644 +--- a/libswc/backend.h ++++ b/libswc/backend.h +@@ -1,17 +1,33 @@ + #ifndef SWC_BACKEND_H + #define SWC_BACKEND_H + ++#include <stdbool.h> + #include <stdint.h> + ++struct screen; ++struct wl_list; ++struct wld_buffer; + struct wld_context; + struct wld_renderer; ++struct wld_surface; + +-/* rendering state shared by the drm and fb backends */ ++/* rendering state shared by every backend */ + struct swc_backend { + struct wld_context *context; + struct wld_renderer *renderer; + uint32_t cursor_width; + uint32_t cursor_height; ++ ++ /* backend feeds the seat, no input devices */ ++ bool virtual_input; ++ ++ /* optional, unset means the built-in video backend */ ++ bool (*create_screens)(struct wl_list *screens); ++ struct wld_surface *(*create_target)(struct screen *screen); ++ void (*set_cursor)(struct wld_buffer *buffer, int32_t hotspot_x, ++ int32_t hotspot_y); ++ void (*start)(void); ++ void (*finalize)(void); + }; + + #endif +diff --git a/libswc/compositor.c b/libswc/compositor.c +index bb762f6..7056ed6 100644 +--- a/libswc/compositor.c ++++ b/libswc/compositor.c +@@ -107,6 +107,8 @@ handle_button(struct pointer_handler *handler, uint32_t time, + struct button *button, uint32_t state); + static void + perform_update(void *data); ++static void ++schedule_updates(uint32_t screens); + + static struct pointer_handler pointer_handler = { + .motion = handle_motion, +@@ -126,6 +128,8 @@ static struct { + uint32_t scheduled_updates; + + bool updating; ++ /* The window whose decoration the pointer is over, if one. */ ++ struct window *chrome_window; + struct wl_global *global; + bool initialized; + +@@ -208,25 +212,33 @@ target_swap_buffers(struct target *target) + return view_attach(target->view, target->next_buffer); + } + ++static struct wld_surface * ++target_create_surface(struct screen *screen) ++{ ++ struct swc_rectangle *geom = &screen->base.geometry; ++ ++ if (swc.backend->create_target) { ++ return swc.backend->create_target(screen); ++ } ++ ++ return wld_create_surface(swc.backend->context, geom->width, geom->height, ++ WLD_FORMAT_XRGB8888, ++#ifdef ENABLE_DRM ++ swc.video == SWC_VIDEO_DRM ? WLD_DRM_FLAG_SCANOUT : ++#endif ++ WLD_FLAG_MAP); ++} ++ + static struct target * + target_new(struct screen *screen) + { + struct target *target; +- struct swc_rectangle *geom = &screen->base.geometry; + + if (!(target = malloc(sizeof(*target)))) { + goto error0; + } + +- target->surface = +- wld_create_surface(swc.backend->context, geom->width, geom->height, +- WLD_FORMAT_XRGB8888, +-#ifdef ENABLE_DRM +- WLD_DRM_FLAG_SCANOUT +-#else +- WLD_FLAG_MAP +-#endif +- ); ++ target->surface = target_create_surface(screen); + + if (!target->surface) { + goto error1; +@@ -311,50 +323,177 @@ view_buffer_is_opaque(struct compositor_view *view) + return view->buffer_opaque; + } + ++/* what the border goes around: the client, or with border.frame the decor too */ ++static struct swc_rectangle ++frame_of(const struct compositor_view *view) ++{ ++ const struct swc_rectangle *geom = &view->base.geometry; ++ struct swc_rectangle frame = *geom; ++ ++ if (view->border.frame) { ++ frame.x -= (int32_t)view->decor.left; ++ frame.y -= (int32_t)view->decor.top; ++ frame.width += view->decor.left + view->decor.right; ++ frame.height += view->decor.top + view->decor.bottom; ++ } ++ return frame; ++} ++ ++/* inner ring on the frame, outer ring on the inner, damaged parts only */ ++static void ++draw_border(struct wld_renderer *renderer, const struct compositor_view *view, ++ const pixman_region32_t *damage, const struct swc_rectangle *target) ++{ ++ struct swc_rectangle frame = frame_of(view); ++ int32_t in = (int32_t)view->border.inwidth; ++ int32_t all = in + (int32_t)view->border.outwidth; ++ pixman_region32_t frame_region, inner, outer; ++ ++ if (!all) { ++ return; ++ } ++ pixman_region32_init_rect(&frame_region, frame.x, frame.y, frame.width, ++ frame.height); ++ pixman_region32_init_rect(&inner, frame.x - in, frame.y - in, ++ frame.width + 2 * in, frame.height + 2 * in); ++ pixman_region32_init_rect(&outer, frame.x - all, frame.y - all, ++ frame.width + 2 * all, frame.height + 2 * all); ++ pixman_region32_subtract(&outer, &outer, &inner); ++ pixman_region32_subtract(&inner, &inner, &frame_region); ++ pixman_region32_intersect(&outer, &outer, (pixman_region32_t *)damage); ++ pixman_region32_intersect(&inner, &inner, (pixman_region32_t *)damage); ++ pixman_region32_translate(&outer, -target->x, -target->y); ++ pixman_region32_translate(&inner, -target->x, -target->y); ++ if (pixman_region32_not_empty(&outer)) { ++ wld_fill_region(renderer, view->border.outcolor, &outer); ++ } ++ if (pixman_region32_not_empty(&inner)) { ++ wld_fill_region(renderer, view->border.incolor, &inner); ++ } ++ pixman_region32_fini(&frame_region); ++ pixman_region32_fini(&inner); ++ pixman_region32_fini(&outer); ++} ++ ++/* a backdrop's area: the view's extents and the margin around them */ ++static pixman_box32_t ++backdrop_box(const struct compositor_view *view) ++{ ++ int32_t m = (int32_t)view->backdrop.margin; ++ ++ return (pixman_box32_t){ view->extents.x1 - m, view->extents.y1 - m, view->extents.x2 + m, ++ view->extents.y2 + m }; ++} ++ ++/* what lies behind a view with a backdrop is read through its whole area, ++ * so a change anywhere in it repaints all of it, and that may reach another ++ * backdrop's area in turn */ ++static void ++backdrop_damage(void) ++{ ++ struct compositor_view *view; ++ bool grew = true; ++ ++ while (grew) { ++ grew = false; ++ wl_list_for_each (view, &compositor.views, link) { ++ pixman_box32_t box = backdrop_box(view); ++ ++ if (!view->visible || !view->backdrop.hook) ++ continue; ++ if (pixman_region32_contains_rectangle(&compositor.damage, &box) == PIXMAN_REGION_PART) { ++ pixman_region32_union_rect(&compositor.damage, &compositor.damage, box.x1, box.y1, ++ (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1)); ++ grew = true; ++ } ++ } ++ } ++} ++ ++static void ++backdrop_call(struct target *target, struct compositor_view *view, pixman_region32_t *damage) ++{ ++ const struct swc_rectangle *tg = &target->view->geometry; ++ pixman_box32_t box = backdrop_box(view), *boxes; ++ pixman_region32_t area; ++ struct swc_rectangle rects[64]; ++ struct swc_backdrop b = { 0 }; ++ int n; ++ ++ pixman_region32_init_rect(&area, box.x1, box.y1, (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1)); ++ pixman_region32_intersect_rect(&area, &area, tg->x, tg->y, tg->width, tg->height); ++ box = *pixman_region32_extents(&area); ++ pixman_region32_intersect(&area, &area, damage); ++ pixman_region32_subtract(&area, &area, &view->clip); ++ boxes = pixman_region32_rectangles(&area, &n); ++ if (n && (b.pixels = wld_target_map(swc.backend->renderer, &b.pitch, &b.format))) { ++ b.area = (struct swc_rectangle){ box.x1 - tg->x, box.y1 - tg->y, (uint32_t)(box.x2 - box.x1), ++ (uint32_t)(box.y2 - box.y1) }; ++ /* more pieces than fit are given as the one box around them */ ++ if (n > 64) { ++ box = *pixman_region32_extents(&area); ++ boxes = &box; ++ n = 1; ++ } ++ for (int i = 0; i < n; i++) ++ rects[i] = (struct swc_rectangle){ boxes[i].x1 - tg->x, boxes[i].y1 - tg->y, ++ (uint32_t)(boxes[i].x2 - boxes[i].x1), ++ (uint32_t)(boxes[i].y2 - boxes[i].y1) }; ++ b.rects = rects; ++ b.nrects = n; ++ view->backdrop.hook(view->backdrop.data, &b); ++ wld_target_unmap(swc.backend->renderer); ++ } ++ pixman_region32_fini(&area); ++} ++ ++void ++compositor_view_set_backdrop(struct compositor_view *view, uint32_t margin, ++ void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data) ++{ ++ pixman_box32_t box; ++ ++ view->backdrop.margin = margin; ++ view->backdrop.hook = hook; ++ view->backdrop.data = data; ++ box = backdrop_box(view); ++ pixman_region32_union_rect(&compositor.damage, &compositor.damage, box.x1, box.y1, ++ (uint32_t)(box.x2 - box.x1), (uint32_t)(box.y2 - box.y1)); ++ schedule_updates(view->base.screens); ++} ++ + static void + repaint_view(struct target *target, struct compositor_view *view, + pixman_region32_t *damage) + { +- pixman_region32_t geom_region, buffer_region, border_region, view_damage, +- buffer_damage, border_damage; ++ pixman_region32_t buffer_region, view_damage, buffer_damage; + const struct swc_rectangle *geom = &view->base.geometry, + *target_geom = &target->view->geometry; + int32_t buf_x, buf_y; + uint32_t buf_w, buf_h; +- int64_t total_border; + + if (!view->base.buffer) { + return; + } ++ if (view->backdrop.hook) ++ backdrop_call(target, view, damage); + + buf_w = view->base.buffer->width; + buf_h = view->base.buffer->height; + buf_x = geom->x - view->buffer_offset_x; + buf_y = geom->y - view->buffer_offset_y; + +- total_border = +- (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; +- pixman_region32_init_rect(&geom_region, geom->x, geom->y, geom->width, +- geom->height); + if (view->window) { + pixman_region32_init_rect(&buffer_region, geom->x, geom->y, geom->width, + geom->height); + } else { + pixman_region32_init_rect(&buffer_region, buf_x, buf_y, buf_w, buf_h); + } +- pixman_region32_init_rect(&border_region, +- geom->x - (int32_t)total_border, +- geom->y - (int32_t)total_border, +- geom->width + (uint32_t)(2 * total_border), +- geom->height + (uint32_t)(2 * total_border)); +- pixman_region32_subtract(&border_region, &border_region, &geom_region); + pixman_region32_init_with_extents(&view_damage, &view->extents); + pixman_region32_init(&buffer_damage); +- pixman_region32_init(&border_damage); + + pixman_region32_intersect(&view_damage, &view_damage, damage); + pixman_region32_subtract(&view_damage, &view_damage, &view->clip); +- pixman_region32_intersect(&border_damage, &view_damage, &border_region); + pixman_region32_intersect(&buffer_damage, &view_damage, &buffer_region); + + if (pixman_region32_not_empty(&buffer_damage)) { +@@ -396,45 +535,10 @@ repaint_view(struct target *target, struct compositor_view *view, + } + } + ++ draw_border(swc.backend->renderer, view, &view_damage, target_geom); + pixman_region32_fini(&view_damage); + pixman_region32_fini(&buffer_damage); +- +- pixman_region32_t in_rect; +- pixman_region32_init_rect(&in_rect, +- geom->x - view->border.inwidth, +- geom->y - view->border.inwidth, +- geom->width + (2 * view->border.inwidth), +- geom->height + (2 * view->border.inwidth)); +- +- pixman_region32_t out_border; +- pixman_region32_init(&out_border); +- pixman_region32_subtract(&out_border, &border_damage, &in_rect); +- +- pixman_region32_t in_border; +- pixman_region32_init(&in_border); +- pixman_region32_subtract(&in_border, &in_rect, &geom_region); +- pixman_region32_intersect(&in_border, &in_border, &border_damage); +- +- pixman_region32_fini(&geom_region); + pixman_region32_fini(&buffer_region); +- pixman_region32_fini(&border_region); +- +- /* Draw border */ +- if (view->border.outwidth > 0 && pixman_region32_not_empty(&out_border)) { +- pixman_region32_translate(&out_border, -target_geom->x, +- -target_geom->y); +- wld_fill_region(swc.backend->renderer, view->border.outcolor, &out_border); +- } +- +- if (view->border.inwidth > 0 && pixman_region32_not_empty(&in_border)) { +- pixman_region32_translate(&in_border, -target_geom->x, -target_geom->y); +- wld_fill_region(swc.backend->renderer, view->border.incolor, &in_border); +- } +- +- pixman_region32_fini(&border_damage); +- pixman_region32_fini(&in_rect); +- pixman_region32_fini(&out_border); +- pixman_region32_fini(&in_border); + + if ((view->decor.top || view->decor.right || view->decor.bottom || + view->decor.left)) { +@@ -627,7 +731,7 @@ damage_view(struct compositor_view *view) + } + + static void +-update_extents(struct compositor_view *view) ++update_extents(struct compositor_view *view, const struct wld_buffer *buffer) + { + int64_t total_border = + (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; +@@ -636,23 +740,20 @@ update_extents(struct compositor_view *view) + int64_t geom_w = view->base.geometry.width; + int64_t geom_h = view->base.geometry.height; + +- int64_t border_x1 = geom_x - total_border; +- int64_t border_y1 = geom_y - total_border; +- int64_t border_x2 = geom_x + geom_w + total_border; +- int64_t border_y2 = geom_y + geom_h + total_border; + int64_t decor_x1 = geom_x - (int64_t)view->decor.left; + int64_t decor_y1 = geom_y - (int64_t)view->decor.top; + int64_t decor_x2 = geom_x + geom_w + (int64_t)view->decor.right; + int64_t decor_y2 = geom_y + geom_h + (int64_t)view->decor.bottom; ++ int64_t border_x1 = (view->border.frame ? decor_x1 : geom_x) - total_border; ++ int64_t border_y1 = (view->border.frame ? decor_y1 : geom_y) - total_border; ++ int64_t border_x2 = (view->border.frame ? decor_x2 : geom_x + geom_w) + total_border; ++ int64_t border_y2 = (view->border.frame ? decor_y2 : geom_y + geom_h) + total_border; + + int64_t buffer_x1 = geom_x - view->buffer_offset_x; + int64_t buffer_y1 = geom_y - view->buffer_offset_y; +- int64_t buffer_x2 = +- buffer_x1 + +- (view->base.buffer ? view->base.buffer->width : (uint32_t)geom_w); ++ int64_t buffer_x2 = buffer_x1 + (buffer ? buffer->width : (uint32_t)geom_w); + int64_t buffer_y2 = +- buffer_y1 + +- (view->base.buffer ? view->base.buffer->height : (uint32_t)geom_h); ++ buffer_y1 + (buffer ? buffer->height : (uint32_t)geom_h); + + view->extents.x1 = clamp_i32(MIN(MIN(border_x1, decor_x1), buffer_x1)); + view->extents.y1 = clamp_i32(MIN(MIN(border_y1, decor_y1), buffer_y1)); +@@ -1073,7 +1174,8 @@ attach(struct view *base, struct wld_buffer *buffer) + if (view_set_size(&view->base, new_width, new_height)) { + /* The view was resized. */ + old_extents = view->extents; +- update_extents(view); ++ /* base.buffer is still the old buffer here, size from the new one */ ++ update_extents(view, buffer); + + if (view->visible) { + /* Damage the region that was newly uncovered or covered. */ +@@ -1099,7 +1201,19 @@ attach(struct view *base, struct wld_buffer *buffer) + static bool + move(struct view *base, int32_t x, int32_t y) + { +- struct compositor_view *view = (void *)base; ++ struct compositor_view *view = (void *)base, *other; ++ int32_t dx = x - base->geometry.x, dy = y - base->geometry.y; ++ ++ /* attached transients follow the parent */ ++ if (view->window && (dx || dy)) { ++ wl_list_for_each(other, &compositor.views, link) ++ { ++ if (other->parent == view && other->attached && other->window) { ++ view_move(&other->base, other->base.geometry.x + dx, ++ other->base.geometry.y + dy); ++ } ++ } ++ } + + if (view->visible) { + damage_below_view(view); +@@ -1107,7 +1221,7 @@ move(struct view *base, int32_t x, int32_t y) + } + + if (view_set_position(&view->base, x, y)) { +- update_extents(view); ++ update_extents(view, view->base.buffer); + + if (view->visible) { + /* Assume worst-case no clipping until we draw the next frame (in +@@ -1142,12 +1256,12 @@ restack_view_for_layer(struct compositor_view *view, bool raise) + continue; + } + +- if (other->stack_layer > view->stack_layer) { ++ if (other->z > view->z) { + insert_after = &other->link; + continue; + } + +- if (other->stack_layer == view->stack_layer) { ++ if (other->z == view->z) { + found_same = true; + if (raise) { + insert_after = other->link.prev; +@@ -1176,40 +1290,87 @@ restack_view_for_layer(struct compositor_view *view, bool raise) + wl_list_insert(insert_after, &view->link); + } + +-static struct compositor_view * +-view_at(int32_t x, int32_t y) ++/* whether the point is on what the view shows and takes input there */ ++static bool ++view_content_at(struct compositor_view *view, int32_t x, int32_t y) + { +- struct compositor_view *view; +- struct swc_rectangle *geom; ++ struct swc_rectangle *geom = &view->base.geometry; + struct swc_rectangle buffer_geom; + ++ if (view->window) { ++ if (!rectangle_contains_point(geom, x, y)) { ++ return false; ++ } ++ } else if (view->base.buffer) { ++ buffer_geom.x = geom->x - view->buffer_offset_x; ++ buffer_geom.y = geom->y - view->buffer_offset_y; ++ buffer_geom.width = view->base.buffer->width; ++ buffer_geom.height = view->base.buffer->height; ++ if (!rectangle_contains_point(&buffer_geom, x, y)) { ++ return false; ++ } ++ } else if (!rectangle_contains_point(geom, x, y)) { ++ return false; ++ } ++ ++ return pixman_region32_contains_point(&view->surface->state.input, ++ x - geom->x + view->buffer_offset_x, ++ y - geom->y + view->buffer_offset_y, ++ NULL); ++} ++ ++struct compositor_view * ++compositor_view_at(int32_t x, int32_t y) ++{ ++ struct compositor_view *view; ++ + wl_list_for_each(view, &compositor.views, link) + { +- if (!view->visible) { ++ if (!view->visible || view->no_input) { + continue; + } ++ if (view_content_at(view, x, y)) { ++ return view; ++ } ++ } + +- geom = &view->base.geometry; +- if (view->window) { +- if (!rectangle_contains_point(geom, x, y)) { +- continue; +- } +- } else if (view->base.buffer) { +- buffer_geom.x = geom->x - view->buffer_offset_x; +- buffer_geom.y = geom->y - view->buffer_offset_y; +- buffer_geom.width = view->base.buffer->width; +- buffer_geom.height = view->base.buffer->height; +- if (!rectangle_contains_point(&buffer_geom, x, y)) { +- continue; +- } +- } else if (!rectangle_contains_point(geom, x, y)) { ++ return NULL; ++} ++ ++/* whether the point is on the window's decoration or its border, which the ++ * pointer is not over for the client: the surface is not under it */ ++static bool ++view_chrome_at(struct compositor_view *view, int32_t x, int32_t y) ++{ ++ struct swc_rectangle *geom = &view->base.geometry; ++ int64_t border = (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; ++ ++ if (!view->window) { ++ return false; ++ } ++ ++ return x >= geom->x - (int64_t)view->decor.left - border && ++ x < geom->x + (int64_t)geom->width + (int64_t)view->decor.right + border && ++ y >= geom->y - (int64_t)view->decor.top - border && ++ y < geom->y + (int64_t)geom->height + (int64_t)view->decor.bottom + border; ++} ++ ++/* the window whose decoration or border is the first thing under the point, ++ * in stacking order, NULL when it is anything else or nothing */ ++static struct compositor_view * ++compositor_chrome_at(int32_t x, int32_t y) ++{ ++ struct compositor_view *view; ++ ++ wl_list_for_each(view, &compositor.views, link) ++ { ++ if (!view->visible || view->no_input) { + continue; + } +- +- if (pixman_region32_contains_point(&view->surface->state.input, +- x - geom->x + view->buffer_offset_x, +- y - geom->y + view->buffer_offset_y, +- NULL)) { ++ if (view_content_at(view, x, y)) { ++ return NULL; ++ } ++ if (view_chrome_at(view, x, y)) { + return view; + } + } +@@ -1226,6 +1387,31 @@ window_view(struct compositor_view *view) + return (view && view->window) ? view : NULL; + } + ++/* transients go above their parent, recursively */ ++static void ++raise_transients(struct compositor_view *view) ++{ ++ struct compositor_view *other, *children[32]; ++ unsigned n = 0; ++ ++ wl_list_for_each(other, &compositor.views, link) ++ { ++ if (other->parent == view && other->window && other->visible && ++ n < sizeof children / sizeof children[0]) { ++ children[n++] = other; ++ } ++ } ++ for (unsigned i = 0; i < n; i++) { ++ /* a transient stays in its parent's layer, or it would be found ++ * among views of another */ ++ children[i]->z = view->z; ++ wl_list_remove(&children[i]->link); ++ wl_list_insert(view->link.prev, &children[i]->link); ++ damage_view(children[i]); ++ raise_transients(children[i]); ++ } ++} ++ + void + raise_window(struct compositor_view *view) + { +@@ -1250,13 +1436,13 @@ raise_window(struct compositor_view *view) + continue; + } + +- if (other->stack_layer > STACK_LAYER_NORMAL || ++ if (other->z > view->z || + other->always_top) { + insert_after = &other->link; + continue; + } + +- if (other->stack_layer < STACK_LAYER_NORMAL) { ++ if (other->z < view->z) { + break; + } + +@@ -1268,6 +1454,7 @@ raise_window(struct compositor_view *view) + } + + if (view == top_window) { ++ raise_transients(view); + return; + } + +@@ -1282,22 +1469,23 @@ raise_window(struct compositor_view *view) + span_u32(view->extents.x1, view->extents.x2), + span_u32(view->extents.y1, view->extents.y2)); + schedule_updates(screens); ++ raise_transients(view); + } + + void + raise_window_top(struct compositor_view *view) + { +- view->stack_layer = STACK_LAYER_OVERLAY; ++ view->z = STACK_LAYER_OVERLAY; + restack_view_for_layer(view, true); + damage_view(view); + schedule_updates(view->base.screens); + } + + void +-compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, ++compositor_view_set_z(struct compositor_view *view, int32_t layer, + bool raise) + { +- if (view->stack_layer == layer) { ++ if (view->z == layer) { + if (raise) { + restack_view_for_layer(view, true); + damage_view(view); +@@ -1307,7 +1495,7 @@ compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, + } + + damage_view(view); +- view->stack_layer = layer; ++ view->z = layer; + restack_view_for_layer(view, raise); + damage_view(view); + schedule_updates(view->base.screens); +@@ -1316,7 +1504,7 @@ compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, + EXPORT struct swc_window * + swc_window_at(int32_t x, int32_t y) + { +- struct compositor_view *view = window_view(view_at(x, y)); ++ struct compositor_view *view = window_view(compositor_view_at(x, y)); + + return view ? &view->window->base : NULL; + } +@@ -1443,7 +1631,8 @@ compositor_create_view(struct surface *surface) + view->buffer_offset_y = 0; + view->visible = false; + view->always_top = false; +- view->stack_layer = STACK_LAYER_NORMAL; ++ view->no_input = false; ++ view->z = STACK_LAYER_NORMAL; + view->extents.x1 = 0; + view->extents.y1 = 0; + view->extents.x2 = 0; +@@ -1612,7 +1801,7 @@ compositor_view_set_border_width(struct compositor_view *view, + + /* XXX: Damage above surface for transparent surfaces? */ + +- update_extents(view); ++ update_extents(view, view->base.buffer); + + /* Damage old extents to prevent leftover border artifacts */ + if (view->visible && +@@ -1634,6 +1823,32 @@ compositor_view_set_border_width(struct compositor_view *view, + update(&view->base); + } + ++void ++compositor_view_set_border_frame(struct compositor_view *view, bool frame) ++{ ++ pixman_box32_t old_extents; ++ ++ if (view->border.frame == frame) { ++ return; ++ } ++ ++ old_extents = view->extents; ++ view->border.frame = frame; ++ update_extents(view, view->base.buffer); ++ ++ /* damage the old border */ ++ if (view->visible) { ++ pixman_region32_t old_region; ++ ++ pixman_region32_init_with_extents(&old_region, &old_extents); ++ pixman_region32_union(&compositor.damage, &compositor.damage, ++ &old_region); ++ pixman_region32_fini(&old_region); ++ } ++ ++ update(&view->base); ++} ++ + void + compositor_view_set_border_color(struct compositor_view *view, + uint32_t outcolor, uint32_t incolor) +@@ -1664,7 +1879,7 @@ compositor_view_set_decor(struct compositor_view *view, + } + + decor_view_set(view, decor); +- update_extents(view); ++ update_extents(view, view->base.buffer); + update(&view->base); + } + +@@ -1778,6 +1993,7 @@ update_screen(struct screen *screen) + return; + } + ++ backdrop_damage(); + pixman_region32_init(&damage); + pixman_region32_intersect_rect(&damage, &compositor.damage, geom->x, + geom->y, geom->width, geom->height); +@@ -1866,10 +2082,23 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, + return false; + } + +- struct compositor_view *view = view_at(x, y); ++ pointer_motion_notify(x, y); ++ ++ struct compositor_view *chrome = compositor_chrome_at(x, y); ++ struct compositor_view *view = chrome ? NULL : compositor_view_at(x, y); + + pointer_set_focus(swc.seat->pointer, view); + ++ /* The pointer over a window's decoration or border is over the window as ++ * much as over its surface: the window's handler is told as when the ++ * surface is entered, once until the pointer is anywhere else */ ++ if (chrome && chrome->window != compositor.chrome_window) { ++ compositor.chrome_window = chrome->window; ++ window_pointer_entered(chrome->window); ++ } else if (!chrome) { ++ compositor.chrome_window = NULL; ++ } ++ + return false; + } + +@@ -1887,7 +2116,7 @@ handle_button(struct pointer_handler *handler, uint32_t time, + + int32_t x = wl_fixed_to_int(swc.seat->pointer->x); + int32_t y = wl_fixed_to_int(swc.seat->pointer->y); +- struct compositor_view *view = view_at(x, y); ++ struct compositor_view *view = compositor_view_at(x, y); + + pointer_set_focus(swc.seat->pointer, view); + raise_window(view); +@@ -2046,6 +2275,31 @@ compositor_get_buffer(struct screen *screen) + return target->current_buffer; + } + ++void ++compositor_screen_resize(struct screen *screen) ++{ ++ struct target *target = target_get(screen); ++ struct wld_surface *surface; ++ ++ if (!target) { ++ return; ++ } ++ ++ if (!(surface = target_create_surface(screen))) { ++ ERROR("Could not resize the target of screen %u\n", screen->id); ++ return; ++ } ++ ++ /* a flip in flight ends on the old surface, forget its buffers */ ++ wld_destroy_surface(target->surface); ++ target->surface = surface; ++ target->current_buffer = NULL; ++ target->next_buffer = NULL; ++ compositor.pending_flips &= ~target->mask; ++ ++ compositor_damage_all(); ++} ++ + struct wld_buffer * + compositor_render_to_shm(struct screen *screen) + { +@@ -2119,61 +2373,15 @@ compositor_render_to_shm(struct screen *screen) + pixman_region32_fini(&source_region); + } + +- if ((view->border.outwidth > 0 || view->border.inwidth > 0) && +- view->base.buffer) { +- pixman_region32_t view_region, view_damage, border_damage; +- const struct swc_rectangle *geom = &view->base.geometry; +- const struct swc_rectangle *target_geom = &screen->base.geometry; ++ if (view->base.buffer) { ++ pixman_region32_t view_damage; + +- pixman_region32_init_rect(&view_region, geom->x, geom->y, +- geom->width, geom->height); + pixman_region32_init_with_extents(&view_damage, &view->extents); +- pixman_region32_init(&border_damage); +- + pixman_region32_intersect(&view_damage, &view_damage, &damage); + pixman_region32_subtract(&view_damage, &view_damage, &view->clip); +- pixman_region32_subtract(&border_damage, &view_damage, +- &view_region); +- +- pixman_region32_t in_rect; +- pixman_region32_init_rect(&in_rect, +- geom->x - view->border.inwidth, +- geom->y - view->border.inwidth, +- geom->width + (2 * view->border.inwidth), +- geom->height + +- (2 * view->border.inwidth)); +- +- pixman_region32_t out_border; +- pixman_region32_init(&out_border); +- pixman_region32_subtract(&out_border, &border_damage, &in_rect); +- +- pixman_region32_t in_border; +- pixman_region32_init(&in_border); +- pixman_region32_subtract(&in_border, &in_rect, &view_region); +- pixman_region32_intersect(&in_border, &in_border, &border_damage); +- +- if (view->border.outwidth > 0 && +- pixman_region32_not_empty(&out_border)) { +- pixman_region32_translate(&out_border, -target_geom->x, +- -target_geom->y); +- wld_fill_region(swc.shm->renderer, view->border.outcolor, +- &out_border); +- } +- +- if (view->border.inwidth > 0 && +- pixman_region32_not_empty(&in_border)) { +- pixman_region32_translate(&in_border, -target_geom->x, +- -target_geom->y); +- wld_fill_region(swc.shm->renderer, view->border.incolor, +- &in_border); +- } +- +- pixman_region32_fini(&border_damage); +- pixman_region32_fini(&view_region); ++ draw_border(swc.shm->renderer, view, &view_damage, ++ &screen->base.geometry); + pixman_region32_fini(&view_damage); +- pixman_region32_fini(&in_rect); +- pixman_region32_fini(&out_border); +- pixman_region32_fini(&in_border); + } + + if (view->decor.top || view->decor.right || view->decor.bottom || +diff --git a/libswc/compositor.h b/libswc/compositor.h +index 678c362..2442a8c 100644 +--- a/libswc/compositor.h ++++ b/libswc/compositor.h +@@ -24,6 +24,7 @@ + #ifndef SWC_COMPOSITOR_H + #define SWC_COMPOSITOR_H + ++#include "swc.h" + #include "view.h" + + #include <pixman.h> +@@ -71,6 +72,8 @@ struct compositor_view { + bool buffer_opaque; + struct window *window; + struct compositor_view *parent; ++ /* moves with its parent */ ++ bool attached; + int32_t buffer_offset_x; + int32_t buffer_offset_y; + +@@ -80,11 +83,21 @@ struct compositor_view { + /* Whether or not to make it always be on top of other windows */ + bool always_top; + ++ /* a drag icon, never under the pointer */ ++ bool no_input; ++ + /* Global stacking layer for this view */ +- uint32_t stack_layer; ++ /* depth: a higher one is drawn over a lower, see swc_window_set_z */ ++ int32_t z; + + /* The box that the surface covers (including it's border). */ + pixman_box32_t extents; ++ /* see swc_window_set_backdrop */ ++ struct { ++ uint32_t margin; ++ void (*hook)(void *data, const struct swc_backdrop *backdrop); ++ void *data; ++ } backdrop; + + /* The region that is covered by opaque regions of surfaces above this + * surface. */ +@@ -101,6 +114,9 @@ struct compositor_view { + uint32_t incolor; + + bool damaged_border2; ++ ++ /* border goes around the decor too */ ++ bool frame; + } border; + + struct { +@@ -138,6 +154,10 @@ void + compositor_view_restack(struct compositor_view *view, + struct compositor_view *sibling, bool above); + ++/* topmost view taking input at x, y, or NULL */ ++struct compositor_view * ++compositor_view_at(int32_t x, int32_t y); ++ + void + compositor_view_show(struct compositor_view *view); + void +@@ -147,6 +167,8 @@ void + compositor_view_set_border_color(struct compositor_view *view, + uint32_t outcolor, uint32_t incolor); + void ++compositor_view_set_border_frame(struct compositor_view *view, bool frame); ++void + compositor_view_set_border_width(struct compositor_view *view, + uint32_t outwidth, uint32_t inwidth); + void +@@ -162,6 +184,10 @@ compositor_view_damage_decor(struct compositor_view *view); + struct wld_buffer * + compositor_get_buffer(struct screen *screen); + ++/* recreate the render target after the screen geometry changed */ ++void ++compositor_screen_resize(struct screen *screen); ++ + /** + * render the compositor scene into a shm buffer + * caller must free with wld_buffer_unreference() +@@ -172,18 +198,21 @@ compositor_render_to_shm(struct screen *screen); + void + raise_window(struct compositor_view *view); + void ++compositor_view_set_backdrop(struct compositor_view *view, uint32_t margin, ++ void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data); ++void + raise_window_top(struct compositor_view *view); + + enum compositor_stack_layer { +- STACK_LAYER_BACKGROUND = 0, +- STACK_LAYER_BOTTOM = 1, +- STACK_LAYER_NORMAL = 2, +- STACK_LAYER_TOP = 3, +- STACK_LAYER_OVERLAY = 4, ++ STACK_LAYER_BACKGROUND = SWC_Z_BACKGROUND, ++ STACK_LAYER_BOTTOM = SWC_Z_BOTTOM, ++ STACK_LAYER_NORMAL = SWC_Z_WINDOW, ++ STACK_LAYER_TOP = SWC_Z_TOP, ++ STACK_LAYER_OVERLAY = SWC_Z_OVERLAY, + }; + + void +-compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, ++compositor_view_set_z(struct compositor_view *view, int32_t layer, + bool raise); + + #endif +diff --git a/libswc/decor.c b/libswc/decor.c +index 5e6eca8..c402933 100644 +--- a/libswc/decor.c ++++ b/libswc/decor.c +@@ -731,7 +731,14 @@ decor_view_set(struct compositor_view *view, const struct swc_decor *decor) + owned_string = strdup(text.string); + } + +- if (font_name) { ++ /* a font is opened once per name: a decor that changes only its ++ * colours or parts keeps the face it has */ ++ if (font_name && view->decor.font && streq(view->decor.font_name, font_name)) { ++ font = view->decor.font; ++ owned_font_name = view->decor.font_name; ++ view->decor.font = NULL; ++ view->decor.font_name = NULL; ++ } else if (font_name) { + owned_font_name = strdup(font_name); + if (owned_font_name && font_context) { + font = wld_font_open_name(font_context, font_name); +diff --git a/libswc/fb.c b/libswc/fb.c +index d3c332c..0755d94 100644 +--- a/libswc/fb.c ++++ b/libswc/fb.c +@@ -81,7 +81,7 @@ fb_create_screens(struct wl_list *screens) + if (!output) { + return false; + } +- screen = screen_new(output); ++ screen = screen_new(0, output, NULL); + if (!screen) { + output_destroy(output); + return false; +diff --git a/libswc/internal.h b/libswc/internal.h +index 54971c6..31fdd47 100644 +--- a/libswc/internal.h ++++ b/libswc/internal.h +@@ -32,6 +32,15 @@ enum { + SWC_EVENT_DEACTIVATED, + }; + ++/* the video backend swc was started with, chosen at run time among those ++ * that were built. a backend that supplies its own screens and target, such ++ * as nested, leaves it unset */ ++enum swc_video { ++ SWC_VIDEO_NONE, ++ SWC_VIDEO_DRM, ++ SWC_VIDEO_FB, ++}; ++ + struct swc { + struct wl_display *display; + struct wl_event_loop *event_loop; +@@ -48,6 +57,7 @@ struct swc { + struct swc_drm *const drm; + #endif + struct swc_backend *backend; ++ enum swc_video video; + struct wl_global *data_device_manager; + struct wl_global *kde_decoration_manager; + struct wl_global *layer_shell; +@@ -57,6 +67,8 @@ struct swc { + struct wl_global *select_manager; + struct wl_global *subcompositor; + struct wl_global *xdg_decoration_manager; ++ struct wl_global *xdg_dialog_manager; ++ struct wl_global *cursor_shape_manager; + struct wl_global *xdg_output_manager; + struct wl_global *xdg_shell; + +diff --git a/libswc/launch.c b/libswc/launch.c +index d9602f4..4144a08 100644 +--- a/libswc/launch.c ++++ b/libswc/launch.c +@@ -116,6 +116,11 @@ send_request(struct swc_launch_request *request, const void *data, size_t size, + {.iov_base = event, .iov_len = sizeof(*event)}, + }; + ++ /* no launcher socket: the zeroed fd would be stdin */ ++ if (!launch.source) { ++ return false; ++ } ++ + request->serial = ++launch.next_serial; + + if (send_fd(launch.socket, out_fd, request_iov, 1 + (size > 0)) == -1) { +diff --git a/libswc/meson.build b/libswc/meson.build +index 15fa57a..03b195a 100644 +--- a/libswc/meson.build ++++ b/libswc/meson.build +@@ -3,6 +3,7 @@ install_headers('swc.h') + libswc_src = files( + 'bindings.c', + 'compositor.c', ++ 'cursor_shape.c', + 'data.c', + 'data_device.c', + 'data_device_manager.c', +@@ -21,6 +22,7 @@ libswc_src = files( + 'region.c', + 'screen.c', + 'select.c', ++ 'selection.c', + 'snap.c', + 'shell.c', + 'shell_surface.c', +@@ -34,6 +36,7 @@ libswc_src = files( + 'wayland_buffer.c', + 'window.c', + 'xdg_decoration.c', ++ 'xdg_dialog.c', + 'xdg_output.c', + 'xdg_shell.c', + +@@ -43,12 +46,16 @@ libswc_src = files( + + xwayland.found() ? ['xserver.c', 'xwm.c'] : [], + +- video_backend == 'drm' ? ['dmabuf.c', 'drm.c', 'plane.c'] : [], +- framebuffer_backend == 'fbdev' ? ['fb.c', 'fb_fbdev.c'] : [], +- framebuffer_backend == 'wsdisplay' ? ['fb.c', 'fb_wsdisplay.c'] : [], ++ with_drm ? ['dmabuf.c', 'drm.c', 'plane.c'] : [], ++ with_fb and framebuffer_backend == 'fbdev' ? ['fb.c', 'fb_fbdev.c'] : [], ++ with_fb and framebuffer_backend == 'wsdisplay' ? ['fb.c', 'fb_wsdisplay.c'] : [], + ) + libswc_src += [cursor, launch_protocol] + ++if have_nested ++ libswc_src += files('nested.c') ++endif ++ + foreach proto, proto_src : protos + libswc_src += proto_src + endforeach +diff --git a/libswc/nested.c b/libswc/nested.c +new file mode 100644 +index 0000000..e44863a +--- /dev/null ++++ b/libswc/nested.c +@@ -0,0 +1,1135 @@ ++/* nested backend: swc as a client of another compositor. ++ * one xdg_toplevel is the screen, the parent seat feeds ours */ ++ ++#include "nested.h" ++#include "backend.h" ++#include "compositor.h" ++#include "internal.h" ++#include "keyboard.h" ++#include "launch/protocol.h" ++#include "output.h" ++#include "pointer.h" ++#include "screen.h" ++#include "seat.h" ++#include "swc.h" ++#include "util.h" ++#include "view.h" ++ ++#include <errno.h> ++#include <limits.h> ++#include <stdio.h> ++#include <stdlib.h> ++#include <string.h> ++#include <strings.h> ++#include <sys/mman.h> ++#include <unistd.h> ++#include <wayland-client.h> ++#include <wld/wayland.h> ++#include <wld/wld.h> ++#include <xkbcommon/xkbcommon.h> ++ ++#include "cursor-shape-v1-client-protocol.h" ++#include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h" ++#include "xdg-shell-client-protocol.h" ++ ++#define NESTED_DEFAULT_WIDTH 1280 ++#define NESTED_DEFAULT_HEIGHT 720 ++#define NESTED_CURSOR_SIZE 64 ++#define NESTED_OUTPUT_NAME "NESTED-1" ++#define NESTED_TITLE "swc" ++#define NESTED_CAPTURE_KEY "ctrl+alt+g" ++#define NESTED_FULLSCREEN_KEY "ctrl+alt+f" ++ ++/* wl_surface.damage_buffer and wl_pointer.frame first appear here */ ++#define COMPOSITOR_VERSION 4 ++#define SEAT_VERSION 5 ++#define OUTPUT_VERSION 2 ++ ++/* a key the parent window keeps for itself, never passed to clients */ ++struct chord { ++ xkb_keysym_t keysym; ++ uint32_t modifiers; ++ char description[64]; ++}; ++ ++static struct { ++ struct swc_backend backend; ++ ++ struct wl_display *display; ++ struct wl_registry *registry; ++ struct wl_compositor *compositor; ++ uint32_t compositor_version; ++ struct xdg_wm_base *wm_base; ++ struct wl_output *output; ++ struct wl_seat *seat; ++ uint32_t seat_version; ++ struct wl_keyboard *keyboard; ++ struct wl_pointer *pointer; ++ struct zwp_keyboard_shortcuts_inhibit_manager_v1 *inhibit_manager; ++ struct zwp_keyboard_shortcuts_inhibitor_v1 *inhibitor; ++ struct wp_cursor_shape_manager_v1 *shape_manager; ++ struct wp_cursor_shape_device_v1 *shape_device; ++ struct wl_event_source *source; ++ ++ struct wl_surface *surface; ++ struct xdg_surface *xdg_surface; ++ struct xdg_toplevel *toplevel; ++ struct wl_callback *frame; ++ struct screen *screen; ++ bool configured; ++ bool captured; ++ bool fullscreen; ++ ++ /* the parent talks in logical units, swc in buffer pixels. every ++ * size and coordinate crossing this file is converted by scale */ ++ int32_t scale; ++ uint32_t width, height; ++ int32_t pending_width, pending_height; ++ ++ struct { ++ struct wl_surface *surface; ++ struct wld_buffer *buffer; ++ int32_t hotspot_x, hotspot_y; ++ uint32_t enter_serial; ++ bool inside; ++ /* where the parent's pointer is, kept while released so a ++ * capture puts ours there */ ++ wl_fixed_t x, y; ++ /* the click that captured, its release goes nowhere either */ ++ uint32_t swallowed; ++ } cursor; ++ ++ struct { ++ enum wl_pointer_axis_source source; ++ int32_t discrete[2]; ++ } axis; ++ ++ /* parent keymap that arrived before the swc seat existed */ ++ struct xkb_keymap *keymap; ++ ++ struct chord capture_key, fullscreen_key; ++ ++ /* the parent's modifier state, kept here while released so the nested ++ * clients see none of it, and handed to swc on capture */ ++ struct { ++ uint32_t depressed, latched, locked, group; ++ } mods; ++} nested; ++ ++bool ++nested_requested(void) ++{ ++ const char *backend = getenv("SWC_BACKEND"); ++ ++ if (backend) { ++ return strcmp(backend, "nested") == 0; ++ } ++ if (getenv(SWC_LAUNCH_SOCKET_ENV)) { ++ return false; ++ } ++ return getenv("WAYLAND_DISPLAY") != NULL; ++} ++ ++static int32_t ++env_int(const char *name, int32_t fallback) ++{ ++ const char *value = getenv(name); ++ long number; ++ char *end; ++ ++ if (!value || !*value) { ++ return fallback; ++ } ++ number = strtol(value, &end, 10); ++ return *end == '\0' && number > 0 && number <= INT32_MAX ++ ? (int32_t)number ++ : fallback; ++} ++ ++/* primary plane */ ++ ++static void ++handle_frame_done(void *data, struct wl_callback *callback, uint32_t time) ++{ ++ wl_callback_destroy(callback); ++ nested.frame = NULL; ++ /* the clock of the parent is not ours, and clients compare this ++ * against the timestamps swc puts on input events */ ++ view_frame(&nested.screen->planes.primary.view, get_time()); ++} ++ ++static const struct wl_callback_listener frame_listener = { ++ .done = handle_frame_done, ++}; ++ ++static bool ++plane_update(struct view *view) ++{ ++ return true; ++} ++ ++static int ++plane_attach(struct view *view, struct wld_buffer *buffer) ++{ ++ union wld_object object; ++ ++ if (!buffer) { ++ return 0; ++ } ++ if (!wld_export(buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) { ++ ERROR("nested: could not export the screen buffer\n"); ++ return -1; ++ } ++ ++ if (nested.frame) { ++ wl_callback_destroy(nested.frame); ++ } ++ nested.frame = wl_surface_frame(nested.surface); ++ wl_callback_add_listener(nested.frame, &frame_listener, NULL); ++ ++ wl_surface_set_buffer_scale(nested.surface, nested.scale); ++ wl_surface_attach(nested.surface, object.ptr, 0, 0); ++ if (nested.compositor_version >= COMPOSITOR_VERSION) { ++ wl_surface_damage_buffer(nested.surface, 0, 0, INT32_MAX, INT32_MAX); ++ } else { ++ wl_surface_damage(nested.surface, 0, 0, INT32_MAX, INT32_MAX); ++ } ++ wl_surface_commit(nested.surface); ++ ++ return 0; ++} ++ ++static bool ++plane_move(struct view *view, int32_t x, int32_t y) ++{ ++ view_set_position(view, x, y); ++ return true; ++} ++ ++static const struct view_impl plane_impl = { ++ .update = plane_update, ++ .attach = plane_attach, ++ .move = plane_move, ++}; ++ ++static struct wld_surface * ++create_target(struct screen *screen) ++{ ++ const struct swc_rectangle *geometry = &screen->base.geometry; ++ ++ return wld_wayland_create_surface(nested.backend.context, geometry->width, ++ geometry->height, WLD_FORMAT_XRGB8888, ++ WLD_FLAG_MAP, nested.surface); ++} ++ ++/* cursor: show our cursor image as the parent cursor, no lag, no second cursor */ ++ ++static void ++cursor_commit(void) ++{ ++ union wld_object object; ++ ++ if (!nested.pointer || !nested.cursor.inside) { ++ return; ++ } ++ /* released, the window is the parent's like any other and shows the ++ * parent's own cursor. a parent without cursor-shape gets ours */ ++ if (!nested.captured && nested.shape_device) { ++ wp_cursor_shape_device_v1_set_shape(nested.shape_device, nested.cursor.enter_serial, ++ WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT); ++ return; ++ } ++ if (!nested.cursor.buffer || ++ !wld_export(nested.cursor.buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) { ++ wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial, NULL, ++ 0, 0); ++ return; ++ } ++ ++ wl_surface_set_buffer_scale(nested.cursor.surface, nested.scale); ++ wl_surface_attach(nested.cursor.surface, object.ptr, 0, 0); ++ wl_surface_damage(nested.cursor.surface, 0, 0, INT32_MAX, INT32_MAX); ++ wl_surface_commit(nested.cursor.surface); ++ wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial, ++ nested.cursor.surface, ++ nested.cursor.hotspot_x / nested.scale, ++ nested.cursor.hotspot_y / nested.scale); ++} ++ ++static void ++set_cursor(struct wld_buffer *buffer, int32_t hotspot_x, int32_t hotspot_y) ++{ ++ nested.cursor.buffer = buffer; ++ nested.cursor.hotspot_x = hotspot_x; ++ nested.cursor.hotspot_y = hotspot_y; ++ cursor_commit(); ++} ++ ++/* input capture like a vm window: a click or the chord captures, ++ * the chord releases. while captured the parent stops using its own ++ * shortcuts. a parent without the inhibit protocol keeps them */ ++ ++static void ++update_title(void) ++{ ++ char title[2 * sizeof(nested.capture_key.description) + 64]; ++ ++ snprintf(title, sizeof(title), NESTED_TITLE " (%s %s input, %s fullscreen)", ++ nested.capture_key.description, ++ nested.captured ? "releases" : "captures", ++ nested.fullscreen_key.description); ++ xdg_toplevel_set_title(nested.toplevel, title); ++} ++ ++static void ++capture(void) ++{ ++ if (nested.captured) { ++ return; ++ } ++ nested.captured = true; ++ if (nested.inhibit_manager && nested.seat) { ++ nested.inhibitor = ++ zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts( ++ nested.inhibit_manager, nested.surface, nested.seat); ++ } ++ if (swc.seat) { ++ keyboard_handle_modifiers(swc.seat->keyboard, nested.mods.depressed, ++ nested.mods.latched, nested.mods.locked, ++ nested.mods.group); ++ } ++ if (swc.seat && nested.cursor.inside) { ++ pointer_handle_absolute_motion(swc.seat->pointer, get_time(), ++ nested.cursor.x * nested.scale, ++ nested.cursor.y * nested.scale); ++ pointer_handle_frame(swc.seat->pointer); ++ } ++ cursor_commit(); ++ update_title(); ++} ++ ++static void ++release(void) ++{ ++ if (!nested.captured) { ++ return; ++ } ++ nested.captured = false; ++ if (nested.inhibitor) { ++ zwp_keyboard_shortcuts_inhibitor_v1_destroy(nested.inhibitor); ++ nested.inhibitor = NULL; ++ } ++ /* the chord that got us here is still held, and its release goes to ++ * whatever the parent focuses next */ ++ if (swc.seat) { ++ keyboard_reset(swc.seat->keyboard); ++ } ++ cursor_commit(); ++ if (nested.toplevel) { ++ update_title(); ++ } ++} ++ ++static uint32_t ++modifier_from_name(const char *name) ++{ ++ static const struct { ++ const char *name; ++ uint32_t modifier; ++ } names[] = { ++ {"ctrl", SWC_MOD_CTRL}, {"control", SWC_MOD_CTRL}, ++ {"alt", SWC_MOD_ALT}, {"mod1", SWC_MOD_ALT}, ++ {"super", SWC_MOD_LOGO}, {"logo", SWC_MOD_LOGO}, ++ {"mod4", SWC_MOD_LOGO}, {"shift", SWC_MOD_SHIFT}, ++ }; ++ size_t i; ++ ++ for (i = 0; i < ARRAY_LENGTH(names); i++) { ++ if (strcasecmp(name, names[i].name) == 0) { ++ return names[i].modifier; ++ } ++ } ++ return 0; ++} ++ ++/* "ctrl+alt+g": all but the last token are modifiers. ++ * the environment variable wins, "none" turns it off */ ++static void ++parse_chord(struct chord *chord, const char *variable, const char *fallback) ++{ ++ const char *spec = getenv(variable); ++ char buffer[sizeof(chord->description)]; ++ char *token, *next, *save; ++ ++ if (!spec || !*spec) { ++ spec = fallback; ++ } ++ snprintf(buffer, sizeof(buffer), "%s", spec); ++ snprintf(chord->description, sizeof(chord->description), "%s", spec); ++ chord->modifiers = 0; ++ chord->keysym = XKB_KEY_NoSymbol; ++ ++ for (token = strtok_r(buffer, "+", &save); token; token = next) { ++ next = strtok_r(NULL, "+", &save); ++ if (next) { ++ chord->modifiers |= modifier_from_name(token); ++ } else { ++ chord->keysym = ++ xkb_keysym_from_name(token, XKB_KEYSYM_CASE_INSENSITIVE); ++ } ++ } ++} ++ ++/* match the keysym ignoring its level */ ++static uint32_t ++held_modifiers(struct xkb_keymap *map) ++{ ++ static const struct { ++ const char *name; ++ uint32_t mod; ++ } names[] = { ++ {XKB_MOD_NAME_CTRL, SWC_MOD_CTRL}, ++ {XKB_MOD_NAME_ALT, SWC_MOD_ALT}, ++ {XKB_MOD_NAME_LOGO, SWC_MOD_LOGO}, ++ {XKB_MOD_NAME_SHIFT, SWC_MOD_SHIFT}, ++ }; ++ struct xkb_state *state = xkb_state_new(map); ++ uint32_t mods = 0; ++ size_t i; ++ ++ if (!state) { ++ return 0; ++ } ++ xkb_state_update_mask(state, nested.mods.depressed, nested.mods.latched, ++ nested.mods.locked, 0, 0, nested.mods.group); ++ for (i = 0; i < ARRAY_LENGTH(names); i++) { ++ if (xkb_state_mod_name_is_active(state, names[i].name, ++ XKB_STATE_MODS_EFFECTIVE) > 0) { ++ mods |= names[i].mod; ++ } ++ } ++ xkb_state_unref(state); ++ return mods; ++} ++ ++/* match the keysym ignoring its level */ ++static bool ++is_chord(const struct chord *chord, struct keyboard *keyboard, uint32_t key) ++{ ++ struct xkb_keymap *map = keyboard->xkb.keymap.map; ++ const xkb_keysym_t *syms; ++ ++ if (chord->keysym == XKB_KEY_NoSymbol || ++ (held_modifiers(map) & chord->modifiers) != chord->modifiers) { ++ return false; ++ } ++ if (xkb_keymap_key_get_syms_by_level(map, XKB_KEY(key), nested.mods.group, 0, ++ &syms) < 1) { ++ return false; ++ } ++ return xkb_keysym_to_lower(syms[0]) == xkb_keysym_to_lower(chord->keysym); ++} ++ ++static void ++toggle_fullscreen(void) ++{ ++ if (nested.fullscreen) { ++ xdg_toplevel_unset_fullscreen(nested.toplevel); ++ } else { ++ xdg_toplevel_set_fullscreen(nested.toplevel, NULL); ++ } ++} ++ ++/* keyboard */ ++ ++static void ++apply_keymap(void) ++{ ++ if (!nested.keymap || !swc.seat) { ++ return; ++ } ++ if (!keyboard_set_keymap(swc.seat->keyboard, nested.keymap)) { ++ WARNING("nested: could not adopt the parent keymap\n"); ++ } ++ xkb_keymap_unref(nested.keymap); ++ nested.keymap = NULL; ++} ++ ++/* raw key codes mean the same only with the same keymap on both sides */ ++static void ++handle_keyboard_keymap(void *data, struct wl_keyboard *keyboard, ++ uint32_t format, int32_t fd, uint32_t size) ++{ ++ struct xkb_context *context; ++ struct xkb_keymap *keymap = NULL; ++ char *string; ++ ++ if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { ++ close(fd); ++ return; ++ } ++ string = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); ++ close(fd); ++ if (string == MAP_FAILED) { ++ return; ++ } ++ if ((context = xkb_context_new(XKB_CONTEXT_NO_FLAGS))) { ++ keymap = xkb_keymap_new_from_buffer(context, string, ++ strnlen(string, size), ++ XKB_KEYMAP_FORMAT_TEXT_V1, ++ XKB_KEYMAP_COMPILE_NO_FLAGS); ++ xkb_context_unref(context); ++ } ++ munmap(string, size); ++ if (!keymap) { ++ WARNING("nested: could not compile the parent keymap\n"); ++ return; ++ } ++ ++ if (nested.keymap) { ++ xkb_keymap_unref(nested.keymap); ++ } ++ nested.keymap = keymap; ++ apply_keymap(); ++} ++ ++static void ++handle_keyboard_enter(void *data, struct wl_keyboard *keyboard, ++ uint32_t serial, struct wl_surface *surface, ++ struct wl_array *keys) ++{ ++} ++ ++/* keys held when focus leaves are never released, so reset them */ ++static void ++handle_keyboard_leave(void *data, struct wl_keyboard *keyboard, ++ uint32_t serial, struct wl_surface *surface) ++{ ++ if (swc.seat) { ++ keyboard_reset(swc.seat->keyboard); ++ } ++} ++ ++static void ++handle_keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, ++ uint32_t time, uint32_t key, uint32_t state) ++{ ++ if (!swc.seat) { ++ return; ++ } ++ if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { ++ if (is_chord(&nested.capture_key, swc.seat->keyboard, key)) { ++ if (nested.captured) { ++ release(); ++ } else { ++ capture(); ++ } ++ return; ++ } ++ if (is_chord(&nested.fullscreen_key, swc.seat->keyboard, key)) { ++ toggle_fullscreen(); ++ return; ++ } ++ } ++ /* released, keys are the parent's. the chords above still see the ++ * modifiers, which keep being tracked */ ++ if (!nested.captured) { ++ return; ++ } ++ keyboard_handle_key(swc.seat->keyboard, get_time(), key, state); ++} ++ ++/* locks, layout group and modifiers held at focus in */ ++static void ++handle_keyboard_modifiers(void *data, struct wl_keyboard *keyboard, ++ uint32_t serial, uint32_t depressed, ++ uint32_t latched, uint32_t locked, uint32_t group) ++{ ++ nested.mods.depressed = depressed; ++ nested.mods.latched = latched; ++ nested.mods.locked = locked; ++ nested.mods.group = group; ++ if (swc.seat && nested.captured) { ++ keyboard_handle_modifiers(swc.seat->keyboard, depressed, latched, ++ locked, group); ++ } ++} ++ ++static void ++handle_keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, ++ int32_t rate, int32_t delay) ++{ ++} ++ ++static const struct wl_keyboard_listener keyboard_listener = { ++ .keymap = handle_keyboard_keymap, ++ .enter = handle_keyboard_enter, ++ .leave = handle_keyboard_leave, ++ .key = handle_keyboard_key, ++ .modifiers = handle_keyboard_modifiers, ++ .repeat_info = handle_keyboard_repeat_info, ++}; ++ ++/* pointer */ ++ ++/* seats older than SEAT_VERSION send no pointer.frame, clients wait for one */ ++static void ++frame_legacy(void) ++{ ++ if (nested.seat_version < SEAT_VERSION) { ++ pointer_handle_frame(swc.seat->pointer); ++ } ++} ++ ++static void ++handle_pointer_enter(void *data, struct wl_pointer *pointer, uint32_t serial, ++ struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) ++{ ++ nested.cursor.inside = true; ++ nested.cursor.enter_serial = serial; ++ nested.cursor.x = x; ++ nested.cursor.y = y; ++ cursor_commit(); ++ if (!swc.seat || !nested.captured) { ++ return; ++ } ++ pointer_handle_absolute_motion(swc.seat->pointer, get_time(), ++ x * nested.scale, y * nested.scale); ++ frame_legacy(); ++} ++ ++static void ++handle_pointer_leave(void *data, struct wl_pointer *pointer, uint32_t serial, ++ struct wl_surface *surface) ++{ ++ nested.cursor.inside = false; ++} ++ ++static void ++handle_pointer_motion(void *data, struct wl_pointer *pointer, uint32_t time, ++ wl_fixed_t x, wl_fixed_t y) ++{ ++ nested.cursor.x = x; ++ nested.cursor.y = y; ++ if (!swc.seat || !nested.captured) { ++ return; ++ } ++ pointer_handle_absolute_motion(swc.seat->pointer, get_time(), ++ x * nested.scale, y * nested.scale); ++ frame_legacy(); ++} ++ ++static void ++handle_pointer_button(void *data, struct wl_pointer *pointer, uint32_t serial, ++ uint32_t time, uint32_t button, uint32_t state) ++{ ++ if (!swc.seat) { ++ return; ++ } ++ /* released, a click only captures, as in a virtual machine window */ ++ if (!nested.captured) { ++ if (state == WL_POINTER_BUTTON_STATE_PRESSED) { ++ nested.cursor.swallowed = button; ++ capture(); ++ } ++ return; ++ } ++ if (button == nested.cursor.swallowed) { ++ nested.cursor.swallowed = 0; ++ if (state == WL_POINTER_BUTTON_STATE_RELEASED) { ++ return; ++ } ++ } ++ pointer_handle_button(swc.seat->pointer, get_time(), button, state); ++ frame_legacy(); ++} ++ ++static void ++handle_pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time, ++ uint32_t axis, wl_fixed_t value) ++{ ++ int32_t discrete = 0; ++ ++ if (!swc.seat || !nested.captured) { ++ return; ++ } ++ if (axis < ARRAY_LENGTH(nested.axis.discrete)) { ++ discrete = nested.axis.discrete[axis]; ++ nested.axis.discrete[axis] = 0; ++ } ++ pointer_handle_axis(swc.seat->pointer, get_time(), axis, ++ nested.axis.source, value, discrete * 120); ++ frame_legacy(); ++} ++ ++static void ++handle_pointer_frame(void *data, struct wl_pointer *pointer) ++{ ++ nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL; ++ if (swc.seat && nested.captured) { ++ pointer_handle_frame(swc.seat->pointer); ++ } ++} ++ ++static void ++handle_pointer_axis_source(void *data, struct wl_pointer *pointer, ++ uint32_t source) ++{ ++ nested.axis.source = source; ++} ++ ++static void ++handle_pointer_axis_stop(void *data, struct wl_pointer *pointer, uint32_t time, ++ uint32_t axis) ++{ ++} ++ ++static void ++handle_pointer_axis_discrete(void *data, struct wl_pointer *pointer, ++ uint32_t axis, int32_t discrete) ++{ ++ if (axis < ARRAY_LENGTH(nested.axis.discrete)) { ++ nested.axis.discrete[axis] = discrete; ++ } ++} ++ ++static const struct wl_pointer_listener pointer_listener = { ++ .enter = handle_pointer_enter, ++ .leave = handle_pointer_leave, ++ .motion = handle_pointer_motion, ++ .button = handle_pointer_button, ++ .axis = handle_pointer_axis, ++ .frame = handle_pointer_frame, ++ .axis_source = handle_pointer_axis_source, ++ .axis_stop = handle_pointer_axis_stop, ++ .axis_discrete = handle_pointer_axis_discrete, ++}; ++ ++/* seat */ ++ ++static void ++handle_seat_capabilities(void *data, struct wl_seat *seat, ++ uint32_t capabilities) ++{ ++ bool has_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD; ++ bool has_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER; ++ ++ if (has_keyboard && !nested.keyboard) { ++ nested.keyboard = wl_seat_get_keyboard(seat); ++ wl_keyboard_add_listener(nested.keyboard, &keyboard_listener, NULL); ++ } else if (!has_keyboard && nested.keyboard) { ++ wl_keyboard_destroy(nested.keyboard); ++ nested.keyboard = NULL; ++ } ++ ++ if (has_pointer && !nested.pointer) { ++ nested.pointer = wl_seat_get_pointer(seat); ++ wl_pointer_add_listener(nested.pointer, &pointer_listener, NULL); ++ if (nested.shape_manager) ++ nested.shape_device = wp_cursor_shape_manager_v1_get_pointer( ++ nested.shape_manager, nested.pointer); ++ } else if (!has_pointer && nested.pointer) { ++ if (nested.shape_device) { ++ wp_cursor_shape_device_v1_destroy(nested.shape_device); ++ nested.shape_device = NULL; ++ } ++ wl_pointer_destroy(nested.pointer); ++ nested.pointer = NULL; ++ nested.cursor.inside = false; ++ } ++} ++ ++static void ++handle_seat_name(void *data, struct wl_seat *seat, const char *name) ++{ ++} ++ ++static const struct wl_seat_listener seat_listener = { ++ .capabilities = handle_seat_capabilities, ++ .name = handle_seat_name, ++}; ++ ++/* output: only its scale matters */ ++ ++static void ++handle_output_geometry(void *data, struct wl_output *output, int32_t x, ++ int32_t y, int32_t physical_width, ++ int32_t physical_height, int32_t subpixel, ++ const char *make, const char *model, int32_t transform) ++{ ++} ++ ++static void ++handle_output_mode(void *data, struct wl_output *output, uint32_t flags, ++ int32_t width, int32_t height, int32_t refresh) ++{ ++} ++ ++static void ++handle_output_done(void *data, struct wl_output *output) ++{ ++} ++ ++static void ++handle_output_scale(void *data, struct wl_output *output, int32_t factor) ++{ ++ /* SWC_NESTED_SCALE pins the scale. 1 on a dense display makes the ++ * parent magnify us, which is what a session with no scaling of its ++ * own wants */ ++ if (factor > 0 && !nested.surface && !getenv("SWC_NESTED_SCALE")) { ++ nested.scale = factor; ++ } ++} ++ ++static const struct wl_output_listener output_listener = { ++ .geometry = handle_output_geometry, ++ .mode = handle_output_mode, ++ .done = handle_output_done, ++ .scale = handle_output_scale, ++}; ++ ++/* shell surface */ ++ ++static void ++handle_wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial) ++{ ++ xdg_wm_base_pong(wm_base, serial); ++} ++ ++static const struct xdg_wm_base_listener wm_base_listener = { ++ .ping = handle_wm_base_ping, ++}; ++ ++static void ++resize(uint32_t width, uint32_t height) ++{ ++ if (width == nested.width && height == nested.height) { ++ return; ++ } ++ nested.width = width; ++ nested.height = height; ++ if (nested.screen) { ++ screen_resize(nested.screen, width, height); ++ } ++} ++ ++static void ++handle_xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, ++ uint32_t serial) ++{ ++ xdg_surface_ack_configure(xdg_surface, serial); ++ nested.configured = true; ++ ++ if (nested.pending_width > 0 && nested.pending_height > 0) { ++ resize((uint32_t)(nested.pending_width * nested.scale), ++ (uint32_t)(nested.pending_height * nested.scale)); ++ } ++ nested.pending_width = 0; ++ nested.pending_height = 0; ++} ++ ++static const struct xdg_surface_listener xdg_surface_listener = { ++ .configure = handle_xdg_surface_configure, ++}; ++ ++/* a 0 here means the parent leaves the size to us, so keep ours */ ++static void ++handle_toplevel_configure(void *data, struct xdg_toplevel *toplevel, ++ int32_t width, int32_t height, ++ struct wl_array *states) ++{ ++ uint32_t *state; ++ ++ nested.pending_width = width; ++ nested.pending_height = height; ++ nested.fullscreen = false; ++ wl_array_for_each(state, states) { ++ if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) { ++ nested.fullscreen = true; ++ } ++ } ++} ++ ++static void ++handle_toplevel_close(void *data, struct xdg_toplevel *toplevel) ++{ ++ wl_display_terminate(swc.display); ++} ++ ++static const struct xdg_toplevel_listener toplevel_listener = { ++ .configure = handle_toplevel_configure, ++ .close = handle_toplevel_close, ++}; ++ ++static bool ++create_screens(struct wl_list *screens) ++{ ++ struct output *output; ++ unsigned width = NESTED_DEFAULT_WIDTH, height = NESTED_DEFAULT_HEIGHT; ++ const char *size = getenv("SWC_NESTED_SIZE"); ++ ++ if (size && (sscanf(size, "%ux%u", &width, &height) != 2 || width == 0 || ++ height == 0)) { ++ WARNING("nested: ignoring malformed SWC_NESTED_SIZE '%s'\n", size); ++ width = NESTED_DEFAULT_WIDTH; ++ height = NESTED_DEFAULT_HEIGHT; ++ } ++ nested.width = width * (uint32_t)nested.scale; ++ nested.height = height * (uint32_t)nested.scale; ++ ++ nested.surface = wl_compositor_create_surface(nested.compositor); ++ nested.cursor.surface = wl_compositor_create_surface(nested.compositor); ++ if (!nested.surface || !nested.cursor.surface) { ++ return false; ++ } ++ nested.xdg_surface = ++ xdg_wm_base_get_xdg_surface(nested.wm_base, nested.surface); ++ xdg_surface_add_listener(nested.xdg_surface, &xdg_surface_listener, NULL); ++ nested.toplevel = xdg_surface_get_toplevel(nested.xdg_surface); ++ xdg_toplevel_add_listener(nested.toplevel, &toplevel_listener, NULL); ++ update_title(); ++ xdg_toplevel_set_app_id(nested.toplevel, NESTED_TITLE); ++ wl_surface_commit(nested.surface); ++ ++ /* attaching a buffer before the first configure is a protocol ++ * error, and a tiling parent answers with the size it really gives ++ * us, which has to be known before the screen exists */ ++ while (!nested.configured) { ++ if (wl_display_dispatch(nested.display) < 0) { ++ ERROR("nested: lost the parent before the first configure\n"); ++ return false; ++ } ++ } ++ ++ output = output_new_fb(nested.width, nested.height, NESTED_OUTPUT_NAME); ++ if (!output) { ++ return false; ++ } ++ nested.screen = screen_new_virtual(output, &plane_impl); ++ if (!nested.screen) { ++ output_destroy(output); ++ return false; ++ } ++ output->screen = nested.screen; ++ wl_list_insert(screens, &nested.screen->link); ++ ++ return true; ++} ++ ++/* registry */ ++ ++static void ++handle_global(void *data, struct wl_registry *registry, uint32_t name, ++ const char *interface, uint32_t version) ++{ ++ if (strcmp(interface, wl_compositor_interface.name) == 0) { ++ nested.compositor_version = MIN(version, COMPOSITOR_VERSION); ++ nested.compositor = ++ wl_registry_bind(registry, name, &wl_compositor_interface, ++ nested.compositor_version); ++ } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { ++ nested.wm_base = ++ wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); ++ xdg_wm_base_add_listener(nested.wm_base, &wm_base_listener, NULL); ++ } else if (strcmp(interface, wl_seat_interface.name) == 0 && !nested.seat) { ++ nested.seat_version = MIN(version, SEAT_VERSION); ++ nested.seat = wl_registry_bind(registry, name, &wl_seat_interface, ++ nested.seat_version); ++ wl_seat_add_listener(nested.seat, &seat_listener, NULL); ++ } else if (strcmp(interface, wl_output_interface.name) == 0 && ++ !nested.output) { ++ nested.output = wl_registry_bind(registry, name, &wl_output_interface, ++ MIN(version, OUTPUT_VERSION)); ++ wl_output_add_listener(nested.output, &output_listener, NULL); ++ } else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) { ++ nested.shape_manager = wl_registry_bind( ++ registry, name, &wp_cursor_shape_manager_v1_interface, 1); ++ } else if (strcmp(interface, ++ zwp_keyboard_shortcuts_inhibit_manager_v1_interface ++ .name) == 0) { ++ nested.inhibit_manager = wl_registry_bind( ++ registry, name, ++ &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1); ++ } ++} ++ ++static void ++handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) ++{ ++} ++ ++static const struct wl_registry_listener registry_listener = { ++ .global = handle_global, ++ .global_remove = handle_global_remove, ++}; ++ ++/* the parent connection is in our event loop. a check source drains ++ * replies already read, which nothing else wakes the loop for */ ++static int ++handle_display(int fd, uint32_t mask, void *data) ++{ ++ int count; ++ ++ if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) { ++ ERROR("nested: the parent compositor went away\n"); ++ wl_display_terminate(swc.display); ++ return 0; ++ } ++ ++ count = mask & WL_EVENT_READABLE ++ ? wl_display_dispatch(nested.display) ++ : wl_display_dispatch_pending(nested.display); ++ if (count < 0 || ++ (wl_display_flush(nested.display) < 0 && errno != EAGAIN)) { ++ ERROR("nested: parent connection failed: %s\n", strerror(errno)); ++ wl_display_terminate(swc.display); ++ return 0; ++ } ++ return count; ++} ++ ++/* the seat exists only now, well after the parent sent its keymap */ ++static void ++start(void) ++{ ++ apply_keymap(); ++ swc_activate(); ++} ++ ++static void ++finalize(void) ++{ ++ release(); ++ if (nested.keymap) { ++ xkb_keymap_unref(nested.keymap); ++ } ++ if (nested.source) { ++ wl_event_source_remove(nested.source); ++ } ++ if (nested.frame) { ++ wl_callback_destroy(nested.frame); ++ } ++ if (nested.toplevel) { ++ xdg_toplevel_destroy(nested.toplevel); ++ } ++ if (nested.xdg_surface) { ++ xdg_surface_destroy(nested.xdg_surface); ++ } ++ if (nested.surface) { ++ wl_surface_destroy(nested.surface); ++ } ++ if (nested.cursor.surface) { ++ wl_surface_destroy(nested.cursor.surface); ++ } ++ if (nested.backend.renderer) { ++ wld_destroy_renderer(nested.backend.renderer); ++ } ++ if (nested.backend.context) { ++ wld_destroy_context(nested.backend.context); ++ } ++ if (nested.shape_device) { ++ wp_cursor_shape_device_v1_destroy(nested.shape_device); ++ } ++ if (nested.pointer) { ++ wl_pointer_destroy(nested.pointer); ++ } ++ if (nested.keyboard) { ++ wl_keyboard_destroy(nested.keyboard); ++ } ++ if (nested.seat) { ++ wl_seat_destroy(nested.seat); ++ } ++ if (nested.output) { ++ wl_output_destroy(nested.output); ++ } ++ if (nested.inhibit_manager) { ++ zwp_keyboard_shortcuts_inhibit_manager_v1_destroy( ++ nested.inhibit_manager); ++ } ++ if (nested.shape_manager) { ++ wp_cursor_shape_manager_v1_destroy(nested.shape_manager); ++ } ++ if (nested.wm_base) { ++ xdg_wm_base_destroy(nested.wm_base); ++ } ++ if (nested.compositor) { ++ wl_compositor_destroy(nested.compositor); ++ } ++ if (nested.registry) { ++ wl_registry_destroy(nested.registry); ++ } ++ if (nested.display) { ++ wl_display_disconnect(nested.display); ++ } ++ memset(&nested, 0, sizeof(nested)); ++} ++ ++bool ++nested_initialize(void) ++{ ++ int32_t cursor_size; ++ ++ nested.scale = env_int("SWC_NESTED_SCALE", 1); ++ nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL; ++ parse_chord(&nested.capture_key, "SWC_NESTED_CAPTURE_KEY", NESTED_CAPTURE_KEY); ++ parse_chord(&nested.fullscreen_key, "SWC_NESTED_FULLSCREEN_KEY", ++ NESTED_FULLSCREEN_KEY); ++ ++ if (!(nested.display = wl_display_connect(NULL))) { ++ ERROR("nested: could not connect to the parent compositor\n"); ++ goto error; ++ } ++ nested.registry = wl_display_get_registry(nested.display); ++ wl_registry_add_listener(nested.registry, &registry_listener, NULL); ++ /* the first roundtrip binds the globals, the second collects what ++ * they announce on bind: the output scale and the seat capabilities */ ++ if (wl_display_roundtrip(nested.display) < 0 || ++ wl_display_roundtrip(nested.display) < 0) { ++ ERROR("nested: parent connection failed during setup\n"); ++ goto error; ++ } ++ if (!nested.compositor || !nested.wm_base) { ++ ERROR("nested: the parent lacks wl_compositor or xdg_wm_base\n"); ++ goto error; ++ } ++ ++ nested.backend.context = ++ wld_wayland_create_context(nested.display, WLD_SHM, WLD_NONE); ++ if (!nested.backend.context) { ++ ERROR("nested: could not create a wld context on the parent\n"); ++ goto error; ++ } ++ nested.backend.renderer = wld_create_renderer(nested.backend.context); ++ if (!nested.backend.renderer) { ++ ERROR("nested: could not create a wld renderer\n"); ++ goto error; ++ } ++ ++ /* the size of a buffer must be a multiple of its buffer scale */ ++ cursor_size = NESTED_CURSOR_SIZE - NESTED_CURSOR_SIZE % nested.scale; ++ nested.backend.cursor_width = (uint32_t)cursor_size; ++ nested.backend.cursor_height = (uint32_t)cursor_size; ++ nested.backend.virtual_input = true; ++ nested.backend.create_screens = create_screens; ++ nested.backend.create_target = create_target; ++ nested.backend.set_cursor = set_cursor; ++ nested.backend.start = start; ++ nested.backend.finalize = finalize; ++ ++ nested.source = wl_event_loop_add_fd(swc.event_loop, ++ wl_display_get_fd(nested.display), ++ WL_EVENT_READABLE, handle_display, ++ NULL); ++ if (!nested.source) { ++ ERROR("nested: could not watch the parent connection\n"); ++ goto error; ++ } ++ wl_event_source_check(nested.source); ++ ++ swc.backend = &nested.backend; ++ return true; ++ ++error: ++ finalize(); ++ return false; ++} +diff --git a/libswc/nested.h b/libswc/nested.h +new file mode 100644 +index 0000000..5c16e53 +--- /dev/null ++++ b/libswc/nested.h +@@ -0,0 +1,16 @@ ++/* nested backend: swc as a client of another compositor */ ++ ++#ifndef SWC_NESTED_H ++#define SWC_NESTED_H ++ ++#include <stdbool.h> ++ ++/* true to come up nested: SWC_BACKEND decides, else WAYLAND_DISPLAY without a launcher socket */ ++bool ++nested_requested(void); ++ ++/* connects to the parent and installs itself as swc.backend */ ++bool ++nested_initialize(void); ++ ++#endif +diff --git a/libswc/output.c b/libswc/output.c +index 8abcb24..a5ca71f 100644 +--- a/libswc/output.c ++++ b/libswc/output.c +@@ -170,6 +170,25 @@ output_new_fb(uint32_t width, uint32_t height, const char *name) + return output; + } + ++void ++output_set_size(struct output *output, uint32_t width, uint32_t height) ++{ ++ struct wl_resource *resource; ++ struct mode *mode = output->preferred_mode; ++ ++ mode->width = width; ++ mode->height = height; ++ wl_resource_for_each(resource, &output->resources) ++ { ++ wl_output_send_mode(resource, ++ WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED, ++ mode->width, mode->height, mode->refresh); ++ if (wl_resource_get_version(resource) >= 2) { ++ wl_output_send_done(resource); ++ } ++ } ++} ++ + void + output_destroy(struct output *output) + { +diff --git a/libswc/output.h b/libswc/output.h +index 82d45cc..01c1dc0 100644 +--- a/libswc/output.h ++++ b/libswc/output.h +@@ -40,6 +40,8 @@ output_new(drmModeConnector *connector); + struct output * + output_new_fb(uint32_t width, uint32_t height, const char *name); + void ++output_set_size(struct output *output, uint32_t width, uint32_t height); ++void + output_destroy(struct output *output); + + #endif +diff --git a/libswc/primary_plane.c b/libswc/primary_plane.c +index eda7f31..011cb39 100644 +--- a/libswc/primary_plane.c ++++ b/libswc/primary_plane.c +@@ -23,10 +23,11 @@ + + #include "primary_plane.h" + #include "event.h" ++#include "compositor.h" + #ifdef ENABLE_DRM + #include "drm.h" +-#else +-#include "compositor.h" ++#endif ++#ifdef ENABLE_FB + #include "fb.h" + #endif + #include "internal.h" +@@ -55,11 +56,10 @@ send_frame(void *data) + view_frame(&plane->view, get_time()); + } + ++#ifdef ENABLE_DRM + static int +-attach(struct view *view, struct wld_buffer *buffer) ++attach_drm(struct primary_plane *plane, struct wld_buffer *buffer) + { +- struct primary_plane *plane = wl_container_of(view, plane, view); +-#ifdef ENABLE_DRM + uint32_t fb; + int ret; + +@@ -88,12 +88,35 @@ attach(struct view *view, struct wld_buffer *buffer) + } + + return 0; +-#else +- if (!fb_present(buffer, view->geometry.x, view->geometry.y)) { ++} ++#endif ++ ++#ifdef ENABLE_FB ++static int ++attach_fb(struct primary_plane *plane, struct wld_buffer *buffer) ++{ ++ if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y)) { + return -1; + } + wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); + return 0; ++} ++#endif ++ ++static int ++attach(struct view *view, struct wld_buffer *buffer) ++{ ++ struct primary_plane *plane = wl_container_of(view, plane, view); ++ ++#ifdef ENABLE_DRM ++ if (plane->drm) { ++ return attach_drm(plane, buffer); ++ } ++#endif ++#ifdef ENABLE_FB ++ return attach_fb(plane, buffer); ++#else ++ return -1; + #endif + } + +@@ -129,24 +152,33 @@ handle_swc_event(struct wl_listener *listener, void *data) + switch (event->type) { + case SWC_EVENT_ACTIVATED: + #ifdef ENABLE_DRM +- plane->need_modeset = true; +-#else +- compositor_damage_all(); ++ if (plane->drm) { ++ plane->need_modeset = true; ++ break; ++ } + #endif ++ compositor_damage_all(); + break; + } + } + +-bool +-#ifdef ENABLE_DRM +-primary_plane_initialize(struct primary_plane *plane, uint32_t crtc, +- struct mode *mode, uint32_t *connectors, +- uint32_t num_connectors) +-#else +-primary_plane_initialize(struct primary_plane *plane, struct mode *mode) +-#endif ++static void ++initialize_common(struct primary_plane *plane, struct mode *mode) + { ++ view_initialize(&plane->view, &view_impl); ++ plane->view.geometry.width = mode->width; ++ plane->view.geometry.height = mode->height; ++ plane->mode = *mode; ++ plane->swc_listener.notify = &handle_swc_event; ++ wl_signal_add(&swc.event_signal, &plane->swc_listener); ++} ++ + #ifdef ENABLE_DRM ++bool ++primary_plane_initialize_drm(struct primary_plane *plane, uint32_t crtc, ++ struct mode *mode, uint32_t *connectors, ++ uint32_t num_connectors) ++{ + uint32_t *plane_connectors; + + if (!(plane->original_crtc_state = drmModeGetCrtc(swc.drm->fd, crtc))) { +@@ -166,43 +198,45 @@ primary_plane_initialize(struct primary_plane *plane, struct mode *mode) + + memcpy(plane_connectors, connectors, + num_connectors * sizeof(connectors[0])); ++ plane->drm = true; + plane->crtc = crtc; + plane->need_modeset = true; +- view_initialize(&plane->view, &view_impl); +- plane->view.geometry.width = mode->width; +- plane->view.geometry.height = mode->height; + plane->drm_handler.page_flip = &handle_page_flip; +- plane->swc_listener.notify = &handle_swc_event; +- plane->mode = *mode; +-#else +- view_initialize(&plane->view, &view_impl); +- plane->view.geometry.width = mode->width; +- plane->view.geometry.height = mode->height; +- plane->mode = *mode; +-#endif +- plane->swc_listener.notify = &handle_swc_event; +- wl_signal_add(&swc.event_signal, &plane->swc_listener); ++ initialize_common(plane, mode); + + return true; + +-#ifdef ENABLE_DRM + error1: + drmModeFreeCrtc(plane->original_crtc_state); + error0: + return false; ++} + #endif ++ ++#ifdef ENABLE_FB ++bool ++primary_plane_initialize_fb(struct primary_plane *plane, struct mode *mode) ++{ ++#ifdef ENABLE_DRM ++ plane->drm = false; ++#endif ++ initialize_common(plane, mode); ++ return true; + } ++#endif + + void + primary_plane_finalize(struct primary_plane *plane) + { + wl_list_remove(&plane->swc_listener.link); + #ifdef ENABLE_DRM +- wl_array_release(&plane->connectors); +- drmModeCrtcPtr crtc = plane->original_crtc_state; +- drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x, +- crtc->y, NULL, 0, &crtc->mode); +- drmModeFreeCrtc(crtc); ++ if (plane->drm) { ++ wl_array_release(&plane->connectors); ++ drmModeCrtcPtr crtc = plane->original_crtc_state; ++ drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x, ++ crtc->y, NULL, 0, &crtc->mode); ++ drmModeFreeCrtc(crtc); ++ } + #else + (void)plane; + #endif +diff --git a/libswc/primary_plane.h b/libswc/primary_plane.h +index 55335ca..c9c8235 100644 +--- a/libswc/primary_plane.h ++++ b/libswc/primary_plane.h +@@ -36,6 +36,8 @@ + + struct primary_plane { + #ifdef ENABLE_DRM ++ /* driven through drm, not the framebuffer */ ++ bool drm; + uint32_t crtc; + drmModeCrtcPtr original_crtc_state; + #endif +@@ -51,12 +53,13 @@ struct primary_plane { + + #ifdef ENABLE_DRM + bool +-primary_plane_initialize(struct primary_plane *plane, uint32_t crtc, +- struct mode *mode, uint32_t *connectors, +- uint32_t num_connectors); +-#else ++primary_plane_initialize_drm(struct primary_plane *plane, uint32_t crtc, ++ struct mode *mode, uint32_t *connectors, ++ uint32_t num_connectors); ++#endif ++#ifdef ENABLE_FB + bool +-primary_plane_initialize(struct primary_plane *plane, struct mode *mode); ++primary_plane_initialize_fb(struct primary_plane *plane, struct mode *mode); + #endif + void + primary_plane_finalize(struct primary_plane *plane); +diff --git a/libswc/screen.c b/libswc/screen.c +index c0f1eb0..54b1dc4 100644 +--- a/libswc/screen.c ++++ b/libswc/screen.c +@@ -22,19 +22,24 @@ + */ + + #include "screen.h" ++#include "backend.h" ++#include "compositor.h" + #ifdef ENABLE_DRM + #include "drm.h" +-#else ++#endif ++#ifdef ENABLE_FB + #include "fb.h" + #endif + #include "event.h" + #include "internal.h" ++#include "layer_shell.h" + #include "mode.h" + #include "output.h" + #ifdef ENABLE_DRM + #include "plane.h" + #endif + #include "pointer.h" ++#include "seat.h" + #include "util.h" + + #include "swc-server-protocol.h" +@@ -66,18 +71,30 @@ swc_screen_set_handler(struct swc_screen *base, + screen->handler_data = data; + } + ++/* the screens of the video backend swc started with */ ++static bool ++create_default_screens(void) ++{ ++#ifdef ENABLE_DRM ++ if (swc.video == SWC_VIDEO_DRM) { ++ return drm_create_screens(&swc.screens); ++ } ++#endif ++#ifdef ENABLE_FB ++ if (swc.video == SWC_VIDEO_FB) { ++ return fb_create_screens(&swc.screens); ++ } ++#endif ++ return false; ++} ++ + bool + screens_initialize(void) + { + wl_list_init(&swc.screens); + +- if (! +-#ifdef ENABLE_DRM +- drm_create_screens(&swc.screens) +-#else +- fb_create_screens(&swc.screens) +-#endif +- ) { ++ if (!(swc.backend->create_screens ? swc.backend->create_screens(&swc.screens) ++ : create_default_screens())) { + return false; + } + +@@ -115,11 +132,7 @@ bind_screen(struct wl_client *client, void *data, uint32_t version, uint32_t id) + } + + struct screen * +-#ifdef ENABLE_DRM + screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane) +-#else +-screen_new(struct output *output) +-#endif + { + struct screen *screen; + int32_t x = 0; +@@ -141,26 +154,33 @@ screen_new(struct output *output) + } + + #ifdef ENABLE_DRM +- screen->crtc = crtc; +- if (!primary_plane_initialize(&screen->planes.primary, +- crtc, +- output->preferred_mode, +- &output->connector, +- 1)) { +- ERROR("Failed to initialize primary plane\n"); +- goto error2; +- } ++ if (swc.video == SWC_VIDEO_DRM) { ++ screen->crtc = crtc; ++ if (!primary_plane_initialize_drm(&screen->planes.primary, ++ crtc, ++ output->preferred_mode, ++ &output->connector, ++ 1)) { ++ ERROR("Failed to initialize primary plane\n"); ++ goto error2; ++ } + +- cursor_plane->screen = screen; +- screen->planes.cursor = cursor_plane; +-#else +- if (!primary_plane_initialize(&screen->planes.primary, +- output->preferred_mode)) { +- ERROR("Failed to initialize primary plane\n"); +- goto error2; +- } +- screen->planes.cursor = NULL; ++ cursor_plane->screen = screen; ++ screen->planes.cursor = cursor_plane; ++ } else ++#endif ++ { ++#ifdef ENABLE_FB ++ if (!primary_plane_initialize_fb(&screen->planes.primary, ++ output->preferred_mode)) { ++ ERROR("Failed to initialize primary plane\n"); ++ goto error2; ++ } + #endif ++ (void)crtc; ++ (void)cursor_plane; ++ screen->planes.cursor = NULL; ++ } + + screen->handler = &null_handler; + wl_signal_init(&screen->destroy_signal); +@@ -199,15 +219,99 @@ screen_destroy(struct screen *screen) + wl_signal_emit(&screen->destroy_signal, NULL); + wl_list_for_each_safe(output, next, &screen->outputs, link) + output_destroy(output); +- primary_plane_finalize(&screen->planes.primary); ++ if (screen->virtual) { ++ view_finalize(&screen->planes.primary.view); ++ } else { ++ primary_plane_finalize(&screen->planes.primary); + #ifdef ENABLE_DRM +- if (screen->planes.cursor) { +- plane_destroy(screen->planes.cursor); +- } ++ if (screen->planes.cursor) { ++ plane_destroy(screen->planes.cursor); ++ } + #endif ++ } + free(screen); + } + ++struct screen * ++screen_new_virtual(struct output *output, const struct view_impl *impl) ++{ ++ struct screen *screen; ++ int32_t x = 0; ++ ++ wl_list_for_each(screen, &swc.screens, link) x = ++ MAX(x, screen->base.geometry.x + screen->base.geometry.width); ++ ++ if (!(screen = calloc(1, sizeof(*screen)))) { ++ return NULL; ++ } ++ ++ screen->global = wl_global_create( ++ swc.display, &swc_screen_interface, 1, screen, &bind_screen); ++ if (!screen->global) { ++ ERROR("Failed to create screen global\n"); ++ free(screen); ++ return NULL; ++ } ++ ++ screen->virtual = true; ++ view_initialize(&screen->planes.primary.view, impl); ++ screen->planes.primary.view.geometry.width = output->preferred_mode->width; ++ screen->planes.primary.view.geometry.height = ++ output->preferred_mode->height; ++ screen->planes.primary.mode = *output->preferred_mode; ++ ++ screen->handler = &null_handler; ++ wl_signal_init(&screen->destroy_signal); ++ wl_list_init(&screen->resources); ++ wl_list_init(&screen->outputs); ++ wl_list_insert(&screen->outputs, &output->link); ++ wl_list_init(&screen->modifiers); ++ ++ view_move(&screen->planes.primary.view, x, 0); ++ screen->base.geometry = screen->planes.primary.view.geometry; ++ screen->base.usable_geometry = screen->base.geometry; ++ ++ swc.manager->new_screen(&screen->base); ++ ++ return screen; ++} ++ ++void ++screen_resize(struct screen *screen, uint32_t width, uint32_t height) ++{ ++ struct output *output; ++ struct screen *other; ++ pixman_region32_t region; ++ ++ screen->planes.primary.view.geometry.width = width; ++ screen->planes.primary.view.geometry.height = height; ++ screen->planes.primary.mode.width = width; ++ screen->planes.primary.mode.height = height; ++ screen->base.geometry.width = width; ++ screen->base.geometry.height = height; ++ ++ wl_list_for_each(output, &screen->outputs, link) ++ output_set_size(output, width, height); ++ ++ compositor_screen_resize(screen); ++ ++ if (swc.seat) { ++ pixman_region32_init(&region); ++ wl_list_for_each(other, &swc.screens, link) ++ pixman_region32_union_rect(&region, &region, ++ other->base.geometry.x, ++ other->base.geometry.y, ++ other->base.geometry.width, ++ other->base.geometry.height); ++ pointer_set_region(swc.seat->pointer, &region); ++ pixman_region32_fini(&region); ++ } ++ ++ /* a resize can leave origin and edges unchanged, notify anyway */ ++ screen->base.usable_geometry.width = 0; ++ screen_update_usable_geometry(screen); ++} ++ + void + screen_update_usable_geometry(struct screen *screen) + { +@@ -239,6 +343,7 @@ screen_update_usable_geometry(struct screen *screen) + screen->base.usable_geometry.width = extents->x2 - extents->x1; + screen->base.usable_geometry.height = extents->y2 - extents->y1; + ++ layer_shell_usable_changed(screen); + if (screen->handler->usable_geometry_changed) { + screen->handler->usable_geometry_changed(screen->handler_data); + } +diff --git a/libswc/screen.h b/libswc/screen.h +index d53b566..ee896ba 100644 +--- a/libswc/screen.h ++++ b/libswc/screen.h +@@ -51,6 +51,8 @@ struct screen { + + struct wl_signal destroy_signal; + uint8_t id; ++ /* no hardware behind it: the backend owns the primary plane */ ++ bool virtual; + #ifdef ENABLE_DRM + uint32_t crtc; + #endif +@@ -73,13 +75,14 @@ screens_initialize(void); + void + screens_finalize(void); + +-#ifdef ENABLE_DRM ++/* crtc and cursor_plane are the drm backend's, the framebuffer's pass 0 and ++ * NULL */ + struct screen * + screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane); +-#else + struct screen * +-screen_new(struct output *output); +-#endif ++screen_new_virtual(struct output *output, const struct view_impl *impl); ++void ++screen_resize(struct screen *screen, uint32_t width, uint32_t height); + void + screen_destroy(struct screen *screen); + +diff --git a/libswc/swc.c b/libswc/swc.c +index e48cd8d..ee131d4 100644 +--- a/libswc/swc.c ++++ b/libswc/swc.c +@@ -27,7 +27,8 @@ + #include "data_device_manager.h" + #ifdef ENABLE_DRM + #include "drm.h" +-#else ++#endif ++#ifdef ENABLE_FB + #include "fb.h" + #endif + #include "event.h" +@@ -36,6 +37,9 @@ + #include "keyboard.h" + #include "launch.h" + #include "layer_shell.h" ++#ifdef ENABLE_NESTED ++#include "nested.h" ++#endif + #include "panel_manager.h" + #include "pointer.h" + #include "screen.h" +@@ -48,6 +52,8 @@ + #include "util.h" + #include "window.h" + #include "xdg_decoration.h" ++#include "xdg_dialog.h" ++#include "cursor_shape.h" + #include "xdg_output.h" + #include "xdg_shell.h" + #ifdef ENABLE_XWAYLAND +@@ -166,6 +172,140 @@ swc_cursor_set_position(int32_t x, int32_t y) + return true; + } + ++EXPORT bool ++swc_button(uint32_t button, uint32_t state) ++{ ++ if (!swc.seat || !swc.seat->pointer) { ++ return false; ++ } ++ ++ pointer_handle_button(swc.seat->pointer, get_time(), button, state); ++ pointer_handle_frame(swc.seat->pointer); ++ ++ return true; ++} ++ ++EXPORT bool ++swc_key(uint32_t key, uint32_t state) ++{ ++ if (!swc.seat || !swc.seat->keyboard) { ++ return false; ++ } ++ ++ keyboard_handle_key(swc.seat->keyboard, get_time(), key, state); ++ ++ return true; ++} ++ ++EXPORT bool ++swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level) ++{ ++ struct xkb_keymap *map; ++ xkb_keycode_t code, max; ++ const xkb_keysym_t *syms; ++ uint32_t l; ++ int i, n; ++ ++ if (!swc.seat || !swc.seat->keyboard) { ++ return false; ++ } ++ ++ map = swc.seat->keyboard->xkb.keymap.map; ++ max = xkb_keymap_max_keycode(map); ++ /* bare keys first */ ++ for (l = 0; l < 2; l++) { ++ for (code = xkb_keymap_min_keycode(map); code <= max; code++) { ++ n = xkb_keymap_key_get_syms_by_level(map, code, 0, l, &syms); ++ for (i = 0; i < n; i++) { ++ if (syms[i] != keysym) { ++ continue; ++ } ++ *key = code - 8; ++ *level = l; ++ return true; ++ } ++ } ++ } ++ ++ return false; ++} ++ ++/* whether SWC_BACKEND leaves the video backend called name open: drm or fb ++ * name one, anything else, or nothing, lets swc try each that was built */ ++static bool ++video_wanted(const char *name) ++{ ++ const char *backend = getenv("SWC_BACKEND"); ++ ++ if (!backend || (strcmp(backend, "drm") != 0 && strcmp(backend, "fb") != 0)) { ++ return true; ++ } ++ return strcmp(backend, name) == 0; ++} ++ ++/* the first video backend that was built, was asked for and comes up */ ++static bool ++video_initialize(void) ++{ ++#ifdef ENABLE_DRM ++ if (video_wanted("drm") && drm_initialize()) { ++ swc.video = SWC_VIDEO_DRM; ++ return true; ++ } ++#endif ++#ifdef ENABLE_FB ++ if (video_wanted("fb") && fb_initialize()) { ++ swc.video = SWC_VIDEO_FB; ++ return true; ++ } ++#endif ++ return false; ++} ++ ++static bool ++backend_initialize(void) ++{ ++#ifdef ENABLE_NESTED ++ if (nested_requested()) { ++ return nested_initialize(); ++ } ++#endif ++ ++ if (!launch_initialize()) { ++ ERROR("Could not connect to swc-launch\n"); ++ return false; ++ } ++ ++ if (!video_initialize()) { ++ ERROR("Could not initialize video backend\n"); ++ launch_finalize(); ++ return false; ++ } ++ ++ return true; ++} ++ ++static void ++backend_finalize(void) ++{ ++ if (swc.backend->finalize) { ++ swc.backend->finalize(); ++ return; ++ } ++ ++#ifdef ENABLE_DRM ++ if (swc.video == SWC_VIDEO_DRM) { ++ drm_finalize(); ++ } ++#endif ++#ifdef ENABLE_FB ++ if (swc.video == SWC_VIDEO_FB) { ++ fb_finalize(); ++ } ++#endif ++ launch_finalize(); ++} ++ + EXPORT bool + swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, + const struct swc_manager *manager) +@@ -177,22 +317,10 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, + const char *default_seat = "seat0"; + wl_signal_init(&swc.event_signal); + +- if (!launch_initialize()) { +- ERROR("Could not connect to swc-launch\n"); ++ if (!backend_initialize()) { + goto error0; + } + +- if (! +-#ifdef ENABLE_DRM +- drm_initialize() +-#else +- fb_initialize() +-#endif +- ) { +- ERROR("Could not initialize video backend\n"); +- goto error1; +- } +- + swc.shm = shm_create(display); + if (!swc.shm) { + ERROR("Could not initialize SHM\n"); +@@ -244,6 +372,12 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, + goto error10; + } + ++ /* optional */ ++ swc.xdg_dialog_manager = xdg_dialog_manager_create(display); ++ ++ /* optional */ ++ swc.cursor_shape_manager = cursor_shape_manager_create(display); ++ + swc.xdg_decoration_manager = xdg_decoration_manager_create(display); + if (!swc.xdg_decoration_manager) { + ERROR("Could not initialize XDG decoration manager\n"); +@@ -295,6 +429,10 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, + + setup_compositor(); + ++ if (swc.backend->start) { ++ swc.backend->start(); ++ } ++ + return true; + + error17: +@@ -330,13 +468,7 @@ error4: + error3: + shm_destroy(swc.shm); + error2: +-#ifdef ENABLE_DRM +- drm_finalize(); +-#else +- fb_finalize(); +-#endif +-error1: +- launch_finalize(); ++ backend_finalize(); + error0: + return false; + } +@@ -361,10 +493,21 @@ swc_finalize(void) + screens_finalize(); + bindings_finalize(); + shm_destroy(swc.shm); +-#ifdef ENABLE_DRM +- drm_finalize(); +-#else +- fb_finalize(); +-#endif +- launch_finalize(); ++ backend_finalize(); ++} ++ ++EXPORT bool ++swc_set_keyboard_layout(const char *layout, const char *variant, ++ const char *options) ++{ ++ struct xkb_rule_names names = { ++ .layout = layout, .variant = variant, .options = options}; ++ ++ return keyboard_set_names(swc.seat->keyboard, &names); ++} ++ ++EXPORT void ++swc_set_keyboard_repeat(int32_t rate, int32_t delay) ++{ ++ keyboard_set_repeat(swc.seat->keyboard, rate, delay); + } +diff --git a/libswc/swc.h b/libswc/swc.h +index fc0fa9a..b6b0c4c 100644 +--- a/libswc/swc.h ++++ b/libswc/swc.h +@@ -58,6 +58,25 @@ swc_cursor_position(int32_t *x, int32_t *y); + bool + swc_cursor_set_position(int32_t x, int32_t y); + ++/** ++ * Feed a button event to the seat, through the bindings. ++ * button is a linux input code. ++ */ ++bool ++swc_button(uint32_t button, uint32_t state); ++ ++/** ++ * Feed a key event to the seat. key is an evdev code. ++ */ ++bool ++swc_key(uint32_t key, uint32_t state); ++ ++/** ++ * Find the key and level (0 bare, 1 shift) that produce keysym. ++ */ ++bool ++swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level); ++ + /** + * Send a pointer button event to the currently focused client. + * +@@ -125,6 +144,74 @@ swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888, + void + swc_clear_cursor_image(enum swc_cursor_kind kind); + ++/** ++ * set the picture of a cursor by its name: "default", "text", "pointer", ++ * "wait", "ew-resize" and the like, the names of CSS and of the ++ * cursor-shape-v1 protocol, which clients ask for by. nothing is drawn ++ * until a name is asked for, so a window manager with a theme gives ++ * every name it has. ++ * ++ * `argb8888` is `width*height` pixels, kept by the caller as long as it ++ * may be used ++ */ ++void ++swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, ++ uint32_t width, uint32_t height, int32_t hotspot_x, ++ int32_t hotspot_y); ++ ++/** ++ * show the cursor of this name where no client has the pointer, over a ++ * window's border for one, NULL for the usual ++ */ ++void ++swc_set_cursor_shape(const char *name); ++ ++/** ++ * be told where the pointer moves, x and y in compositor coordinates, so ++ * a window manager can say what is under it. at most one, NULL to stop ++ */ ++void ++swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), ++ void *data); ++ ++/** ++ * the clipboard, for a compositor that keeps it itself. a client sets the ++ * selection as it does in any compositor, and the handler is told which types ++ * it offers (none when it is gone, the client exited or the selection was ++ * cleared). what the compositor keeps it offers again as a selection of its ++ * own, so the clipboard does not end with the client that copied ++ */ ++struct swc_selection_handler { ++ void (*changed)(void *data, const char *const *mime_types, unsigned count); ++}; ++ ++/* what answers a paste of a selection the compositor offers */ ++struct swc_selection_source { ++ /* write the type to fd, which the caller closes */ ++ void (*send)(void *data, const char *mime_type, int fd); ++ /* another client took the selection */ ++ void (*cancelled)(void *data); ++}; ++ ++void ++swc_selection_set_handler(const struct swc_selection_handler *handler, void *data); ++ ++/** ++ * asks the client that holds the selection for a type, written to fd. ++ * false when no client holds it or it does not offer the type. the caller ++ * closes fd, the client has its own copy ++ */ ++bool ++swc_selection_request(const char *mime_type, int fd); ++ ++/** ++ * takes the selection, offering these types, answered by source. what was ++ * the selection is cancelled ++ */ ++bool ++swc_selection_offer(const char *const *mime_types, unsigned count, ++ const struct swc_selection_source *source, void *data); ++ + /** + * draw [or update] a simple box overlay + * +@@ -267,6 +354,23 @@ struct swc_window_handler { + * the interactive resize will be honored. + */ + void (*resize)(void *data); ++ ++ /** ++ * Called when the window starts or stops blocking its parent. ++ */ ++ void (*modal_changed)(void *data); ++ ++ /** ++ * Called when the window size changes. ++ */ ++ void (*size_changed)(void *data); ++ ++ /** ++ * Called when the window asks to be shown fullscreen, or to stop being. ++ * ++ * The window manager decides, see swc_window_set_fullscreen. ++ */ ++ void (*fullscreen)(void *data, bool fullscreen); + }; + + struct swc_window { +@@ -274,6 +378,8 @@ struct swc_window { + char *app_id; + + struct swc_window *parent; ++ /* the window says it blocks its parent */ ++ bool modal; + uint32_t motion_throttle_ms; + uint32_t min_width; + uint32_t min_height; +@@ -288,6 +394,41 @@ void + swc_window_set_handler(struct swc_window *window, + const struct swc_window_handler *handler, void *data); + ++/* what lies behind a window while the screen is composited: the target's ++ * pixels as drawn so far, pitch bytes a row in a WLD format, the window's ++ * area with the margin asked for (in those pixels, clipped to them), and the ++ * parts of it being repainted now, which every caller may read and write */ ++struct swc_backdrop { ++ void *pixels; ++ uint32_t pitch, format; ++ struct swc_rectangle area; ++ const struct swc_rectangle *rects; ++ int nrects; ++}; ++ ++/* calls hook right before the window is drawn with what lies behind it, so ++ * a window manager can read it or change it: glass, a shade, a lens. the ++ * whole area and margin is drawn again whenever any of it changes, so the ++ * pixels are always complete there. a NULL hook takes it away */ ++void ++swc_window_set_backdrop(struct swc_window *window, uint32_t margin, ++ void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data); ++ ++/* depths: a view at a higher one is drawn over one at a lower. the ++ * shells' layers stand at these, a window at SWC_Z_WINDOW until its manager ++ * says otherwise, and every depth between them is the manager's: floating ++ * windows over tiled ones, a fullscreen window over the panels. a window is ++ * raised to the front of the views at its depth, and its transients share ++ * it */ ++#define SWC_Z_BACKGROUND (-3000) ++#define SWC_Z_BOTTOM (-2000) ++#define SWC_Z_WINDOW 0 ++#define SWC_Z_TOP 2000 ++#define SWC_Z_OVERLAY 3000 ++ ++void ++swc_window_set_z(struct swc_window *window, int32_t z); ++ + /** + * Request that the specified window close. + */ +@@ -344,6 +485,24 @@ swc_window_set_tiled(struct swc_window *window); + void + swc_window_set_fullscreen(struct swc_window *window, struct swc_screen *screen); + ++/** ++ * Make window a transient of parent, or of nothing when NULL. ++ */ ++void ++swc_window_set_parent(struct swc_window *window, struct swc_window *parent); ++ ++/** ++ * An attached transient moves with its parent. ++ */ ++void ++swc_window_set_attached(struct swc_window *window, bool attached); ++ ++/** ++ * Raise the window with its transients. ++ */ ++void ++swc_window_raise(struct swc_window *window); ++ + /** + * Set the window's position. + * +@@ -400,6 +559,27 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, + uint32_t inner_border_width, uint32_t outer_border_color, + uint32_t outer_border_width); + ++/** ++ * What the border goes around. ++ */ ++enum swc_border_placement { ++ /** ++ * The client area only. ++ */ ++ SWC_BORDER_CLIENT, ++ /** ++ * The client area and its decor. ++ */ ++ SWC_BORDER_FRAME, ++}; ++ ++/** ++ * Choose what the border goes around. The default is SWC_BORDER_CLIENT. ++ */ ++void ++swc_window_set_border_placement(struct swc_window *window, ++ enum swc_border_placement placement); ++ + /*window decor things*/ + + /** +@@ -567,6 +747,22 @@ swc_window_stack(struct swc_window *window, int32_t direction); + */ + extern int32_t swc_repeat_rate, swc_repeat_delay; + ++/** ++ * Change the keyboard layout while running: names as XKB's, any of them ++ * NULL for its default. False, the old layout kept, when they make no ++ * keymap. ++ */ ++bool ++swc_set_keyboard_layout(const char *layout, const char *variant, ++ const char *options); ++ ++/** ++ * Change the repeat rate (characters per second) and delay (ms), which ++ * clients are told again. ++ */ ++void ++swc_set_keyboard_repeat(int32_t rate, int32_t delay); ++ + /* Bindings {{{ */ + + enum { +@@ -577,6 +773,22 @@ enum { + SWC_MOD_ANY = ~0 + }; + ++/** ++ * Drag actions, as in wl_data_device_manager.dnd_action. ++ */ ++enum swc_drag_action { ++ SWC_DRAG_COPY = 1, ++ SWC_DRAG_MOVE = 2, ++ SWC_DRAG_ASK = 4, ++}; ++ ++/** ++ * Set the modifiers that ask for an action during a drag, all held. ++ * 0 disables it. Defaults: Ctrl copy, Shift move. ++ */ ++void ++swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers); ++ + enum swc_binding_type { + SWC_BINDING_KEY, + SWC_BINDING_BUTTON, +diff --git a/meson.build b/meson.build +index 356dcbc..79dee81 100644 +--- a/meson.build ++++ b/meson.build +@@ -39,15 +39,20 @@ requires_private = [ + + video_backend = get_option('video') + framebuffer_backend = get_option('fb') ++# 'all' builds the drm and the framebuffer backends together, and swc picks ++# one when it starts ++with_drm = video_backend in ['drm', 'all'] ++with_fb = video_backend in ['fb', 'all'] + drm = disabler() +-if video_backend == 'drm' +- if framebuffer_backend != 'auto' +- error('-Dfb applies only when -Dvideo=fb') ++if with_drm ++ if video_backend == 'drm' and framebuffer_backend != 'auto' ++ error('-Dfb applies only when -Dvideo=fb or -Dvideo=all') + endif + drm = dependency('libdrm') + requires_private += drm + add_project_arguments('-DENABLE_DRM=1', language: 'c') +-elif video_backend == 'fb' ++endif ++if with_fb + if framebuffer_backend == 'auto' + if os == 'linux' + framebuffer_backend = 'fbdev' +@@ -58,6 +63,7 @@ elif video_backend == 'fb' + endif + endif + ++ add_project_arguments('-DENABLE_FB=1', language: 'c') + if framebuffer_backend == 'fbdev' + if os != 'linux' + error('The fbdev framebuffer interface is supported only on Linux') +@@ -78,7 +84,7 @@ elif video_backend == 'fb' + endif + + if video_backend != 'drm' and get_option('xwayland').enabled() +- error('Xwayland requires the DRM video backend') ++ error('Xwayland requires the DRM video backend alone') + endif + xwayland = video_backend == 'drm' \ + ? dependency('xwayland', required: get_option('xwayland')) \ +@@ -124,6 +130,20 @@ if input_backend == 'libinput' + requires_private += [libinput, udev] + endif + ++# the nested backend replaces the display and the input devices at run ++# time, so it rides along in any build. it feeds the libinput seat, the ++# wscons one has no virtual input path ++wayland_client = dependency('wayland-client', required: get_option('nested')) ++have_nested = (wayland_client.found() and input_backend == 'libinput' ++ and cc.has_header('wld/wayland.h', dependencies: dependency('wld'))) ++if get_option('nested').enabled() and not have_nested ++ error('The nested backend needs wayland-client, the libinput input backend, and wld built with Wayland support') ++endif ++if have_nested ++ add_project_arguments('-DENABLE_NESTED=1', language: 'c') ++ requires_private += wayland_client ++endif ++ + subdir('protocol') + subdir('cursor') + subdir('launch') +@@ -139,7 +159,8 @@ endif + + summary({ + 'Xwayland support': xwayland.found(), ++ 'Nested backend': have_nested, + 'Input backend': input_backend, + 'Video backend': video_backend, +- 'Framebuffer interface': video_backend == 'fb' ? framebuffer_backend : 'n/a', ++ 'Framebuffer interface': with_fb ? framebuffer_backend : 'n/a', + }, bool_yn: true) +diff --git a/meson.options b/meson.options +index 98a42a1..09702bd 100644 +--- a/meson.options ++++ b/meson.options +@@ -1,9 +1,10 @@ + option('xwayland', type: 'feature', value: 'auto', description: 'Automatic Xwayland handling') + option('input', type: 'combo', value: 'auto', choices: ['auto', 'libinput', 'wscons'], description: 'Input backend to use') +-option('video', type: 'combo', value: 'drm', choices: ['drm', 'fb'], description: 'Video backend to use') ++option('video', type: 'combo', value: 'drm', choices: ['drm', 'fb', 'all'], description: 'Video backend to use: all builds drm and the framebuffer and picks one at run time') + option('fb', type: 'combo', value: 'auto', choices: ['auto', 'fbdev', 'wsdisplay'], description: 'Framebuffer interface to use') + option('fbdev-device', type: 'string', value: '/dev/fb0', description: 'Linux framebuffer device') + option('wsdisplay-device', type: 'string', value: '', description: 'wsdisplay framebuffer device (empty uses the VT selected by swc-launch)') + option('udev', type: 'feature', value: 'auto', description: 'libinput libudev support') ++option('nested', type: 'feature', value: 'auto', description: 'Backend that runs swc inside another Wayland compositor') + option('extra', type: 'boolean', value: true, description: 'Build swc utility applications') + option('example', type: 'boolean', value: false, description: 'Build swc example compositor') +diff --git a/protocol/meson.build b/protocol/meson.build +index c6a0e63..02129b3 100644 +--- a/protocol/meson.build ++++ b/protocol/meson.build +@@ -13,6 +13,10 @@ foreach proto : [ + wl.find_protocol('linux-dmabuf', state: 'unstable', version: 1), + wl.find_protocol('xdg-decoration', state: 'unstable', version: 1), + wl.find_protocol('xdg-output', state: 'unstable', version: 1), ++ wl.find_protocol('xdg-dialog', state: 'staging', version: 1), ++ wl.find_protocol('keyboard-shortcuts-inhibit', state: 'unstable', version: 1), ++ wl.find_protocol('cursor-shape', state: 'staging', version: 1), ++ wl.find_protocol('tablet', state: 'unstable', version: 2), + ] + protocols = wl.scan_xml(proto, + public: true, diff --git a/patches/cursorShapes.patch b/patches/cursorShapes.patch @@ -1,477 +0,0 @@ -From 58f66a46347a728e9dff4ccc683398afcc4cdeb9 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 24 Nov 2026 09:19:53 -0300 -Subject: [PATCH] cursors by name: a window manager gives pictures, clients ask - with cursor-shape-v1 - -The pointer's pictures were a few the compositor kept. A window manager -can now hand over a picture for each name a cursor has (the names of -CSS and of cursor-shape-v1), and a client that asks for a shape by -name, as toolkits do, is shown that picture. the window manager can -show a name where no client has the pointer, over a window's border, -and is told where the pointer moves to know which. ---- - libswc/compositor.c | 2 + - libswc/cursor_shape.c | 146 ++++++++++++++++++++++++++++++++++++++++++ - libswc/cursor_shape.h | 11 ++++ - libswc/internal.h | 1 + - libswc/meson.build | 1 + - libswc/pointer.c | 139 +++++++++++++++++++++++++++++++++++++++- - libswc/pointer.h | 4 ++ - libswc/swc.c | 4 ++ - libswc/swc.h | 30 +++++++++ - 9 files changed, 337 insertions(+), 1 deletion(-) - create mode 100644 libswc/cursor_shape.c - create mode 100644 libswc/cursor_shape.h - -diff --git a/libswc/compositor.c b/libswc/compositor.c -index 7c3ec4d..03433ab 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -1987,6 +1987,8 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, - return false; - } - -+ pointer_motion_notify(x, y); -+ - struct compositor_view *chrome = compositor_chrome_at(x, y); - struct compositor_view *view = chrome ? NULL : compositor_view_at(x, y); - -diff --git a/libswc/cursor_shape.c b/libswc/cursor_shape.c -new file mode 100644 -index 0000000..28e5a78 ---- /dev/null -+++ b/libswc/cursor_shape.c -@@ -0,0 +1,146 @@ -+/* cursor-shape-v1 */ -+ -+#include "cursor_shape.h" -+#include "internal.h" -+#include "pointer.h" -+#include "seat.h" -+#include "util.h" -+ -+#include <wayland-server.h> -+#include "cursor-shape-v1-server-protocol.h" -+ -+/* the names of the shapes, the ones of CSS: what a window manager that has a theme gives pictures for */ -+static const char *const names[] = { -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT] = "default", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CONTEXT_MENU] = "context-menu", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP] = "help", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER] = "pointer", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS] = "progress", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT] = "wait", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL] = "cell", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR] = "crosshair", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT] = "text", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT] = "vertical-text", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS] = "alias", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY] = "copy", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE] = "move", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP] = "no-drop", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED] = "not-allowed", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB] = "grab", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING] = "grabbing", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE] = "e-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE] = "n-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE] = "ne-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE] = "nw-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE] = "s-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE] = "se-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE] = "sw-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE] = "w-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE] = "ew-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE] = "ns-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE] = "nesw-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE] = "nwse-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE] = "col-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE] = "row-resize", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_SCROLL] = "all-scroll", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN] = "zoom-in", -+ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT] = "zoom-out", -+}; -+ -+#define NAMES (sizeof names / sizeof names[0]) -+ -+static void -+pointer_set_shape_request(struct wl_client *client, struct wl_resource *resource, -+ uint32_t serial, uint32_t shape) -+{ -+ struct pointer *pointer = wl_resource_get_user_data(resource); -+ -+ (void)serial; -+ if (shape < 1 || shape >= NAMES || !names[shape]) { -+ wl_resource_post_error(resource, WP_CURSOR_SHAPE_DEVICE_V1_ERROR_INVALID_SHAPE, -+ "no such shape: %u", shape); -+ return; -+ } -+ /* only what has the pointer says what it looks like */ -+ if (!pointer || client != pointer->focus.client) { -+ return; -+ } -+ pointer_set_shape(pointer, names[shape]); -+} -+ -+static const struct wp_cursor_shape_device_v1_interface pointer_device_impl = { -+ .destroy = destroy_resource, -+ .set_shape = pointer_set_shape_request, -+}; -+ -+/* a tablet tool has no pointer here: it is answered and does nothing */ -+static void -+tool_set_shape_request(struct wl_client *client, struct wl_resource *resource, -+ uint32_t serial, uint32_t shape) -+{ -+ (void)client; -+ (void)serial; -+ (void)shape; -+ (void)resource; -+} -+ -+static const struct wp_cursor_shape_device_v1_interface tool_device_impl = { -+ .destroy = destroy_resource, -+ .set_shape = tool_set_shape_request, -+}; -+ -+static void -+get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id, -+ struct wl_resource *pointer_resource) -+{ -+ struct wl_resource *device = wl_resource_create( -+ client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); -+ -+ if (!device) { -+ wl_resource_post_no_memory(resource); -+ return; -+ } -+ wl_resource_set_implementation(device, &pointer_device_impl, -+ wl_resource_get_user_data(pointer_resource), NULL); -+} -+ -+static void -+get_tablet_tool_v2(struct wl_client *client, struct wl_resource *resource, uint32_t id, -+ struct wl_resource *tool) -+{ -+ struct wl_resource *device = wl_resource_create( -+ client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); -+ -+ (void)tool; -+ if (!device) { -+ wl_resource_post_no_memory(resource); -+ return; -+ } -+ wl_resource_set_implementation(device, &tool_device_impl, NULL, NULL); -+} -+ -+static const struct wp_cursor_shape_manager_v1_interface manager_impl = { -+ .destroy = destroy_resource, -+ .get_pointer = get_pointer, -+ .get_tablet_tool_v2 = get_tablet_tool_v2, -+}; -+ -+static void -+bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) -+{ -+ struct wl_resource *resource; -+ -+ (void)data; -+ resource = wl_resource_create(client, &wp_cursor_shape_manager_v1_interface, version, id); -+ if (!resource) { -+ wl_client_post_no_memory(client); -+ return; -+ } -+ wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); -+} -+ -+struct wl_global * -+cursor_shape_manager_create(struct wl_display *display) -+{ -+ return wl_global_create(display, &wp_cursor_shape_manager_v1_interface, 1, NULL, &bind_manager); -+} -diff --git a/libswc/cursor_shape.h b/libswc/cursor_shape.h -new file mode 100644 -index 0000000..be8ebb4 ---- /dev/null -+++ b/libswc/cursor_shape.h -@@ -0,0 +1,11 @@ -+/* cursor-shape-v1: a client asks for the pointer's picture by name */ -+ -+#ifndef SWC_CURSOR_SHAPE_H -+#define SWC_CURSOR_SHAPE_H -+ -+struct wl_display; -+ -+struct wl_global * -+cursor_shape_manager_create(struct wl_display *display); -+ -+#endif -diff --git a/libswc/internal.h b/libswc/internal.h -index ce62c60..31fdd47 100644 ---- a/libswc/internal.h -+++ b/libswc/internal.h -@@ -68,6 +68,7 @@ struct swc { - struct wl_global *subcompositor; - struct wl_global *xdg_decoration_manager; - struct wl_global *xdg_dialog_manager; -+ struct wl_global *cursor_shape_manager; - struct wl_global *xdg_output_manager; - struct wl_global *xdg_shell; - -diff --git a/libswc/meson.build b/libswc/meson.build -index f506b5c..e1ef27b 100644 ---- a/libswc/meson.build -+++ b/libswc/meson.build -@@ -3,6 +3,7 @@ install_headers('swc.h') - libswc_src = files( - 'bindings.c', - 'compositor.c', -+ 'cursor_shape.c', - 'data.c', - 'data_device.c', - 'data_device_manager.c', -diff --git a/libswc/pointer.c b/libswc/pointer.c -index 662d8d3..9e3fa4b 100644 ---- a/libswc/pointer.c -+++ b/libswc/pointer.c -@@ -42,6 +42,19 @@ - #include <string.h> - #include <wld/wld.h> - -+/* the pictures of cursors by name, and the one the window manager shows */ -+#define SHAPES 64 -+struct shape { -+ char name[32]; -+ const uint32_t *data; -+ uint32_t width, height; -+ int32_t hotspot_x, hotspot_y; -+}; -+static struct shape shapes[SHAPES]; -+static char shape_override[32]; -+static void (*motion_handler)(void *data, int32_t x, int32_t y); -+static void *motion_data; -+ - static enum swc_cursor_kind cursor_override = SWC_CURSOR_DEFAULT; - static enum swc_cursor_mode cursor_mode = SWC_CURSOR_MODE_CLIENT; - -@@ -367,6 +380,117 @@ swc_clear_cursor_image(enum swc_cursor_kind kind) - apply_cursor_override(pointer); - } - -+static const struct shape * -+shape_named(const char *name) -+{ -+ if (!name || !*name) { -+ return NULL; -+ } -+ for (int i = 0; i < SHAPES; i++) { -+ if (shapes[i].data && !strcmp(shapes[i].name, name)) { -+ return &shapes[i]; -+ } -+ } -+ return NULL; -+} -+ -+EXPORT void -+swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, -+ uint32_t width, uint32_t height, int32_t hotspot_x, -+ int32_t hotspot_y) -+{ -+ int free_slot = -1; -+ -+ if (!name || !*name || strlen(name) >= sizeof shapes[0].name || !argb8888 || -+ !width || !height) { -+ return; -+ } -+ for (int i = 0; i < SHAPES; i++) { -+ if (shapes[i].data && !strcmp(shapes[i].name, name)) { -+ free_slot = i; -+ break; -+ } -+ if (!shapes[i].data && free_slot < 0) { -+ free_slot = i; -+ } -+ } -+ if (free_slot < 0) { -+ return; -+ } -+ strcpy(shapes[free_slot].name, name); -+ shapes[free_slot].data = argb8888; -+ shapes[free_slot].width = width; -+ shapes[free_slot].height = height; -+ shapes[free_slot].hotspot_x = hotspot_x; -+ shapes[free_slot].hotspot_y = hotspot_y; -+} -+ -+EXPORT void -+swc_set_cursor_shape(const char *name) -+{ -+ struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; -+ -+ if (!name) { -+ name = ""; -+ } -+ if (!strcmp(shape_override, name) || strlen(name) >= sizeof shape_override) { -+ return; -+ } -+ strcpy(shape_override, name); -+ apply_cursor_override(pointer); -+} -+ -+EXPORT void -+swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), -+ void *data) -+{ -+ motion_handler = handler; -+ motion_data = data; -+} -+ -+void -+pointer_motion_notify(int32_t x, int32_t y) -+{ -+ if (motion_handler) { -+ motion_handler(motion_data, x, y); -+ } -+} -+ -+/* the cursor a client asked for by name: its picture, drawn by the compositor, where the client's -+ * own cursor surface would be */ -+void -+pointer_set_shape(struct pointer *pointer, const char *name) -+{ -+ const struct shape *shape = shape_named(name); -+ struct cursor shape_cursor; -+ union wld_object object; -+ struct wld_buffer *buffer; -+ -+ if (!shape && !(shape = shape_named("default"))) { -+ return; -+ } -+ if (pointer->cursor.internal_buffer) { -+ wld_buffer_unreference(pointer->cursor.internal_buffer); -+ pointer->cursor.internal_buffer = NULL; -+ } -+ drop_client_cursor_surface(pointer); -+ shape_cursor.width = (int)shape->width; -+ shape_cursor.height = (int)shape->height; -+ object.ptr = (void *)shape->data; -+ buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, -+ shape_cursor.width, shape_cursor.height, -+ WLD_FORMAT_ARGB8888, shape_cursor.width * 4); -+ if (!buffer) { -+ WARNING("Failed to create cursor buffer\n"); -+ return; -+ } -+ pointer->cursor.internal_buffer = buffer; -+ pointer->cursor.hotspot.x = shape->hotspot_x; -+ pointer->cursor.hotspot.y = shape->hotspot_y; -+ update_cursor(pointer); -+ view_attach(&pointer->cursor.view, buffer); -+} -+ - void - pointer_set_cursor(struct pointer *pointer, uint32_t id) - { -@@ -375,7 +499,20 @@ pointer_set_cursor(struct pointer *pointer, uint32_t id) - union wld_object object = {.ptr = &cursor_data[cursor->offset]}; - struct wld_buffer *buffer; - -- if (id == cursor_left_ptr) { -+ if (id == cursor_left_ptr && shape_named(shape_override)) { -+ /* the window manager's, where no client has the pointer */ -+ static struct cursor shape_cursor; -+ const struct shape *shape = shape_named(shape_override); -+ -+ shape_cursor.width = (int)shape->width; -+ shape_cursor.height = (int)shape->height; -+ shape_cursor.hotspot_x = shape->hotspot_x; -+ shape_cursor.hotspot_y = shape->hotspot_y; -+ shape_cursor.offset = 0; -+ cursor = &shape_cursor; -+ data = shape->data; -+ object.ptr = (void *)data; -+ } else if (id == cursor_left_ptr) { - enum swc_cursor_kind kind = cursor_override; - if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) { - kind = SWC_CURSOR_DEFAULT; -diff --git a/libswc/pointer.h b/libswc/pointer.h -index 21a54cd..c6e2126 100644 ---- a/libswc/pointer.h -+++ b/libswc/pointer.h -@@ -86,6 +86,10 @@ void - pointer_set_region(struct pointer *pointer, pixman_region32_t *region); - void - pointer_set_cursor(struct pointer *pointer, uint32_t id); -+void -+pointer_set_shape(struct pointer *pointer, const char *name); -+void -+pointer_motion_notify(int32_t x, int32_t y); - - struct button * - pointer_get_button(struct pointer *pointer, uint32_t serial); -diff --git a/libswc/swc.c b/libswc/swc.c -index d6570b8..ee131d4 100644 ---- a/libswc/swc.c -+++ b/libswc/swc.c -@@ -53,6 +53,7 @@ - #include "window.h" - #include "xdg_decoration.h" - #include "xdg_dialog.h" -+#include "cursor_shape.h" - #include "xdg_output.h" - #include "xdg_shell.h" - #ifdef ENABLE_XWAYLAND -@@ -374,6 +375,9 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, - /* optional */ - swc.xdg_dialog_manager = xdg_dialog_manager_create(display); - -+ /* optional */ -+ swc.cursor_shape_manager = cursor_shape_manager_create(display); -+ - swc.xdg_decoration_manager = xdg_decoration_manager_create(display); - if (!swc.xdg_decoration_manager) { - ERROR("Could not initialize XDG decoration manager\n"); -diff --git a/libswc/swc.h b/libswc/swc.h -index 32f6c48..5b7e04e 100644 ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -144,6 +144,36 @@ swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888, - void - swc_clear_cursor_image(enum swc_cursor_kind kind); - -+/** -+ * set the picture of a cursor by its name: "default", "text", "pointer", -+ * "wait", "ew-resize" and the like, the names of CSS and of the -+ * cursor-shape-v1 protocol, which clients ask for by. nothing is drawn -+ * until a name is asked for, so a window manager with a theme gives -+ * every name it has. -+ * -+ * `argb8888` is `width*height` pixels, kept by the caller as long as it -+ * may be used -+ */ -+void -+swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, -+ uint32_t width, uint32_t height, int32_t hotspot_x, -+ int32_t hotspot_y); -+ -+/** -+ * show the cursor of this name where no client has the pointer, over a -+ * window's border for one, NULL for the usual -+ */ -+void -+swc_set_cursor_shape(const char *name); -+ -+/** -+ * be told where the pointer moves, x and y in compositor coordinates, so -+ * a window manager can say what is under it. at most one, NULL to stop -+ */ -+void -+swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), -+ void *data); -+ - /** - * the clipboard, for a compositor that keeps it itself. a client sets the - * selection as it does in any compositor, and the handler is told which types --- -2.56.1 - diff --git a/patches/dragAndDrop.patch b/patches/dragAndDrop.patch @@ -1,515 +0,0 @@ -From 226d997cdb8ffa6bed5c0e155e571e27a9c997d2 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 17 Nov 2026 14:20:43 -0300 -Subject: [PATCH 12/19] data_device: implement start_drag - ---- - libswc/compositor.c | 13 +-- - libswc/compositor.h | 7 ++ - libswc/data.c | 80 +++++++++++++-- - libswc/data.h | 8 ++ - libswc/data_device.c | 234 ++++++++++++++++++++++++++++++++++++++++++- - 5 files changed, 326 insertions(+), 16 deletions(-) - -diff --git a/libswc/compositor.c b/libswc/compositor.c -index c6238ad..08fe096 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -1174,8 +1174,8 @@ restack_view_for_layer(struct compositor_view *view, bool raise) - wl_list_insert(insert_after, &view->link); - } - --static struct compositor_view * --view_at(int32_t x, int32_t y) -+struct compositor_view * -+compositor_view_at(int32_t x, int32_t y) - { - struct compositor_view *view; - struct swc_rectangle *geom; -@@ -1183,7 +1183,7 @@ view_at(int32_t x, int32_t y) - - wl_list_for_each(view, &compositor.views, link) - { -- if (!view->visible) { -+ if (!view->visible || view->no_input) { - continue; - } - -@@ -1314,7 +1314,7 @@ compositor_view_set_stack_layer(struct compositor_view *view, uint32_t layer, - EXPORT struct swc_window * - swc_window_at(int32_t x, int32_t y) - { -- struct compositor_view *view = window_view(view_at(x, y)); -+ struct compositor_view *view = window_view(compositor_view_at(x, y)); - - return view ? &view->window->base : NULL; - } -@@ -1441,6 +1441,7 @@ compositor_create_view(struct surface *surface) - view->buffer_offset_y = 0; - view->visible = false; - view->always_top = false; -+ view->no_input = false; - view->stack_layer = STACK_LAYER_NORMAL; - view->extents.x1 = 0; - view->extents.y1 = 0; -@@ -1864,7 +1865,7 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, - return false; - } - -- struct compositor_view *view = view_at(x, y); -+ struct compositor_view *view = compositor_view_at(x, y); - - pointer_set_focus(swc.seat->pointer, view); - -@@ -1885,7 +1886,7 @@ handle_button(struct pointer_handler *handler, uint32_t time, - - int32_t x = wl_fixed_to_int(swc.seat->pointer->x); - int32_t y = wl_fixed_to_int(swc.seat->pointer->y); -- struct compositor_view *view = view_at(x, y); -+ struct compositor_view *view = compositor_view_at(x, y); - - pointer_set_focus(swc.seat->pointer, view); - raise_window(view); -diff --git a/libswc/compositor.h b/libswc/compositor.h -index 678c362..8040a00 100644 ---- a/libswc/compositor.h -+++ b/libswc/compositor.h -@@ -80,6 +80,9 @@ struct compositor_view { - /* Whether or not to make it always be on top of other windows */ - bool always_top; - -+ /* a drag icon, never under the pointer */ -+ bool no_input; -+ - /* Global stacking layer for this view */ - uint32_t stack_layer; - -@@ -138,6 +141,10 @@ void - compositor_view_restack(struct compositor_view *view, - struct compositor_view *sibling, bool above); - -+/* topmost view taking input at x, y, or NULL */ -+struct compositor_view * -+compositor_view_at(int32_t x, int32_t y); -+ - void - compositor_view_show(struct compositor_view *view); - void -diff --git a/libswc/data.c b/libswc/data.c -index 8c34b83..903cf10 100644 ---- a/libswc/data.c -+++ b/libswc/data.c -@@ -33,8 +33,33 @@ struct data { - struct wl_array mime_types; - struct wl_resource *source; - struct wl_list offers; -+ -+ /* drag state: source actions, target actions, agreed action */ -+ uint32_t source_actions, action; -+ bool accepted; - }; - -+/* action both allow: target preference, then copy, move, ask */ -+static uint32_t -+agree(uint32_t both, uint32_t preferred) -+{ -+ static const uint32_t order[] = { -+ WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, -+ WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE, -+ WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK, -+ }; -+ -+ if (preferred & both) { -+ return preferred; -+ } -+ for (size_t i = 0; i < sizeof(order) / sizeof(order[0]); i++) { -+ if (order[i] & both) { -+ return order[i]; -+ } -+ } -+ return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; -+} -+ - static void - offer_accept(struct wl_client *client, struct wl_resource *offer, - uint32_t serial, const char *mime_type) -@@ -46,6 +71,7 @@ offer_accept(struct wl_client *client, struct wl_resource *offer, - return; - } - -+ data->accepted = mime_type != NULL; - wl_data_source_send_target(data->source, mime_type); - } - -@@ -64,22 +90,32 @@ offer_receive(struct wl_client *client, struct wl_resource *offer, - close(fd); - } - -+/* drop finished: the source may release its data, delete it after a move */ - static void - offer_finish(struct wl_client *client, struct wl_resource *offer) - { -+ struct data *data = wl_resource_get_user_data(offer); -+ - (void)client; -- (void)offer; -- /* TODO: Implement */ -+ if (data && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { -+ wl_data_source_send_dnd_finished(data->source); -+ } - } - - static void - offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t dnd_actions, uint32_t preferred_action) - { -+ struct data *data = wl_resource_get_user_data(offer); -+ - (void)client; -- (void)offer; -- (void)dnd_actions; -- (void)preferred_action; -- /* TODO: Implement */ -+ if (!data) { -+ return; -+ } -+ data->action = agree(data->source_actions & dnd_actions, preferred_action); -+ wl_data_offer_send_action(offer, data->action); -+ if (wl_resource_get_version(data->source) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION) { -+ wl_data_source_send_action(data->source, data->action); -+ } - } - - static const struct wl_data_offer_interface data_offer_impl = { -@@ -117,10 +153,10 @@ error0: - static void - source_set_actions(struct wl_client *client, struct wl_resource *resource, uint32_t dnd_actions) - { -+ struct data *data = wl_resource_get_user_data(resource); -+ - (void)client; -- (void)resource; -- (void)dnd_actions; -- /* TODO: Implement */ -+ data->source_actions = dnd_actions; - } - - static const struct wl_data_source_interface data_source_impl = { -@@ -167,6 +203,8 @@ data_source_new(struct wl_client *client, uint32_t version, uint32_t id) - } - wl_array_init(&data->mime_types); - wl_list_init(&data->offers); -+ data->source_actions = data->action = 0; -+ data->accepted = false; - - data->source = - wl_resource_create(client, &wl_data_source_interface, version, id); -@@ -211,3 +249,27 @@ data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) - wl_array_for_each(mime_type, &data->mime_types) - wl_data_offer_send_offer(offer, *mime_type); - } -+ -+void -+data_drag_enter(struct wl_resource *source, struct wl_resource *offer) -+{ -+ struct data *data = wl_resource_get_user_data(source); -+ -+ data->accepted = false; -+ data->action = 0; -+ if (wl_resource_get_version(offer) >= WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) { -+ wl_data_offer_send_source_actions(offer, data->source_actions); -+ } -+} -+ -+bool -+data_drag_droppable(struct wl_resource *source) -+{ -+ struct data *data = wl_resource_get_user_data(source); -+ -+ if (!data->accepted) { -+ return false; -+ } -+ /* no actions before version 3, a drop copies */ -+ return wl_resource_get_version(source) < WL_DATA_SOURCE_ACTION_SINCE_VERSION || data->action; -+} -diff --git a/libswc/data.h b/libswc/data.h -index c99f274..ab1e50b 100644 ---- a/libswc/data.h -+++ b/libswc/data.h -@@ -24,6 +24,7 @@ - #ifndef SWC_DATA_H - #define SWC_DATA_H - -+#include <stdbool.h> - #include <stdint.h> - - struct wl_client; -@@ -36,4 +37,11 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, - void - data_send_mime_types(struct wl_resource *source, struct wl_resource *offer); - -+/* a drag offer for source entered a client, nothing accepted yet */ -+void -+data_drag_enter(struct wl_resource *source, struct wl_resource *offer); -+/* target took a type and an action both allow */ -+bool -+data_drag_droppable(struct wl_resource *source); -+ - #endif -diff --git a/libswc/data_device.c b/libswc/data_device.c -index f286c37..8cec18b 100644 ---- a/libswc/data_device.c -+++ b/libswc/data_device.c -@@ -22,9 +22,202 @@ - */ - - #include "data_device.h" -+#include "compositor.h" - #include "data.h" - #include "event.h" -+#include "internal.h" -+#include "pointer.h" -+#include "seat.h" -+#include "surface.h" - #include "util.h" -+#include "view.h" -+ -+#include <string.h> -+ -+/* drag and drop, one drag at a time, holding the pointer until the buttons are released */ -+static struct drag { -+ bool active; -+ struct data_device *device; -+ struct wl_client *client; /* starter */ -+ struct wl_resource *source; /* NULL for a drag inside one client */ -+ struct compositor_view *icon; /* the icon */ -+ struct compositor_view *focus; /* the view it is over */ -+ struct wl_resource *target; /* its client's data device */ -+ struct pointer_handler handler; -+ struct wl_listener source_destroy, focus_destroy, icon_destroy; -+} drag; -+ -+static bool drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state); -+ -+static void -+drag_unfocus(void) -+{ -+ if (drag.focus) { -+ wl_list_remove(&drag.focus_destroy.link); -+ } -+ drag.focus = NULL; -+ drag.target = NULL; -+} -+ -+static void -+drag_leave(void) -+{ -+ if (drag.target) { -+ wl_data_device_send_leave(drag.target); -+ } -+ drag_unfocus(); -+} -+ -+static wl_fixed_t -+local(int32_t at, int32_t origin, int32_t offset) -+{ -+ return wl_fixed_from_int(at - origin + offset); -+} -+ -+static void -+drag_enter(struct compositor_view *view, int32_t x, int32_t y) -+{ -+ struct wl_client *client; -+ struct wl_resource *device, *offer = NULL; -+ -+ if (view == drag.focus) { -+ return; -+ } -+ drag_leave(); -+ if (!view || !view->surface) { -+ return; -+ } -+ client = wl_resource_get_client(view->surface->resource); -+ /* a drag with no source stays inside the client that made it */ -+ if (!drag.source && client != drag.client) { -+ return; -+ } -+ device = wl_resource_find_for_client(&drag.device->resources, client); -+ if (!device) { -+ return; -+ } -+ if (drag.source && (offer = data_offer_new(client, drag.source, wl_resource_get_version(device)))) { -+ wl_data_device_send_data_offer(device, offer); -+ data_send_mime_types(drag.source, offer); -+ data_drag_enter(drag.source, offer); -+ } -+ drag.focus = view; -+ drag.target = device; -+ wl_signal_add(&view->destroy_signal, &drag.focus_destroy); -+ wl_data_device_send_enter(device, wl_display_next_serial(swc.display), view->surface->resource, -+ local(x, view->base.geometry.x, view->buffer_offset_x), -+ local(y, view->base.geometry.y, view->buffer_offset_y), offer); -+} -+ -+static void -+drag_end(void) -+{ -+ struct pointer *pointer = swc.seat->pointer; -+ -+ drag_leave(); -+ wl_list_remove(&drag.handler.link); -+ if (drag.source) { -+ wl_list_remove(&drag.source_destroy.link); -+ } -+ if (drag.icon) { -+ wl_list_remove(&drag.icon_destroy.link); -+ compositor_view_destroy(drag.icon); -+ } -+ memset(&drag, 0, sizeof(drag)); -+ /* held buttons are swallowed as they go up */ -+ drag.handler.button = drag_swallow; -+ /* the pointer goes back to what is under it */ -+ pointer_set_focus(pointer, compositor_view_at(wl_fixed_to_int(pointer->x), wl_fixed_to_int(pointer->y))); -+} -+ -+static void -+drag_drop(void) -+{ -+ bool take = drag.target && (!drag.source || data_drag_droppable(drag.source)); -+ -+ if (!take) { -+ if (drag.source) { -+ wl_data_source_send_cancelled(drag.source); -+ } -+ drag_end(); -+ return; -+ } -+ wl_data_device_send_drop(drag.target); -+ if (drag.source && wl_resource_get_version(drag.source) >= WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) { -+ wl_data_source_send_dnd_drop_performed(drag.source); -+ } -+ /* the target keeps its offer, no leave follows */ -+ drag_unfocus(); -+ drag_end(); -+} -+ -+static bool -+drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state) -+{ -+ (void)handler; -+ (void)time; -+ (void)button; -+ (void)state; -+ return true; -+} -+ -+static bool -+drag_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, wl_fixed_t fy) -+{ -+ int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy); -+ -+ (void)handler; -+ if (drag.icon) { -+ view_move(&drag.icon->base, x, y); -+ } -+ drag_enter(compositor_view_at(x, y), x, y); -+ if (drag.target) { -+ wl_data_device_send_motion(drag.target, time, -+ local(x, drag.focus->base.geometry.x, drag.focus->buffer_offset_x), -+ local(y, drag.focus->base.geometry.y, drag.focus->buffer_offset_y)); -+ } -+ return true; -+} -+ -+static bool -+drag_button(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state) -+{ -+ (void)handler; -+ (void)time; -+ (void)button; -+ /* the drop comes when the last button held is let go */ -+ if (state == WL_POINTER_BUTTON_STATE_RELEASED && swc.seat->pointer->buttons.size <= sizeof(struct button)) { -+ drag_drop(); -+ } -+ return true; -+} -+ -+static void -+handle_drag_source_destroy(struct wl_listener *listener, void *data) -+{ -+ (void)listener; -+ (void)data; -+ drag.source = NULL; -+ drag_end(); -+} -+ -+static void -+handle_drag_focus_destroy(struct wl_listener *listener, void *data) -+{ -+ (void)data; -+ wl_list_remove(&listener->link); -+ drag.focus = NULL; -+ drag.target = NULL; -+} -+ -+static void -+handle_drag_icon_destroy(struct wl_listener *listener, void *data) -+{ -+ (void)listener; -+ (void)data; -+ compositor_view_destroy(drag.icon); -+ drag.icon = NULL; -+} - - static void - start_drag(struct wl_client *client, struct wl_resource *resource, -@@ -32,7 +225,46 @@ start_drag(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *origin_resource, - struct wl_resource *icon_resource, uint32_t serial) - { -- /* XXX: Implement */ -+ struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; -+ int32_t x, y; -+ struct button *button; -+ -+ (void)origin_resource; -+ (void)serial; -+ /* a drag needs a button held, and one at a time */ -+ if (drag.active || !pointer || !pointer->buttons.size) { -+ if (source_resource) { -+ wl_data_source_send_cancelled(source_resource); -+ } -+ return; -+ } -+ x = wl_fixed_to_int(pointer->x); -+ y = wl_fixed_to_int(pointer->y); -+ drag.active = true; -+ drag.device = wl_resource_get_user_data(resource); -+ drag.client = client; -+ drag.source = source_resource; -+ drag.focus_destroy.notify = handle_drag_focus_destroy; -+ if (drag.source) { -+ drag.source_destroy.notify = handle_drag_source_destroy; -+ wl_resource_add_destroy_listener(drag.source, &drag.source_destroy); -+ } -+ if (icon_resource && (drag.icon = compositor_create_view(wl_resource_get_user_data(icon_resource)))) { -+ drag.icon->no_input = true; -+ drag.icon->always_top = true; -+ compositor_view_set_stack_layer(drag.icon, STACK_LAYER_OVERLAY, true); -+ view_move(&drag.icon->base, x, y); -+ compositor_view_show(drag.icon); -+ drag.icon_destroy.notify = handle_drag_icon_destroy; -+ wl_resource_add_destroy_listener(icon_resource, &drag.icon_destroy); -+ } -+ /* the pointer is the drag's: no client sees it until the drop */ -+ pointer_set_focus(pointer, NULL); -+ drag.handler.motion = drag_motion; -+ drag.handler.button = drag_button; -+ wl_list_insert(&pointer->handlers, &drag.handler.link); -+ wl_array_for_each(button, &pointer->buttons) button->handler = &drag.handler; -+ drag_enter(compositor_view_at(x, y), x, y); - } - - static void --- -2.56.1 - diff --git a/patches/dragModifiersChooseAction.patch b/patches/dragModifiersChooseAction.patch @@ -1,221 +0,0 @@ -From 4edcc5c1400687966335765a6bfebf7eba22a6f8 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 17 Nov 2026 14:58:02 -0300 -Subject: [PATCH 18/19] data_device: modifiers choose the drag action - ---- - libswc/data.c | 45 +++++++++++++++++++++++++++++++++----- - libswc/data.h | 3 +++ - libswc/data_device.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ - libswc/swc.h | 16 ++++++++++++++ - 4 files changed, 110 insertions(+), 6 deletions(-) - -diff --git a/libswc/data.c b/libswc/data.c -index 903cf10..db91576 100644 ---- a/libswc/data.c -+++ b/libswc/data.c -@@ -37,6 +37,9 @@ struct data { - /* drag state: source actions, target actions, agreed action */ - uint32_t source_actions, action; - bool accepted; -+ /* current offer, target actions and preference, key-chosen action */ -+ struct wl_resource *drag_offer; -+ uint32_t offer_actions, preferred, chosen; - }; - - /* action both allow: target preference, then copy, move, ask */ -@@ -102,6 +105,21 @@ offer_finish(struct wl_client *client, struct wl_resource *offer) - } - } - -+/* pick the action, a held key first, and tell both sides */ -+static void -+settle(struct data *data, struct wl_resource *offer) -+{ -+ uint32_t both = data->source_actions & data->offer_actions; -+ -+ data->action = data->chosen & both ? data->chosen : agree(both, data->preferred); -+ if (wl_resource_get_version(offer) >= WL_DATA_OFFER_ACTION_SINCE_VERSION) { -+ wl_data_offer_send_action(offer, data->action); -+ } -+ if (wl_resource_get_version(data->source) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION) { -+ wl_data_source_send_action(data->source, data->action); -+ } -+} -+ - static void - offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t dnd_actions, uint32_t preferred_action) - { -@@ -111,11 +129,9 @@ offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t - if (!data) { - return; - } -- data->action = agree(data->source_actions & dnd_actions, preferred_action); -- wl_data_offer_send_action(offer, data->action); -- if (wl_resource_get_version(data->source) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION) { -- wl_data_source_send_action(data->source, data->action); -- } -+ data->offer_actions = dnd_actions; -+ data->preferred = preferred_action; -+ settle(data, offer); - } - - static const struct wl_data_offer_interface data_offer_impl = { -@@ -205,6 +221,8 @@ data_source_new(struct wl_client *client, uint32_t version, uint32_t id) - wl_list_init(&data->offers); - data->source_actions = data->action = 0; - data->accepted = false; -+ data->drag_offer = NULL; -+ data->offer_actions = data->preferred = data->chosen = 0; - - data->source = - wl_resource_create(client, &wl_data_source_interface, version, id); -@@ -256,7 +274,8 @@ data_drag_enter(struct wl_resource *source, struct wl_resource *offer) - struct data *data = wl_resource_get_user_data(source); - - data->accepted = false; -- data->action = 0; -+ data->action = data->offer_actions = data->preferred = 0; -+ data->drag_offer = offer; - if (wl_resource_get_version(offer) >= WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) { - wl_data_offer_send_source_actions(offer, data->source_actions); - } -@@ -273,3 +292,17 @@ data_drag_droppable(struct wl_resource *source) - /* no actions before version 3, a drop copies */ - return wl_resource_get_version(source) < WL_DATA_SOURCE_ACTION_SINCE_VERSION || data->action; - } -+ -+void -+data_drag_choose(struct wl_resource *source, uint32_t action) -+{ -+ struct data *data = wl_resource_get_user_data(source); -+ -+ if (data->chosen == action) { -+ return; -+ } -+ data->chosen = action; -+ if (data->drag_offer && data->offer_actions) { -+ settle(data, data->drag_offer); -+ } -+} -diff --git a/libswc/data.h b/libswc/data.h -index ab1e50b..e271dd5 100644 ---- a/libswc/data.h -+++ b/libswc/data.h -@@ -40,6 +40,9 @@ data_send_mime_types(struct wl_resource *source, struct wl_resource *offer); - /* a drag offer for source entered a client, nothing accepted yet */ - void - data_drag_enter(struct wl_resource *source, struct wl_resource *offer); -+/* action a held key asks for, 0 for none, wins over the target preference */ -+void -+data_drag_choose(struct wl_resource *source, uint32_t action); - /* target took a type and an action both allow */ - bool - data_drag_droppable(struct wl_resource *source); -diff --git a/libswc/data_device.c b/libswc/data_device.c -index 8cec18b..e24cbf6 100644 ---- a/libswc/data_device.c -+++ b/libswc/data_device.c -@@ -25,6 +25,7 @@ - #include "compositor.h" - #include "data.h" - #include "event.h" -+#include "keyboard.h" - #include "internal.h" - #include "pointer.h" - #include "seat.h" -@@ -161,6 +162,52 @@ drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *butt - return true; - } - -+/* modifiers that ask for each action, all held. none means never */ -+static uint32_t action_modifiers[] = { -+ [0] = SWC_MOD_CTRL, /* copy */ -+ [1] = SWC_MOD_SHIFT, /* move */ -+ [2] = 0, /* ask */ -+}; -+ -+static const uint32_t actions[] = { -+ WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, -+ WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE, -+ WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK, -+}; -+ -+/* action the held modifiers ask for, the one with most modifiers wins */ -+static uint32_t -+drag_action_for(uint32_t mods) -+{ -+ uint32_t best = 0; -+ int bits, most = 0; -+ size_t i; -+ -+ for (i = 0; i < sizeof actions / sizeof actions[0]; i++) { -+ if (!action_modifiers[i] || (mods & action_modifiers[i]) != action_modifiers[i]) { -+ continue; -+ } -+ bits = __builtin_popcount(action_modifiers[i]); -+ if (bits > most) { -+ most = bits; -+ best = actions[i]; -+ } -+ } -+ return best; -+} -+ -+EXPORT void -+swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers) -+{ -+ size_t i; -+ -+ for (i = 0; i < sizeof actions / sizeof actions[0]; i++) { -+ if (actions[i] == (uint32_t)action) { -+ action_modifiers[i] = modifiers & (SWC_MOD_CTRL | SWC_MOD_ALT | SWC_MOD_LOGO | SWC_MOD_SHIFT); -+ } -+ } -+} -+ - static bool - drag_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, wl_fixed_t fy) - { -@@ -170,6 +217,11 @@ drag_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, wl_fi - if (drag.icon) { - view_move(&drag.icon->base, x, y); - } -+ if (drag.source) { -+ uint32_t mods = swc.seat->keyboard ? swc.seat->keyboard->modifiers : 0; -+ -+ data_drag_choose(drag.source, drag_action_for(mods)); -+ } - drag_enter(compositor_view_at(x, y), x, y); - if (drag.target) { - wl_data_device_send_motion(drag.target, time, -diff --git a/libswc/swc.h b/libswc/swc.h -index bd23d6a..c5a2b3c 100644 ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -647,6 +647,22 @@ enum { - SWC_MOD_ANY = ~0 - }; - -+/** -+ * Drag actions, as in wl_data_device_manager.dnd_action. -+ */ -+enum swc_drag_action { -+ SWC_DRAG_COPY = 1, -+ SWC_DRAG_MOVE = 2, -+ SWC_DRAG_ASK = 4, -+}; -+ -+/** -+ * Set the modifiers that ask for an action during a drag, all held. -+ * 0 disables it. Defaults: Ctrl copy, Shift move. -+ */ -+void -+swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers); -+ - enum swc_binding_type { - SWC_BINDING_KEY, - SWC_BINDING_BUTTON, --- -2.56.1 - diff --git a/patches/extentsFromAttachedBuffer.patch b/patches/extentsFromAttachedBuffer.patch @@ -1,78 +0,0 @@ -From 4afbdeaf21c3599c1f2f78bf5871f2dce2500c93 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Fri, 13 Nov 2026 19:29:22 -0300 -Subject: [PATCH 01/19] compositor: size extents from the new buffer - -base.buffer still held the old one during attach. ---- - libswc/compositor.c | 18 ++++++++---------- - 1 file changed, 8 insertions(+), 10 deletions(-) - -diff --git a/libswc/compositor.c b/libswc/compositor.c -index bb762f6..c6238ad 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -627,7 +627,7 @@ damage_view(struct compositor_view *view) - } - - static void --update_extents(struct compositor_view *view) -+update_extents(struct compositor_view *view, const struct wld_buffer *buffer) - { - int64_t total_border = - (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; -@@ -647,12 +647,9 @@ update_extents(struct compositor_view *view) - - int64_t buffer_x1 = geom_x - view->buffer_offset_x; - int64_t buffer_y1 = geom_y - view->buffer_offset_y; -- int64_t buffer_x2 = -- buffer_x1 + -- (view->base.buffer ? view->base.buffer->width : (uint32_t)geom_w); -+ int64_t buffer_x2 = buffer_x1 + (buffer ? buffer->width : (uint32_t)geom_w); - int64_t buffer_y2 = -- buffer_y1 + -- (view->base.buffer ? view->base.buffer->height : (uint32_t)geom_h); -+ buffer_y1 + (buffer ? buffer->height : (uint32_t)geom_h); - - view->extents.x1 = clamp_i32(MIN(MIN(border_x1, decor_x1), buffer_x1)); - view->extents.y1 = clamp_i32(MIN(MIN(border_y1, decor_y1), buffer_y1)); -@@ -1073,7 +1070,8 @@ attach(struct view *base, struct wld_buffer *buffer) - if (view_set_size(&view->base, new_width, new_height)) { - /* The view was resized. */ - old_extents = view->extents; -- update_extents(view); -+ /* base.buffer is still the old buffer here, size from the new one */ -+ update_extents(view, buffer); - - if (view->visible) { - /* Damage the region that was newly uncovered or covered. */ -@@ -1107,7 +1105,7 @@ move(struct view *base, int32_t x, int32_t y) - } - - if (view_set_position(&view->base, x, y)) { -- update_extents(view); -+ update_extents(view, view->base.buffer); - - if (view->visible) { - /* Assume worst-case no clipping until we draw the next frame (in -@@ -1612,7 +1610,7 @@ compositor_view_set_border_width(struct compositor_view *view, - - /* XXX: Damage above surface for transparent surfaces? */ - -- update_extents(view); -+ update_extents(view, view->base.buffer); - - /* Damage old extents to prevent leftover border artifacts */ - if (view->visible && -@@ -1664,7 +1662,7 @@ compositor_view_set_decor(struct compositor_view *view, - } - - decor_view_set(view, decor); -- update_extents(view); -+ update_extents(view, view->base.buffer); - update(&view->base); - } - --- -2.56.1 - diff --git a/patches/focusFromDecoration.patch b/patches/focusFromDecoration.patch @@ -1,211 +0,0 @@ -From 4d1bd1573a7baa4b612ea25340379e68817970c7 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 24 Nov 2026 08:51:51 -0300 -Subject: [PATCH] compositor: the pointer over a window's decoration or border - enters it - -The pointer was over a window only where its surface is, so with focus -following the pointer the title bar did nothing. The window under the -pointer is now the first thing in stacking order that shows its surface, -its decoration or its border there, and its handler is told it was -entered, once, when it is the decoration. ---- - libswc/compositor.c | 104 ++++++++++++++++++++++++++++++++++---------- - libswc/window.c | 12 +++-- - libswc/window.h | 5 +++ - 3 files changed, 96 insertions(+), 25 deletions(-) - -diff --git a/libswc/compositor.c b/libswc/compositor.c -index 16c68ae..7c3ec4d 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -126,6 +126,8 @@ static struct { - uint32_t scheduled_updates; - - bool updating; -+ /* The window whose decoration the pointer is over, if one. */ -+ struct window *chrome_window; - struct wl_global *global; - bool initialized; - -@@ -1197,40 +1199,87 @@ restack_view_for_layer(struct compositor_view *view, bool raise) - wl_list_insert(insert_after, &view->link); - } - -+/* whether the point is on what the view shows and takes input there */ -+static bool -+view_content_at(struct compositor_view *view, int32_t x, int32_t y) -+{ -+ struct swc_rectangle *geom = &view->base.geometry; -+ struct swc_rectangle buffer_geom; -+ -+ if (view->window) { -+ if (!rectangle_contains_point(geom, x, y)) { -+ return false; -+ } -+ } else if (view->base.buffer) { -+ buffer_geom.x = geom->x - view->buffer_offset_x; -+ buffer_geom.y = geom->y - view->buffer_offset_y; -+ buffer_geom.width = view->base.buffer->width; -+ buffer_geom.height = view->base.buffer->height; -+ if (!rectangle_contains_point(&buffer_geom, x, y)) { -+ return false; -+ } -+ } else if (!rectangle_contains_point(geom, x, y)) { -+ return false; -+ } -+ -+ return pixman_region32_contains_point(&view->surface->state.input, -+ x - geom->x + view->buffer_offset_x, -+ y - geom->y + view->buffer_offset_y, -+ NULL); -+} -+ - struct compositor_view * - compositor_view_at(int32_t x, int32_t y) - { - struct compositor_view *view; -- struct swc_rectangle *geom; -- struct swc_rectangle buffer_geom; - - wl_list_for_each(view, &compositor.views, link) - { - if (!view->visible || view->no_input) { - continue; - } -+ if (view_content_at(view, x, y)) { -+ return view; -+ } -+ } - -- geom = &view->base.geometry; -- if (view->window) { -- if (!rectangle_contains_point(geom, x, y)) { -- continue; -- } -- } else if (view->base.buffer) { -- buffer_geom.x = geom->x - view->buffer_offset_x; -- buffer_geom.y = geom->y - view->buffer_offset_y; -- buffer_geom.width = view->base.buffer->width; -- buffer_geom.height = view->base.buffer->height; -- if (!rectangle_contains_point(&buffer_geom, x, y)) { -- continue; -- } -- } else if (!rectangle_contains_point(geom, x, y)) { -+ return NULL; -+} -+ -+/* whether the point is on the window's decoration or its border, which the -+ * pointer is not over for the client: the surface is not under it */ -+static bool -+view_chrome_at(struct compositor_view *view, int32_t x, int32_t y) -+{ -+ struct swc_rectangle *geom = &view->base.geometry; -+ int64_t border = (int64_t)view->border.outwidth + (int64_t)view->border.inwidth; -+ -+ if (!view->window) { -+ return false; -+ } -+ -+ return x >= geom->x - (int64_t)view->decor.left - border && -+ x < geom->x + (int64_t)geom->width + (int64_t)view->decor.right + border && -+ y >= geom->y - (int64_t)view->decor.top - border && -+ y < geom->y + (int64_t)geom->height + (int64_t)view->decor.bottom + border; -+} -+ -+/* the window whose decoration or border is the first thing under the point, -+ * in stacking order, NULL when it is anything else or nothing */ -+static struct compositor_view * -+compositor_chrome_at(int32_t x, int32_t y) -+{ -+ struct compositor_view *view; -+ -+ wl_list_for_each(view, &compositor.views, link) -+ { -+ if (!view->visible || view->no_input) { - continue; - } -- -- if (pixman_region32_contains_point(&view->surface->state.input, -- x - geom->x + view->buffer_offset_x, -- y - geom->y + view->buffer_offset_y, -- NULL)) { -+ if (view_content_at(view, x, y)) { -+ return NULL; -+ } -+ if (view_chrome_at(view, x, y)) { - return view; - } - } -@@ -1938,10 +1987,21 @@ handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, - return false; - } - -- struct compositor_view *view = compositor_view_at(x, y); -+ struct compositor_view *chrome = compositor_chrome_at(x, y); -+ struct compositor_view *view = chrome ? NULL : compositor_view_at(x, y); - - pointer_set_focus(swc.seat->pointer, view); - -+ /* The pointer over a window's decoration or border is over the window as -+ * much as over its surface: the window's handler is told as when the -+ * surface is entered, once until the pointer is anywhere else */ -+ if (chrome && chrome->window != compositor.chrome_window) { -+ compositor.chrome_window = chrome->window; -+ window_pointer_entered(chrome->window); -+ } else if (!chrome) { -+ compositor.chrome_window = NULL; -+ } -+ - return false; - } - -diff --git a/libswc/window.c b/libswc/window.c -index 80c410e..4bc3054 100644 ---- a/libswc/window.c -+++ b/libswc/window.c -@@ -95,6 +95,14 @@ clamp_window_size(const struct window *window, uint32_t *width, - window->base.max_height); - } - -+void -+window_pointer_entered(struct window *window) -+{ -+ if (window->handler->entered) { -+ window->handler->entered(window->handler_data); -+ } -+} -+ - static void - handle_window_enter(struct wl_listener *listener, void *data) - { -@@ -110,9 +118,7 @@ handle_window_enter(struct wl_listener *listener, void *data) - return; - } - -- if (window->handler->entered) { -- window->handler->entered(window->handler_data); -- } -+ window_pointer_entered(window); - } - - struct wl_listener window_enter_listener = { -diff --git a/libswc/window.h b/libswc/window.h -index bf6baec..d987307 100644 ---- a/libswc/window.h -+++ b/libswc/window.h -@@ -95,6 +95,11 @@ struct window_impl { - - extern struct wl_listener window_enter_listener; - -+/* The pointer is over the window as much as over its surface: its handler is -+ * told, as when the surface is entered. */ -+void -+window_pointer_entered(struct window *window); -+ - bool - window_initialize(struct window *window, const struct window_impl *impl, - struct surface *surface); --- -2.56.1 - diff --git a/patches/input.patch b/patches/input.patch @@ -0,0 +1,1755 @@ +Subject: input, cursors, drag and drop + +diff --git a/libswc/cursor_shape.c b/libswc/cursor_shape.c +new file mode 100644 +index 0000000..28e5a78 +--- /dev/null ++++ b/libswc/cursor_shape.c +@@ -0,0 +1,146 @@ ++/* cursor-shape-v1 */ ++ ++#include "cursor_shape.h" ++#include "internal.h" ++#include "pointer.h" ++#include "seat.h" ++#include "util.h" ++ ++#include <wayland-server.h> ++#include "cursor-shape-v1-server-protocol.h" ++ ++/* the names of the shapes, the ones of CSS: what a window manager that has a theme gives pictures for */ ++static const char *const names[] = { ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT] = "default", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CONTEXT_MENU] = "context-menu", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_HELP] = "help", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER] = "pointer", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_PROGRESS] = "progress", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_WAIT] = "wait", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CELL] = "cell", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR] = "crosshair", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT] = "text", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_VERTICAL_TEXT] = "vertical-text", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALIAS] = "alias", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COPY] = "copy", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_MOVE] = "move", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NO_DROP] = "no-drop", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED] = "not-allowed", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRAB] = "grab", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_GRABBING] = "grabbing", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE] = "e-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE] = "n-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE] = "ne-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE] = "nw-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE] = "s-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE] = "se-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE] = "sw-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE] = "w-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_EW_RESIZE] = "ew-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NS_RESIZE] = "ns-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE] = "nesw-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NWSE_RESIZE] = "nwse-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_COL_RESIZE] = "col-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ROW_RESIZE] = "row-resize", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_SCROLL] = "all-scroll", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_IN] = "zoom-in", ++ [WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ZOOM_OUT] = "zoom-out", ++}; ++ ++#define NAMES (sizeof names / sizeof names[0]) ++ ++static void ++pointer_set_shape_request(struct wl_client *client, struct wl_resource *resource, ++ uint32_t serial, uint32_t shape) ++{ ++ struct pointer *pointer = wl_resource_get_user_data(resource); ++ ++ (void)serial; ++ if (shape < 1 || shape >= NAMES || !names[shape]) { ++ wl_resource_post_error(resource, WP_CURSOR_SHAPE_DEVICE_V1_ERROR_INVALID_SHAPE, ++ "no such shape: %u", shape); ++ return; ++ } ++ /* only what has the pointer says what it looks like */ ++ if (!pointer || client != pointer->focus.client) { ++ return; ++ } ++ pointer_set_shape(pointer, names[shape]); ++} ++ ++static const struct wp_cursor_shape_device_v1_interface pointer_device_impl = { ++ .destroy = destroy_resource, ++ .set_shape = pointer_set_shape_request, ++}; ++ ++/* a tablet tool has no pointer here: it is answered and does nothing */ ++static void ++tool_set_shape_request(struct wl_client *client, struct wl_resource *resource, ++ uint32_t serial, uint32_t shape) ++{ ++ (void)client; ++ (void)serial; ++ (void)shape; ++ (void)resource; ++} ++ ++static const struct wp_cursor_shape_device_v1_interface tool_device_impl = { ++ .destroy = destroy_resource, ++ .set_shape = tool_set_shape_request, ++}; ++ ++static void ++get_pointer(struct wl_client *client, struct wl_resource *resource, uint32_t id, ++ struct wl_resource *pointer_resource) ++{ ++ struct wl_resource *device = wl_resource_create( ++ client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); ++ ++ if (!device) { ++ wl_resource_post_no_memory(resource); ++ return; ++ } ++ wl_resource_set_implementation(device, &pointer_device_impl, ++ wl_resource_get_user_data(pointer_resource), NULL); ++} ++ ++static void ++get_tablet_tool_v2(struct wl_client *client, struct wl_resource *resource, uint32_t id, ++ struct wl_resource *tool) ++{ ++ struct wl_resource *device = wl_resource_create( ++ client, &wp_cursor_shape_device_v1_interface, wl_resource_get_version(resource), id); ++ ++ (void)tool; ++ if (!device) { ++ wl_resource_post_no_memory(resource); ++ return; ++ } ++ wl_resource_set_implementation(device, &tool_device_impl, NULL, NULL); ++} ++ ++static const struct wp_cursor_shape_manager_v1_interface manager_impl = { ++ .destroy = destroy_resource, ++ .get_pointer = get_pointer, ++ .get_tablet_tool_v2 = get_tablet_tool_v2, ++}; ++ ++static void ++bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) ++{ ++ struct wl_resource *resource; ++ ++ (void)data; ++ resource = wl_resource_create(client, &wp_cursor_shape_manager_v1_interface, version, id); ++ if (!resource) { ++ wl_client_post_no_memory(client); ++ return; ++ } ++ wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); ++} ++ ++struct wl_global * ++cursor_shape_manager_create(struct wl_display *display) ++{ ++ return wl_global_create(display, &wp_cursor_shape_manager_v1_interface, 1, NULL, &bind_manager); ++} +diff --git a/libswc/cursor_shape.h b/libswc/cursor_shape.h +new file mode 100644 +index 0000000..be8ebb4 +--- /dev/null ++++ b/libswc/cursor_shape.h +@@ -0,0 +1,11 @@ ++/* cursor-shape-v1: a client asks for the pointer's picture by name */ ++ ++#ifndef SWC_CURSOR_SHAPE_H ++#define SWC_CURSOR_SHAPE_H ++ ++struct wl_display; ++ ++struct wl_global * ++cursor_shape_manager_create(struct wl_display *display); ++ ++#endif +diff --git a/libswc/data.c b/libswc/data.c +index 8c34b83..d497544 100644 +--- a/libswc/data.c ++++ b/libswc/data.c +@@ -31,10 +31,39 @@ + + struct data { + struct wl_array mime_types; +- struct wl_resource *source; ++ struct wl_resource *source; /* NULL for a source of the compositor's own */ ++ struct data_host host; + struct wl_list offers; ++ ++ /* drag state: source actions, target actions, agreed action */ ++ uint32_t source_actions, action; ++ bool accepted; ++ /* current offer, target actions and preference, key-chosen action */ ++ struct wl_resource *drag_offer; ++ uint32_t offer_actions, preferred, chosen; + }; + ++/* action both allow: target preference, then copy, move, ask */ ++static uint32_t ++agree(uint32_t both, uint32_t preferred) ++{ ++ static const uint32_t order[] = { ++ WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, ++ WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE, ++ WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK, ++ }; ++ ++ if (preferred & both) { ++ return preferred; ++ } ++ for (size_t i = 0; i < sizeof(order) / sizeof(order[0]); i++) { ++ if (order[i] & both) { ++ return order[i]; ++ } ++ } ++ return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE; ++} ++ + static void + offer_accept(struct wl_client *client, struct wl_resource *offer, + uint32_t serial, const char *mime_type) +@@ -46,6 +75,10 @@ offer_accept(struct wl_client *client, struct wl_resource *offer, + return; + } + ++ if (!data->source) { ++ return; ++ } ++ data->accepted = mime_type != NULL; + wl_data_source_send_target(data->source, mime_type); + } + +@@ -60,26 +93,49 @@ offer_receive(struct wl_client *client, struct wl_resource *offer, + return; + } + +- wl_data_source_send_send(data->source, mime_type, fd); ++ data_request(data, mime_type, fd); + close(fd); + } + ++/* drop finished: the source may release its data, delete it after a move */ + static void + offer_finish(struct wl_client *client, struct wl_resource *offer) + { ++ struct data *data = wl_resource_get_user_data(offer); ++ + (void)client; +- (void)offer; +- /* TODO: Implement */ ++ if (data && data->source && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { ++ wl_data_source_send_dnd_finished(data->source); ++ } ++} ++ ++/* pick the action, a held key first, and tell both sides */ ++static void ++settle(struct data *data, struct wl_resource *offer) ++{ ++ uint32_t both = data->source_actions & data->offer_actions; ++ ++ data->action = data->chosen & both ? data->chosen : agree(both, data->preferred); ++ if (wl_resource_get_version(offer) >= WL_DATA_OFFER_ACTION_SINCE_VERSION) { ++ wl_data_offer_send_action(offer, data->action); ++ } ++ if (wl_resource_get_version(data->source) >= WL_DATA_SOURCE_ACTION_SINCE_VERSION) { ++ wl_data_source_send_action(data->source, data->action); ++ } + } + + static void + offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t dnd_actions, uint32_t preferred_action) + { ++ struct data *data = wl_resource_get_user_data(offer); ++ + (void)client; +- (void)offer; +- (void)dnd_actions; +- (void)preferred_action; +- /* TODO: Implement */ ++ if (!data || !data->source) { ++ return; ++ } ++ data->offer_actions = dnd_actions; ++ data->preferred = preferred_action; ++ settle(data, offer); + } + + static const struct wl_data_offer_interface data_offer_impl = { +@@ -117,10 +173,10 @@ error0: + static void + source_set_actions(struct wl_client *client, struct wl_resource *resource, uint32_t dnd_actions) + { ++ struct data *data = wl_resource_get_user_data(resource); ++ + (void)client; +- (void)resource; +- (void)dnd_actions; +- /* TODO: Implement */ ++ data->source_actions = dnd_actions; + } + + static const struct wl_data_source_interface data_source_impl = { +@@ -129,33 +185,32 @@ static const struct wl_data_source_interface data_source_impl = { + .set_actions = source_set_actions, + }; + ++/* after the source is gone each data_offer made from it has a pointer to a ++ * free'd struct. the resources cannot be destroyed, a client that correctly ++ * calls data_offer.destroy would crash it, so the user data of each offer is ++ * cleared, and a client that still calls accept or receive does nothing */ + static void +-data_destroy(struct wl_resource *source) ++release(struct data *data) + { +- struct data *data = wl_resource_get_user_data(source); + struct wl_resource *offer; + char **mime_type; + + wl_array_for_each(mime_type, &data->mime_types) free(*mime_type); + wl_array_release(&data->mime_types); +- +- /* After this data_source is destroyed, each of the data_offer objects +- * associated with the data_source has a pointer to a free'd struct. We +- * can't destroy the resources because this results in a segfault on the +- * client when it correctly tries to call data_source.destroy. However, a +- * misbehaving client could still attempt to call accept or receive on the +- * data_offer, which would crash the server. +- * +- * So, we clear the user data on each of the offers to protect us. */ + wl_resource_for_each(offer, &data->offers) + { + wl_resource_set_user_data(offer, NULL); + wl_resource_set_destructor(offer, NULL); + } +- + free(data); + } + ++static void ++data_destroy(struct wl_resource *source) ++{ ++ release(wl_resource_get_user_data(source)); ++} ++ + struct wl_resource * + data_source_new(struct wl_client *client, uint32_t version, uint32_t id) + { +@@ -167,6 +222,11 @@ data_source_new(struct wl_client *client, uint32_t version, uint32_t id) + } + wl_array_init(&data->mime_types); + wl_list_init(&data->offers); ++ data->source_actions = data->action = 0; ++ memset(&data->host, 0, sizeof(data->host)); ++ data->accepted = false; ++ data->drag_offer = NULL; ++ data->offer_actions = data->preferred = data->chosen = 0; + + data->source = + wl_resource_create(client, &wl_data_source_interface, version, id); +@@ -185,10 +245,8 @@ error0: + } + + struct wl_resource * +-data_offer_new(struct wl_client *client, struct wl_resource *source, +- uint32_t version) ++data_offer_for(struct wl_client *client, struct data *data, uint32_t version) + { +- struct data *data = wl_resource_get_user_data(source); + struct wl_resource *offer; + + offer = wl_resource_create(client, &wl_data_offer_interface, version, 0); +@@ -202,12 +260,137 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, + return offer; + } + ++struct wl_resource * ++data_offer_new(struct wl_client *client, struct wl_resource *source, ++ uint32_t version) ++{ ++ return data_offer_for(client, wl_resource_get_user_data(source), version); ++} ++ + void +-data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) ++data_send_types(struct data *data, struct wl_resource *offer) + { +- struct data *data = wl_resource_get_user_data(source); + char **mime_type; + + wl_array_for_each(mime_type, &data->mime_types) + wl_data_offer_send_offer(offer, *mime_type); + } ++ ++void ++data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) ++{ ++ data_send_types(wl_resource_get_user_data(source), offer); ++} ++ ++struct data * ++data_of(struct wl_resource *source) ++{ ++ return wl_resource_get_user_data(source); ++} ++ ++unsigned ++data_type_count(const struct data *data) ++{ ++ return data->mime_types.size / sizeof(char *); ++} ++ ++const char * ++data_type_at(const struct data *data, unsigned index) ++{ ++ return ((char **)data->mime_types.data)[index]; ++} ++ ++void ++data_request(struct data *data, const char *mime_type, int fd) ++{ ++ if (data->source) { ++ wl_data_source_send_send(data->source, mime_type, fd); ++ } else if (data->host.send) { ++ data->host.send(data->host.data, mime_type, fd); ++ } ++} ++ ++void ++data_cancel(struct data *data) ++{ ++ if (data->source) { ++ wl_data_source_send_cancelled(data->source); ++ } else if (data->host.cancelled) { ++ data->host.cancelled(data->host.data); ++ } ++} ++ ++struct data * ++data_host_new(const char *const *types, unsigned count, ++ const struct data_host *host, void *host_data) ++{ ++ struct data *data = calloc(1, sizeof(*data)); ++ char **dst; ++ unsigned i; ++ ++ if (!data) { ++ return NULL; ++ } ++ wl_array_init(&data->mime_types); ++ wl_list_init(&data->offers); ++ data->host = *host; ++ data->host.data = host_data; ++ for (i = 0; i < count; i++) { ++ dst = wl_array_add(&data->mime_types, sizeof(*dst)); ++ if (!dst) { ++ release(data); ++ return NULL; ++ } ++ if (!(*dst = strdup(types[i]))) { ++ data->mime_types.size -= sizeof(*dst); ++ release(data); ++ return NULL; ++ } ++ } ++ return data; ++} ++ ++void ++data_host_free(struct data *data) ++{ ++ release(data); ++} ++ ++void ++data_drag_enter(struct wl_resource *source, struct wl_resource *offer) ++{ ++ struct data *data = wl_resource_get_user_data(source); ++ ++ data->accepted = false; ++ data->action = data->offer_actions = data->preferred = 0; ++ data->drag_offer = offer; ++ if (wl_resource_get_version(offer) >= WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) { ++ wl_data_offer_send_source_actions(offer, data->source_actions); ++ } ++} ++ ++bool ++data_drag_droppable(struct wl_resource *source) ++{ ++ struct data *data = wl_resource_get_user_data(source); ++ ++ if (!data->accepted) { ++ return false; ++ } ++ /* no actions before version 3, a drop copies */ ++ return wl_resource_get_version(source) < WL_DATA_SOURCE_ACTION_SINCE_VERSION || data->action; ++} ++ ++void ++data_drag_choose(struct wl_resource *source, uint32_t action) ++{ ++ struct data *data = wl_resource_get_user_data(source); ++ ++ if (data->chosen == action) { ++ return; ++ } ++ data->chosen = action; ++ if (data->drag_offer && data->offer_actions) { ++ settle(data, data->drag_offer); ++ } ++} +diff --git a/libswc/data.h b/libswc/data.h +index c99f274..d1c762f 100644 +--- a/libswc/data.h ++++ b/libswc/data.h +@@ -24,6 +24,7 @@ + #ifndef SWC_DATA_H + #define SWC_DATA_H + ++#include <stdbool.h> + #include <stdint.h> + + struct wl_client; +@@ -36,4 +37,47 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, + void + data_send_mime_types(struct wl_resource *source, struct wl_resource *offer); + ++struct data; ++ ++/* a source of the compositor's own, for a selection it keeps itself: it ++ * offers types and answers each ask on its own */ ++struct data_host { ++ void (*send)(void *data, const char *mime_type, int fd); ++ void (*cancelled)(void *data); ++ void *data; ++}; ++ ++struct data * ++data_host_new(const char *const *types, unsigned count, ++ const struct data_host *host, void *data); ++void ++data_host_free(struct data *data); ++struct data * ++data_of(struct wl_resource *source); ++/* offers of a selection, host or client */ ++struct wl_resource * ++data_offer_for(struct wl_client *client, struct data *data, uint32_t version); ++void ++data_send_types(struct data *data, struct wl_resource *offer); ++unsigned ++data_type_count(const struct data *data); ++const char * ++data_type_at(const struct data *data, unsigned index); ++/* asks the source for a type, to be written to fd (the caller closes its copy) */ ++void ++data_request(struct data *data, const char *mime_type, int fd); ++/* the selection went to someone else */ ++void ++data_cancel(struct data *data); ++ ++/* a drag offer for source entered a client, nothing accepted yet */ ++void ++data_drag_enter(struct wl_resource *source, struct wl_resource *offer); ++/* action a held key asks for, 0 for none, wins over the target preference */ ++void ++data_drag_choose(struct wl_resource *source, uint32_t action); ++/* target took a type and an action both allow */ ++bool ++data_drag_droppable(struct wl_resource *source); ++ + #endif +diff --git a/libswc/data_device.c b/libswc/data_device.c +index f286c37..b69f57c 100644 +--- a/libswc/data_device.c ++++ b/libswc/data_device.c +@@ -22,9 +22,254 @@ + */ + + #include "data_device.h" ++#include "compositor.h" + #include "data.h" + #include "event.h" ++#include "keyboard.h" ++#include "internal.h" ++#include "pointer.h" ++#include "seat.h" ++#include "surface.h" + #include "util.h" ++#include "view.h" ++ ++#include <string.h> ++ ++/* drag and drop, one drag at a time, holding the pointer until the buttons are released */ ++static struct drag { ++ bool active; ++ struct data_device *device; ++ struct wl_client *client; /* starter */ ++ struct wl_resource *source; /* NULL for a drag inside one client */ ++ struct compositor_view *icon; /* the icon */ ++ struct compositor_view *focus; /* the view it is over */ ++ struct wl_resource *target; /* its client's data device */ ++ struct pointer_handler handler; ++ struct wl_listener source_destroy, focus_destroy, icon_destroy; ++} drag; ++ ++static bool drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state); ++ ++static void ++drag_unfocus(void) ++{ ++ if (drag.focus) { ++ wl_list_remove(&drag.focus_destroy.link); ++ } ++ drag.focus = NULL; ++ drag.target = NULL; ++} ++ ++static void ++drag_leave(void) ++{ ++ if (drag.target) { ++ wl_data_device_send_leave(drag.target); ++ } ++ drag_unfocus(); ++} ++ ++static wl_fixed_t ++local(int32_t at, int32_t origin, int32_t offset) ++{ ++ return wl_fixed_from_int(at - origin + offset); ++} ++ ++static void ++drag_enter(struct compositor_view *view, int32_t x, int32_t y) ++{ ++ struct wl_client *client; ++ struct wl_resource *device, *offer = NULL; ++ ++ if (view == drag.focus) { ++ return; ++ } ++ drag_leave(); ++ if (!view || !view->surface) { ++ return; ++ } ++ client = wl_resource_get_client(view->surface->resource); ++ /* a drag with no source stays inside the client that made it */ ++ if (!drag.source && client != drag.client) { ++ return; ++ } ++ device = wl_resource_find_for_client(&drag.device->resources, client); ++ if (!device) { ++ return; ++ } ++ if (drag.source && (offer = data_offer_new(client, drag.source, wl_resource_get_version(device)))) { ++ wl_data_device_send_data_offer(device, offer); ++ data_send_mime_types(drag.source, offer); ++ data_drag_enter(drag.source, offer); ++ } ++ drag.focus = view; ++ drag.target = device; ++ wl_signal_add(&view->destroy_signal, &drag.focus_destroy); ++ wl_data_device_send_enter(device, wl_display_next_serial(swc.display), view->surface->resource, ++ local(x, view->base.geometry.x, view->buffer_offset_x), ++ local(y, view->base.geometry.y, view->buffer_offset_y), offer); ++} ++ ++static void ++drag_end(void) ++{ ++ struct pointer *pointer = swc.seat->pointer; ++ ++ drag_leave(); ++ wl_list_remove(&drag.handler.link); ++ if (drag.source) { ++ wl_list_remove(&drag.source_destroy.link); ++ } ++ if (drag.icon) { ++ wl_list_remove(&drag.icon_destroy.link); ++ compositor_view_destroy(drag.icon); ++ } ++ memset(&drag, 0, sizeof(drag)); ++ /* held buttons are swallowed as they go up */ ++ drag.handler.button = drag_swallow; ++ /* the pointer goes back to what is under it */ ++ pointer_set_focus(pointer, compositor_view_at(wl_fixed_to_int(pointer->x), wl_fixed_to_int(pointer->y))); ++} ++ ++static void ++drag_drop(void) ++{ ++ bool take = drag.target && (!drag.source || data_drag_droppable(drag.source)); ++ ++ if (!take) { ++ if (drag.source) { ++ wl_data_source_send_cancelled(drag.source); ++ } ++ drag_end(); ++ return; ++ } ++ wl_data_device_send_drop(drag.target); ++ if (drag.source && wl_resource_get_version(drag.source) >= WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION) { ++ wl_data_source_send_dnd_drop_performed(drag.source); ++ } ++ /* the target keeps its offer, no leave follows */ ++ drag_unfocus(); ++ drag_end(); ++} ++ ++static bool ++drag_swallow(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state) ++{ ++ (void)handler; ++ (void)time; ++ (void)button; ++ (void)state; ++ return true; ++} ++ ++/* modifiers that ask for each action, all held. none means never */ ++static uint32_t action_modifiers[] = { ++ [0] = SWC_MOD_CTRL, /* copy */ ++ [1] = SWC_MOD_SHIFT, /* move */ ++ [2] = 0, /* ask */ ++}; ++ ++static const uint32_t actions[] = { ++ WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY, ++ WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE, ++ WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK, ++}; ++ ++/* action the held modifiers ask for, the one with most modifiers wins */ ++static uint32_t ++drag_action_for(uint32_t mods) ++{ ++ uint32_t best = 0; ++ int bits, most = 0; ++ size_t i; ++ ++ for (i = 0; i < sizeof actions / sizeof actions[0]; i++) { ++ if (!action_modifiers[i] || (mods & action_modifiers[i]) != action_modifiers[i]) { ++ continue; ++ } ++ bits = __builtin_popcount(action_modifiers[i]); ++ if (bits > most) { ++ most = bits; ++ best = actions[i]; ++ } ++ } ++ return best; ++} ++ ++EXPORT void ++swc_set_drag_modifiers(enum swc_drag_action action, uint32_t modifiers) ++{ ++ size_t i; ++ ++ for (i = 0; i < sizeof actions / sizeof actions[0]; i++) { ++ if (actions[i] == (uint32_t)action) { ++ action_modifiers[i] = modifiers & (SWC_MOD_CTRL | SWC_MOD_ALT | SWC_MOD_LOGO | SWC_MOD_SHIFT); ++ } ++ } ++} ++ ++static bool ++drag_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, wl_fixed_t fy) ++{ ++ int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy); ++ ++ (void)handler; ++ if (drag.icon) { ++ view_move(&drag.icon->base, x, y); ++ } ++ if (drag.source) { ++ uint32_t mods = swc.seat->keyboard ? swc.seat->keyboard->modifiers : 0; ++ ++ data_drag_choose(drag.source, drag_action_for(mods)); ++ } ++ drag_enter(compositor_view_at(x, y), x, y); ++ if (drag.target) { ++ wl_data_device_send_motion(drag.target, time, ++ local(x, drag.focus->base.geometry.x, drag.focus->buffer_offset_x), ++ local(y, drag.focus->base.geometry.y, drag.focus->buffer_offset_y)); ++ } ++ return true; ++} ++ ++static bool ++drag_button(struct pointer_handler *handler, uint32_t time, struct button *button, uint32_t state) ++{ ++ (void)handler; ++ (void)time; ++ (void)button; ++ /* the drop comes when the last button held is let go */ ++ if (state == WL_POINTER_BUTTON_STATE_RELEASED && swc.seat->pointer->buttons.size <= sizeof(struct button)) { ++ drag_drop(); ++ } ++ return true; ++} ++ ++static void ++handle_drag_source_destroy(struct wl_listener *listener, void *data) ++{ ++ (void)listener; ++ (void)data; ++ drag.source = NULL; ++ drag_end(); ++} ++ ++static void ++handle_drag_focus_destroy(struct wl_listener *listener, void *data) ++{ ++ (void)data; ++ wl_list_remove(&listener->link); ++ drag.focus = NULL; ++ drag.target = NULL; ++} ++ ++static void ++handle_drag_icon_destroy(struct wl_listener *listener, void *data) ++{ ++ (void)listener; ++ (void)data; ++ compositor_view_destroy(drag.icon); ++ drag.icon = NULL; ++} + + static void + start_drag(struct wl_client *client, struct wl_resource *resource, +@@ -32,7 +277,46 @@ start_drag(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *origin_resource, + struct wl_resource *icon_resource, uint32_t serial) + { +- /* XXX: Implement */ ++ struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; ++ int32_t x, y; ++ struct button *button; ++ ++ (void)origin_resource; ++ (void)serial; ++ /* a drag needs a button held, and one at a time */ ++ if (drag.active || !pointer || !pointer->buttons.size) { ++ if (source_resource) { ++ wl_data_source_send_cancelled(source_resource); ++ } ++ return; ++ } ++ x = wl_fixed_to_int(pointer->x); ++ y = wl_fixed_to_int(pointer->y); ++ drag.active = true; ++ drag.device = wl_resource_get_user_data(resource); ++ drag.client = client; ++ drag.source = source_resource; ++ drag.focus_destroy.notify = handle_drag_focus_destroy; ++ if (drag.source) { ++ drag.source_destroy.notify = handle_drag_source_destroy; ++ wl_resource_add_destroy_listener(drag.source, &drag.source_destroy); ++ } ++ if (icon_resource && (drag.icon = compositor_create_view(wl_resource_get_user_data(icon_resource)))) { ++ drag.icon->no_input = true; ++ drag.icon->always_top = true; ++ compositor_view_set_z(drag.icon, STACK_LAYER_OVERLAY, true); ++ view_move(&drag.icon->base, x, y); ++ compositor_view_show(drag.icon); ++ drag.icon_destroy.notify = handle_drag_icon_destroy; ++ wl_resource_add_destroy_listener(icon_resource, &drag.icon_destroy); ++ } ++ /* the pointer is the drag's: no client sees it until the drop */ ++ pointer_set_focus(pointer, NULL); ++ drag.handler.motion = drag_motion; ++ drag.handler.button = drag_button; ++ wl_list_insert(&pointer->handlers, &drag.handler.link); ++ wl_array_for_each(button, &pointer->buttons) button->handler = &drag.handler; ++ drag_enter(compositor_view_at(x, y), x, y); + } + + static void +@@ -50,6 +334,11 @@ set_selection(struct wl_client *client, struct wl_resource *resource, + wl_data_source_send_cancelled(data_device->selection); + wl_list_remove(&data_device->selection_destroy_listener.link); + } ++ if (data_device->host) { ++ data_cancel(data_device->host); ++ data_host_free(data_device->host); ++ data_device->host = NULL; ++ } + + data_device->selection = data_source; + +@@ -89,6 +378,7 @@ data_device_create(void) + return NULL; + } + data_device->selection = NULL; ++ data_device->host = NULL; + data_device->selection_destroy_listener.notify = &handle_selection_destroy; + wl_signal_init(&data_device->event_signal); + wl_list_init(&data_device->resources); +@@ -103,6 +393,9 @@ data_device_destroy(struct data_device *data_device) + + wl_list_for_each_safe(resource, tmp, &data_device->resources, link) + wl_resource_destroy(resource); ++ if (data_device->host) { ++ data_host_free(data_device->host); ++ } + free(data_device); + } + +@@ -124,18 +417,41 @@ data_device_bind(struct data_device *data_device, struct wl_client *client, + return resource; + } + ++struct data * ++data_device_client_selection(struct data_device *data_device) ++{ ++ return data_device->selection ? data_of(data_device->selection) : NULL; ++} ++ ++void ++data_device_set_host(struct data_device *data_device, struct data *host) ++{ ++ if (data_device->selection) { ++ wl_data_source_send_cancelled(data_device->selection); ++ wl_list_remove(&data_device->selection_destroy_listener.link); ++ data_device->selection = NULL; ++ } ++ if (data_device->host) { ++ data_cancel(data_device->host); ++ data_host_free(data_device->host); ++ } ++ data_device->host = host; ++ send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED, ++ host); ++} ++ + static struct wl_resource * + new_offer(struct wl_resource *resource, struct wl_client *client, +- struct wl_resource *source) ++ struct data *source) + { + struct wl_resource *offer; + +- offer = data_offer_new(client, source, wl_resource_get_version(resource)); ++ offer = data_offer_for(client, source, wl_resource_get_version(resource)); + if (!offer) { + return NULL; + } + wl_data_device_send_data_offer(resource, offer); +- data_send_mime_types(source, offer); ++ data_send_types(source, offer); + + return offer; + } +@@ -156,8 +472,10 @@ data_device_offer_selection(struct data_device *data_device, + } + + /* If we have a selection, create a new offer for the client. */ +- if (data_device->selection) { +- offer = new_offer(resource, client, data_device->selection); ++ if (data_device->host) { ++ offer = new_offer(resource, client, data_device->host); ++ } else if (data_device->selection) { ++ offer = new_offer(resource, client, data_of(data_device->selection)); + } + + wl_data_device_send_selection(resource, offer); +diff --git a/libswc/data_device.h b/libswc/data_device.h +index 6712190..f7242cc 100644 +--- a/libswc/data_device.h ++++ b/libswc/data_device.h +@@ -29,9 +29,13 @@ + + enum { DATA_DEVICE_EVENT_SELECTION_CHANGED }; + ++struct data; ++ + struct data_device { + /* The data source corresponding to the current selection. */ + struct wl_resource *selection; ++ /* the selection when the compositor keeps it itself, never with selection */ ++ struct data *host; + struct wl_listener selection_destroy_listener; + + struct wl_signal event_signal; +@@ -45,6 +49,14 @@ data_device_destroy(struct data_device *data_device); + struct wl_resource * + data_device_bind(struct data_device *data_device, struct wl_client *client, + uint32_t version, uint32_t id); ++/* the selection a client set, as data, NULL for none */ ++struct data * ++data_device_client_selection(struct data_device *data_device); ++/* takes the selection for the compositor, host owned by the device now. what ++ * held it is told it is cancelled. a event with data set says it came from ++ * the compositor and not from a client */ ++void ++data_device_set_host(struct data_device *data_device, struct data *host); + void + data_device_offer_selection(struct data_device *data_device, + struct wl_client *client); +diff --git a/libswc/keyboard.c b/libswc/keyboard.c +index af9a286..3e7e531 100644 +--- a/libswc/keyboard.c ++++ b/libswc/keyboard.c +@@ -29,6 +29,7 @@ + #include "compositor.h" + #include "internal.h" + #include "surface.h" ++#include "xdg_shell.h" + #include "swc.h" + #include "util.h" + +@@ -304,6 +305,68 @@ keyboard_destroy(struct keyboard *keyboard) + free(keyboard); + } + ++static void update_modifiers(struct keyboard *keyboard); ++ ++/* A keymap made from names, put in place of the one in use. Clients are ++ * sent the new one on their keyboards and the keys held are let go, since ++ * what they meant belongs to the old layout. False, with the old keymap ++ * kept, when the names make none. */ ++bool ++keyboard_set_names(struct keyboard *keyboard, struct xkb_rule_names *names) ++{ ++ struct xkb next = keyboard->xkb; ++ struct wl_resource *resource; ++ ++ if (!(next.keymap.map = ++ xkb_keymap_new_from_names(next.context, names, 0))) { ++ WARNING("Could not create XKB keymap\n"); ++ return false; ++ } ++ if (!(next.state = xkb_state_new(next.keymap.map))) { ++ xkb_keymap_unref(next.keymap.map); ++ return false; ++ } ++ if (!update_keymap(&next)) { ++ xkb_state_unref(next.state); ++ xkb_keymap_unref(next.keymap.map); ++ return false; ++ } ++ ++ munmap(keyboard->xkb.keymap.area, keyboard->xkb.keymap.size); ++ close(keyboard->xkb.keymap.fd); ++ xkb_state_unref(keyboard->xkb.state); ++ xkb_keymap_unref(keyboard->xkb.keymap.map); ++ keyboard->xkb = next; ++ ++ wl_resource_for_each(resource, &keyboard->focus.active) ++ wl_keyboard_send_keymap(resource, ++ WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, ++ keyboard->xkb.keymap.fd, ++ keyboard->xkb.keymap.size); ++ wl_resource_for_each(resource, &keyboard->focus.inactive) ++ wl_keyboard_send_keymap(resource, ++ WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, ++ keyboard->xkb.keymap.fd, ++ keyboard->xkb.keymap.size); ++ return keyboard_reset(keyboard); ++} ++ ++/* The repeat rate and delay, told again to every client that can hear it. */ ++void ++keyboard_set_repeat(struct keyboard *keyboard, int32_t rate, int32_t delay) ++{ ++ struct wl_resource *resource; ++ ++ swc_repeat_rate = rate; ++ swc_repeat_delay = delay; ++ wl_resource_for_each(resource, &keyboard->focus.active) ++ if (wl_resource_get_version(resource) >= 4) ++ wl_keyboard_send_repeat_info(resource, rate, delay); ++ wl_resource_for_each(resource, &keyboard->focus.inactive) ++ if (wl_resource_get_version(resource) >= 4) ++ wl_keyboard_send_repeat_info(resource, rate, delay); ++} ++ + bool + keyboard_reset(struct keyboard *keyboard) + { +@@ -328,8 +391,6 @@ keyboard_reset(struct keyboard *keyboard) + * handler. */ + assert(keyboard->client_keys.size == 0); + keyboard->keys.size = 0; +- keyboard->modifier_state = (struct keyboard_modifier_state){0}; +- keyboard->modifiers = 0; + + if (!(state = xkb_state_new(keyboard->xkb.keymap.map))) { + ERROR("Failed to allocate new XKB state\n"); +@@ -338,6 +399,8 @@ keyboard_reset(struct keyboard *keyboard) + + xkb_state_unref(keyboard->xkb.state); + keyboard->xkb.state = state; ++ /* tell clients the modifiers are released */ ++ update_modifiers(keyboard); + + return true; + } +@@ -348,6 +411,8 @@ keyboard_reset(struct keyboard *keyboard) + void + keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view) + { ++ xdg_popups_focus_changed( ++ view && view->surface ? wl_resource_get_client(view->surface->resource) : NULL); + input_focus_set(&keyboard->focus, view); + } + +@@ -393,6 +458,56 @@ keyboard_bind(struct keyboard *keyboard, + return client_resource; + } + ++/* update modifiers from xkb state, run handlers on change */ ++static void ++update_modifiers(struct keyboard *keyboard) ++{ ++ struct xkb *xkb = &keyboard->xkb; ++ struct keyboard_handler *handler; ++ struct keyboard_modifier_state modifier_state; ++ ++ modifier_state.depressed = ++ xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED); ++ modifier_state.latched = ++ xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED); ++ modifier_state.locked = ++ xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED); ++ modifier_state.group = ++ xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE); ++ ++ if (modifier_state.depressed != keyboard->modifier_state.depressed || ++ modifier_state.latched != keyboard->modifier_state.latched || ++ modifier_state.locked != keyboard->modifier_state.locked || ++ modifier_state.group != keyboard->modifier_state.group) { ++ uint32_t mods_active = ++ modifier_state.depressed | modifier_state.latched; ++ ++ /* Update keyboard modifier state. */ ++ keyboard->modifier_state = modifier_state; ++ keyboard->modifiers = 0; ++ if (mods_active & (1 << keyboard->xkb.indices.ctrl)) { ++ keyboard->modifiers |= SWC_MOD_CTRL; ++ } ++ if (mods_active & (1 << keyboard->xkb.indices.alt)) { ++ keyboard->modifiers |= SWC_MOD_ALT; ++ } ++ if (mods_active & (1 << keyboard->xkb.indices.super)) { ++ keyboard->modifiers |= SWC_MOD_LOGO; ++ } ++ if (mods_active & (1 << keyboard->xkb.indices.shift)) { ++ keyboard->modifiers |= SWC_MOD_SHIFT; ++ } ++ ++ /* Run any modifier handlers. */ ++ wl_list_for_each(handler, &keyboard->handlers, link) ++ { ++ if (handler->modifiers) { ++ handler->modifiers(keyboard, &modifier_state); ++ } ++ } ++ } ++} ++ + void + keyboard_handle_key(struct keyboard *keyboard, + uint32_t time, +@@ -400,7 +515,6 @@ keyboard_handle_key(struct keyboard *keyboard, + uint32_t state) + { + struct key *key; +- struct keyboard_modifier_state modifier_state; + enum xkb_key_direction direction; + struct xkb *xkb = &keyboard->xkb; + struct keyboard_handler *handler; +@@ -455,44 +569,47 @@ update_xkb_state: + state == WL_KEYBOARD_KEY_STATE_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP; + xkb_state_update_key(xkb->state, XKB_KEY(value), direction); + +- modifier_state.depressed = +- xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED); +- modifier_state.latched = +- xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED); +- modifier_state.locked = +- xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED); +- modifier_state.group = +- xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE); ++ update_modifiers(keyboard); ++} + +- if (modifier_state.depressed != keyboard->modifier_state.depressed || +- modifier_state.latched != keyboard->modifier_state.latched || +- modifier_state.locked != keyboard->modifier_state.locked || +- modifier_state.group != keyboard->modifier_state.group) { +- uint32_t mods_active = +- modifier_state.depressed | modifier_state.latched; ++void ++keyboard_handle_modifiers(struct keyboard *keyboard, uint32_t depressed, ++ uint32_t latched, uint32_t locked, uint32_t group) ++{ ++ xkb_state_update_mask(keyboard->xkb.state, depressed, latched, locked, 0, ++ 0, group); ++ update_modifiers(keyboard); ++} + +- /* Update keyboard modifier state. */ +- keyboard->modifier_state = modifier_state; +- keyboard->modifiers = 0; +- if (mods_active & (1 << keyboard->xkb.indices.ctrl)) { +- keyboard->modifiers |= SWC_MOD_CTRL; +- } +- if (mods_active & (1 << keyboard->xkb.indices.alt)) { +- keyboard->modifiers |= SWC_MOD_ALT; +- } +- if (mods_active & (1 << keyboard->xkb.indices.super)) { +- keyboard->modifiers |= SWC_MOD_LOGO; +- } +- if (mods_active & (1 << keyboard->xkb.indices.shift)) { +- keyboard->modifiers |= SWC_MOD_SHIFT; +- } ++bool ++keyboard_set_keymap(struct keyboard *keyboard, struct xkb_keymap *map) ++{ ++ struct xkb old = keyboard->xkb; ++ struct wl_resource *resource; + +- /* Run any modifier handlers. */ +- wl_list_for_each(handler, &keyboard->handlers, link) +- { +- if (handler->modifiers) { +- handler->modifiers(keyboard, &modifier_state); +- } +- } ++ keyboard->xkb.keymap.map = xkb_keymap_ref(map); ++ if (!update_keymap(&keyboard->xkb)) { ++ xkb_keymap_unref(map); ++ keyboard->xkb = old; ++ return false; ++ } ++ ++ /* held keys belong to the old keymap */ ++ if (!keyboard_reset(keyboard)) { ++ return false; + } ++ munmap(old.keymap.area, old.keymap.size); ++ close(old.keymap.fd); ++ xkb_keymap_unref(old.keymap.map); ++ ++ wl_resource_for_each(resource, &keyboard->focus.active) ++ wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, ++ keyboard->xkb.keymap.fd, ++ keyboard->xkb.keymap.size); ++ wl_resource_for_each(resource, &keyboard->focus.inactive) ++ wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, ++ keyboard->xkb.keymap.fd, ++ keyboard->xkb.keymap.size); ++ ++ return true; + } +diff --git a/libswc/keyboard.h b/libswc/keyboard.h +index 28c5882..01ac208 100644 +--- a/libswc/keyboard.h ++++ b/libswc/keyboard.h +@@ -92,6 +92,10 @@ void + keyboard_destroy(struct keyboard *keyboard); + bool + keyboard_reset(struct keyboard *keyboard); ++bool ++keyboard_set_names(struct keyboard *keyboard, struct xkb_rule_names *names); ++void ++keyboard_set_repeat(struct keyboard *keyboard, int32_t rate, int32_t delay); + void + keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view); + struct wl_resource * +@@ -100,5 +104,12 @@ keyboard_bind(struct keyboard *keyboard, struct wl_client *client, + void + keyboard_handle_key(struct keyboard *keyboard, uint32_t time, uint32_t key, + uint32_t state); ++/* for backends that get modifier state from outside */ ++void ++keyboard_handle_modifiers(struct keyboard *keyboard, uint32_t depressed, ++ uint32_t latched, uint32_t locked, uint32_t group); ++/* swap the keymap and resend it */ ++bool ++keyboard_set_keymap(struct keyboard *keyboard, struct xkb_keymap *map); + + #endif +diff --git a/libswc/pointer.c b/libswc/pointer.c +index ab23c3a..e785cbd 100644 +--- a/libswc/pointer.c ++++ b/libswc/pointer.c +@@ -26,6 +26,7 @@ + #include "compositor.h" + #include "cursor/cursor_data.h" + #include "event.h" ++#include "layer_shell.h" + #include "internal.h" + #ifdef ENABLE_DRM + #include "plane.h" +@@ -41,6 +42,19 @@ + #include <string.h> + #include <wld/wld.h> + ++/* the pictures of cursors by name, and the one the window manager shows */ ++#define SHAPES 64 ++struct shape { ++ char name[32]; ++ const uint32_t *data; ++ uint32_t width, height; ++ int32_t hotspot_x, hotspot_y; ++}; ++static struct shape shapes[SHAPES]; ++static char shape_override[32]; ++static void (*motion_handler)(void *data, int32_t x, int32_t y); ++static void *motion_data; ++ + static enum swc_cursor_kind cursor_override = SWC_CURSOR_DEFAULT; + static enum swc_cursor_mode cursor_mode = SWC_CURSOR_MODE_CLIENT; + +@@ -62,6 +76,9 @@ swc_pointer_send_button(uint32_t time, uint32_t button, uint32_t state) + return; + } + ++ if (state == WL_POINTER_BUTTON_STATE_PRESSED) { ++ layer_shell_pressed(pointer->focus.view); ++ } + serial = wl_display_next_serial(swc.display); + wl_resource_for_each(resource, &pointer->focus.active) + wl_pointer_send_button(resource, serial, time, button, state); +@@ -208,15 +225,24 @@ attach(struct view *view, struct wld_buffer *buffer) + view_update_screens(view); + } + ++ if (swc.backend->set_cursor) { ++ swc.backend->set_cursor(buffer ? pointer->cursor.buffer : NULL, ++ pointer->cursor.hotspot.x, ++ pointer->cursor.hotspot.y); ++ return 0; ++ } ++ + #ifdef ENABLE_DRM +- wl_list_for_each(screen, &swc.screens, link) { +- view_attach(&screen->planes.cursor->view, +- buffer ? pointer->cursor.buffer : NULL); +- view_update(&screen->planes.cursor->view); ++ if (swc.video == SWC_VIDEO_DRM) { ++ wl_list_for_each(screen, &swc.screens, link) { ++ view_attach(&screen->planes.cursor->view, ++ buffer ? pointer->cursor.buffer : NULL); ++ view_update(&screen->planes.cursor->view); ++ } ++ return 0; + } +-#else +- compositor_damage_all(); + #endif ++ compositor_damage_all(); + + return 0; + } +@@ -232,15 +258,22 @@ move(struct view *view, int32_t x, int32_t y) + view_update_screens(view); + } + ++ /* the backend shows the cursor itself and already knows where it is */ ++ if (swc.backend->set_cursor) { ++ return true; ++ } ++ + #ifdef ENABLE_DRM +- wl_list_for_each(screen, &swc.screens, link) { +- view_move(&screen->planes.cursor->view, view->geometry.x, +- view->geometry.y); +- view_update(&screen->planes.cursor->view); ++ if (swc.video == SWC_VIDEO_DRM) { ++ wl_list_for_each(screen, &swc.screens, link) { ++ view_move(&screen->planes.cursor->view, view->geometry.x, ++ view->geometry.y); ++ view_update(&screen->planes.cursor->view); ++ } ++ return true; + } +-#else +- compositor_damage_all(); + #endif ++ compositor_damage_all(); + + return true; + } +@@ -347,6 +380,117 @@ swc_clear_cursor_image(enum swc_cursor_kind kind) + apply_cursor_override(pointer); + } + ++static const struct shape * ++shape_named(const char *name) ++{ ++ if (!name || !*name) { ++ return NULL; ++ } ++ for (int i = 0; i < SHAPES; i++) { ++ if (shapes[i].data && !strcmp(shapes[i].name, name)) { ++ return &shapes[i]; ++ } ++ } ++ return NULL; ++} ++ ++EXPORT void ++swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888, ++ uint32_t width, uint32_t height, int32_t hotspot_x, ++ int32_t hotspot_y) ++{ ++ int free_slot = -1; ++ ++ if (!name || !*name || strlen(name) >= sizeof shapes[0].name || !argb8888 || ++ !width || !height) { ++ return; ++ } ++ for (int i = 0; i < SHAPES; i++) { ++ if (shapes[i].data && !strcmp(shapes[i].name, name)) { ++ free_slot = i; ++ break; ++ } ++ if (!shapes[i].data && free_slot < 0) { ++ free_slot = i; ++ } ++ } ++ if (free_slot < 0) { ++ return; ++ } ++ strcpy(shapes[free_slot].name, name); ++ shapes[free_slot].data = argb8888; ++ shapes[free_slot].width = width; ++ shapes[free_slot].height = height; ++ shapes[free_slot].hotspot_x = hotspot_x; ++ shapes[free_slot].hotspot_y = hotspot_y; ++} ++ ++EXPORT void ++swc_set_cursor_shape(const char *name) ++{ ++ struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL; ++ ++ if (!name) { ++ name = ""; ++ } ++ if (!strcmp(shape_override, name) || strlen(name) >= sizeof shape_override) { ++ return; ++ } ++ strcpy(shape_override, name); ++ apply_cursor_override(pointer); ++} ++ ++EXPORT void ++swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y), ++ void *data) ++{ ++ motion_handler = handler; ++ motion_data = data; ++} ++ ++void ++pointer_motion_notify(int32_t x, int32_t y) ++{ ++ if (motion_handler) { ++ motion_handler(motion_data, x, y); ++ } ++} ++ ++/* the cursor a client asked for by name: its picture, drawn by the compositor, where the client's ++ * own cursor surface would be */ ++void ++pointer_set_shape(struct pointer *pointer, const char *name) ++{ ++ const struct shape *shape = shape_named(name); ++ struct cursor shape_cursor; ++ union wld_object object; ++ struct wld_buffer *buffer; ++ ++ if (!shape && !(shape = shape_named("default"))) { ++ return; ++ } ++ if (pointer->cursor.internal_buffer) { ++ wld_buffer_unreference(pointer->cursor.internal_buffer); ++ pointer->cursor.internal_buffer = NULL; ++ } ++ drop_client_cursor_surface(pointer); ++ shape_cursor.width = (int)shape->width; ++ shape_cursor.height = (int)shape->height; ++ object.ptr = (void *)shape->data; ++ buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, ++ shape_cursor.width, shape_cursor.height, ++ WLD_FORMAT_ARGB8888, shape_cursor.width * 4); ++ if (!buffer) { ++ WARNING("Failed to create cursor buffer\n"); ++ return; ++ } ++ pointer->cursor.internal_buffer = buffer; ++ pointer->cursor.hotspot.x = shape->hotspot_x; ++ pointer->cursor.hotspot.y = shape->hotspot_y; ++ update_cursor(pointer); ++ view_attach(&pointer->cursor.view, buffer); ++} ++ + void + pointer_set_cursor(struct pointer *pointer, uint32_t id) + { +@@ -355,7 +499,20 @@ pointer_set_cursor(struct pointer *pointer, uint32_t id) + union wld_object object = {.ptr = &cursor_data[cursor->offset]}; + struct wld_buffer *buffer; + +- if (id == cursor_left_ptr) { ++ if (id == cursor_left_ptr && shape_named(shape_override)) { ++ /* the window manager's, where no client has the pointer */ ++ static struct cursor shape_cursor; ++ const struct shape *shape = shape_named(shape_override); ++ ++ shape_cursor.width = (int)shape->width; ++ shape_cursor.height = (int)shape->height; ++ shape_cursor.hotspot_x = shape->hotspot_x; ++ shape_cursor.hotspot_y = shape->hotspot_y; ++ shape_cursor.offset = 0; ++ cursor = &shape_cursor; ++ data = shape->data; ++ object.ptr = (void *)data; ++ } else if (id == cursor_left_ptr) { + enum swc_cursor_kind kind = cursor_override; + if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) { + kind = SWC_CURSOR_DEFAULT; +@@ -431,6 +588,9 @@ client_handle_button(struct pointer_handler *handler, uint32_t time, + if (wl_list_empty(&pointer->focus.active)) { + return false; + } ++ if (state == WL_POINTER_BUTTON_STATE_PRESSED) { ++ layer_shell_pressed(pointer->focus.view); ++ } + + wl_resource_for_each(resource, &pointer->focus.active) + wl_pointer_send_button(resource, button->press.serial, time, +@@ -548,6 +708,17 @@ pointer_initialize(struct pointer *pointer) + void + pointer_finalize(struct pointer *pointer) + { ++ if (pointer->cursor.surface) { ++ surface_set_view(pointer->cursor.surface, NULL); ++ wl_list_remove(&pointer->cursor.destroy_listener.link); ++ } ++ if (swc.backend->set_cursor) ++ swc.backend->set_cursor(NULL, 0, 0); ++ view_finalize(&pointer->cursor.view); ++ if (pointer->cursor.internal_buffer) ++ wld_buffer_unreference(pointer->cursor.internal_buffer); ++ wld_buffer_unreference(pointer->cursor.buffer); ++ wl_array_release(&pointer->buttons); + input_focus_finalize(&pointer->focus); + pixman_region32_fini(&pointer->region); + } +@@ -768,8 +939,6 @@ pointer_handle_absolute_motion(struct pointer *pointer, uint32_t time, + break; + } + } +- +- update_cursor(pointer); + } + + void +diff --git a/libswc/pointer.h b/libswc/pointer.h +index 21a54cd..c6e2126 100644 +--- a/libswc/pointer.h ++++ b/libswc/pointer.h +@@ -86,6 +86,10 @@ void + pointer_set_region(struct pointer *pointer, pixman_region32_t *region); + void + pointer_set_cursor(struct pointer *pointer, uint32_t id); ++void ++pointer_set_shape(struct pointer *pointer, const char *name); ++void ++pointer_motion_notify(int32_t x, int32_t y); + + struct button * + pointer_get_button(struct pointer *pointer, uint32_t serial); +diff --git a/libswc/seat.c b/libswc/seat.c +index 413a396..02d43a0 100644 +--- a/libswc/seat.c ++++ b/libswc/seat.c +@@ -22,6 +22,7 @@ + */ + + #include "seat.h" ++#include "backend.h" + #include "compositor.h" + #include "data_device.h" + #include "event.h" +@@ -50,8 +51,14 @@ + #include <libudev.h> + #endif + ++/* The netlink groups libinput listens on for devices that appear: a bit ++ * for each group, and group 1 is the kernel's own uevents, which are there ++ * with no device manager running. Another group hears nothing unless ++ * something forwards to it, and a device that appears after the compositor ++ * started (a touchpad whose driver binds late, a USB keyboard) is then not ++ * seen until the seat is resumed. */ + #ifndef NETLINK_MASK +-#define NETLINK_MASK 4 ++#define NETLINK_MASK 1 + #endif + + struct seat { +@@ -122,11 +129,13 @@ handle_swc_event(struct wl_listener *listener, void *data) + + switch (ev->type) { + case SWC_EVENT_DEACTIVATED: +- libinput_suspend(seat->libinput); ++ if (seat->libinput) { ++ libinput_suspend(seat->libinput); ++ } + keyboard_reset(seat->base.keyboard); + break; + case SWC_EVENT_ACTIVATED: +- if (libinput_resume(seat->libinput) != 0) { ++ if (seat->libinput && libinput_resume(seat->libinput) != 0) { + WARNING("Failed to resume libinput context\n"); + } + break; +@@ -484,7 +493,12 @@ seat_create(struct wl_display *display, const char *seat_name) + } + seat->base.pointer = &seat->pointer; + +- if (!initialize_libinput(seat)) { ++ seat->libinput = NULL; ++ seat->libinput_source = NULL; ++ if (swc.backend->virtual_input) { ++ update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD | ++ WL_SEAT_CAPABILITY_POINTER); ++ } else if (!initialize_libinput(seat)) { + goto error6; + } + +@@ -511,11 +525,13 @@ seat_destroy(struct swc_seat *seat_base) + { + struct seat *seat = wl_container_of(seat_base, seat, base); + +- wl_event_source_remove(seat->libinput_source); +- libinput_unref(seat->libinput); ++ if (seat->libinput) { ++ wl_event_source_remove(seat->libinput_source); ++ libinput_unref(seat->libinput); + #ifdef ENABLE_LIBUDEV +- udev_unref(seat->udev); ++ udev_unref(seat->udev); + #endif ++ } + + pointer_finalize(&seat->pointer); + keyboard_destroy(seat->base.keyboard); +diff --git a/libswc/selection.c b/libswc/selection.c +new file mode 100644 +index 0000000..b6845e0 +--- /dev/null ++++ b/libswc/selection.c +@@ -0,0 +1,100 @@ ++/* swc: selection.c ++ * ++ * the clipboard for a compositor that keeps it itself, see swc.h ++ * ++ * Permission is hereby granted, free of charge, to any person obtaining a copy ++ * of this software and associated documentation files (the "Software"), to deal ++ * in the Software without restriction, including without limitation the rights ++ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ++ * copies of the Software, and to permit persons to whom the Software is ++ * furnished to do so, subject to the following conditions: ++ * ++ * The above copyright notice and this permission notice shall be included in ++ * all copies or substantial portions of the Software. ++ */ ++ ++#include "swc.h" ++#include "data.h" ++#include "data_device.h" ++#include "event.h" ++#include "internal.h" ++#include "seat.h" ++#include "util.h" ++ ++#include <string.h> ++ ++#define MAX_TYPES 64 ++ ++static struct { ++ const struct swc_selection_handler *handler; ++ void *data; ++ struct wl_listener listener; ++ bool hooked; ++} selection; ++ ++static void ++handle_event(struct wl_listener *listener, void *data) ++{ ++ struct event *ev = data; ++ struct data *source; ++ const char *types[MAX_TYPES]; ++ unsigned i, count = 0; ++ ++ if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED || ev->data || !selection.handler) { ++ return; ++ } ++ if ((source = data_device_client_selection(swc.seat->data_device))) { ++ count = data_type_count(source); ++ if (count > MAX_TYPES) { ++ count = MAX_TYPES; ++ } ++ for (i = 0; i < count; i++) { ++ types[i] = data_type_at(source, i); ++ } ++ } ++ selection.handler->changed(selection.data, types, count); ++} ++ ++EXPORT void ++swc_selection_set_handler(const struct swc_selection_handler *handler, void *data) ++{ ++ selection.handler = handler; ++ selection.data = data; ++ if (!selection.hooked && swc.seat) { ++ selection.listener.notify = handle_event; ++ wl_signal_add(&swc.seat->data_device->event_signal, &selection.listener); ++ selection.hooked = true; ++ } ++} ++ ++EXPORT bool ++swc_selection_request(const char *mime_type, int fd) ++{ ++ struct data *source = data_device_client_selection(swc.seat->data_device); ++ unsigned i; ++ ++ if (!source) { ++ return false; ++ } ++ for (i = 0; i < data_type_count(source); i++) { ++ if (strcmp(data_type_at(source, i), mime_type) == 0) { ++ data_request(source, mime_type, fd); ++ return true; ++ } ++ } ++ return false; ++} ++ ++EXPORT bool ++swc_selection_offer(const char *const *mime_types, unsigned count, ++ const struct swc_selection_source *source, void *data) ++{ ++ struct data_host host = { .send = source->send, .cancelled = source->cancelled }; ++ struct data *offered = data_host_new(mime_types, count, &host, data); ++ ++ if (!offered) { ++ return false; ++ } ++ data_device_set_host(swc.seat->data_device, offered); ++ return true; ++} diff --git a/patches/keyboardResetTellsModifiers.patch b/patches/keyboardResetTellsModifiers.patch @@ -1,43 +0,0 @@ -From df5b8fb88b24945c1cc5b3f0cf82eed5b880853c Mon Sep 17 00:00:00 2001 -From: xplshn <noreply@localhost> -Date: Mon, 16 Nov 2026 13:07:18 -0300 -Subject: [PATCH 03/19] keyboard: clear modifiers for clients on reset - ---- - libswc/keyboard.c | 6 ++++-- - 1 file changed, 4 insertions(+), 2 deletions(-) - -diff --git a/libswc/keyboard.c b/libswc/keyboard.c -index af9a286..4d0083a 100644 ---- a/libswc/keyboard.c -+++ b/libswc/keyboard.c -@@ -304,6 +304,8 @@ keyboard_destroy(struct keyboard *keyboard) - free(keyboard); - } - -+static void update_modifiers(struct keyboard *keyboard); -+ - bool - keyboard_reset(struct keyboard *keyboard) - { -@@ -328,8 +330,6 @@ keyboard_reset(struct keyboard *keyboard) - * handler. */ - assert(keyboard->client_keys.size == 0); - keyboard->keys.size = 0; -- keyboard->modifier_state = (struct keyboard_modifier_state){0}; -- keyboard->modifiers = 0; - - if (!(state = xkb_state_new(keyboard->xkb.keymap.map))) { - ERROR("Failed to allocate new XKB state\n"); -@@ -338,6 +338,8 @@ keyboard_reset(struct keyboard *keyboard) - - xkb_state_unref(keyboard->xkb.state); - keyboard->xkb.state = state; -+ /* tell clients the modifiers are released */ -+ update_modifiers(keyboard); - - return true; - } --- -2.56.1 - diff --git a/patches/keymapAtRuntime.patch b/patches/keymapAtRuntime.patch @@ -1,129 +0,0 @@ ---- a/libswc/keyboard.c -+++ b/libswc/keyboard.c -@@ -306,6 +306,66 @@ - - static void update_modifiers(struct keyboard *keyboard); - -+/* A keymap made from names, put in place of the one in use. Clients are -+ * sent the new one on their keyboards and the keys held are let go, since -+ * what they meant belongs to the old layout. False, with the old keymap -+ * kept, when the names make none. */ -+bool -+keyboard_set_names(struct keyboard *keyboard, struct xkb_rule_names *names) -+{ -+ struct xkb next = keyboard->xkb; -+ struct wl_resource *resource; -+ -+ if (!(next.keymap.map = -+ xkb_keymap_new_from_names(next.context, names, 0))) { -+ WARNING("Could not create XKB keymap\n"); -+ return false; -+ } -+ if (!(next.state = xkb_state_new(next.keymap.map))) { -+ xkb_keymap_unref(next.keymap.map); -+ return false; -+ } -+ if (!update_keymap(&next)) { -+ xkb_state_unref(next.state); -+ xkb_keymap_unref(next.keymap.map); -+ return false; -+ } -+ -+ munmap(keyboard->xkb.keymap.area, keyboard->xkb.keymap.size); -+ close(keyboard->xkb.keymap.fd); -+ xkb_state_unref(keyboard->xkb.state); -+ xkb_keymap_unref(keyboard->xkb.keymap.map); -+ keyboard->xkb = next; -+ -+ wl_resource_for_each(resource, &keyboard->focus.active) -+ wl_keyboard_send_keymap(resource, -+ WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, -+ keyboard->xkb.keymap.fd, -+ keyboard->xkb.keymap.size); -+ wl_resource_for_each(resource, &keyboard->focus.inactive) -+ wl_keyboard_send_keymap(resource, -+ WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, -+ keyboard->xkb.keymap.fd, -+ keyboard->xkb.keymap.size); -+ return keyboard_reset(keyboard); -+} -+ -+/* The repeat rate and delay, told again to every client that can hear it. */ -+void -+keyboard_set_repeat(struct keyboard *keyboard, int32_t rate, int32_t delay) -+{ -+ struct wl_resource *resource; -+ -+ swc_repeat_rate = rate; -+ swc_repeat_delay = delay; -+ wl_resource_for_each(resource, &keyboard->focus.active) -+ if (wl_resource_get_version(resource) >= 4) -+ wl_keyboard_send_repeat_info(resource, rate, delay); -+ wl_resource_for_each(resource, &keyboard->focus.inactive) -+ if (wl_resource_get_version(resource) >= 4) -+ wl_keyboard_send_repeat_info(resource, rate, delay); -+} -+ - bool - keyboard_reset(struct keyboard *keyboard) - { ---- a/libswc/keyboard.h -+++ b/libswc/keyboard.h -@@ -92,6 +92,10 @@ - keyboard_destroy(struct keyboard *keyboard); - bool - keyboard_reset(struct keyboard *keyboard); -+bool -+keyboard_set_names(struct keyboard *keyboard, struct xkb_rule_names *names); -+void -+keyboard_set_repeat(struct keyboard *keyboard, int32_t rate, int32_t delay); - void - keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view); - struct wl_resource * ---- a/libswc/swc.c -+++ b/libswc/swc.c -@@ -491,3 +491,19 @@ - shm_destroy(swc.shm); - backend_finalize(); - } -+ -+EXPORT bool -+swc_set_keyboard_layout(const char *layout, const char *variant, -+ const char *options) -+{ -+ struct xkb_rule_names names = { -+ .layout = layout, .variant = variant, .options = options}; -+ -+ return keyboard_set_names(swc.seat->keyboard, &names); -+} -+ -+EXPORT void -+swc_set_keyboard_repeat(int32_t rate, int32_t delay) -+{ -+ keyboard_set_repeat(swc.seat->keyboard, rate, delay); -+} ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -637,6 +637,22 @@ - */ - extern int32_t swc_repeat_rate, swc_repeat_delay; - -+/** -+ * Change the keyboard layout while running: names as XKB's, any of them -+ * NULL for its default. False, the old layout kept, when they make no -+ * keymap. -+ */ -+bool -+swc_set_keyboard_layout(const char *layout, const char *variant, -+ const char *options); -+ -+/** -+ * Change the repeat rate (characters per second) and delay (ms), which -+ * clients are told again. -+ */ -+void -+swc_set_keyboard_repeat(int32_t rate, int32_t delay); -+ - /* Bindings {{{ */ - - enum { diff --git a/patches/launch.patch b/patches/launch.patch @@ -0,0 +1,425 @@ +Subject: launch + +diff --git a/launch/launch.c b/launch/launch.c +index 47fcd38..d1a9031 100644 +--- a/launch/launch.c ++++ b/launch/launch.c +@@ -47,6 +47,8 @@ + #include <sys/stat.h> + #include <sys/types.h> + #include <sys/wait.h> ++#include <dirent.h> ++#include <time.h> + #ifdef __linux__ + #include <sys/sysmacros.h> + #endif +@@ -81,6 +83,11 @@ mode_t _permit_setugid = S_ISUID; + #endif + + #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0]) ++#ifdef __linux__ ++#define ESCAPE_KEYBOARDS_MAX 32 ++#else ++#define ESCAPE_KEYBOARDS_MAX 0 ++#endif + + static void + activate(void); +@@ -97,6 +104,23 @@ static int tty_fd; + static bool active; + static pid_t child_pid = -1; + ++/* with -e, read the keyboards here so a wedged server cant block these: ++ * ctrl+alt+f1..f12 switch vt ++ * ctrl+alt+backspace x2 end the session */ ++#ifdef __linux__ ++#define ESCAPE_KEYBOARDS ESCAPE_KEYBOARDS_MAX ++#define ESCAPE_WINDOW 3000 ++#define ESCAPE_RESCAN 3000 ++static struct { ++ int fd; ++ dev_t rdev; ++} keyboards[ESCAPE_KEYBOARDS]; ++static int num_keyboards; ++static bool escape_enabled; ++static bool ctrl_l, ctrl_r, alt_l, alt_r; ++static long long last_zap; ++#endif ++ + static struct { + bool altered; + int vt; +@@ -110,7 +134,7 @@ cleanup(void); + static noreturn void + usage(const char *name) + { +- fprintf(stderr, "usage: %s [-n] [-t tty] [--] server [args...]\n", name); ++ fprintf(stderr, "usage: %s [-ne] [-t tty] [--] server [args...]\n", name); + exit(2); + } + +@@ -195,14 +219,77 @@ cleanup(void) + kill(child_pid, SIGTERM); + } + ++/* async-signal-safe: console back to text, keyboard back on */ ++static void ++restore_vt_raw(void) ++{ ++ struct vt_mode mode = {.mode = VT_AUTO}; ++ ++ if (!original_vt_state.altered) ++ return; ++ ioctl(tty_fd, VT_SETMODE, &mode); ++#ifndef __OpenBSD__ ++ ioctl(tty_fd, KDSKBMODE, original_vt_state.kb_mode); ++#endif ++ ioctl(tty_fd, KDSETMODE, original_vt_state.console_mode); ++} ++ ++static void ++handle_fatal(int sig) ++{ ++ restore_vt_raw(); ++ if (child_pid > 0) ++ kill(child_pid, SIGKILL); ++ signal(sig, SIG_DFL); ++ raise(sig); ++} ++ ++static long long ++now_ms(void) ++{ ++ struct timespec ts; ++ ++ clock_gettime(CLOCK_MONOTONIC, &ts); ++ return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; ++} ++ ++/* restore the console, ask the session to end, kill it after 3s */ ++static noreturn void ++end_session(int code) ++{ ++ long long until; ++ int status; ++ pid_t pid; ++ ++ cleanup(); ++ until = now_ms() + 3000; ++ while (child_pid > 0) { ++ pid = waitpid(child_pid, &status, WNOHANG); ++ if (pid == child_pid || (pid < 0 && errno == ECHILD)) ++ break; ++ if (now_ms() >= until) { ++ kill(child_pid, SIGKILL); ++ waitpid(child_pid, &status, 0); ++ break; ++ } ++ nanosleep(&(struct timespec){0, 50000000}, NULL); ++ } ++ exit(code); ++} ++ + static void + activate(void) + { + struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_ACTIVATE}; + + start_devices(); +- send(sock[0], &event, sizeof(event), 0); ++ /* a hung server must not hold the vt */ ++ send(sock[0], &event, sizeof(event), MSG_DONTWAIT | MSG_NOSIGNAL); + active = true; ++#ifdef __linux__ ++ ctrl_l = ctrl_r = alt_l = alt_r = false; ++ last_zap = 0; ++#endif + } + + static void +@@ -210,7 +297,7 @@ deactivate(void) + { + struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_DEACTIVATE}; + +- send(sock[0], &event, sizeof(event), 0); ++ send(sock[0], &event, sizeof(event), MSG_DONTWAIT | MSG_NOSIGNAL); + stop_devices(true); + active = false; + } +@@ -273,6 +360,18 @@ handle_socket_data(int socket) + if (!active) { + goto fail; + } ++ /* the same device again means a restarted server: drop the stale fd */ ++ for (int i = 0; i < num_input_fds; ++i) { ++ struct stat old; ++ ++ if (fstat(input_fds[i], &old) == 0 && old.st_rdev == st.st_rdev) { ++#ifdef EVIOCREVOKE ++ ioctl(input_fds[i], EVIOCREVOKE, 0); ++#endif ++ close(input_fds[i]); ++ input_fds[i--] = input_fds[--num_input_fds]; ++ } ++ } + if (num_input_fds == ARRAY_LENGTH(input_fds)) { + fprintf(stderr, "too many input devices opened\n"); + goto fail; +@@ -291,11 +390,23 @@ handle_socket_data(int socket) + input_fds[num_input_fds++] = fd; + } else if (device_is_drm(st.st_rdev)) { + #ifdef ENABLE_DRM ++ /* the stale fd is still master and would keep the new server from drawing */ ++ for (int i = 0; i < num_drm_fds; ++i) { ++ struct stat old; ++ ++ if (fstat(drm_fds[i], &old) == 0 && old.st_rdev == st.st_rdev) { ++ drmDropMaster(drm_fds[i]); ++ close(drm_fds[i]); ++ drm_fds[i--] = drm_fds[--num_drm_fds]; ++ } ++ } + if (num_drm_fds == ARRAY_LENGTH(drm_fds)) { + fprintf(stderr, "too many DRM devices opened\n"); + goto fail; + } + drm_fds[num_drm_fds++] = fd; ++ if (active && drmSetMaster(fd) < 0) ++ fprintf(stderr, "drmSetMaster %s: %s\n", path, strerror(errno)); + #else + fprintf(stderr, "DRM device requested by non-DRM backend\n"); + goto fail; +@@ -548,35 +659,157 @@ error0: + exit(EXIT_FAILURE); + } + ++#ifdef __linux__ ++#define TEST_BIT(bits, n) ((bits)[(n) / (8 * sizeof(long))] >> ((n) % (8 * sizeof(long))) & 1) ++ ++/* open every keyboard not already open */ ++static void ++scan_keyboards(void) ++{ ++ DIR *dir; ++ struct dirent *entry; ++ char path[PATH_MAX]; ++ unsigned long keys[KEY_MAX / (8 * sizeof(long)) + 1]; ++ struct stat st; ++ int fd, i; ++ ++ if (!(dir = opendir("/dev/input"))) ++ return; ++ while ((entry = readdir(dir)) && num_keyboards < ESCAPE_KEYBOARDS) { ++ if (strncmp(entry->d_name, "event", 5) != 0) ++ continue; ++ snprintf(path, sizeof(path), "/dev/input/%s", entry->d_name); ++ if (stat(path, &st) < 0 || !device_is_input(st.st_rdev)) ++ continue; ++ for (i = 0; i < num_keyboards && keyboards[i].rdev != st.st_rdev; ++i) ++ ; ++ if (i < num_keyboards) ++ continue; ++ if ((fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)) < 0) ++ continue; ++ memset(keys, 0, sizeof(keys)); ++ if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0 || ++ !TEST_BIT(keys, KEY_BACKSPACE) || !TEST_BIT(keys, KEY_LEFTCTRL)) { ++ close(fd); ++ continue; ++ } ++ keyboards[num_keyboards].fd = fd; ++ keyboards[num_keyboards].rdev = st.st_rdev; ++ ++num_keyboards; ++ } ++ closedir(dir); ++} ++ ++static void ++drop_keyboard(int i) ++{ ++ close(keyboards[i].fd); ++ keyboards[i] = keyboards[--num_keyboards]; ++} ++ ++static void ++handle_keyboard(int i) ++{ ++ struct input_event ev[64]; ++ ssize_t n; ++ size_t k; ++ ++ while ((n = read(keyboards[i].fd, ev, sizeof(ev))) > 0) { ++ for (k = 0; k < (size_t)n / sizeof(ev[0]); ++k) { ++ bool down = ev[k].value != 0; ++ ++ if (ev[k].type != EV_KEY) ++ continue; ++ switch (ev[k].code) { ++ case KEY_LEFTCTRL: ctrl_l = down; continue; ++ case KEY_RIGHTCTRL: ctrl_r = down; continue; ++ case KEY_LEFTALT: alt_l = down; continue; ++ case KEY_RIGHTALT: alt_r = down; continue; ++ } ++ /* presses with ctrl+alt held, on our vt only */ ++ if (ev[k].value != 1 || !active || !(ctrl_l || ctrl_r) || !(alt_l || alt_r)) ++ continue; ++ if (ev[k].code == KEY_BACKSPACE) { ++ long long now = now_ms(); ++ ++ if (last_zap && now - last_zap <= ESCAPE_WINDOW) { ++ fprintf(stderr, "swc-launch: Ctrl+Alt+Backspace twice, ending the session\n"); ++ end_session(EXIT_FAILURE); ++ } ++ last_zap = now; ++ fprintf(stderr, "swc-launch: Ctrl+Alt+Backspace; again within 3s ends the session\n"); ++ } else if (ev[k].code >= KEY_F1 && ev[k].code <= KEY_F10) { ++ ioctl(tty_fd, VT_ACTIVATE, ev[k].code - KEY_F1 + 1); ++ } else if (ev[k].code == KEY_F11 || ev[k].code == KEY_F12) { ++ ioctl(tty_fd, VT_ACTIVATE, ev[k].code == KEY_F11 ? 11 : 12); ++ } ++ } ++ } ++ if (n < 0 && errno != EAGAIN && errno != EINTR) ++ drop_keyboard(i); /* unplugged */ ++} ++#endif ++ + static void + run(int fd) + { +- struct pollfd fds[] = { +- {.fd = fd, .events = POLLIN}, +- {.fd = sigfd[0], .events = POLLIN}, +- }; +- int status; ++ struct pollfd fds[2 + ESCAPE_KEYBOARDS_MAX]; ++ int status, nfds, i; ++ long long next_scan = 0; ++ pid_t pid; + char sig; + + for (;;) { +- if (poll(fds, ARRAY_LENGTH(fds), -1) < 0) { ++ nfds = 0; ++ fds[nfds++] = (struct pollfd){.fd = fd, .events = POLLIN}; ++ fds[nfds++] = (struct pollfd){.fd = sigfd[0], .events = POLLIN}; ++#ifdef __linux__ ++ if (escape_enabled && now_ms() >= next_scan) { ++ scan_keyboards(); ++ next_scan = now_ms() + ESCAPE_RESCAN; ++ } ++ for (i = 0; i < num_keyboards; ++i) ++ fds[nfds++] = (struct pollfd){.fd = keyboards[i].fd, .events = POLLIN}; ++#endif ++ if (poll(fds, nfds, escape_enabled ? ESCAPE_RESCAN : -1) < 0) { + if (errno == EINTR) { + continue; + } + die("poll:"); + } +- if (fds[0].revents) { ++ if (fds[0].revents & POLLIN) { + handle_socket_data(fd); ++ } else if (fds[0].revents & (POLLHUP | POLLERR)) { ++ /* server gone: stop polling its socket, sigchld ends the session */ ++ fds[0].fd = -1; ++ fd = -1; ++ } ++#ifdef __linux__ ++ for (i = nfds - 1; i >= 2; --i) { ++ if (fds[i].revents) ++ handle_keyboard(i - 2); + } ++#endif + if (fds[1].revents) { + if (read(sigfd[0], &sig, 1) <= 0) { + continue; + } + switch (sig) { + case SIGCHLD: +- wait(&status); +- cleanup(); +- exit(WEXITSTATUS(status)); ++ while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { ++ if (pid == child_pid) { ++ child_pid = -1; ++ cleanup(); ++ exit(WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status)); ++ } ++ } ++ break; ++ case SIGTERM: ++ case SIGINT: ++ case SIGHUP: ++ case SIGQUIT: ++ fprintf(stderr, "swc-launch: signal %d, ending the session\n", sig); ++ end_session(128 + sig); + case SIGUSR1: + deactivate(); + ioctl(tty_fd, VT_RELDISP, 1); +@@ -604,11 +837,16 @@ main(int argc, char *argv[]) + pid_t pid; + posix_spawnattr_t attr; + +- while ((option = getopt(argc, argv, "nt:")) != -1) { ++ while ((option = getopt(argc, argv, "net:")) != -1) { + switch (option) { + case 'n': + nflag = true; + break; ++ case 'e': ++#ifdef __linux__ ++ escape_enabled = true; ++#endif ++ break; + case 't': + vt = optarg; + break; +@@ -647,11 +885,34 @@ main(int argc, char *argv[]) + if (sigaction(SIGUSR2, &action, NULL) == -1) { + die("sigaction SIGUSR2:"); + } ++ /* end the session, console first */ ++ if (sigaction(SIGTERM, &action, NULL) == -1 || ++ sigaction(SIGINT, &action, NULL) == -1 || ++ sigaction(SIGHUP, &action, NULL) == -1 || ++ sigaction(SIGQUIT, &action, NULL) == -1) { ++ die("sigaction:"); ++ } ++ { ++ /* restore the console on our own crash */ ++ struct sigaction fatal = {.sa_handler = handle_fatal, .sa_flags = SA_RESETHAND}; ++ int sigs[] = {SIGSEGV, SIGBUS, SIGABRT, SIGFPE, SIGILL}; + ++ for (size_t i = 0; i < ARRAY_LENGTH(sigs); ++i) ++ sigaction(sigs[i], &fatal, NULL); ++ } + sigfillset(&set); + sigdelset(&set, SIGCHLD); + sigdelset(&set, SIGUSR1); + sigdelset(&set, SIGUSR2); ++ sigdelset(&set, SIGTERM); ++ sigdelset(&set, SIGINT); ++ sigdelset(&set, SIGHUP); ++ sigdelset(&set, SIGQUIT); ++ sigdelset(&set, SIGSEGV); ++ sigdelset(&set, SIGBUS); ++ sigdelset(&set, SIGABRT); ++ sigdelset(&set, SIGFPE); ++ sigdelset(&set, SIGILL); + sigprocmask(SIG_SETMASK, &set, NULL); + + if (!vt) { +diff --git a/launch/meson.build b/launch/meson.build +index d280fcc..bd8fc33 100644 +--- a/launch/meson.build ++++ b/launch/meson.build +@@ -1,7 +1,7 @@ + launch_protocol = files('protocol.c') + executable('swc-launch', + ['launch.c', launch_protocol, 'devmajor-@0@.c'.format(os)], +- dependencies: video_backend == 'drm' ? drm : [], ++ dependencies: with_drm ? drm : [], + install: true, + install_mode: 'rwsr-xr-x', + ) diff --git a/patches/launchEscapeKeys.patch b/patches/launchEscapeKeys.patch @@ -1,238 +0,0 @@ -From 01933277f6295503bbfe91a902f5062f11485a64 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:58:36 -0300 -Subject: [PATCH 19/19] launch: -e adds escape keys - -Ctrl+Alt+F1..F12 switch vt, Ctrl+Alt+Backspace twice ends the session. ---- - launch/launch.c | 154 +++++++++++++++++++++++++++++++++++++++++++++--- - 1 file changed, 146 insertions(+), 8 deletions(-) - -diff --git a/launch/launch.c b/launch/launch.c -index f781433..d1a9031 100644 ---- a/launch/launch.c -+++ b/launch/launch.c -@@ -47,6 +47,7 @@ - #include <sys/stat.h> - #include <sys/types.h> - #include <sys/wait.h> -+#include <dirent.h> - #include <time.h> - #ifdef __linux__ - #include <sys/sysmacros.h> -@@ -82,6 +83,11 @@ mode_t _permit_setugid = S_ISUID; - #endif - - #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0]) -+#ifdef __linux__ -+#define ESCAPE_KEYBOARDS_MAX 32 -+#else -+#define ESCAPE_KEYBOARDS_MAX 0 -+#endif - - static void - activate(void); -@@ -98,6 +104,23 @@ static int tty_fd; - static bool active; - static pid_t child_pid = -1; - -+/* with -e, read the keyboards here so a wedged server cant block these: -+ * ctrl+alt+f1..f12 switch vt -+ * ctrl+alt+backspace x2 end the session */ -+#ifdef __linux__ -+#define ESCAPE_KEYBOARDS ESCAPE_KEYBOARDS_MAX -+#define ESCAPE_WINDOW 3000 -+#define ESCAPE_RESCAN 3000 -+static struct { -+ int fd; -+ dev_t rdev; -+} keyboards[ESCAPE_KEYBOARDS]; -+static int num_keyboards; -+static bool escape_enabled; -+static bool ctrl_l, ctrl_r, alt_l, alt_r; -+static long long last_zap; -+#endif -+ - static struct { - bool altered; - int vt; -@@ -111,7 +134,7 @@ cleanup(void); - static noreturn void - usage(const char *name) - { -- fprintf(stderr, "usage: %s [-n] [-t tty] [--] server [args...]\n", name); -+ fprintf(stderr, "usage: %s [-ne] [-t tty] [--] server [args...]\n", name); - exit(2); - } - -@@ -263,6 +286,10 @@ activate(void) - /* a hung server must not hold the vt */ - send(sock[0], &event, sizeof(event), MSG_DONTWAIT | MSG_NOSIGNAL); - active = true; -+#ifdef __linux__ -+ ctrl_l = ctrl_r = alt_l = alt_r = false; -+ last_zap = 0; -+#endif - } - - static void -@@ -632,19 +659,119 @@ error0: - exit(EXIT_FAILURE); - } - -+#ifdef __linux__ -+#define TEST_BIT(bits, n) ((bits)[(n) / (8 * sizeof(long))] >> ((n) % (8 * sizeof(long))) & 1) -+ -+/* open every keyboard not already open */ -+static void -+scan_keyboards(void) -+{ -+ DIR *dir; -+ struct dirent *entry; -+ char path[PATH_MAX]; -+ unsigned long keys[KEY_MAX / (8 * sizeof(long)) + 1]; -+ struct stat st; -+ int fd, i; -+ -+ if (!(dir = opendir("/dev/input"))) -+ return; -+ while ((entry = readdir(dir)) && num_keyboards < ESCAPE_KEYBOARDS) { -+ if (strncmp(entry->d_name, "event", 5) != 0) -+ continue; -+ snprintf(path, sizeof(path), "/dev/input/%s", entry->d_name); -+ if (stat(path, &st) < 0 || !device_is_input(st.st_rdev)) -+ continue; -+ for (i = 0; i < num_keyboards && keyboards[i].rdev != st.st_rdev; ++i) -+ ; -+ if (i < num_keyboards) -+ continue; -+ if ((fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)) < 0) -+ continue; -+ memset(keys, 0, sizeof(keys)); -+ if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0 || -+ !TEST_BIT(keys, KEY_BACKSPACE) || !TEST_BIT(keys, KEY_LEFTCTRL)) { -+ close(fd); -+ continue; -+ } -+ keyboards[num_keyboards].fd = fd; -+ keyboards[num_keyboards].rdev = st.st_rdev; -+ ++num_keyboards; -+ } -+ closedir(dir); -+} -+ -+static void -+drop_keyboard(int i) -+{ -+ close(keyboards[i].fd); -+ keyboards[i] = keyboards[--num_keyboards]; -+} -+ -+static void -+handle_keyboard(int i) -+{ -+ struct input_event ev[64]; -+ ssize_t n; -+ size_t k; -+ -+ while ((n = read(keyboards[i].fd, ev, sizeof(ev))) > 0) { -+ for (k = 0; k < (size_t)n / sizeof(ev[0]); ++k) { -+ bool down = ev[k].value != 0; -+ -+ if (ev[k].type != EV_KEY) -+ continue; -+ switch (ev[k].code) { -+ case KEY_LEFTCTRL: ctrl_l = down; continue; -+ case KEY_RIGHTCTRL: ctrl_r = down; continue; -+ case KEY_LEFTALT: alt_l = down; continue; -+ case KEY_RIGHTALT: alt_r = down; continue; -+ } -+ /* presses with ctrl+alt held, on our vt only */ -+ if (ev[k].value != 1 || !active || !(ctrl_l || ctrl_r) || !(alt_l || alt_r)) -+ continue; -+ if (ev[k].code == KEY_BACKSPACE) { -+ long long now = now_ms(); -+ -+ if (last_zap && now - last_zap <= ESCAPE_WINDOW) { -+ fprintf(stderr, "swc-launch: Ctrl+Alt+Backspace twice, ending the session\n"); -+ end_session(EXIT_FAILURE); -+ } -+ last_zap = now; -+ fprintf(stderr, "swc-launch: Ctrl+Alt+Backspace; again within 3s ends the session\n"); -+ } else if (ev[k].code >= KEY_F1 && ev[k].code <= KEY_F10) { -+ ioctl(tty_fd, VT_ACTIVATE, ev[k].code - KEY_F1 + 1); -+ } else if (ev[k].code == KEY_F11 || ev[k].code == KEY_F12) { -+ ioctl(tty_fd, VT_ACTIVATE, ev[k].code == KEY_F11 ? 11 : 12); -+ } -+ } -+ } -+ if (n < 0 && errno != EAGAIN && errno != EINTR) -+ drop_keyboard(i); /* unplugged */ -+} -+#endif -+ - static void - run(int fd) - { -- struct pollfd fds[] = { -- {.fd = fd, .events = POLLIN}, -- {.fd = sigfd[0], .events = POLLIN}, -- }; -- int status; -+ struct pollfd fds[2 + ESCAPE_KEYBOARDS_MAX]; -+ int status, nfds, i; -+ long long next_scan = 0; - pid_t pid; - char sig; - - for (;;) { -- if (poll(fds, ARRAY_LENGTH(fds), -1) < 0) { -+ nfds = 0; -+ fds[nfds++] = (struct pollfd){.fd = fd, .events = POLLIN}; -+ fds[nfds++] = (struct pollfd){.fd = sigfd[0], .events = POLLIN}; -+#ifdef __linux__ -+ if (escape_enabled && now_ms() >= next_scan) { -+ scan_keyboards(); -+ next_scan = now_ms() + ESCAPE_RESCAN; -+ } -+ for (i = 0; i < num_keyboards; ++i) -+ fds[nfds++] = (struct pollfd){.fd = keyboards[i].fd, .events = POLLIN}; -+#endif -+ if (poll(fds, nfds, escape_enabled ? ESCAPE_RESCAN : -1) < 0) { - if (errno == EINTR) { - continue; - } -@@ -657,6 +784,12 @@ run(int fd) - fds[0].fd = -1; - fd = -1; - } -+#ifdef __linux__ -+ for (i = nfds - 1; i >= 2; --i) { -+ if (fds[i].revents) -+ handle_keyboard(i - 2); -+ } -+#endif - if (fds[1].revents) { - if (read(sigfd[0], &sig, 1) <= 0) { - continue; -@@ -704,11 +837,16 @@ main(int argc, char *argv[]) - pid_t pid; - posix_spawnattr_t attr; - -- while ((option = getopt(argc, argv, "nt:")) != -1) { -+ while ((option = getopt(argc, argv, "net:")) != -1) { - switch (option) { - case 'n': - nflag = true; - break; -+ case 'e': -+#ifdef __linux__ -+ escape_enabled = true; -+#endif -+ break; - case 't': - vt = optarg; - break; --- -2.56.1 - diff --git a/patches/launchRestoresConsole.patch b/patches/launchRestoresConsole.patch @@ -1,232 +0,0 @@ -From bf35769bccb99a50a81a95a3eedbc56d5ee7c67b Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:56:06 -0300 -Subject: [PATCH 04/19] launch: restore the console on any exit - -Handle TERM, INT, HUP, QUIT and crashes, never block on the server, drop stale device fds. ---- - launch/launch.c | 135 +++++++++++++++++++++++++++++++++++++++++++++--- - 1 file changed, 129 insertions(+), 6 deletions(-) - -diff --git a/launch/launch.c b/launch/launch.c -index 47fcd38..f781433 100644 ---- a/launch/launch.c -+++ b/launch/launch.c -@@ -47,6 +47,7 @@ - #include <sys/stat.h> - #include <sys/types.h> - #include <sys/wait.h> -+#include <time.h> - #ifdef __linux__ - #include <sys/sysmacros.h> - #endif -@@ -195,13 +196,72 @@ cleanup(void) - kill(child_pid, SIGTERM); - } - -+/* async-signal-safe: console back to text, keyboard back on */ -+static void -+restore_vt_raw(void) -+{ -+ struct vt_mode mode = {.mode = VT_AUTO}; -+ -+ if (!original_vt_state.altered) -+ return; -+ ioctl(tty_fd, VT_SETMODE, &mode); -+#ifndef __OpenBSD__ -+ ioctl(tty_fd, KDSKBMODE, original_vt_state.kb_mode); -+#endif -+ ioctl(tty_fd, KDSETMODE, original_vt_state.console_mode); -+} -+ -+static void -+handle_fatal(int sig) -+{ -+ restore_vt_raw(); -+ if (child_pid > 0) -+ kill(child_pid, SIGKILL); -+ signal(sig, SIG_DFL); -+ raise(sig); -+} -+ -+static long long -+now_ms(void) -+{ -+ struct timespec ts; -+ -+ clock_gettime(CLOCK_MONOTONIC, &ts); -+ return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; -+} -+ -+/* restore the console, ask the session to end, kill it after 3s */ -+static noreturn void -+end_session(int code) -+{ -+ long long until; -+ int status; -+ pid_t pid; -+ -+ cleanup(); -+ until = now_ms() + 3000; -+ while (child_pid > 0) { -+ pid = waitpid(child_pid, &status, WNOHANG); -+ if (pid == child_pid || (pid < 0 && errno == ECHILD)) -+ break; -+ if (now_ms() >= until) { -+ kill(child_pid, SIGKILL); -+ waitpid(child_pid, &status, 0); -+ break; -+ } -+ nanosleep(&(struct timespec){0, 50000000}, NULL); -+ } -+ exit(code); -+} -+ - static void - activate(void) - { - struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_ACTIVATE}; - - start_devices(); -- send(sock[0], &event, sizeof(event), 0); -+ /* a hung server must not hold the vt */ -+ send(sock[0], &event, sizeof(event), MSG_DONTWAIT | MSG_NOSIGNAL); - active = true; - } - -@@ -210,7 +270,7 @@ deactivate(void) - { - struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_DEACTIVATE}; - -- send(sock[0], &event, sizeof(event), 0); -+ send(sock[0], &event, sizeof(event), MSG_DONTWAIT | MSG_NOSIGNAL); - stop_devices(true); - active = false; - } -@@ -273,6 +333,18 @@ handle_socket_data(int socket) - if (!active) { - goto fail; - } -+ /* the same device again means a restarted server: drop the stale fd */ -+ for (int i = 0; i < num_input_fds; ++i) { -+ struct stat old; -+ -+ if (fstat(input_fds[i], &old) == 0 && old.st_rdev == st.st_rdev) { -+#ifdef EVIOCREVOKE -+ ioctl(input_fds[i], EVIOCREVOKE, 0); -+#endif -+ close(input_fds[i]); -+ input_fds[i--] = input_fds[--num_input_fds]; -+ } -+ } - if (num_input_fds == ARRAY_LENGTH(input_fds)) { - fprintf(stderr, "too many input devices opened\n"); - goto fail; -@@ -291,11 +363,23 @@ handle_socket_data(int socket) - input_fds[num_input_fds++] = fd; - } else if (device_is_drm(st.st_rdev)) { - #ifdef ENABLE_DRM -+ /* the stale fd is still master and would keep the new server from drawing */ -+ for (int i = 0; i < num_drm_fds; ++i) { -+ struct stat old; -+ -+ if (fstat(drm_fds[i], &old) == 0 && old.st_rdev == st.st_rdev) { -+ drmDropMaster(drm_fds[i]); -+ close(drm_fds[i]); -+ drm_fds[i--] = drm_fds[--num_drm_fds]; -+ } -+ } - if (num_drm_fds == ARRAY_LENGTH(drm_fds)) { - fprintf(stderr, "too many DRM devices opened\n"); - goto fail; - } - drm_fds[num_drm_fds++] = fd; -+ if (active && drmSetMaster(fd) < 0) -+ fprintf(stderr, "drmSetMaster %s: %s\n", path, strerror(errno)); - #else - fprintf(stderr, "DRM device requested by non-DRM backend\n"); - goto fail; -@@ -556,6 +640,7 @@ run(int fd) - {.fd = sigfd[0], .events = POLLIN}, - }; - int status; -+ pid_t pid; - char sig; - - for (;;) { -@@ -565,8 +650,12 @@ run(int fd) - } - die("poll:"); - } -- if (fds[0].revents) { -+ if (fds[0].revents & POLLIN) { - handle_socket_data(fd); -+ } else if (fds[0].revents & (POLLHUP | POLLERR)) { -+ /* server gone: stop polling its socket, sigchld ends the session */ -+ fds[0].fd = -1; -+ fd = -1; - } - if (fds[1].revents) { - if (read(sigfd[0], &sig, 1) <= 0) { -@@ -574,9 +663,20 @@ run(int fd) - } - switch (sig) { - case SIGCHLD: -- wait(&status); -- cleanup(); -- exit(WEXITSTATUS(status)); -+ while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { -+ if (pid == child_pid) { -+ child_pid = -1; -+ cleanup(); -+ exit(WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status)); -+ } -+ } -+ break; -+ case SIGTERM: -+ case SIGINT: -+ case SIGHUP: -+ case SIGQUIT: -+ fprintf(stderr, "swc-launch: signal %d, ending the session\n", sig); -+ end_session(128 + sig); - case SIGUSR1: - deactivate(); - ioctl(tty_fd, VT_RELDISP, 1); -@@ -647,11 +747,34 @@ main(int argc, char *argv[]) - if (sigaction(SIGUSR2, &action, NULL) == -1) { - die("sigaction SIGUSR2:"); - } -+ /* end the session, console first */ -+ if (sigaction(SIGTERM, &action, NULL) == -1 || -+ sigaction(SIGINT, &action, NULL) == -1 || -+ sigaction(SIGHUP, &action, NULL) == -1 || -+ sigaction(SIGQUIT, &action, NULL) == -1) { -+ die("sigaction:"); -+ } -+ { -+ /* restore the console on our own crash */ -+ struct sigaction fatal = {.sa_handler = handle_fatal, .sa_flags = SA_RESETHAND}; -+ int sigs[] = {SIGSEGV, SIGBUS, SIGABRT, SIGFPE, SIGILL}; - -+ for (size_t i = 0; i < ARRAY_LENGTH(sigs); ++i) -+ sigaction(sigs[i], &fatal, NULL); -+ } - sigfillset(&set); - sigdelset(&set, SIGCHLD); - sigdelset(&set, SIGUSR1); - sigdelset(&set, SIGUSR2); -+ sigdelset(&set, SIGTERM); -+ sigdelset(&set, SIGINT); -+ sigdelset(&set, SIGHUP); -+ sigdelset(&set, SIGQUIT); -+ sigdelset(&set, SIGSEGV); -+ sigdelset(&set, SIGBUS); -+ sigdelset(&set, SIGABRT); -+ sigdelset(&set, SIGFPE); -+ sigdelset(&set, SIGILL); - sigprocmask(SIG_SETMASK, &set, NULL); - - if (!vt) { --- -2.56.1 - diff --git a/patches/layerPopupsOnScreen.patch b/patches/layerPopupsOnScreen.patch @@ -1,241 +0,0 @@ -From 6dc200a840c0842a791c4735c7244690af400ee6 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 17 Nov 2026 08:11:26 -0300 -Subject: [PATCH 08/19] layer_shell: implement get_popup, keep popups on screen - ---- - libswc/layer_shell.c | 8 ++- - libswc/xdg_shell.c | 143 +++++++++++++++++++++++++++++++++++++++---- - libswc/xdg_shell.h | 6 ++ - 3 files changed, 142 insertions(+), 15 deletions(-) - -diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c -index 348a13c..5cd130a 100644 ---- a/libswc/layer_shell.c -+++ b/libswc/layer_shell.c -@@ -9,6 +9,7 @@ - #include "surface.h" - #include "util.h" - #include "view.h" -+#include "xdg_shell.h" - - #include "wlr-layer-shell-unstable-v1-server-protocol.h" - #include <assert.h> -@@ -320,9 +321,12 @@ static void - get_popup(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *popup) - { -+ struct layer_surface *surface = wl_resource_get_user_data(resource); -+ - (void)client; -- (void)resource; -- (void)popup; -+ if (surface->view) { -+ xdg_popup_set_parent(popup, surface->view->surface); -+ } - } - - static void -diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c -index ea3b533..8f54b95 100644 ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -24,6 +24,7 @@ - #include "xdg_shell.h" - #include "compositor.h" - #include "internal.h" -+#include "screen.h" - #include "seat.h" - #include "surface.h" - #include "util.h" -@@ -560,6 +561,128 @@ static const struct xdg_popup_interface popup_impl = { - .grab = grab, - }; - -+/* mirror an anchor or gravity, the enums share values */ -+static uint32_t -+mirror_y(uint32_t v) -+{ -+ static const uint32_t to[] = {0, 2, 1, 3, 4, 6, 5, 8, 7}; -+ -+ return v < 9 ? to[v] : v; -+} -+ -+static uint32_t -+mirror_x(uint32_t v) -+{ -+ static const uint32_t to[] = {0, 1, 2, 4, 3, 7, 8, 5, 6}; -+ -+ return v < 9 ? to[v] : v; -+} -+ -+/* screen at x, y, else the first */ -+static struct swc_rectangle * -+screen_at(int32_t x, int32_t y) -+{ -+ struct screen *screen, *first = NULL; -+ -+ wl_list_for_each (screen, &swc.screens, link) { -+ struct swc_rectangle *g = &screen->base.geometry; -+ -+ if (!first) { -+ first = screen; -+ } -+ if (x >= g->x && y >= g->y && x < g->x + (int32_t)g->width && y < g->y + (int32_t)g->height) { -+ return g; -+ } -+ } -+ return first ? &first->base.geometry : NULL; -+} -+ -+/* popup position kept on screen: flip, then slide, as the positioner allows */ -+static struct swc_rectangle -+constrained_position(struct xdg_positioner *positioner, int32_t px, int32_t py) -+{ -+ struct swc_rectangle r = calculate_position(positioner), f; -+ struct swc_rectangle *g = screen_at(px + r.x, py + r.y); -+ struct xdg_positioner flipped; -+ -+ if (!g) { -+ return r; -+ } -+ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y && -+ py + r.y + r.height > g->y + (int32_t)g->height) { -+ flipped = *positioner; -+ flipped.anchor = mirror_y(flipped.anchor); -+ flipped.gravity = mirror_y(flipped.gravity); -+ flipped.offset_y = -flipped.offset_y; -+ f = calculate_position(&flipped); -+ if (py + f.y >= g->y) { -+ r.y = f.y; -+ } -+ } -+ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X && -+ px + r.x + r.width > g->x + (int32_t)g->width) { -+ flipped = *positioner; -+ flipped.anchor = mirror_x(flipped.anchor); -+ flipped.gravity = mirror_x(flipped.gravity); -+ flipped.offset_x = -flipped.offset_x; -+ f = calculate_position(&flipped); -+ if (px + f.x >= g->x) { -+ r.x = f.x; -+ } -+ } -+ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y) { -+ if (py + r.y + r.height > g->y + (int32_t)g->height) { -+ r.y = g->y + (int32_t)g->height - r.height - py; -+ } -+ if (py + r.y < g->y) { -+ r.y = g->y - py; -+ } -+ } -+ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X) { -+ if (px + r.x + r.width > g->x + (int32_t)g->width) { -+ r.x = g->x + (int32_t)g->width - r.width - px; -+ } -+ if (px + r.x < g->x) { -+ r.x = g->x - px; -+ } -+ } -+ return r; -+} -+ -+/* place popup against its parent and tell the client */ -+static bool -+xdg_popup_place(struct xdg_popup *popup, struct compositor_view *parent_view) -+{ -+ uint32_t serial = wl_display_next_serial(swc.display); -+ struct swc_rectangle rect; -+ -+ if (!parent_view) { -+ return false; -+ } -+ rect = constrained_position(&popup->positioner, parent_view->base.geometry.x, -+ parent_view->base.geometry.y); -+ compositor_view_set_parent(popup->view, parent_view); -+ view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, -+ parent_view->base.geometry.y + rect.y); -+ xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, -+ rect.height); -+ xdg_surface_send_configure(popup->xdg_surface->resource, serial); -+ return true; -+} -+ -+void -+xdg_popup_set_parent(struct wl_resource *resource, struct surface *parent) -+{ -+ struct xdg_popup *popup; -+ -+ if (!resource || !wl_resource_instance_of(resource, &xdg_popup_interface, &popup_impl)) { -+ return; -+ } -+ popup = wl_resource_get_user_data(resource); -+ xdg_popup_place(popup, compositor_view(parent->view)); -+} -+ -+/* a popup with no parent waits for one, as layer surface popups do */ - static struct xdg_popup * - xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, - struct xdg_surface *xdg_surface, struct xdg_surface *parent, -@@ -567,11 +690,9 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, - { - struct xdg_popup *popup; - struct compositor_view *parent_view = -- compositor_view(parent->surface->view); -- uint32_t serial = wl_display_next_serial(swc.display); -- struct swc_rectangle rect; -+ parent ? compositor_view(parent->surface->view) : NULL; - -- if (!parent_view) { -+ if (parent && !parent_view) { - goto error0; - } - popup = malloc(sizeof(*popup)); -@@ -594,14 +715,9 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, - } - wl_resource_set_implementation(popup->resource, &popup_impl, popup, - &destroy_popup); -- -- rect = calculate_position(positioner); -- compositor_view_set_parent(popup->view, parent_view); -- view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, -- parent_view->base.geometry.y + rect.y); -- xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, -- rect.height); -- xdg_surface_send_configure(xdg_surface->resource, serial); -+ if (parent_view) { -+ xdg_popup_place(popup, parent_view); -+ } - - return popup; - -@@ -649,7 +765,8 @@ get_popup(struct wl_client *client, struct wl_resource *resource, uint32_t id, - struct wl_resource *positioner_resource) - { - struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); -- struct xdg_surface *parent = wl_resource_get_user_data(parent_resource); -+ struct xdg_surface *parent = -+ parent_resource ? wl_resource_get_user_data(parent_resource) : NULL; - struct xdg_positioner *positioner = - wl_resource_get_user_data(positioner_resource); - struct xdg_popup *popup; -diff --git a/libswc/xdg_shell.h b/libswc/xdg_shell.h -index fd51a2d..72e3845 100644 ---- a/libswc/xdg_shell.h -+++ b/libswc/xdg_shell.h -@@ -31,6 +31,12 @@ struct window; - struct wl_global * - xdg_shell_create(struct wl_display *display); - -+struct surface; -+ -+/* give a parentless popup its parent and place it */ -+void -+xdg_popup_set_parent(struct wl_resource *popup, struct surface *parent); -+ - /* window of an xdg_toplevel */ - struct window * - xdg_toplevel_window(struct wl_resource *resource); --- -2.56.1 - diff --git a/patches/layerZoneZeroUsableArea.patch b/patches/layerZoneZeroUsableArea.patch @@ -1,127 +0,0 @@ -From 10f11d3c8295ac86a9e66cbfc677665374117468 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 17 Nov 2026 01:10:24 -0300 -Subject: [PATCH 07/19] layer_shell: zone 0 surfaces keep clear of reserved - zones - ---- - libswc/layer_shell.c | 40 +++++++++++++++++++++++++++++++++++++--- - libswc/layer_shell.h | 6 ++++++ - libswc/screen.c | 2 ++ - 3 files changed, 45 insertions(+), 3 deletions(-) - -diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c -index 194ed01..348a13c 100644 ---- a/libswc/layer_shell.c -+++ b/libswc/layer_shell.c -@@ -179,10 +179,22 @@ restack_layer(struct layer_surface *surface) - compositor_view_set_stack_layer(surface->view, stack_layer, true); - } - -+/* zone 0 surfaces keep clear of reserved zones, others span the screen */ -+static const struct swc_rectangle * -+area_of(const struct layer_surface *surface) -+{ -+ const struct swc_rectangle *usable = &surface->screen->base.usable_geometry; -+ -+ if (surface->current.exclusive_zone == 0 && usable->width && usable->height) { -+ return usable; -+ } -+ return &surface->screen->base.geometry; -+} -+ - static void - update_position(struct layer_surface *surface) - { -- struct swc_rectangle *screen = &surface->screen->base.geometry; -+ const struct swc_rectangle *screen = area_of(surface); - struct swc_rectangle *view = &surface->view->base.geometry; - int32_t x, y; - -@@ -229,14 +241,14 @@ send_configure(struct layer_surface *surface) - width = configure_size(surface->current.desired_width, surface->current.anchor, - ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT, - ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT, -- surface->screen->base.geometry.width, -+ area_of(surface)->width, - surface->current.margin.left, - surface->current.margin.right); - height = - configure_size(surface->current.desired_height, surface->current.anchor, - ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, - ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, -- surface->screen->base.geometry.height, -+ area_of(surface)->height, - surface->current.margin.top, - surface->current.margin.bottom); - surface->configure_serial = wl_display_next_serial(swc.display); -@@ -432,6 +444,28 @@ handle_surface_commit(struct wl_listener *listener, void *data) - } - } - -+static void modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, -+ pixman_region32_t *usable); -+ -+void -+layer_shell_usable_changed(struct screen *screen) -+{ -+ struct screen_modifier *modifier; -+ struct layer_surface *surface; -+ -+ wl_list_for_each(modifier, &screen->modifiers, link) -+ { -+ if (modifier->modify != modify) { -+ continue; -+ } -+ surface = wl_container_of(modifier, surface, modifier); -+ if (surface->configured && surface->current.exclusive_zone == 0) { -+ send_configure(surface); -+ update_position(surface); -+ } -+ } -+} -+ - static void - modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, - pixman_region32_t *usable) -diff --git a/libswc/layer_shell.h b/libswc/layer_shell.h -index 76e5432..5d8bab4 100644 ---- a/libswc/layer_shell.h -+++ b/libswc/layer_shell.h -@@ -4,7 +4,13 @@ - struct wl_display; - struct wl_global; - -+struct screen; -+ - struct wl_global * - layer_shell_create(struct wl_display *display); - -+/* usable area changed, lay out the surfaces that depend on it */ -+void -+layer_shell_usable_changed(struct screen *screen); -+ - #endif -diff --git a/libswc/screen.c b/libswc/screen.c -index c0f1eb0..ba43dd8 100644 ---- a/libswc/screen.c -+++ b/libswc/screen.c -@@ -29,6 +29,7 @@ - #endif - #include "event.h" - #include "internal.h" -+#include "layer_shell.h" - #include "mode.h" - #include "output.h" - #ifdef ENABLE_DRM -@@ -239,6 +240,7 @@ screen_update_usable_geometry(struct screen *screen) - screen->base.usable_geometry.width = extents->x2 - extents->x1; - screen->base.usable_geometry.height = extents->y2 - extents->y1; - -+ layer_shell_usable_changed(screen); - if (screen->handler->usable_geometry_changed) { - screen->handler->usable_geometry_changed(screen->handler_data); - } --- -2.56.1 - diff --git a/patches/managerTransients.patch b/patches/managerTransients.patch @@ -1,208 +0,0 @@ -From eb4b54b10a3c3dd75b3e86db074bc9ac96d098d8 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:56:47 -0300 -Subject: [PATCH 13/19] window: parent, attach and raise for managers - ---- - libswc/compositor.c | 38 +++++++++++++++++++++++++++++++++++++- - libswc/compositor.h | 2 ++ - libswc/swc.h | 23 +++++++++++++++++++++++ - libswc/window.c | 41 ++++++++++++++++++++++++++++++++++++++--- - 4 files changed, 100 insertions(+), 4 deletions(-) - -diff --git a/libswc/compositor.c b/libswc/compositor.c -index 08fe096..ef1a76e 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -1097,7 +1097,19 @@ attach(struct view *base, struct wld_buffer *buffer) - static bool - move(struct view *base, int32_t x, int32_t y) - { -- struct compositor_view *view = (void *)base; -+ struct compositor_view *view = (void *)base, *other; -+ int32_t dx = x - base->geometry.x, dy = y - base->geometry.y; -+ -+ /* attached transients follow the parent */ -+ if (view->window && (dx || dy)) { -+ wl_list_for_each(other, &compositor.views, link) -+ { -+ if (other->parent == view && other->attached && other->window) { -+ view_move(&other->base, other->base.geometry.x + dx, -+ other->base.geometry.y + dy); -+ } -+ } -+ } - - if (view->visible) { - damage_below_view(view); -@@ -1224,6 +1236,28 @@ window_view(struct compositor_view *view) - return (view && view->window) ? view : NULL; - } - -+/* transients go above their parent, recursively */ -+static void -+raise_transients(struct compositor_view *view) -+{ -+ struct compositor_view *other, *children[32]; -+ unsigned n = 0; -+ -+ wl_list_for_each(other, &compositor.views, link) -+ { -+ if (other->parent == view && other->window && other->visible && -+ n < sizeof children / sizeof children[0]) { -+ children[n++] = other; -+ } -+ } -+ for (unsigned i = 0; i < n; i++) { -+ wl_list_remove(&children[i]->link); -+ wl_list_insert(view->link.prev, &children[i]->link); -+ damage_view(children[i]); -+ raise_transients(children[i]); -+ } -+} -+ - void - raise_window(struct compositor_view *view) - { -@@ -1266,6 +1300,7 @@ raise_window(struct compositor_view *view) - } - - if (view == top_window) { -+ raise_transients(view); - return; - } - -@@ -1280,6 +1315,7 @@ raise_window(struct compositor_view *view) - span_u32(view->extents.x1, view->extents.x2), - span_u32(view->extents.y1, view->extents.y2)); - schedule_updates(screens); -+ raise_transients(view); - } - - void -diff --git a/libswc/compositor.h b/libswc/compositor.h -index 8040a00..073f1b1 100644 ---- a/libswc/compositor.h -+++ b/libswc/compositor.h -@@ -71,6 +71,8 @@ struct compositor_view { - bool buffer_opaque; - struct window *window; - struct compositor_view *parent; -+ /* moves with its parent */ -+ bool attached; - int32_t buffer_offset_x; - int32_t buffer_offset_y; - -diff --git a/libswc/swc.h b/libswc/swc.h -index 2d4e58d..88155ac 100644 ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -272,6 +272,11 @@ struct swc_window_handler { - * Called when the window starts or stops blocking its parent. - */ - void (*modal_changed)(void *data); -+ -+ /** -+ * Called when the window size changes. -+ */ -+ void (*size_changed)(void *data); - }; - - struct swc_window { -@@ -351,6 +356,24 @@ swc_window_set_tiled(struct swc_window *window); - void - swc_window_set_fullscreen(struct swc_window *window, struct swc_screen *screen); - -+/** -+ * Make window a transient of parent, or of nothing when NULL. -+ */ -+void -+swc_window_set_parent(struct swc_window *window, struct swc_window *parent); -+ -+/** -+ * An attached transient moves with its parent. -+ */ -+void -+swc_window_set_attached(struct swc_window *window, bool attached); -+ -+/** -+ * Raise the window with its transients. -+ */ -+void -+swc_window_raise(struct swc_window *window); -+ - /** - * Set the window's position. - * -diff --git a/libswc/window.c b/libswc/window.c -index a24ec1a..34bed2c 100644 ---- a/libswc/window.c -+++ b/libswc/window.c -@@ -234,6 +234,32 @@ swc_window_focus(struct swc_window *base) - keyboard_set_focus(swc.seat->keyboard, new); - } - -+EXPORT void -+swc_window_set_parent(struct swc_window *base, struct swc_window *parent) -+{ -+ window_set_parent(INTERNAL(base), parent ? INTERNAL(parent) : NULL); -+} -+ -+EXPORT void -+swc_window_set_attached(struct swc_window *base, bool attached) -+{ -+ struct window *window = INTERNAL(base); -+ -+ if (window->view) { -+ window->view->attached = attached; -+ } -+} -+ -+EXPORT void -+swc_window_raise(struct swc_window *base) -+{ -+ struct window *window = INTERNAL(base); -+ -+ if (window->view) { -+ raise_window(window->view); -+ } -+} -+ - EXPORT void - swc_window_set_stacked(struct swc_window *base) - { -@@ -497,6 +523,10 @@ handle_resize(struct view_handler *handler, uint32_t old_width, - - view_move(&window->view->base, x, y); - } -+ -+ if (window->handler->size_changed) { -+ window->handler->size_changed(window->handler_data); -+ } - } - - static const struct view_handler_impl view_handler_impl = { -@@ -631,12 +661,17 @@ window_set_modal(struct window *window, bool modal) - void - window_set_parent(struct window *window, struct window *parent) - { -- if (window->base.parent == &parent->base) { -+ struct swc_window *base = parent ? &parent->base : NULL; -+ -+ if (window->base.parent == base || window == parent) { - return; - } - -- compositor_view_set_parent(window->view, parent->view); -- window->base.parent = &parent->base; -+ compositor_view_set_parent(window->view, parent ? parent->view : NULL); -+ window->base.parent = base; -+ if (parent) { -+ raise_window(parent->view); -+ } - - if (window->handler->parent_changed) { - window->handler->parent_changed(window->handler_data); --- -2.56.1 - diff --git a/patches/nestedBackend.patch b/patches/nestedBackend.patch @@ -1,2052 +0,0 @@ -From 7245afd86719616ce2e23d993c60bc8373455f83 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Fri, 13 Nov 2026 19:29:22 -0300 -Subject: [PATCH 14/19] add a nested backend - -Runs swc inside another compositor, for testing without a vt. ---- - libswc/backend.h | 18 +- - libswc/compositor.c | 55 +- - libswc/compositor.h | 4 + - libswc/keyboard.c | 126 +++-- - libswc/keyboard.h | 7 + - libswc/launch.c | 5 + - libswc/layer_shell.c | 11 + - libswc/meson.build | 4 + - libswc/nested.c | 1135 ++++++++++++++++++++++++++++++++++++++++++ - libswc/nested.h | 16 + - libswc/output.c | 19 + - libswc/output.h | 2 + - libswc/pointer.c | 12 + - libswc/screen.c | 103 +++- - libswc/screen.h | 6 + - libswc/seat.c | 22 +- - libswc/swc.c | 77 ++- - meson.build | 15 + - meson.options | 1 + - protocol/meson.build | 3 + - 20 files changed, 1555 insertions(+), 86 deletions(-) - create mode 100644 libswc/nested.c - create mode 100644 libswc/nested.h - -diff --git a/libswc/backend.h b/libswc/backend.h -index 65432bc..8d0e7bf 100644 ---- a/libswc/backend.h -+++ b/libswc/backend.h -@@ -1,17 +1,33 @@ - #ifndef SWC_BACKEND_H - #define SWC_BACKEND_H - -+#include <stdbool.h> - #include <stdint.h> - -+struct screen; -+struct wl_list; -+struct wld_buffer; - struct wld_context; - struct wld_renderer; -+struct wld_surface; - --/* rendering state shared by the drm and fb backends */ -+/* rendering state shared by every backend */ - struct swc_backend { - struct wld_context *context; - struct wld_renderer *renderer; - uint32_t cursor_width; - uint32_t cursor_height; -+ -+ /* backend feeds the seat, no input devices */ -+ bool virtual_input; -+ -+ /* optional, unset means the built-in video backend */ -+ bool (*create_screens)(struct wl_list *screens); -+ struct wld_surface *(*create_target)(struct screen *screen); -+ void (*set_cursor)(struct wld_buffer *buffer, int32_t hotspot_x, -+ int32_t hotspot_y); -+ void (*start)(void); -+ void (*finalize)(void); - }; - - #endif -diff --git a/libswc/compositor.c b/libswc/compositor.c -index ef1a76e..b7b9eed 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -208,25 +208,35 @@ target_swap_buffers(struct target *target) - return view_attach(target->view, target->next_buffer); - } - -+static struct wld_surface * -+target_create_surface(struct screen *screen) -+{ -+ struct swc_rectangle *geom = &screen->base.geometry; -+ -+ if (swc.backend->create_target) { -+ return swc.backend->create_target(screen); -+ } -+ -+ return wld_create_surface(swc.backend->context, geom->width, geom->height, -+ WLD_FORMAT_XRGB8888, -+#ifdef ENABLE_DRM -+ WLD_DRM_FLAG_SCANOUT -+#else -+ WLD_FLAG_MAP -+#endif -+ ); -+} -+ - static struct target * - target_new(struct screen *screen) - { - struct target *target; -- struct swc_rectangle *geom = &screen->base.geometry; - - if (!(target = malloc(sizeof(*target)))) { - goto error0; - } - -- target->surface = -- wld_create_surface(swc.backend->context, geom->width, geom->height, -- WLD_FORMAT_XRGB8888, --#ifdef ENABLE_DRM -- WLD_DRM_FLAG_SCANOUT --#else -- WLD_FLAG_MAP --#endif -- ); -+ target->surface = target_create_surface(screen); - - if (!target->surface) { - goto error1; -@@ -2081,6 +2091,31 @@ compositor_get_buffer(struct screen *screen) - return target->current_buffer; - } - -+void -+compositor_screen_resize(struct screen *screen) -+{ -+ struct target *target = target_get(screen); -+ struct wld_surface *surface; -+ -+ if (!target) { -+ return; -+ } -+ -+ if (!(surface = target_create_surface(screen))) { -+ ERROR("Could not resize the target of screen %u\n", screen->id); -+ return; -+ } -+ -+ /* a flip in flight ends on the old surface, forget its buffers */ -+ wld_destroy_surface(target->surface); -+ target->surface = surface; -+ target->current_buffer = NULL; -+ target->next_buffer = NULL; -+ compositor.pending_flips &= ~target->mask; -+ -+ compositor_damage_all(); -+} -+ - struct wld_buffer * - compositor_render_to_shm(struct screen *screen) - { -diff --git a/libswc/compositor.h b/libswc/compositor.h -index 073f1b1..ddb4275 100644 ---- a/libswc/compositor.h -+++ b/libswc/compositor.h -@@ -171,6 +171,10 @@ compositor_view_damage_decor(struct compositor_view *view); - struct wld_buffer * - compositor_get_buffer(struct screen *screen); - -+/* recreate the render target after the screen geometry changed */ -+void -+compositor_screen_resize(struct screen *screen); -+ - /** - * render the compositor scene into a shm buffer - * caller must free with wld_buffer_unreference() -diff --git a/libswc/keyboard.c b/libswc/keyboard.c -index 4d0083a..27837e8 100644 ---- a/libswc/keyboard.c -+++ b/libswc/keyboard.c -@@ -395,6 +395,56 @@ keyboard_bind(struct keyboard *keyboard, - return client_resource; - } - -+/* update modifiers from xkb state, run handlers on change */ -+static void -+update_modifiers(struct keyboard *keyboard) -+{ -+ struct xkb *xkb = &keyboard->xkb; -+ struct keyboard_handler *handler; -+ struct keyboard_modifier_state modifier_state; -+ -+ modifier_state.depressed = -+ xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED); -+ modifier_state.latched = -+ xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED); -+ modifier_state.locked = -+ xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED); -+ modifier_state.group = -+ xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE); -+ -+ if (modifier_state.depressed != keyboard->modifier_state.depressed || -+ modifier_state.latched != keyboard->modifier_state.latched || -+ modifier_state.locked != keyboard->modifier_state.locked || -+ modifier_state.group != keyboard->modifier_state.group) { -+ uint32_t mods_active = -+ modifier_state.depressed | modifier_state.latched; -+ -+ /* Update keyboard modifier state. */ -+ keyboard->modifier_state = modifier_state; -+ keyboard->modifiers = 0; -+ if (mods_active & (1 << keyboard->xkb.indices.ctrl)) { -+ keyboard->modifiers |= SWC_MOD_CTRL; -+ } -+ if (mods_active & (1 << keyboard->xkb.indices.alt)) { -+ keyboard->modifiers |= SWC_MOD_ALT; -+ } -+ if (mods_active & (1 << keyboard->xkb.indices.super)) { -+ keyboard->modifiers |= SWC_MOD_LOGO; -+ } -+ if (mods_active & (1 << keyboard->xkb.indices.shift)) { -+ keyboard->modifiers |= SWC_MOD_SHIFT; -+ } -+ -+ /* Run any modifier handlers. */ -+ wl_list_for_each(handler, &keyboard->handlers, link) -+ { -+ if (handler->modifiers) { -+ handler->modifiers(keyboard, &modifier_state); -+ } -+ } -+ } -+} -+ - void - keyboard_handle_key(struct keyboard *keyboard, - uint32_t time, -@@ -402,7 +452,6 @@ keyboard_handle_key(struct keyboard *keyboard, - uint32_t state) - { - struct key *key; -- struct keyboard_modifier_state modifier_state; - enum xkb_key_direction direction; - struct xkb *xkb = &keyboard->xkb; - struct keyboard_handler *handler; -@@ -457,44 +506,47 @@ update_xkb_state: - state == WL_KEYBOARD_KEY_STATE_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP; - xkb_state_update_key(xkb->state, XKB_KEY(value), direction); - -- modifier_state.depressed = -- xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED); -- modifier_state.latched = -- xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED); -- modifier_state.locked = -- xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED); -- modifier_state.group = -- xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE); -+ update_modifiers(keyboard); -+} - -- if (modifier_state.depressed != keyboard->modifier_state.depressed || -- modifier_state.latched != keyboard->modifier_state.latched || -- modifier_state.locked != keyboard->modifier_state.locked || -- modifier_state.group != keyboard->modifier_state.group) { -- uint32_t mods_active = -- modifier_state.depressed | modifier_state.latched; -+void -+keyboard_handle_modifiers(struct keyboard *keyboard, uint32_t depressed, -+ uint32_t latched, uint32_t locked, uint32_t group) -+{ -+ xkb_state_update_mask(keyboard->xkb.state, depressed, latched, locked, 0, -+ 0, group); -+ update_modifiers(keyboard); -+} - -- /* Update keyboard modifier state. */ -- keyboard->modifier_state = modifier_state; -- keyboard->modifiers = 0; -- if (mods_active & (1 << keyboard->xkb.indices.ctrl)) { -- keyboard->modifiers |= SWC_MOD_CTRL; -- } -- if (mods_active & (1 << keyboard->xkb.indices.alt)) { -- keyboard->modifiers |= SWC_MOD_ALT; -- } -- if (mods_active & (1 << keyboard->xkb.indices.super)) { -- keyboard->modifiers |= SWC_MOD_LOGO; -- } -- if (mods_active & (1 << keyboard->xkb.indices.shift)) { -- keyboard->modifiers |= SWC_MOD_SHIFT; -- } -+bool -+keyboard_set_keymap(struct keyboard *keyboard, struct xkb_keymap *map) -+{ -+ struct xkb old = keyboard->xkb; -+ struct wl_resource *resource; - -- /* Run any modifier handlers. */ -- wl_list_for_each(handler, &keyboard->handlers, link) -- { -- if (handler->modifiers) { -- handler->modifiers(keyboard, &modifier_state); -- } -- } -+ keyboard->xkb.keymap.map = xkb_keymap_ref(map); -+ if (!update_keymap(&keyboard->xkb)) { -+ xkb_keymap_unref(map); -+ keyboard->xkb = old; -+ return false; -+ } -+ -+ /* held keys belong to the old keymap */ -+ if (!keyboard_reset(keyboard)) { -+ return false; - } -+ munmap(old.keymap.area, old.keymap.size); -+ close(old.keymap.fd); -+ xkb_keymap_unref(old.keymap.map); -+ -+ wl_resource_for_each(resource, &keyboard->focus.active) -+ wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, -+ keyboard->xkb.keymap.fd, -+ keyboard->xkb.keymap.size); -+ wl_resource_for_each(resource, &keyboard->focus.inactive) -+ wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, -+ keyboard->xkb.keymap.fd, -+ keyboard->xkb.keymap.size); -+ -+ return true; - } -diff --git a/libswc/keyboard.h b/libswc/keyboard.h -index 28c5882..0051e76 100644 ---- a/libswc/keyboard.h -+++ b/libswc/keyboard.h -@@ -100,5 +100,12 @@ keyboard_bind(struct keyboard *keyboard, struct wl_client *client, - void - keyboard_handle_key(struct keyboard *keyboard, uint32_t time, uint32_t key, - uint32_t state); -+/* for backends that get modifier state from outside */ -+void -+keyboard_handle_modifiers(struct keyboard *keyboard, uint32_t depressed, -+ uint32_t latched, uint32_t locked, uint32_t group); -+/* swap the keymap and resend it */ -+bool -+keyboard_set_keymap(struct keyboard *keyboard, struct xkb_keymap *map); - - #endif -diff --git a/libswc/launch.c b/libswc/launch.c -index d9602f4..4144a08 100644 ---- a/libswc/launch.c -+++ b/libswc/launch.c -@@ -116,6 +116,11 @@ send_request(struct swc_launch_request *request, const void *data, size_t size, - {.iov_base = event, .iov_len = sizeof(*event)}, - }; - -+ /* no launcher socket: the zeroed fd would be stdin */ -+ if (!launch.source) { -+ return false; -+ } -+ - request->serial = ++launch.next_serial; - - if (send_fd(launch.socket, out_fd, request_iov, 1 + (size > 0)) == -1) { -diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c -index 3245fc2..c96851d 100644 ---- a/libswc/layer_shell.c -+++ b/libswc/layer_shell.c -@@ -36,6 +36,8 @@ struct layer_surface { - uint32_t desired_width, desired_height; - } current, pending; - uint32_t configure_serial; -+ /* screen size the last configure was computed from */ -+ uint32_t screen_width, screen_height; - bool configured; - bool mapped; - }; -@@ -252,6 +254,8 @@ send_configure(struct layer_surface *surface) - area_of(surface)->height, - surface->current.margin.top, - surface->current.margin.bottom); -+ surface->screen_width = surface->screen->base.geometry.width; -+ surface->screen_height = surface->screen->base.geometry.height; - surface->configure_serial = wl_display_next_serial(swc.display); - zwlr_layer_surface_v1_send_configure(surface->resource, - surface->configure_serial, width, -@@ -483,6 +487,13 @@ modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, - .y2 = geom->y + geom->height}; - int32_t size; - -+ /* every usable-area pass visits us, so notice a size change here */ -+ if (surface->configured && (geom->width != surface->screen_width || -+ geom->height != surface->screen_height)) { -+ send_configure(surface); -+ update_position(surface); -+ } -+ - if (!surface->mapped) { - pixman_region32_reset(usable, &box); - return; -diff --git a/libswc/meson.build b/libswc/meson.build -index 04cef6e..022b3e2 100644 ---- a/libswc/meson.build -+++ b/libswc/meson.build -@@ -50,6 +50,10 @@ libswc_src = files( - ) - libswc_src += [cursor, launch_protocol] - -+if have_nested -+ libswc_src += files('nested.c') -+endif -+ - foreach proto, proto_src : protos - libswc_src += proto_src - endforeach -diff --git a/libswc/nested.c b/libswc/nested.c -new file mode 100644 -index 0000000..e44863a ---- /dev/null -+++ b/libswc/nested.c -@@ -0,0 +1,1135 @@ -+/* nested backend: swc as a client of another compositor. -+ * one xdg_toplevel is the screen, the parent seat feeds ours */ -+ -+#include "nested.h" -+#include "backend.h" -+#include "compositor.h" -+#include "internal.h" -+#include "keyboard.h" -+#include "launch/protocol.h" -+#include "output.h" -+#include "pointer.h" -+#include "screen.h" -+#include "seat.h" -+#include "swc.h" -+#include "util.h" -+#include "view.h" -+ -+#include <errno.h> -+#include <limits.h> -+#include <stdio.h> -+#include <stdlib.h> -+#include <string.h> -+#include <strings.h> -+#include <sys/mman.h> -+#include <unistd.h> -+#include <wayland-client.h> -+#include <wld/wayland.h> -+#include <wld/wld.h> -+#include <xkbcommon/xkbcommon.h> -+ -+#include "cursor-shape-v1-client-protocol.h" -+#include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h" -+#include "xdg-shell-client-protocol.h" -+ -+#define NESTED_DEFAULT_WIDTH 1280 -+#define NESTED_DEFAULT_HEIGHT 720 -+#define NESTED_CURSOR_SIZE 64 -+#define NESTED_OUTPUT_NAME "NESTED-1" -+#define NESTED_TITLE "swc" -+#define NESTED_CAPTURE_KEY "ctrl+alt+g" -+#define NESTED_FULLSCREEN_KEY "ctrl+alt+f" -+ -+/* wl_surface.damage_buffer and wl_pointer.frame first appear here */ -+#define COMPOSITOR_VERSION 4 -+#define SEAT_VERSION 5 -+#define OUTPUT_VERSION 2 -+ -+/* a key the parent window keeps for itself, never passed to clients */ -+struct chord { -+ xkb_keysym_t keysym; -+ uint32_t modifiers; -+ char description[64]; -+}; -+ -+static struct { -+ struct swc_backend backend; -+ -+ struct wl_display *display; -+ struct wl_registry *registry; -+ struct wl_compositor *compositor; -+ uint32_t compositor_version; -+ struct xdg_wm_base *wm_base; -+ struct wl_output *output; -+ struct wl_seat *seat; -+ uint32_t seat_version; -+ struct wl_keyboard *keyboard; -+ struct wl_pointer *pointer; -+ struct zwp_keyboard_shortcuts_inhibit_manager_v1 *inhibit_manager; -+ struct zwp_keyboard_shortcuts_inhibitor_v1 *inhibitor; -+ struct wp_cursor_shape_manager_v1 *shape_manager; -+ struct wp_cursor_shape_device_v1 *shape_device; -+ struct wl_event_source *source; -+ -+ struct wl_surface *surface; -+ struct xdg_surface *xdg_surface; -+ struct xdg_toplevel *toplevel; -+ struct wl_callback *frame; -+ struct screen *screen; -+ bool configured; -+ bool captured; -+ bool fullscreen; -+ -+ /* the parent talks in logical units, swc in buffer pixels. every -+ * size and coordinate crossing this file is converted by scale */ -+ int32_t scale; -+ uint32_t width, height; -+ int32_t pending_width, pending_height; -+ -+ struct { -+ struct wl_surface *surface; -+ struct wld_buffer *buffer; -+ int32_t hotspot_x, hotspot_y; -+ uint32_t enter_serial; -+ bool inside; -+ /* where the parent's pointer is, kept while released so a -+ * capture puts ours there */ -+ wl_fixed_t x, y; -+ /* the click that captured, its release goes nowhere either */ -+ uint32_t swallowed; -+ } cursor; -+ -+ struct { -+ enum wl_pointer_axis_source source; -+ int32_t discrete[2]; -+ } axis; -+ -+ /* parent keymap that arrived before the swc seat existed */ -+ struct xkb_keymap *keymap; -+ -+ struct chord capture_key, fullscreen_key; -+ -+ /* the parent's modifier state, kept here while released so the nested -+ * clients see none of it, and handed to swc on capture */ -+ struct { -+ uint32_t depressed, latched, locked, group; -+ } mods; -+} nested; -+ -+bool -+nested_requested(void) -+{ -+ const char *backend = getenv("SWC_BACKEND"); -+ -+ if (backend) { -+ return strcmp(backend, "nested") == 0; -+ } -+ if (getenv(SWC_LAUNCH_SOCKET_ENV)) { -+ return false; -+ } -+ return getenv("WAYLAND_DISPLAY") != NULL; -+} -+ -+static int32_t -+env_int(const char *name, int32_t fallback) -+{ -+ const char *value = getenv(name); -+ long number; -+ char *end; -+ -+ if (!value || !*value) { -+ return fallback; -+ } -+ number = strtol(value, &end, 10); -+ return *end == '\0' && number > 0 && number <= INT32_MAX -+ ? (int32_t)number -+ : fallback; -+} -+ -+/* primary plane */ -+ -+static void -+handle_frame_done(void *data, struct wl_callback *callback, uint32_t time) -+{ -+ wl_callback_destroy(callback); -+ nested.frame = NULL; -+ /* the clock of the parent is not ours, and clients compare this -+ * against the timestamps swc puts on input events */ -+ view_frame(&nested.screen->planes.primary.view, get_time()); -+} -+ -+static const struct wl_callback_listener frame_listener = { -+ .done = handle_frame_done, -+}; -+ -+static bool -+plane_update(struct view *view) -+{ -+ return true; -+} -+ -+static int -+plane_attach(struct view *view, struct wld_buffer *buffer) -+{ -+ union wld_object object; -+ -+ if (!buffer) { -+ return 0; -+ } -+ if (!wld_export(buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) { -+ ERROR("nested: could not export the screen buffer\n"); -+ return -1; -+ } -+ -+ if (nested.frame) { -+ wl_callback_destroy(nested.frame); -+ } -+ nested.frame = wl_surface_frame(nested.surface); -+ wl_callback_add_listener(nested.frame, &frame_listener, NULL); -+ -+ wl_surface_set_buffer_scale(nested.surface, nested.scale); -+ wl_surface_attach(nested.surface, object.ptr, 0, 0); -+ if (nested.compositor_version >= COMPOSITOR_VERSION) { -+ wl_surface_damage_buffer(nested.surface, 0, 0, INT32_MAX, INT32_MAX); -+ } else { -+ wl_surface_damage(nested.surface, 0, 0, INT32_MAX, INT32_MAX); -+ } -+ wl_surface_commit(nested.surface); -+ -+ return 0; -+} -+ -+static bool -+plane_move(struct view *view, int32_t x, int32_t y) -+{ -+ view_set_position(view, x, y); -+ return true; -+} -+ -+static const struct view_impl plane_impl = { -+ .update = plane_update, -+ .attach = plane_attach, -+ .move = plane_move, -+}; -+ -+static struct wld_surface * -+create_target(struct screen *screen) -+{ -+ const struct swc_rectangle *geometry = &screen->base.geometry; -+ -+ return wld_wayland_create_surface(nested.backend.context, geometry->width, -+ geometry->height, WLD_FORMAT_XRGB8888, -+ WLD_FLAG_MAP, nested.surface); -+} -+ -+/* cursor: show our cursor image as the parent cursor, no lag, no second cursor */ -+ -+static void -+cursor_commit(void) -+{ -+ union wld_object object; -+ -+ if (!nested.pointer || !nested.cursor.inside) { -+ return; -+ } -+ /* released, the window is the parent's like any other and shows the -+ * parent's own cursor. a parent without cursor-shape gets ours */ -+ if (!nested.captured && nested.shape_device) { -+ wp_cursor_shape_device_v1_set_shape(nested.shape_device, nested.cursor.enter_serial, -+ WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT); -+ return; -+ } -+ if (!nested.cursor.buffer || -+ !wld_export(nested.cursor.buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) { -+ wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial, NULL, -+ 0, 0); -+ return; -+ } -+ -+ wl_surface_set_buffer_scale(nested.cursor.surface, nested.scale); -+ wl_surface_attach(nested.cursor.surface, object.ptr, 0, 0); -+ wl_surface_damage(nested.cursor.surface, 0, 0, INT32_MAX, INT32_MAX); -+ wl_surface_commit(nested.cursor.surface); -+ wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial, -+ nested.cursor.surface, -+ nested.cursor.hotspot_x / nested.scale, -+ nested.cursor.hotspot_y / nested.scale); -+} -+ -+static void -+set_cursor(struct wld_buffer *buffer, int32_t hotspot_x, int32_t hotspot_y) -+{ -+ nested.cursor.buffer = buffer; -+ nested.cursor.hotspot_x = hotspot_x; -+ nested.cursor.hotspot_y = hotspot_y; -+ cursor_commit(); -+} -+ -+/* input capture like a vm window: a click or the chord captures, -+ * the chord releases. while captured the parent stops using its own -+ * shortcuts. a parent without the inhibit protocol keeps them */ -+ -+static void -+update_title(void) -+{ -+ char title[2 * sizeof(nested.capture_key.description) + 64]; -+ -+ snprintf(title, sizeof(title), NESTED_TITLE " (%s %s input, %s fullscreen)", -+ nested.capture_key.description, -+ nested.captured ? "releases" : "captures", -+ nested.fullscreen_key.description); -+ xdg_toplevel_set_title(nested.toplevel, title); -+} -+ -+static void -+capture(void) -+{ -+ if (nested.captured) { -+ return; -+ } -+ nested.captured = true; -+ if (nested.inhibit_manager && nested.seat) { -+ nested.inhibitor = -+ zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts( -+ nested.inhibit_manager, nested.surface, nested.seat); -+ } -+ if (swc.seat) { -+ keyboard_handle_modifiers(swc.seat->keyboard, nested.mods.depressed, -+ nested.mods.latched, nested.mods.locked, -+ nested.mods.group); -+ } -+ if (swc.seat && nested.cursor.inside) { -+ pointer_handle_absolute_motion(swc.seat->pointer, get_time(), -+ nested.cursor.x * nested.scale, -+ nested.cursor.y * nested.scale); -+ pointer_handle_frame(swc.seat->pointer); -+ } -+ cursor_commit(); -+ update_title(); -+} -+ -+static void -+release(void) -+{ -+ if (!nested.captured) { -+ return; -+ } -+ nested.captured = false; -+ if (nested.inhibitor) { -+ zwp_keyboard_shortcuts_inhibitor_v1_destroy(nested.inhibitor); -+ nested.inhibitor = NULL; -+ } -+ /* the chord that got us here is still held, and its release goes to -+ * whatever the parent focuses next */ -+ if (swc.seat) { -+ keyboard_reset(swc.seat->keyboard); -+ } -+ cursor_commit(); -+ if (nested.toplevel) { -+ update_title(); -+ } -+} -+ -+static uint32_t -+modifier_from_name(const char *name) -+{ -+ static const struct { -+ const char *name; -+ uint32_t modifier; -+ } names[] = { -+ {"ctrl", SWC_MOD_CTRL}, {"control", SWC_MOD_CTRL}, -+ {"alt", SWC_MOD_ALT}, {"mod1", SWC_MOD_ALT}, -+ {"super", SWC_MOD_LOGO}, {"logo", SWC_MOD_LOGO}, -+ {"mod4", SWC_MOD_LOGO}, {"shift", SWC_MOD_SHIFT}, -+ }; -+ size_t i; -+ -+ for (i = 0; i < ARRAY_LENGTH(names); i++) { -+ if (strcasecmp(name, names[i].name) == 0) { -+ return names[i].modifier; -+ } -+ } -+ return 0; -+} -+ -+/* "ctrl+alt+g": all but the last token are modifiers. -+ * the environment variable wins, "none" turns it off */ -+static void -+parse_chord(struct chord *chord, const char *variable, const char *fallback) -+{ -+ const char *spec = getenv(variable); -+ char buffer[sizeof(chord->description)]; -+ char *token, *next, *save; -+ -+ if (!spec || !*spec) { -+ spec = fallback; -+ } -+ snprintf(buffer, sizeof(buffer), "%s", spec); -+ snprintf(chord->description, sizeof(chord->description), "%s", spec); -+ chord->modifiers = 0; -+ chord->keysym = XKB_KEY_NoSymbol; -+ -+ for (token = strtok_r(buffer, "+", &save); token; token = next) { -+ next = strtok_r(NULL, "+", &save); -+ if (next) { -+ chord->modifiers |= modifier_from_name(token); -+ } else { -+ chord->keysym = -+ xkb_keysym_from_name(token, XKB_KEYSYM_CASE_INSENSITIVE); -+ } -+ } -+} -+ -+/* match the keysym ignoring its level */ -+static uint32_t -+held_modifiers(struct xkb_keymap *map) -+{ -+ static const struct { -+ const char *name; -+ uint32_t mod; -+ } names[] = { -+ {XKB_MOD_NAME_CTRL, SWC_MOD_CTRL}, -+ {XKB_MOD_NAME_ALT, SWC_MOD_ALT}, -+ {XKB_MOD_NAME_LOGO, SWC_MOD_LOGO}, -+ {XKB_MOD_NAME_SHIFT, SWC_MOD_SHIFT}, -+ }; -+ struct xkb_state *state = xkb_state_new(map); -+ uint32_t mods = 0; -+ size_t i; -+ -+ if (!state) { -+ return 0; -+ } -+ xkb_state_update_mask(state, nested.mods.depressed, nested.mods.latched, -+ nested.mods.locked, 0, 0, nested.mods.group); -+ for (i = 0; i < ARRAY_LENGTH(names); i++) { -+ if (xkb_state_mod_name_is_active(state, names[i].name, -+ XKB_STATE_MODS_EFFECTIVE) > 0) { -+ mods |= names[i].mod; -+ } -+ } -+ xkb_state_unref(state); -+ return mods; -+} -+ -+/* match the keysym ignoring its level */ -+static bool -+is_chord(const struct chord *chord, struct keyboard *keyboard, uint32_t key) -+{ -+ struct xkb_keymap *map = keyboard->xkb.keymap.map; -+ const xkb_keysym_t *syms; -+ -+ if (chord->keysym == XKB_KEY_NoSymbol || -+ (held_modifiers(map) & chord->modifiers) != chord->modifiers) { -+ return false; -+ } -+ if (xkb_keymap_key_get_syms_by_level(map, XKB_KEY(key), nested.mods.group, 0, -+ &syms) < 1) { -+ return false; -+ } -+ return xkb_keysym_to_lower(syms[0]) == xkb_keysym_to_lower(chord->keysym); -+} -+ -+static void -+toggle_fullscreen(void) -+{ -+ if (nested.fullscreen) { -+ xdg_toplevel_unset_fullscreen(nested.toplevel); -+ } else { -+ xdg_toplevel_set_fullscreen(nested.toplevel, NULL); -+ } -+} -+ -+/* keyboard */ -+ -+static void -+apply_keymap(void) -+{ -+ if (!nested.keymap || !swc.seat) { -+ return; -+ } -+ if (!keyboard_set_keymap(swc.seat->keyboard, nested.keymap)) { -+ WARNING("nested: could not adopt the parent keymap\n"); -+ } -+ xkb_keymap_unref(nested.keymap); -+ nested.keymap = NULL; -+} -+ -+/* raw key codes mean the same only with the same keymap on both sides */ -+static void -+handle_keyboard_keymap(void *data, struct wl_keyboard *keyboard, -+ uint32_t format, int32_t fd, uint32_t size) -+{ -+ struct xkb_context *context; -+ struct xkb_keymap *keymap = NULL; -+ char *string; -+ -+ if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { -+ close(fd); -+ return; -+ } -+ string = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); -+ close(fd); -+ if (string == MAP_FAILED) { -+ return; -+ } -+ if ((context = xkb_context_new(XKB_CONTEXT_NO_FLAGS))) { -+ keymap = xkb_keymap_new_from_buffer(context, string, -+ strnlen(string, size), -+ XKB_KEYMAP_FORMAT_TEXT_V1, -+ XKB_KEYMAP_COMPILE_NO_FLAGS); -+ xkb_context_unref(context); -+ } -+ munmap(string, size); -+ if (!keymap) { -+ WARNING("nested: could not compile the parent keymap\n"); -+ return; -+ } -+ -+ if (nested.keymap) { -+ xkb_keymap_unref(nested.keymap); -+ } -+ nested.keymap = keymap; -+ apply_keymap(); -+} -+ -+static void -+handle_keyboard_enter(void *data, struct wl_keyboard *keyboard, -+ uint32_t serial, struct wl_surface *surface, -+ struct wl_array *keys) -+{ -+} -+ -+/* keys held when focus leaves are never released, so reset them */ -+static void -+handle_keyboard_leave(void *data, struct wl_keyboard *keyboard, -+ uint32_t serial, struct wl_surface *surface) -+{ -+ if (swc.seat) { -+ keyboard_reset(swc.seat->keyboard); -+ } -+} -+ -+static void -+handle_keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, -+ uint32_t time, uint32_t key, uint32_t state) -+{ -+ if (!swc.seat) { -+ return; -+ } -+ if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { -+ if (is_chord(&nested.capture_key, swc.seat->keyboard, key)) { -+ if (nested.captured) { -+ release(); -+ } else { -+ capture(); -+ } -+ return; -+ } -+ if (is_chord(&nested.fullscreen_key, swc.seat->keyboard, key)) { -+ toggle_fullscreen(); -+ return; -+ } -+ } -+ /* released, keys are the parent's. the chords above still see the -+ * modifiers, which keep being tracked */ -+ if (!nested.captured) { -+ return; -+ } -+ keyboard_handle_key(swc.seat->keyboard, get_time(), key, state); -+} -+ -+/* locks, layout group and modifiers held at focus in */ -+static void -+handle_keyboard_modifiers(void *data, struct wl_keyboard *keyboard, -+ uint32_t serial, uint32_t depressed, -+ uint32_t latched, uint32_t locked, uint32_t group) -+{ -+ nested.mods.depressed = depressed; -+ nested.mods.latched = latched; -+ nested.mods.locked = locked; -+ nested.mods.group = group; -+ if (swc.seat && nested.captured) { -+ keyboard_handle_modifiers(swc.seat->keyboard, depressed, latched, -+ locked, group); -+ } -+} -+ -+static void -+handle_keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, -+ int32_t rate, int32_t delay) -+{ -+} -+ -+static const struct wl_keyboard_listener keyboard_listener = { -+ .keymap = handle_keyboard_keymap, -+ .enter = handle_keyboard_enter, -+ .leave = handle_keyboard_leave, -+ .key = handle_keyboard_key, -+ .modifiers = handle_keyboard_modifiers, -+ .repeat_info = handle_keyboard_repeat_info, -+}; -+ -+/* pointer */ -+ -+/* seats older than SEAT_VERSION send no pointer.frame, clients wait for one */ -+static void -+frame_legacy(void) -+{ -+ if (nested.seat_version < SEAT_VERSION) { -+ pointer_handle_frame(swc.seat->pointer); -+ } -+} -+ -+static void -+handle_pointer_enter(void *data, struct wl_pointer *pointer, uint32_t serial, -+ struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) -+{ -+ nested.cursor.inside = true; -+ nested.cursor.enter_serial = serial; -+ nested.cursor.x = x; -+ nested.cursor.y = y; -+ cursor_commit(); -+ if (!swc.seat || !nested.captured) { -+ return; -+ } -+ pointer_handle_absolute_motion(swc.seat->pointer, get_time(), -+ x * nested.scale, y * nested.scale); -+ frame_legacy(); -+} -+ -+static void -+handle_pointer_leave(void *data, struct wl_pointer *pointer, uint32_t serial, -+ struct wl_surface *surface) -+{ -+ nested.cursor.inside = false; -+} -+ -+static void -+handle_pointer_motion(void *data, struct wl_pointer *pointer, uint32_t time, -+ wl_fixed_t x, wl_fixed_t y) -+{ -+ nested.cursor.x = x; -+ nested.cursor.y = y; -+ if (!swc.seat || !nested.captured) { -+ return; -+ } -+ pointer_handle_absolute_motion(swc.seat->pointer, get_time(), -+ x * nested.scale, y * nested.scale); -+ frame_legacy(); -+} -+ -+static void -+handle_pointer_button(void *data, struct wl_pointer *pointer, uint32_t serial, -+ uint32_t time, uint32_t button, uint32_t state) -+{ -+ if (!swc.seat) { -+ return; -+ } -+ /* released, a click only captures, as in a virtual machine window */ -+ if (!nested.captured) { -+ if (state == WL_POINTER_BUTTON_STATE_PRESSED) { -+ nested.cursor.swallowed = button; -+ capture(); -+ } -+ return; -+ } -+ if (button == nested.cursor.swallowed) { -+ nested.cursor.swallowed = 0; -+ if (state == WL_POINTER_BUTTON_STATE_RELEASED) { -+ return; -+ } -+ } -+ pointer_handle_button(swc.seat->pointer, get_time(), button, state); -+ frame_legacy(); -+} -+ -+static void -+handle_pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time, -+ uint32_t axis, wl_fixed_t value) -+{ -+ int32_t discrete = 0; -+ -+ if (!swc.seat || !nested.captured) { -+ return; -+ } -+ if (axis < ARRAY_LENGTH(nested.axis.discrete)) { -+ discrete = nested.axis.discrete[axis]; -+ nested.axis.discrete[axis] = 0; -+ } -+ pointer_handle_axis(swc.seat->pointer, get_time(), axis, -+ nested.axis.source, value, discrete * 120); -+ frame_legacy(); -+} -+ -+static void -+handle_pointer_frame(void *data, struct wl_pointer *pointer) -+{ -+ nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL; -+ if (swc.seat && nested.captured) { -+ pointer_handle_frame(swc.seat->pointer); -+ } -+} -+ -+static void -+handle_pointer_axis_source(void *data, struct wl_pointer *pointer, -+ uint32_t source) -+{ -+ nested.axis.source = source; -+} -+ -+static void -+handle_pointer_axis_stop(void *data, struct wl_pointer *pointer, uint32_t time, -+ uint32_t axis) -+{ -+} -+ -+static void -+handle_pointer_axis_discrete(void *data, struct wl_pointer *pointer, -+ uint32_t axis, int32_t discrete) -+{ -+ if (axis < ARRAY_LENGTH(nested.axis.discrete)) { -+ nested.axis.discrete[axis] = discrete; -+ } -+} -+ -+static const struct wl_pointer_listener pointer_listener = { -+ .enter = handle_pointer_enter, -+ .leave = handle_pointer_leave, -+ .motion = handle_pointer_motion, -+ .button = handle_pointer_button, -+ .axis = handle_pointer_axis, -+ .frame = handle_pointer_frame, -+ .axis_source = handle_pointer_axis_source, -+ .axis_stop = handle_pointer_axis_stop, -+ .axis_discrete = handle_pointer_axis_discrete, -+}; -+ -+/* seat */ -+ -+static void -+handle_seat_capabilities(void *data, struct wl_seat *seat, -+ uint32_t capabilities) -+{ -+ bool has_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD; -+ bool has_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER; -+ -+ if (has_keyboard && !nested.keyboard) { -+ nested.keyboard = wl_seat_get_keyboard(seat); -+ wl_keyboard_add_listener(nested.keyboard, &keyboard_listener, NULL); -+ } else if (!has_keyboard && nested.keyboard) { -+ wl_keyboard_destroy(nested.keyboard); -+ nested.keyboard = NULL; -+ } -+ -+ if (has_pointer && !nested.pointer) { -+ nested.pointer = wl_seat_get_pointer(seat); -+ wl_pointer_add_listener(nested.pointer, &pointer_listener, NULL); -+ if (nested.shape_manager) -+ nested.shape_device = wp_cursor_shape_manager_v1_get_pointer( -+ nested.shape_manager, nested.pointer); -+ } else if (!has_pointer && nested.pointer) { -+ if (nested.shape_device) { -+ wp_cursor_shape_device_v1_destroy(nested.shape_device); -+ nested.shape_device = NULL; -+ } -+ wl_pointer_destroy(nested.pointer); -+ nested.pointer = NULL; -+ nested.cursor.inside = false; -+ } -+} -+ -+static void -+handle_seat_name(void *data, struct wl_seat *seat, const char *name) -+{ -+} -+ -+static const struct wl_seat_listener seat_listener = { -+ .capabilities = handle_seat_capabilities, -+ .name = handle_seat_name, -+}; -+ -+/* output: only its scale matters */ -+ -+static void -+handle_output_geometry(void *data, struct wl_output *output, int32_t x, -+ int32_t y, int32_t physical_width, -+ int32_t physical_height, int32_t subpixel, -+ const char *make, const char *model, int32_t transform) -+{ -+} -+ -+static void -+handle_output_mode(void *data, struct wl_output *output, uint32_t flags, -+ int32_t width, int32_t height, int32_t refresh) -+{ -+} -+ -+static void -+handle_output_done(void *data, struct wl_output *output) -+{ -+} -+ -+static void -+handle_output_scale(void *data, struct wl_output *output, int32_t factor) -+{ -+ /* SWC_NESTED_SCALE pins the scale. 1 on a dense display makes the -+ * parent magnify us, which is what a session with no scaling of its -+ * own wants */ -+ if (factor > 0 && !nested.surface && !getenv("SWC_NESTED_SCALE")) { -+ nested.scale = factor; -+ } -+} -+ -+static const struct wl_output_listener output_listener = { -+ .geometry = handle_output_geometry, -+ .mode = handle_output_mode, -+ .done = handle_output_done, -+ .scale = handle_output_scale, -+}; -+ -+/* shell surface */ -+ -+static void -+handle_wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial) -+{ -+ xdg_wm_base_pong(wm_base, serial); -+} -+ -+static const struct xdg_wm_base_listener wm_base_listener = { -+ .ping = handle_wm_base_ping, -+}; -+ -+static void -+resize(uint32_t width, uint32_t height) -+{ -+ if (width == nested.width && height == nested.height) { -+ return; -+ } -+ nested.width = width; -+ nested.height = height; -+ if (nested.screen) { -+ screen_resize(nested.screen, width, height); -+ } -+} -+ -+static void -+handle_xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, -+ uint32_t serial) -+{ -+ xdg_surface_ack_configure(xdg_surface, serial); -+ nested.configured = true; -+ -+ if (nested.pending_width > 0 && nested.pending_height > 0) { -+ resize((uint32_t)(nested.pending_width * nested.scale), -+ (uint32_t)(nested.pending_height * nested.scale)); -+ } -+ nested.pending_width = 0; -+ nested.pending_height = 0; -+} -+ -+static const struct xdg_surface_listener xdg_surface_listener = { -+ .configure = handle_xdg_surface_configure, -+}; -+ -+/* a 0 here means the parent leaves the size to us, so keep ours */ -+static void -+handle_toplevel_configure(void *data, struct xdg_toplevel *toplevel, -+ int32_t width, int32_t height, -+ struct wl_array *states) -+{ -+ uint32_t *state; -+ -+ nested.pending_width = width; -+ nested.pending_height = height; -+ nested.fullscreen = false; -+ wl_array_for_each(state, states) { -+ if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) { -+ nested.fullscreen = true; -+ } -+ } -+} -+ -+static void -+handle_toplevel_close(void *data, struct xdg_toplevel *toplevel) -+{ -+ wl_display_terminate(swc.display); -+} -+ -+static const struct xdg_toplevel_listener toplevel_listener = { -+ .configure = handle_toplevel_configure, -+ .close = handle_toplevel_close, -+}; -+ -+static bool -+create_screens(struct wl_list *screens) -+{ -+ struct output *output; -+ unsigned width = NESTED_DEFAULT_WIDTH, height = NESTED_DEFAULT_HEIGHT; -+ const char *size = getenv("SWC_NESTED_SIZE"); -+ -+ if (size && (sscanf(size, "%ux%u", &width, &height) != 2 || width == 0 || -+ height == 0)) { -+ WARNING("nested: ignoring malformed SWC_NESTED_SIZE '%s'\n", size); -+ width = NESTED_DEFAULT_WIDTH; -+ height = NESTED_DEFAULT_HEIGHT; -+ } -+ nested.width = width * (uint32_t)nested.scale; -+ nested.height = height * (uint32_t)nested.scale; -+ -+ nested.surface = wl_compositor_create_surface(nested.compositor); -+ nested.cursor.surface = wl_compositor_create_surface(nested.compositor); -+ if (!nested.surface || !nested.cursor.surface) { -+ return false; -+ } -+ nested.xdg_surface = -+ xdg_wm_base_get_xdg_surface(nested.wm_base, nested.surface); -+ xdg_surface_add_listener(nested.xdg_surface, &xdg_surface_listener, NULL); -+ nested.toplevel = xdg_surface_get_toplevel(nested.xdg_surface); -+ xdg_toplevel_add_listener(nested.toplevel, &toplevel_listener, NULL); -+ update_title(); -+ xdg_toplevel_set_app_id(nested.toplevel, NESTED_TITLE); -+ wl_surface_commit(nested.surface); -+ -+ /* attaching a buffer before the first configure is a protocol -+ * error, and a tiling parent answers with the size it really gives -+ * us, which has to be known before the screen exists */ -+ while (!nested.configured) { -+ if (wl_display_dispatch(nested.display) < 0) { -+ ERROR("nested: lost the parent before the first configure\n"); -+ return false; -+ } -+ } -+ -+ output = output_new_fb(nested.width, nested.height, NESTED_OUTPUT_NAME); -+ if (!output) { -+ return false; -+ } -+ nested.screen = screen_new_virtual(output, &plane_impl); -+ if (!nested.screen) { -+ output_destroy(output); -+ return false; -+ } -+ output->screen = nested.screen; -+ wl_list_insert(screens, &nested.screen->link); -+ -+ return true; -+} -+ -+/* registry */ -+ -+static void -+handle_global(void *data, struct wl_registry *registry, uint32_t name, -+ const char *interface, uint32_t version) -+{ -+ if (strcmp(interface, wl_compositor_interface.name) == 0) { -+ nested.compositor_version = MIN(version, COMPOSITOR_VERSION); -+ nested.compositor = -+ wl_registry_bind(registry, name, &wl_compositor_interface, -+ nested.compositor_version); -+ } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { -+ nested.wm_base = -+ wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); -+ xdg_wm_base_add_listener(nested.wm_base, &wm_base_listener, NULL); -+ } else if (strcmp(interface, wl_seat_interface.name) == 0 && !nested.seat) { -+ nested.seat_version = MIN(version, SEAT_VERSION); -+ nested.seat = wl_registry_bind(registry, name, &wl_seat_interface, -+ nested.seat_version); -+ wl_seat_add_listener(nested.seat, &seat_listener, NULL); -+ } else if (strcmp(interface, wl_output_interface.name) == 0 && -+ !nested.output) { -+ nested.output = wl_registry_bind(registry, name, &wl_output_interface, -+ MIN(version, OUTPUT_VERSION)); -+ wl_output_add_listener(nested.output, &output_listener, NULL); -+ } else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) { -+ nested.shape_manager = wl_registry_bind( -+ registry, name, &wp_cursor_shape_manager_v1_interface, 1); -+ } else if (strcmp(interface, -+ zwp_keyboard_shortcuts_inhibit_manager_v1_interface -+ .name) == 0) { -+ nested.inhibit_manager = wl_registry_bind( -+ registry, name, -+ &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1); -+ } -+} -+ -+static void -+handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) -+{ -+} -+ -+static const struct wl_registry_listener registry_listener = { -+ .global = handle_global, -+ .global_remove = handle_global_remove, -+}; -+ -+/* the parent connection is in our event loop. a check source drains -+ * replies already read, which nothing else wakes the loop for */ -+static int -+handle_display(int fd, uint32_t mask, void *data) -+{ -+ int count; -+ -+ if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) { -+ ERROR("nested: the parent compositor went away\n"); -+ wl_display_terminate(swc.display); -+ return 0; -+ } -+ -+ count = mask & WL_EVENT_READABLE -+ ? wl_display_dispatch(nested.display) -+ : wl_display_dispatch_pending(nested.display); -+ if (count < 0 || -+ (wl_display_flush(nested.display) < 0 && errno != EAGAIN)) { -+ ERROR("nested: parent connection failed: %s\n", strerror(errno)); -+ wl_display_terminate(swc.display); -+ return 0; -+ } -+ return count; -+} -+ -+/* the seat exists only now, well after the parent sent its keymap */ -+static void -+start(void) -+{ -+ apply_keymap(); -+ swc_activate(); -+} -+ -+static void -+finalize(void) -+{ -+ release(); -+ if (nested.keymap) { -+ xkb_keymap_unref(nested.keymap); -+ } -+ if (nested.source) { -+ wl_event_source_remove(nested.source); -+ } -+ if (nested.frame) { -+ wl_callback_destroy(nested.frame); -+ } -+ if (nested.toplevel) { -+ xdg_toplevel_destroy(nested.toplevel); -+ } -+ if (nested.xdg_surface) { -+ xdg_surface_destroy(nested.xdg_surface); -+ } -+ if (nested.surface) { -+ wl_surface_destroy(nested.surface); -+ } -+ if (nested.cursor.surface) { -+ wl_surface_destroy(nested.cursor.surface); -+ } -+ if (nested.backend.renderer) { -+ wld_destroy_renderer(nested.backend.renderer); -+ } -+ if (nested.backend.context) { -+ wld_destroy_context(nested.backend.context); -+ } -+ if (nested.shape_device) { -+ wp_cursor_shape_device_v1_destroy(nested.shape_device); -+ } -+ if (nested.pointer) { -+ wl_pointer_destroy(nested.pointer); -+ } -+ if (nested.keyboard) { -+ wl_keyboard_destroy(nested.keyboard); -+ } -+ if (nested.seat) { -+ wl_seat_destroy(nested.seat); -+ } -+ if (nested.output) { -+ wl_output_destroy(nested.output); -+ } -+ if (nested.inhibit_manager) { -+ zwp_keyboard_shortcuts_inhibit_manager_v1_destroy( -+ nested.inhibit_manager); -+ } -+ if (nested.shape_manager) { -+ wp_cursor_shape_manager_v1_destroy(nested.shape_manager); -+ } -+ if (nested.wm_base) { -+ xdg_wm_base_destroy(nested.wm_base); -+ } -+ if (nested.compositor) { -+ wl_compositor_destroy(nested.compositor); -+ } -+ if (nested.registry) { -+ wl_registry_destroy(nested.registry); -+ } -+ if (nested.display) { -+ wl_display_disconnect(nested.display); -+ } -+ memset(&nested, 0, sizeof(nested)); -+} -+ -+bool -+nested_initialize(void) -+{ -+ int32_t cursor_size; -+ -+ nested.scale = env_int("SWC_NESTED_SCALE", 1); -+ nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL; -+ parse_chord(&nested.capture_key, "SWC_NESTED_CAPTURE_KEY", NESTED_CAPTURE_KEY); -+ parse_chord(&nested.fullscreen_key, "SWC_NESTED_FULLSCREEN_KEY", -+ NESTED_FULLSCREEN_KEY); -+ -+ if (!(nested.display = wl_display_connect(NULL))) { -+ ERROR("nested: could not connect to the parent compositor\n"); -+ goto error; -+ } -+ nested.registry = wl_display_get_registry(nested.display); -+ wl_registry_add_listener(nested.registry, &registry_listener, NULL); -+ /* the first roundtrip binds the globals, the second collects what -+ * they announce on bind: the output scale and the seat capabilities */ -+ if (wl_display_roundtrip(nested.display) < 0 || -+ wl_display_roundtrip(nested.display) < 0) { -+ ERROR("nested: parent connection failed during setup\n"); -+ goto error; -+ } -+ if (!nested.compositor || !nested.wm_base) { -+ ERROR("nested: the parent lacks wl_compositor or xdg_wm_base\n"); -+ goto error; -+ } -+ -+ nested.backend.context = -+ wld_wayland_create_context(nested.display, WLD_SHM, WLD_NONE); -+ if (!nested.backend.context) { -+ ERROR("nested: could not create a wld context on the parent\n"); -+ goto error; -+ } -+ nested.backend.renderer = wld_create_renderer(nested.backend.context); -+ if (!nested.backend.renderer) { -+ ERROR("nested: could not create a wld renderer\n"); -+ goto error; -+ } -+ -+ /* the size of a buffer must be a multiple of its buffer scale */ -+ cursor_size = NESTED_CURSOR_SIZE - NESTED_CURSOR_SIZE % nested.scale; -+ nested.backend.cursor_width = (uint32_t)cursor_size; -+ nested.backend.cursor_height = (uint32_t)cursor_size; -+ nested.backend.virtual_input = true; -+ nested.backend.create_screens = create_screens; -+ nested.backend.create_target = create_target; -+ nested.backend.set_cursor = set_cursor; -+ nested.backend.start = start; -+ nested.backend.finalize = finalize; -+ -+ nested.source = wl_event_loop_add_fd(swc.event_loop, -+ wl_display_get_fd(nested.display), -+ WL_EVENT_READABLE, handle_display, -+ NULL); -+ if (!nested.source) { -+ ERROR("nested: could not watch the parent connection\n"); -+ goto error; -+ } -+ wl_event_source_check(nested.source); -+ -+ swc.backend = &nested.backend; -+ return true; -+ -+error: -+ finalize(); -+ return false; -+} -diff --git a/libswc/nested.h b/libswc/nested.h -new file mode 100644 -index 0000000..5c16e53 ---- /dev/null -+++ b/libswc/nested.h -@@ -0,0 +1,16 @@ -+/* nested backend: swc as a client of another compositor */ -+ -+#ifndef SWC_NESTED_H -+#define SWC_NESTED_H -+ -+#include <stdbool.h> -+ -+/* true to come up nested: SWC_BACKEND decides, else WAYLAND_DISPLAY without a launcher socket */ -+bool -+nested_requested(void); -+ -+/* connects to the parent and installs itself as swc.backend */ -+bool -+nested_initialize(void); -+ -+#endif -diff --git a/libswc/output.c b/libswc/output.c -index 8abcb24..a5ca71f 100644 ---- a/libswc/output.c -+++ b/libswc/output.c -@@ -170,6 +170,25 @@ output_new_fb(uint32_t width, uint32_t height, const char *name) - return output; - } - -+void -+output_set_size(struct output *output, uint32_t width, uint32_t height) -+{ -+ struct wl_resource *resource; -+ struct mode *mode = output->preferred_mode; -+ -+ mode->width = width; -+ mode->height = height; -+ wl_resource_for_each(resource, &output->resources) -+ { -+ wl_output_send_mode(resource, -+ WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED, -+ mode->width, mode->height, mode->refresh); -+ if (wl_resource_get_version(resource) >= 2) { -+ wl_output_send_done(resource); -+ } -+ } -+} -+ - void - output_destroy(struct output *output) - { -diff --git a/libswc/output.h b/libswc/output.h -index 82d45cc..01c1dc0 100644 ---- a/libswc/output.h -+++ b/libswc/output.h -@@ -40,6 +40,8 @@ output_new(drmModeConnector *connector); - struct output * - output_new_fb(uint32_t width, uint32_t height, const char *name); - void -+output_set_size(struct output *output, uint32_t width, uint32_t height); -+void - output_destroy(struct output *output); - - #endif -diff --git a/libswc/pointer.c b/libswc/pointer.c -index 0b576b7..88d3740 100644 ---- a/libswc/pointer.c -+++ b/libswc/pointer.c -@@ -212,6 +212,13 @@ attach(struct view *view, struct wld_buffer *buffer) - view_update_screens(view); - } - -+ if (swc.backend->set_cursor) { -+ swc.backend->set_cursor(buffer ? pointer->cursor.buffer : NULL, -+ pointer->cursor.hotspot.x, -+ pointer->cursor.hotspot.y); -+ return 0; -+ } -+ - #ifdef ENABLE_DRM - wl_list_for_each(screen, &swc.screens, link) { - view_attach(&screen->planes.cursor->view, -@@ -236,6 +243,11 @@ move(struct view *view, int32_t x, int32_t y) - view_update_screens(view); - } - -+ /* the backend shows the cursor itself and already knows where it is */ -+ if (swc.backend->set_cursor) { -+ return true; -+ } -+ - #ifdef ENABLE_DRM - wl_list_for_each(screen, &swc.screens, link) { - view_move(&screen->planes.cursor->view, view->geometry.x, -diff --git a/libswc/screen.c b/libswc/screen.c -index ba43dd8..31c5db5 100644 ---- a/libswc/screen.c -+++ b/libswc/screen.c -@@ -22,6 +22,8 @@ - */ - - #include "screen.h" -+#include "backend.h" -+#include "compositor.h" - #ifdef ENABLE_DRM - #include "drm.h" - #else -@@ -36,6 +38,7 @@ - #include "plane.h" - #endif - #include "pointer.h" -+#include "seat.h" - #include "util.h" - - #include "swc-server-protocol.h" -@@ -72,13 +75,15 @@ screens_initialize(void) - { - wl_list_init(&swc.screens); - -- if (! -+ if (!(swc.backend->create_screens -+ ? swc.backend->create_screens(&swc.screens) -+ : - #ifdef ENABLE_DRM -- drm_create_screens(&swc.screens) -+ drm_create_screens(&swc.screens) - #else -- fb_create_screens(&swc.screens) -+ fb_create_screens(&swc.screens) - #endif -- ) { -+ )) { - return false; - } - -@@ -200,15 +205,97 @@ screen_destroy(struct screen *screen) - wl_signal_emit(&screen->destroy_signal, NULL); - wl_list_for_each_safe(output, next, &screen->outputs, link) - output_destroy(output); -- primary_plane_finalize(&screen->planes.primary); -+ if (!screen->virtual) { -+ primary_plane_finalize(&screen->planes.primary); - #ifdef ENABLE_DRM -- if (screen->planes.cursor) { -- plane_destroy(screen->planes.cursor); -- } -+ if (screen->planes.cursor) { -+ plane_destroy(screen->planes.cursor); -+ } - #endif -+ } - free(screen); - } - -+struct screen * -+screen_new_virtual(struct output *output, const struct view_impl *impl) -+{ -+ struct screen *screen; -+ int32_t x = 0; -+ -+ wl_list_for_each(screen, &swc.screens, link) x = -+ MAX(x, screen->base.geometry.x + screen->base.geometry.width); -+ -+ if (!(screen = calloc(1, sizeof(*screen)))) { -+ return NULL; -+ } -+ -+ screen->global = wl_global_create( -+ swc.display, &swc_screen_interface, 1, screen, &bind_screen); -+ if (!screen->global) { -+ ERROR("Failed to create screen global\n"); -+ free(screen); -+ return NULL; -+ } -+ -+ screen->virtual = true; -+ view_initialize(&screen->planes.primary.view, impl); -+ screen->planes.primary.view.geometry.width = output->preferred_mode->width; -+ screen->planes.primary.view.geometry.height = -+ output->preferred_mode->height; -+ screen->planes.primary.mode = *output->preferred_mode; -+ -+ screen->handler = &null_handler; -+ wl_signal_init(&screen->destroy_signal); -+ wl_list_init(&screen->resources); -+ wl_list_init(&screen->outputs); -+ wl_list_insert(&screen->outputs, &output->link); -+ wl_list_init(&screen->modifiers); -+ -+ view_move(&screen->planes.primary.view, x, 0); -+ screen->base.geometry = screen->planes.primary.view.geometry; -+ screen->base.usable_geometry = screen->base.geometry; -+ -+ swc.manager->new_screen(&screen->base); -+ -+ return screen; -+} -+ -+void -+screen_resize(struct screen *screen, uint32_t width, uint32_t height) -+{ -+ struct output *output; -+ struct screen *other; -+ pixman_region32_t region; -+ -+ screen->planes.primary.view.geometry.width = width; -+ screen->planes.primary.view.geometry.height = height; -+ screen->planes.primary.mode.width = width; -+ screen->planes.primary.mode.height = height; -+ screen->base.geometry.width = width; -+ screen->base.geometry.height = height; -+ -+ wl_list_for_each(output, &screen->outputs, link) -+ output_set_size(output, width, height); -+ -+ compositor_screen_resize(screen); -+ -+ if (swc.seat) { -+ pixman_region32_init(&region); -+ wl_list_for_each(other, &swc.screens, link) -+ pixman_region32_union_rect(&region, &region, -+ other->base.geometry.x, -+ other->base.geometry.y, -+ other->base.geometry.width, -+ other->base.geometry.height); -+ pointer_set_region(swc.seat->pointer, &region); -+ pixman_region32_fini(&region); -+ } -+ -+ /* a resize can leave origin and edges unchanged, notify anyway */ -+ screen->base.usable_geometry.width = 0; -+ screen_update_usable_geometry(screen); -+} -+ - void - screen_update_usable_geometry(struct screen *screen) - { -diff --git a/libswc/screen.h b/libswc/screen.h -index d53b566..a64072b 100644 ---- a/libswc/screen.h -+++ b/libswc/screen.h -@@ -51,6 +51,8 @@ struct screen { - - struct wl_signal destroy_signal; - uint8_t id; -+ /* no hardware behind it: the backend owns the primary plane */ -+ bool virtual; - #ifdef ENABLE_DRM - uint32_t crtc; - #endif -@@ -80,6 +82,10 @@ screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane); - struct screen * - screen_new(struct output *output); - #endif -+struct screen * -+screen_new_virtual(struct output *output, const struct view_impl *impl); -+void -+screen_resize(struct screen *screen, uint32_t width, uint32_t height); - void - screen_destroy(struct screen *screen); - -diff --git a/libswc/seat.c b/libswc/seat.c -index 413a396..98728df 100644 ---- a/libswc/seat.c -+++ b/libswc/seat.c -@@ -22,6 +22,7 @@ - */ - - #include "seat.h" -+#include "backend.h" - #include "compositor.h" - #include "data_device.h" - #include "event.h" -@@ -122,11 +123,13 @@ handle_swc_event(struct wl_listener *listener, void *data) - - switch (ev->type) { - case SWC_EVENT_DEACTIVATED: -- libinput_suspend(seat->libinput); -+ if (seat->libinput) { -+ libinput_suspend(seat->libinput); -+ } - keyboard_reset(seat->base.keyboard); - break; - case SWC_EVENT_ACTIVATED: -- if (libinput_resume(seat->libinput) != 0) { -+ if (seat->libinput && libinput_resume(seat->libinput) != 0) { - WARNING("Failed to resume libinput context\n"); - } - break; -@@ -484,7 +487,12 @@ seat_create(struct wl_display *display, const char *seat_name) - } - seat->base.pointer = &seat->pointer; - -- if (!initialize_libinput(seat)) { -+ seat->libinput = NULL; -+ seat->libinput_source = NULL; -+ if (swc.backend->virtual_input) { -+ update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD | -+ WL_SEAT_CAPABILITY_POINTER); -+ } else if (!initialize_libinput(seat)) { - goto error6; - } - -@@ -511,11 +519,13 @@ seat_destroy(struct swc_seat *seat_base) - { - struct seat *seat = wl_container_of(seat_base, seat, base); - -- wl_event_source_remove(seat->libinput_source); -- libinput_unref(seat->libinput); -+ if (seat->libinput) { -+ wl_event_source_remove(seat->libinput_source); -+ libinput_unref(seat->libinput); - #ifdef ENABLE_LIBUDEV -- udev_unref(seat->udev); -+ udev_unref(seat->udev); - #endif -+ } - - pointer_finalize(&seat->pointer); - keyboard_destroy(seat->base.keyboard); -diff --git a/libswc/swc.c b/libswc/swc.c -index 55f3351..62e3e88 100644 ---- a/libswc/swc.c -+++ b/libswc/swc.c -@@ -36,6 +36,9 @@ - #include "keyboard.h" - #include "launch.h" - #include "layer_shell.h" -+#ifdef ENABLE_NESTED -+#include "nested.h" -+#endif - #include "panel_manager.h" - #include "pointer.h" - #include "screen.h" -@@ -167,20 +170,18 @@ swc_cursor_set_position(int32_t x, int32_t y) - return true; - } - --EXPORT bool --swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, -- const struct swc_manager *manager) -+static bool -+backend_initialize(void) - { -- swc.display = display; -- swc.event_loop = -- event_loop ? event_loop : wl_display_get_event_loop(display); -- swc.manager = manager; -- const char *default_seat = "seat0"; -- wl_signal_init(&swc.event_signal); -+#ifdef ENABLE_NESTED -+ if (nested_requested()) { -+ return nested_initialize(); -+ } -+#endif - - if (!launch_initialize()) { - ERROR("Could not connect to swc-launch\n"); -- goto error0; -+ return false; - } - - if (! -@@ -191,7 +192,42 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, - #endif - ) { - ERROR("Could not initialize video backend\n"); -- goto error1; -+ launch_finalize(); -+ return false; -+ } -+ -+ return true; -+} -+ -+static void -+backend_finalize(void) -+{ -+ if (swc.backend->finalize) { -+ swc.backend->finalize(); -+ return; -+ } -+ -+#ifdef ENABLE_DRM -+ drm_finalize(); -+#else -+ fb_finalize(); -+#endif -+ launch_finalize(); -+} -+ -+EXPORT bool -+swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, -+ const struct swc_manager *manager) -+{ -+ swc.display = display; -+ swc.event_loop = -+ event_loop ? event_loop : wl_display_get_event_loop(display); -+ swc.manager = manager; -+ const char *default_seat = "seat0"; -+ wl_signal_init(&swc.event_signal); -+ -+ if (!backend_initialize()) { -+ goto error0; - } - - swc.shm = shm_create(display); -@@ -299,6 +335,10 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, - - setup_compositor(); - -+ if (swc.backend->start) { -+ swc.backend->start(); -+ } -+ - return true; - - error17: -@@ -334,13 +374,7 @@ error4: - error3: - shm_destroy(swc.shm); - error2: --#ifdef ENABLE_DRM -- drm_finalize(); --#else -- fb_finalize(); --#endif --error1: -- launch_finalize(); -+ backend_finalize(); - error0: - return false; - } -@@ -365,10 +399,5 @@ swc_finalize(void) - screens_finalize(); - bindings_finalize(); - shm_destroy(swc.shm); --#ifdef ENABLE_DRM -- drm_finalize(); --#else -- fb_finalize(); --#endif -- launch_finalize(); -+ backend_finalize(); - } -diff --git a/meson.build b/meson.build -index 356dcbc..1cfefa2 100644 ---- a/meson.build -+++ b/meson.build -@@ -124,6 +124,20 @@ if input_backend == 'libinput' - requires_private += [libinput, udev] - endif - -+# the nested backend replaces the display and the input devices at run -+# time, so it rides along in any build. it feeds the libinput seat, the -+# wscons one has no virtual input path -+wayland_client = dependency('wayland-client', required: get_option('nested')) -+have_nested = (wayland_client.found() and input_backend == 'libinput' -+ and cc.has_header('wld/wayland.h', dependencies: dependency('wld'))) -+if get_option('nested').enabled() and not have_nested -+ error('The nested backend needs wayland-client, the libinput input backend, and wld built with Wayland support') -+endif -+if have_nested -+ add_project_arguments('-DENABLE_NESTED=1', language: 'c') -+ requires_private += wayland_client -+endif -+ - subdir('protocol') - subdir('cursor') - subdir('launch') -@@ -139,6 +153,7 @@ endif - - summary({ - 'Xwayland support': xwayland.found(), -+ 'Nested backend': have_nested, - 'Input backend': input_backend, - 'Video backend': video_backend, - 'Framebuffer interface': video_backend == 'fb' ? framebuffer_backend : 'n/a', -diff --git a/meson.options b/meson.options -index 98a42a1..488182b 100644 ---- a/meson.options -+++ b/meson.options -@@ -5,5 +5,6 @@ option('fb', type: 'combo', value: 'auto', choices: ['auto', 'fbdev', 'wsdisplay - option('fbdev-device', type: 'string', value: '/dev/fb0', description: 'Linux framebuffer device') - option('wsdisplay-device', type: 'string', value: '', description: 'wsdisplay framebuffer device (empty uses the VT selected by swc-launch)') - option('udev', type: 'feature', value: 'auto', description: 'libinput libudev support') -+option('nested', type: 'feature', value: 'auto', description: 'Backend that runs swc inside another Wayland compositor') - option('extra', type: 'boolean', value: true, description: 'Build swc utility applications') - option('example', type: 'boolean', value: false, description: 'Build swc example compositor') -diff --git a/protocol/meson.build b/protocol/meson.build -index 4896a30..02129b3 100644 ---- a/protocol/meson.build -+++ b/protocol/meson.build -@@ -14,6 +14,9 @@ foreach proto : [ - wl.find_protocol('xdg-decoration', state: 'unstable', version: 1), - wl.find_protocol('xdg-output', state: 'unstable', version: 1), - wl.find_protocol('xdg-dialog', state: 'staging', version: 1), -+ wl.find_protocol('keyboard-shortcuts-inhibit', state: 'unstable', version: 1), -+ wl.find_protocol('cursor-shape', state: 'staging', version: 1), -+ wl.find_protocol('tablet', state: 'unstable', version: 2), - ] - protocols = wl.scan_xml(proto, - public: true, --- -2.56.1 - diff --git a/patches/netlinkKernelEvents.patch b/patches/netlinkKernelEvents.patch @@ -1,18 +0,0 @@ ---- a/libswc/seat.c -+++ b/libswc/seat.c -@@ -51,8 +51,14 @@ - #include <libudev.h> - #endif - -+/* The netlink groups libinput listens on for devices that appear: a bit -+ * for each group, and group 1 is the kernel's own uevents, which are there -+ * with no device manager running. Another group hears nothing unless -+ * something forwards to it, and a device that appears after the compositor -+ * started (a touchpad whose driver binds late, a USB keyboard) is then not -+ * seen until the seat is resumed. */ - #ifndef NETLINK_MASK --#define NETLINK_MASK 4 -+#define NETLINK_MASK 1 - #endif - - struct seat { diff --git a/patches/neuswc-buffer-release.patch b/patches/neuswc-buffer-release.patch @@ -1,33 +0,0 @@ ---- a/libswc/screen.c -+++ b/libswc/screen.c -@@ -219,7 +219,9 @@ - wl_signal_emit(&screen->destroy_signal, NULL); - wl_list_for_each_safe(output, next, &screen->outputs, link) - output_destroy(output); -- if (!screen->virtual) { -+ if (screen->virtual) { -+ view_finalize(&screen->planes.primary.view); -+ } else { - primary_plane_finalize(&screen->planes.primary); - #ifdef ENABLE_DRM - if (screen->planes.cursor) { ---- a/libswc/pointer.c -+++ b/libswc/pointer.c -@@ -571,6 +571,17 @@ - void - pointer_finalize(struct pointer *pointer) - { -+ if (pointer->cursor.surface) { -+ surface_set_view(pointer->cursor.surface, NULL); -+ wl_list_remove(&pointer->cursor.destroy_listener.link); -+ } -+ if (swc.backend->set_cursor) -+ swc.backend->set_cursor(NULL, 0, 0); -+ view_finalize(&pointer->cursor.view); -+ if (pointer->cursor.internal_buffer) -+ wld_buffer_unreference(pointer->cursor.internal_buffer); -+ wld_buffer_unreference(pointer->cursor.buffer); -+ wl_array_release(&pointer->buttons); - input_focus_finalize(&pointer->focus); - pixman_region32_fini(&pointer->region); - } diff --git a/patches/neuswc-configure.patch b/patches/neuswc-configure.patch @@ -1,86 +0,0 @@ ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -321,23 +321,27 @@ - return false; - } - --static uint32_t -+static void - send_configure(struct xdg_toplevel *toplevel, int32_t width, int32_t height) - { - uint32_t serial = wl_display_next_serial(swc.display); -- -- if (width < 0) { -- width = toplevel->window.configure.width; -- } -- if (height < 0) { -- height = toplevel->window.configure.height; -- } - - xdg_toplevel_send_configure(toplevel->resource, width, height, - &toplevel->states); - xdg_surface_send_configure(toplevel->xdg_surface->resource, serial); -- -- return serial; -+ toplevel->xdg_surface->configure_serial = serial; -+} -+ -+static void -+send_state(struct xdg_toplevel *toplevel) -+{ -+ struct window *window = &toplevel->window; -+ struct swc_rectangle *geometry = &window->view->base.geometry; -+ -+ if (window->configure.pending) -+ send_configure(toplevel, window->configure.width, window->configure.height); -+ else -+ send_configure(toplevel, geometry->width, geometry->height); - } - - static void -@@ -346,33 +350,25 @@ - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); - - window->configure.acknowledged = false; -- toplevel->xdg_surface->configure_serial = -- send_configure(toplevel, width, height); -+ send_configure(toplevel, width, height); - } - - static void - focus(struct window *window) - { - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); -- uint32_t width = window->view->base.geometry.width; -- uint32_t height = window->view->base.geometry.height; - - add_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); -- /* dont send 0x0 on focus change */ -- send_configure(toplevel, width ? (int32_t)width : -1, -- height ? (int32_t)height : -1); -+ send_state(toplevel); - } - - static void - unfocus(struct window *window) - { - struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); -- uint32_t width = window->view->base.geometry.width; -- uint32_t height = window->view->base.geometry.height; - - remove_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); -- send_configure(toplevel, width ? (int32_t)width : -1, -- height ? (int32_t)height : -1); -+ send_state(toplevel); - } - - static void -@@ -406,7 +402,7 @@ - break; - } - -- send_configure(toplevel, -1, -1); -+ send_state(toplevel); - } - - static const struct window_impl toplevel_window_impl = { diff --git a/patches/neuswc-cursor-frame.patch b/patches/neuswc-cursor-frame.patch @@ -1,15 +0,0 @@ ---- a/libswc/pointer.c -+++ b/libswc/pointer.c -@@ -800,12 +800,10 @@ - handler->motion(handler, time, pointer->x, pointer->y)) { - handler->pending = true; - break; - } - } -- -- update_cursor(pointer); - } - - void - pointer_handle_frame(struct pointer *pointer) - { diff --git a/patches/neuswc-fullscreen.patch b/patches/neuswc-fullscreen.patch @@ -1,81 +0,0 @@ ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -327,6 +327,12 @@ - void - swc_window_set_handler(struct swc_window *window, - const struct swc_window_handler *handler, void *data); -+ -+/* Deliver xdg_toplevel fullscreen requests using the window handler's data. */ -+#define SWC_WINDOW_FULLSCREEN_REQUEST 1 -+void -+swc_window_set_fullscreen_handler(struct swc_window *window, -+ void (*handler)(void *data, bool fullscreen)); - - /** - * Request that the specified window close. ---- a/libswc/window.h -+++ b/libswc/window.h -@@ -47,6 +47,7 @@ - const struct window_impl *impl; - const struct swc_window_handler *handler; - void *handler_data; -+ void (*fullscreen)(void *, bool); - - struct compositor_view *view; - struct view_handler view_handler; ---- a/libswc/window.c -+++ b/libswc/window.c -@@ -189,6 +189,13 @@ - } - - EXPORT void -+swc_window_set_fullscreen_handler(struct swc_window *base, -+ void (*handler)(void *, bool)) -+{ -+ INTERNAL(base)->fullscreen = handler; -+} -+ -+EXPORT void - swc_window_close(struct swc_window *base) - { - struct window *window = INTERNAL(base); -@@ -564,6 +571,7 @@ - window->base.min_height = 0; - window->base.max_width = 0; - window->base.max_height = 0; -+ window->fullscreen = NULL; - window->managed = false; - window->mode = WINDOW_MODE_STACKED; - window->move.pending = false; -@@ -621,6 +629,7 @@ - window->handler->destroy(window->handler_data); - } - window->handler = &null_handler; -+ window->fullscreen = NULL; - window->managed = false; - } - ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -516,11 +516,21 @@ - set_fullscreen(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *output) - { -+ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); -+ struct window *window = &toplevel->window; -+ -+ if (window->fullscreen) -+ window->fullscreen(window->handler_data, true); - } - - static void - unset_fullscreen(struct wl_client *client, struct wl_resource *resource) - { -+ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); -+ struct window *window = &toplevel->window; -+ -+ if (window->fullscreen) -+ window->fullscreen(window->handler_data, false); - } - - static void diff --git a/patches/panelRedockOnResize.patch b/patches/panelRedockOnResize.patch @@ -1,83 +0,0 @@ -From 18dff5faa05c056e743f8de34dce201c253ac573 Mon Sep 17 00:00:00 2001 -From: xplshn <noreply@localhost> -Date: Mon, 16 Nov 2026 10:57:19 -0300 -Subject: [PATCH 02/19] panel: redock on screen resize - ---- - libswc/panel.c | 36 ++++++++++++++++++++++++++---------- - 1 file changed, 26 insertions(+), 10 deletions(-) - -diff --git a/libswc/panel.c b/libswc/panel.c -index 5434d38..09add87 100644 ---- a/libswc/panel.c -+++ b/libswc/panel.c -@@ -47,9 +47,26 @@ struct panel { - uint32_t edge; - uint32_t offset, strut_size; - uint32_t y_offset; -+ /* length last sent */ -+ uint32_t length; - bool docked; - }; - -+static uint32_t -+edge_length(struct screen *screen, uint32_t edge) -+{ -+ switch (edge) { -+ case SWC_PANEL_EDGE_TOP: -+ case SWC_PANEL_EDGE_BOTTOM: -+ return screen->base.geometry.width; -+ case SWC_PANEL_EDGE_LEFT: -+ case SWC_PANEL_EDGE_RIGHT: -+ return screen->base.geometry.height; -+ default: -+ return 0; -+ } -+} -+ - static void - update_position(struct panel *panel) - { -@@ -95,16 +112,7 @@ dock(struct wl_client *client, struct wl_resource *resource, uint32_t edge, - screen = wl_container_of(swc.screens.next, screen, link); - } - -- switch (edge) { -- case SWC_PANEL_EDGE_TOP: -- case SWC_PANEL_EDGE_BOTTOM: -- length = screen->base.geometry.width; -- break; -- case SWC_PANEL_EDGE_LEFT: -- case SWC_PANEL_EDGE_RIGHT: -- length = screen->base.geometry.height; -- break; -- default: -+ if (!(length = edge_length(screen, edge))) { - return; - } - -@@ -115,6 +123,7 @@ dock(struct wl_client *client, struct wl_resource *resource, uint32_t edge, - - panel->screen = screen; - panel->edge = edge; -+ panel->length = length; - panel->docked = true; - - update_position(panel); -@@ -196,6 +205,13 @@ modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, - - assert(panel->docked); - -+ /* every usable-area pass visits us, so notice a size change here */ -+ if (edge_length(panel->screen, panel->edge) != panel->length) { -+ panel->length = edge_length(panel->screen, panel->edge); -+ update_position(panel); -+ swc_panel_send_docked(panel->resource, panel->length); -+ } -+ - DEBUG("Original geometry { x1: %d, y1: %d, x2: %d, y2: %d }\n", box.x1, - box.y1, box.x2, box.y2); - --- -2.56.1 - diff --git a/patches/popupGrabDismiss.patch b/patches/popupGrabDismiss.patch @@ -1,115 +0,0 @@ -From 146bb9619c415ec40769a31b7a9b89b1679c42bd Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 17 Nov 2026 23:11:35 -0300 -Subject: [PATCH 11/19] xdg_shell: popup grabs end on an outside press - -Menus never closed. ---- - libswc/xdg_shell.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ - 1 file changed, 61 insertions(+) - -diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c -index b3c160f..fb87d03 100644 ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -24,6 +24,7 @@ - #include "xdg_shell.h" - #include "compositor.h" - #include "internal.h" -+#include "pointer.h" - #include "screen.h" - #include "seat.h" - #include "surface.h" -@@ -64,8 +65,55 @@ struct xdg_popup { - struct xdg_surface *xdg_surface; - struct xdg_positioner positioner; - struct compositor_view *view; -+ struct wl_list grab_link; /* in grabs while it holds a grab */ -+ bool grabbing; - }; - -+/* popups holding a grab, newest last. a press elsewhere dismisses them newest first */ -+static struct wl_list grabs = {&grabs, &grabs}; -+ -+static bool popup_grab_button(struct pointer_handler *handler, uint32_t time, -+ struct button *button, uint32_t state); -+ -+static struct pointer_handler popup_grab_handler = { -+ .button = popup_grab_button, -+}; -+ -+static void -+grab_end(struct xdg_popup *popup) -+{ -+ if (!popup->grabbing) { -+ return; -+ } -+ wl_list_remove(&popup->grab_link); -+ popup->grabbing = false; -+ if (wl_list_empty(&grabs)) { -+ wl_list_remove(&popup_grab_handler.link); -+ wl_list_init(&popup_grab_handler.link); -+ } -+} -+ -+static bool -+popup_grab_button(struct pointer_handler *handler, uint32_t time, -+ struct button *button, uint32_t state) -+{ -+ struct xdg_popup *top, *popup, *tmp; -+ struct wl_client *focus = swc.seat->pointer->focus.client; -+ -+ if (state != WL_POINTER_BUTTON_STATE_PRESSED || wl_list_empty(&grabs)) { -+ return false; -+ } -+ top = wl_container_of(grabs.prev, top, grab_link); -+ if (focus && focus == wl_resource_get_client(top->resource)) { -+ return false; -+ } -+ wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { -+ xdg_popup_send_popup_done(popup->resource); -+ grab_end(popup); -+ } -+ return true; -+} -+ - /* xdg_positioner */ - static void - destroy_positioner(struct wl_resource *resource) -@@ -546,6 +594,7 @@ destroy_popup(struct wl_resource *resource) - { - struct xdg_popup *popup = wl_resource_get_user_data(resource); - -+ grab_end(popup); - compositor_view_destroy(popup->view); - free(popup); - } -@@ -554,6 +603,17 @@ static void - grab(struct wl_client *client, struct wl_resource *resource, - struct wl_resource *seat, uint32_t serial) - { -+ struct xdg_popup *popup = wl_resource_get_user_data(resource); -+ -+ if (popup->grabbing) { -+ return; -+ } -+ if (wl_list_empty(&grabs)) { -+ /* before bindings and clients */ -+ wl_list_insert(&swc.seat->pointer->handlers, &popup_grab_handler.link); -+ } -+ wl_list_insert(grabs.prev, &popup->grab_link); -+ popup->grabbing = true; - } - - static const struct xdg_popup_interface popup_impl = { -@@ -716,6 +776,7 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, - } - popup->xdg_surface = xdg_surface; - popup->positioner = *positioner; -+ popup->grabbing = false; - popup->resource = - wl_resource_create(client, &xdg_popup_interface, version, id); - if (!popup->resource) { --- -2.56.1 - diff --git a/patches/popupGrabKeyboardFocus.patch b/patches/popupGrabKeyboardFocus.patch @@ -1,64 +0,0 @@ ---- a/libswc/keyboard.c -+++ b/libswc/keyboard.c -@@ -29,6 +29,7 @@ - #include "compositor.h" - #include "internal.h" - #include "surface.h" -+#include "xdg_shell.h" - #include "swc.h" - #include "util.h" - -@@ -410,6 +411,8 @@ - void - keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view) - { -+ xdg_popups_focus_changed( -+ view && view->surface ? wl_resource_get_client(view->surface->resource) : NULL); - input_focus_set(&keyboard->focus, view); - } - ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -114,6 +114,27 @@ - return true; - } - -+/* Keyboard focus went to client: a menu is a grab, and a grab ends when focus -+ * leaves what grabbed it, as menus do on every desktop. A press elsewhere -+ * is the other way it ends (popup_grab_button). */ -+void -+xdg_popups_focus_changed(struct wl_client *client) -+{ -+ struct xdg_popup *top, *popup, *tmp; -+ -+ if (wl_list_empty(&grabs)) { -+ return; -+ } -+ top = wl_container_of(grabs.prev, top, grab_link); -+ if (client && client == wl_resource_get_client(top->resource)) { -+ return; -+ } -+ wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { -+ xdg_popup_send_popup_done(popup->resource); -+ grab_end(popup); -+ } -+} -+ - /* xdg_positioner */ - static void - destroy_positioner(struct wl_resource *resource) ---- a/libswc/xdg_shell.h -+++ b/libswc/xdg_shell.h -@@ -37,6 +37,12 @@ - void - xdg_popup_set_parent(struct wl_resource *popup, struct surface *parent); - -+/* keyboard focus moved to client (NULL for none): grabbing popups end unless -+ * it is theirs */ -+struct wl_client; -+void -+xdg_popups_focus_changed(struct wl_client *client); -+ - /* window of an xdg_toplevel */ - struct window * - xdg_toplevel_window(struct wl_resource *resource); diff --git a/patches/popupsFitScreenKeysOnDemand.patch b/patches/popupsFitScreenKeysOnDemand.patch @@ -1,127 +0,0 @@ -From 1664c1fe1f4ed89ba49dd84bfbd7ec9e7c901104 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 17 Nov 2026 13:58:47 -0300 -Subject: [PATCH 09/19] popups fit the screen, on-demand layer surfaces get - keys - ---- - libswc/layer_shell.c | 27 +++++++++++++++++++++++++-- - libswc/layer_shell.h | 5 +++++ - libswc/pointer.c | 7 +++++++ - libswc/xdg_shell.c | 9 +++++++++ - 4 files changed, 46 insertions(+), 2 deletions(-) - -diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c -index 5cd130a..209cbb7 100644 ---- a/libswc/layer_shell.c -+++ b/libswc/layer_shell.c -@@ -394,8 +394,9 @@ handle_attach(struct view_handler *handler) - restack_layer(surface); - if (!surface->mapped) { - compositor_view_show(surface->view); -- if (surface->current.keyboard_interactivity != -- ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE) { -+ /* on demand waits for a press, see layer_shell_pressed */ -+ if (surface->current.keyboard_interactivity == -+ ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE) { - keyboard_set_focus(swc.seat->keyboard, surface->view); - } - } -@@ -670,6 +671,28 @@ bind_layer_shell(struct wl_client *client, void *data, uint32_t version, - wl_resource_set_implementation(resource, &layer_shell_impl, NULL, NULL); - } - -+void -+layer_shell_pressed(struct compositor_view *view) -+{ -+ struct view_handler *handler; -+ struct layer_surface *surface; -+ -+ if (!view) { -+ return; -+ } -+ wl_list_for_each(handler, &view->base.handlers, link) { -+ if (handler->impl != &view_handler_impl) { -+ continue; -+ } -+ surface = wl_container_of(handler, surface, view_handler); -+ if (surface->current.keyboard_interactivity == -+ ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND) { -+ keyboard_set_focus(swc.seat->keyboard, view); -+ } -+ return; -+ } -+} -+ - struct wl_global * - layer_shell_create(struct wl_display *display) - { -diff --git a/libswc/layer_shell.h b/libswc/layer_shell.h -index 5d8bab4..138db1a 100644 ---- a/libswc/layer_shell.h -+++ b/libswc/layer_shell.h -@@ -13,4 +13,9 @@ layer_shell_create(struct wl_display *display); - void - layer_shell_usable_changed(struct screen *screen); - -+/* a press on view gives on-demand layer surfaces the keyboard */ -+struct compositor_view; -+void -+layer_shell_pressed(struct compositor_view *view); -+ - #endif -diff --git a/libswc/pointer.c b/libswc/pointer.c -index ab23c3a..0b576b7 100644 ---- a/libswc/pointer.c -+++ b/libswc/pointer.c -@@ -26,6 +26,7 @@ - #include "compositor.h" - #include "cursor/cursor_data.h" - #include "event.h" -+#include "layer_shell.h" - #include "internal.h" - #ifdef ENABLE_DRM - #include "plane.h" -@@ -62,6 +63,9 @@ swc_pointer_send_button(uint32_t time, uint32_t button, uint32_t state) - return; - } - -+ if (state == WL_POINTER_BUTTON_STATE_PRESSED) { -+ layer_shell_pressed(pointer->focus.view); -+ } - serial = wl_display_next_serial(swc.display); - wl_resource_for_each(resource, &pointer->focus.active) - wl_pointer_send_button(resource, serial, time, button, state); -@@ -431,6 +435,9 @@ client_handle_button(struct pointer_handler *handler, uint32_t time, - if (wl_list_empty(&pointer->focus.active)) { - return false; - } -+ if (state == WL_POINTER_BUTTON_STATE_PRESSED) { -+ layer_shell_pressed(pointer->focus.view); -+ } - - wl_resource_for_each(resource, &pointer->focus.active) - wl_pointer_send_button(resource, button->press.serial, time, -diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c -index 8f54b95..25fd17f 100644 ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -646,6 +646,15 @@ constrained_position(struct xdg_positioner *positioner, int32_t px, int32_t py) - r.x = g->x - px; - } - } -+ /* cut what still does not fit and tell the client */ -+ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y && -+ py + r.y + r.height > g->y + (int32_t)g->height) { -+ r.height = g->y + (int32_t)g->height - py - r.y; -+ } -+ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X && -+ px + r.x + r.width > g->x + (int32_t)g->width) { -+ r.width = g->x + (int32_t)g->width - px - r.x; -+ } - return r; - } - --- -2.56.1 - diff --git a/patches/popupsOverParent.patch b/patches/popupsOverParent.patch @@ -1,45 +0,0 @@ -From 65720cb70d5c87317b0ae532050904dae5a0f121 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Tue, 17 Nov 2026 14:56:02 -0300 -Subject: [PATCH 10/19] popups stack above their parent - ---- - libswc/layer_shell.c | 3 ++- - libswc/xdg_shell.c | 6 ++++++ - 2 files changed, 8 insertions(+), 1 deletion(-) - -diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c -index 209cbb7..3245fc2 100644 ---- a/libswc/layer_shell.c -+++ b/libswc/layer_shell.c -@@ -391,8 +391,9 @@ handle_attach(struct view_handler *handler) - - if (mapped) { - update_position(surface); -- restack_layer(surface); -+ /* raise on map only, or it covers its own popups */ - if (!surface->mapped) { -+ restack_layer(surface); - compositor_view_show(surface->view); - /* on demand waits for a press, see layer_shell_pressed */ - if (surface->current.keyboard_interactivity == -diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c -index 25fd17f..b3c160f 100644 ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -671,6 +671,12 @@ xdg_popup_place(struct xdg_popup *popup, struct compositor_view *parent_view) - rect = constrained_position(&popup->positioner, parent_view->base.geometry.x, - parent_view->base.geometry.y); - compositor_view_set_parent(popup->view, parent_view); -+ /* above the parent, in a layer of normal at least */ -+ popup->view->always_top = parent_view->always_top; -+ compositor_view_set_stack_layer(popup->view, -+ parent_view->stack_layer > STACK_LAYER_NORMAL ? parent_view->stack_layer -+ : STACK_LAYER_NORMAL, -+ true); - view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, - parent_view->base.geometry.y + rect.y); - xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, --- -2.56.1 - diff --git a/patches/seatFeedInput.patch b/patches/seatFeedInput.patch @@ -1,113 +0,0 @@ -From 7c3427b3c127a4b6e85ef91cf7b918e440ccb58b Mon Sep 17 00:00:00 2001 -From: xplshn <noreply@localhost> -Date: Mon, 16 Nov 2026 10:48:20 -0300 -Subject: [PATCH 15/19] add swc_key and swc_button - -For scripts, tests and on-screen keyboards. ---- - libswc/swc.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ - libswc/swc.h | 19 +++++++++++++++++ - 2 files changed, 77 insertions(+) - -diff --git a/libswc/swc.c b/libswc/swc.c -index 62e3e88..898253c 100644 ---- a/libswc/swc.c -+++ b/libswc/swc.c -@@ -170,6 +170,64 @@ swc_cursor_set_position(int32_t x, int32_t y) - return true; - } - -+EXPORT bool -+swc_button(uint32_t button, uint32_t state) -+{ -+ if (!swc.seat || !swc.seat->pointer) { -+ return false; -+ } -+ -+ pointer_handle_button(swc.seat->pointer, get_time(), button, state); -+ pointer_handle_frame(swc.seat->pointer); -+ -+ return true; -+} -+ -+EXPORT bool -+swc_key(uint32_t key, uint32_t state) -+{ -+ if (!swc.seat || !swc.seat->keyboard) { -+ return false; -+ } -+ -+ keyboard_handle_key(swc.seat->keyboard, get_time(), key, state); -+ -+ return true; -+} -+ -+EXPORT bool -+swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level) -+{ -+ struct xkb_keymap *map; -+ xkb_keycode_t code, max; -+ const xkb_keysym_t *syms; -+ uint32_t l; -+ int i, n; -+ -+ if (!swc.seat || !swc.seat->keyboard) { -+ return false; -+ } -+ -+ map = swc.seat->keyboard->xkb.keymap.map; -+ max = xkb_keymap_max_keycode(map); -+ /* bare keys first */ -+ for (l = 0; l < 2; l++) { -+ for (code = xkb_keymap_min_keycode(map); code <= max; code++) { -+ n = xkb_keymap_key_get_syms_by_level(map, code, 0, l, &syms); -+ for (i = 0; i < n; i++) { -+ if (syms[i] != keysym) { -+ continue; -+ } -+ *key = code - 8; -+ *level = l; -+ return true; -+ } -+ } -+ } -+ -+ return false; -+} -+ - static bool - backend_initialize(void) - { -diff --git a/libswc/swc.h b/libswc/swc.h -index 88155ac..6477b27 100644 ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -58,6 +58,25 @@ swc_cursor_position(int32_t *x, int32_t *y); - bool - swc_cursor_set_position(int32_t x, int32_t y); - -+/** -+ * Feed a button event to the seat, through the bindings. -+ * button is a linux input code. -+ */ -+bool -+swc_button(uint32_t button, uint32_t state); -+ -+/** -+ * Feed a key event to the seat. key is an evdev code. -+ */ -+bool -+swc_key(uint32_t key, uint32_t state); -+ -+/** -+ * Find the key and level (0 bare, 1 shift) that produce keysym. -+ */ -+bool -+swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level); -+ - /** - * Send a pointer button event to the currently focused client. - * --- -2.56.1 - diff --git a/patches/selectionHandler.patch b/patches/selectionHandler.patch @@ -1,557 +0,0 @@ -diff --git a/libswc/data.c b/libswc/data.c -index db91576..d497544 100644 ---- a/libswc/data.c -+++ b/libswc/data.c -@@ -31,7 +31,8 @@ - - struct data { - struct wl_array mime_types; -- struct wl_resource *source; -+ struct wl_resource *source; /* NULL for a source of the compositor's own */ -+ struct data_host host; - struct wl_list offers; - - /* drag state: source actions, target actions, agreed action */ -@@ -74,6 +75,9 @@ offer_accept(struct wl_client *client, struct wl_resource *offer, - return; - } - -+ if (!data->source) { -+ return; -+ } - data->accepted = mime_type != NULL; - wl_data_source_send_target(data->source, mime_type); - } -@@ -89,7 +93,7 @@ offer_receive(struct wl_client *client, struct wl_resource *offer, - return; - } - -- wl_data_source_send_send(data->source, mime_type, fd); -+ data_request(data, mime_type, fd); - close(fd); - } - -@@ -100,7 +104,7 @@ offer_finish(struct wl_client *client, struct wl_resource *offer) - struct data *data = wl_resource_get_user_data(offer); - - (void)client; -- if (data && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { -+ if (data && data->source && wl_resource_get_version(data->source) >= WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) { - wl_data_source_send_dnd_finished(data->source); - } - } -@@ -126,7 +130,7 @@ offer_set_actions(struct wl_client *client, struct wl_resource *offer, uint32_t - struct data *data = wl_resource_get_user_data(offer); - - (void)client; -- if (!data) { -+ if (!data || !data->source) { - return; - } - data->offer_actions = dnd_actions; -@@ -181,33 +185,32 @@ static const struct wl_data_source_interface data_source_impl = { - .set_actions = source_set_actions, - }; - -+/* after the source is gone each data_offer made from it has a pointer to a -+ * free'd struct. the resources cannot be destroyed, a client that correctly -+ * calls data_offer.destroy would crash it, so the user data of each offer is -+ * cleared, and a client that still calls accept or receive does nothing */ - static void --data_destroy(struct wl_resource *source) -+release(struct data *data) - { -- struct data *data = wl_resource_get_user_data(source); - struct wl_resource *offer; - char **mime_type; - - wl_array_for_each(mime_type, &data->mime_types) free(*mime_type); - wl_array_release(&data->mime_types); -- -- /* After this data_source is destroyed, each of the data_offer objects -- * associated with the data_source has a pointer to a free'd struct. We -- * can't destroy the resources because this results in a segfault on the -- * client when it correctly tries to call data_source.destroy. However, a -- * misbehaving client could still attempt to call accept or receive on the -- * data_offer, which would crash the server. -- * -- * So, we clear the user data on each of the offers to protect us. */ - wl_resource_for_each(offer, &data->offers) - { - wl_resource_set_user_data(offer, NULL); - wl_resource_set_destructor(offer, NULL); - } -- - free(data); - } - -+static void -+data_destroy(struct wl_resource *source) -+{ -+ release(wl_resource_get_user_data(source)); -+} -+ - struct wl_resource * - data_source_new(struct wl_client *client, uint32_t version, uint32_t id) - { -@@ -220,6 +223,7 @@ data_source_new(struct wl_client *client, uint32_t version, uint32_t id) - wl_array_init(&data->mime_types); - wl_list_init(&data->offers); - data->source_actions = data->action = 0; -+ memset(&data->host, 0, sizeof(data->host)); - data->accepted = false; - data->drag_offer = NULL; - data->offer_actions = data->preferred = data->chosen = 0; -@@ -241,10 +245,8 @@ error0: - } - - struct wl_resource * --data_offer_new(struct wl_client *client, struct wl_resource *source, -- uint32_t version) -+data_offer_for(struct wl_client *client, struct data *data, uint32_t version) - { -- struct data *data = wl_resource_get_user_data(source); - struct wl_resource *offer; - - offer = wl_resource_create(client, &wl_data_offer_interface, version, 0); -@@ -258,16 +260,102 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, - return offer; - } - -+struct wl_resource * -+data_offer_new(struct wl_client *client, struct wl_resource *source, -+ uint32_t version) -+{ -+ return data_offer_for(client, wl_resource_get_user_data(source), version); -+} -+ - void --data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) -+data_send_types(struct data *data, struct wl_resource *offer) - { -- struct data *data = wl_resource_get_user_data(source); - char **mime_type; - - wl_array_for_each(mime_type, &data->mime_types) - wl_data_offer_send_offer(offer, *mime_type); - } - -+void -+data_send_mime_types(struct wl_resource *source, struct wl_resource *offer) -+{ -+ data_send_types(wl_resource_get_user_data(source), offer); -+} -+ -+struct data * -+data_of(struct wl_resource *source) -+{ -+ return wl_resource_get_user_data(source); -+} -+ -+unsigned -+data_type_count(const struct data *data) -+{ -+ return data->mime_types.size / sizeof(char *); -+} -+ -+const char * -+data_type_at(const struct data *data, unsigned index) -+{ -+ return ((char **)data->mime_types.data)[index]; -+} -+ -+void -+data_request(struct data *data, const char *mime_type, int fd) -+{ -+ if (data->source) { -+ wl_data_source_send_send(data->source, mime_type, fd); -+ } else if (data->host.send) { -+ data->host.send(data->host.data, mime_type, fd); -+ } -+} -+ -+void -+data_cancel(struct data *data) -+{ -+ if (data->source) { -+ wl_data_source_send_cancelled(data->source); -+ } else if (data->host.cancelled) { -+ data->host.cancelled(data->host.data); -+ } -+} -+ -+struct data * -+data_host_new(const char *const *types, unsigned count, -+ const struct data_host *host, void *host_data) -+{ -+ struct data *data = calloc(1, sizeof(*data)); -+ char **dst; -+ unsigned i; -+ -+ if (!data) { -+ return NULL; -+ } -+ wl_array_init(&data->mime_types); -+ wl_list_init(&data->offers); -+ data->host = *host; -+ data->host.data = host_data; -+ for (i = 0; i < count; i++) { -+ dst = wl_array_add(&data->mime_types, sizeof(*dst)); -+ if (!dst) { -+ release(data); -+ return NULL; -+ } -+ if (!(*dst = strdup(types[i]))) { -+ data->mime_types.size -= sizeof(*dst); -+ release(data); -+ return NULL; -+ } -+ } -+ return data; -+} -+ -+void -+data_host_free(struct data *data) -+{ -+ release(data); -+} -+ - void - data_drag_enter(struct wl_resource *source, struct wl_resource *offer) - { -diff --git a/libswc/data.h b/libswc/data.h -index e271dd5..d1c762f 100644 ---- a/libswc/data.h -+++ b/libswc/data.h -@@ -37,6 +37,39 @@ data_offer_new(struct wl_client *client, struct wl_resource *source, - void - data_send_mime_types(struct wl_resource *source, struct wl_resource *offer); - -+struct data; -+ -+/* a source of the compositor's own, for a selection it keeps itself: it -+ * offers types and answers each ask on its own */ -+struct data_host { -+ void (*send)(void *data, const char *mime_type, int fd); -+ void (*cancelled)(void *data); -+ void *data; -+}; -+ -+struct data * -+data_host_new(const char *const *types, unsigned count, -+ const struct data_host *host, void *data); -+void -+data_host_free(struct data *data); -+struct data * -+data_of(struct wl_resource *source); -+/* offers of a selection, host or client */ -+struct wl_resource * -+data_offer_for(struct wl_client *client, struct data *data, uint32_t version); -+void -+data_send_types(struct data *data, struct wl_resource *offer); -+unsigned -+data_type_count(const struct data *data); -+const char * -+data_type_at(const struct data *data, unsigned index); -+/* asks the source for a type, to be written to fd (the caller closes its copy) */ -+void -+data_request(struct data *data, const char *mime_type, int fd); -+/* the selection went to someone else */ -+void -+data_cancel(struct data *data); -+ - /* a drag offer for source entered a client, nothing accepted yet */ - void - data_drag_enter(struct wl_resource *source, struct wl_resource *offer); -diff --git a/libswc/data_device.c b/libswc/data_device.c -index e24cbf6..3255fcc 100644 ---- a/libswc/data_device.c -+++ b/libswc/data_device.c -@@ -334,6 +334,11 @@ set_selection(struct wl_client *client, struct wl_resource *resource, - wl_data_source_send_cancelled(data_device->selection); - wl_list_remove(&data_device->selection_destroy_listener.link); - } -+ if (data_device->host) { -+ data_cancel(data_device->host); -+ data_host_free(data_device->host); -+ data_device->host = NULL; -+ } - - data_device->selection = data_source; - -@@ -373,6 +378,7 @@ data_device_create(void) - return NULL; - } - data_device->selection = NULL; -+ data_device->host = NULL; - data_device->selection_destroy_listener.notify = &handle_selection_destroy; - wl_signal_init(&data_device->event_signal); - wl_list_init(&data_device->resources); -@@ -387,6 +393,9 @@ data_device_destroy(struct data_device *data_device) - - wl_list_for_each_safe(resource, tmp, &data_device->resources, link) - wl_resource_destroy(resource); -+ if (data_device->host) { -+ data_host_free(data_device->host); -+ } - free(data_device); - } - -@@ -408,18 +417,41 @@ data_device_bind(struct data_device *data_device, struct wl_client *client, - return resource; - } - -+struct data * -+data_device_client_selection(struct data_device *data_device) -+{ -+ return data_device->selection ? data_of(data_device->selection) : NULL; -+} -+ -+void -+data_device_set_host(struct data_device *data_device, struct data *host) -+{ -+ if (data_device->selection) { -+ wl_data_source_send_cancelled(data_device->selection); -+ wl_list_remove(&data_device->selection_destroy_listener.link); -+ data_device->selection = NULL; -+ } -+ if (data_device->host) { -+ data_cancel(data_device->host); -+ data_host_free(data_device->host); -+ } -+ data_device->host = host; -+ send_event(&data_device->event_signal, DATA_DEVICE_EVENT_SELECTION_CHANGED, -+ host); -+} -+ - static struct wl_resource * - new_offer(struct wl_resource *resource, struct wl_client *client, -- struct wl_resource *source) -+ struct data *source) - { - struct wl_resource *offer; - -- offer = data_offer_new(client, source, wl_resource_get_version(resource)); -+ offer = data_offer_for(client, source, wl_resource_get_version(resource)); - if (!offer) { - return NULL; - } - wl_data_device_send_data_offer(resource, offer); -- data_send_mime_types(source, offer); -+ data_send_types(source, offer); - - return offer; - } -@@ -440,8 +472,10 @@ data_device_offer_selection(struct data_device *data_device, - } - - /* If we have a selection, create a new offer for the client. */ -- if (data_device->selection) { -- offer = new_offer(resource, client, data_device->selection); -+ if (data_device->host) { -+ offer = new_offer(resource, client, data_device->host); -+ } else if (data_device->selection) { -+ offer = new_offer(resource, client, data_of(data_device->selection)); - } - - wl_data_device_send_selection(resource, offer); -diff --git a/libswc/data_device.h b/libswc/data_device.h -index 6712190..f7242cc 100644 ---- a/libswc/data_device.h -+++ b/libswc/data_device.h -@@ -29,9 +29,13 @@ - - enum { DATA_DEVICE_EVENT_SELECTION_CHANGED }; - -+struct data; -+ - struct data_device { - /* The data source corresponding to the current selection. */ - struct wl_resource *selection; -+ /* the selection when the compositor keeps it itself, never with selection */ -+ struct data *host; - struct wl_listener selection_destroy_listener; - - struct wl_signal event_signal; -@@ -45,6 +49,14 @@ data_device_destroy(struct data_device *data_device); - struct wl_resource * - data_device_bind(struct data_device *data_device, struct wl_client *client, - uint32_t version, uint32_t id); -+/* the selection a client set, as data, NULL for none */ -+struct data * -+data_device_client_selection(struct data_device *data_device); -+/* takes the selection for the compositor, host owned by the device now. what -+ * held it is told it is cancelled. a event with data set says it came from -+ * the compositor and not from a client */ -+void -+data_device_set_host(struct data_device *data_device, struct data *host); - void - data_device_offer_selection(struct data_device *data_device, - struct wl_client *client); -diff --git a/libswc/meson.build b/libswc/meson.build -index b9cdde1..f506b5c 100644 ---- a/libswc/meson.build -+++ b/libswc/meson.build -@@ -21,6 +21,7 @@ libswc_src = files( - 'region.c', - 'screen.c', - 'select.c', -+ 'selection.c', - 'snap.c', - 'shell.c', - 'shell_surface.c', -diff --git a/libswc/selection.c b/libswc/selection.c -new file mode 100644 -index 0000000..b6845e0 ---- /dev/null -+++ b/libswc/selection.c -@@ -0,0 +1,100 @@ -+/* swc: selection.c -+ * -+ * the clipboard for a compositor that keeps it itself, see swc.h -+ * -+ * Permission is hereby granted, free of charge, to any person obtaining a copy -+ * of this software and associated documentation files (the "Software"), to deal -+ * in the Software without restriction, including without limitation the rights -+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -+ * copies of the Software, and to permit persons to whom the Software is -+ * furnished to do so, subject to the following conditions: -+ * -+ * The above copyright notice and this permission notice shall be included in -+ * all copies or substantial portions of the Software. -+ */ -+ -+#include "swc.h" -+#include "data.h" -+#include "data_device.h" -+#include "event.h" -+#include "internal.h" -+#include "seat.h" -+#include "util.h" -+ -+#include <string.h> -+ -+#define MAX_TYPES 64 -+ -+static struct { -+ const struct swc_selection_handler *handler; -+ void *data; -+ struct wl_listener listener; -+ bool hooked; -+} selection; -+ -+static void -+handle_event(struct wl_listener *listener, void *data) -+{ -+ struct event *ev = data; -+ struct data *source; -+ const char *types[MAX_TYPES]; -+ unsigned i, count = 0; -+ -+ if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED || ev->data || !selection.handler) { -+ return; -+ } -+ if ((source = data_device_client_selection(swc.seat->data_device))) { -+ count = data_type_count(source); -+ if (count > MAX_TYPES) { -+ count = MAX_TYPES; -+ } -+ for (i = 0; i < count; i++) { -+ types[i] = data_type_at(source, i); -+ } -+ } -+ selection.handler->changed(selection.data, types, count); -+} -+ -+EXPORT void -+swc_selection_set_handler(const struct swc_selection_handler *handler, void *data) -+{ -+ selection.handler = handler; -+ selection.data = data; -+ if (!selection.hooked && swc.seat) { -+ selection.listener.notify = handle_event; -+ wl_signal_add(&swc.seat->data_device->event_signal, &selection.listener); -+ selection.hooked = true; -+ } -+} -+ -+EXPORT bool -+swc_selection_request(const char *mime_type, int fd) -+{ -+ struct data *source = data_device_client_selection(swc.seat->data_device); -+ unsigned i; -+ -+ if (!source) { -+ return false; -+ } -+ for (i = 0; i < data_type_count(source); i++) { -+ if (strcmp(data_type_at(source, i), mime_type) == 0) { -+ data_request(source, mime_type, fd); -+ return true; -+ } -+ } -+ return false; -+} -+ -+EXPORT bool -+swc_selection_offer(const char *const *mime_types, unsigned count, -+ const struct swc_selection_source *source, void *data) -+{ -+ struct data_host host = { .send = source->send, .cancelled = source->cancelled }; -+ struct data *offered = data_host_new(mime_types, count, &host, data); -+ -+ if (!offered) { -+ return false; -+ } -+ data_device_set_host(swc.seat->data_device, offered); -+ return true; -+} -diff --git a/libswc/swc.h b/libswc/swc.h -index 96ea020..32f6c48 100644 ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -144,6 +144,44 @@ swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888, - void - swc_clear_cursor_image(enum swc_cursor_kind kind); - -+/** -+ * the clipboard, for a compositor that keeps it itself. a client sets the -+ * selection as it does in any compositor, and the handler is told which types -+ * it offers (none when it is gone, the client exited or the selection was -+ * cleared). what the compositor keeps it offers again as a selection of its -+ * own, so the clipboard does not end with the client that copied -+ */ -+struct swc_selection_handler { -+ void (*changed)(void *data, const char *const *mime_types, unsigned count); -+}; -+ -+/* what answers a paste of a selection the compositor offers */ -+struct swc_selection_source { -+ /* write the type to fd, which the caller closes */ -+ void (*send)(void *data, const char *mime_type, int fd); -+ /* another client took the selection */ -+ void (*cancelled)(void *data); -+}; -+ -+void -+swc_selection_set_handler(const struct swc_selection_handler *handler, void *data); -+ -+/** -+ * asks the client that holds the selection for a type, written to fd. -+ * false when no client holds it or it does not offer the type. the caller -+ * closes fd, the client has its own copy -+ */ -+bool -+swc_selection_request(const char *mime_type, int fd); -+ -+/** -+ * takes the selection, offering these types, answered by source. what was -+ * the selection is cancelled -+ */ -+bool -+swc_selection_offer(const char *const *mime_types, unsigned count, -+ const struct swc_selection_source *source, void *data); -+ - /** - * draw [or update] a simple box overlay - * diff --git a/patches/shell.patch b/patches/shell.patch @@ -0,0 +1,1099 @@ +Subject: shells and windows + +diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c +index 194ed01..f167c8f 100644 +--- a/libswc/layer_shell.c ++++ b/libswc/layer_shell.c +@@ -9,6 +9,7 @@ + #include "surface.h" + #include "util.h" + #include "view.h" ++#include "xdg_shell.h" + + #include "wlr-layer-shell-unstable-v1-server-protocol.h" + #include <assert.h> +@@ -35,6 +36,8 @@ struct layer_surface { + uint32_t desired_width, desired_height; + } current, pending; + uint32_t configure_serial; ++ /* screen size the last configure was computed from */ ++ uint32_t screen_width, screen_height; + bool configured; + bool mapped; + }; +@@ -153,22 +156,22 @@ update_usable_geometry(struct layer_surface *surface) + static void + restack_layer(struct layer_surface *surface) + { +- uint32_t stack_layer = STACK_LAYER_NORMAL; ++ int32_t z = STACK_LAYER_NORMAL; + bool always_top = false; + + switch (surface->current.layer) { + case ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND: +- stack_layer = STACK_LAYER_BACKGROUND; ++ z = STACK_LAYER_BACKGROUND; + break; + case ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM: +- stack_layer = STACK_LAYER_BOTTOM; ++ z = STACK_LAYER_BOTTOM; + break; + case ZWLR_LAYER_SHELL_V1_LAYER_TOP: +- stack_layer = STACK_LAYER_TOP; ++ z = STACK_LAYER_TOP; + always_top = true; + break; + case ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY: +- stack_layer = STACK_LAYER_OVERLAY; ++ z = STACK_LAYER_OVERLAY; + always_top = true; + break; + default: +@@ -176,13 +179,25 @@ restack_layer(struct layer_surface *surface) + } + + surface->view->always_top = always_top; +- compositor_view_set_stack_layer(surface->view, stack_layer, true); ++ compositor_view_set_z(surface->view, z, true); ++} ++ ++/* zone 0 surfaces keep clear of reserved zones, others span the screen */ ++static const struct swc_rectangle * ++area_of(const struct layer_surface *surface) ++{ ++ const struct swc_rectangle *usable = &surface->screen->base.usable_geometry; ++ ++ if (surface->current.exclusive_zone == 0 && usable->width && usable->height) { ++ return usable; ++ } ++ return &surface->screen->base.geometry; + } + + static void + update_position(struct layer_surface *surface) + { +- struct swc_rectangle *screen = &surface->screen->base.geometry; ++ const struct swc_rectangle *screen = area_of(surface); + struct swc_rectangle *view = &surface->view->base.geometry; + int32_t x, y; + +@@ -229,16 +244,18 @@ send_configure(struct layer_surface *surface) + width = configure_size(surface->current.desired_width, surface->current.anchor, + ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT, + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT, +- surface->screen->base.geometry.width, ++ area_of(surface)->width, + surface->current.margin.left, + surface->current.margin.right); + height = + configure_size(surface->current.desired_height, surface->current.anchor, + ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, + ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, +- surface->screen->base.geometry.height, ++ area_of(surface)->height, + surface->current.margin.top, + surface->current.margin.bottom); ++ surface->screen_width = surface->screen->base.geometry.width; ++ surface->screen_height = surface->screen->base.geometry.height; + surface->configure_serial = wl_display_next_serial(swc.display); + zwlr_layer_surface_v1_send_configure(surface->resource, + surface->configure_serial, width, +@@ -308,9 +325,12 @@ static void + get_popup(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *popup) + { ++ struct layer_surface *surface = wl_resource_get_user_data(resource); ++ + (void)client; +- (void)resource; +- (void)popup; ++ if (surface->view) { ++ xdg_popup_set_parent(popup, surface->view->surface); ++ } + } + + static void +@@ -375,11 +395,13 @@ handle_attach(struct view_handler *handler) + + if (mapped) { + update_position(surface); +- restack_layer(surface); ++ /* raise on map only, or it covers its own popups */ + if (!surface->mapped) { ++ restack_layer(surface); + compositor_view_show(surface->view); +- if (surface->current.keyboard_interactivity != +- ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE) { ++ /* on demand waits for a press, see layer_shell_pressed */ ++ if (surface->current.keyboard_interactivity == ++ ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE) { + keyboard_set_focus(swc.seat->keyboard, surface->view); + } + } +@@ -432,6 +454,28 @@ handle_surface_commit(struct wl_listener *listener, void *data) + } + } + ++static void modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, ++ pixman_region32_t *usable); ++ ++void ++layer_shell_usable_changed(struct screen *screen) ++{ ++ struct screen_modifier *modifier; ++ struct layer_surface *surface; ++ ++ wl_list_for_each(modifier, &screen->modifiers, link) ++ { ++ if (modifier->modify != modify) { ++ continue; ++ } ++ surface = wl_container_of(modifier, surface, modifier); ++ if (surface->configured && surface->current.exclusive_zone == 0) { ++ send_configure(surface); ++ update_position(surface); ++ } ++ } ++} ++ + static void + modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, + pixman_region32_t *usable) +@@ -443,6 +487,13 @@ modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, + .y2 = geom->y + geom->height}; + int32_t size; + ++ /* every usable-area pass visits us, so notice a size change here */ ++ if (surface->configured && (geom->width != surface->screen_width || ++ geom->height != surface->screen_height)) { ++ send_configure(surface); ++ update_position(surface); ++ } ++ + if (!surface->mapped) { + pixman_region32_reset(usable, &box); + return; +@@ -632,6 +683,28 @@ bind_layer_shell(struct wl_client *client, void *data, uint32_t version, + wl_resource_set_implementation(resource, &layer_shell_impl, NULL, NULL); + } + ++void ++layer_shell_pressed(struct compositor_view *view) ++{ ++ struct view_handler *handler; ++ struct layer_surface *surface; ++ ++ if (!view) { ++ return; ++ } ++ wl_list_for_each(handler, &view->base.handlers, link) { ++ if (handler->impl != &view_handler_impl) { ++ continue; ++ } ++ surface = wl_container_of(handler, surface, view_handler); ++ if (surface->current.keyboard_interactivity == ++ ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND) { ++ keyboard_set_focus(swc.seat->keyboard, view); ++ } ++ return; ++ } ++} ++ + struct wl_global * + layer_shell_create(struct wl_display *display) + { +diff --git a/libswc/layer_shell.h b/libswc/layer_shell.h +index 76e5432..138db1a 100644 +--- a/libswc/layer_shell.h ++++ b/libswc/layer_shell.h +@@ -4,7 +4,18 @@ + struct wl_display; + struct wl_global; + ++struct screen; ++ + struct wl_global * + layer_shell_create(struct wl_display *display); + ++/* usable area changed, lay out the surfaces that depend on it */ ++void ++layer_shell_usable_changed(struct screen *screen); ++ ++/* a press on view gives on-demand layer surfaces the keyboard */ ++struct compositor_view; ++void ++layer_shell_pressed(struct compositor_view *view); ++ + #endif +diff --git a/libswc/panel.c b/libswc/panel.c +index 5434d38..09add87 100644 +--- a/libswc/panel.c ++++ b/libswc/panel.c +@@ -47,9 +47,26 @@ struct panel { + uint32_t edge; + uint32_t offset, strut_size; + uint32_t y_offset; ++ /* length last sent */ ++ uint32_t length; + bool docked; + }; + ++static uint32_t ++edge_length(struct screen *screen, uint32_t edge) ++{ ++ switch (edge) { ++ case SWC_PANEL_EDGE_TOP: ++ case SWC_PANEL_EDGE_BOTTOM: ++ return screen->base.geometry.width; ++ case SWC_PANEL_EDGE_LEFT: ++ case SWC_PANEL_EDGE_RIGHT: ++ return screen->base.geometry.height; ++ default: ++ return 0; ++ } ++} ++ + static void + update_position(struct panel *panel) + { +@@ -95,16 +112,7 @@ dock(struct wl_client *client, struct wl_resource *resource, uint32_t edge, + screen = wl_container_of(swc.screens.next, screen, link); + } + +- switch (edge) { +- case SWC_PANEL_EDGE_TOP: +- case SWC_PANEL_EDGE_BOTTOM: +- length = screen->base.geometry.width; +- break; +- case SWC_PANEL_EDGE_LEFT: +- case SWC_PANEL_EDGE_RIGHT: +- length = screen->base.geometry.height; +- break; +- default: ++ if (!(length = edge_length(screen, edge))) { + return; + } + +@@ -115,6 +123,7 @@ dock(struct wl_client *client, struct wl_resource *resource, uint32_t edge, + + panel->screen = screen; + panel->edge = edge; ++ panel->length = length; + panel->docked = true; + + update_position(panel); +@@ -196,6 +205,13 @@ modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, + + assert(panel->docked); + ++ /* every usable-area pass visits us, so notice a size change here */ ++ if (edge_length(panel->screen, panel->edge) != panel->length) { ++ panel->length = edge_length(panel->screen, panel->edge); ++ update_position(panel); ++ swc_panel_send_docked(panel->resource, panel->length); ++ } ++ + DEBUG("Original geometry { x1: %d, y1: %d, x2: %d, y2: %d }\n", box.x1, + box.y1, box.x2, box.y2); + +diff --git a/libswc/window.c b/libswc/window.c +index 19a9897..0efd250 100644 +--- a/libswc/window.c ++++ b/libswc/window.c +@@ -95,6 +95,14 @@ clamp_window_size(const struct window *window, uint32_t *width, + window->base.max_height); + } + ++void ++window_pointer_entered(struct window *window) ++{ ++ if (window->handler->entered) { ++ window->handler->entered(window->handler_data); ++ } ++} ++ + static void + handle_window_enter(struct wl_listener *listener, void *data) + { +@@ -110,9 +118,7 @@ handle_window_enter(struct wl_listener *listener, void *data) + return; + } + +- if (window->handler->entered) { +- window->handler->entered(window->handler_data); +- } ++ window_pointer_entered(window); + } + + struct wl_listener window_enter_listener = { +@@ -188,6 +194,25 @@ swc_window_set_handler(struct swc_window *base, + window->handler_data = data; + } + ++EXPORT void ++swc_window_set_backdrop(struct swc_window *base, uint32_t margin, ++ void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data) ++{ ++ struct window *window = INTERNAL(base); ++ ++ if (window->view) ++ compositor_view_set_backdrop(window->view, margin, hook, data); ++} ++ ++EXPORT void ++swc_window_set_z(struct swc_window *base, int32_t z) ++{ ++ struct window *window = INTERNAL(base); ++ ++ if (window->view) ++ compositor_view_set_z(window->view, z, true); ++} ++ + EXPORT void + swc_window_close(struct swc_window *base) + { +@@ -234,6 +259,32 @@ swc_window_focus(struct swc_window *base) + keyboard_set_focus(swc.seat->keyboard, new); + } + ++EXPORT void ++swc_window_set_parent(struct swc_window *base, struct swc_window *parent) ++{ ++ window_set_parent(INTERNAL(base), parent ? INTERNAL(parent) : NULL); ++} ++ ++EXPORT void ++swc_window_set_attached(struct swc_window *base, bool attached) ++{ ++ struct window *window = INTERNAL(base); ++ ++ if (window->view) { ++ window->view->attached = attached; ++ } ++} ++ ++EXPORT void ++swc_window_raise(struct swc_window *base) ++{ ++ struct window *window = INTERNAL(base); ++ ++ if (window->view) { ++ raise_window(window->view); ++ } ++} ++ + EXPORT void + swc_window_set_stacked(struct swc_window *base) + { +@@ -367,6 +418,14 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, + inner_border_width); + } + ++EXPORT void ++swc_window_set_border_placement(struct swc_window *window, ++ enum swc_border_placement placement) ++{ ++ compositor_view_set_border_frame(INTERNAL(window)->view, ++ placement == SWC_BORDER_FRAME); ++} ++ + EXPORT void + swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor) + { +@@ -497,6 +556,10 @@ handle_resize(struct view_handler *handler, uint32_t old_width, + + view_move(&window->view->base, x, y); + } ++ ++ if (window->handler->size_changed) { ++ window->handler->size_changed(window->handler_data); ++ } + } + + static const struct view_handler_impl view_handler_impl = { +@@ -616,15 +679,32 @@ window_set_app_id(struct window *window, const char *app_id) + } + } + ++void ++window_set_modal(struct window *window, bool modal) ++{ ++ if (window->base.modal == modal) { ++ return; ++ } ++ window->base.modal = modal; ++ if (window->handler->modal_changed) { ++ window->handler->modal_changed(window->handler_data); ++ } ++} ++ + void + window_set_parent(struct window *window, struct window *parent) + { +- if (window->base.parent == &parent->base) { ++ struct swc_window *base = parent ? &parent->base : NULL; ++ ++ if (window->base.parent == base || window == parent) { + return; + } + +- compositor_view_set_parent(window->view, parent->view); +- window->base.parent = &parent->base; ++ compositor_view_set_parent(window->view, parent ? parent->view : NULL); ++ window->base.parent = base; ++ if (parent) { ++ raise_window(parent->view); ++ } + + if (window->handler->parent_changed) { + window->handler->parent_changed(window->handler_data); +diff --git a/libswc/window.h b/libswc/window.h +index f1eadde..d987307 100644 +--- a/libswc/window.h ++++ b/libswc/window.h +@@ -95,6 +95,11 @@ struct window_impl { + + extern struct wl_listener window_enter_listener; + ++/* The pointer is over the window as much as over its surface: its handler is ++ * told, as when the surface is entered. */ ++void ++window_pointer_entered(struct window *window); ++ + bool + window_initialize(struct window *window, const struct window_impl *impl, + struct surface *surface); +@@ -111,6 +116,8 @@ window_set_app_id(struct window *window, const char *app_id); + void + window_set_parent(struct window *window, struct window *parent); + void ++window_set_modal(struct window *window, bool modal); ++void + window_begin_move(struct window *window, struct button *button); + void + window_begin_resize(struct window *window, uint32_t edges, +diff --git a/libswc/xdg_dialog.c b/libswc/xdg_dialog.c +new file mode 100644 +index 0000000..25233b9 +--- /dev/null ++++ b/libswc/xdg_dialog.c +@@ -0,0 +1,117 @@ ++/* xdg-dialog-v1 */ ++ ++#include "xdg_dialog.h" ++#include "util.h" ++#include "window.h" ++#include "xdg_shell.h" ++ ++#include <stdlib.h> ++#include <wayland-server.h> ++#include "xdg-dialog-v1-server-protocol.h" ++ ++struct dialog { ++ struct wl_resource *resource; ++ struct wl_resource *toplevel; ++ struct wl_listener toplevel_destroy; ++}; ++ ++static void ++set(struct wl_resource *resource, bool modal) ++{ ++ struct dialog *dialog = wl_resource_get_user_data(resource); ++ struct window *window; ++ ++ if (dialog->toplevel && (window = xdg_toplevel_window(dialog->toplevel))) { ++ window_set_modal(window, modal); ++ } ++} ++ ++static void ++set_modal(struct wl_client *client, struct wl_resource *resource) ++{ ++ (void)client; ++ set(resource, true); ++} ++ ++static void ++unset_modal(struct wl_client *client, struct wl_resource *resource) ++{ ++ (void)client; ++ set(resource, false); ++} ++ ++static const struct xdg_dialog_v1_interface dialog_impl = { ++ .destroy = destroy_resource, ++ .set_modal = set_modal, ++ .unset_modal = unset_modal, ++}; ++ ++static void ++handle_toplevel_destroy(struct wl_listener *listener, void *data) ++{ ++ struct dialog *dialog = wl_container_of(listener, dialog, toplevel_destroy); ++ ++ (void)data; ++ dialog->toplevel = NULL; ++} ++ ++static void ++destroy_dialog(struct wl_resource *resource) ++{ ++ struct dialog *dialog = wl_resource_get_user_data(resource); ++ ++ /* no longer a dialog: stop blocking */ ++ set(resource, false); ++ if (dialog->toplevel) { ++ wl_list_remove(&dialog->toplevel_destroy.link); ++ } ++ free(dialog); ++} ++ ++static void ++get_xdg_dialog(struct wl_client *client, struct wl_resource *resource, uint32_t id, ++ struct wl_resource *toplevel) ++{ ++ struct dialog *dialog = calloc(1, sizeof *dialog); ++ ++ if (!dialog) { ++ wl_resource_post_no_memory(resource); ++ return; ++ } ++ dialog->resource = wl_resource_create(client, &xdg_dialog_v1_interface, ++ wl_resource_get_version(resource), id); ++ if (!dialog->resource) { ++ free(dialog); ++ wl_resource_post_no_memory(resource); ++ return; ++ } ++ dialog->toplevel = toplevel; ++ dialog->toplevel_destroy.notify = handle_toplevel_destroy; ++ wl_resource_add_destroy_listener(toplevel, &dialog->toplevel_destroy); ++ wl_resource_set_implementation(dialog->resource, &dialog_impl, dialog, destroy_dialog); ++} ++ ++static const struct xdg_wm_dialog_v1_interface manager_impl = { ++ .destroy = destroy_resource, ++ .get_xdg_dialog = get_xdg_dialog, ++}; ++ ++static void ++bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) ++{ ++ struct wl_resource *resource; ++ ++ (void)data; ++ resource = wl_resource_create(client, &xdg_wm_dialog_v1_interface, version, id); ++ if (!resource) { ++ wl_client_post_no_memory(client); ++ return; ++ } ++ wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); ++} ++ ++struct wl_global * ++xdg_dialog_manager_create(struct wl_display *display) ++{ ++ return wl_global_create(display, &xdg_wm_dialog_v1_interface, 1, NULL, &bind_manager); ++} +diff --git a/libswc/xdg_dialog.h b/libswc/xdg_dialog.h +new file mode 100644 +index 0000000..134b329 +--- /dev/null ++++ b/libswc/xdg_dialog.h +@@ -0,0 +1,11 @@ ++/* xdg-dialog-v1 */ ++ ++#ifndef SWC_XDG_DIALOG_H ++#define SWC_XDG_DIALOG_H ++ ++struct wl_display; ++ ++struct wl_global * ++xdg_dialog_manager_create(struct wl_display *display); ++ ++#endif +diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c +index c5d6986..1e327ad 100644 +--- a/libswc/xdg_shell.c ++++ b/libswc/xdg_shell.c +@@ -24,6 +24,8 @@ + #include "xdg_shell.h" + #include "compositor.h" + #include "internal.h" ++#include "pointer.h" ++#include "screen.h" + #include "seat.h" + #include "surface.h" + #include "util.h" +@@ -63,8 +65,76 @@ struct xdg_popup { + struct xdg_surface *xdg_surface; + struct xdg_positioner positioner; + struct compositor_view *view; ++ struct wl_list grab_link; /* in grabs while it holds a grab */ ++ bool grabbing; + }; + ++/* popups holding a grab, newest last. a press elsewhere dismisses them newest first */ ++static struct wl_list grabs = {&grabs, &grabs}; ++ ++static bool popup_grab_button(struct pointer_handler *handler, uint32_t time, ++ struct button *button, uint32_t state); ++ ++static struct pointer_handler popup_grab_handler = { ++ .button = popup_grab_button, ++}; ++ ++static void ++grab_end(struct xdg_popup *popup) ++{ ++ if (!popup->grabbing) { ++ return; ++ } ++ wl_list_remove(&popup->grab_link); ++ popup->grabbing = false; ++ if (wl_list_empty(&grabs)) { ++ wl_list_remove(&popup_grab_handler.link); ++ wl_list_init(&popup_grab_handler.link); ++ } ++} ++ ++static bool ++popup_grab_button(struct pointer_handler *handler, uint32_t time, ++ struct button *button, uint32_t state) ++{ ++ struct xdg_popup *top, *popup, *tmp; ++ struct wl_client *focus = swc.seat->pointer->focus.client; ++ ++ if (state != WL_POINTER_BUTTON_STATE_PRESSED || wl_list_empty(&grabs)) { ++ return false; ++ } ++ top = wl_container_of(grabs.prev, top, grab_link); ++ if (focus && focus == wl_resource_get_client(top->resource)) { ++ return false; ++ } ++ wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { ++ xdg_popup_send_popup_done(popup->resource); ++ grab_end(popup); ++ } ++ return true; ++} ++ ++/* Keyboard focus went to client: a menu is a grab, and a grab ends when focus ++ * leaves what grabbed it, as menus do on every desktop. A press elsewhere ++ * is the other way it ends (popup_grab_button). */ ++void ++xdg_popups_focus_changed(struct wl_client *client) ++{ ++ struct xdg_popup *top, *popup, *tmp; ++ ++ if (wl_list_empty(&grabs)) { ++ return; ++ } ++ top = wl_container_of(grabs.prev, top, grab_link); ++ if (client && client == wl_resource_get_client(top->resource)) { ++ return; ++ } ++ wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { ++ xdg_popup_send_popup_done(popup->resource); ++ grab_end(popup); ++ } ++} ++ + /* xdg_positioner */ + static void + destroy_positioner(struct wl_resource *resource) +@@ -269,23 +339,29 @@ remove_state(struct xdg_toplevel *toplevel, uint32_t state) + return false; + } + +-static uint32_t ++static void + send_configure(struct xdg_toplevel *toplevel, int32_t width, int32_t height) + { + uint32_t serial = wl_display_next_serial(swc.display); + +- if (width < 0) { +- width = toplevel->window.configure.width; +- } +- if (height < 0) { +- height = toplevel->window.configure.height; +- } +- + xdg_toplevel_send_configure(toplevel->resource, width, height, + &toplevel->states); + xdg_surface_send_configure(toplevel->xdg_surface->resource, serial); ++ toplevel->xdg_surface->configure_serial = serial; ++} + +- return serial; ++static void ++send_state(struct xdg_toplevel *toplevel) ++{ ++ struct window *window = &toplevel->window; ++ struct swc_rectangle *geometry = &window->view->base.geometry; ++ ++ /* a window not yet mapped has no geometry: it keeps the size it was ++ * last asked for rather than being told to choose its own */ ++ if (window->configure.pending || !geometry->width || !geometry->height) ++ send_configure(toplevel, window->configure.width, window->configure.height); ++ else ++ send_configure(toplevel, geometry->width, geometry->height); + } + + static void +@@ -294,33 +370,25 @@ configure(struct window *window, uint32_t width, uint32_t height) + struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); + + window->configure.acknowledged = false; +- toplevel->xdg_surface->configure_serial = +- send_configure(toplevel, width, height); ++ send_configure(toplevel, width, height); + } + + static void + focus(struct window *window) + { + struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); +- uint32_t width = window->view->base.geometry.width; +- uint32_t height = window->view->base.geometry.height; + + add_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); +- /* dont send 0x0 on focus change */ +- send_configure(toplevel, width ? (int32_t)width : -1, +- height ? (int32_t)height : -1); ++ send_state(toplevel); + } + + static void + unfocus(struct window *window) + { + struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); +- uint32_t width = window->view->base.geometry.width; +- uint32_t height = window->view->base.geometry.height; + + remove_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); +- send_configure(toplevel, width ? (int32_t)width : -1, +- height ? (int32_t)height : -1); ++ send_state(toplevel); + } + + static void +@@ -354,7 +422,7 @@ set_mode(struct window *window, unsigned mode) + break; + } + +- send_configure(toplevel, -1, -1); ++ send_state(toplevel); + } + + static const struct window_impl toplevel_window_impl = { +@@ -431,12 +499,23 @@ static void + set_max_size(struct wl_client *client, struct wl_resource *resource, + int32_t width, int32_t height) + { ++ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); ++ ++ (void)client; ++ /* 0 is no limit */ ++ toplevel->window.base.max_width = width > 0 ? (uint32_t)width : 0; ++ toplevel->window.base.max_height = height > 0 ? (uint32_t)height : 0; + } + + static void + set_min_size(struct wl_client *client, struct wl_resource *resource, + int32_t width, int32_t height) + { ++ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); ++ ++ (void)client; ++ toplevel->window.base.min_width = width > 0 ? (uint32_t)width : 1; ++ toplevel->window.base.min_height = height > 0 ? (uint32_t)height : 1; + } + + static void +@@ -453,11 +532,21 @@ static void + set_fullscreen(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *output) + { ++ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); ++ struct window *window = &toplevel->window; ++ ++ if (window->handler->fullscreen) ++ window->handler->fullscreen(window->handler_data, true); + } + + static void + unset_fullscreen(struct wl_client *client, struct wl_resource *resource) + { ++ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); ++ struct window *window = &toplevel->window; ++ ++ if (window->handler->fullscreen) ++ window->handler->fullscreen(window->handler_data, false); + } + + static void +@@ -482,6 +571,14 @@ static const struct xdg_toplevel_interface toplevel_impl = { + .set_minimized = set_minimized, + }; + ++struct window * ++xdg_toplevel_window(struct wl_resource *resource) ++{ ++ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); ++ ++ return toplevel ? &toplevel->window : NULL; ++} ++ + static struct xdg_toplevel * + xdg_toplevel_new(struct wl_client *client, uint32_t version, uint32_t id, + struct xdg_surface *xdg_surface) +@@ -526,6 +623,7 @@ destroy_popup(struct wl_resource *resource) + { + struct xdg_popup *popup = wl_resource_get_user_data(resource); + ++ grab_end(popup); + compositor_view_destroy(popup->view); + free(popup); + } +@@ -534,6 +632,17 @@ static void + grab(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *seat, uint32_t serial) + { ++ struct xdg_popup *popup = wl_resource_get_user_data(resource); ++ ++ if (popup->grabbing) { ++ return; ++ } ++ if (wl_list_empty(&grabs)) { ++ /* before bindings and clients */ ++ wl_list_insert(&swc.seat->pointer->handlers, &popup_grab_handler.link); ++ } ++ wl_list_insert(grabs.prev, &popup->grab_link); ++ popup->grabbing = true; + } + + static const struct xdg_popup_interface popup_impl = { +@@ -541,6 +650,141 @@ static const struct xdg_popup_interface popup_impl = { + .grab = grab, + }; + ++/* mirror an anchor or gravity, the enums share values */ ++static uint32_t ++mirror_y(uint32_t v) ++{ ++ static const uint32_t to[] = {0, 2, 1, 3, 4, 6, 5, 8, 7}; ++ ++ return v < 9 ? to[v] : v; ++} ++ ++static uint32_t ++mirror_x(uint32_t v) ++{ ++ static const uint32_t to[] = {0, 1, 2, 4, 3, 7, 8, 5, 6}; ++ ++ return v < 9 ? to[v] : v; ++} ++ ++/* screen at x, y, else the first */ ++static struct swc_rectangle * ++screen_at(int32_t x, int32_t y) ++{ ++ struct screen *screen, *first = NULL; ++ ++ wl_list_for_each (screen, &swc.screens, link) { ++ struct swc_rectangle *g = &screen->base.geometry; ++ ++ if (!first) { ++ first = screen; ++ } ++ if (x >= g->x && y >= g->y && x < g->x + (int32_t)g->width && y < g->y + (int32_t)g->height) { ++ return g; ++ } ++ } ++ return first ? &first->base.geometry : NULL; ++} ++ ++/* popup position kept on screen: flip, then slide, as the positioner allows */ ++static struct swc_rectangle ++constrained_position(struct xdg_positioner *positioner, int32_t px, int32_t py) ++{ ++ struct swc_rectangle r = calculate_position(positioner), f; ++ struct swc_rectangle *g = screen_at(px + r.x, py + r.y); ++ struct xdg_positioner flipped; ++ ++ if (!g) { ++ return r; ++ } ++ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y && ++ py + r.y + r.height > g->y + (int32_t)g->height) { ++ flipped = *positioner; ++ flipped.anchor = mirror_y(flipped.anchor); ++ flipped.gravity = mirror_y(flipped.gravity); ++ flipped.offset_y = -flipped.offset_y; ++ f = calculate_position(&flipped); ++ if (py + f.y >= g->y) { ++ r.y = f.y; ++ } ++ } ++ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X && ++ px + r.x + r.width > g->x + (int32_t)g->width) { ++ flipped = *positioner; ++ flipped.anchor = mirror_x(flipped.anchor); ++ flipped.gravity = mirror_x(flipped.gravity); ++ flipped.offset_x = -flipped.offset_x; ++ f = calculate_position(&flipped); ++ if (px + f.x >= g->x) { ++ r.x = f.x; ++ } ++ } ++ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y) { ++ if (py + r.y + r.height > g->y + (int32_t)g->height) { ++ r.y = g->y + (int32_t)g->height - r.height - py; ++ } ++ if (py + r.y < g->y) { ++ r.y = g->y - py; ++ } ++ } ++ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X) { ++ if (px + r.x + r.width > g->x + (int32_t)g->width) { ++ r.x = g->x + (int32_t)g->width - r.width - px; ++ } ++ if (px + r.x < g->x) { ++ r.x = g->x - px; ++ } ++ } ++ /* cut what still does not fit and tell the client */ ++ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y && ++ py + r.y + r.height > g->y + (int32_t)g->height) { ++ r.height = g->y + (int32_t)g->height - py - r.y; ++ } ++ if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X && ++ px + r.x + r.width > g->x + (int32_t)g->width) { ++ r.width = g->x + (int32_t)g->width - px - r.x; ++ } ++ return r; ++} ++ ++/* place popup against its parent and tell the client */ ++static bool ++xdg_popup_place(struct xdg_popup *popup, struct compositor_view *parent_view) ++{ ++ uint32_t serial = wl_display_next_serial(swc.display); ++ struct swc_rectangle rect; ++ ++ if (!parent_view) { ++ return false; ++ } ++ rect = constrained_position(&popup->positioner, parent_view->base.geometry.x, ++ parent_view->base.geometry.y); ++ compositor_view_set_parent(popup->view, parent_view); ++ /* above the parent, in a layer of normal at least */ ++ popup->view->always_top = parent_view->always_top; ++ compositor_view_set_z(popup->view, parent_view->z > STACK_LAYER_NORMAL ? parent_view->z : STACK_LAYER_NORMAL, ++ true); ++ view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, ++ parent_view->base.geometry.y + rect.y); ++ xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, ++ rect.height); ++ xdg_surface_send_configure(popup->xdg_surface->resource, serial); ++ return true; ++} ++ ++void ++xdg_popup_set_parent(struct wl_resource *resource, struct surface *parent) ++{ ++ struct xdg_popup *popup; ++ ++ if (!resource || !wl_resource_instance_of(resource, &xdg_popup_interface, &popup_impl)) { ++ return; ++ } ++ popup = wl_resource_get_user_data(resource); ++ xdg_popup_place(popup, compositor_view(parent->view)); ++} ++ ++/* a popup with no parent waits for one, as layer surface popups do */ + static struct xdg_popup * + xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, + struct xdg_surface *xdg_surface, struct xdg_surface *parent, +@@ -548,11 +792,9 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, + { + struct xdg_popup *popup; + struct compositor_view *parent_view = +- compositor_view(parent->surface->view); +- uint32_t serial = wl_display_next_serial(swc.display); +- struct swc_rectangle rect; ++ parent ? compositor_view(parent->surface->view) : NULL; + +- if (!parent_view) { ++ if (parent && !parent_view) { + goto error0; + } + popup = malloc(sizeof(*popup)); +@@ -561,6 +803,7 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, + } + popup->xdg_surface = xdg_surface; + popup->positioner = *positioner; ++ popup->grabbing = false; + popup->resource = + wl_resource_create(client, &xdg_popup_interface, version, id); + if (!popup->resource) { +@@ -575,14 +818,9 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, + } + wl_resource_set_implementation(popup->resource, &popup_impl, popup, + &destroy_popup); +- +- rect = calculate_position(positioner); +- compositor_view_set_parent(popup->view, parent_view); +- view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, +- parent_view->base.geometry.y + rect.y); +- xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, +- rect.height); +- xdg_surface_send_configure(xdg_surface->resource, serial); ++ if (parent_view) { ++ xdg_popup_place(popup, parent_view); ++ } + + return popup; + +@@ -630,7 +868,8 @@ get_popup(struct wl_client *client, struct wl_resource *resource, uint32_t id, + struct wl_resource *positioner_resource) + { + struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); +- struct xdg_surface *parent = wl_resource_get_user_data(parent_resource); ++ struct xdg_surface *parent = ++ parent_resource ? wl_resource_get_user_data(parent_resource) : NULL; + struct xdg_positioner *positioner = + wl_resource_get_user_data(positioner_resource); + struct xdg_popup *popup; +diff --git a/libswc/xdg_shell.h b/libswc/xdg_shell.h +index e7bfaa7..de6997b 100644 +--- a/libswc/xdg_shell.h ++++ b/libswc/xdg_shell.h +@@ -25,8 +25,26 @@ + #define SWC_XDG_SHELL_H + + struct wl_display; ++struct wl_resource; ++struct window; + + struct wl_global * + xdg_shell_create(struct wl_display *display); + ++struct surface; ++ ++/* give a parentless popup its parent and place it */ ++void ++xdg_popup_set_parent(struct wl_resource *popup, struct surface *parent); ++ ++/* keyboard focus moved to client (NULL for none): grabbing popups end unless ++ * it is theirs */ ++struct wl_client; ++void ++xdg_popups_focus_changed(struct wl_client *client); ++ ++/* window of an xdg_toplevel */ ++struct window * ++xdg_toplevel_window(struct wl_resource *resource); ++ + #endif diff --git a/patches/tiledRaise.patch b/patches/tiledRaise.patch @@ -1,14 +0,0 @@ -diff --git a/libswc/compositor.c b/libswc/compositor.c -index 16c68ae..f5bee90 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -1303,7 +1303,8 @@ raise_window(struct compositor_view *view) - break; - } - -- if (other->window) { -+ if (other->window && (view->window->mode != WINDOW_MODE_TILED || -+ other->window->mode == WINDOW_MODE_TILED)) { - top_window = other; - break; - } diff --git a/patches/videoBackendsAtRuntime.patch b/patches/videoBackendsAtRuntime.patch @@ -1,665 +0,0 @@ -From b5ead3b6b58b5a7ac3477156e68b79d77899ca15 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 12:18:49 -0300 -Subject: [PATCH] video: drm and fb in one build - --Dvideo=all. SWC_BACKEND=drm or fb picks at run time. ---- - launch/meson.build | 2 +- - libswc/compositor.c | 6 +-- - libswc/fb.c | 2 +- - libswc/internal.h | 10 ++++ - libswc/meson.build | 6 +-- - libswc/pointer.c | 28 ++++++----- - libswc/primary_plane.c | 108 +++++++++++++++++++++++++++-------------- - libswc/primary_plane.h | 13 +++-- - libswc/screen.c | 78 +++++++++++++++++------------ - libswc/screen.h | 7 +-- - libswc/swc.c | 54 ++++++++++++++++----- - meson.build | 18 ++++--- - meson.options | 2 +- - 13 files changed, 216 insertions(+), 118 deletions(-) - -diff --git a/launch/meson.build b/launch/meson.build -index d280fcc..bd8fc33 100644 ---- a/launch/meson.build -+++ b/launch/meson.build -@@ -1,7 +1,7 @@ - launch_protocol = files('protocol.c') - executable('swc-launch', - ['launch.c', launch_protocol, 'devmajor-@0@.c'.format(os)], -- dependencies: video_backend == 'drm' ? drm : [], -+ dependencies: with_drm ? drm : [], - install: true, - install_mode: 'rwsr-xr-x', - ) -diff --git a/libswc/compositor.c b/libswc/compositor.c -index c1cec76..16c68ae 100644 ---- a/libswc/compositor.c -+++ b/libswc/compositor.c -@@ -220,11 +220,9 @@ target_create_surface(struct screen *screen) - return wld_create_surface(swc.backend->context, geom->width, geom->height, - WLD_FORMAT_XRGB8888, - #ifdef ENABLE_DRM -- WLD_DRM_FLAG_SCANOUT --#else -- WLD_FLAG_MAP -+ swc.video == SWC_VIDEO_DRM ? WLD_DRM_FLAG_SCANOUT : - #endif -- ); -+ WLD_FLAG_MAP); - } - - static struct target * -diff --git a/libswc/fb.c b/libswc/fb.c -index d3c332c..0755d94 100644 ---- a/libswc/fb.c -+++ b/libswc/fb.c -@@ -81,7 +81,7 @@ fb_create_screens(struct wl_list *screens) - if (!output) { - return false; - } -- screen = screen_new(output); -+ screen = screen_new(0, output, NULL); - if (!screen) { - output_destroy(output); - return false; -diff --git a/libswc/internal.h b/libswc/internal.h -index eab28c7..ce62c60 100644 ---- a/libswc/internal.h -+++ b/libswc/internal.h -@@ -32,6 +32,15 @@ enum { - SWC_EVENT_DEACTIVATED, - }; - -+/* the video backend swc was started with, chosen at run time among those -+ * that were built. a backend that supplies its own screens and target, such -+ * as nested, leaves it unset */ -+enum swc_video { -+ SWC_VIDEO_NONE, -+ SWC_VIDEO_DRM, -+ SWC_VIDEO_FB, -+}; -+ - struct swc { - struct wl_display *display; - struct wl_event_loop *event_loop; -@@ -48,6 +57,7 @@ struct swc { - struct swc_drm *const drm; - #endif - struct swc_backend *backend; -+ enum swc_video video; - struct wl_global *data_device_manager; - struct wl_global *kde_decoration_manager; - struct wl_global *layer_shell; -diff --git a/libswc/meson.build b/libswc/meson.build -index 022b3e2..0da90b2 100644 ---- a/libswc/meson.build -+++ b/libswc/meson.build -@@ -44,9 +44,9 @@ libswc_src = files( - - xwayland.found() ? ['xserver.c', 'xwm.c'] : [], - -- video_backend == 'drm' ? ['dmabuf.c', 'drm.c', 'plane.c'] : [], -- framebuffer_backend == 'fbdev' ? ['fb.c', 'fb_fbdev.c'] : [], -- framebuffer_backend == 'wsdisplay' ? ['fb.c', 'fb_wsdisplay.c'] : [], -+ with_drm ? ['dmabuf.c', 'drm.c', 'plane.c'] : [], -+ with_fb and framebuffer_backend == 'fbdev' ? ['fb.c', 'fb_fbdev.c'] : [], -+ with_fb and framebuffer_backend == 'wsdisplay' ? ['fb.c', 'fb_wsdisplay.c'] : [], - ) - libswc_src += [cursor, launch_protocol] - -diff --git a/libswc/pointer.c b/libswc/pointer.c -index 88d3740..662d8d3 100644 ---- a/libswc/pointer.c -+++ b/libswc/pointer.c -@@ -220,14 +220,16 @@ attach(struct view *view, struct wld_buffer *buffer) - } - - #ifdef ENABLE_DRM -- wl_list_for_each(screen, &swc.screens, link) { -- view_attach(&screen->planes.cursor->view, -- buffer ? pointer->cursor.buffer : NULL); -- view_update(&screen->planes.cursor->view); -+ if (swc.video == SWC_VIDEO_DRM) { -+ wl_list_for_each(screen, &swc.screens, link) { -+ view_attach(&screen->planes.cursor->view, -+ buffer ? pointer->cursor.buffer : NULL); -+ view_update(&screen->planes.cursor->view); -+ } -+ return 0; - } --#else -- compositor_damage_all(); - #endif -+ compositor_damage_all(); - - return 0; - } -@@ -249,14 +251,16 @@ move(struct view *view, int32_t x, int32_t y) - } - - #ifdef ENABLE_DRM -- wl_list_for_each(screen, &swc.screens, link) { -- view_move(&screen->planes.cursor->view, view->geometry.x, -- view->geometry.y); -- view_update(&screen->planes.cursor->view); -+ if (swc.video == SWC_VIDEO_DRM) { -+ wl_list_for_each(screen, &swc.screens, link) { -+ view_move(&screen->planes.cursor->view, view->geometry.x, -+ view->geometry.y); -+ view_update(&screen->planes.cursor->view); -+ } -+ return true; - } --#else -- compositor_damage_all(); - #endif -+ compositor_damage_all(); - - return true; - } -diff --git a/libswc/primary_plane.c b/libswc/primary_plane.c -index eda7f31..011cb39 100644 ---- a/libswc/primary_plane.c -+++ b/libswc/primary_plane.c -@@ -23,10 +23,11 @@ - - #include "primary_plane.h" - #include "event.h" -+#include "compositor.h" - #ifdef ENABLE_DRM - #include "drm.h" --#else --#include "compositor.h" -+#endif -+#ifdef ENABLE_FB - #include "fb.h" - #endif - #include "internal.h" -@@ -55,11 +56,10 @@ send_frame(void *data) - view_frame(&plane->view, get_time()); - } - -+#ifdef ENABLE_DRM - static int --attach(struct view *view, struct wld_buffer *buffer) -+attach_drm(struct primary_plane *plane, struct wld_buffer *buffer) - { -- struct primary_plane *plane = wl_container_of(view, plane, view); --#ifdef ENABLE_DRM - uint32_t fb; - int ret; - -@@ -88,12 +88,35 @@ attach(struct view *view, struct wld_buffer *buffer) - } - - return 0; --#else -- if (!fb_present(buffer, view->geometry.x, view->geometry.y)) { -+} -+#endif -+ -+#ifdef ENABLE_FB -+static int -+attach_fb(struct primary_plane *plane, struct wld_buffer *buffer) -+{ -+ if (!fb_present(buffer, plane->view.geometry.x, plane->view.geometry.y)) { - return -1; - } - wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); - return 0; -+} -+#endif -+ -+static int -+attach(struct view *view, struct wld_buffer *buffer) -+{ -+ struct primary_plane *plane = wl_container_of(view, plane, view); -+ -+#ifdef ENABLE_DRM -+ if (plane->drm) { -+ return attach_drm(plane, buffer); -+ } -+#endif -+#ifdef ENABLE_FB -+ return attach_fb(plane, buffer); -+#else -+ return -1; - #endif - } - -@@ -129,24 +152,33 @@ handle_swc_event(struct wl_listener *listener, void *data) - switch (event->type) { - case SWC_EVENT_ACTIVATED: - #ifdef ENABLE_DRM -- plane->need_modeset = true; --#else -- compositor_damage_all(); -+ if (plane->drm) { -+ plane->need_modeset = true; -+ break; -+ } - #endif -+ compositor_damage_all(); - break; - } - } - --bool --#ifdef ENABLE_DRM --primary_plane_initialize(struct primary_plane *plane, uint32_t crtc, -- struct mode *mode, uint32_t *connectors, -- uint32_t num_connectors) --#else --primary_plane_initialize(struct primary_plane *plane, struct mode *mode) --#endif -+static void -+initialize_common(struct primary_plane *plane, struct mode *mode) - { -+ view_initialize(&plane->view, &view_impl); -+ plane->view.geometry.width = mode->width; -+ plane->view.geometry.height = mode->height; -+ plane->mode = *mode; -+ plane->swc_listener.notify = &handle_swc_event; -+ wl_signal_add(&swc.event_signal, &plane->swc_listener); -+} -+ - #ifdef ENABLE_DRM -+bool -+primary_plane_initialize_drm(struct primary_plane *plane, uint32_t crtc, -+ struct mode *mode, uint32_t *connectors, -+ uint32_t num_connectors) -+{ - uint32_t *plane_connectors; - - if (!(plane->original_crtc_state = drmModeGetCrtc(swc.drm->fd, crtc))) { -@@ -166,43 +198,45 @@ primary_plane_initialize(struct primary_plane *plane, struct mode *mode) - - memcpy(plane_connectors, connectors, - num_connectors * sizeof(connectors[0])); -+ plane->drm = true; - plane->crtc = crtc; - plane->need_modeset = true; -- view_initialize(&plane->view, &view_impl); -- plane->view.geometry.width = mode->width; -- plane->view.geometry.height = mode->height; - plane->drm_handler.page_flip = &handle_page_flip; -- plane->swc_listener.notify = &handle_swc_event; -- plane->mode = *mode; --#else -- view_initialize(&plane->view, &view_impl); -- plane->view.geometry.width = mode->width; -- plane->view.geometry.height = mode->height; -- plane->mode = *mode; --#endif -- plane->swc_listener.notify = &handle_swc_event; -- wl_signal_add(&swc.event_signal, &plane->swc_listener); -+ initialize_common(plane, mode); - - return true; - --#ifdef ENABLE_DRM - error1: - drmModeFreeCrtc(plane->original_crtc_state); - error0: - return false; -+} - #endif -+ -+#ifdef ENABLE_FB -+bool -+primary_plane_initialize_fb(struct primary_plane *plane, struct mode *mode) -+{ -+#ifdef ENABLE_DRM -+ plane->drm = false; -+#endif -+ initialize_common(plane, mode); -+ return true; - } -+#endif - - void - primary_plane_finalize(struct primary_plane *plane) - { - wl_list_remove(&plane->swc_listener.link); - #ifdef ENABLE_DRM -- wl_array_release(&plane->connectors); -- drmModeCrtcPtr crtc = plane->original_crtc_state; -- drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x, -- crtc->y, NULL, 0, &crtc->mode); -- drmModeFreeCrtc(crtc); -+ if (plane->drm) { -+ wl_array_release(&plane->connectors); -+ drmModeCrtcPtr crtc = plane->original_crtc_state; -+ drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, crtc->x, -+ crtc->y, NULL, 0, &crtc->mode); -+ drmModeFreeCrtc(crtc); -+ } - #else - (void)plane; - #endif -diff --git a/libswc/primary_plane.h b/libswc/primary_plane.h -index 55335ca..c9c8235 100644 ---- a/libswc/primary_plane.h -+++ b/libswc/primary_plane.h -@@ -36,6 +36,8 @@ - - struct primary_plane { - #ifdef ENABLE_DRM -+ /* driven through drm, not the framebuffer */ -+ bool drm; - uint32_t crtc; - drmModeCrtcPtr original_crtc_state; - #endif -@@ -51,12 +53,13 @@ struct primary_plane { - - #ifdef ENABLE_DRM - bool --primary_plane_initialize(struct primary_plane *plane, uint32_t crtc, -- struct mode *mode, uint32_t *connectors, -- uint32_t num_connectors); --#else -+primary_plane_initialize_drm(struct primary_plane *plane, uint32_t crtc, -+ struct mode *mode, uint32_t *connectors, -+ uint32_t num_connectors); -+#endif -+#ifdef ENABLE_FB - bool --primary_plane_initialize(struct primary_plane *plane, struct mode *mode); -+primary_plane_initialize_fb(struct primary_plane *plane, struct mode *mode); - #endif - void - primary_plane_finalize(struct primary_plane *plane); -diff --git a/libswc/screen.c b/libswc/screen.c -index 31c5db5..c01de9d 100644 ---- a/libswc/screen.c -+++ b/libswc/screen.c -@@ -26,7 +26,8 @@ - #include "compositor.h" - #ifdef ENABLE_DRM - #include "drm.h" --#else -+#endif -+#ifdef ENABLE_FB - #include "fb.h" - #endif - #include "event.h" -@@ -70,20 +71,30 @@ swc_screen_set_handler(struct swc_screen *base, - screen->handler_data = data; - } - -+/* the screens of the video backend swc started with */ -+static bool -+create_default_screens(void) -+{ -+#ifdef ENABLE_DRM -+ if (swc.video == SWC_VIDEO_DRM) { -+ return drm_create_screens(&swc.screens); -+ } -+#endif -+#ifdef ENABLE_FB -+ if (swc.video == SWC_VIDEO_FB) { -+ return fb_create_screens(&swc.screens); -+ } -+#endif -+ return false; -+} -+ - bool - screens_initialize(void) - { - wl_list_init(&swc.screens); - -- if (!(swc.backend->create_screens -- ? swc.backend->create_screens(&swc.screens) -- : --#ifdef ENABLE_DRM -- drm_create_screens(&swc.screens) --#else -- fb_create_screens(&swc.screens) --#endif -- )) { -+ if (!(swc.backend->create_screens ? swc.backend->create_screens(&swc.screens) -+ : create_default_screens())) { - return false; - } - -@@ -121,11 +132,7 @@ bind_screen(struct wl_client *client, void *data, uint32_t version, uint32_t id) - } - - struct screen * --#ifdef ENABLE_DRM - screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane) --#else --screen_new(struct output *output) --#endif - { - struct screen *screen; - int32_t x = 0; -@@ -147,26 +154,33 @@ screen_new(struct output *output) - } - - #ifdef ENABLE_DRM -- screen->crtc = crtc; -- if (!primary_plane_initialize(&screen->planes.primary, -- crtc, -- output->preferred_mode, -- &output->connector, -- 1)) { -- ERROR("Failed to initialize primary plane\n"); -- goto error2; -- } -+ if (swc.video == SWC_VIDEO_DRM) { -+ screen->crtc = crtc; -+ if (!primary_plane_initialize_drm(&screen->planes.primary, -+ crtc, -+ output->preferred_mode, -+ &output->connector, -+ 1)) { -+ ERROR("Failed to initialize primary plane\n"); -+ goto error2; -+ } - -- cursor_plane->screen = screen; -- screen->planes.cursor = cursor_plane; --#else -- if (!primary_plane_initialize(&screen->planes.primary, -- output->preferred_mode)) { -- ERROR("Failed to initialize primary plane\n"); -- goto error2; -- } -- screen->planes.cursor = NULL; -+ cursor_plane->screen = screen; -+ screen->planes.cursor = cursor_plane; -+ } else - #endif -+ { -+#ifdef ENABLE_FB -+ if (!primary_plane_initialize_fb(&screen->planes.primary, -+ output->preferred_mode)) { -+ ERROR("Failed to initialize primary plane\n"); -+ goto error2; -+ } -+#endif -+ (void)crtc; -+ (void)cursor_plane; -+ screen->planes.cursor = NULL; -+ } - - screen->handler = &null_handler; - wl_signal_init(&screen->destroy_signal); -diff --git a/libswc/screen.h b/libswc/screen.h -index a64072b..ee896ba 100644 ---- a/libswc/screen.h -+++ b/libswc/screen.h -@@ -75,13 +75,10 @@ screens_initialize(void); - void - screens_finalize(void); - --#ifdef ENABLE_DRM -+/* crtc and cursor_plane are the drm backend's, the framebuffer's pass 0 and -+ * NULL */ - struct screen * - screen_new(uint32_t crtc, struct output *output, struct plane *cursor_plane); --#else --struct screen * --screen_new(struct output *output); --#endif - struct screen * - screen_new_virtual(struct output *output, const struct view_impl *impl); - void -diff --git a/libswc/swc.c b/libswc/swc.c -index 898253c..6a37415 100644 ---- a/libswc/swc.c -+++ b/libswc/swc.c -@@ -27,7 +27,8 @@ - #include "data_device_manager.h" - #ifdef ENABLE_DRM - #include "drm.h" --#else -+#endif -+#ifdef ENABLE_FB - #include "fb.h" - #endif - #include "event.h" -@@ -228,6 +229,38 @@ swc_keysym_to_key(uint32_t keysym, uint32_t *key, uint32_t *level) - return false; - } - -+/* whether SWC_BACKEND leaves the video backend called name open: drm or fb -+ * name one, anything else, or nothing, lets swc try each that was built */ -+static bool -+video_wanted(const char *name) -+{ -+ const char *backend = getenv("SWC_BACKEND"); -+ -+ if (!backend || (strcmp(backend, "drm") != 0 && strcmp(backend, "fb") != 0)) { -+ return true; -+ } -+ return strcmp(backend, name) == 0; -+} -+ -+/* the first video backend that was built, was asked for and comes up */ -+static bool -+video_initialize(void) -+{ -+#ifdef ENABLE_DRM -+ if (video_wanted("drm") && drm_initialize()) { -+ swc.video = SWC_VIDEO_DRM; -+ return true; -+ } -+#endif -+#ifdef ENABLE_FB -+ if (video_wanted("fb") && fb_initialize()) { -+ swc.video = SWC_VIDEO_FB; -+ return true; -+ } -+#endif -+ return false; -+} -+ - static bool - backend_initialize(void) - { -@@ -242,13 +275,7 @@ backend_initialize(void) - return false; - } - -- if (! --#ifdef ENABLE_DRM -- drm_initialize() --#else -- fb_initialize() --#endif -- ) { -+ if (!video_initialize()) { - ERROR("Could not initialize video backend\n"); - launch_finalize(); - return false; -@@ -266,9 +293,14 @@ backend_finalize(void) - } - - #ifdef ENABLE_DRM -- drm_finalize(); --#else -- fb_finalize(); -+ if (swc.video == SWC_VIDEO_DRM) { -+ drm_finalize(); -+ } -+#endif -+#ifdef ENABLE_FB -+ if (swc.video == SWC_VIDEO_FB) { -+ fb_finalize(); -+ } - #endif - launch_finalize(); - } -diff --git a/meson.build b/meson.build -index 1cfefa2..79dee81 100644 ---- a/meson.build -+++ b/meson.build -@@ -39,15 +39,20 @@ requires_private = [ - - video_backend = get_option('video') - framebuffer_backend = get_option('fb') -+# 'all' builds the drm and the framebuffer backends together, and swc picks -+# one when it starts -+with_drm = video_backend in ['drm', 'all'] -+with_fb = video_backend in ['fb', 'all'] - drm = disabler() --if video_backend == 'drm' -- if framebuffer_backend != 'auto' -- error('-Dfb applies only when -Dvideo=fb') -+if with_drm -+ if video_backend == 'drm' and framebuffer_backend != 'auto' -+ error('-Dfb applies only when -Dvideo=fb or -Dvideo=all') - endif - drm = dependency('libdrm') - requires_private += drm - add_project_arguments('-DENABLE_DRM=1', language: 'c') --elif video_backend == 'fb' -+endif -+if with_fb - if framebuffer_backend == 'auto' - if os == 'linux' - framebuffer_backend = 'fbdev' -@@ -58,6 +63,7 @@ elif video_backend == 'fb' - endif - endif - -+ add_project_arguments('-DENABLE_FB=1', language: 'c') - if framebuffer_backend == 'fbdev' - if os != 'linux' - error('The fbdev framebuffer interface is supported only on Linux') -@@ -78,7 +84,7 @@ elif video_backend == 'fb' - endif - - if video_backend != 'drm' and get_option('xwayland').enabled() -- error('Xwayland requires the DRM video backend') -+ error('Xwayland requires the DRM video backend alone') - endif - xwayland = video_backend == 'drm' \ - ? dependency('xwayland', required: get_option('xwayland')) \ -@@ -156,5 +162,5 @@ summary({ - 'Nested backend': have_nested, - 'Input backend': input_backend, - 'Video backend': video_backend, -- 'Framebuffer interface': video_backend == 'fb' ? framebuffer_backend : 'n/a', -+ 'Framebuffer interface': with_fb ? framebuffer_backend : 'n/a', - }, bool_yn: true) -diff --git a/meson.options b/meson.options -index 488182b..09702bd 100644 ---- a/meson.options -+++ b/meson.options -@@ -1,6 +1,6 @@ - option('xwayland', type: 'feature', value: 'auto', description: 'Automatic Xwayland handling') - option('input', type: 'combo', value: 'auto', choices: ['auto', 'libinput', 'wscons'], description: 'Input backend to use') --option('video', type: 'combo', value: 'drm', choices: ['drm', 'fb'], description: 'Video backend to use') -+option('video', type: 'combo', value: 'drm', choices: ['drm', 'fb', 'all'], description: 'Video backend to use: all builds drm and the framebuffer and picks one at run time') - option('fb', type: 'combo', value: 'auto', choices: ['auto', 'fbdev', 'wsdisplay'], description: 'Framebuffer interface to use') - option('fbdev-device', type: 'string', value: '/dev/fb0', description: 'Linux framebuffer device') - option('wsdisplay-device', type: 'string', value: '', description: 'wsdisplay framebuffer device (empty uses the VT selected by swc-launch)') --- -2.56.1 - diff --git a/patches/xdgDialog.patch b/patches/xdgDialog.patch @@ -1,322 +0,0 @@ -From 5b9647240f8026d5f4e21f7f669d29f3a9e81932 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:56:32 -0300 -Subject: [PATCH 06/19] add xdg-dialog-v1 - -Lets a manager know a toplevel is modal. ---- - libswc/internal.h | 1 + - libswc/meson.build | 1 + - libswc/swc.c | 4 ++ - libswc/swc.h | 7 +++ - libswc/window.c | 12 +++++ - libswc/window.h | 2 + - libswc/xdg_dialog.c | 117 +++++++++++++++++++++++++++++++++++++++++++ - libswc/xdg_dialog.h | 11 ++++ - libswc/xdg_shell.c | 8 +++ - libswc/xdg_shell.h | 6 +++ - protocol/meson.build | 1 + - 11 files changed, 170 insertions(+) - create mode 100644 libswc/xdg_dialog.c - create mode 100644 libswc/xdg_dialog.h - -diff --git a/libswc/internal.h b/libswc/internal.h -index 54971c6..eab28c7 100644 ---- a/libswc/internal.h -+++ b/libswc/internal.h -@@ -57,6 +57,7 @@ struct swc { - struct wl_global *select_manager; - struct wl_global *subcompositor; - struct wl_global *xdg_decoration_manager; -+ struct wl_global *xdg_dialog_manager; - struct wl_global *xdg_output_manager; - struct wl_global *xdg_shell; - -diff --git a/libswc/meson.build b/libswc/meson.build -index 15fa57a..04cef6e 100644 ---- a/libswc/meson.build -+++ b/libswc/meson.build -@@ -34,6 +34,7 @@ libswc_src = files( - 'wayland_buffer.c', - 'window.c', - 'xdg_decoration.c', -+ 'xdg_dialog.c', - 'xdg_output.c', - 'xdg_shell.c', - -diff --git a/libswc/swc.c b/libswc/swc.c -index e48cd8d..55f3351 100644 ---- a/libswc/swc.c -+++ b/libswc/swc.c -@@ -48,6 +48,7 @@ - #include "util.h" - #include "window.h" - #include "xdg_decoration.h" -+#include "xdg_dialog.h" - #include "xdg_output.h" - #include "xdg_shell.h" - #ifdef ENABLE_XWAYLAND -@@ -244,6 +245,9 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, - goto error10; - } - -+ /* optional */ -+ swc.xdg_dialog_manager = xdg_dialog_manager_create(display); -+ - swc.xdg_decoration_manager = xdg_decoration_manager_create(display); - if (!swc.xdg_decoration_manager) { - ERROR("Could not initialize XDG decoration manager\n"); -diff --git a/libswc/swc.h b/libswc/swc.h -index fc0fa9a..2d4e58d 100644 ---- a/libswc/swc.h -+++ b/libswc/swc.h -@@ -267,6 +267,11 @@ struct swc_window_handler { - * the interactive resize will be honored. - */ - void (*resize)(void *data); -+ -+ /** -+ * Called when the window starts or stops blocking its parent. -+ */ -+ void (*modal_changed)(void *data); - }; - - struct swc_window { -@@ -274,6 +279,8 @@ struct swc_window { - char *app_id; - - struct swc_window *parent; -+ /* the window says it blocks its parent */ -+ bool modal; - uint32_t motion_throttle_ms; - uint32_t min_width; - uint32_t min_height; -diff --git a/libswc/window.c b/libswc/window.c -index 19a9897..a24ec1a 100644 ---- a/libswc/window.c -+++ b/libswc/window.c -@@ -616,6 +616,18 @@ window_set_app_id(struct window *window, const char *app_id) - } - } - -+void -+window_set_modal(struct window *window, bool modal) -+{ -+ if (window->base.modal == modal) { -+ return; -+ } -+ window->base.modal = modal; -+ if (window->handler->modal_changed) { -+ window->handler->modal_changed(window->handler_data); -+ } -+} -+ - void - window_set_parent(struct window *window, struct window *parent) - { -diff --git a/libswc/window.h b/libswc/window.h -index f1eadde..bf6baec 100644 ---- a/libswc/window.h -+++ b/libswc/window.h -@@ -111,6 +111,8 @@ window_set_app_id(struct window *window, const char *app_id); - void - window_set_parent(struct window *window, struct window *parent); - void -+window_set_modal(struct window *window, bool modal); -+void - window_begin_move(struct window *window, struct button *button); - void - window_begin_resize(struct window *window, uint32_t edges, -diff --git a/libswc/xdg_dialog.c b/libswc/xdg_dialog.c -new file mode 100644 -index 0000000..25233b9 ---- /dev/null -+++ b/libswc/xdg_dialog.c -@@ -0,0 +1,117 @@ -+/* xdg-dialog-v1 */ -+ -+#include "xdg_dialog.h" -+#include "util.h" -+#include "window.h" -+#include "xdg_shell.h" -+ -+#include <stdlib.h> -+#include <wayland-server.h> -+#include "xdg-dialog-v1-server-protocol.h" -+ -+struct dialog { -+ struct wl_resource *resource; -+ struct wl_resource *toplevel; -+ struct wl_listener toplevel_destroy; -+}; -+ -+static void -+set(struct wl_resource *resource, bool modal) -+{ -+ struct dialog *dialog = wl_resource_get_user_data(resource); -+ struct window *window; -+ -+ if (dialog->toplevel && (window = xdg_toplevel_window(dialog->toplevel))) { -+ window_set_modal(window, modal); -+ } -+} -+ -+static void -+set_modal(struct wl_client *client, struct wl_resource *resource) -+{ -+ (void)client; -+ set(resource, true); -+} -+ -+static void -+unset_modal(struct wl_client *client, struct wl_resource *resource) -+{ -+ (void)client; -+ set(resource, false); -+} -+ -+static const struct xdg_dialog_v1_interface dialog_impl = { -+ .destroy = destroy_resource, -+ .set_modal = set_modal, -+ .unset_modal = unset_modal, -+}; -+ -+static void -+handle_toplevel_destroy(struct wl_listener *listener, void *data) -+{ -+ struct dialog *dialog = wl_container_of(listener, dialog, toplevel_destroy); -+ -+ (void)data; -+ dialog->toplevel = NULL; -+} -+ -+static void -+destroy_dialog(struct wl_resource *resource) -+{ -+ struct dialog *dialog = wl_resource_get_user_data(resource); -+ -+ /* no longer a dialog: stop blocking */ -+ set(resource, false); -+ if (dialog->toplevel) { -+ wl_list_remove(&dialog->toplevel_destroy.link); -+ } -+ free(dialog); -+} -+ -+static void -+get_xdg_dialog(struct wl_client *client, struct wl_resource *resource, uint32_t id, -+ struct wl_resource *toplevel) -+{ -+ struct dialog *dialog = calloc(1, sizeof *dialog); -+ -+ if (!dialog) { -+ wl_resource_post_no_memory(resource); -+ return; -+ } -+ dialog->resource = wl_resource_create(client, &xdg_dialog_v1_interface, -+ wl_resource_get_version(resource), id); -+ if (!dialog->resource) { -+ free(dialog); -+ wl_resource_post_no_memory(resource); -+ return; -+ } -+ dialog->toplevel = toplevel; -+ dialog->toplevel_destroy.notify = handle_toplevel_destroy; -+ wl_resource_add_destroy_listener(toplevel, &dialog->toplevel_destroy); -+ wl_resource_set_implementation(dialog->resource, &dialog_impl, dialog, destroy_dialog); -+} -+ -+static const struct xdg_wm_dialog_v1_interface manager_impl = { -+ .destroy = destroy_resource, -+ .get_xdg_dialog = get_xdg_dialog, -+}; -+ -+static void -+bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) -+{ -+ struct wl_resource *resource; -+ -+ (void)data; -+ resource = wl_resource_create(client, &xdg_wm_dialog_v1_interface, version, id); -+ if (!resource) { -+ wl_client_post_no_memory(client); -+ return; -+ } -+ wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); -+} -+ -+struct wl_global * -+xdg_dialog_manager_create(struct wl_display *display) -+{ -+ return wl_global_create(display, &xdg_wm_dialog_v1_interface, 1, NULL, &bind_manager); -+} -diff --git a/libswc/xdg_dialog.h b/libswc/xdg_dialog.h -new file mode 100644 -index 0000000..134b329 ---- /dev/null -+++ b/libswc/xdg_dialog.h -@@ -0,0 +1,11 @@ -+/* xdg-dialog-v1 */ -+ -+#ifndef SWC_XDG_DIALOG_H -+#define SWC_XDG_DIALOG_H -+ -+struct wl_display; -+ -+struct wl_global * -+xdg_dialog_manager_create(struct wl_display *display); -+ -+#endif -diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c -index b76f884..ea3b533 100644 ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -493,6 +493,14 @@ static const struct xdg_toplevel_interface toplevel_impl = { - .set_minimized = set_minimized, - }; - -+struct window * -+xdg_toplevel_window(struct wl_resource *resource) -+{ -+ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); -+ -+ return toplevel ? &toplevel->window : NULL; -+} -+ - static struct xdg_toplevel * - xdg_toplevel_new(struct wl_client *client, uint32_t version, uint32_t id, - struct xdg_surface *xdg_surface) -diff --git a/libswc/xdg_shell.h b/libswc/xdg_shell.h -index e7bfaa7..fd51a2d 100644 ---- a/libswc/xdg_shell.h -+++ b/libswc/xdg_shell.h -@@ -25,8 +25,14 @@ - #define SWC_XDG_SHELL_H - - struct wl_display; -+struct wl_resource; -+struct window; - - struct wl_global * - xdg_shell_create(struct wl_display *display); - -+/* window of an xdg_toplevel */ -+struct window * -+xdg_toplevel_window(struct wl_resource *resource); -+ - #endif -diff --git a/protocol/meson.build b/protocol/meson.build -index c6a0e63..4896a30 100644 ---- a/protocol/meson.build -+++ b/protocol/meson.build -@@ -13,6 +13,7 @@ foreach proto : [ - wl.find_protocol('linux-dmabuf', state: 'unstable', version: 1), - wl.find_protocol('xdg-decoration', state: 'unstable', version: 1), - wl.find_protocol('xdg-output', state: 'unstable', version: 1), -+ wl.find_protocol('xdg-dialog', state: 'staging', version: 1), - ] - protocols = wl.scan_xml(proto, - public: true, --- -2.56.1 - diff --git a/patches/xdgMinMaxSize.patch b/patches/xdgMinMaxSize.patch @@ -1,40 +0,0 @@ -From 6340387cb119fb8683c03ea41e51e320f22b3675 Mon Sep 17 00:00:00 2001 -From: xplshn <anto@xplshn.com.ar> -Date: Sat, 21 Nov 2026 11:56:11 -0300 -Subject: [PATCH 05/19] xdg_shell: honor set_min_size and set_max_size - ---- - libswc/xdg_shell.c | 11 +++++++++++ - 1 file changed, 11 insertions(+) - -diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c -index c5d6986..b76f884 100644 ---- a/libswc/xdg_shell.c -+++ b/libswc/xdg_shell.c -@@ -431,12 +431,23 @@ static void - set_max_size(struct wl_client *client, struct wl_resource *resource, - int32_t width, int32_t height) - { -+ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); -+ -+ (void)client; -+ /* 0 is no limit */ -+ toplevel->window.base.max_width = width > 0 ? (uint32_t)width : 0; -+ toplevel->window.base.max_height = height > 0 ? (uint32_t)height : 0; - } - - static void - set_min_size(struct wl_client *client, struct wl_resource *resource, - int32_t width, int32_t height) - { -+ struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); -+ -+ (void)client; -+ toplevel->window.base.min_width = width > 0 ? (uint32_t)width : 1; -+ toplevel->window.base.min_height = height > 0 ? (uint32_t)height : 1; - } - - static void --- -2.56.1 -