-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautostart_linux.go
More file actions
62 lines (50 loc) · 1.35 KB
/
autostart_linux.go
File metadata and controls
62 lines (50 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//go:build linux
package main
import (
"fmt"
"os"
"path/filepath"
)
// LinuxAutostart implements Autostart for Linux using XDG autostart
type LinuxAutostart struct{}
// NewAutostart creates a new autostart handler for Linux
func NewAutostart() Autostart {
return &LinuxAutostart{}
}
func (a *LinuxAutostart) getAutostartDir() string {
config := os.Getenv("XDG_CONFIG_HOME")
if config == "" {
home, _ := os.UserHomeDir()
config = filepath.Join(home, ".config")
}
return filepath.Join(config, "autostart")
}
func (a *LinuxAutostart) getDesktopFilePath() string {
return filepath.Join(a.getAutostartDir(), "mousekeys.desktop")
}
func (a *LinuxAutostart) IsEnabled() bool {
_, err := os.Stat(a.getDesktopFilePath())
return err == nil
}
func (a *LinuxAutostart) Enable() error {
// Create autostart directory if it doesn't exist
autostartDir := a.getAutostartDir()
if err := os.MkdirAll(autostartDir, 0755); err != nil {
return err
}
exe, _ := os.Executable()
desktopEntry := fmt.Sprintf(`[Desktop Entry]
Type=Application
Name=MouseKeys
Comment=Keyboard-based mouse control
Exec=%s
Icon=input-mouse
Terminal=false
Categories=Utility;Accessibility;
X-GNOME-Autostart-enabled=true
`, exe)
return os.WriteFile(a.getDesktopFilePath(), []byte(desktopEntry), 0644)
}
func (a *LinuxAutostart) Disable() error {
return os.Remove(a.getDesktopFilePath())
}