mirror of
https://github.com/jahruz67/wisp-open.git
synced 2026-08-08 18:14:08 +00:00
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:
@@ -6,8 +6,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"wis-free-v3/internal/audio/recorder"
|
||||
@@ -90,6 +90,7 @@ func (a *App) startup(ctx context.Context) {
|
||||
|
||||
// Initialize components
|
||||
a.startupHeadless()
|
||||
a.startLinuxPressDaemon()
|
||||
|
||||
// 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.
|
||||
@@ -338,7 +339,6 @@ func (a *App) processRecording(recordingPath string) {
|
||||
transcribeDuration := time.Since(startTranscribe)
|
||||
logger.Info("Transcription completed in %v", transcribeDuration)
|
||||
|
||||
|
||||
if err != nil {
|
||||
logger.Error("Transcription failed: %v", err)
|
||||
tray.UpdateStatus("Ready")
|
||||
@@ -437,6 +437,12 @@ func (a *App) GetSettings() map[string]interface{} {
|
||||
conf["history"] = a.config.History
|
||||
conf["startup"] = platform.IsInStartup()
|
||||
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
|
||||
}
|
||||
|
||||
@@ -445,35 +451,37 @@ func (a *App) SaveSettings(settings map[string]interface{}) string {
|
||||
if val, ok := settings["api_key"].(string); ok {
|
||||
a.config.APIKey = val
|
||||
}
|
||||
if val, ok := settings["shortcut"].(string); ok {
|
||||
_, _, modOnly, ok := hotkey.ParseShortcut(val)
|
||||
if !ok {
|
||||
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 runtime.GOOS != "linux" {
|
||||
if val, ok := settings["shortcut"].(string); ok {
|
||||
_, _, modOnly, ok := hotkey.ParseShortcut(val)
|
||||
if !ok {
|
||||
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"
|
||||
}
|
||||
|
||||
a.config.Shortcut = val
|
||||
// Update existing listener with new shortcut (hot-swap)
|
||||
if a.hotkeyListener != nil {
|
||||
a.hotkeyListener.UpdateShortcut(val)
|
||||
} else {
|
||||
// Should not happen if app started correctly, but just in case
|
||||
a.hotkeyListener = hotkey.NewListener(val, a.StartRecording, a.StopRecording)
|
||||
if runtime.GOOS != "windows" {
|
||||
a.hotkeyListener.SetRegistrationErrorCallback(func(err error) {
|
||||
logger.Error("Linux hotkey registration failed: %v", err)
|
||||
go func() {
|
||||
time.Sleep(2 * time.Second)
|
||||
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.config.Shortcut = val
|
||||
// Update existing listener with new shortcut (hot-swap)
|
||||
if a.hotkeyListener != nil {
|
||||
a.hotkeyListener.UpdateShortcut(val)
|
||||
} else {
|
||||
// Should not happen if app started correctly, but just in case
|
||||
a.hotkeyListener = hotkey.NewListener(val, a.StartRecording, a.StopRecording)
|
||||
if runtime.GOOS != "windows" {
|
||||
a.hotkeyListener.SetRegistrationErrorCallback(func(err error) {
|
||||
logger.Error("Linux hotkey registration failed: %v", err)
|
||||
go func() {
|
||||
time.Sleep(2 * time.Second)
|
||||
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.hotkeyListener.Start()
|
||||
}
|
||||
a.hotkeyListener.Start()
|
||||
}
|
||||
}
|
||||
if val, ok := settings["whisper_model"].(string); ok {
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+7
-6
@@ -43,18 +43,19 @@
|
||||
|
||||
<div class="section-group-label">Input</div>
|
||||
|
||||
<!-- Shortcut -->
|
||||
<div class="section">
|
||||
<label>Hotkey</label>
|
||||
<!-- Linux Wayland Hotkey Command -->
|
||||
<div class="section" id="linuxPressSection" style="display: none;">
|
||||
<label>System Hotkey Command (Linux)</label>
|
||||
<div class="form-control">
|
||||
<div class="flex-row">
|
||||
<div class="input-wrapper">
|
||||
<input type="text" id="shortcutInput" placeholder="Click Record to set..." readonly>
|
||||
<input type="text" id="linuxPressCommand" readonly>
|
||||
</div>
|
||||
<button onclick="recordShortcut()" id="recordBtn">Record</button>
|
||||
<button onclick="copyLinuxPressCommand()">Copy</button>
|
||||
</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>
|
||||
|
||||
<!-- Microphone -->
|
||||
|
||||
@@ -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)
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
//go:build !linux
|
||||
|
||||
package main
|
||||
|
||||
func (a *App) startLinuxPressDaemon() {}
|
||||
Reference in New Issue
Block a user