-
Notifications
You must be signed in to change notification settings - Fork 6
/
sessions.go
90 lines (81 loc) · 1.82 KB
/
sessions.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"bufio"
"os"
"path/filepath"
"strings"
"fyne.io/fyne/v2"
)
var xinitSession = &session{
name: ".xinitrc",
exec: "/bin/sh -l .xinitrc",
}
type session struct {
name, exec string
}
func loadSessions() []*session {
var list []*session
for _, dir := range lookupXdgDataDirs() {
sessionDir := filepath.Join(dir, "xsessions")
files, err := os.ReadDir(sessionDir)
if err != nil {
continue
}
for _, file := range files {
list = append(list, loadSession(filepath.Join(sessionDir, file.Name())))
}
}
return list
}
func loadSession(path string) *session {
file, err := os.Open(path)
if err != nil {
fyne.LogError("Could not open session file", err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
data := session{}
var currentSection string
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "[") {
currentSection = line
}
if currentSection != "[Desktop Entry]" {
continue
}
if strings.HasPrefix(line, "Name=") {
name := strings.SplitAfter(line, "=")
data.name = name[1]
} else if strings.HasPrefix(line, "Exec=") {
exec := strings.SplitAfter(line, "=")
data.exec = exec[1]
}
}
if err := scanner.Err(); err != nil {
fyne.LogError("Could not read file", err)
return nil
}
return &data
}
// lookupXdgDataDirs returns a string slice of all XDG_DATA_DIRS
func lookupXdgDataDirs() []string {
dataHome := os.Getenv("XDG_DATA_HOME")
var dirs []string
if dataHome == "" {
home, err := os.UserHomeDir()
if err == nil {
dataHome = filepath.Join(home, ".local/share")
dirs = []string{dataHome}
}
} else {
dirs = []string{dataHome}
}
dataDirs := os.Getenv("XDG_DATA_DIRS")
if dataDirs == "" {
dataDirs = "/usr/local/share/:/usr/share/"
}
dirs = append(dirs, strings.Split(dataDirs, ":")...)
return dirs
}