~ruther/dwl

717ee61e04b8a7456d9143949e5bcad2adc2dc4a — Rutherther a month ago c5333e4 + be2bd72
Merge remote-tracking branch 'upstream/main' into v0.10-dev/patched-keycodes

Resolve conflicts with upstream's transactions, keysym bindings, IME,
image capture and single-TU build while keeping the patch set:
- keep keycode-based bindings and modes, adopt consumed[] release
  suppression, IME key forwarding and locked check in keyrepeat
- skip ghost snapshot for swallowed clients in unmapnotify
- keep applybounds-relative after upstream's size hint clamping
- build dwl-ipc-unstable-v2-protocol.c in the single-TU Makefile
- rename IPC local focused_client to avoid shadowing new global

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed, 1656 insertions(+), 425 deletions(-)

M CHANGELOG.md
M Makefile
M README.md
M client.h
M config.def.h
M config.mk
M dwl.c
A ime.h
D util.c
M util.h
M CHANGELOG.md => CHANGELOG.md +90 -7
@@ 1,12 1,98 @@
# Changelog

* [Unreleased](#unreleased)
* [0.9](#0.9)
* [0.8](#0.8)
* [0.7](#0.7)
* [0.6](#0.6)
* [0.5](#0.5)


## Unreleased
## 0.9

### Added

* Add axis mouse bindings ([#652][652])
* Add support for the ext-data-control protocol ([#1184][1184])
* Add support for the ext-foreign-toplevel-list protocol ([#1223][1223])
* Add support for the ext-image-copy-capture and ext-image-capture-source protocols (screensharing) ([#1218][1218], [#1223][1223], [#1224][1224])
* Add support for version 4 of the layer shell protocol (on-demand keyboard interactivity) ([#674][674])
* Add support for wlr-text-input and wlr-input-method protocols (IME) ([#1219][1219])
* Snap windows to edges when moving or resizing them ([#608][608])

[1184]: https://codeberg.org/dwl/dwl/pulls/1184
[1218]: https://codeberg.org/dwl/dwl/pulls/1218
[1219]: https://codeberg.org/dwl/dwl/pulls/1219
[1223]: https://codeberg.org/dwl/dwl/pulls/1223
[1224]: https://codeberg.org/dwl/dwl/pulls/1224
[608]: https://codeberg.org/dwl/dwl/pulls/608
[652]: https://codeberg.org/dwl/dwl/pulls/652
[674]: https://codeberg.org/dwl/dwl/pulls/674


### Changed

* Update wlroots to version 0.20 ([#1218][1218])
* Use modifier-independent key symbols in config.h keybindings ([#1075][1075])

[1075]: https://codeberg.org/dwl/dwl/pulls/1075
[1218]: https://codeberg.org/dwl/dwl/pulls/1218


### Fixed

* Apply a minimum window size of 1x1 ([#1231][1231])
* Close Xwayland submenus when changing tag or focus ([#1233][1233])
* Don't make Firefox draw rounded corners when it is opened ([#d1ebf3c][d1ebf3c])
* Don't send key release events to the wrong client ([#1108][1108])
* Fix a crash when closing mpv ([#1220][1220])
* Fix a crash when destroying an idle-inhibitor ([#1214][1214])
* Fix choppy resizing on newer wlroots ([#1280][1280])
* Fix the position of certain sub-menus in Xwayland applications ([#1232][1232])
* Keep the focused border color on the regular client when opening a layer shell surface like wmenu ([#6013835][6013835])
* Let keybindings focus and tag monitors above and below ([#5fd19fe][5fd19fe])
* Preserve asleep disabled outputs in output config ([#1198][1198])
* Respect size hints ([#4de32d2][4de32d2])
* Set seat capabilities for virtual keyboards and pointers when there is no other input device ([#99cdc9a][99cdc9a])
* Show popups in fullscreen Xwayland applications ([#1fe1b4e][1fe1b4e])
* Show the correct cursor after moving and resizing ([#8ad2c92][8ad2c92])

[1108]: https://codeberg.org/dwl/dwl/pulls/1108
[1198]: https://codeberg.org/dwl/dwl/pulls/1198
[1214]: https://codeberg.org/dwl/dwl/pulls/1214
[1220]: https://codeberg.org/dwl/dwl/pulls/1220
[1231]: https://codeberg.org/dwl/dwl/pulls/1231
[1232]: https://codeberg.org/dwl/dwl/pulls/1232
[1233]: https://codeberg.org/dwl/dwl/pulls/1233
[1280]: https://codeberg.org/dwl/dwl/pulls/1280
[1fe1b4e]: https://codeberg.org/dwl/dwl/commit/1fe1b4e
[4de32d2]: https://codeberg.org/dwl/dwl/commit/4de32d2
[5fd19fe]: https://codeberg.org/dwl/dwl/commit/5fd19fe
[6013835]: https://codeberg.org/dwl/dwl/commit/6013835
[8ad2c92]: https://codeberg.org/dwl/dwl/commit/8ad2c92
[99cdc9a]: https://codeberg.org/dwl/dwl/commit/99cdc9a
[d1ebf3c]: https://codeberg.org/dwl/dwl/commit/d1ebf3c


### Contributors


* A Frederick Christensen
* Alex Denes
* Andrea Chiavazza
* Diego Viola
* Guido Cella
* Leonardo Hernández Hernández
* Peter Hofmann
* Siva Mahadevan
* julmajustus
* klim
* save196
* sewn
* thanatos


## 0.8

### Added

* Support for the linux-drm-syncobj-v1 protocol ([wlroots!4715][wlroots!4715], [#685][685])


@@ 17,15 103,12 @@
[646]: https://codeberg.org/dwl/dwl/pulls/646


### Changed
### Deprecated
### Removed
### Fixed

* Crash when a client is created while all outputs are disabled.

### Security
### Contributors

Most changes in this release are not listed. See the commits for details.


## 0.7

M Makefile => Makefile +10 -13
@@ 5,7 5,7 @@ include config.mk

# flags for compiling
DWLCPPFLAGS = -I. -DWLR_USE_UNSTABLE -D_POSIX_C_SOURCE=200809L \
	-DVERSION=\"$(VERSION)\" $(XWAYLAND)
	-DVERSION=\"$(VERSION)\" $(XWAYLAND) $(CPPFLAGS)
DWLDEVCFLAGS = -g -Wpedantic -Wall -Wextra -Wdeclaration-after-statement \
	-Wno-unused-parameter -Wshadow -Wunused-macros -Werror=strict-prototypes \
	-Werror=implicit -Werror=return-type -Werror=incompatible-pointer-types \


@@ 17,14 17,12 @@ DWLCFLAGS = `$(PKG_CONFIG) --cflags $(PKGS)` $(WLR_INCS) $(DWLCPPFLAGS) $(DWLDEV
LDLIBS    = `$(PKG_CONFIG) --libs $(PKGS)` $(WLR_LIBS) -lm $(LIBS)

all: dwl
dwl: dwl.o util.o dwl-ipc-unstable-v2-protocol.o
	$(CC) dwl.o util.o dwl-ipc-unstable-v2-protocol.o $(DWLCFLAGS) $(LDFLAGS) $(LDLIBS) -o $@
dwl.o: dwl.c client.h config.h config.mk cursor-shape-v1-protocol.h \
dwl: dwl.c client.h config.h ime.h util.h config.mk cursor-shape-v1-protocol.h \
	ext-image-copy-capture-v1-protocol.h \
	pointer-constraints-unstable-v1-protocol.h wlr-layer-shell-unstable-v1-protocol.h \
	wlr-output-power-management-unstable-v1-protocol.h xdg-shell-protocol.h \
	dwl-ipc-unstable-v2-protocol.h
util.o: util.c util.h
dwl-ipc-unstable-v2-protocol.o: dwl-ipc-unstable-v2-protocol.c dwl-ipc-unstable-v2-protocol.h
	dwl-ipc-unstable-v2-protocol.h dwl-ipc-unstable-v2-protocol.c
	$(CC) dwl.c dwl-ipc-unstable-v2-protocol.c $(DWLCFLAGS) $(LDFLAGS) $(LDLIBS) -o $@

# wayland-scanner is a tool which generates C headers and rigging for Wayland
# protocols, which are specified in XML. wlroots requires you to rig these up


@@ 35,6 33,9 @@ WAYLAND_PROTOCOLS = `$(PKG_CONFIG) --variable=pkgdatadir wayland-protocols`
cursor-shape-v1-protocol.h:
	$(WAYLAND_SCANNER) enum-header \
		$(WAYLAND_PROTOCOLS)/staging/cursor-shape/cursor-shape-v1.xml $@
ext-image-copy-capture-v1-protocol.h:
	$(WAYLAND_SCANNER) enum-header \
		$(WAYLAND_PROTOCOLS)/staging/ext-image-copy-capture/ext-image-copy-capture-v1.xml $@
pointer-constraints-unstable-v1-protocol.h:
	$(WAYLAND_SCANNER) enum-header \
		$(WAYLAND_PROTOCOLS)/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml $@


@@ 57,12 58,12 @@ dwl-ipc-unstable-v2-protocol.c:
config.h:
	cp config.def.h $@
clean:
	rm -f dwl *.o *-protocol.h
	rm -f dwl *.o *-protocol.h *-protocol.c

dist: clean
	mkdir -p dwl-$(VERSION)
	cp -R LICENSE* Makefile CHANGELOG.md README.md client.h config.def.h \
		config.mk protocols dwl.1 dwl.c util.c util.h dwl.desktop \
		config.mk protocols dwl.1 dwl.c ime.h util.h dwl.desktop \
		dwl-$(VERSION)
	tar -caf dwl-$(VERSION).tar.gz dwl-$(VERSION)
	rm -rf dwl-$(VERSION)


@@ 81,7 82,3 @@ install: dwl
uninstall:
	rm -f $(DESTDIR)$(PREFIX)/bin/dwl $(DESTDIR)$(MANDIR)/man1/dwl.1 \
		$(DESTDIR)$(DATADIR)/wayland-sessions/dwl.desktop

.SUFFIXES: .c .o
.c.o:
	$(CC) $(CPPFLAGS) $(DWLCFLAGS) -o $@ -c $<

M README.md => README.md +37 -21
@@ 1,15 1,16 @@
# dwl - dwm for Wayland

Join us on our IRC channel: [#dwl on Libera Chat]  
Or on the community-maintained [Discord server].
Join us on our [Discord server]  
Or Matrix: [#dwl-official:matrix.org]  
Or on our IRC channel: [#dwl on Libera Chat]

dwl is a compact, hackable compositor for [Wayland] based on [wlroots]. It is
intended to fill the same space in the Wayland world that dwm does in X11,
intended to fill the same space in the Wayland world that [dwm] does in X11,
primarily in terms of functionality, and secondarily in terms of
philosophy. Like dwm, dwl is:
philosophy. Like [dwm], dwl is:

- Easy to understand, hack on, and extend with patches
- One C source file (or a very small number) configurable via `config.h`
- One C source file configurable via `config.h`
- Tied to as few external dependencies as possible

## Getting Started:


@@ 23,12 24,14 @@ with each release on the [release] page

### Development branch [main]
Active development progresses on the `main` branch. The `main` branch is built
against a late (and often changing) git commit of wlroots. While the adventurous
are welcome to use `main`, it is a rocky road. Using `main` requires that the
user be willing to chase git commits of wlroots. Testing development pull
requests may involve merging unmerged pull requests in [wlroots]' git repository
and/or git commits of wayland.
  
against the latest release of [wlroots]. PRs should target this branch unless they
depend on functionality that is not in the current release of `wlroots`.

### Preview branch [wlroots-next]
The `wlroots-next` branch is built against the git version of [wlroots], which
is unstable and changes frequently. PRs requiring functionality from the git
version of `wlroots` should target this branch.

### Building dwl
dwl has the following dependencies:
- libinput


@@ 55,11 58,11 @@ To enable XWayland, you should uncomment its flags in `config.mk`.
## Configuration

All configuration is done by editing `config.h` and recompiling, in the same
manner as dwm. There is no way to separately restart the window manager in
manner as [dwm]. There is no way to separately restart the window manager in
Wayland without restarting the entire display server, so any changes will take
effect the next time dwl is executed.

As in the dwm community, we encourage users to share patches they have
As in the [dwm] community, we encourage users to share patches they have
created. Check out the [dwl-patches] repository!

## Running dwl


@@ 118,17 121,26 @@ script with the line

To get a list of status bars that work with dwl consult our [wiki].

### (Known) Java nonreparenting WM issue
Certain IDEs don't display correctly unless an environmental variable for Java AWT
indicates that the WM is nonreparenting.

For some Java AWT-based IDEs, such as Xilinx Vivado and Microchip MPLAB X, the
following environment variable needs to be set before running the IDE or dwl:

    export _JAVA_AWT_WM_NONREPARENTING=1

## Replacements for X applications

You can find a [list of useful resources on our wiki].

## Background

dwl is not meant to provide every feature under the sun. Instead, like dwm, it
dwl is not meant to provide every feature under the sun. Instead, like [dwm], it
sticks to features which are necessary, simple, and straightforward to implement
given the base on which it is built. Implemented default features are:

- Any features provided by dwm/Xlib: simple window borders, tags, keybindings,
- Any features provided by [dwm]/Xlib: simple window borders, tags, keybindings,
  client rules, mouse move/resize. Providing a built-in status bar is an
  exception to this goal, to avoid dependencies on font rendering and/or drawing
  libraries when an external bar could work well.


@@ 145,18 157,15 @@ given the base on which it is built. Implemented default features are:
- Layer shell popups (used by Waybar)
- Damage tracking provided by scenegraph API

Given the Wayland architecture, dwl has to implement features from dwm **and**
Given the Wayland architecture, dwl has to implement features from [dwm] **and**
the xorg-server. Because of this, it is impossible to maintain the original
project goal of 2000 SLOC and have a reasonably complete compositor with
features comparable to dwm. However, this does not mean that the code will grow
features comparable to [dwm]. However, this does not mean that the code will grow
indiscriminately. We will try to keep the code as small as possible.

Features under consideration (possibly as patches) are:

- Protocols made trivial by wlroots
- Implement the text-input and input-method protocols to support IME once ibus
  implements input-method v2 (see https://github.com/ibus/ibus/pull/2256 and
  https://codeberg.org/dwl/dwl/pulls/235)

Feature *non-goals* for the main codebase include:



@@ 172,7 181,7 @@ developers. This was made possible in many cases by looking at how sway
accomplished something, then trying to do the same in as suckless a way as
possible.

Many thanks to suckless.org and the dwm developers and community for the
Many thanks to suckless.org and the [dwm] developers and community for the
inspiration, and to the various contributors to the project, including:

- **Devin J. Pohly for creating and nurturing the fledgling project**


@@ 181,8 190,13 @@ inspiration, and to the various contributors to the project, including:
  and for helping to keep the project running
- Stivvo for output management and fullscreen support, and patch maintenance

Also, thanks to our current lead developers:

- @thanatos
- @guidocella

[wlroots]: https://gitlab.freedesktop.org/wlroots
[dwm]: https://dwm.suckless.org/
[`systemd --user`]: https://wiki.archlinux.org/title/Systemd/User
[#dwl on Libera Chat]: https://web.libera.chat/?channels=#dwl
[0.7-rc1]: https://codeberg.org/dwl/dwl/releases/tag/v0.7-rc1


@@ 192,6 206,7 @@ inspiration, and to the various contributors to the project, including:
[dwl-patches]: https://codeberg.org/dwl/dwl-patches
[list of useful resources on our wiki]: https://codeberg.org/dwl/dwl/wiki/Home#migrating-from-x
[main]: https://codeberg.org/dwl/dwl/src/branch/main
[wlroots-next]: https://codeberg.org/dwl/dwl/src/branch/wlroots-next
[release]: https://codeberg.org/dwl/dwl/releases
[runit]: http://smarden.org/runit/faq.html#userservices
[s6]: https://skarnet.org/software/s6/


@@ 199,3 214,4 @@ inspiration, and to the various contributors to the project, including:
[wiki]: https://codeberg.org/dwl/dwl/wiki/Home#compatible-status-bars
[Discord server]: https://discord.gg/jJxZnrGPWN
[Wayland]: https://wayland.freedesktop.org/
[#dwl-official:matrix.org]: https://matrix.to/#/#dwl-official:matrix.org

M client.h => client.h +63 -60
@@ 15,6 15,32 @@ client_is_x11(Client *c)
	return 0;
}

static inline void
client_get_size_hints(Client *c, struct wlr_box *max, struct wlr_box *min)
{
	struct wlr_xdg_toplevel_state *state;

#ifdef XWAYLAND
	if (client_is_x11(c)) {
		xcb_size_hints_t *size_hints = c->surface.xwayland->size_hints;
		if (size_hints) {
			max->width = size_hints->max_width;
			max->height = size_hints->max_height;
			min->width = size_hints->min_width;
			min->height = size_hints->min_height;
		}
		return;
	}
#endif

	state = &c->surface.xdg->toplevel->current;
	max->width = state->max_width;
	max->height = state->max_height;
	min->width = state->min_width;
	min->height = state->min_height;
}


static inline struct wlr_surface *
client_surface(Client *c)
{


@@ 175,6 201,7 @@ client_get_geometry(Client *c, struct wlr_box *geom)
	}
#endif
	*geom = c->surface.xdg->geometry;
	geom->x = geom->y = 0;
}

static inline Client *


@@ 218,13 245,12 @@ client_get_title(Client *c)
static inline int
client_is_float_type(Client *c)
{
	struct wlr_xdg_toplevel *toplevel;
	struct wlr_xdg_toplevel_state state;
	struct wlr_box min = {0}, max = {0};
	client_get_size_hints(c, &max, &min);

#ifdef XWAYLAND
	if (client_is_x11(c)) {
		struct wlr_xwayland_surface *surface = c->surface.xwayland;
		xcb_size_hints_t *size_hints = surface->size_hints;
		if (surface->modal)
			return 1;



@@ 235,59 261,13 @@ client_is_float_type(Client *c)
			return 1;
		}

		return size_hints && size_hints->min_width > 0 && size_hints->min_height > 0
			&& (size_hints->max_width == size_hints->min_width
				|| size_hints->max_height == size_hints->min_height);
		return min.width > 0 && min.height > 0 &&
			(min.width == max.width || min.height == max.height);
	}
#endif

	toplevel = c->surface.xdg->toplevel;
	state = toplevel->current;
	return toplevel->parent || (state.min_width != 0 && state.min_height != 0
		&& (state.min_width == state.max_width
			|| state.min_height == state.max_height));
}

static inline int
client_is_rendered_on_mon(Client *c, Monitor *m)
{
	/* This is needed for when you don't want to check formal assignment,
	 * but rather actual displaying of the pixels.
	 * Usually VISIBLEON suffices and is also faster. */
	struct wlr_surface_output *s;
	int unused_lx, unused_ly;
	if (!wlr_scene_node_coords(&c->scene->node, &unused_lx, &unused_ly))
		return 0;
	wl_list_for_each(s, &client_surface(c)->current_outputs, link)
		if (s->output == m->wlr_output)
			return 1;
	return 0;
}

static inline int
client_is_stopped(Client *c)
{
	int pid;
	siginfo_t in = {0};
#ifdef XWAYLAND
	if (client_is_x11(c))
		return 0;
#endif

	wl_client_get_credentials(c->surface.xdg->client->client, &pid, NULL, NULL);
	if (waitid(P_PID, pid, &in, WNOHANG|WCONTINUED|WSTOPPED|WNOWAIT) < 0) {
		/* This process is not our child process, while is very unlikely that
		 * it is stopped, in order to do not skip frames, assume that it is. */
		if (errno == ECHILD)
			return 1;
	} else if (in.si_pid) {
		if (in.si_code == CLD_STOPPED || in.si_code == CLD_TRAPPED)
			return 1;
		if (in.si_code == CLD_CONTINUED)
			return 0;
	}

	return 0;
	return c->surface.xdg->toplevel->parent || (min.width > 0 && min.height > 0 &&
			(min.width == max.width || min.height == max.height));
}

static inline int


@@ 303,11 283,18 @@ client_is_unmanaged(Client *c)
static inline void
client_notify_enter(struct wlr_surface *s, struct wlr_keyboard *kb)
{
	if (kb)
		wlr_seat_keyboard_notify_enter(seat, s, kb->keycodes,
				kb->num_keycodes, &kb->modifiers);
	else
	uint32_t filtered[WLR_KEYBOARD_KEYS_CAP];
	size_t size = 0;
	if (!kb) {
		wlr_seat_keyboard_notify_enter(seat, s, NULL, 0, NULL);
		return;
	}
	for (size_t i = 0; i < kb->num_keycodes; i++) {
		uint32_t key = kb->keycodes[i];
		if (!consumed[key])
			filtered[size++] = key;
	}
	wlr_seat_keyboard_notify_enter(seat, s, filtered, size, &kb->modifiers);
}

static inline void


@@ 349,6 336,7 @@ client_set_scale(struct wlr_surface *s, float scale)
	wlr_surface_set_preferred_buffer_scale(s, (int32_t)ceilf(scale));
}

/* Returns nonzero if the client has to commit a buffer with the new size */
static inline uint32_t
client_set_size(Client *c, uint32_t width, uint32_t height)
{


@@ 356,16 344,31 @@ client_set_size(Client *c, uint32_t width, uint32_t height)
	if (client_is_x11(c)) {
		wlr_xwayland_surface_configure(c->surface.xwayland,
				c->geom.x + c->bw, c->geom.y + c->bw, width, height);
		return 0;
		return (int)width != client_surface(c)->current.width
				|| (int)height != client_surface(c)->current.height;
	}
#endif
	if ((int32_t)width == c->surface.xdg->toplevel->current.width
			&& (int32_t)height == c->surface.xdg->toplevel->current.height)
	if ((int32_t)width == c->surface.xdg->toplevel->scheduled.width
			&& (int32_t)height == c->surface.xdg->toplevel->scheduled.height)
		return 0;
	return wlr_xdg_toplevel_set_size(c->surface.xdg->toplevel, (int32_t)width, (int32_t)height);
}

static inline void
client_send_frame_done_iterator(struct wlr_surface *s, int sx, int sy, void *data)
{
	wlr_surface_send_frame_done(s, data);
}

static inline void
client_send_frame_done(Client *c)
{
	struct timespec now;
	clock_gettime(CLOCK_MONOTONIC, &now);
	wlr_surface_for_each_surface(client_surface(c), client_send_frame_done_iterator, &now);
}

static inline void
client_set_tiled(Client *c, uint32_t edges)
{
#ifdef XWAYLAND

M config.def.h => config.def.h +11 -6
@@ 7,11 7,11 @@
static const int sloppyfocus               = 1;  /* focus follows mouse */
static const int bypass_surface_visibility = 0;  /* 1 means idle inhibitors will disable idle tracking even if it's surface isn't visible  */
static const unsigned int borderpx         = 1;  /* border pixel of windows */
static const unsigned int snap             = 32; /* snap pixel */
static const float rootcolor[]             = COLOR(0x222222ff);
static const float bordercolor[]           = COLOR(0x444444ff);
static const float focuscolor[]            = COLOR(0x005577ff);
static const float urgentcolor[]           = COLOR(0xff0000ff);
/* This conforms to the xdg-protocol. Set the alpha to zero to restore the old behavior */
static const float fullscreen_bg[]         = {0.0f, 0.0f, 0.0f, 1.0f}; /* You can also use glsl colors */

enum {


@@ 27,7 27,6 @@ const char *modes_labels[] = {
/* logging */
static int log_level = WLR_ERROR;

/* NOTE: ALWAYS keep a rule declared even if you don't use rules (e.g leave at least one example) */
static const Rule rules[] = {
	/* app_id             title       tags mask     isfloating  isterm  noswallow allow_activation monitor scratchkey */
	/* examples: */


@@ 49,10 48,8 @@ static const Layout layouts[] = {

/* monitors */
/* (x=-1, y=-1) is reserved as an "autoconfigure" monitor position indicator
 * WARNING: negative values other than (-1, -1) cause problems with Xwayland clients
 * https://gitlab.freedesktop.org/xorg/xserver/-/issues/899
*/
/* NOTE: ALWAYS add a fallback rule, even if you are completely sure it won't be used */
 * WARNING: negative values other than (-1, -1) cause problems with Xwayland clients due to
 * https://gitlab.freedesktop.org/xorg/xserver/-/issues/899 */
static const MonitorRule monrules[] = {
	/* name   scale  rotate/reflect                x    y */
	/* example of a HiDPI laptop monitor:


@@ 223,3 220,11 @@ bool applycustomrules(const char *appid, const char *title, Rule *r)
  // TODO: apply any rules you wish
  return false;
}

static const Axis axes[] = {
	/* example of volume control:
	{ MODKEY, AxisUp,   spawn, SHCMD("volume-up_EXAMPLE") },
	{ MODKEY, AxisDown, spawn, SHCMD("volume-down_EXAMPLE") }, */
	{ 0, 0, NULL, {0} },
	/* does nothing, but the array cannot be empty */
};

M config.mk => config.mk +7 -7
@@ 1,4 1,4 @@
_VERSION = 0.8-dev
_VERSION = 0.10-dev
VERSION  = `git describe --tags --dirty 2>/dev/null || echo $(_VERSION)`

PKG_CONFIG = pkg-config


@@ 8,8 8,8 @@ PREFIX = /usr/local
MANDIR = $(PREFIX)/share/man
DATADIR = $(PREFIX)/share

WLR_INCS = `$(PKG_CONFIG) --cflags wlroots-0.19`
WLR_LIBS = `$(PKG_CONFIG) --libs wlroots-0.19`
WLR_INCS = `$(PKG_CONFIG) --cflags wlroots-0.20`
WLR_LIBS = `$(PKG_CONFIG) --libs wlroots-0.20`

# Allow using an alternative wlroots installation
# This has to have all the includes required by wlroots, e.g:


@@ 17,12 17,12 @@ WLR_LIBS = `$(PKG_CONFIG) --libs wlroots-0.19`
#WLR_INCS = -I/usr/include/pixman-1 -I/usr/include/elogind -I/usr/include/libdrm \
#	-I$(PWD)/wlroots/include
# Set -rpath to avoid using the wrong library.
#WLR_LIBS = -Wl,-rpath,$(PWD)/wlroots/build -L$(PWD)/wlroots/build -lwlroots-0.19
#WLR_LIBS = -Wl,-rpath,$(PWD)/wlroots/build -L$(PWD)/wlroots/build -lwlroots-0.20

# Assuming you ran "meson setup --prefix ${PWD}/0.19 build && ninja -C build install"
# Assuming you ran "meson setup --prefix ${PWD}/0.20 build && ninja -C build install"
#WLR_INCS = -I/usr/include/pixman-1 -I/usr/include/elogind -I/usr/include/libdrm \
#	-I$(PWD)/wlroots/0.19/include/wlroots-0.19
#WLR_LIBS = -Wl,-rpath,$(PWD)/wlroots/0.19/lib64 -L$(PWD)/wlroots/0.19/lib64 -lwlroots-0.19
#	-I$(PWD)/wlroots/0.20/include/wlroots-0.20
#WLR_LIBS = -Wl,-rpath,$(PWD)/wlroots/0.20/lib64 -L$(PWD)/wlroots/0.20/lib64 -lwlroots-0.20

XWAYLAND =
XLIBS =

M dwl.c => dwl.c +731 -257
@@ 3,6 3,7 @@
 */
#include <getopt.h>
#include <libinput.h>
#include <limits.h>
#include <linux/input-event-codes.h>
#include <math.h>
#include <signal.h>


@@ 16,6 17,7 @@
#include <wlr/backend.h>
#include <wlr/backend/libinput.h>
#include <wlr/render/allocator.h>
#include <wlr/render/swapchain.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_alpha_modifier_v1.h>
#include <wlr/types/wlr_compositor.h>


@@ 25,6 27,10 @@
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_drm.h>
#include <wlr/types/wlr_export_dmabuf_v1.h>
#include <wlr/types/wlr_ext_data_control_v1.h>
#include <wlr/types/wlr_ext_image_capture_source_v1.h>
#include <wlr/types/wlr_ext_image_copy_capture_v1.h>
#include <wlr/types/wlr_ext_foreign_toplevel_list_v1.h>
#include <wlr/types/wlr_fractional_scale_v1.h>
#include <wlr/types/wlr_gamma_control_v1.h>
#include <wlr/types/wlr_idle_inhibit_v1.h>


@@ 40,6 46,7 @@
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_output_management_v1.h>
#include <wlr/types/wlr_output_power_management_v1.h>
#include <wlr/types/wlr_output_swapchain_manager.h>
#include <wlr/types/wlr_pointer.h>
#include <wlr/types/wlr_pointer_constraints_v1.h>
#include <wlr/types/wlr_presentation_time.h>


@@ 72,16 79,13 @@
#endif

#include "dwl-ipc-unstable-v2-protocol.h"
#include "xdg-shell-protocol.h"
#include "util.h"

/* macros */
#define MAX(A, B)               ((A) > (B) ? (A) : (B))
#define MIN(A, B)               ((A) < (B) ? (A) : (B))
#define CLEANMASK(mask)         (mask & ~WLR_MODIFIER_CAPS)
#define VISIBLEON(C, M)         ((M) && (C)->mon == (M) && ((((C)->tags & (M)->tagset[(M)->seltags])) || (C)->issticky))
#define SVISIBLEON(C, M)        ((M) && (C)->mon && (((C)->tags & (M)->tagset[(M)->seltags]) && !(C)->issticky) || ((C)->issticky && (C)->mon == (M)))
#define LENGTH(X)               (sizeof X / sizeof X[0])
#define END(A)                  ((A) + LENGTH(A))
#define TAGMASK                 ((1u << TAGCOUNT) - 1)
#define LISTEN(E, L, H)         wl_signal_add((E), ((L)->notify = (H), (L)))
#define LISTEN_STATIC(E, H)     do { struct wl_listener *_l = ecalloc(1, sizeof(*_l)); _l->notify = (H); wl_signal_add((E), _l); } while (0)


@@ 89,7 93,9 @@
/* enums */
enum { CurNormal, CurPressed, CurMove, CurResize }; /* cursor */
enum { XDGShell, LayerShell, X11 }; /* client types */
enum { LyrBg, LyrBottom, LyrTile, LyrFloat, LyrTop, LyrFS, LyrOverlay, LyrBlock, NUM_LAYERS }; /* scene layers */
enum { LyrBg, LyrBottom, LyrTile, LyrFloat, LyrTop, LyrFS, LyrUnmanaged, LyrOverlay,
	LyrIMPopup, LyrBlock, NUM_LAYERS }; /* scene layers */
enum { AxisUp, AxisRight, AxisDown, AxisLeft };

typedef union {
	int i;


@@ 100,6 106,13 @@ typedef union {

typedef struct {
	unsigned int mod;
	unsigned int dir;
	void (*func)(const Arg *);
	const Arg arg;
} Axis;

typedef struct {
	unsigned int mod;
	unsigned int button;
	void (*func)(const Arg *);
	const Arg arg;


@@ 111,20 124,32 @@ typedef struct Client Client;
struct Client {
	/* Must keep this field first */
	unsigned int type; /* XDGShell or X11* */
	/* Must keep these three elements in this order */
	struct wlr_box geom; /* layout-relative, includes border */
	Monitor *mon;

	Monitor *mon;
	struct wlr_scene_tree *scene;
	struct wlr_scene_rect *border[4]; /* top, bottom, left, right */
	struct wlr_scene_tree *scene_surface;
	struct wl_list link;
	struct wl_list flink;
	struct wlr_box geom; /* layout-relative, includes border */
	struct wlr_box prev; /* layout-relative, includes border */
	struct wlr_box current; /* geom currently shown in the scene graph */
	struct wlr_scene_node *snapshot; /* content shown while resizing */
	uint32_t resize_serial; /* nonzero while waiting for a resized buffer */
	struct wlr_box bounds; /* only width and height are used */
	struct wlr_box prev_bounds;
	union {
		struct wlr_xdg_surface *xdg;
		struct wlr_xwayland_surface *xwayland;
	} surface;
	struct wlr_xdg_toplevel_decoration_v1 *decoration;
	struct wlr_ext_foreign_toplevel_handle_v1 *foreign_toplevel_handle;
	struct wlr_scene *image_capture_scene;
	struct wlr_ext_image_capture_source_v1 *image_capture_source;
	union {
		struct wlr_scene_tree *image_capture_tree;
		struct wlr_scene_surface *image_capture_scene_surface;
	} capture;
	struct wl_listener commit;
	struct wl_listener map;
	struct wl_listener maximize;


@@ 134,22 159,19 @@ struct Client {
	struct wl_listener fullscreen;
	struct wl_listener set_decoration_mode;
	struct wl_listener destroy_decoration;
	struct wlr_box prev; /* layout-relative, includes border */
	struct wlr_box bounds;
	struct wlr_box prev_bounds;
#ifdef XWAYLAND
	struct wl_listener activate;
	struct wl_listener associate;
	struct wl_listener dissociate;
	struct wl_listener configure;
	struct wl_listener set_geometry;
	struct wl_listener set_hints;
#endif
	unsigned int bw;
	uint32_t tags;
  int isfloating, isurgent, isfullscreen, issticky, isterm, noswallow;
	int isfloating, isurgent, isfullscreen, issticky, isterm, noswallow;
	char scratchkey;
	int allow_activation;
	uint32_t resize; /* configure serial of a pending resize */
	pid_t pid;
	Client *swallowing, *swallowedby;
};


@@ 175,6 197,7 @@ typedef struct {
typedef struct {
	struct wl_list link;
	struct wlr_keyboard_group *wlr_group;
	struct wlr_keyboard *virtual_keyboard;

	xkb_keycode_t keycode;
	uint32_t mods; /* invalid if keycode == 0 */


@@ 216,6 239,7 @@ struct Monitor {
	struct wl_listener frame;
	struct wl_listener destroy;
	struct wl_listener request_state;
	struct wl_listener commit_lock_surface;
	struct wl_listener destroy_lock_surface;
	struct wlr_session_lock_surface_v1 *lock_surface;
	struct wlr_box m; /* monitor area, layout-relative */


@@ 275,6 299,7 @@ typedef struct {

/* function declarations */
static void applybounds(Client *c, struct wlr_box *bbox);
static void applygeom(Client *c);
static void applypertag(Monitor *m);
static void applyrules(Client *c);
static void arrange(Monitor *m);


@@ 284,6 309,7 @@ static void arrangelayers(Monitor *m);
static void attachclients(Monitor *m);
static void axisnotify(struct wl_listener *listener, void *data);
static void buttonpress(struct wl_listener *listener, void *data);
static void capturerequest(struct wl_listener *listener, void *data);
static void chvt(const Arg *arg);
static void checkidleinhibitor(struct wlr_surface *exclude);
static void cleanup(void);


@@ 291,6 317,7 @@ static void cleanupmon(struct wl_listener *listener, void *data);
static void cleanuplisteners(void);
static void closemon(Monitor *m);
static void commitlayersurfacenotify(struct wl_listener *listener, void *data);
static void commitlocksurface(struct wl_listener *listener, void *data);
static void commitnotify(struct wl_listener *listener, void *data);
static void commitpopup(struct wl_listener *listener, void *data);
static void createdecoration(struct wl_listener *listener, void *data);


@@ 331,7 358,7 @@ static void dwl_ipc_output_set_client_tags(struct wl_client *client, struct wl_r
static void dwl_ipc_output_set_layout(struct wl_client *client, struct wl_resource *resource, uint32_t index);
static void dwl_ipc_output_set_tags(struct wl_client *client, struct wl_resource *resource, uint32_t tagmask, uint32_t toggle_tagset);
static void dwl_ipc_output_release(struct wl_client *client, struct wl_resource *resource);
static void focusclient(Client *c, int lift);
static void focusclient(Client *c, LayerSurface *l, int lift);
static void focusmon(const Arg *arg);
static void focusortogglematchingscratch(const Arg *arg);
static void focusortogglescratch(const Arg *arg);


@@ 348,8 375,7 @@ static void handlesig(int signo);
static void incnmaster(const Arg *arg);
static void inputdevice(struct wl_listener *listener, void *data);
static int keybinding(uint32_t mods, xkb_keycode_t keycode);
static int modekeybinding(uint32_t mods, xkb_keysym_t sym);
static int modekeybinding(uint32_t mods, xkb_keycode_t sym);
static int modekeybinding(uint32_t mods, xkb_keycode_t keycode);
static int is_inbounds(struct wlr_box *box, struct wlr_box *bounds);
static void keypress(struct wl_listener *listener, void *data);
static void keypressmod(struct wl_listener *listener, void *data);


@@ 361,6 387,7 @@ static void maximizenotify(struct wl_listener *listener, void *data);
static void monocle(Monitor *m);
static void movestack(const Arg *arg);
static void motionabsolute(struct wl_listener *listener, void *data);
static char **monoff(struct wlr_output *wlr_output);
static void motionnotify(uint32_t time, struct wlr_input_device *device, double sx,
		double sy, double sx_unaccel, double sy_unaccel);
static void motionrelative(struct wl_listener *listener, void *data);


@@ 368,7 395,7 @@ static void moveresize(const Arg *arg);
static void outputmgrapply(struct wl_listener *listener, void *data);
static void outputmgrapplyortest(struct wlr_output_configuration_v1 *config, int test);
static void outputmgrtest(struct wl_listener *listener, void *data);
static void pointerfocus(Client *c, struct wlr_surface *surface,
static void pointerfocus(Client *c, LayerSurface* l, struct wlr_surface *surface,
		double sx, double sy, uint32_t time);
static void printstatus(void);
static void powermgrsetmode(struct wl_listener *listener, void *data);


@@ 388,9 415,12 @@ static void setgamma(struct wl_listener *listener, void *data);
static void setlayout(const Arg *arg);
static void setmfact(const Arg *arg);
static void setmon(Client *c, Monitor *m, uint32_t newtags);
static void setmonoff(struct wlr_output *wlr_output, int off);
static void setpsel(struct wl_listener *listener, void *data);
static void setsel(struct wl_listener *listener, void *data);
static void setup(void);
static struct wlr_scene_node *snapshot(struct wlr_scene_node *node,
		struct wlr_scene_tree *parent, void *data);
static void spawn(const Arg *arg);
static void spawnscratch(const Arg *arg);
static void startdrag(struct wl_listener *listener, void *data);


@@ 405,6 435,11 @@ static void togglefullscreen(const Arg *arg);
static void togglescratch(const Arg *arg);
static void toggletag(const Arg *arg);
static void toggleview(const Arg *arg);
static void transactionapply(void);
static void transactioncheck(void *data);
static void transactioncommit(Client *c, int ready);
static void transactionschedule(void);
static int transactiontimeout(void *data);
static void unlocksession(struct wl_listener *listener, void *data);
static void unmaplayersurfacenotify(struct wl_listener *listener, void *data);
static void unmapnotify(struct wl_listener *listener, void *data);


@@ 430,7 465,7 @@ static void entermode(const Arg *arg);
static const char broken[] = "broken";
static pid_t child_pid = -1;
static int locked;
static void *exclusive_focus;
static Client *focused_client;
static struct wl_display *dpy;
static struct wl_event_loop *event_loop;
static struct wlr_backend *backend;


@@ 458,6 493,8 @@ static struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard_mgr;
static struct wlr_virtual_pointer_manager_v1 *virtual_pointer_mgr;
static struct wlr_cursor_shape_manager_v1 *cursor_shape_mgr;
static struct wlr_output_power_manager_v1 *power_mgr;
static struct wlr_ext_foreign_toplevel_list_v1 *foreign_toplevel_list;
static struct wlr_ext_foreign_toplevel_image_capture_source_manager_v1 *ext_foreign_toplevel_image_capture_source_manager_v1;

static struct wlr_pointer_constraints_v1 *pointer_constraints;
static struct wlr_relative_pointer_manager_v1 *relative_pointer_mgr;


@@ 491,6 528,17 @@ static struct zdwl_ipc_output_v2_interface dwl_output_implementation = {.release
static const int NORMAL = -1;
static int active_mode_index = NORMAL;

static int consumed[KEY_MAX + 1];

/* Transactions: the scene graph is updated only once all visible clients have
 * committed buffers with their new size, or after TRANSACTION_TIMEOUT ms */
#define TRANSACTION_TIMEOUT 100
static struct wl_event_source *transaction_idle;
static struct wl_event_source *transaction_timer;
static int transaction_waiting;
static struct wl_array ghosts; /* struct wlr_scene_node *, unmapped clients */
static struct wl_array offmons; /* char *, names of outputs turned off by clients */

/* global event handlers */
static struct wl_listener cursor_axis = {.notify = axisnotify};
static struct wl_listener cursor_button = {.notify = buttonpress};


@@ 519,14 567,18 @@ static struct wl_listener request_set_sel = {.notify = setsel};
static struct wl_listener request_set_cursor_shape = {.notify = setcursorshape};
static struct wl_listener request_start_drag = {.notify = requeststartdrag};
static struct wl_listener start_drag = {.notify = startdrag};
static struct wl_listener drag_icon_destroy = {.notify = destroydragicon};
static struct wl_listener new_session_lock = {.notify = locksession};
static struct wl_listener new_foreign_toplevel_capture_request = {.notify = capturerequest};

#ifdef XWAYLAND
static void activatex11(struct wl_listener *listener, void *data);
static void associatex11(struct wl_listener *listener, void *data);
static void commitx11(struct wl_listener *listener, void *data);
static void configurex11(struct wl_listener *listener, void *data);
static void createnotifyx11(struct wl_listener *listener, void *data);
static void dissociatex11(struct wl_listener *listener, void *data);
static void setgeometryx11(struct wl_listener *listener, void *data);
static void sethints(struct wl_listener *listener, void *data);
static void xwaylandready(struct wl_listener *listener, void *data);
static struct wl_listener new_xwayland_surface = {.notify = createnotifyx11};


@@ 554,14 606,29 @@ is_inbounds(struct wlr_box *box, struct wlr_box *bounds)
		box->x + box->width <= bounds->x + bounds->width && box->y + box->height <= bounds->y + bounds->height;
}

#include "ime.h"

/* function implementations */
void
applybounds(Client *c, struct wlr_box *bbox)
{
	/* set minimum possible */
	/* set minimum client size to 1 */
	c->geom.width = MAX(1 + 2 * (int)c->bw, c->geom.width);
	c->geom.height = MAX(1 + 2 * (int)c->bw, c->geom.height);

	if (!c->isfullscreen) {
		struct wlr_box min = {0}, max = {0};
		client_get_size_hints(c, &max, &min);
		c->geom.width = MAX(min.width + 2 * (int)c->bw, c->geom.width);
		c->geom.height = MAX(min.height + 2 * (int)c->bw, c->geom.height);
		/* Some clients set their max size to INT_MAX, which does not violate the
		 * protocol but it's unnecesary, as they can set their max size to zero. */
		if (max.width > 0 && 2 * (int)c->bw <= INT_MAX - max.width) /* Checks for overflow */
			c->geom.width = MIN(max.width + 2 * (int)c->bw, c->geom.width);
		if (max.height > 0 && 2 * (int)c->bw <= INT_MAX - max.height) /* Checks for overflow */
			c->geom.height = MIN(max.height + 2 * (int)c->bw, c->geom.height);
	}

	if (c->isfloating && c->prev_bounds.width && c->prev_bounds.height &&
		is_inbounds(&c->geom, &c->prev_bounds) && !is_inbounds(&c->geom, bbox)) {
		c->geom.x = bbox->x + (c->geom.x - c->prev_bounds.x) * (bbox->width / c->prev_bounds.width);


@@ 579,6 646,41 @@ applybounds(Client *c, struct wlr_box *bbox)
}

void
applygeom(Client *c)
{
	struct wlr_box clip;
	Client *p = client_get_parent(c);

	/* We move all floating clients to LyrTile while in floating layout
	 * to avoid "real" floating clients be always on top */
	wlr_scene_node_reparent(&c->scene->node, layers[c->isfullscreen
			|| (p && p->isfullscreen) ? LyrFS
			: c->isfloating && c->mon->lt[c->mon->sellt]->arrange ? LyrFloat
			: LyrTile]);

	c->current = c->geom;
	if (c->snapshot) {
		wlr_scene_node_destroy(c->snapshot);
		c->snapshot = NULL;
		wlr_scene_node_set_enabled(&c->scene_surface->node, 1);
	}

	/* Update scene-graph, including borders */
	wlr_scene_node_set_position(&c->scene->node, c->geom.x, c->geom.y);
	wlr_scene_node_set_position(&c->scene_surface->node, c->bw, c->bw);
	wlr_scene_rect_set_size(c->border[0], c->geom.width, c->bw);
	wlr_scene_rect_set_size(c->border[1], c->geom.width, c->bw);
	wlr_scene_rect_set_size(c->border[2], c->bw, c->geom.height - 2 * c->bw);
	wlr_scene_rect_set_size(c->border[3], c->bw, c->geom.height - 2 * c->bw);
	wlr_scene_node_set_position(&c->border[1]->node, 0, c->geom.height - c->bw);
	wlr_scene_node_set_position(&c->border[2]->node, 0, c->bw);
	wlr_scene_node_set_position(&c->border[3]->node, c->geom.width - c->bw, c->bw);

	client_get_clip(c, &clip);
	wlr_scene_subsurface_tree_set_clip(&c->scene_surface->node, &clip);
}

void
applyrules(Client *c)
{
	/* rule matching */


@@ 664,35 766,18 @@ arrange(Monitor *m)
		return;

	wl_list_for_each(c, &clients, link) {
		if (c->mon == m) {
			wlr_scene_node_set_enabled(&c->scene->node, VISIBLEON(c, m));
			client_set_suspended(c, !VISIBLEON(c, m));
		}
		if (c->mon == m)
			client_set_suspended(c, !VISIBLEON(c, m)
					&& (!c->image_capture_source
					|| wl_list_empty(&c->image_capture_source->resources)));
	}

	wlr_scene_node_set_enabled(&m->fullscreen_bg->node,
			(c = focustop(m)) && c->isfullscreen);

	strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, LENGTH(m->ltsymbol));

	/* We move all clients (except fullscreen and unmanaged) to LyrTile while
	 * in floating layout to avoid "real" floating clients be always on top */
	wl_list_for_each(c, &clients, link) {
		if (c->mon != m || c->scene->node.parent == layers[LyrFS])
			continue;

		wlr_scene_node_reparent(&c->scene->node,
				(!m->lt[m->sellt]->arrange && c->isfloating)
						? layers[LyrTile]
						: (m->lt[m->sellt]->arrange && c->isfloating)
								? layers[LyrFloat]
								: c->scene->node.parent);
	}

	if (m->lt[m->sellt]->arrange)
		m->lt[m->sellt]->arrange(m);
	motionnotify(0, NULL, 0, 0, 0, 0);
	checkidleinhibitor(NULL);

	transactionschedule();
}

void


@@ 737,19 822,19 @@ arrangelayers(Monitor *m)
		arrange(m);
	}

	/* Arrange non-exlusive surfaces from top->bottom */
	/* Arrange non-exclusive surfaces from top->bottom */
	for (i = 3; i >= 0; i--)
		arrangelayer(m, &m->layers[i], &usable_area, 0);

	/* Find topmost keyboard interactive layer, if such a layer exists */
	/* Find topmost keyboard interactive layer that has indicated it wants
	 * exclusive access to the keyboard, if such a layer exists */
	for (i = 0; i < (int)LENGTH(layers_above_shell); i++) {
		wl_list_for_each_reverse(l, &m->layers[layers_above_shell[i]], link) {
			if (locked || !l->layer_surface->current.keyboard_interactive || !l->mapped)
			if (locked ||
					l->layer_surface->current.keyboard_interactive != ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE ||
					!l->mapped)
				continue;
			/* Deactivate the focused client. */
			focusclient(NULL, 0);
			exclusive_focus = l;
			client_notify_enter(l->layer_surface->surface, wlr_seat_get_keyboard(seat));
			focusclient(NULL, l, 0);
			return;
		}
	}


@@ 800,9 885,28 @@ axisnotify(struct wl_listener *listener, void *data)
	/* This event is forwarded by the cursor when a pointer emits an axis event,
	 * for example when you move the scroll wheel. */
	struct wlr_pointer_axis_event *event = data;
	const Axis *a;
	unsigned int adir;
	struct wlr_keyboard *keyboard;
	uint32_t mods;

	wlr_idle_notifier_v1_notify_activity(idle_notifier, seat);
	/* TODO: allow usage of scroll wheel for mousebindings, it can be implemented
	 * by checking the event's orientation and the delta of the event */

	if (!locked) {
		if (event->orientation == WL_POINTER_AXIS_VERTICAL_SCROLL)
			adir = event->delta > 0 ? AxisDown : AxisUp;
		else
			adir = event->delta > 0 ? AxisRight : AxisLeft;
		keyboard = wlr_seat_get_keyboard(seat);
		mods = keyboard ? wlr_keyboard_get_modifiers(keyboard) : 0;
		for (a = axes; a < END(axes); a++) {
			if (CLEANMASK(mods) == CLEANMASK(a->mod) && adir == a->dir && a->func) {
				a->func(&a->arg);
				return;
			}
		}
	}

	/* Notify the client with pointer focus of the axis event. */
	wlr_seat_pointer_notify_axis(seat,
			event->time_msec, event->orientation, event->delta,


@@ 815,7 919,8 @@ buttonpress(struct wl_listener *listener, void *data)
	struct wlr_pointer_button_event *event = data;
	struct wlr_keyboard *keyboard;
	uint32_t mods;
	Client *c;
	Client *c = NULL;
	LayerSurface *l = NULL;
	const Button *b;

	wlr_idle_notifier_v1_notify_activity(idle_notifier, seat);


@@ 827,10 932,14 @@ buttonpress(struct wl_listener *listener, void *data)
		if (locked)
			break;

		/* Change focus if the button was _pressed_ over a client */
		xytonode(cursor->x, cursor->y, NULL, &c, NULL, NULL, NULL);
		if (c && (!client_is_unmanaged(c) || client_wants_focus(c)))
			focusclient(c, 1);
		/* Change focus if the button was _pressed_ over a client
		   or a layer surface with on-demand keyboard interactivity */
		xytonode(cursor->x, cursor->y, NULL, &c, &l, NULL, NULL);
		if (c && (!client_is_unmanaged(c) || client_wants_focus(c))) {
			focusclient(c, NULL, 1);
		} else if (l && l->layer_surface->current.keyboard_interactive) {
			focusclient(NULL, l, 0);
		}

		keyboard = wlr_seat_get_keyboard(seat);
		mods = keyboard ? wlr_keyboard_get_modifiers(keyboard) : 0;


@@ 844,10 953,11 @@ buttonpress(struct wl_listener *listener, void *data)
		break;
	case WL_POINTER_BUTTON_STATE_RELEASED:
		/* If you released any buttons, we exit interactive move/resize mode. */
		/* TODO: should reset to the pointer focus's current setcursor */
		if (!locked && cursor_mode != CurNormal && cursor_mode != CurPressed) {
			wlr_cursor_set_xcursor(cursor, cursor_mgr, "default");
			cursor_mode = CurNormal;
			/* Update the cursor */
			wlr_seat_pointer_clear_focus(seat);
			motionnotify(0, NULL, 0, 0, 0, 0);
			/* Drop the window off on its new monitor */
			selmon = xytomon(cursor->x, cursor->y);
			setmon(grabc, selmon, 0);


@@ 864,6 974,25 @@ buttonpress(struct wl_listener *listener, void *data)
}

void
capturerequest(struct wl_listener *listener, void *data)
{
	struct wlr_ext_foreign_toplevel_image_capture_source_manager_v1_request *request = data;
	Client *view = request->toplevel_handle->data;

	if (!view->image_capture_source) {
		view->image_capture_source = wlr_ext_image_capture_source_v1_create_with_scene_node(
				&view->image_capture_scene->tree.node, event_loop, alloc, drw);
		if (!view->image_capture_source)
			return;
	}

	wlr_ext_foreign_toplevel_image_capture_source_manager_v1_request_accept(
		request, view->image_capture_source);

	client_set_suspended(view, 0);
}

void
chvt(const Arg *arg)
{
	wlr_session_change_vt(session, arg->ui);


@@ 890,6 1019,8 @@ checkidleinhibitor(struct wlr_surface *exclude)
void
cleanup(void)
{
	char **name;

	cleanuplisteners();
#ifdef XWAYLAND
	wlr_xwayland_destroy(xwayland);


@@ 904,10 1035,21 @@ cleanup(void)

	destroykeyboardgroup(&kb_group->destroy, NULL);

	input_method_relay_finish(input_method_relay);
	input_method_relay = NULL;

	/* If it's not destroyed manually, it will cause a use-after-free of wlr_seat.
	 * Destroy it until it's fixed on the wlroots side */
	wlr_backend_destroy(backend);

	if (transaction_idle)
		wl_event_source_remove(transaction_idle);
	wl_event_source_remove(transaction_timer);
	wl_array_release(&ghosts);
	wl_array_for_each(name, &offmons)
		free(*name);
	wl_array_release(&offmons);

	wl_display_destroy(dpy);
	/* Destroy after the wayland display (when the monitors are already destroyed)
	   to avoid destroying them with an invalid scene output. */


@@ 977,9 1119,12 @@ cleanuplisteners(void)
	wl_list_remove(&request_start_drag.link);
	wl_list_remove(&start_drag.link);
	wl_list_remove(&new_session_lock.link);
	wl_list_remove(&new_foreign_toplevel_capture_request.link);
#ifdef XWAYLAND
	wl_list_remove(&new_xwayland_surface.link);
	wl_list_remove(&xwayland_ready.link);
	if (xwayland) {
		wl_list_remove(&new_xwayland_surface.link);
		wl_list_remove(&xwayland_ready.link);
	}
#endif
}



@@ 1009,9 1154,9 @@ closemon(Monitor *m)
			setmon(c, selmon, c->tags);
	}

  m->tagset[0] = m->tagset[1] = 0;
	m->tagset[0] = m->tagset[1] = 0;

	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
	printstatus();
}



@@ 1051,6 1196,20 @@ commitlayersurfacenotify(struct wl_listener *listener, void *data)
}

void
commitlocksurface(struct wl_listener *listener, void *data)
{
	Monitor *m = wl_container_of(listener, m, commit_lock_surface);

	if (!m->lock_surface->surface->mapped)
		return;

	wl_list_remove(&m->commit_lock_surface.link);
	wl_list_init(&m->commit_lock_surface.link);

	motionnotify(0, NULL, 0, 0, 0, 0);
}

void
commitnotify(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, commit);


@@ 1073,14 1232,15 @@ commitnotify(struct wl_listener *listener, void *data)
		if (c->decoration)
			requestdecorationmode(&c->set_decoration_mode, c->decoration);
		wlr_xdg_toplevel_set_size(c->surface.xdg->toplevel, 0, 0);
		if (!c->isfloating)
			client_set_tiled(c, WLR_EDGE_TOP | WLR_EDGE_BOTTOM | WLR_EDGE_LEFT |
					WLR_EDGE_RIGHT);
		return;
	}

	transactioncommit(c, c->resize_serial && c->surface.xdg->current.configure_serial
			>= c->resize_serial);
	resize(c, c->geom, (c->isfloating && !c->isfullscreen));

	/* mark a pending resize as completed */
	if (c->resize && c->resize <= c->surface.xdg->current.configure_serial)
		c->resize = 0;
}

void


@@ 1106,8 1266,8 @@ commitpopup(struct wl_listener *listener, void *data)
		return;
	}
	box = type == LayerShell ? l->mon->m : c->mon->w;
	box.x -= (type == LayerShell ? l->scene->node.x : c->geom.x);
	box.y -= (type == LayerShell ? l->scene->node.y : c->geom.y);
	box.x -= (type == LayerShell ? l->scene->node.x : c->geom.x - c->surface.xdg->geometry.x);
	box.y -= (type == LayerShell ? l->scene->node.y : c->geom.y - c->surface.xdg->geometry.y);
	wlr_xdg_popup_unconstrain_from_box(popup, &box);
	wl_list_remove(&listener->link);
	free(listener);


@@ 1228,6 1388,7 @@ createlocksurface(struct wl_listener *listener, void *data)
	wlr_session_lock_surface_v1_configure(lock_surface, m->m.width, m->m.height);

	LISTEN(&lock_surface->events.destroy, &m->destroy_lock_surface, destroylocksurface);
	LISTEN(&lock_surface->surface->events.commit, &m->commit_lock_surface, commitlocksurface);

	if (m == selmon)
		client_notify_enter(lock_surface->surface, wlr_seat_get_keyboard(seat));


@@ 1302,14 1463,17 @@ createmon(struct wl_listener *listener, void *data)
	 * monitor supports only a specific set of modes. We just pick the
	 * monitor's preferred mode; a more sophisticated compositor would let
	 * the user configure it. */
	wlr_output_state_set_mode(&state, wlr_output_preferred_mode(wlr_output));

	/* Set up event listeners */
	LISTEN(&wlr_output->events.frame, &m->frame, rendermon);
	LISTEN(&wlr_output->events.destroy, &m->destroy, cleanupmon);
	LISTEN(&wlr_output->events.request_state, &m->request_state, requestmonstate);

	wlr_output_state_set_enabled(&state, 1);
	/* Some monitors disconnect when they are turned off and reconnect shortly
	 * after, so keep outputs that were turned off disabled. */
	if (!monoff(wlr_output)) {
		wlr_output_state_set_mode(&state, wlr_output_preferred_mode(wlr_output));
		wlr_output_state_set_enabled(&state, 1);
	}
	wlr_output_commit_state(wlr_output, &state);
	wlr_output_state_finish(&state);



@@ 1335,6 1499,12 @@ createmon(struct wl_listener *listener, void *data)
	 * output (such as DPI, scale factor, manufacturer, etc).
	 */
	m->scene_output = wlr_scene_output_create(scene, wlr_output);

	if (!wlr_output->enabled) {
		updatemons(NULL, NULL);
		return;
	}

	if (m->m.x == -1 && m->m.y == -1)
		wlr_output_layout_add_auto(output_layout, wlr_output);
	else


@@ 1469,6 1639,7 @@ void
destroydecoration(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, destroy_decoration);
	c->decoration = NULL;

	wl_list_remove(&c->destroy_decoration.link);
	wl_list_remove(&c->set_decoration_mode.link);


@@ 1478,10 1649,9 @@ void
destroydragicon(struct wl_listener *listener, void *data)
{
	/* Focus enter isn't sent during drag, so refocus the focused node. */
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
	motionnotify(0, NULL, 0, 0, 0, 0);
	wl_list_remove(&listener->link);
	free(listener);
}

void


@@ 1517,7 1687,7 @@ destroylock(SessionLock *lock, int unlock)

	wlr_scene_node_set_enabled(&locked_bg->node, 0);

	focusclient(focustop(selmon), 0);
	focusclient(focustop(selmon), NULL, 0);
	motionnotify(0, NULL, 0, 0, 0, 0);

destroy:


@@ 1538,6 1708,7 @@ destroylocksurface(struct wl_listener *listener, void *data)

	m->lock_surface = NULL;
	wl_list_remove(&m->destroy_lock_surface.link);
	wl_list_remove(&m->commit_lock_surface.link);

	if (lock_surface->surface != seat->keyboard_state.focused_surface)
		return;


@@ 1546,7 1717,7 @@ destroylocksurface(struct wl_listener *listener, void *data)
		surface = wl_container_of(cur_lock->surfaces.next, surface, link);
		client_notify_enter(surface->surface, wlr_seat_get_keyboard(seat));
	} else if (!locked) {
		focusclient(focustop(selmon), 1);
		focusclient(focustop(selmon), NULL, 1);
	} else {
		wlr_seat_keyboard_clear_focus(seat);
	}


@@ 1566,6 1737,7 @@ destroynotify(struct wl_listener *listener, void *data)
		wl_list_remove(&c->associate.link);
		wl_list_remove(&c->configure.link);
		wl_list_remove(&c->dissociate.link);
		wl_list_remove(&c->set_geometry.link);
		wl_list_remove(&c->set_hints.link);
	} else
#endif


@@ 1574,6 1746,10 @@ destroynotify(struct wl_listener *listener, void *data)
		wl_list_remove(&c->map.link);
		wl_list_remove(&c->unmap.link);
		wl_list_remove(&c->maximize.link);
		if (c->decoration) {
			wl_list_remove(&c->set_decoration_mode.link);
			wl_list_remove(&c->destroy_decoration.link);
		}
	}
	free(c);
}


@@ 1626,7 1802,8 @@ dirtomon(enum wlr_direction dir)
			dir, selmon->wlr_output, selmon->m.x, selmon->m.y)))
		return next->data;
	if ((next = wlr_output_layout_farthest_output(output_layout,
			dir ^ (WLR_DIRECTION_LEFT|WLR_DIRECTION_RIGHT),
			dir ^ (WLR_DIRECTION_LEFT | WLR_DIRECTION_RIGHT |
					WLR_DIRECTION_UP | WLR_DIRECTION_DOWN),
			selmon->wlr_output, selmon->m.x, selmon->m.y)))
		return next->data;
	return selmon;


@@ 1718,13 1895,13 @@ dwl_ipc_output_printstatus_to(DwlIpcOutput *ipc_output)
	Monitor *monitor = ipc_output->mon;
	Monitor *m;
	Client *c, *focused;
	int tagmask, state, numclients, focused_client, tag;
	int tagmask, state, numclients, tagfocused, tag;
	const char *title, *appid;
	focused = focustop(monitor);
	zdwl_ipc_output_v2_send_active(ipc_output->resource, monitor == selmon);

	for (tag = 0 ; tag < TAGCOUNT; tag++) {
		numclients = state = focused_client = 0;
		numclients = state = tagfocused = 0;
		tagmask = 1 << tag;
		if ((tagmask & monitor->tagset[monitor->seltags]) != 0)
			state |= ZDWL_IPC_OUTPUT_V2_TAG_STATE_ACTIVE;


@@ 1738,13 1915,13 @@ dwl_ipc_output_printstatus_to(DwlIpcOutput *ipc_output)
			if (!(c->tags & tagmask))
				continue;
			if (c == focused)
				focused_client = 1;
				tagfocused = 1;
			if (c->isurgent)
				state |= ZDWL_IPC_OUTPUT_V2_TAG_STATE_URGENT;

			numclients++;
		}
		zdwl_ipc_output_v2_send_tag(ipc_output->resource, tag, state, numclients, focused_client);
		zdwl_ipc_output_v2_send_tag(ipc_output->resource, tag, state, numclients, tagfocused);
	}
	title = focused ? client_get_title(focused) : "";
	appid = focused ? client_get_appid(focused) : "";


@@ 1784,7 1961,7 @@ dwl_ipc_output_set_client_tags(struct wl_client *client, struct wl_resource *res
		return;

	selected_client->tags = newtags;
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
	arrange(selmon);
	printstatus();
}


@@ 1806,7 1983,7 @@ dwl_ipc_output_set_layout(struct wl_client *client, struct wl_resource *resource
	selmon = ipc_output->mon;
	setlayout(&(Arg){.v = &layouts[index]});
	selmon = monitor;
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
}

void


@@ 1830,7 2007,7 @@ dwl_ipc_output_set_tags(struct wl_client *client, struct wl_resource *resource, 
	selmon = ipc_output->mon;
	view(&(Arg){.ui = newtags});
	selmon = monitor;
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
}

void


@@ 1840,9 2017,9 @@ dwl_ipc_output_release(struct wl_client *client, struct wl_resource *resource)
}

void
focusclient(Client *c, int lift)
focusclient(Client *c, LayerSurface *l, int lift)
{
	struct wlr_surface *old = seat->keyboard_state.focused_surface;
	struct wlr_surface *surface, *old = seat->keyboard_state.focused_surface;
	int unused_lx, unused_ly, old_client_type;
	Client *old_c = NULL;
	LayerSurface *old_l = NULL;


@@ 1872,45 2049,50 @@ focusclient(Client *c, int lift)

		/* Don't change border color if there is an exclusive focus or we are
		 * handling a drag operation */
		if (!exclusive_focus && !seat->drag)
		if (!(old_l && old_l->layer_surface->current.keyboard_interactive == ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE)
				&& !seat->drag)
			client_set_border_color(c, focuscolor);
	}

	/* Deactivate old client if focus is changing */
	if (old && (!c || client_surface(c) != old)) {
		/* If an overlay is focused, don't focus or activate the client,
		 * but only update its position in fstack to render its border with focuscolor
		 * and focus it after the overlay is closed. */
		if (old_client_type == LayerShell && wlr_scene_node_coords(
					&old_l->scene->node, &unused_lx, &unused_ly)
				&& old_l->layer_surface->current.layer >= ZWLR_LAYER_SHELL_V1_LAYER_TOP) {
			return;
		} else if (old_c && old_c == exclusive_focus && client_wants_focus(old_c)) {
			return;
		/* Don't deactivate old client if the new one wants focus, as this causes issues with winecfg
		 * and probably other clients */
		} else if (old_c && !client_is_unmanaged(old_c) && (!c || !client_wants_focus(c))) {
			client_set_border_color(old_c, bordercolor);
	/* If an overlay is focused, don't focus or activate the client,
	 * but only update its position in fstack to render its border with focuscolor
	 * and focus it after the overlay is closed. */
	if (!l && old && (!c || client_surface(c) != old)
			&& old_client_type == LayerShell && wlr_scene_node_coords(
				&old_l->scene->node, &unused_lx, &unused_ly)
			&& old_l->layer_surface->current.layer >= ZWLR_LAYER_SHELL_V1_LAYER_TOP
			&& old_l->layer_surface->current.keyboard_interactive == ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE)
		return;

			client_activate_surface(old, 0);
		}
	if (focused_client && focused_client != c && !(c && client_is_unmanaged(c))) {
		struct wlr_surface *s = client_surface(focused_client);
		if (c)
			client_set_border_color(focused_client, bordercolor);
		if (s && s->mapped)
			client_activate_surface(s, 0);
		focused_client = NULL;
	}
	printstatus();

	if (!c) {
	if (!c && !l) {
		/* With no client, all we have left is to clear focus */
		input_method_relay_set_focus(input_method_relay, NULL);
		wlr_seat_keyboard_notify_clear_focus(seat);
		return;
	}

	/* Change cursor surface */
	motionnotify(0, NULL, 0, 0, 0, 0);
	surface = c ? client_surface(c) : l->layer_surface->surface;
	input_method_relay_set_focus(input_method_relay, surface);
	client_notify_enter(surface, wlr_seat_get_keyboard(seat));

	/* Have a client, so focus its top-level wlr_surface */
	client_notify_enter(client_surface(c), wlr_seat_get_keyboard(seat));
	if (l)
		return;

	client_activate_surface(surface, 1);
	motionnotify(0, NULL, 0, 0, 0, 0);

	/* Activate the new client */
	client_activate_surface(client_surface(c), 1);
	if (!client_is_unmanaged(c))
		focused_client = c;
}

void


@@ 1922,7 2104,7 @@ focusmon(const Arg *arg)
			selmon = dirtomon(arg->i);
		while (!selmon->wlr_output->enabled && i++ < nmons);
	}
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
}

void


@@ 1941,18 2123,18 @@ focusortogglematchingscratch(const Arg *arg)
				if (found == 1) {
					if (hide == 1) {
						c->tags = 0;
						focusclient(focustop(selmon), 1);
						focusclient(focustop(selmon), NULL, 1);
					}
					continue;
				}
				if (focustop(selmon) == c) {
					// hide
					c->tags = 0;
					focusclient(focustop(selmon), 1);
					focusclient(focustop(selmon), NULL, 1);
					hide = 1;
				} else {
					// focus
					focusclient(c, 1);
					focusclient(c, NULL, 1);
				}
			} else {
				// show


@@ 1961,7 2143,7 @@ focusortogglematchingscratch(const Arg *arg)
        else
          setmon(c, selmon, 0);
				// focus
				focusclient(c, 1);
				focusclient(c, NULL, 1);
			}
			found = 1;
			continue;


@@ 1997,10 2179,10 @@ focusortogglescratch(const Arg *arg)
			if (focustop(selmon) == c) {
				// hide
				c->tags = 0;
				focusclient(focustop(selmon), 1);
				focusclient(focustop(selmon), NULL, 1);
			} else {
				// focus
				focusclient(c, 1);
				focusclient(c, NULL, 1);
			}
		} else {
			// show


@@ 2008,7 2190,7 @@ focusortogglescratch(const Arg *arg)
        c->tags = selmon->tagset[selmon->seltags];
      else
        setmon(c, selmon, 0);
			focusclient(c, 1);
			focusclient(c, NULL, 1);
		}
		arrange(selmon);
	} else{


@@ 2031,7 2213,7 @@ focusnthmon(const Arg *arg)
	}

	selmon = m;
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
}

void


@@ 2057,7 2239,7 @@ focusstack(const Arg *arg)
		}
	}
	/* If only one client is visible on selmon, then c == sel */
	focusclient(c, 1);
	focusclient(c, NULL, 1);
}

/* We probably should change the name of this: it sounds like it


@@ 2299,9 2481,11 @@ keypress(struct wl_listener *listener, void *data)
	/* On _press_ if there is no active screen locker,
	 * attempt to process a compositor keybinding. */
	if (!locked
    && event->state == WL_KEYBOARD_KEY_STATE_PRESSED
    && wl_list_empty(&shortcuts_inhibit_mgr->inhibitors)) {
			&& event->state == WL_KEYBOARD_KEY_STATE_PRESSED
			&& wl_list_empty(&shortcuts_inhibit_mgr->inhibitors)) {
		handled = keybinding(mods, keycode);
		if (handled)
			consumed[event->keycode] = 1;
	}

	if (handled && group->wlr_group->keyboard.repeat_info.delay > 0) {


@@ 2314,9 2498,14 @@ keypress(struct wl_listener *listener, void *data)
		wl_event_source_timer_update(group->key_repeat_source, 0);
	}

	if (handled)
	if (handled || input_method_keyboard_grab_forward_key(group, event))
		return;

	if (consumed[event->keycode]) {
		if (event->state == WL_KEYBOARD_KEY_STATE_RELEASED)
			consumed[event->keycode] = 0;
		return; /* Don't pass to the client the release event of a handled key-press */
	}
	wlr_seat_set_keyboard(seat, &group->wlr_group->keyboard);
	/* Pass unhandled keycodes along to the client. */
	wlr_seat_keyboard_notify_key(seat, event->time_msec,


@@ 2330,6 2519,9 @@ keypressmod(struct wl_listener *listener, void *data)
	 * pressed. We simply communicate this to the client. */
	KeyboardGroup *group = wl_container_of(listener, group, modifiers);

	if (input_method_keyboard_grab_forward_modifiers(group))
		return;

	wlr_seat_set_keyboard(seat, &group->wlr_group->keyboard);
	/* Send modifiers to the client. */
	wlr_seat_keyboard_notify_modifiers(seat,


@@ 2340,7 2532,7 @@ int
keyrepeat(void *data)
{
	KeyboardGroup *group = data;
	if (!group->keycode || group->wlr_group->keyboard.repeat_info.rate <= 0)
	if (locked || !group->keycode || group->wlr_group->keyboard.repeat_info.rate <= 0)
		return 0;

	wl_event_source_timer_update(group->key_repeat_source,


@@ 2370,12 2562,18 @@ locksession(struct wl_listener *listener, void *data)
		return;
	}
	lock = session_lock->data = ecalloc(1, sizeof(*lock));
	focusclient(NULL, 0);
	focusclient(NULL, NULL, 0);

	lock->scene = wlr_scene_tree_create(layers[LyrBlock]);
	cur_lock = lock->lock = session_lock;
	locked = 1;

	/* Cancel any interactive move/resize and take the pointer focus away
	 * from the clients below the lock screen */
	cursor_mode = CurNormal;
	grabc = NULL;
	motionnotify(0, NULL, 0, 0, 0, 0);

	LISTEN(&session_lock->events.new_surface, &lock->new_surface, createlocksurface);
	LISTEN(&session_lock->events.destroy, &lock->destroy, destroysessionlock);
	LISTEN(&session_lock->events.unlock, &lock->unlock, unlocksession);


@@ 2392,6 2590,11 @@ mapnotify(struct wl_listener *listener, void *data)
	Monitor *m;
	int i;

	struct wlr_ext_foreign_toplevel_handle_v1_state foreign_toplevel_state = {
		.app_id = client_get_appid(c),
		.title = client_get_title(c),
	};

	/* Create scene tree for this client and its border */
	c->scene = client_surface(c)->data = wlr_scene_tree_create(layers[LyrTile]);
	/* Enabled later by a call to arrange() */


@@ 2406,13 2609,10 @@ mapnotify(struct wl_listener *listener, void *data)
	/* Handle unmanaged clients first so we can return prior create borders */
	if (client_is_unmanaged(c)) {
		/* Unmanaged clients always are floating */
		wlr_scene_node_reparent(&c->scene->node, layers[LyrFloat]);
		wlr_scene_node_reparent(&c->scene->node, layers[LyrUnmanaged]);
		wlr_scene_node_set_position(&c->scene->node, c->geom.x, c->geom.y);
		client_set_size(c, c->geom.width, c->geom.height);
		if (client_wants_focus(c)) {
			focusclient(c, 1);
			exclusive_focus = c;
		}
		if (client_wants_focus(c))
			focusclient(c, NULL, 1);
		goto unset_fullscreen;
	}



@@ 2423,14 2623,32 @@ mapnotify(struct wl_listener *listener, void *data)
	}

	/* Initialize client geometry with room for border */
	client_set_tiled(c, WLR_EDGE_TOP | WLR_EDGE_BOTTOM | WLR_EDGE_LEFT | WLR_EDGE_RIGHT);
	client_set_tiled(c, WLR_EDGE_TOP | WLR_EDGE_BOTTOM | WLR_EDGE_LEFT |
			WLR_EDGE_RIGHT);
	c->geom.width += 2 * c->bw;
	c->geom.height += 2 * c->bw;
	if (client_is_x11(c)) {
		int x = c->geom.x - (int)c->bw, y = c->geom.y - (int)c->bw;
		c->geom.x = c->geom.x < sgeom.x ? x : MAX(x, sgeom.x);
		c->geom.y = c->geom.y < sgeom.y ? y : MAX(y, sgeom.y);
	}

	/* Insert this client into client lists. */
	wl_list_insert(&clients, &c->link);
	wl_list_insert(&fstack, &c->flink);

	c->foreign_toplevel_handle = wlr_ext_foreign_toplevel_handle_v1_create(
			foreign_toplevel_list, &foreign_toplevel_state);
	c->foreign_toplevel_handle->data = c;
	c->image_capture_scene = wlr_scene_create();
	if (c->type == XDGShell)
		c->capture.image_capture_tree = wlr_scene_xdg_surface_create(&c->image_capture_scene->tree, c->surface.xdg);
#ifdef XWAYLAND
	else
		c->capture.image_capture_scene_surface = wlr_scene_surface_create(&c->image_capture_scene->tree, c->surface.xwayland->surface);
#endif


	/* Set initial monitor, tags, floating status, and focus:
	 * we always consider floating, clients that have parent and thus
	 * we set the same tags and monitor as its parent.


@@ 2545,26 2763,38 @@ motionabsolute(struct wl_listener *listener, void *data)
	motionnotify(event->time_msec, &event->pointer->base, dx, dy, dx, dy);
}

char **
monoff(struct wlr_output *wlr_output)
{
	char **name;
	wl_array_for_each(name, &offmons) {
		if (!strcmp(*name, wlr_output->name))
			return name;
	}
	return NULL;
}

void
motionnotify(uint32_t time, struct wlr_input_device *device, double dx, double dy,
		double dx_unaccel, double dy_unaccel)
{
	int nx, ny;
	double sx = 0, sy = 0, sx_confined, sy_confined;
	Client *c = NULL, *w = NULL;
	LayerSurface *l = NULL;
	LayerSurface *l = NULL, *focusedl = NULL;
	struct wlr_surface *surface = NULL;
	struct wlr_pointer_constraint_v1 *constraint;

	/* Find the client under the pointer and send the event along. */
	xytonode(cursor->x, cursor->y, &surface, &c, NULL, &sx, &sy);
	xytonode(cursor->x, cursor->y, &surface, &c, &l, &sx, &sy);

	if (cursor_mode == CurPressed && !seat->drag
			&& surface != seat->pointer_state.focused_surface
			&& toplevel_from_wlr_surface(seat->pointer_state.focused_surface, &w, &l) >= 0) {
			&& toplevel_from_wlr_surface(seat->pointer_state.focused_surface, &w, &focusedl) >= 0) {
		c = w;
		surface = seat->pointer_state.focused_surface;
		sx = cursor->x - (l ? l->scene->node.x : w->geom.x);
		sy = cursor->y - (l ? l->scene->node.y : w->geom.y);
		sx = cursor->x - (focusedl ? focusedl->scene->node.x : w->geom.x);
		sy = cursor->y - (focusedl ? focusedl->scene->node.y : w->geom.y);
	}

	/* time is 0 in internal calls meant to restore pointer focus. */


@@ 2606,7 2836,17 @@ motionnotify(uint32_t time, struct wlr_input_device *device, double dx, double d
	/* If we are currently grabbing the mouse, handle and return */
	if (cursor_mode == CurMove) {
		/* Move the grabbed client to the new position. */
		resize(grabc, (struct wlr_box){.x = (int)round(cursor->x) - grabcx, .y = (int)round(cursor->y) - grabcy,
		nx = (int)round(cursor->x) - grabcx;
		ny = (int)round(cursor->y) - grabcy;
		if (abs(selmon->w.x - nx) < (int)snap)
			nx = selmon->w.x;
		else if (abs((selmon->w.x + selmon->w.width) - (nx + grabc->geom.width)) < (int)snap)
			nx = selmon->w.x + selmon->w.width - grabc->geom.width;
		if (abs(selmon->w.y - ny) < (int)snap)
			ny = selmon->w.y;
		else if (abs((selmon->w.y + selmon->w.height) - (ny + grabc->geom.height)) < (int)snap)
			ny = selmon->w.y + selmon->w.height - grabc->geom.height;
		resize(grabc, (struct wlr_box){.x = nx, .y = ny,
			.width = grabc->geom.width, .height = grabc->geom.height}, 1);
		return;
	} else if (cursor_mode == CurResize) {


@@ 2621,7 2861,7 @@ motionnotify(uint32_t time, struct wlr_input_device *device, double dx, double d
	if (!surface && !seat->drag)
		wlr_cursor_set_xcursor(cursor, cursor_mgr, "default");

	pointerfocus(c, surface, sx, sy, time);
	pointerfocus(c, l, surface, sx, sy, time);
}

void


@@ 2684,49 2924,62 @@ outputmgrapplyortest(struct wlr_output_configuration_v1 *config, int test)
	 * output_layout.change event, not here.
	 */
	struct wlr_output_configuration_head_v1 *config_head;
	int ok = 1;
	size_t states_len, i;
	struct wlr_output_swapchain_manager swapchain_manager;
	struct wlr_backend_output_state *states =
		wlr_output_configuration_v1_build_state(config, &states_len);
	int ok = 0;

	if (!states) {
		wlr_output_configuration_v1_send_failed(config);
		return;
	}

	wlr_output_swapchain_manager_init(&swapchain_manager, backend);

	ok = wlr_output_swapchain_manager_prepare(&swapchain_manager, states, states_len);
	if (!ok || test)
		goto out;

	for (i = 0; i < states_len; i++) {
		struct wlr_swapchain *swapchain = wlr_output_swapchain_manager_get_swapchain(
				&swapchain_manager, states[i].output);
		if (swapchain && !states[i].output->enabled)
			wlr_output_state_set_buffer(&states[i].base, wlr_swapchain_acquire(swapchain));
	}

	if (!(ok = wlr_backend_commit(backend, states, states_len)))
		goto out;

	wlr_output_swapchain_manager_apply(&swapchain_manager);

	wl_list_for_each(config_head, &config->heads, link) {
		struct wlr_output *wlr_output = config_head->state.output;
		Monitor *m = wlr_output->data;
		struct wlr_output_state state;

		/* Ensure displays previously disabled by wlr-output-power-management-v1
		 * are properly handled*/
		m->asleep = 0;

		wlr_output_state_init(&state);
		wlr_output_state_set_enabled(&state, config_head->state.enabled);
		if (!config_head->state.enabled)
			goto apply_or_test;

		if (config_head->state.mode)
			wlr_output_state_set_mode(&state, config_head->state.mode);
		else
			wlr_output_state_set_custom_mode(&state,
					config_head->state.custom_mode.width,
					config_head->state.custom_mode.height,
					config_head->state.custom_mode.refresh);

		wlr_output_state_set_transform(&state, config_head->state.transform);
		wlr_output_state_set_scale(&state, config_head->state.scale);
		wlr_output_state_set_adaptive_sync_enabled(&state,
				config_head->state.adaptive_sync_enabled);

apply_or_test:
		ok &= test ? wlr_output_test_state(wlr_output, &state)
				: wlr_output_commit_state(wlr_output, &state);
		setmonoff(wlr_output, !config_head->state.enabled);

		/* Don't move monitors if position wouldn't change. This avoids
		 * wlroots marking the output as manually configured.
		 * wlr_output_layout_add does not like disabled outputs */
		if (!test && wlr_output->enabled && (m->m.x != config_head->state.x || m->m.y != config_head->state.y))
		if (config_head->state.enabled
				&& (m->m.x != config_head->state.x || m->m.y != config_head->state.y
					|| !wlr_output_layout_get(output_layout, wlr_output))) {
			wlr_output_layout_add(output_layout, wlr_output,
					config_head->state.x, config_head->state.y);

		wlr_output_state_finish(&state);
		}
	}

out:
	wlr_output_swapchain_manager_finish(&swapchain_manager);
	for (i = 0; i < states_len; i++)
		wlr_output_state_finish(&states[i].base);

	free(states);

	if (ok)
		wlr_output_configuration_v1_send_succeeded(config);
	else


@@ 2745,14 2998,18 @@ outputmgrtest(struct wl_listener *listener, void *data)
}

void
pointerfocus(Client *c, struct wlr_surface *surface, double sx, double sy,
pointerfocus(Client *c, LayerSurface *l, struct wlr_surface *surface, double sx, double sy,
		uint32_t time)
{
	struct timespec now;

	if (surface != seat->pointer_state.focused_surface &&
			sloppyfocus && time && c && !client_is_unmanaged(c))
		focusclient(c, 0);
	if (surface != seat->pointer_state.focused_surface && sloppyfocus && time) {
		if (c && (!client_is_unmanaged(c) || client_wants_focus(c))) {
			focusclient(c, NULL, 0);
		} else if (l && l->layer_surface->current.keyboard_interactive) {
			focusclient(NULL, l, 0);
		}
	}

	/* If surface is NULL, clear pointer focus */
	if (!surface) {


@@ 2791,8 3048,12 @@ powermgrsetmode(struct wl_listener *listener, void *data)
	if (!m)
		return;

	m->gamma_lut_changed = 1; /* Reapply gamma LUT when re-enabling the ouput */
	m->gamma_lut_changed = 1; /* Reapply gamma LUT when re-enabling the output */
	wlr_output_state_set_enabled(&state, event->mode);
	/* The output was reconnected while off and was never modeset */
	if (event->mode && !m->wlr_output->current_mode)
		wlr_output_state_set_mode(&state, wlr_output_preferred_mode(m->wlr_output));
	setmonoff(m->wlr_output, !event->mode);
	wlr_output_commit_state(m->wlr_output, &state);

	m->asleep = !event->mode;


@@ 2811,24 3072,15 @@ rendermon(struct wl_listener *listener, void *data)
	/* This function is called every time an output is ready to display a frame,
	 * generally at the output's refresh rate (e.g. 60Hz). */
	Monitor *m = wl_container_of(listener, m, frame);
	Client *c;
	struct wlr_output_state pending = {0};
	struct timespec now;

	/* Render if no XDG clients have an outstanding resize and are visible on
	 * this monitor. */
	wl_list_for_each(c, &clients, link) {
		if (c->resize && !c->isfloating && client_is_rendered_on_mon(c, m) && !client_is_stopped(c))
			goto skip;
	if (!wlr_scene_output_needs_frame(m->scene_output)) {
		return;
	}

	wlr_scene_output_commit(m->scene_output, NULL);

skip:
	/* Let clients know a frame has been rendered */
	clock_gettime(CLOCK_MONOTONIC, &now);
	wlr_scene_output_send_frame_done(m->scene_output, &now);
	wlr_output_state_finish(&pending);
}

void


@@ 2864,7 3116,7 @@ void
resize(Client *c, struct wlr_box geo, int interact)
{
	struct wlr_box *bbox;
	struct wlr_box clip;
	uint32_t serial;

	if (!c->mon || !client_surface(c)->mapped)
		return;


@@ 2875,22 3127,27 @@ resize(Client *c, struct wlr_box geo, int interact)
	c->geom = geo;
	applybounds(c, bbox);

	/* Update scene-graph, including borders */
	wlr_scene_node_set_position(&c->scene->node, c->geom.x, c->geom.y);
	wlr_scene_node_set_position(&c->scene_surface->node, c->bw, c->bw);
	wlr_scene_rect_set_size(c->border[0], c->geom.width, c->bw);
	wlr_scene_rect_set_size(c->border[1], c->geom.width, c->bw);
	wlr_scene_rect_set_size(c->border[2], c->bw, c->geom.height - 2 * c->bw);
	wlr_scene_rect_set_size(c->border[3], c->bw, c->geom.height - 2 * c->bw);
	wlr_scene_node_set_position(&c->border[1]->node, 0, c->geom.height - c->bw);
	wlr_scene_node_set_position(&c->border[2]->node, 0, c->bw);
	wlr_scene_node_set_position(&c->border[3]->node, c->geom.width - c->bw, c->bw);
	if ((serial = client_set_size(c, c->geom.width - 2 * c->bw,
			c->geom.height - 2 * c->bw)))
		c->resize_serial = serial;

	/* this is a no-op if size hasn't changed */
	c->resize = client_set_size(c, c->geom.width - 2 * c->bw,
			c->geom.height - 2 * c->bw);
	client_get_clip(c, &clip);
	wlr_scene_subsurface_tree_set_clip(&c->scene_surface->node, &clip);
	if (interact || (!c->resize_serial && wlr_box_equal(&c->geom, &c->current))) {
		c->resize_serial = 0;
		applygeom(c);
		return;
	}

	/* Until the transaction is applied, keep showing the current content
	 * instead of buffers with the new size at the old position. */
	if (c->resize_serial) {
		if (!c->snapshot && c->scene->node.enabled
				&& (c->snapshot = snapshot(&c->scene_surface->node, c->scene, c)))
			wlr_scene_node_set_enabled(&c->scene_surface->node, 0);
		/* The scene graph doesn't send frame done events to hidden
		 * surfaces, but the client may be waiting for one to redraw */
		client_send_frame_done(c);
	}
	transactionschedule();
}

void


@@ 2991,14 3248,10 @@ setcursorshape(struct wl_listener *listener, void *data)
void
setfloating(Client *c, int floating)
{
	Client *p = client_get_parent(c);
	c->isfloating = floating;
	/* If in floating layout do not change the client's layer */
	if (!c->mon || !client_surface(c)->mapped || !c->mon->lt[c->mon->sellt]->arrange)
		return;
	wlr_scene_node_reparent(&c->scene->node, layers[c->isfullscreen ||
			(p && p->isfullscreen) ? LyrFS
			: c->isfloating ? LyrFloat : LyrTile]);
	arrange(c->mon);
	printstatus();
}


@@ 3011,8 3264,6 @@ setfullscreen(Client *c, int fullscreen)
		return;
	c->bw = fullscreen ? 0 : borderpx;
	client_set_fullscreen(c, fullscreen);
	wlr_scene_node_reparent(&c->scene->node, layers[c->isfullscreen
			? LyrFS : c->isfloating ? LyrFloat : LyrTile]);

	if (fullscreen) {
		c->prev = c->geom;


@@ 3101,7 3352,23 @@ setmon(Client *c, Monitor *m, uint32_t newtags)
		setfloating(c, c->isfloating);
	}
	c->prev_bounds = (struct wlr_box){.x = 0, .y = 0, .width = 0, .height = 0};
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
}

void
setmonoff(struct wlr_output *wlr_output, int off)
{
	char **name = monoff(wlr_output), **last;

	if (off && !name && (name = wl_array_add(&offmons, sizeof(*name)))) {
		if (!(*name = strdup(wlr_output->name)))
			offmons.size -= sizeof(*name);
	} else if (!off && name) {
		free(*name);
		last = (char **)((char *)offmons.data + offmons.size) - 1;
		*name = *last;
		offmons.size -= sizeof(*name);
	}
}

void


@@ 3143,7 3410,7 @@ setup(void)
	wlr_log_init(log_level, NULL);

	/* The Wayland display is managed by libwayland. It handles accepting
	 * clients from the Unix socket, manging Wayland globals, and so on. */
	 * clients from the Unix socket, managing Wayland globals, and so on. */
	dpy = wl_display_create();
	event_loop = wl_display_get_event_loop(dpy);



@@ 3205,7 3472,10 @@ setup(void)
	wlr_data_device_manager_create(dpy);
	wlr_export_dmabuf_manager_v1_create(dpy);
	wlr_screencopy_manager_v1_create(dpy);
	wlr_ext_image_copy_capture_manager_v1_create(dpy, 1);
	wlr_ext_output_image_capture_source_manager_v1_create(dpy, 1);
	wlr_data_control_manager_v1_create(dpy);
	wlr_ext_data_control_manager_v1_create(dpy, 1);
	wlr_primary_selection_v1_device_manager_create(dpy);
	wlr_viewporter_create(dpy);
	wlr_single_pixel_buffer_manager_v1_create(dpy);


@@ 3256,11 3526,15 @@ setup(void)
	wl_list_init(&clients);
	wl_list_init(&fstack);

	transaction_timer = wl_event_loop_add_timer(event_loop, transactiontimeout, NULL);
	wl_array_init(&ghosts);
	wl_array_init(&offmons);

	xdg_shell = wlr_xdg_shell_create(dpy, 6);
	wl_signal_add(&xdg_shell->events.new_toplevel, &new_xdg_toplevel);
	wl_signal_add(&xdg_shell->events.new_popup, &new_xdg_popup);

	layer_shell = wlr_layer_shell_v1_create(dpy, 3);
	layer_shell = wlr_layer_shell_v1_create(dpy, 4);
	wl_signal_add(&layer_shell->events.new_surface, &new_layer_surface);

	idle_notifier = wlr_idle_notifier_v1_create(dpy);


@@ 3274,9 3548,15 @@ setup(void)
			(float [4]){0.1f, 0.1f, 0.1f, 1.0f});
	wlr_scene_node_set_enabled(&locked_bg->node, 0);

    shortcuts_inhibit_mgr = wlr_keyboard_shortcuts_inhibit_v1_create(dpy);
    wl_signal_add(&shortcuts_inhibit_mgr->events.new_inhibitor, &new_shortcuts_inhibitor);
    LISTEN_STATIC(&shortcuts_inhibit_mgr->events.destroy, destroyshortcutsinhibitmgr);
	shortcuts_inhibit_mgr = wlr_keyboard_shortcuts_inhibit_v1_create(dpy);
	wl_signal_add(&shortcuts_inhibit_mgr->events.new_inhibitor, &new_shortcuts_inhibitor);
	LISTEN_STATIC(&shortcuts_inhibit_mgr->events.destroy, destroyshortcutsinhibitmgr);

	foreign_toplevel_list = wlr_ext_foreign_toplevel_list_v1_create(dpy,1);
	ext_foreign_toplevel_image_capture_source_manager_v1 =
            wlr_ext_foreign_toplevel_image_capture_source_manager_v1_create(dpy, 1);
	wl_signal_add(&ext_foreign_toplevel_image_capture_source_manager_v1->events.new_request,
			&new_foreign_toplevel_capture_request);

	/* Use decoration protocols to negotiate server-side decorations */
	wlr_server_decoration_manager_set_default_mode(


@@ 3352,6 3632,9 @@ setup(void)
	wl_signal_add(&output_mgr->events.test, &output_mgr_test);

	wl_global_create(dpy, &zdwl_ipc_manager_v2_interface, 2, NULL, dwl_ipc_manager_bind);
	input_method_manager = wlr_input_method_manager_v2_create(dpy);
	text_input_manager = wlr_text_input_manager_v3_create(dpy);
	input_method_relay = input_method_relay_create();

	/* Make sure XWayland clients don't connect to the parent X server,
	 * e.g when running in the x11 backend or the wayland backend and the


@@ 3373,6 3656,57 @@ setup(void)
#endif
}

struct wlr_scene_node *
snapshot(struct wlr_scene_node *node, struct wlr_scene_tree *parent, void *data)
{
	/* Copy the visible part of a scene graph subtree, keeping references to
	 * the buffers so that they can be shown after the client changes them */
	struct wlr_scene_node *child, *copy;
	struct wlr_scene_tree *tree;
	struct wlr_scene_rect *rect;
	struct wlr_scene_buffer *src, *buffer;

	if (!node->enabled)
		return NULL;

	switch (node->type) {
	case WLR_SCENE_NODE_TREE:
		tree = wlr_scene_tree_create(parent);
		wl_list_for_each(child, &wlr_scene_tree_from_node(node)->children, link)
			snapshot(child, tree, data);
		copy = &tree->node;
		break;
	case WLR_SCENE_NODE_RECT:
		rect = wlr_scene_rect_from_node(node);
		copy = &wlr_scene_rect_create(parent, rect->width, rect->height, rect->color)->node;
		break;
	case WLR_SCENE_NODE_BUFFER:
		src = wlr_scene_buffer_from_node(node);
		buffer = wlr_scene_buffer_create(parent, NULL);
		wlr_scene_buffer_set_dest_size(buffer, src->dst_width, src->dst_height);
		wlr_scene_buffer_set_opaque_region(buffer, &src->opaque_region);
		wlr_scene_buffer_set_opacity(buffer, src->opacity);
		wlr_scene_buffer_set_filter_mode(buffer, src->filter_mode);
		wlr_scene_buffer_set_transfer_function(buffer, src->transfer_function);
		wlr_scene_buffer_set_primaries(buffer, src->primaries);
		wlr_scene_buffer_set_color_encoding(buffer, src->color_encoding);
		wlr_scene_buffer_set_color_range(buffer, src->color_range);
		wlr_scene_buffer_set_source_box(buffer, &src->src_box);
		wlr_scene_buffer_set_transform(buffer, src->transform);
		wlr_scene_buffer_set_buffer(buffer, src->buffer);
		copy = &buffer->node;
		break;
	default:
		return NULL;
	}

	copy->data = data;
	wlr_scene_node_set_position(copy, node->x, node->y);
	if (node->parent == parent)
		wlr_scene_node_place_above(copy, node);
	return copy;
}

void
spawn(const Arg *arg)
{


@@ 3402,7 3736,7 @@ startdrag(struct wl_listener *listener, void *data)
		return;

	drag->icon->data = &wlr_scene_drag_icon_create(drag_icon, drag->icon)->node;
	LISTEN_STATIC(&drag->icon->events.destroy, destroydragicon);
	wl_signal_add(&drag->icon->events.destroy, &drag_icon_destroy);
}

void


@@ 3414,7 3748,7 @@ tag(const Arg *arg)
		return;

	sel->tags = arg->ui & TAGMASK;
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
	arrange(selmon);
	wl_list_for_each(m, &mons, link) {
		attachclients(m);


@@ 3433,7 3767,7 @@ tagmon(const Arg *arg)
		wl_list_for_each(m, &mons, link) {
			arrange(m);
		}
		focusclient(focustop(sel->mon), 1);
		focusclient(focustop(sel->mon), NULL, 1);
	}
}



@@ 3462,7 3796,7 @@ focusnthclient(const Arg *arg)
		}

		if (i == 0) {
			focusclient(c, 1);
			focusclient(c, NULL, 1);
			return;
		}



@@ 3554,7 3888,7 @@ togglescratch(const Arg *arg)
	if (found) {
		c->tags = VISIBLEON(c, selmon) ? 0 : selmon->tagset[selmon->seltags];

		focusclient(c->tags == 0 ? focustop(selmon) : c, 1);
		focusclient(c->tags == 0 ? focustop(selmon) : c, NULL, 1);
		arrange(selmon);
	} else{
		spawnscratch(arg);


@@ 3576,7 3910,7 @@ toggletag(const Arg *arg)

	sel->tags = newtags;
	attachclients(selmon);
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
	arrange(selmon);
	printstatus();
}


@@ 3601,12 3935,94 @@ toggleview(const Arg *arg)
	applypertag(selmon);
	selmon->tagset[selmon->seltags] = newtagset;
	attachclients(selmon);
	focusclient(focustop(selmon), 1);
	focusclient(focustop(selmon), NULL, 1);
	arrange(selmon);
	printstatus();
}

void
transactionapply(void)
{
	struct wlr_scene_node **ghost;
	Client *c;
	Monitor *m;

	wl_event_source_timer_update(transaction_timer, 0);
	transaction_waiting = 0;

	wl_array_for_each(ghost, &ghosts)
		wlr_scene_node_destroy(*ghost);
	ghosts.size = 0;

	wl_list_for_each(c, &clients, link) {
		c->resize_serial = 0;
		if (!c->mon)
			continue;
		if (c->mon->wlr_output->enabled)
			wlr_scene_node_set_enabled(&c->scene->node, VISIBLEON(c, c->mon));
		applygeom(c);
	}

	wl_list_for_each(m, &mons, link) {
		if (m->wlr_output->enabled)
			wlr_scene_node_set_enabled(&m->fullscreen_bg->node,
					(c = focustop(m)) && c->isfullscreen);
	}

	motionnotify(0, NULL, 0, 0, 0, 0);
	checkidleinhibitor(NULL);
}

void
transactioncheck(void *data)
{
	Client *c;

	transaction_idle = NULL;

	/* Hidden clients don't have to redraw before the layout is shown */
	wl_list_for_each(c, &clients, link) {
		if (c->resize_serial && VISIBLEON(c, c->mon) && c->mon->wlr_output->enabled) {
			if (!transaction_waiting) {
				/* Don't restart the timer on updates of the pending
				 * layout, so the delay is bounded */
				wl_event_source_timer_update(transaction_timer, TRANSACTION_TIMEOUT);
				transaction_waiting = 1;
			}
			return;
		}
	}

	transactionapply();
}

void
transactioncommit(Client *c, int ready)
{
	if (c->resize_serial && ready) {
		c->resize_serial = 0;
		transactionschedule();
	} else if (c->snapshot) {
		/* This buffer is not shown, let the client draw again */
		client_send_frame_done(c);
	}
}

void
transactionschedule(void)
{
	if (!transaction_idle)
		transaction_idle = wl_event_loop_add_idle(event_loop, transactioncheck, NULL);
}

int
transactiontimeout(void *data)
{
	transactionapply();
	return 0;
}

void
unlocksession(struct wl_listener *listener, void *data)
{
	SessionLock *lock = wl_container_of(listener, lock, unlock);


@@ 3620,12 4036,10 @@ unmaplayersurfacenotify(struct wl_listener *listener, void *data)

	l->mapped = 0;
	wlr_scene_node_set_enabled(&l->scene->node, 0);
	if (l == exclusive_focus)
		exclusive_focus = NULL;
	if (l->layer_surface->output && (l->mon = l->layer_surface->output->data))
		arrangelayers(l->mon);
	if (l->layer_surface->surface == seat->keyboard_state.focused_surface)
		focusclient(focustop(selmon), 1);
		focusclient(focustop(selmon), NULL, 1);
	motionnotify(0, NULL, 0, 0, 0, 0);
}



@@ 3634,25 4048,36 @@ unmapnotify(struct wl_listener *listener, void *data)
{
	/* Called when the surface is unmapped, and should no longer be shown. */
	Client *c = wl_container_of(listener, c, unmap);
	struct wlr_scene_node *ghost, **slot;
	if (c == grabc) {
		cursor_mode = CurNormal;
		grabc = NULL;
	}
	if (c == focused_client)
		focused_client = NULL;

	if (c->swallowedby)
		swallow(c->swallowedby, c);

	if (client_is_unmanaged(c)) {
		if (c == exclusive_focus) {
			exclusive_focus = NULL;
			focusclient(focustop(selmon), 1);
	if (!client_is_unmanaged(c)) {
		/* Keep showing the client until the transaction rearranging the
		 * remaining ones is applied, to not show a gap. A swallowed client
		 * is replaced by its swallower, so there is no gap to hide. */
		if (!c->swallowing && !c->swallowedby
				&& (ghost = snapshot(&c->scene->node, c->scene->node.parent, NULL))) {
			if ((slot = wl_array_add(&ghosts, sizeof(ghost))))
				*slot = ghost;
			else
				wlr_scene_node_destroy(ghost);
			transactionschedule();
		}
	} else {
		if (!c->swallowing)
			wl_list_remove(&c->link);
		setmon(c, NULL, 0);
		if (!c->swallowing)
			wl_list_remove(&c->flink);
	} else if (client_wants_focus(c)) {
		focusclient(focustop(selmon), NULL, 1);
	}

	if (c->swallowedby) {


@@ 3667,7 4092,25 @@ unmapnotify(struct wl_listener *listener, void *data)
		c->swallowing = NULL;
	}

	if (c->foreign_toplevel_handle) {
		wlr_ext_foreign_toplevel_handle_v1_destroy(c->foreign_toplevel_handle);
		c->foreign_toplevel_handle = NULL;
	}
#ifdef XWAYLAND
	if (c->type != XDGShell && c->capture.image_capture_scene_surface) {
		wlr_scene_node_destroy(&c->capture.image_capture_scene_surface->buffer->node);
		c->capture.image_capture_scene_surface = NULL;
	}
#endif

	wlr_scene_node_destroy(&c->image_capture_scene->tree.node);
	c->image_capture_scene = NULL;
	c->image_capture_source = NULL;
	wlr_scene_node_destroy(&c->scene->node);
	client_surface(c)->data = NULL;
	c->snapshot = NULL;
	c->resize_serial = 0;
	c->current = (struct wlr_box){0};
	printstatus();
	motionnotify(0, NULL, 0, 0, 0, 0);
}


@@ 3690,10 4133,13 @@ updatemons(struct wl_listener *listener, void *data)

	/* First remove from the layout the disabled monitors */
	wl_list_for_each(m, &mons, link) {
		if (m->wlr_output->enabled || m->asleep)
		if (m->wlr_output->enabled)
			continue;
		config_head = wlr_output_configuration_head_v1_create(config, m->wlr_output);
		config_head->state.enabled = 0;
		if (m->asleep)
			continue;

		/* Remove this output from the layout to avoid cursor enter inside it */
		wlr_output_layout_remove(output_layout, m->wlr_output);
		closemon(m);


@@ 3764,7 4210,7 @@ updatemons(struct wl_listener *listener, void *data)
			if (!c->mon && client_surface(c)->mapped)
				setmon(c, selmon, c->tags);
		}
		focusclient(focustop(selmon), 1);
		focusclient(focustop(selmon), NULL, 1);
		if (selmon->lock_surface) {
			client_notify_enter(selmon->lock_surface->surface,
					wlr_seat_get_keyboard(seat));


@@ 3788,6 4234,15 @@ updatetitle(struct wl_listener *listener, void *data)
	Client *c = wl_container_of(listener, c, set_title);
	if (c == focustop(c->mon))
		printstatus();

	if (c->foreign_toplevel_handle) {
		struct wlr_ext_foreign_toplevel_handle_v1_state foreign_toplevel_state = {
			.app_id = client_get_appid(c),
			.title = client_get_title(c),
		};
		wlr_ext_foreign_toplevel_handle_v1_update_state(c->foreign_toplevel_handle,
				&foreign_toplevel_state);
	}
}

void


@@ 3809,7 4264,7 @@ urgent(struct wl_listener *listener, void *data)
		selmon = c->mon;
		if (!VISIBLEON(c, c->mon))
			view(&((Arg){.ui = c->tags}));
		focusclient(c, 1);
		focusclient(c, NULL, 1);
	} else {
		client_set_border_color(c, urgentcolor);
		printstatus();


@@ 3845,7 4300,7 @@ view(const Arg *arg)
			applypertag(m);
			attachclients(m);
			/* Beware: this changes selmon */
			focusclient(focustop(m), 1);
			focusclient(focustop(m), NULL, 1);
			arrange(m);
			break;
		}


@@ 3862,7 4317,7 @@ view(const Arg *arg)
	selmon = origm;
	applypertag(origm);
	attachclients(origm);
	focusclient(focustop(origm), 1);
	focusclient(focustop(origm), NULL, 1);
	arrange(origm);
	printstatus();
}


@@ 3873,12 4328,14 @@ virtualkeyboard(struct wl_listener *listener, void *data)
	struct wlr_virtual_keyboard_v1 *kb = data;
	/* virtual keyboards shouldn't share keyboard group */
	KeyboardGroup *group = createkeyboardgroup();
	group->virtual_keyboard = &kb->keyboard;
	/* Set the keymap to match the group keymap */
	wlr_keyboard_set_keymap(&kb->keyboard, group->wlr_group->keyboard.keymap);
	LISTEN(&kb->keyboard.base.events.destroy, &group->destroy, destroykeyboardgroup);

	/* Add the new keyboard to the group */
	wlr_keyboard_group_add_keyboard(group->wlr_group, &kb->keyboard);
	wlr_seat_set_capabilities(seat, seat->capabilities | WL_SEAT_CAPABILITY_KEYBOARD);
}

void


@@ 3890,6 4347,7 @@ virtualpointer(struct wl_listener *listener, void *data)
	wlr_cursor_attach_input_device(cursor, device);
	if (event->suggested_output)
		wlr_cursor_map_input_to_output(cursor, device, event->suggested_output);
	wlr_seat_set_capabilities(seat, seat->capabilities | WL_SEAT_CAPABILITY_POINTER);
}

Monitor *


@@ 3903,31 4361,28 @@ void
xytonode(double x, double y, struct wlr_surface **psurface,
		Client **pc, LayerSurface **pl, double *nx, double *ny)
{
	struct wlr_scene_node *node, *pnode;
	struct wlr_scene_node *node;
	struct wlr_scene_surface *scene_surface;
	struct wlr_surface *surface = NULL;
	Client *c = NULL;
	LayerSurface *l = NULL;
	int layer;

	for (layer = NUM_LAYERS - 1; !surface && layer >= 0; layer--) {
		if (!(node = wlr_scene_node_at(&layers[layer]->node, x, y, nx, ny)))
	for (layer = NUM_LAYERS - 1; !surface && !c && layer >= (locked ? LyrBlock : 0); layer--) {
		if (layer == LyrIMPopup ||
				!(node = wlr_scene_node_at(&layers[layer]->node, x, y, nx, ny))
				|| node->type != WLR_SCENE_NODE_BUFFER)
			continue;

		if (node->type == WLR_SCENE_NODE_BUFFER)
			surface = wlr_scene_surface_try_from_buffer(
					wlr_scene_buffer_from_node(node))->surface;
		/* Walk the tree to find a node that knows the client */
		for (pnode = node; pnode && !c; pnode = &pnode->parent->node)
			c = pnode->data;
		if (c && c->type == LayerShell) {
			c = NULL;
			l = pnode->data;
		}
		if ((scene_surface = wlr_scene_surface_try_from_buffer(
				wlr_scene_buffer_from_node(node))))
			surface = scene_surface->surface;
		else /* snapshot of a client during a transaction */
			c = node->data;
	}

	if (psurface) *psurface = surface;
	if (pc) *pc = c;
	if (pl) *pl = l;
	toplevel_from_wlr_surface(surface, pc, pl);
}

void


@@ 3959,7 4414,7 @@ zoom(const Arg *arg)
	wl_list_remove(&sel->link);
	wl_list_insert(&clients, &sel->link);

	focusclient(sel, 1);
	focusclient(sel, NULL, 1);
	arrange(selmon);
}



@@ 3987,22 4442,28 @@ associatex11(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, associate);

	LISTEN(&client_surface(c)->events.commit, &c->commit, commitx11);
	LISTEN(&client_surface(c)->events.map, &c->map, mapnotify);
	LISTEN(&client_surface(c)->events.unmap, &c->unmap, unmapnotify);
}

void
commitx11(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, commit);
	struct wlr_surface *surface = client_surface(c);

	transactioncommit(c, surface->current.width == c->geom.width - 2 * (int)c->bw
			&& surface->current.height == c->geom.height - 2 * (int)c->bw);
}

void
configurex11(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, configure);
	struct wlr_xwayland_surface_configure_event *event = data;
	if (!client_surface(c) || !client_surface(c)->mapped) {
		wlr_xwayland_surface_configure(c->surface.xwayland,
				event->x, event->y, event->width, event->height);
		return;
	}
	if (client_is_unmanaged(c)) {
		wlr_scene_node_set_position(&c->scene->node, event->x, event->y);
	if (!client_surface(c) || !client_surface(c)->mapped
			|| client_is_unmanaged(c)) {
		wlr_xwayland_surface_configure(c->surface.xwayland,
				event->x, event->y, event->width, event->height);
		return;


@@ 4035,6 4496,7 @@ createnotifyx11(struct wl_listener *listener, void *data)
	LISTEN(&xsurface->events.request_activate, &c->activate, activatex11);
	LISTEN(&xsurface->events.request_configure, &c->configure, configurex11);
	LISTEN(&xsurface->events.request_fullscreen, &c->fullscreen, fullscreennotify);
	LISTEN(&xsurface->events.set_geometry, &c->set_geometry, setgeometryx11);
	LISTEN(&xsurface->events.set_hints, &c->set_hints, sethints);
	LISTEN(&xsurface->events.set_title, &c->set_title, updatetitle);
}


@@ 4043,11 4505,25 @@ void
dissociatex11(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, dissociate);
	wl_list_remove(&c->commit.link);
	wl_list_remove(&c->map.link);
	wl_list_remove(&c->unmap.link);
}

void
setgeometryx11(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, set_geometry);

	if (!client_is_unmanaged(c) || !client_surface(c)
			|| !client_surface(c)->mapped)
		return;

	client_get_geometry(c, &c->geom);
	wlr_scene_node_set_position(&c->scene->node, c->geom.x, c->geom.y);
}

void
sethints(struct wl_listener *listener, void *data)
{
	Client *c = wl_container_of(listener, c, set_hints);


@@ 4066,7 4542,7 @@ sethints(struct wl_listener *listener, void *data)
			selmon = c->mon;
			if (!VISIBLEON(c, c->mon))
				view(&((Arg){.ui = c->tags}));
			focusclient(c, 1);
			focusclient(c, NULL, 1);
		} else {
			client_set_border_color(c, urgentcolor);
			printstatus();


@@ 4084,9 4560,7 @@ xwaylandready(struct wl_listener *listener, void *data)

	/* Set the default XWayland cursor to match the rest of dwl. */
	if ((xcursor = wlr_xcursor_manager_get_xcursor(cursor_mgr, "default", 1)))
		wlr_xwayland_set_cursor(xwayland,
				xcursor->images[0]->buffer, xcursor->images[0]->width * 4,
				xcursor->images[0]->width, xcursor->images[0]->height,
		wlr_xwayland_set_cursor(xwayland, wlr_xcursor_image_get_buffer(xcursor->images[0]),
				xcursor->images[0]->hotspot_x, xcursor->images[0]->hotspot_y);
}
#endif

A ime.h => ime.h +639 -0
@@ 0,0 1,639 @@
#include <wlr/types/wlr_input_method_v2.h>
#include <wlr/types/wlr_text_input_v3.h>

struct input_method_relay {
	struct wl_list text_inputs; /* struct text_input.link */
	struct wlr_input_method_v2 *input_method;

	struct wlr_surface *focused_surface;
	struct text_input *active_text_input;

	struct wl_list popups; /* input_method_popup.link */
	struct wlr_scene_tree *popup_tree;

	struct wl_listener new_text_input;
	struct wl_listener new_input_method;

	struct wl_listener input_method_commit;
	struct wl_listener input_method_grab_keyboard;
	struct wl_listener input_method_destroy;
	struct wl_listener input_method_new_popup_surface;

	struct wl_listener keyboard_grab_destroy;
	struct wl_listener focused_surface_destroy;
};

struct input_method_popup {
	struct wlr_input_popup_surface_v2 *popup_surface;
	struct wlr_scene_tree *tree;
	struct wlr_scene_tree *scene_surface;
	struct input_method_relay *relay;
	struct wl_list link; /* input_method_relay.popups */

	struct wl_listener destroy;
	struct wl_listener commit;
};

struct text_input {
	struct input_method_relay *relay;
	struct wlr_text_input_v3 *input;
	struct wl_list link;

	struct wl_listener enable;
	struct wl_listener commit;
	struct wl_listener disable;
	struct wl_listener destroy;
};

struct wlr_input_method_manager_v2 *input_method_manager;
struct wlr_text_input_manager_v3 *text_input_manager;
struct input_method_relay *input_method_relay;

/* functions used in dwl.c */
static int input_method_keyboard_grab_forward_key(
		KeyboardGroup *keyboard, struct wlr_keyboard_key_event *event);
static int input_method_keyboard_grab_forward_modifiers(KeyboardGroup *keyboard);
static struct input_method_relay *input_method_relay_create(void);
static void input_method_relay_finish(struct input_method_relay *relay);
static void input_method_relay_set_focus(struct input_method_relay *relay,
		struct wlr_surface *surface);

/* internal functions */
static struct text_input *get_active_text_input(struct input_method_relay *relay);
static struct wlr_input_method_keyboard_grab_v2 *get_keyboard_grab(KeyboardGroup *keyboard);
static int keyboard_is_emulated_by_input_method(struct wlr_keyboard *keyboard,
		struct wlr_input_method_v2 *input_method);
static void handle_focused_surface_destroy(struct wl_listener *listener, void *data);
static void handle_input_method_commit(struct wl_listener *listener, void *data);
static void handle_input_method_destroy(struct wl_listener *listener, void *data);
static void handle_input_method_grab_keyboard(struct wl_listener *listener, void *data);
static void handle_input_method_new_popup_surface(struct wl_listener *listener, void *data);
static void handle_keyboard_grab_destroy(struct wl_listener *listener, void *data);
static void handle_new_input_method(struct wl_listener *listener, void *data);
static void handle_new_text_input(struct wl_listener *listener, void *data);
static void handle_popup_surface_commit(struct wl_listener *listener, void *data);
static void handle_popup_surface_destroy(struct wl_listener *listener, void *data);
static void handle_text_input_commit(struct wl_listener *listener, void *data);
static void handle_text_input_destroy(struct wl_listener *listener, void *data);
static void handle_text_input_disable(struct wl_listener *listener, void *data);
static void handle_text_input_enable(struct wl_listener *listener, void *data);
static Monitor *output_from_wlr_output(struct wlr_output *wlr_output);
static Monitor *output_nearest_to(int lx, int ly);
static void send_state_to_input_method(struct input_method_relay *relay);
static void update_active_text_input(struct input_method_relay *relay);
static void update_popup_position(struct input_method_popup *popup);
static void update_popups_position(struct input_method_relay *relay);
static void update_text_inputs_focused_surface(struct input_method_relay *relay);

Monitor *
output_from_wlr_output(struct wlr_output *wlr_output)
{
	Monitor *m = NULL;
	wl_list_for_each(m, &mons, link) {
		if (m->wlr_output == wlr_output)
			return m;
	}
	return NULL;
}

Monitor *
output_nearest_to(int lx, int ly)
{
	double closest_x, closest_y;
	wlr_output_layout_closest_point(output_layout, NULL, lx, ly, &closest_x,
			&closest_y);

	return output_from_wlr_output(
			wlr_output_layout_output_at(output_layout, closest_x, closest_y));
}

int
keyboard_is_emulated_by_input_method(struct wlr_keyboard *keyboard,
		struct wlr_input_method_v2 *input_method)
{
	struct wlr_virtual_keyboard_v1 *virtual_keyboard;

	if (!keyboard || !input_method)
		return 0;

	virtual_keyboard = wlr_input_device_get_virtual_keyboard(&keyboard->base);

	return virtual_keyboard &&
			wl_resource_get_client(virtual_keyboard->resource) ==
				wl_resource_get_client(input_method->resource);
}

struct wlr_input_method_keyboard_grab_v2 *
get_keyboard_grab(KeyboardGroup *keyboard)
{
	struct wlr_input_method_v2 *input_method = input_method_relay->input_method;

	if (!input_method || !input_method->keyboard_grab || keyboard != kb_group ||
			keyboard_is_emulated_by_input_method(keyboard->virtual_keyboard,
				input_method))
		return NULL;

	return input_method->keyboard_grab;
}

int
input_method_keyboard_grab_forward_modifiers(KeyboardGroup *keyboard)
{
	struct wlr_input_method_keyboard_grab_v2 *keyboard_grab =
		get_keyboard_grab(keyboard);

	struct wlr_keyboard_modifiers *modifiers =
		&keyboard->wlr_group->keyboard.modifiers;

	if (keyboard_grab) {
		wlr_input_method_keyboard_grab_v2_set_keyboard(
				keyboard_grab, &keyboard->wlr_group->keyboard);
		wlr_input_method_keyboard_grab_v2_send_modifiers(keyboard_grab, modifiers);
		return 1;
	} else {
		return 0;
	}
}

int
input_method_keyboard_grab_forward_key(
		KeyboardGroup *keyboard, struct wlr_keyboard_key_event *event)
{
	struct wlr_input_method_keyboard_grab_v2 *keyboard_grab =
		get_keyboard_grab(keyboard);
	if (keyboard_grab) {
		wlr_input_method_keyboard_grab_v2_set_keyboard(
				keyboard_grab, &keyboard->wlr_group->keyboard);
		wlr_input_method_keyboard_grab_v2_send_key(keyboard_grab, event->time_msec,
				event->keycode, event->state);
		return 1;
	} else {
		return 0;
	}
}

struct text_input *
get_active_text_input(struct input_method_relay *relay)
{
	struct text_input *text_input;

	if (!relay->input_method)
		return NULL;

	wl_list_for_each(text_input, &relay->text_inputs, link) {
		if (text_input->input->focused_surface &&
				text_input->input->current_enabled)
			return text_input;
	}
	return NULL;
}

void
update_active_text_input(struct input_method_relay *relay)
{
	struct text_input *active_text_input = get_active_text_input(relay);

	if (relay->input_method && relay->active_text_input != active_text_input) {
		if (active_text_input)
			wlr_input_method_v2_send_activate(relay->input_method);
		else
			wlr_input_method_v2_send_deactivate(relay->input_method);
		wlr_input_method_v2_send_done(relay->input_method);
	}

	relay->active_text_input = active_text_input;
}

void
update_text_inputs_focused_surface(struct input_method_relay *relay)
{
	struct text_input *text_input;
	wl_list_for_each(text_input, &relay->text_inputs, link) {
		struct wlr_text_input_v3 *input = text_input->input;

		struct wlr_surface *new_focused_surface;
		if (relay->input_method && relay->focused_surface &&
			wl_resource_get_client(input->resource) ==
				wl_resource_get_client(relay->focused_surface->resource)) {
			new_focused_surface = relay->focused_surface;
		} else {
			new_focused_surface = NULL;
		}

		if (input->focused_surface == new_focused_surface)
			continue;
		if (input->focused_surface)
			wlr_text_input_v3_send_leave(input);
		if (new_focused_surface)
			wlr_text_input_v3_send_enter(input, new_focused_surface);
	}
}

void
update_popup_position(struct input_method_popup *popup)
{
	struct input_method_relay *relay = popup->relay;
	struct text_input *text_input = relay->active_text_input;
	struct wlr_box cursor_rect;
	struct wlr_xdg_surface *xdg_surface;
	struct wlr_layer_surface_v1 *layer_surface;
	struct wlr_scene_tree *tree;
	int lx, ly;
	Monitor *output;
	struct wlr_box output_box;
	struct wlr_xdg_positioner_rules positioner_rules;
	struct wlr_box popup_box;

	if (!text_input || !relay->focused_surface ||
			!popup->popup_surface->surface->mapped)
		return;

	xdg_surface = wlr_xdg_surface_try_from_wlr_surface(relay->focused_surface);
	layer_surface = wlr_layer_surface_v1_try_from_wlr_surface(relay->focused_surface);

	if ((text_input->input->current.features &
				WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE) &&
			(xdg_surface || layer_surface)) {
		cursor_rect = text_input->input->current.cursor_rectangle;

		tree = relay->focused_surface->data;
		wlr_scene_node_coords(&tree->node, &lx, &ly);
		cursor_rect.x += lx;
		cursor_rect.y += ly;

		if (xdg_surface) {
			cursor_rect.x -= xdg_surface->geometry.x;
			cursor_rect.y -= xdg_surface->geometry.y;
		}
	} else {
		cursor_rect = (struct wlr_box){0};
	}

	output = output_nearest_to(cursor_rect.x, cursor_rect.y);
	if (!output || !output->wlr_output->enabled)
		return;

	wlr_output_layout_get_box(output_layout, output->wlr_output, &output_box);

	positioner_rules = (struct wlr_xdg_positioner_rules) {
		.anchor_rect = cursor_rect,
		.anchor = XDG_POSITIONER_ANCHOR_BOTTOM_LEFT,
		.gravity = XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT,
		.size =
		{
			.width = popup->popup_surface->surface->current.width,
			.height = popup->popup_surface->surface->current.height,
		},
		.constraint_adjustment = XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y |
			XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X,
	};

	wlr_xdg_positioner_rules_get_geometry(&positioner_rules, &popup_box);
	wlr_xdg_positioner_rules_unconstrain_box(&positioner_rules, &output_box, &popup_box);

	wlr_scene_node_set_position(&popup->tree->node, popup_box.x, popup_box.y);
	wlr_scene_node_raise_to_top(&relay->popup_tree->node);

	wlr_input_popup_surface_v2_send_text_input_rectangle(
			popup->popup_surface, &(struct wlr_box){
			.x = cursor_rect.x - popup_box.x,
			.y = cursor_rect.y - popup_box.y,
			.width = cursor_rect.width,
			.height = cursor_rect.height,
			});
}

void
update_popups_position(struct input_method_relay *relay)
{
	struct input_method_popup *popup;
	wl_list_for_each(popup, &relay->popups, link) {
		update_popup_position(popup);
	}
}

void
handle_input_method_commit(struct wl_listener *listener,
		void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, input_method_commit);
	struct wlr_input_method_v2 *input_method = relay->input_method;
	struct text_input *text_input = relay->active_text_input;

	if (!text_input)
		return;

	if (input_method->current.preedit.text) {
		wlr_text_input_v3_send_preedit_string(text_input->input,
				input_method->current.preedit.text,
				input_method->current.preedit.cursor_begin,
				input_method->current.preedit.cursor_end);
	}
	if (input_method->current.commit_text) {
		wlr_text_input_v3_send_commit_string(text_input->input,
				input_method->current.commit_text);
	}
	if (input_method->current.delete.before_length ||
			input_method->current.delete.after_length) {
		wlr_text_input_v3_send_delete_surrounding_text(text_input->input,
				input_method->current.delete.before_length,
				input_method->current.delete.after_length);
	}
	wlr_text_input_v3_send_done(text_input->input);
}

void
handle_keyboard_grab_destroy(struct wl_listener *listener,
		void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, keyboard_grab_destroy);
	struct wlr_input_method_keyboard_grab_v2 *keyboard_grab =
		relay->input_method->keyboard_grab;
	wl_list_remove(&relay->keyboard_grab_destroy.link);

	if (keyboard_grab->keyboard) {
		/* Send modifier state to original client */
		wlr_seat_keyboard_notify_modifiers(keyboard_grab->input_method->seat,
				&keyboard_grab->keyboard->modifiers);
	}
}

void
handle_input_method_grab_keyboard(struct wl_listener *listener,
		void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, input_method_grab_keyboard);
	struct wlr_input_method_keyboard_grab_v2 *keyboard_grab = data;

	struct wlr_keyboard *active_keyboard = &kb_group->wlr_group->keyboard;

	if (!keyboard_is_emulated_by_input_method(active_keyboard,
				relay->input_method)) {
		/* Send modifier state to grab */
		wlr_input_method_keyboard_grab_v2_set_keyboard(keyboard_grab,
				active_keyboard);
	}

	relay->keyboard_grab_destroy.notify = handle_keyboard_grab_destroy;
	wl_signal_add(&keyboard_grab->events.destroy, &relay->keyboard_grab_destroy);
}

void
handle_input_method_destroy(struct wl_listener *listener,
		void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, input_method_destroy);
	wl_list_remove(&relay->input_method_commit.link);
	wl_list_remove(&relay->input_method_grab_keyboard.link);
	wl_list_remove(&relay->input_method_new_popup_surface.link);
	wl_list_remove(&relay->input_method_destroy.link);
	relay->input_method = NULL;

	update_text_inputs_focused_surface(relay);
	update_active_text_input(relay);
}

void
handle_popup_surface_destroy(struct wl_listener *listener,
		void *data)
{
	struct input_method_popup *popup = wl_container_of(listener, popup, destroy);
	wlr_scene_node_destroy(&popup->tree->node);
	wl_list_remove(&popup->destroy.link);
	wl_list_remove(&popup->commit.link);
	wl_list_remove(&popup->link);
	free(popup);
}

void
handle_popup_surface_commit(struct wl_listener *listener,
		void *data)
{
	struct input_method_popup *popup = wl_container_of(listener, popup, commit);
	update_popup_position(popup);
}

void
handle_input_method_new_popup_surface(struct wl_listener *listener,
		void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, input_method_new_popup_surface);

	struct input_method_popup *popup = ecalloc(1, sizeof(*popup));
	popup->popup_surface = data;
	popup->relay = relay;

	popup->destroy.notify = handle_popup_surface_destroy;
	wl_signal_add(&popup->popup_surface->events.destroy, &popup->destroy);

	popup->commit.notify = handle_popup_surface_commit;
	wl_signal_add(&popup->popup_surface->surface->events.commit, &popup->commit);

	popup->tree = wlr_scene_tree_create(layers[LyrIMPopup]);
	popup->scene_surface = wlr_scene_subsurface_tree_create(
			popup->tree, popup->popup_surface->surface);
	popup->scene_surface->node.data = popup;

	wl_list_insert(&relay->popups, &popup->link);

	update_popup_position(popup);
}

void
handle_new_input_method(struct wl_listener *listener, void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, new_input_method);
	struct wlr_input_method_v2 *input_method = data;
	if (seat != input_method->seat)
		return;

	if (relay->input_method) {
		wlr_input_method_v2_send_unavailable(input_method);
		return;
	}

	relay->input_method = input_method;

	relay->input_method_commit.notify = handle_input_method_commit;
	wl_signal_add(&relay->input_method->events.commit,
			&relay->input_method_commit);

	relay->input_method_grab_keyboard.notify = handle_input_method_grab_keyboard;
	wl_signal_add(&relay->input_method->events.grab_keyboard,
			&relay->input_method_grab_keyboard);

	relay->input_method_destroy.notify = handle_input_method_destroy;
	wl_signal_add(&relay->input_method->events.destroy,
			&relay->input_method_destroy);

	relay->input_method_new_popup_surface.notify =
		handle_input_method_new_popup_surface;
	wl_signal_add(&relay->input_method->events.new_popup_surface,
			&relay->input_method_new_popup_surface);

	update_text_inputs_focused_surface(relay);
	update_active_text_input(relay);
}

void
send_state_to_input_method(struct input_method_relay *relay)
{
	struct wlr_input_method_v2 *input_method = relay->input_method;
	struct wlr_text_input_v3 *input = relay->active_text_input->input;

	if (input->active_features & WLR_TEXT_INPUT_V3_FEATURE_SURROUNDING_TEXT) {
		wlr_input_method_v2_send_surrounding_text(input_method,
				input->current.surrounding.text,
				input->current.surrounding.cursor,
				input->current.surrounding.anchor);
	}
	wlr_input_method_v2_send_text_change_cause(input_method,
			input->current.text_change_cause);
	if (input->active_features & WLR_TEXT_INPUT_V3_FEATURE_CONTENT_TYPE) {
		wlr_input_method_v2_send_content_type(input_method,
				input->current.content_type.hint,
				input->current.content_type.purpose);
	}
	wlr_input_method_v2_send_done(input_method);
}

void
handle_text_input_enable(struct wl_listener *listener, void *data)
{
	struct text_input *text_input = wl_container_of(listener, text_input, enable);
	struct input_method_relay *relay = text_input->relay;

	update_active_text_input(relay);
	if (relay->active_text_input == text_input) {
		update_popups_position(relay);
		send_state_to_input_method(relay);
	}
	wlr_text_input_v3_send_done(text_input->input);
}

void
handle_text_input_disable(struct wl_listener *listener,
		void *data)
{
	struct text_input *text_input =
		wl_container_of(listener, text_input, disable);
	update_active_text_input(text_input->relay);
}

void
handle_text_input_commit(struct wl_listener *listener, void *data)
{
	struct text_input *text_input = wl_container_of(listener, text_input, commit);
	struct input_method_relay *relay = text_input->relay;

	if (relay->active_text_input == text_input) {
		update_popups_position(relay);
		send_state_to_input_method(relay);
	}
}

void
handle_text_input_destroy(struct wl_listener *listener,
		void *data)
{
	struct text_input *text_input =
		wl_container_of(listener, text_input, destroy);
	wl_list_remove(&text_input->enable.link);
	wl_list_remove(&text_input->disable.link);
	wl_list_remove(&text_input->commit.link);
	wl_list_remove(&text_input->destroy.link);
	wl_list_remove(&text_input->link);
	update_active_text_input(text_input->relay);
	free(text_input);
}

void
handle_new_text_input(struct wl_listener *listener, void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, new_text_input);
	struct wlr_text_input_v3 *wlr_text_input = data;
	struct text_input *text_input;

	if (seat != wlr_text_input->seat)
		return;

	text_input = ecalloc(1, sizeof(*text_input));
	text_input->input = wlr_text_input;
	text_input->relay = relay;
	wl_list_insert(&relay->text_inputs, &text_input->link);

	text_input->enable.notify = handle_text_input_enable;
	wl_signal_add(&text_input->input->events.enable, &text_input->enable);

	text_input->disable.notify = handle_text_input_disable;
	wl_signal_add(&text_input->input->events.disable, &text_input->disable);

	text_input->commit.notify = handle_text_input_commit;
	wl_signal_add(&text_input->input->events.commit, &text_input->commit);

	text_input->destroy.notify = handle_text_input_destroy;
	wl_signal_add(&text_input->input->events.destroy, &text_input->destroy);

	update_text_inputs_focused_surface(relay);
}

void
handle_focused_surface_destroy(struct wl_listener *listener,
		void *data)
{
	struct input_method_relay *relay =
		wl_container_of(listener, relay, focused_surface_destroy);
	input_method_relay_set_focus(relay, NULL);
}

struct input_method_relay *
input_method_relay_create(void)
{
	struct input_method_relay *relay = ecalloc(1, sizeof(*relay));
	wl_list_init(&relay->text_inputs);
	wl_list_init(&relay->popups);
	relay->popup_tree = wlr_scene_tree_create(&scene->tree);

	relay->new_text_input.notify = handle_new_text_input;
	wl_signal_add(&text_input_manager->events.new_text_input, &relay->new_text_input);

	relay->new_input_method.notify = handle_new_input_method;
	wl_signal_add(&input_method_manager->events.new_input_method,
			&relay->new_input_method);

	relay->focused_surface_destroy.notify = handle_focused_surface_destroy;

	return relay;
}

void
input_method_relay_finish(struct input_method_relay *relay)
{
	wl_list_remove(&relay->new_text_input.link);
	wl_list_remove(&relay->new_input_method.link);
	free(relay);
}

void
input_method_relay_set_focus(struct input_method_relay *relay,
		struct wlr_surface *surface)
{
	if (!relay || relay->focused_surface == surface)
		return;

	if (relay->focused_surface)
		wl_list_remove(&relay->focused_surface_destroy.link);
	relay->focused_surface = surface;
	if (surface)
		wl_signal_add(&surface->events.destroy, &relay->focused_surface_destroy);

	update_text_inputs_focused_surface(relay);
	update_active_text_input(relay);
}

D util.c => util.c +0 -51
@@ 1,51 0,0 @@
/* See LICENSE.dwm file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "util.h"

void
die(const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}

	exit(1);
}

void *
ecalloc(size_t nmemb, size_t size)
{
	void *p;

	if (!(p = calloc(nmemb, size)))
		die("calloc:");
	return p;
}

int
fd_set_nonblock(int fd) {
	int flags = fcntl(fd, F_GETFL);
	if (flags < 0) {
		perror("fcntl(F_GETFL):");
		return -1;
	}
	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
		perror("fcntl(F_SETFL):");
		return -1;
	}

	return 0;
}

M util.h => util.h +68 -3
@@ 1,5 1,70 @@
/* See LICENSE.dwm file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <xkbcommon/xkbcommon.h>

void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);
int fd_set_nonblock(int fd);
#define MAX(A, B)               ((A) > (B) ? (A) : (B))
#define MIN(A, B)               ((A) < (B) ? (A) : (B))
#define LENGTH(X)               (sizeof X / sizeof X[0])
#define END(A)                  ((A) + LENGTH(A))

static void die(const char *fmt, ...);
static void *ecalloc(size_t nmemb, size_t size);
static int fd_set_nonblock(int fd);
xkb_keysym_t keymap_get_one_sym_by_level(struct xkb_keymap *keymap,
        xkb_keycode_t key, xkb_layout_index_t layout, xkb_level_index_t level);

void
die(const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}

	exit(1);
}

void *
ecalloc(size_t nmemb, size_t size)
{
	void *p;

	if (!(p = calloc(nmemb, size)))
		die("calloc:");
	return p;
}

int
fd_set_nonblock(int fd) {
	int flags = fcntl(fd, F_GETFL);
	if (flags < 0) {
		perror("fcntl(F_GETFL):");
		return -1;
	}
	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
		perror("fcntl(F_SETFL):");
		return -1;
	}

	return 0;
}

xkb_keysym_t
keymap_get_one_sym_by_level(struct xkb_keymap *keymap, xkb_keycode_t key,
        xkb_layout_index_t layout, xkb_level_index_t level)
{
    const xkb_keysym_t *syms;
    int count = xkb_keymap_key_get_syms_by_level(keymap, key, layout, level, &syms);
    return count ? syms[0] : XKB_KEY_NoSymbol;
}