neuswc

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

commit 74edc13d1964295404e15d50560aeeba60125e0f
parent 0a80f1f8a1a0792c61eb74f42933ff09b95e7215
Author: cowmonk <cowmonk@based.pt>
Date:   Sun, 22 Nov 2026 21:07:24 -0700

apply MICE patches

README should take presedence over README.md, the idea is to make it
easier(-ish) to pull from upstream via rebase.

Diffstat:
AREADME | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlaunch/launch.c | 289+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mlaunch/meson.build | 2+-
Mlibswc/backend.h | 18+++++++++++++++++-
Mlibswc/compositor.c | 315++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Mlibswc/compositor.h | 18++++++++++++++++++
Mlibswc/data.c | 113++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mlibswc/data.h | 11+++++++++++
Mlibswc/data_device.c | 286++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mlibswc/fb.c | 2+-
Mlibswc/internal.h | 11+++++++++++
Mlibswc/keyboard.c | 195+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Mlibswc/keyboard.h | 11+++++++++++
Mlibswc/launch.c | 5+++++
Mlibswc/layer_shell.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mlibswc/layer_shell.h | 11+++++++++++
Mlibswc/meson.build | 11++++++++---
Alibswc/nested.c | 1135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibswc/nested.h | 16++++++++++++++++
Mlibswc/output.c | 19+++++++++++++++++++
Mlibswc/output.h | 2++
Mlibswc/panel.c | 36++++++++++++++++++++++++++----------
Mlibswc/pointer.c | 60++++++++++++++++++++++++++++++++++++++++++++++--------------
Mlibswc/primary_plane.c | 108++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mlibswc/primary_plane.h | 13++++++++-----
Mlibswc/screen.c | 173+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Mlibswc/screen.h | 11+++++++----
Mlibswc/seat.c | 30+++++++++++++++++++++++-------
Mlibswc/swc.c | 193++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Mlibswc/swc.h | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibswc/swc.h.orig | 773+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibswc/window.c | 70+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Alibswc/window.c.orig | 772+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibswc/window.h | 3+++
Alibswc/xdg_dialog.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alibswc/xdg_dialog.h | 11+++++++++++
Mlibswc/xdg_shell.c | 307++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Alibswc/xdg_shell.c.orig | 1075+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlibswc/xdg_shell.h | 18++++++++++++++++++
Mmeson.build | 33+++++++++++++++++++++++++++------
Mmeson.options | 3++-
Apatches/borderAbleToGoRoundDecor.patch | 159+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/borderSharedRoutine.patch | 240+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/dragAndDrop.patch | 515+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/dragModifiersChooseAction.patch | 221+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/extentsFromAttachedBuffer.patch | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/keyboardResetTellsModifiers.patch | 43+++++++++++++++++++++++++++++++++++++++++++
Apatches/keymapAtRuntime.patch | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/launchEscapeKeys.patch | 238+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/launchRestoresConsole.patch | 232+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/layerPopupsOnScreen.patch | 241+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/layerZoneZeroUsableArea.patch | 127+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/managerTransients.patch | 208+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/nestedBackend.patch | 2052+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/netlinkKernelEvents.patch | 18++++++++++++++++++
Apatches/neuswc-buffer-release.patch | 33+++++++++++++++++++++++++++++++++
Apatches/neuswc-configure.patch | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/neuswc-cursor-frame.patch | 15+++++++++++++++
Apatches/neuswc-fullscreen.patch | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/panelRedockOnResize.patch | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/popupGrabDismiss.patch | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/popupGrabKeyboardFocus.patch | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/popupsFitScreenKeysOnDemand.patch | 127+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/popupsOverParent.patch | 45+++++++++++++++++++++++++++++++++++++++++++++
Apatches/seatFeedInput.patch | 113+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/videoBackendsAtRuntime.patch | 665+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/xdgDialog.patch | 322+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apatches/xdgMinMaxSize.patch | 40++++++++++++++++++++++++++++++++++++++++
Mprotocol/meson.build | 4++++
69 files changed, 12446 insertions(+), 391 deletions(-)

