diff --git a/README.md b/README.md index f46f7b9..8215b26 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,14 @@ paru -S google-java-format java-language-server-git ``` sudo systemctl start reflector ``` + +## GNOME keybindings + +Shortcuts registered with GNOME live in dconf rather than in a file, so `stow` +cannot link them. Run this once per machine to register Handy's dictation +shortcuts (Ctrl+Super+Space to toggle, +Ctrl+Super+Backspace to cancel): + +``` +handy-sync-keybindings +``` diff --git a/handy/.local/bin/handy-sync-keybindings b/handy/.local/bin/handy-sync-keybindings new file mode 100755 index 0000000..05b5b7d --- /dev/null +++ b/handy/.local/bin/handy-sync-keybindings @@ -0,0 +1,83 @@ +#!/usr/bin/env bash +# +# Registers Handy's dictation shortcuts as GNOME custom keybindings. +# +# Under Wayland an application cannot grab a global hotkey, so Handy's own +# shortcut setting never fires. GNOME holds the keys instead and runs Handy's +# CLI, which signals the already-running instance. +# +# Entries are matched by name, so re-running updates them in place. Slots are +# allocated around everything already present in dconf, including entries other +# applications have orphaned by dropping them from the list but leaving their +# keys behind. + +set -u + +schema=org.gnome.settings-daemon.plugins.media-keys +entry_schema=$schema.custom-keybinding +dconf_dir=/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings + +# name|command|binding +shortcuts=( + 'Handy: toggle dictation|/usr/bin/handy --toggle-transcription|space' + 'Handy: cancel dictation|/usr/bin/handy --cancel|BackSpace' +) + +if ! command -v gsettings >/dev/null; then + printf >&2 '%s\n' 'gsettings not found; these shortcuts require GNOME.' + exit 1 +fi + +# Strips the single quotes GVariant wraps around string values. +unquote() { + local value=$1 + value=${value#\'} + value=${value%\'} + printf '%s' "$value" +} + +readarray -t paths < <(gsettings get "$schema" custom-keybindings | grep -o "'[^']*'" | tr -d "'") +occupied=$(dconf list "$dconf_dir/" 2>/dev/null) + +next_free_slot() { + local index=0 candidate + while true; do + candidate="$dconf_dir/custom$index/" + if [[ " ${paths[*]} " != *" $candidate "* ]] && ! grep -qx "custom$index/" <<<"$occupied"; then + printf '%s' "$candidate" + return + fi + ((index++)) + done +} + +listed=false + +for shortcut in "${shortcuts[@]}"; do + IFS='|' read -r name command binding <<<"$shortcut" + + path= + for candidate in "${paths[@]}"; do + if [[ $(unquote "$(gsettings get "$entry_schema:$candidate" name)") == "$name" ]]; then + path=$candidate + break + fi + done + + if [[ -z $path ]]; then + path=$(next_free_slot) + paths+=("$path") + listed=true + fi + + gsettings set "$entry_schema:$path" name "$name" + gsettings set "$entry_schema:$path" command "$command" + gsettings set "$entry_schema:$path" binding "$binding" + + printf '%s -> %s (%s)\n' "$binding" "$command" "${path%/}" +done + +if "$listed"; then + list=$(printf "'%s'," "${paths[@]}") + gsettings set "$schema" custom-keybindings "[${list%,}]" +fi