forked from diamondburned/gtkcord3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (113 loc) · 2.77 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"os"
"runtime"
"runtime/debug"
"runtime/trace"
"time"
"github.com/TheBlueOompaLoompa/gtkcord3/gtkcord"
"github.com/TheBlueOompaLoompa/gtkcord3/gtkcord/components/login"
"github.com/TheBlueOompaLoompa/gtkcord3/gtkcord/ningen"
"github.com/TheBlueOompaLoompa/gtkcord3/gtkcord/semaphore"
"github.com/TheBlueOompaLoompa/gtkcord3/internal/keyring"
"github.com/TheBlueOompaLoompa/gtkcord3/internal/log"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
// Profiler
"net/http"
_ "net/http/pprof"
)
var profile bool
func init() {
// flag.BoolVar(&profile, "prof", false, "Enable the profiler")
// AGGRESSIVE GC
debug.SetGCPercent(100)
// Set the right envs:
LoadEnvs()
debug.SetTraceback("all")
f, err := os.OpenFile("/tmp/gtkcord-trace", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0750)
if err != nil {
log.Fatalln("Failed to open trace file:", err)
}
if err := trace.Start(f); err != nil {
log.Fatalln("Failed to start trace:", err)
}
}
func LoadToken() string {
var token = os.Getenv("TOKEN")
if token != "" {
return token
}
// flag.StringVar(&token, "t", "", "Token")
// flag.Parse()
return token
}
func LoadKeyring() string {
// If $TOKEN or -t is provided, override it in the keyring and use it:
if token := LoadToken(); token != "" {
return token
}
// Does the keyring have the token? Maybe.
return keyring.Get()
}
func Login(finish func(s *ningen.State)) error {
var lastErr error
var token = LoadKeyring()
if token != "" {
_, err := ningen.Connect(token, finish)
if err == nil {
return nil
}
log.Errorln("Failed to re-use token:", err)
lastErr = err
}
// No, so we need to display the login window:
semaphore.IdleMust(func() {
l := login.NewLogin(finish)
l.LastError = lastErr
l.LastToken = token
l.Run()
})
return nil
}
func Finish(a *gtkcord.Application) func(s *ningen.State) {
return func(s *ningen.State) {
if err := a.Ready(s); err != nil {
log.Fatalln("Failed to get gtkcord ready:", err)
}
}
}
func main() {
a, err := gtk.ApplicationNew("com.github.diamondburned.gtkcord3", glib.APPLICATION_FLAGS_NONE)
if err != nil {
log.Fatalln("Failed to create a new *gtk.Application:", err)
}
g := gtkcord.New(a)
a.Connect("activate", func() {
g.Activate()
go func() {
// Try and log in:
if err := Login(Finish(g)); err != nil {
log.Fatalln("Failed to login:", err)
}
}()
})
a.Connect("shutdown", func() {
g.Close()
})
if profile {
// Profiler
runtime.SetMutexProfileFraction(5) // ???
runtime.SetBlockProfileRate(5000000) // 5ms
go http.ListenAndServe("localhost:6969", nil)
}
// I will do what's called an epic gamer move.
go func() {
for range time.Tick(30 * time.Second) {
runtime.GC()
}
}()
if sig := a.Run(os.Args); sig > 0 {
os.Exit(sig)
}
}