diff --git a/README b/README @@ -0,0 +1,70 @@ +neuswc +------ + +NOTE: this is a patched version of neuswc which uses non-upstreamed patches, thisk will likely be archived once all patches have made it upstream. +See: ./patches for all the patches applied + +neuswc is a fork of [swc](https://github.com/michaelforney/swc/) created by [wayland.fyi](https://wayland.fyi). it provides an easy C library interface to make a wayland compositor. it is much smaller than alternatives like wlroots, and easier to use. it is also portable to linux, freebsd, netbsd, and openbsd. if you want to help with that (or anything else) submit a patch to the [mailing list](https://lists.sr.ht/~shrub900/neuswc) + +you can view a list of some compositors made with neuswc at the [wayland.fyi website.](https://wayland.fyi) + +the refrence compositor implementation is [tohu](https://git.sr.ht/~shrub900/tohu): it uses many of the new features and is small and simple to understand. if you want to make your own compositor, it can be a useful place to refrence how things work, or a base to customize. + + +neu features +------------ + +- z axis ordering +- more cursor functions +- zooming +- experimental subsurface support +- fullscreen +- double window borders +- screenshots +- layer shell support +- window decorations +- framebuffer support + +neuswc is in active development, we plan to add many more features, and increase compatibility with new wayland protocols. + +build +----- + +you will need: +- A C99-compatible compiler +- meson or muon +- pkg-config +- wayland-scanner, wayland-server, wayland-client +- wayland-server, wayland-client +- libdrm (if building with DRM support) +- pixman, xkbcommon +- [neuwld](https://git.sr.ht/~shrub900/neuwld) +- libinput on Linux and wscons on BSD +- xcb, xcb-composite, xcb-ewmh and xcb-icccm if you want Xwayland support. + +to build, i use muon and samu. you can probably do something similar with meson and ninja. +warning if you use muon, you will need a recent version built from their git for wayland module support, probably not your distro-packaged version. i do: + +``` +muon setup build +samu -C build +sudo samu -C build install +``` + +openbsd +_______ +you need to make sure xenocara is stopped. this includes xenodm which you can close with `rcctl stop xenodm # disable permanent and after reboot`; this is because openbsd doesnt support handing off the drm device yet. + +you also need to own `/dev/dri/card0` and any keyboard or mouse devices in `/dev/wskbdN` and `/dev/wsmouseN`. we do not use the general keyboard multiplexer as its not suitable for wayland yet. + +for persistent ownership of said devices, you can check the fbtab(5) in `/etc/fbtab` + +credits +------- +an extremely large thank you to [michael forney](https://mforney.org) for creating the original [swc](https://github.com/michaelforney/swc) and [wld](https://github.com/michaelforney/wld), without his amazing work none of this would be possible! once they are more stable, we hope some of the changes from our neu* forks will eventually be upstreamed into the original projects. + +repositories +------------ + +- [srcdump](https://srcdump.net/shrub/neuswc) +- [sourcehut](https://git.sr.ht/~shrub900/neuswc) diff --git 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 @@ -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/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 @@ -208,25 +208,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,17 +319,67 @@ 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); +} + 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; @@ -332,29 +390,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)) { @@ -396,45 +442,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 +638,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 +647,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 +1081,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 +1108,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 +1128,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 @@ -1176,8 +1197,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; @@ -1185,7 +1206,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; } @@ -1226,6 +1247,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) { @@ -1268,6 +1311,7 @@ raise_window(struct compositor_view *view) } if (view == top_window) { + raise_transients(view); return; } @@ -1282,6 +1326,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 @@ -1316,7 +1361,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,6 +1488,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; @@ -1612,7 +1658,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 && @@ -1635,6 +1681,32 @@ compositor_view_set_border_width(struct compositor_view *view, } 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 +1736,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); } @@ -1866,7 +1938,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); @@ -1887,7 +1959,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 +2118,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 +2216,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 @@ -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; @@ -80,6 +82,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; @@ -101,6 +106,9 @@ struct compositor_view { uint32_t incolor; bool damaged_border2; + + /* border goes around the decor too */ + bool frame; } border; struct { @@ -138,6 +146,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 +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 @@ -162,6 +176,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/data.c b/libswc/data.c @@ -33,8 +33,36 @@ 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; + /* 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 +74,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 +93,45 @@ 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); + } +} + +/* 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) { + 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 +169,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 +219,10 @@ 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->drag_offer = NULL; + data->offer_actions = data->preferred = data->chosen = 0; data->source = wl_resource_create(client, &wl_data_source_interface, version, id); @@ -211,3 +267,42 @@ 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 = 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 @@ -24,6 +24,7 @@ #ifndef SWC_DATA_H #define SWC_DATA_H +#include <stdbool.h> #include <stdint.h> struct wl_client; @@ -36,4 +37,14 @@ 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); +/* 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 @@ -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_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 diff --git 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 @@ -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,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/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 @@ -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/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 @@ -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; }; @@ -179,10 +182,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,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 @@ -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/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', @@ -43,12 +44,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 @@ -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 @@ -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 @@ -171,6 +171,25 @@ 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) +{ + 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) { struct wl_resource *resource, *tmp; diff --git 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/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/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); @@ -208,15 +212,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 +245,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; } @@ -431,6 +451,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 +571,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 +802,6 @@ pointer_handle_absolute_motion(struct pointer *pointer, uint32_t time, break; } } - - update_cursor(pointer); } void diff --git 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 @@ -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 @@ -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 @@ -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/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/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,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 @@ -167,6 +172,140 @@ swc_cursor_set_position(int32_t x, int32_t y) } 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 +316,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 +371,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"); @@ -295,6 +425,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 +464,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 +489,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 @@ -59,6 +59,25 @@ 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 @@ -267,6 +286,16 @@ 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); }; struct swc_window { @@ -274,6 +303,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 +319,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. */ @@ -345,6 +382,24 @@ 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 @@ -400,6 +455,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 +643,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 +669,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/libswc/swc.h.orig b/libswc/swc.h.orig @@ -0,0 +1,773 @@ +/* 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 @@ -189,6 +189,13 @@ swc_window_set_handler(struct swc_window *base, } 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); @@ -235,6 +242,32 @@ swc_window_focus(struct swc_window *base) } 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); @@ -368,6 +401,14 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, } 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; @@ -497,6 +538,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 = { @@ -534,6 +579,7 @@ 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; @@ -591,6 +637,7 @@ window_unmanage(struct window *window) window->handler->destroy(window->handler_data); } window->handler = &null_handler; + window->fullscreen = NULL; window->managed = false; } @@ -617,14 +664,31 @@ 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.c.orig b/libswc/window.c.orig @@ -0,0 +1,772 @@ +/* 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,6 +47,7 @@ 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; @@ -111,6 +112,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 @@ -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 @@ -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 @@ -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,27 @@ 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; + + if (window->configure.pending) + send_configure(toplevel, window->configure.width, window->configure.height); + else + send_configure(toplevel, geometry->width, geometry->height); } static void @@ -294,33 +368,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 +420,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 +497,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 +530,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->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 @@ -482,6 +569,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 +621,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 +630,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 +648,143 @@ 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_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, @@ -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.c.orig b/libswc/xdg_shell.c.orig @@ -0,0 +1,1075 @@ +/* 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/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/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 @@ -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/patches/borderAbleToGoRoundDecor.patch b/patches/borderAbleToGoRoundDecor.patch @@ -0,0 +1,159 @@ +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 @@ -0,0 +1,240 @@ +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/dragAndDrop.patch b/patches/dragAndDrop.patch @@ -0,0 +1,515 @@ +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 @@ -0,0 +1,221 @@ +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 @@ -0,0 +1,78 @@ +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/keyboardResetTellsModifiers.patch b/patches/keyboardResetTellsModifiers.patch @@ -0,0 +1,43 @@ +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 @@ -0,0 +1,129 @@ +--- 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/launchEscapeKeys.patch b/patches/launchEscapeKeys.patch @@ -0,0 +1,238 @@ +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 @@ -0,0 +1,232 @@ +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 @@ -0,0 +1,241 @@ +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 @@ -0,0 +1,127 @@ +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 @@ -0,0 +1,208 @@ +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 @@ -0,0 +1,2052 @@ +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 @@ -0,0 +1,18 @@ +--- 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 @@ -0,0 +1,33 @@ +--- 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 @@ -0,0 +1,86 @@ +--- 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 @@ -0,0 +1,15 @@ +--- 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 @@ -0,0 +1,81 @@ +--- 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 @@ -0,0 +1,83 @@ +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 @@ -0,0 +1,115 @@ +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 @@ -0,0 +1,64 @@ +--- 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 @@ -0,0 +1,127 @@ +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 @@ -0,0 +1,45 @@ +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 @@ -0,0 +1,113 @@ +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/videoBackendsAtRuntime.patch b/patches/videoBackendsAtRuntime.patch @@ -0,0 +1,665 @@ +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 @@ -0,0 +1,322 @@ +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 @@ -0,0 +1,40 @@ +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 + diff --git 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,