#!/bin/sh # X11 preedit with a CJK input method on the headless display: fcitx5 with # Pinyin (fcitx5-chinese-addons) as the XIM server, the composer as its # client (XMODIFIERS=@im=fcitx), "nihao" typed: the preedit must show in # the input (marked text), Space commits 你好, Ctrl+Enter sends it. fcitx5's # configuration and state stay in the headless session's XDG directories. # # spikes/gpui/shell.sh spikes/gpui/headless.sh -- spikes/gpui/composer/ime.sh # # FCITX5: an fcitx5 with the Pinyin addon (default: built from nixpkgs). set -eu root=$(cd "$(dirname "$0")/../../.." && pwd) out=${OUT:-$root/.scratch/gpui-spikes-results} mkdir -p "$out" out=$(cd "$out" && pwd) if [ -z "${FCITX5:-}" ]; then FCITX5=$(nix build --option substituters "https://cache.nixos.org/ https://nix-community.cachix.org" \ --no-link --print-out-paths --impure --expr \ 'let p = (builtins.getFlake "nixpkgs").legacyPackages.x86_64-linux; in p.qt6Packages.fcitx5-with-addons.override { addons = [ p.qt6Packages.fcitx5-chinese-addons ]; }') fi mkdir -p "$XDG_CONFIG_HOME/fcitx5" cat >"$XDG_CONFIG_HOME/fcitx5/profile" <<'PROFILE' [Groups/0] Name=Default Default Layout=us DefaultIM=pinyin [Groups/0/Items/0] Name=keyboard-us Layout= [Groups/0/Items/1] Name=pinyin Layout= [GroupOrder] 0=Default PROFILE cat >"$XDG_CONFIG_HOME/fcitx5/config" <<'CONFIG' [Behavior] ActiveByDefault=True ShareInputState=All CONFIG # On-the-spot: the preedit goes to the client (GPUI asks for # XIMPreeditCallbacks); fcitx5 draws it in its own window otherwise. mkdir -p "$XDG_CONFIG_HOME/fcitx5/conf" printf 'UseOnTheSpot=True\n' >"$XDG_CONFIG_HOME/fcitx5/conf/xim.conf" "$FCITX5/bin/fcitx5" --replace --disable=wayland,waylandim,notificationitem,kimpanel >"$out/fcitx5.log" 2>&1 & fcitx=$! i=0 until "$FCITX5/bin/fcitx5-remote" >/dev/null 2>&1; do i=$((i + 1)) [ $i -lt 100 ] || { echo "fcitx5 did not start" >&2; break; } sleep 0.1 done sleep 1 events=$out/ime-events.jsonl XMODIFIERS=@im=fcitx "$root/target/release/spike-composer" --seconds 60 >"$events" 2>"$out/ime.log" & pid=$! i=0 until win=$(xdotool search --name 'Composer \(spike\)' 2>/dev/null | head -1) && [ -n "$win" ]; do i=$((i + 1)) [ $i -lt 150 ] || { echo "no window" >&2; exit 1; } sleep 0.1 done xdotool windowactivate --sync "$win" sleep 2 "$FCITX5/bin/fcitx5-remote" -o || true echo "fcitx5 state: $("$FCITX5/bin/fcitx5-remote" 2>&1) (2: active)" >&2 sleep 0.5 xdotool type --delay 150 'nihao' sleep 1.5 xwd -root -silent | magick xwd:- -resize 50% "png:$out/ime-preedit.png" xwd -id "$win" -silent | magick xwd:- "png:$out/ime-preedit-window.png" xdotool key space sleep 1 xwd -id "$win" -silent | magick xwd:- "png:$out/ime-committed.png" xdotool key ctrl+Return sleep 0.8 kill "$pid" 2>/dev/null || true wait "$pid" 2>/dev/null || true kill "$fcitx" 2>/dev/null || true fail=0 check() { if jq -e -s "$2" "$events" >/dev/null; then echo "PASS $1" else echo "FAIL $1" fail=1 fi } ev='[.[] | select(.event)]' check "the input shows the preedit as marked text" "$ev | any(.event == \"preedit\" and .marked != null and (.marked | length) > 0)" check "the commit puts Chinese text in the input" "$ev | any((.value // \"\") | explode | any(. >= 19968 and . <= 40959))" check "Ctrl+Enter sends it" "$ev | any(.event == \"sent\" and (.text | explode | any(. >= 19968 and . <= 40959)))" exit $fail