forked from leukipp/cortile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (115 loc) · 2.62 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
package main
import (
_ "embed"
"fmt"
"io"
"os"
"runtime/debug"
"syscall"
"github.com/BurntSushi/xgbutil/xevent"
"github.com/seyys/sticky-display/common"
"github.com/seyys/sticky-display/desktop"
"github.com/seyys/sticky-display/input"
"github.com/seyys/sticky-display/store"
log "github.com/sirupsen/logrus"
)
var (
// Build name
name = "sticky-display"
// Build version
version = "0.1"
// Build commit
commit = "local"
// Build date
date = "unknown"
)
var (
//go:embed config.toml
toml []byte
)
func main() {
// Init command line arguments
common.InitArgs(name, version, commit, date)
// Init embedded files
common.InitFiles(toml)
// Init lock and log files
defer InitLock().Close()
InitLog()
// Init config and root
common.InitConfig()
store.InitRoot()
prepare()
if common.Args.PrintDisplay {
println("This is display", store.CurrentScreen)
return
}
// Run X event loop
xevent.Main(store.X)
}
func prepare() {
defer func() {
if err := recover(); err != nil {
log.Fatal(fmt.Errorf("%s\n%s", err, debug.Stack()))
}
}()
// Create workspaces and tracker
workspaces := desktop.CreateWorkspaces()
tracker := desktop.CreateTracker(workspaces)
// Bind input events
input.BindSignal(tracker)
input.BindSocket(tracker)
input.BindMouse(tracker)
input.BindKeys(tracker)
}
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
}