From df8d122229d155064c85a7db51aa291476b3beb4 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Fri, 30 Oct 2026 17:23:31 -0600 Subject: [PATCH] feat(autostart): XDG autostart entry for AppImage/plain installs Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013UzjWSBjtVZ2zi8fJxS2hH --- cmd/deckhudd/main.go | 40 ++++++++++++++++++++----- internal/autostart/xdg.go | 51 ++++++++++++++++++++++++++++++++ internal/autostart/xdg_test.go | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 internal/autostart/xdg.go create mode 100644 internal/autostart/xdg_test.go diff --git a/cmd/deckhudd/main.go b/cmd/deckhudd/main.go index 621bd17..9a895cb 100644 --- a/cmd/deckhudd/main.go +++ b/cmd/deckhudd/main.go @@ -29,8 +29,9 @@ commands: (default: ../share/deckhud/ui next to the binary) events run without the overlay, printing events as JSON lines demo [ui-dir] play a scripted session through the overlay (no controller needed) - autostart on|off start with Plasma via the Background portal (Flatpak); - native installs use the systemd user unit instead + autostart on|off start with Plasma: Background portal in Flatpak, an XDG + autostart entry otherwise (AppImage); 'mise run install' + uses a systemd user unit instead peek toggle the cheat sheet in the running daemon (bind it to a KDE shortcut, then map a Steam button to that) find print the Deck controller hidraw device @@ -63,11 +64,7 @@ func main() { case "events": err = run(ctx, runOpts{print: true}) case "autostart": - enable := len(os.Args) < 3 || os.Args[2] != "off" - var granted bool - if granted, err = autostart.Request(ctx, enable, []string{"deckhudd", "run"}); err == nil { - fmt.Println("autostart:", map[bool]string{true: "on", false: "off"}[granted]) - } + err = setAutostart(ctx, len(os.Args) < 3 || os.Args[2] != "off") case "peek": err = control.Send(control.DefaultPath(), "peek") case "find": @@ -252,3 +249,32 @@ func defaultUIDir() string { } return filepath.Join(filepath.Dir(exe), "..", "share", "deckhud", "ui") } + +// setAutostart uses the Background portal inside Flatpak and an XDG autostart +// entry otherwise, pointing at the AppImage itself when run from one. +func setAutostart(ctx context.Context, enable bool) error { + state := map[bool]string{true: "on", false: "off"} + if os.Getenv("FLATPAK_ID") != "" { + granted, err := autostart.Request(ctx, enable, []string{"deckhudd", "run"}) + if err == nil { + fmt.Println("autostart:", state[granted]) + } + return err + } + self := os.Getenv("APPIMAGE") + if self == "" { + var err error + if self, err = os.Executable(); err != nil { + return err + } + } + dir, err := os.UserConfigDir() + if err != nil { + return err + } + path, err := autostart.WriteXDG(filepath.Join(dir, "autostart"), enable, []string{self, "run"}) + if err == nil { + fmt.Printf("autostart: %s (%s)\n", state[enable], path) + } + return err +} diff --git a/internal/autostart/xdg.go b/internal/autostart/xdg.go new file mode 100644 index 0000000..b0fcf60 --- /dev/null +++ b/internal/autostart/xdg.go @@ -0,0 +1,51 @@ +package autostart + +import ( + "errors" + "os" + "path/filepath" + "strings" +) + +const desktopID = "io.github.RustyRaptor.deckhud.desktop" + +// Exec renders args as a Desktop Entry Exec value: arguments with reserved +// characters are double-quoted with ", `, $ and \ escaped, then backslashes +// are escaped again by the general string rule, and % is doubled. +func Exec(args []string) string { + parts := make([]string, len(args)) + for i, a := range args { + a = strings.ReplaceAll(a, "%", "%%") + if strings.ContainsAny(a, " \t\n\"'\\><~|&;$*?#()`") { + r := strings.NewReplacer(`\`, `\\`, `"`, `\"`, "`", "\\`", `$`, `\$`) + a = `"` + r.Replace(a) + `"` + } + parts[i] = strings.ReplaceAll(a, `\`, `\\`) + } + return strings.Join(parts, " ") +} + +// WriteXDG creates (or with enable=false removes) an XDG autostart entry in +// dir, normally ~/.config/autostart. It returns the entry's path. +func WriteXDG(dir string, enable bool, args []string) (string, error) { + path := filepath.Join(dir, desktopID) + if !enable { + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { + return path, err + } + return path, nil + } + entry := "[Desktop Entry]\n" + + "Type=Application\n" + + "Name=deckhud\n" + + "Comment=On-screen HUD for Steam Input\n" + + "Exec=" + Exec(args) + "\n" + + "Icon=io.github.RustyRaptor.deckhud\n" + + "Terminal=false\n" + + "OnlyShowIn=KDE;\n" + + "X-GNOME-Autostart-enabled=true\n" + if err := os.MkdirAll(dir, 0o755); err != nil { + return path, err + } + return path, os.WriteFile(path, []byte(entry), 0o644) +} diff --git a/internal/autostart/xdg_test.go b/internal/autostart/xdg_test.go new file mode 100644 index 0000000..a336e81 --- /dev/null +++ b/internal/autostart/xdg_test.go @@ -0,0 +1,54 @@ +package autostart + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestExecQuoting(t *testing.T) { + cases := map[string][]string{ + `/home/deck/Applications/deckhud.AppImage run`: {"/home/deck/Applications/deckhud.AppImage", "run"}, + `"/home/deck/My Apps/deckhud.AppImage" run`: {"/home/deck/My Apps/deckhud.AppImage", "run"}, + `"/tmp/a\\$b\\"c\\\\d\\` + "`" + `e f" run`: {"/tmp/a$b\"c\\d`e f", "run"}, + } + for want, args := range cases { + if got := Exec(args); got != want { + t.Errorf("Exec(%q)\n got %s\n want %s", args, got, want) + } + } +} + +func TestWriteXDGEnableDisable(t *testing.T) { + dir := filepath.Join(t.TempDir(), "autostart") + path, err := WriteXDG(dir, true, []string{"/apps/deckhud.AppImage", "run"}) + if err != nil { + t.Fatal(err) + } + if path != filepath.Join(dir, "io.github.RustyRaptor.deckhud.desktop") { + t.Errorf("path = %s", path) + } + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + for _, want := range []string{ + "[Desktop Entry]\n", "Type=Application\n", "Exec=/apps/deckhud.AppImage run\n", + "Icon=io.github.RustyRaptor.deckhud\n", "OnlyShowIn=KDE;\n", "X-GNOME-Autostart-enabled=true\n", + } { + if !strings.Contains(string(data), want) { + t.Errorf("entry lacks %q:\n%s", want, data) + } + } + + if _, err := WriteXDG(dir, false, nil); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(path); !os.IsNotExist(err) { + t.Errorf("disable should remove the entry, stat err = %v", err) + } + if _, err := WriteXDG(dir, false, nil); err != nil { + t.Errorf("disabling twice should be fine: %v", err) + } +}