diff --git a/app.go b/app.go index fc629b1..7e9777e 100644 --- a/app.go +++ b/app.go @@ -408,6 +408,7 @@ func (a *App) GetSettings() map[string]interface{} { conf["linux_press_command"] = exePath + " --press" } conf["linux_press_mode"] = true + conf["linux_ydotool_status"] = linuxYdotoolStatus() } return conf } diff --git a/frontend/dist/assets/index.27c7f0d4.js b/frontend/dist/assets/index.27c7f0d4.js new file mode 100644 index 0000000..e723dd6 --- /dev/null +++ b/frontend/dist/assets/index.27c7f0d4.js @@ -0,0 +1,4 @@ +(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const i of document.querySelectorAll('link[rel="modulepreload"]'))n(i);new MutationObserver(i=>{for(const l of i)if(l.type==="childList")for(const r of l.addedNodes)r.tagName==="LINK"&&r.rel==="modulepreload"&&n(r)}).observe(document,{childList:!0,subtree:!0});function o(i){const l={};return i.integrity&&(l.integrity=i.integrity),i.referrerpolicy&&(l.referrerPolicy=i.referrerpolicy),i.crossorigin==="use-credentials"?l.credentials="include":i.crossorigin==="anonymous"?l.credentials="omit":l.credentials="same-origin",l}function n(i){if(i.ep)return;i.ep=!0;const l=o(i);fetch(i.href,l)}})();let u=!1;window.toggleApiKey=function(){const e=document.getElementById("apiKey"),t=document.getElementById("eyeBtn");u=!u,e.type=u?"text":"password",t.textContent=u?"\u{1F648}":"\u{1F441}\uFE0F"};window.recordShortcut=function(){const e=document.getElementById("recordBtn"),t=document.getElementById("shortcutInput"),o=a=>a.metaKey||a.key==="Meta"||a.key==="OS"||a.code==="MetaLeft"||a.code==="MetaRight";e.textContent="Recording...",e.disabled=!0,t.value="";let n="";const i=a=>{a.preventDefault(),a.stopPropagation();const c=[];a.ctrlKey&&c.push("ctrl"),a.altKey&&c.push("alt"),a.shiftKey&&c.push("shift"),o(a)&&c.push("win");let s=a.key.toLowerCase();s==="control"&&(s="ctrl"),s==="alt"&&(s="alt"),s==="shift"&&(s="shift"),(s==="meta"||s==="os")&&(s="win"),(a.code==="MetaLeft"||a.code==="MetaRight")&&(s="win"),s===" "&&(s="space");const f=new Set(c);f.add(s);const g=Array.from(f).filter(w=>w!==s);g.push(s),n=g.join("+"),t.value=n,["ctrl","alt","shift","win"].includes(s)||r()},l=a=>{a.preventDefault(),a.stopPropagation(),n&&(n.split("+").every(s=>["ctrl","alt","shift","win"].includes(s))&&n.includes("+")?setTimeout(r,100):!a.ctrlKey&&!a.altKey&&!a.shiftKey&&!o(a)&&r())},r=()=>{if(!n){e.textContent="Record",e.disabled=!1,y();return}window.go.main.App.SaveSettings({shortcut:n}).then(a=>{a&&a.indexOf("Invalid shortcut")!==-1&&alert(a),e.textContent="Record",e.disabled=!1,y()})},y=()=>{window.removeEventListener("keydown",i,!0),window.removeEventListener("keyup",l,!0)};window.addEventListener("keydown",i,!0),window.addEventListener("keyup",l,!0)};function m(e){const t=document.getElementById("historyList");if(!e||e.length===0){t.innerHTML='
GNOME: Settings -> Keyboard -> Custom Shortcuts -> Add a shortcut with the copied command.
KDE: System Settings -> Shortcuts -> Command/URL -> Add a shortcut with the copied command.
+ diff --git a/frontend/index.html b/frontend/index.html index f8ac21c..ed25ab4 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -54,6 +54,7 @@GNOME: Settings -> Keyboard -> Custom Shortcuts -> Add a shortcut with the copied command.
KDE: System Settings -> Shortcuts -> Command/URL -> Add a shortcut with the copied command.
+ @@ -318,6 +319,37 @@ }); } + function renderLinuxYdotoolStatus(status) { + const el = document.getElementById('linuxYdotoolStatus'); + if (!el || !status) return; + + const ready = !!status.ready; + el.style.display = 'block'; + el.style.background = ready ? 'var(--green-dim)' : 'var(--red-dim)'; + el.style.color = ready ? 'var(--green)' : 'var(--red)'; + el.style.border = ready ? '1px solid rgba(61, 186, 110, 0.25)' : '1px solid rgba(224, 82, 82, 0.25)'; + el.innerHTML = ''; + + const title = document.createElement('div'); + title.style.fontWeight = '600'; + title.textContent = ready ? 'Automatic paste ready' : 'Automatic paste needs ydotool setup'; + el.appendChild(title); + + const message = document.createElement('div'); + message.style.marginTop = '4px'; + message.textContent = status.message || ''; + el.appendChild(message); + + if (!ready && Array.isArray(status.setup_commands) && status.setup_commands.length) { + const pre = document.createElement('pre'); + pre.style.whiteSpace = 'pre-wrap'; + pre.style.margin = '8px 0 0'; + pre.style.color = 'inherit'; + pre.textContent = status.setup_commands.join('\n'); + el.appendChild(pre); + } + } + async function refreshHistoryFromBackend() { try { const settings = await window.go.main.App.GetSettings(); @@ -342,6 +374,7 @@ const cmdInput = document.getElementById('linuxPressCommand'); if (section) section.style.display = 'block'; if (cmdInput) cmdInput.value = settings.linux_press_command || ''; + renderLinuxYdotoolStatus(settings.linux_ydotool_status); } document.getElementById('whisperModel').value = settings.whisper_model || 'whisper-large-v3-turbo'; document.getElementById('aiModel').value = settings.ai_model || 'llama-3.3-70b-versatile'; diff --git a/scripts/setup-ydotool.sh b/scripts/setup-ydotool.sh new file mode 100644 index 0000000..f8cfab7 --- /dev/null +++ b/scripts/setup-ydotool.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +# Exit on error +set -e + +echo "==========================================================" +echo " ydotool Wayland Auto-Configuration Utility" +echo "==========================================================" +echo "" + +# Function to check if a command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# 1. Package Installation +echo "[1/4] Checking and installing ydotool..." +if ! command_exists ydotool; then + if command_exists dnf; then + echo "Fedora detected. Installing ydotool via dnf..." + sudo dnf install -y ydotool + elif command_exists apt-get; then + echo "Debian/Ubuntu detected. Installing ydotool via apt..." + sudo apt-get update && sudo apt-get install -y ydotool + elif command_exists pacman; then + echo "Arch Linux detected. Installing ydotool via pacman..." + sudo pacman -S --noconfirm ydotool + else + echo "ERROR: Unsupported package manager. Please install 'ydotool' manually, then run this script again." + exit 1 + fi +else + echo "ydotool is already installed." +fi + +# 2. Configure Non-Root /dev/uinput Access via udev +echo "[2/4] Configuring non-root udev rule for /dev/uinput..." +sudo tee /etc/udev/rules.d/80-uinput.rules << 'EOF' > /dev/null +KERNEL=="uinput", SUBSYSTEM=="misc", TAG+="uaccess", OPTIONS+="static_node=uinput" +EOF + +echo "Reloading udev rules..." +sudo udevadm control --reload-rules && sudo udevadm trigger + +# 3. Locate the Executable and Setup Systemd User Service +echo "[3/4] Designing systemd user-level service..." + +# Dynamically locate the binary path to support varying distributions (/usr/bin vs /usr/local/bin) +YDOTOOLD_PATH=$(command -v ydotoold || true) +if [ -z "$YDOTOOLD_PATH" ]; then + YDOTOOLD_PATH="/usr/bin/ydotoold" +fi + +mkdir -p "$HOME/.config/systemd/user" +tee "$HOME/.config/systemd/user/ydotool.service" << EOF > /dev/null +[Unit] +Description=ydotoold key emulation daemon for non-root user + +[Service] +Type=simple +ExecStart=$YDOTOOLD_PATH +Restart=always + +[Install] +WantedBy=default.target +EOF + +# 4. Activate User-Level Daemon +echo "[4/4] Starting ydotool daemon for user: $USER (UID: $(id -u))..." + +# Disable 'exit on error' temporarily in case systemd user-manager lacks session initialization +set +e +systemctl --user daemon-reload +systemctl --user enable ydotool.service +systemctl --user start ydotool.service +set -e + +# 5. Post-Configuration Diagnostics +echo "" +echo "==========================================================" +echo " Post-Installation Diagnostics" +echo "==========================================================" +echo "Giving the daemon a moment to spin up..." +sleep 2 + +# Check if daemon is active +if systemctl --user is-active --quiet ydotool.service; then + echo "[-] Daemon Status: Active (Running)" +else + echo "[!] Daemon Status: Warning! The daemon failed to start." + echo " Check logs using: journalctl --user -u ydotool.service" +fi + +# Check for socket existence +SOCKET_PATH="/run/user/$(id -u)/.ydotool_socket" +if [ -S "$SOCKET_PATH" ]; then + echo "[-] Socket Location: Found at $SOCKET_PATH" + echo "" + echo "SUCCESS: Configuration completed." + echo "You can now run your Go/Wails application, and paste operations" + echo "will route seamlessly through ydotool." +else + echo "[!] Socket Location: Missing at $SOCKET_PATH" + echo "" + echo "WARNING: The setup ran but the communications socket is absent." + echo "This is typically caused by /dev/uinput permissions not applying yet." + echo "Please log out of your desktop session and log back in, or run:" + echo " systemctl --user restart ydotool.service" +fi +echo "==========================================================" diff --git a/text_insert_linux.go b/text_insert_linux.go index 4cab0a8..7c507aa 100644 --- a/text_insert_linux.go +++ b/text_insert_linux.go @@ -6,7 +6,9 @@ import ( "context" "errors" "fmt" + "os" "os/exec" + "path/filepath" "strings" "time" "unicode/utf8" @@ -21,19 +23,22 @@ func (a *App) insertTranscription(text string) { logger.Error("Failed to copy transcription to clipboard: %v", err) } else { waitForLinuxClipboardText(a.ctx, text) - if typer, err := pasteLinuxClipboardWithoutXTest(); err == nil { - logger.Info("Pasted transcription on Linux using %s (%d chars)", typer, utf8.RuneCountInString(text)) + a.releaseLinuxInputFocus() + if err := pasteLinuxClipboardWithYdotool(); err == nil { + logger.Info("Pasted transcription on Linux using ydotool (%d chars)", utf8.RuneCountInString(text)) return } else { - logger.Error("Linux auto-paste unavailable without XTEST: %v", err) + logger.Error("Linux auto-paste unavailable via ydotool: %v", err) } } - if typer, err := typeLinuxTextWithoutXTest(text); err == nil { - logger.Info("Typed transcription on Linux using %s (%d chars)", typer, utf8.RuneCountInString(text)) + if !isASCII(text) { + logger.Info("Skipping ydotool direct typing fallback for non-ASCII transcript") + } else if err := typeLinuxTextWithYdotool(text); err == nil { + logger.Info("Typed transcription on Linux using ydotool (%d chars)", utf8.RuneCountInString(text)) return } else { - logger.Error("Linux auto-type unavailable without XTEST: %v", err) + logger.Error("Linux auto-type unavailable via ydotool: %v", err) } logger.Info("Copied transcription to clipboard; install ydotool with ydotoold/uinput access for automatic paste on GNOME Wayland") @@ -42,6 +47,24 @@ func (a *App) insertTranscription(text string) { } } +func (a *App) releaseLinuxInputFocus() { + if a.ctx == nil { + time.Sleep(150 * time.Millisecond) + return + } + wailsruntime.WindowHide(a.ctx) + time.Sleep(250 * time.Millisecond) +} + +func isASCII(text string) bool { + for _, r := range text { + if r > 127 { + return false + } + } + return true +} + func waitForLinuxClipboardText(ctx context.Context, text string) { deadline := time.Now().Add(700 * time.Millisecond) for time.Now().Before(deadline) { @@ -53,63 +76,102 @@ func waitForLinuxClipboardText(ctx context.Context, text string) { } } -func pasteLinuxClipboardWithoutXTest() (string, error) { +func pasteLinuxClipboardWithYdotool() error { time.Sleep(150 * time.Millisecond) - var errs []string - - if path, err := exec.LookPath("ydotool"); err == nil { - if err := runLinuxInputCommand(path, []string{"key", "-d", "20", "29:1", "47:1", "47:0", "29:0"}, "", 3*time.Second); err == nil { - return "ydotool", nil - } else { - errs = append(errs, "ydotool: "+err.Error()) - } + path, socketPath, err := getYdotoolCommand() + if err != nil { + return err } - if path, err := exec.LookPath("wtype"); err == nil { - if err := runLinuxInputCommand(path, []string{"-M", "ctrl", "v", "-m", "ctrl"}, "", 3*time.Second); err == nil { - return "wtype", nil - } else { - errs = append(errs, "wtype: "+err.Error()) - } - } - - if len(errs) == 0 { - return "", errors.New("install ydotool with ydotoold/uinput access for GNOME Wayland auto-paste") - } - return "", errors.New(strings.Join(errs, "; ")) + return runLinuxInputCommand(path, []string{"key", "-d", "20", "29:1", "47:1", "47:0", "29:0"}, "", 3*time.Second, socketPath) } -func typeLinuxTextWithoutXTest(text string) (string, error) { - var errs []string - - if path, err := exec.LookPath("ydotool"); err == nil { - if err := runLinuxInputCommand(path, []string{"type", "--file", "-"}, text, linuxTextTyperTimeout(text)); err == nil { - return "ydotool", nil - } else { - errs = append(errs, "ydotool: "+err.Error()) - } +func typeLinuxTextWithYdotool(text string) error { + path, socketPath, err := getYdotoolCommand() + if err != nil { + return err } - if path, err := exec.LookPath("wtype"); err == nil { - if err := runLinuxInputCommand(path, []string{"-"}, text, linuxTextTyperTimeout(text)); err == nil { - return "wtype", nil - } else { - errs = append(errs, "wtype: "+err.Error()) - } - } - - if len(errs) == 0 { - return "", errors.New("install ydotool with ydotoold/uinput access for GNOME Wayland auto-typing") - } - return "", errors.New(strings.Join(errs, "; ")) + return runLinuxInputCommand(path, []string{"type", "--file", "-"}, text, linuxTextTyperTimeout(text), socketPath) } -func runLinuxInputCommand(path string, args []string, stdin string, timeout time.Duration) error { +func getYdotoolCommand() (string, string, error) { + path, err := exec.LookPath("ydotool") + if err != nil { + return "", "", errors.New("ydotool not found; install ydotool and start the ydotool user service") + } + + socketPath, err := getYdotoolSocketPath() + if err != nil { + return "", "", err + } + + return path, socketPath, nil +} + +func linuxYdotoolStatus() map[string]interface{} { + status := map[string]interface{}{ + "ready": false, + "installed": false, + "socket": false, + "socket_path": "", + "message": "", + "setup_commands": []string{ + "sudo dnf install ydotool", + "echo 'KERNEL==\"uinput\", SUBSYSTEM==\"misc\", TAG+=\"uaccess\", OPTIONS+=\"static_node=uinput\"' | sudo tee /etc/udev/rules.d/80-uinput.rules", + "sudo udevadm control --reload-rules && sudo udevadm trigger", + "systemctl --user enable --now ydotool.service", + }, + } + + path, err := exec.LookPath("ydotool") + if err != nil { + status["message"] = "ydotool is not installed." + return status + } + status["installed"] = true + + socketPath, err := getYdotoolSocketPath() + if err != nil { + status["message"] = err.Error() + return status + } + status["socket"] = true + status["socket_path"] = socketPath + + if err := runLinuxInputCommand(path, []string{"key", "-d", "1", "0"}, "", 800*time.Millisecond, socketPath); err != nil { + status["message"] = "ydotool is installed, but the daemon test failed: " + err.Error() + return status + } + + status["ready"] = true + status["message"] = "ydotool is ready for automatic paste." + return status +} + +func getYdotoolSocketPath() (string, error) { + if socketPath := strings.TrimSpace(os.Getenv("YDOTOOL_SOCKET")); socketPath != "" { + if _, err := os.Stat(socketPath); err == nil { + return socketPath, nil + } + return "", fmt.Errorf("YDOTOOL_SOCKET is set but not accessible: %s", socketPath) + } + + socketPath := filepath.Join("/run/user", fmt.Sprintf("%d", os.Getuid()), ".ydotool_socket") + if _, err := os.Stat(socketPath); err != nil { + return "", fmt.Errorf("ydotoold socket not found at %s; run `systemctl --user start ydotool.service` after configuring /dev/uinput permissions", socketPath) + } + + return socketPath, nil +} + +func runLinuxInputCommand(path string, args []string, stdin string, timeout time.Duration, socketPath string) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() cmd := exec.CommandContext(ctx, path, args...) + cmd.Env = append(os.Environ(), "YDOTOOL_SOCKET="+socketPath) if stdin != "" { cmd.Stdin = strings.NewReader(stdin) } diff --git a/text_insert_status_nonlinux.go b/text_insert_status_nonlinux.go new file mode 100644 index 0000000..8c23e2f --- /dev/null +++ b/text_insert_status_nonlinux.go @@ -0,0 +1,13 @@ +//go:build !linux + +package main + +func linuxYdotoolStatus() map[string]interface{} { + return map[string]interface{}{ + "ready": false, + "installed": false, + "socket": false, + "socket_path": "", + "message": "ydotool is only used on Linux.", + } +}