Merge pull request #3 from jahruz67/codex/refactor-linux-global-hotkey-system

Linux: add --press daemon trigger architecture for Wayland hotkeys
This commit is contained in:
jahruz67
2026-05-19 20:17:24 -07:00
committed by GitHub
5 changed files with 135 additions and 38 deletions
+37 -29
View File
@@ -6,8 +6,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync/atomic"
"strings" "strings"
"sync/atomic"
"time" "time"
"wis-free-v3/internal/audio/recorder" "wis-free-v3/internal/audio/recorder"
@@ -90,6 +90,7 @@ func (a *App) startup(ctx context.Context) {
// Initialize components // Initialize components
a.startupHeadless() a.startupHeadless()
a.startLinuxPressDaemon()
// When the user launches the app again while it is already running (tray-only), // When the user launches the app again while it is already running (tray-only),
// the second process signals us here so the settings window becomes visible. // the second process signals us here so the settings window becomes visible.
@@ -338,7 +339,6 @@ func (a *App) processRecording(recordingPath string) {
transcribeDuration := time.Since(startTranscribe) transcribeDuration := time.Since(startTranscribe)
logger.Info("Transcription completed in %v", transcribeDuration) logger.Info("Transcription completed in %v", transcribeDuration)
if err != nil { if err != nil {
logger.Error("Transcription failed: %v", err) logger.Error("Transcription failed: %v", err)
tray.UpdateStatus("Ready") tray.UpdateStatus("Ready")
@@ -437,6 +437,12 @@ func (a *App) GetSettings() map[string]interface{} {
conf["history"] = a.config.History conf["history"] = a.config.History
conf["startup"] = platform.IsInStartup() conf["startup"] = platform.IsInStartup()
conf["app_version"] = AppVersion conf["app_version"] = AppVersion
if runtime.GOOS == "linux" {
if exePath, err := os.Executable(); err == nil {
conf["linux_press_command"] = exePath + " --press"
}
conf["linux_press_mode"] = true
}
return conf return conf
} }
@@ -445,35 +451,37 @@ func (a *App) SaveSettings(settings map[string]interface{}) string {
if val, ok := settings["api_key"].(string); ok { if val, ok := settings["api_key"].(string); ok {
a.config.APIKey = val a.config.APIKey = val
} }
if val, ok := settings["shortcut"].(string); ok { if runtime.GOOS != "linux" {
_, _, modOnly, ok := hotkey.ParseShortcut(val) if val, ok := settings["shortcut"].(string); ok {
if !ok { _, _, modOnly, ok := hotkey.ParseShortcut(val)
logger.Error("Invalid shortcut: %s (rejected)", val) if !ok {
return "Invalid shortcut - use modifiers plus a key (e.g. ctrl+k), or modifier-only on Windows (e.g. ctrl+win)" logger.Error("Invalid shortcut: %s (rejected)", val)
} return "Invalid shortcut - use modifiers plus a key (e.g. ctrl+k), or modifier-only on Windows (e.g. ctrl+win)"
if modOnly && runtime.GOOS != "windows" { }
return "Modifier-only shortcuts (like ctrl+win) are only supported on Windows" if modOnly && runtime.GOOS != "windows" {
} return "Modifier-only shortcuts (like ctrl+win) are only supported on Windows"
}
a.config.Shortcut = val a.config.Shortcut = val
// Update existing listener with new shortcut (hot-swap) // Update existing listener with new shortcut (hot-swap)
if a.hotkeyListener != nil { if a.hotkeyListener != nil {
a.hotkeyListener.UpdateShortcut(val) a.hotkeyListener.UpdateShortcut(val)
} else { } else {
// Should not happen if app started correctly, but just in case // Should not happen if app started correctly, but just in case
a.hotkeyListener = hotkey.NewListener(val, a.StartRecording, a.StopRecording) a.hotkeyListener = hotkey.NewListener(val, a.StartRecording, a.StopRecording)
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
a.hotkeyListener.SetRegistrationErrorCallback(func(err error) { a.hotkeyListener.SetRegistrationErrorCallback(func(err error) {
logger.Error("Linux hotkey registration failed: %v", err) logger.Error("Linux hotkey registration failed: %v", err)
go func() { go func() {
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
if a.overlay != nil { if a.overlay != nil {
a.overlay.Show("Shortcut registration failed. Please add a custom system shortcut calling 'wis-free-v3 --action=toggle' as a fallback.") a.overlay.Show("Shortcut registration failed. Please add a custom system shortcut calling 'wis-free-v3 --action=toggle' as a fallback.")
} }
}() }()
}) })
}
a.hotkeyListener.Start()
} }
a.hotkeyListener.Start()
} }
} }
if val, ok := settings["whisper_model"].(string); ok { if val, ok := settings["whisper_model"].(string); ok {
File diff suppressed because one or more lines are too long
+7 -6
View File
@@ -43,18 +43,19 @@
<div class="section-group-label">Input</div> <div class="section-group-label">Input</div>
<!-- Shortcut --> <!-- Linux Wayland Hotkey Command -->
<div class="section"> <div class="section" id="linuxPressSection" style="display: none;">
<label>Hotkey</label> <label>System Hotkey Command (Linux)</label>
<div class="form-control"> <div class="form-control">
<div class="flex-row"> <div class="flex-row">
<div class="input-wrapper"> <div class="input-wrapper">
<input type="text" id="shortcutInput" placeholder="Click Record to set..." readonly> <input type="text" id="linuxPressCommand" readonly>
</div> </div>
<button onclick="recordShortcut()" id="recordBtn">Record</button> <button onclick="copyLinuxPressCommand()">Copy</button>
</div> </div>
</div> </div>
<p class="hint">Click Record, then press your desired key combination (e.g. Ctrl+X).</p> <p class="hint">GNOME: Settings -> Keyboard -> Custom Shortcuts -> Add a shortcut using the copied command.</p>
<p class="hint">KDE: System Settings -> Shortcuts -> Command/URL -> Add a shortcut using the copied command.</p>
</div> </div>
<!-- Microphone --> <!-- Microphone -->
+83
View File
@@ -0,0 +1,83 @@
//go:build linux
package main
import (
"net/http"
"os"
"os/exec"
"sync"
"time"
)
const linuxPressAddr = "127.0.0.1:9876"
var (
linuxPressMu sync.Mutex
linuxPressTimer *time.Timer
linuxPressRecording bool
)
func init() {
if hasPressFlag(os.Args[1:]) {
if err := sendLinuxPressPing(); err != nil {
_ = exec.Command("notify-send", "Voice App", "Please open the main application first").Run()
os.Exit(1)
}
os.Exit(0)
}
}
func hasPressFlag(args []string) bool {
for _, arg := range args {
if arg == "--press" {
return true
}
}
return false
}
func sendLinuxPressPing() error {
client := &http.Client{Timeout: 300 * time.Millisecond}
resp, err := client.Get("http://" + linuxPressAddr + "/press")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return os.ErrNotExist
}
return nil
}
func (a *App) startLinuxPressDaemon() {
mux := http.NewServeMux()
mux.HandleFunc("/press", func(w http.ResponseWriter, r *http.Request) {
linuxPressMu.Lock()
defer linuxPressMu.Unlock()
if !linuxPressRecording {
linuxPressRecording = true
go a.StartRecording()
}
if linuxPressTimer != nil {
linuxPressTimer.Stop()
}
linuxPressTimer = time.AfterFunc(300*time.Millisecond, func() {
linuxPressMu.Lock()
defer linuxPressMu.Unlock()
if linuxPressRecording {
linuxPressRecording = false
go a.StopRecording()
}
})
w.WriteHeader(http.StatusNoContent)
})
go func() {
_ = http.ListenAndServe(linuxPressAddr, mux)
}()
}
+5
View File
@@ -0,0 +1,5 @@
//go:build !linux
package main
func (a *App) startLinuxPressDaemon() {}