forked from FyshOS/fin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (81 loc) · 1.63 KB
/
main.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
91
92
93
94
package main // import "fyne.io/fin"
import (
"os"
"os/exec"
"os/signal"
"runtime"
"syscall"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
)
func hostname() string {
host, err := os.Hostname()
if err != nil {
host = "localhost"
}
return host
}
func init() {
runtime.LockOSThread()
}
func main() {
var xPID int
display := os.Getenv("DISPLAY")
if display == "" {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM)
go func() {
for {
<-sig
stopX(xPID)
}
}()
xPID = startX()
_ = os.Setenv("DISPLAY", ":0")
}
a := app.NewWithID("io.fyne.fin")
w := a.Driver().CreateWindow("Fin")
ui := newUI(w, a.Preferences(), hostname, getUsers)
w.SetPadded(false)
if display == "" {
screenW, screenH := getScreenSize()
go func() {
tries := 0
scale := float32(1.0)
for scale == float32(1.0) && tries < 50 {
time.Sleep(time.Millisecond * 100) // TODO use lifecycle to resize this at the correct time
scale = w.Canvas().Scale()
tries++
}
w.Resize(fyne.NewSize(float32(screenW)/scale, float32(screenH)/scale))
}()
w.Resize(fyne.NewSize(float32(screenW), float32(screenH)))
ui.loadUI()
} else {
ui.loadUI()
w.Resize(fyne.NewSize(1280, 720))
}
w.ShowAndRun()
if xPID != 0 {
stopX(xPID)
}
}
func startX() int {
cmd := "X :0 vt05"
exe := exec.Command("/bin/sh", "-c", cmd)
err := exe.Start()
if err != nil {
fyne.LogError("Could not start X server", err)
os.Exit(1)
}
time.Sleep(time.Second)
return exe.Process.Pid
}
func stopX(pid int) {
p, err := os.FindProcess(pid)
if err != nil {
fyne.LogError("Could not find X server pid", err)
}
_ = p.Kill()
}