Skip to content

Commit 5e06095

Browse files
stoa-predict: popup at the text cursor with arrow-key navigation (Wayland input method) (#208)
## What Text prediction becomes a **Wayland input method** (`input-method-v2`, the protocol fcitx uses). This answers three requests: | Request | Before | Now | |---|---|---| | Popup above/below where you type | fixed strip at the bottom of the screen | compositor places it at the text cursor | | ↓/↑ into the popup, ←/→ to move | not possible | ↓/↑ enter (and leave), ←/→ move, Enter/Tab accept, Esc dismiss; click still works | | Emojis | never inserted | inserted | Why an input method: a shell panel can't see another app's caret, and it can't take arrow keys without stealing focus. An input method gets both from the compositor. ### How it works (`scripts/stoa-predict.py`) - **Popup:** an input-method popup surface, drawn with cairo + Pango into shm buffers using the Stoa palette (`StoaCustom.json` if present). It follows the output scale and shows a `↓` hint until the popup is entered. - **Keys:** a keyboard grab sees every key. Keys the popup doesn't use are forwarded through the input method's virtual keyboard. niri and sway route those past the grab: `src/handlers/virtual_keyboard.rs` says so explicitly, and fcitx relies on the same path. - **Prefix:** comes from text-input `surrounding_text`. In terminals, which send none, it comes from the keys themselves via xkbcommon (ctypes, no extra package), so abnt2 and ç work. - **Inserting text:** `commit_string`, preceded by `delete_surrounding_text`. In terminals it is preceded by BackSpaces, using the keycode looked up in the current keymap. Case follows what was typed (`Stoi` → `Stoical`). - **Bindings:** generated once into `~/.cache` from XMLs shipped in `scripts/stoa-predict-protocols/` (MIT, from wlroots). No Arch package installs these two protocols. ### Why the emojis never went in Two causes in the old `wtype` path, both reproduced and measured: - without a UTF-8 locale, `wtype` refuses any non-ASCII input (`Failed to deencode input argv`); - typing a single character right after `wtype` creates its keyboard dropped it 9 times out of 15. `commit_string` avoids both. ### Plumbing - **Noctalia plugin:** keeps only its service (start/stop/notify). `panel.luau` is gone, and `plugin_api` drops back to 9. - **`stoa-predict.sh`:** `toggle | start | stop | run | kill`. `kill` waits for the old instance to disconnect, because the compositor refuses a second input method while the first is still connected. - **Packages:** `post-install` swaps `python-evdev` for `python-pywayland python-cairo python-gobject`. The `input` group stays, relabelled: `libinput-gestures` needs it, prediction no longer does. - **Brave (binds + `.desktop`) and Obsidian (`~/.config/obsidian/user-flags.conf`):** get `--enable-wayland-ime --wayland-text-input-version=3`, so Chromium talks to the input method. ## Bugs caught while testing - **SIGTERM ignored when idle:** a Python SIGTERM handler never runs while libwayland sits in `poll()` (it restarts on EINTR). The daemon survived toggle-off, and the next start was refused. Now SIGTERM keeps its default action; the compositor cleans up on disconnect. - **cffi swallows `sys.exit()`:** the "another input method is running" path kept running. Fatal exits now use `os._exit`. - **Pidfile overwrite:** a refused second instance used to overwrite and then delete the pidfile of the instance holding the input method. It is now written only after registration and removed only by its owner. ## Verification Headless **sway 1.10** (built here, since sway 1.9 doesn't draw IME popups), with a GTK4 text view, foot, and a test virtual keyboard carrying a full br/abnt2 keymap: - **Popup placement:** the popup appears right under the caret; screenshots show the passive state and ↓ → selection. - **GTK words:** `the Stoi` → ↓ → Enter gives `the Stoical `. - **GTK emoji:** `Fir` → ↓ ← Enter gives `Fir🔥`. - **Esc:** dismisses the popup, and typing continues. - **Terminal (foot):** `go stoi` → ↓ Enter gives 4×DEL + `stoic `. - **Whole chain:** Noctalia 5.3.0 + plugin → `stoa-predict toggle` → service → daemon, typing into GTK and foot. Toggling off leaves no daemon, and text then goes in plain. - **Controller:** run → run again (takes over) → kill, with no refusal and the pidfile removed. - **Repo checks:** `tests/run.sh` 18 passed; `ruff`, `luac5.4`, shellcheck and `noctalia plugins lint` are clean. **Not tested here:** niri and Hyprland themselves, which don't run headless. niri's source supports the IME popup (`PopupKind::InputMethod`) and the grab passthrough. A real Brave/Obsidian has not been tried either. --- _Generated by [Claude Code](https://claude.ai/code/session_01CrdbH4MCHhLKimzdRSzXZp)_
2 parents badb2dd + 4f021ea commit 5e06095

12 files changed

Lines changed: 1347 additions & 510 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ A family of small, single-purpose `stoa-*` tools — each one exists because rea
168168
<tr><td><code>stoa-fetch</code></td><td>System fetch</td><td><code>stoa-resize</code></td><td>Batch resize images</td></tr>
169169
<tr><td><code>stoa-walls</code></td><td>Wallpaper generator</td><td><code>stoa-rename</code></td><td>Regex rename + preview</td></tr>
170170
<tr><td><code>stoa-memento-data</code></td><td>Memento Mori data (desktop widget)</td><td><code>stoa-locksmith</code></td><td>Find + end what's locking a file/folder</td></tr>
171-
<tr><td><code>stoa-doctor</code></td><td>System health check</td><td><code>stoa-predict</code></td><td>Text prediction + emoji suggestions (Noctalia panel)</td></tr>
171+
<tr><td><code>stoa-doctor</code></td><td>System health check</td><td><code>stoa-predict</code></td><td>Word + emoji suggestions at the text cursor (input method)</td></tr>
172172
<tr><td><code>stoa-screensaver</code></td><td>Living marble screensaver</td><td><code>stoa-archive</code></td><td>Compress selection / extract archives</td></tr>
173173
<tr><td><code>stoa-clipboard</code></td><td>Clipboard + pins</td><td></td><td></td></tr>
174174
<tr><td><code>stoa-drive</code></td><td>Cloud drive (rclone)</td><td></td><td></td></tr>

‎config/hypr/hyprland.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ hl.bind("ALT + SHIFT + Tab", hl.dsp.exec_cmd(
153153
-- ── Apps ──
154154
hl.bind(mod .. " + A", hl.dsp.exec_cmd("~/.local/bin/stoa-store"))
155155
hl.bind(mod .. " + B", hl.dsp.exec_cmd(
156-
"brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland"))
156+
"brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland --enable-wayland-ime --wayland-text-input-version=3"))
157157
hl.bind(mod .. " + C", hl.dsp.exec_cmd("qalculate-gtk"))
158158
hl.bind(mod .. " + SHIFT + E", hl.dsp.exec_cmd("kitty -e lf"))
159159
hl.bind(mod .. " + E", hl.dsp.exec_cmd("thunar"))

‎config/niri/config.kdl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ binds {
204204

205205
// ── Apps ──
206206
Mod+A { spawn-sh "~/.local/bin/stoa-store"; }
207-
Mod+B { spawn-sh "brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland"; }
207+
Mod+B { spawn-sh "brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland --enable-wayland-ime --wayland-text-input-version=3"; }
208208
Mod+C { spawn "qalculate-gtk"; }
209209
Mod+Shift+E { spawn "kitty" "-e" "lf"; }
210210
Mod+E { spawn "thunar"; }

‎install.sh‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ Version=1.0
629629
Name=Brave Web Browser
630630
GenericName=Web Browser
631631
Comment=Access the Internet
632-
Exec=brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland %U
632+
Exec=brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland --enable-wayland-ime --wayland-text-input-version=3 %U
633633
StartupNotify=true
634634
Terminal=false
635635
Icon=brave-desktop
@@ -640,13 +640,23 @@ Actions=new-window;new-private-window;
640640
641641
[Desktop Action new-window]
642642
Name=New Window
643-
Exec=brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland
643+
Exec=brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland --enable-wayland-ime --wayland-text-input-version=3
644644
645645
[Desktop Action new-private-window]
646646
Name=New Incognito Window
647-
Exec=brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland --incognito
647+
Exec=brave --password-store=basic --enable-features=UseOzonePlatform --ozone-platform=wayland --enable-wayland-ime --wayland-text-input-version=3 --incognito
648648
BRAVE
649649
echo -e " ${O}[+] brave-browser.desktop (--password-store=basic)${R}"
650+
# --enable-wayland-ime --wayland-text-input-version=3 (above, and in the
651+
# Super+B binds): Chromium only talks to a Wayland input method through
652+
# text-input-v3 with these, and stoa-predict is one. Obsidian is Electron;
653+
# Arch's launcher appends ~/.config/obsidian/user-flags.conf to its argv.
654+
OBSIDIAN_FLAGS="${CONFIG_DIR}/obsidian/user-flags.conf"
655+
if ! grep -qs -- "--enable-wayland-ime" "$OBSIDIAN_FLAGS"; then
656+
mkdir -p "$(dirname "$OBSIDIAN_FLAGS")"
657+
printf '%s\n' "--enable-wayland-ime" "--wayland-text-input-version=3" >> "$OBSIDIAN_FLAGS"
658+
echo -e " ${O}[+] obsidian user-flags.conf (Wayland input method)${R}"
659+
fi
650660

651661
echo ""
652662
echo -e "${F}Shell configuration:${R}"

0 commit comments

Comments
 (0)