SWOv git · main
SWOv - window overview for sway
C 97.3% Markdown 2.4%git clone https://git.christianimmanuel.de/sway/SWOv.gitwget https://git.christianimmanuel.de/sway/SWOv/archive/SWOv.tar.gzFix key input
Makefile | 5 +++- README.md | 5 ++++ commit_message.txt | 9 +++++++ sw_keymap.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++++----- sw_wl.h | 48 +++++++++++++++++++++++++++++---- swov.c | 2 +- 6 files changed, 134 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 15ab6e5..6b4dbaf 100644 --- a/Makefile +++ b/Makefile @@ -26,12 +26,15 @@ bindir ?= $(PREFIX)/bin datadir ?= $(PREFIX)/share/swov shdatadir ?= $(PREFIX)/share/sw -BUILD := $(shell md5sum swov.c 2>/dev/null | cut -c1-8) SRC := swov.c HDR := sw_theme.h nanosvg.h nanosvgrast.h stb_image.h stb_truetype.h \ sw_draw.h sw_wl.h sw_keymap.h BIN := swov +# the build id covers the headers too: a fix in the shared drawing layer is +# a different build even when swov.c has not moved +BUILD := $(shell cat $(SRC) $(HDR) 2>/dev/null | md5sum | cut -c1-8) + WL_CFLAGS := $(shell $(PKG_CONFIG) --cflags wayland-client 2>/dev/null) WL_LIBS := $(shell $(PKG_CONFIG) --libs wayland-client 2>/dev/null || echo -lwayland-client) diff --git a/README.md b/README.md index 2edef7c..1655dad 100644 --- a/README.md +++ b/README.md @@ -477,6 +477,11 @@ keep your own copy. Edits you make there are overwritten on install. is resized, unmapped or restacked while it is up. - Keyboard layout comes from the compositor's own keymap, so dead keys, AltGr and non-US layouts type correctly. There is no `libxkbcommon` to install. + A keymap it cannot read costs you your layout, not your keyboard: those + keys fall back to a built-in US one. +- If a key does nothing, run it from a terminal with `SW_DEBUG_KEYS=1`. It + prints the keymap it was given and whether it could be read, when it gains + and loses the keyboard, and every key with its code, keysym and character. - `ssaa` is accepted and ignored. Every shape is antialiased as it is drawn, so there is nothing left to supersample. - Tabbed and stacked containers are drawn the way sway draws them: a tab strip diff --git a/commit_message.txt b/commit_message.txt index 4e5b3b7..a6923be 100644 --- a/commit_message.txt +++ b/commit_message.txt @@ -31,5 +31,14 @@ sw_wl.h: SwEvent.code carries the raw evdev keycode, so a program can read a key by its place on the board; and sw_win_create accepts a NULL SwApp, which a program driving its own frames has no use for and which used to crash. +sw_keymap.h: a built-in US layout as a fallback. Any keycode the parsed +keymap has nothing for, and every key when the parse failed outright, comes +from a compact evdev table instead of returning nothing. A keymap this +parser cannot read now costs the user their layout, not their keyboard. + +sw_wl.h: SW_DEBUG_KEYS=1 in the environment narrates the keyboard to stderr +— the keymap's size and whether it parsed, keyboard enter and leave, and +every key with its code, keysym, modifiers and character. + Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SuAfijAAYjLpcVwhCq3AF4 diff --git a/sw_keymap.h b/sw_keymap.h index a1cb9a7..245fc8b 100644 --- a/sw_keymap.h +++ b/sw_keymap.h @@ -53,7 +53,9 @@ typedef struct { bool ok; } SwKeymap; -/* Parse what arrived on wl_keyboard.keymap. Safe to call again on a change. */ +/* Parse what arrived on wl_keyboard.keymap. Safe to call again on a change. + * A keymap this cannot read is not fatal: any keycode it has nothing for + * falls back to a built-in US layout, so the program still takes input. */ bool sw_keymap_parse(SwKeymap *km, const char *text, size_t len); /* The keysym for a keycode under the current modifiers, and what it types. @@ -966,18 +968,82 @@ unsigned sw_keymap_mods(const SwKeymap *km, uint32_t depressed) return m; } +/* A plain US layout by evdev code, used for any key the parsed keymap has + * nothing for — and for every key if the keymap could not be read at all. + * It is not meant to be right for a German or French board; it is there so a + * keymap this parser chokes on costs the user their layout, not their + * keyboard. Index is the evdev code (xkb keycode minus 8); the two entries + * are the plain and the shifted keysym. */ +typedef struct { uint16_t code; uint32_t plain, shift; } SwKcDefault; +static const SwKcDefault SWK_US[] = { + { 1, SW_KEY_ESCAPE, SW_KEY_ESCAPE }, + { 2, '1', '!' }, { 3, '2', '@' }, { 4, '3', '#' }, { 5, '4', '$' }, + { 6, '5', '%' }, { 7, '6', '^' }, { 8, '7', '&' }, { 9, '8', '*' }, + { 10, '9', '(' }, { 11, '0', ')' }, { 12, '-', '_' }, { 13, '=', '+' }, + { 14, SW_KEY_BACKSPACE, SW_KEY_BACKSPACE }, { 15, SW_KEY_TAB, 0xfe20 }, + { 16, 'q', 'Q' }, { 17, 'w', 'W' }, { 18, 'e', 'E' }, { 19, 'r', 'R' }, + { 20, 't', 'T' }, { 21, 'y', 'Y' }, { 22, 'u', 'U' }, { 23, 'i', 'I' }, + { 24, 'o', 'O' }, { 25, 'p', 'P' }, { 26, '[', '{' }, { 27, ']', '}' }, + { 28, SW_KEY_RETURN, SW_KEY_RETURN }, + { 30, 'a', 'A' }, { 31, 's', 'S' }, { 32, 'd', 'D' }, { 33, 'f', 'F' }, + { 34, 'g', 'G' }, { 35, 'h', 'H' }, { 36, 'j', 'J' }, { 37, 'k', 'K' }, + { 38, 'l', 'L' }, { 39, ';', ':' }, { 40, '\'', '"' }, { 41, '`', '~' }, + { 43, '\\', '|' }, + { 44, 'z', 'Z' }, { 45, 'x', 'X' }, { 46, 'c', 'C' }, { 47, 'v', 'V' }, + { 48, 'b', 'B' }, { 49, 'n', 'N' }, { 50, 'm', 'M' }, { 51, ',', '<' }, + { 52, '.', '>' }, { 53, '/', '?' }, { 55, '*', '*' }, + { 57, ' ', ' ' }, + { 59, 0xffbe, 0xffbe }, { 60, 0xffbf, 0xffbf }, { 61, 0xffc0, 0xffc0 }, + { 62, 0xffc1, 0xffc1 }, { 63, 0xffc2, 0xffc2 }, { 64, 0xffc3, 0xffc3 }, + { 65, 0xffc4, 0xffc4 }, { 66, 0xffc5, 0xffc5 }, { 67, 0xffc6, 0xffc6 }, + { 68, 0xffc7, 0xffc7 }, { 87, 0xffc8, 0xffc8 }, { 88, 0xffc9, 0xffc9 }, + { 71, 0xffb7, 0xff95 }, { 72, 0xffb8, 0xff97 }, { 73, 0xffb9, 0xff9a }, + { 74, '-', '-' }, + { 75, 0xffb4, 0xff96 }, { 76, 0xffb5, 0xff9d }, { 77, 0xffb6, 0xff98 }, + { 78, '+', '+' }, + { 79, 0xffb1, 0xff9c }, { 80, 0xffb2, 0xff99 }, { 81, 0xffb3, 0xff9b }, + { 82, 0xffb0, 0xff9e }, { 83, 0xffae, 0xff9f }, + { 96, SW_KEY_KP_ENTER, SW_KEY_KP_ENTER }, { 98, '/', '/' }, + { 102, SW_KEY_HOME, SW_KEY_HOME }, { 103, SW_KEY_UP, SW_KEY_UP }, + { 104, SW_KEY_PAGE_UP, SW_KEY_PAGE_UP }, { 105, SW_KEY_LEFT, SW_KEY_LEFT }, + { 106, SW_KEY_RIGHT, SW_KEY_RIGHT }, { 107, SW_KEY_END, SW_KEY_END }, + { 108, SW_KEY_DOWN, SW_KEY_DOWN }, { 109, SW_KEY_PAGE_DOWN, SW_KEY_PAGE_DOWN }, + { 110, SW_KEY_INSERT, SW_KEY_INSERT }, { 111, SW_KEY_DELETE, SW_KEY_DELETE } +}; + +static uint32_t swk_default_sym(uint32_t keycode, bool shift) +{ + uint32_t ev; + size_t i; + if (keycode < 8) return 0; + ev = keycode - 8; + for (i = 0; i < sizeof(SWK_US) / sizeof(SWK_US[0]); ++i) + if (SWK_US[i].code == ev) return shift ? SWK_US[i].shift : SWK_US[i].plain; + return 0; +} + uint32_t sw_keymap_lookup(const SwKeymap *km, uint32_t keycode, unsigned mods, char *utf8, size_t n) { + bool shift = (mods & 1u) != 0, altgr = (mods & 16u) != 0, caps = (mods & 32u) != 0; + uint32_t sym; + int level; + if (utf8 && n) utf8[0] = 0; - if (!km || !km->ok || keycode >= SW_KEYMAP_MAX) return 0; + if (keycode >= SW_KEYMAP_MAX) return 0; - bool shift = (mods & 1u) != 0, altgr = (mods & 16u) != 0, caps = (mods & 32u) != 0; - int level = km->keypad[keycode] ? ((mods & 64u) ? 1 : 0) - : ((shift ? 1 : 0) + (altgr ? 2 : 0)); - uint32_t sym = km->sym[keycode][level]; + if (!km || !km->ok) { /* nothing parsed: the built-in one */ + sym = swk_default_sym(keycode, shift); + if (utf8 && n && !(mods & 2u)) swk_utf8(sw_keysym_to_codepoint(sym), utf8, n); + return sym; + } + + level = km->keypad[keycode] ? ((mods & 64u) ? 1 : 0) + : ((shift ? 1 : 0) + (altgr ? 2 : 0)); + sym = km->sym[keycode][level]; if (!sym && level) sym = km->sym[keycode][level & 1]; if (!sym) sym = km->sym[keycode][0]; + if (!sym) sym = swk_default_sym(keycode, shift); /* a key it had nothing for */ /* Caps lock is shift, but only for letters — it must not turn 1 into !. */ if (caps && !shift) { diff --git a/sw_wl.h b/sw_wl.h index 8f3b0df..f71631b 100644 --- a/sw_wl.h +++ b/sw_wl.h @@ -782,15 +782,47 @@ static const struct wl_pointer_listener sw_ptr_listener = { static SwKeymap SW_KEYMAP; static unsigned SW_MODS; +/* SW_DEBUG_KEYS=1 in the environment narrates the keyboard to stderr: what + keymap arrived and whether it could be read, when the surface gains and + loses the keyboard, and every key with its code, keysym and character. + One run of that says which of those is missing. */ +static int sw_dbg_keys(void) +{ + static int on = -1; + if (on < 0) { + const char *e = getenv("SW_DEBUG_KEYS"); + on = (e && *e && *e != '0') ? 1 : 0; + } + return on; +} + static void sw_kbd_keymap(void *d, struct wl_keyboard *k, uint32_t format, int32_t fd, uint32_t size) { (void)d; (void)k; - if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { close(fd); return; } - char *text = (char *)mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); - if (text != MAP_FAILED) { - sw_keymap_parse(&SW_KEYMAP, text, size); - munmap(text, size); + if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { + if (sw_dbg_keys()) + fprintf(stderr, "sw: keymap in format %u, not xkb — using the built-in one\n", + format); + close(fd); + return; + } + { + char *text = (char *)mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); + if (text == MAP_FAILED) { + if (sw_dbg_keys()) + fprintf(stderr, "sw: cannot map the %u-byte keymap (%s) — using the built-in one\n", + size, strerror(errno)); + } else { + bool ok = sw_keymap_parse(&SW_KEYMAP, text, size); + if (sw_dbg_keys()) { + int keys = 0, i; + for (i = 0; i < SW_KEYMAP_MAX; ++i) if (SW_KEYMAP.sym[i][0]) keys++; + fprintf(stderr, "sw: keymap %u bytes, parsed %s, %d keys\n", + size, ok ? "ok" : "FAILED", keys); + } + munmap(text, size); + } } close(fd); } @@ -801,6 +833,8 @@ static void sw_kbd_enter(void *d, struct wl_keyboard *k, uint32_t serial, (void)d; (void)k; (void)serial; (void)keys; SW_KBD_WIN = sw_win_of(s); SwEvent e = { .type = SW_EV_KB_ENTER }; + if (sw_dbg_keys()) + fprintf(stderr, "sw: keyboard enter%s\n", SW_KBD_WIN ? "" : " (not our surface)"); sw_push(&e); } @@ -810,6 +844,7 @@ static void sw_kbd_leave(void *d, struct wl_keyboard *k, uint32_t serial, (void)d; (void)k; (void)serial; SwWin *w = sw_win_of(s); SwEvent e = { .type = SW_EV_KB_LEAVE }; + if (sw_dbg_keys()) fprintf(stderr, "sw: keyboard leave\n"); sw_push(&e); if (w && w->app.keyboard_leave) w->app.keyboard_leave(w->app.user); if (SW_KBD_WIN == w) SW_KBD_WIN = NULL; @@ -825,6 +860,9 @@ static void sw_kbd_key(void *d, struct wl_keyboard *k, uint32_t serial, SwEvent e = { .type = SW_EV_KEY, .keysym = sym, .code = key, .mods = SW_MODS, .pressed = state != 0 }; memcpy(e.text, utf8, sizeof(e.text)); + if (sw_dbg_keys()) + fprintf(stderr, "sw: key %s code %u sym 0x%04x mods 0x%02x text '%s'\n", + state ? "down" : "up ", key, sym, SW_MODS, utf8); sw_push(&e); if (!w || !w->app.key) return; w->app.key(w->app.user, sym, utf8, SW_MODS, state != 0); diff --git a/swov.c b/swov.c index c2d838a..9c42481 100644 --- a/swov.c +++ b/swov.c @@ -121,7 +121,7 @@ static uint64_t now_ms_(void) } #define APP_ID "swov" -#define SWOV_VERSION "1.3.1" +#define SWOV_VERSION "1.3.2" #ifndef SWOV_BUILD /* set by the Makefile: md5 of this file */ #define SWOV_BUILD "unknown" #endif