-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
194 lines (150 loc) · 3.42 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
_ "embed"
"fmt"
"io"
"os"
"syscall"
"runtime/debug"
"github.com/jezek/xgbutil/xevent"
"github.com/leukipp/cortile/v2/common"
"github.com/leukipp/cortile/v2/desktop"
"github.com/leukipp/cortile/v2/input"
"github.com/leukipp/cortile/v2/store"
"github.com/leukipp/cortile/v2/ui"
log "github.com/sirupsen/logrus"
)
var (
// Build name
name string = "cortile"
// Build target
target string = "unknown"
// Build version
version string = "0.0.0"
// Build commit
commit string = "local"
// Build date
date string = "unknown"
// Build source
source string = "github.com/leukipp/cortile"
// Build flags
flags string
)
var (
//go:embed config.toml
toml []byte
//go:embed assets/images/logo.png
logo []byte
)
func main() {
// Init process, build and source information
common.InitInfo(name, target, version, commit, date, source, flags)
// Init command line arguments
common.InitArgs(input.Introspect())
// Init embedded files
common.InitFiles(toml, logo)
// Run dbus instance
runDbus()
// Run main instance
runMain()
}
func runDbus() {
property := len(common.Args.Dbus.Property) > 0
method := len(common.Args.Dbus.Method) > 0
listen := common.Args.Dbus.Listen
// Receive dbus property
if property {
input.Property(common.Args.Dbus.Property)
}
// Execute dbus method
if method {
input.Method(common.Args.Dbus.Method, common.Args.Dbus.P)
}
// Listen to dbus events
if listen {
go input.Listen(common.Args.Dbus.P)
select {}
}
// Prevent main instance start
if property || method || listen {
os.Exit(0)
}
}
func runMain() {
defer func() {
if err := recover(); err != nil {
log.Fatal(fmt.Errorf("%s\n%s", err, debug.Stack()))
}
}()
// Init lock and log files
defer InitLock().Close()
InitLog()
// Init cache and config
common.InitCache()
common.InitConfig()
// Init root properties
store.InitRoot()
// Create tracker instance
tr := desktop.CreateTracker()
input.Bind(tr)
tr.Update()
// Show layout overlay
ws := tr.ActiveWorkspace()
if ws.TilingEnabled() {
ui.ShowLayout(ws)
}
// Run X event loop
xevent.Main(store.X)
}
func InitLock() *os.File {
file, err := createLockFile(common.Args.Lock)
if err != nil {
fmt.Println(fmt.Errorf("%s already running (%s)", common.Build.Name, err))
os.Exit(1)
}
return file
}
func InitLog() *os.File {
if common.Args.VVV {
log.SetLevel(log.TraceLevel)
} else if common.Args.VV {
log.SetLevel(log.DebugLevel)
} else if common.Args.V {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.WarnLevel)
}
log.SetFormatter(&log.TextFormatter{ForceColors: true, FullTimestamp: true})
file, err := createLogFile(common.Args.Log)
if err != nil {
return file
}
log.SetOutput(io.MultiWriter(os.Stderr, file))
log.RegisterExitHandler(func() {
if file != nil {
file.Close()
}
})
return file
}
func createLockFile(filename string) (*os.File, error) {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
fmt.Println(fmt.Errorf("FILE error (%s)", err))
return nil, nil
}
err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
file.Close()
return nil, err
}
return file, nil
}
func createLogFile(filename string) (*os.File, error) {
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(fmt.Errorf("FILE error (%s)", err))
return nil, err
}
return file, nil
}