diff --git a/bin/omarchy-clipboard-paste-file b/bin/omarchy-clipboard-paste-file index 431d6c2b57c0..3290e166580d 100755 --- a/bin/omarchy-clipboard-paste-file +++ b/bin/omarchy-clipboard-paste-file @@ -30,4 +30,4 @@ if [[ $copy_only == "true" ]]; then fi sleep 0.15 -wtype -M shift -k Insert -m shift 2>/dev/null || true +omarchy-hyprland-send-shortcut SHIFT Insert 2>/dev/null || true diff --git a/bin/omarchy-clipboard-paste-text b/bin/omarchy-clipboard-paste-text index b8ac8e3127a7..c7610eed8021 100755 --- a/bin/omarchy-clipboard-paste-text +++ b/bin/omarchy-clipboard-paste-text @@ -57,7 +57,7 @@ fi sleep 0.15 if [[ $use_shift_insert == "true" ]]; then - wtype -M shift -k Insert -m shift 2>/dev/null || true + omarchy-hyprland-send-shortcut SHIFT Insert 2>/dev/null || true else wtype "$text" 2>/dev/null || true fi diff --git a/bin/omarchy-hyprland-send-shortcut b/bin/omarchy-hyprland-send-shortcut new file mode 100755 index 000000000000..d96e818bbbfc --- /dev/null +++ b/bin/omarchy-hyprland-send-shortcut @@ -0,0 +1,29 @@ +#!/bin/bash + +# omarchy:summary=Send a keyboard shortcut to the focused surface through Hyprland +# omarchy:args= +# omarchy:examples=omarchy-hyprland-send-shortcut SHIFT Insert +# omarchy:hidden=true + +# Hyprland presses the keys on the seat's own keymap, so XWayland apps receive +# them as sent. wtype types through a keymap it uploads for each run, which +# XWayland clients never load: they read its keys as Escape, digits and +# letters, so wtype's Shift+Insert arrives as Shift+Escape. Omitting the window +# target also reaches focused layer-shell surfaces, and the down/up split works +# around Hyprland leaving synthetic keys stuck, as default/hypr/bindings/clipboard.lua does. + +mods=${1:-} +key=${2:-} + +if [[ ! $mods =~ ^[A-Z_\ ]*$ || ! $key =~ ^[A-Za-z0-9_]+$ ]]; then + echo "Usage: omarchy-hyprland-send-shortcut " >&2 + exit 1 +fi + +send_key_state() { + hyprctl dispatch "hl.dsp.send_key_state({ mods = \"$mods\", key = \"$key\", state = \"$1\" })" >/dev/null +} + +send_key_state down +sleep 0.05 +send_key_state up diff --git a/bin/omarchy-menu-emoji-insert b/bin/omarchy-menu-emoji-insert index 583702e09534..eebc5585db0f 100755 --- a/bin/omarchy-menu-emoji-insert +++ b/bin/omarchy-menu-emoji-insert @@ -14,7 +14,7 @@ printf '%s' "$emoji" | wl-copy --type text/plain --sensitive --foreground & copy_pid=$! sleep 0.15 -wtype -M shift -k Insert -m shift 2>/dev/null || true +omarchy-hyprland-send-shortcut SHIFT Insert 2>/dev/null || true sleep 0.2 kill "$copy_pid" 2>/dev/null || true diff --git a/bin/omarchy-voxtype-output b/bin/omarchy-voxtype-output new file mode 100755 index 000000000000..1c4302d21080 --- /dev/null +++ b/bin/omarchy-voxtype-output @@ -0,0 +1,57 @@ +#!/bin/bash + +# omarchy:summary=Paste Voxtype dictation through Hyprland +# omarchy:args= +# omarchy:hidden=true + +# Voxtype runs `begin` as its pre_output_command and `end` as its +# post_output_command. In clipboard mode it only copies the transcript, and +# `end` pastes it with Shift+Insert from Hyprland, which XWayland apps receive +# intact where wtype's typing arrives as Escape and digits. Voxtype also runs +# post_output_command after cancelled and empty recordings, so `end` pastes +# only after a `begin`. Type and paste modes deliver the text themselves, so +# `begin` arms nothing there. + +runtime_dir="${XDG_RUNTIME_DIR:-/tmp}" +marker="$runtime_dir/omarchy-voxtype-output" + +daemon_identity() { + local pid process_stat + local stat_fields=() + + # Voxtype writes this before running hooks, including for manual launches. + # Hook shell PIDs change on each call; the daemon's PID and start time do not. + [[ -r $runtime_dir/voxtype/pid ]] || return 1 + pid=$(<"$runtime_dir/voxtype/pid") || return 1 + [[ $pid =~ ^[1-9][0-9]*$ && -r /proc/$pid/stat ]] || return 1 + process_stat=$( "$marker" + else + rm -f "$marker" + fi + ;; + end) + if [[ -f $marker ]]; then + pending=$(<"$marker") + rm -f "$marker" + # A marker left by a dead daemon must not paste on the next cancellation. + if identity=$(daemon_identity) && [[ $pending == "$identity" ]]; then + # Voxtype's own paste mode gives the clipboard as long to settle. + sleep 0.1 + omarchy-hyprland-send-shortcut SHIFT Insert + fi + fi + ;; + *) + echo "Usage: omarchy-voxtype-output " >&2 + exit 1 + ;; +esac diff --git a/default/voxtype/config.toml b/default/voxtype/config.toml index 9ae56db753eb..31b9a943743e 100644 --- a/default/voxtype/config.toml +++ b/default/voxtype/config.toml @@ -58,10 +58,19 @@ translate = false # threads = 4 [output] -# Primary output mode: "type" or "clipboard" -# - type: Simulates keyboard input at cursor position (requires ydotool) -# - clipboard: Copies text to clipboard (requires wl-copy) -mode = "type" +# Primary output mode: "clipboard", "type" or "paste" +# - clipboard: Copies text to the clipboard (requires wl-copy). The hooks below +# then paste it with Shift+Insert sent by Hyprland. +# - type: Simulates keyboard input at the cursor through wtype. XWayland apps, +# such as the ChatGPT and Hermes desktop apps, receive wtype's keys as Escape +# and digits instead of the transcript. +# - paste: Copies text and sends paste_keys through wtype, which XWayland apps +# receive as Escape. +mode = "clipboard" + +# Paste the transcript after clipboard mode copies it; see omarchy-voxtype-output +pre_output_command = "omarchy-voxtype-output begin" +post_output_command = "omarchy-voxtype-output end" # Fall back to clipboard if typing fails fallback_to_clipboard = true diff --git a/test/shell.d/clipboard-test.sh b/test/shell.d/clipboard-test.sh index b89f488eccef..4da6b4747d4d 100644 --- a/test/shell.d/clipboard-test.sh +++ b/test/shell.d/clipboard-test.sh @@ -280,9 +280,9 @@ elif [[ $1 == "--type" && $2 == "text" ]]; then fi SH -cat >"$TMPDIR/bin/wtype" <<'SH' +cat >"$TMPDIR/bin/hyprctl" <<'SH' #!/bin/bash -printf '%s\n' "$*" >"$WTYPE_OUT" +printf '%s\n' "$*" >>"$HYPRCTL_OUT" SH cat >"$TMPDIR/bin/omarchy-launch-browser" <<'SH' @@ -301,7 +301,7 @@ cat >"$TMPDIR/bin/omasnap" <<'SH' printf '%s\n' "$*" >"$OMASNAP_OUT" SH -chmod +x "$TMPDIR/bin/wl-copy" "$TMPDIR/bin/wl-paste" "$TMPDIR/bin/wtype" "$TMPDIR/bin/omarchy-launch-browser" "$TMPDIR/bin/omarchy-launch-editor" "$TMPDIR/bin/omasnap" +chmod +x "$TMPDIR/bin/wl-copy" "$TMPDIR/bin/wl-paste" "$TMPDIR/bin/hyprctl" "$TMPDIR/bin/omarchy-launch-browser" "$TMPDIR/bin/omarchy-launch-editor" "$TMPDIR/bin/omasnap" capture_output=$(XDG_RUNTIME_DIR="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" PATH="$TMPDIR/bin:$PATH" "$ROOT/shell/plugins/clipboard/capture.sh") [[ $capture_output == '{"type":"text","text":"terminal copy"}' ]] || fail "clipboard capture records normal text events" @@ -530,36 +530,38 @@ kill "$owner_pid" 2>/dev/null || true process_gone "$watch_pid" || fail "clipboard watcher dies with its owner via pdeathsig" pass "clipboard watcher dies with its owner via pdeathsig" +shift_insert_through_hyprland=$'dispatch hl.dsp.send_key_state({ mods = "SHIFT", key = "Insert", state = "down" })\ndispatch hl.dsp.send_key_state({ mods = "SHIFT", key = "Insert", state = "up" })' + jq -n --arg text "$(printf 'large block line 1\nlarge block line 2\n')" '[{type:"text", text:"ignored"}, {type:"text", text:$text}]' >"$TMPDIR/home/.local/state/omarchy/clipboard-history.json" -WL_COPY_OUT="$TMPDIR/copied" WTYPE_OUT="$TMPDIR/wtype" HOME="$TMPDIR/home" PATH="$TMPDIR/bin:$PATH" \ +WL_COPY_OUT="$TMPDIR/copied" HYPRCTL_OUT="$TMPDIR/hyprctl" HOME="$TMPDIR/home" PATH="$TMPDIR/bin:$ROOT/bin:$PATH" \ "$ROOT/bin/omarchy-clipboard-paste-text" --shift-insert --history-index 1 [[ $(<"$TMPDIR/copied") == "$(printf 'large block line 1\nlarge block line 2')" ]] || fail "clipboard paste helper copies history entry text" pass "clipboard paste helper copies history entry text" -[[ $(<"$TMPDIR/wtype") == "-M shift -k Insert -m shift" ]] || fail "clipboard paste helper pastes history entries with shift insert" +[[ $(<"$TMPDIR/hyprctl") == "$shift_insert_through_hyprland" ]] || fail "clipboard paste helper pastes history entries with shift insert" pass "clipboard paste helper pastes history entries with shift insert" -rm -f "$TMPDIR/wtype" -WL_COPY_OUT="$TMPDIR/copied" WTYPE_OUT="$TMPDIR/wtype" HOME="$TMPDIR/home" PATH="$TMPDIR/bin:$PATH" \ +rm -f "$TMPDIR/hyprctl" +WL_COPY_OUT="$TMPDIR/copied" HYPRCTL_OUT="$TMPDIR/hyprctl" HOME="$TMPDIR/home" PATH="$TMPDIR/bin:$ROOT/bin:$PATH" \ "$ROOT/bin/omarchy-clipboard-paste-text" --copy-only --history-index 1 [[ $(<"$TMPDIR/copied") == "$(printf 'large block line 1\nlarge block line 2')" ]] || fail "clipboard paste helper copy-only copies history entry text" pass "clipboard paste helper copy-only copies history entry text" -[[ ! -e "$TMPDIR/wtype" ]] || fail "clipboard paste helper copy-only skips typing" +[[ ! -e "$TMPDIR/hyprctl" ]] || fail "clipboard paste helper copy-only skips typing" pass "clipboard paste helper copy-only skips typing" printf 'image-data' >"$TMPDIR/image.png" -rm -f "$TMPDIR/wtype" -WL_COPY_OUT="$TMPDIR/copied" WTYPE_OUT="$TMPDIR/wtype" PATH="$TMPDIR/bin:$PATH" \ +rm -f "$TMPDIR/hyprctl" +WL_COPY_OUT="$TMPDIR/copied" HYPRCTL_OUT="$TMPDIR/hyprctl" PATH="$TMPDIR/bin:$ROOT/bin:$PATH" \ "$ROOT/bin/omarchy-clipboard-paste-file" --copy-only image/png "$TMPDIR/image.png" [[ $(<"$TMPDIR/copied") == "image-data" ]] || fail "clipboard file paste helper copy-only copies file content" pass "clipboard file paste helper copy-only copies file content" -[[ ! -e "$TMPDIR/wtype" ]] || fail "clipboard file paste helper copy-only skips paste keystroke" +[[ ! -e "$TMPDIR/hyprctl" ]] || fail "clipboard file paste helper copy-only skips paste keystroke" pass "clipboard file paste helper copy-only skips paste keystroke" jq -n --arg url 'https://example.com/docs' --arg text "$(printf 'plain text\nsecond line')" --arg image "$TMPDIR/image.png" \ diff --git a/test/shell.d/dictation-test.sh b/test/shell.d/dictation-test.sh index 5f4835aceb6e..2f87d98cb4a6 100644 --- a/test/shell.d/dictation-test.sh +++ b/test/shell.d/dictation-test.sh @@ -345,3 +345,57 @@ printf '%s\n' voxtype > "$config" omarchy-remove-dictation-superwhisper [[ $(cat "$config") == "voxtype" ]] || fail "removing Superwhisper preserves another selected backend" pass "Superwhisper removal clears its selection and owned links while preserving user data and other backends" + +cat > "$test_tmp/bin/voxtype" <<'SH' +#!/bin/bash +[[ $* == "config get output.mode" ]] && echo "$VOXTYPE_MODE" +SH +chmod +x "$test_tmp/bin/voxtype" +export XDG_RUNTIME_DIR="$test_tmp/run" +mkdir -p "$XDG_RUNTIME_DIR/voxtype" +printf '%s\n' "$$" > "$XDG_RUNTIME_DIR/voxtype/pid" +: > "$DICTATION_LOG" +VOXTYPE_MODE=clipboard omarchy-voxtype-output end +[[ ! -s $DICTATION_LOG ]] || fail "a cancelled recording must not paste the clipboard" +for mode in type paste; do + VOXTYPE_MODE=$mode omarchy-voxtype-output begin + omarchy-voxtype-output end + [[ ! -s $DICTATION_LOG ]] || fail "$mode mode must not paste over its own output" +done +VOXTYPE_MODE=clipboard omarchy-voxtype-output begin +omarchy-voxtype-output end +omarchy-voxtype-output end +expected=$'hyprctl dispatch hl.dsp.send_key_state({ mods = "SHIFT", key = "Insert", state = "down" })\nhyprctl dispatch hl.dsp.send_key_state({ mods = "SHIFT", key = "Insert", state = "up" })' +[[ $(cat "$DICTATION_LOG") == "$expected" ]] || fail "clipboard mode pastes each transcript once through Hyprland" +pass "Voxtype output hooks paste clipboard transcripts once and nothing else" + +# Voxtype writes its PID before launching hooks through sh -c. Separate +# subshells model a daemon dying after begin and its replacement cancelling. +daemon_hook() { + printf '%s\n' "$BASHPID" > "$XDG_RUNTIME_DIR/voxtype/pid" + VOXTYPE_MODE=clipboard /bin/sh -c 'omarchy-voxtype-output "$1"' sh "$1" +} + +: > "$DICTATION_LOG" +(daemon_hook begin) +(daemon_hook end) +[[ ! -s $DICTATION_LOG ]] || fail "cancellation after a daemon restart must not paste stale text" +[[ ! -e $XDG_RUNTIME_DIR/omarchy-voxtype-output ]] || fail "a stale output marker must be removed" +pass "Voxtype cancellation after a daemon restart discards pending output" + +(daemon_hook begin; daemon_hook end) +[[ $(cat "$DICTATION_LOG") == "$expected" ]] || fail "a restarted daemon must still paste its own transcript" +pass "a restarted Voxtype daemon can paste a new transcript" + +: > "$DICTATION_LOG" +: > "$XDG_RUNTIME_DIR/omarchy-voxtype-output" +daemon_hook end +[[ ! -s $DICTATION_LOG ]] || fail "an old unqualified marker must not authorize a paste" +pass "Voxtype ignores output markers from before the lifetime guard" + +VOXTYPE_MODE=clipboard omarchy-voxtype-output begin +rm -f "$XDG_RUNTIME_DIR/voxtype/pid" +omarchy-voxtype-output end +[[ ! -s $DICTATION_LOG ]] || fail "missing daemon identity must not authorize a paste" +[[ ! -e $XDG_RUNTIME_DIR/omarchy-voxtype-output ]] || fail "unverifiable output must be discarded" +pass "Voxtype discards pending output when the daemon identity is unavailable" diff --git a/test/shell.d/emojis-test.sh b/test/shell.d/emojis-test.sh index 003fcc9a37a5..38503b7b1548 100644 --- a/test/shell.d/emojis-test.sh +++ b/test/shell.d/emojis-test.sh @@ -63,9 +63,9 @@ printf '%s\n' "$args" >"$target.args" cat >"$target" SH -cat >"$TMPDIR/bin/wtype" <<'SH' +cat >"$TMPDIR/bin/hyprctl" <<'SH' #!/bin/bash -printf '%s\n' "$*" >"$WTYPE_OUT" +printf '%s\n' "$*" >>"$HYPRCTL_OUT" SH cat >"$TMPDIR/bin/sleep" <<'SH' @@ -73,9 +73,9 @@ cat >"$TMPDIR/bin/sleep" <<'SH' exit 0 SH -chmod +x "$TMPDIR/bin/wl-copy" "$TMPDIR/bin/wtype" "$TMPDIR/bin/sleep" +chmod +x "$TMPDIR/bin/wl-copy" "$TMPDIR/bin/hyprctl" "$TMPDIR/bin/sleep" -WL_COPY_OUT="$TMPDIR/copy" WL_COPY_EMOJI_OUT="$TMPDIR/emoji" WTYPE_OUT="$TMPDIR/wtype" PATH="$TMPDIR/bin:$PATH" \ +WL_COPY_OUT="$TMPDIR/copy" WL_COPY_EMOJI_OUT="$TMPDIR/emoji" HYPRCTL_OUT="$TMPDIR/hyprctl" PATH="$TMPDIR/bin:$ROOT/bin:$PATH" \ "$ROOT/bin/omarchy-menu-emoji-insert" "😀" [[ $(<"$TMPDIR/emoji") == "😀" ]] || fail "emoji insert helper copies emoji transiently" @@ -84,5 +84,5 @@ pass "emoji insert helper copies emoji transiently" [[ $(<"$TMPDIR/emoji.args") == "--type text/plain --sensitive --foreground" ]] || fail "emoji insert helper serves sensitive transient clipboard in foreground" pass "emoji insert helper serves transient clipboard in foreground" -[[ $(<"$TMPDIR/wtype") == "-M shift -k Insert -m shift" ]] || fail "emoji insert helper pastes with shift insert" +[[ $(<"$TMPDIR/hyprctl") == $'dispatch hl.dsp.send_key_state({ mods = "SHIFT", key = "Insert", state = "down" })\ndispatch hl.dsp.send_key_state({ mods = "SHIFT", key = "Insert", state = "up" })' ]] || fail "emoji insert helper pastes with shift insert" pass "emoji insert helper pastes with shift insert"