-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
225 lines (190 loc) · 4.98 KB
/
server.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"image"
"os"
"os/exec"
"strings"
"deedles.dev/wlr"
"deedles.dev/ximage/geom"
)
var (
mainMenuText = []string{
"New",
"Resize",
"Tile",
"Move",
"Close",
"Hide",
}
systemMenuText = []string{
"Log Out",
}
)
type Server struct {
Terms []string
OutputConfigs []OutputConfig
display wlr.Display
allocator wlr.Allocator
backend wlr.Backend
compositor wlr.Compositor
cursor wlr.Cursor
outputLayout wlr.OutputLayout
renderer wlr.Renderer
seat wlr.Seat
cursorMgr wlr.XCursorManager
xdgShell wlr.XDGShell
layerShell wlr.LayerShellV1
xwayland wlr.Xwayland
decorationManager wlr.ServerDecorationManager
xdgDecorationManager wlr.XDGDecorationManagerV1
outputs []*Output
//inputs []wlr.InputDevice
pointers []wlr.Pointer
keyboards []*Keyboard
views []*View
tiled []*View
hidden []*View
newViews map[int]*geom.Rect[float64]
bg wlr.Texture
bgScale scaleFunc
mainMenu *Menu
systemMenu *Menu
statusBar *StatusBar
inputMode InputMode
onNewOutputListener wlr.Listener
onNewInputListener wlr.Listener
onCursorMotionListener wlr.Listener
onCursorMotionAbsoluteListener wlr.Listener
onCursorButtonListener wlr.Listener
onCursorAxisListener wlr.Listener
onCursorFrameListener wlr.Listener
onRequestCursorListener wlr.Listener
onNewXDGSurfaceListener wlr.Listener
onNewXwaylandSurfaceListener wlr.Listener
onNewLayerSurfaceListener wlr.Listener
onNewDecorationListener wlr.Listener
onNewToplevelDecorationListener wlr.Listener
}
func (server *Server) Release() {
server.onNewOutputListener.Destroy()
server.onNewInputListener.Destroy()
server.onCursorMotionListener.Destroy()
server.onCursorMotionAbsoluteListener.Destroy()
server.onCursorButtonListener.Destroy()
server.onCursorAxisListener.Destroy()
server.onCursorFrameListener.Destroy()
server.onRequestCursorListener.Destroy()
server.onNewXDGSurfaceListener.Destroy()
server.onNewXwaylandSurfaceListener.Destroy()
server.onNewLayerSurfaceListener.Destroy()
server.onNewDecorationListener.Destroy()
server.onNewToplevelDecorationListener.Destroy()
}
func (server *Server) Shutdown() {
server.display.Terminate()
}
func (server *Server) loadBG(path string) {
file, err := os.Open(path)
if err != nil {
wlr.Log(wlr.Error, "load %q as background: %v", path, err)
return
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
wlr.Log(wlr.Error, "decode %q as background: %v", path, err)
return
}
if server.bg.Valid() {
server.bg.Destroy()
}
server.bg = wlr.TextureFromImage(server.renderer, img)
wlr.Log(wlr.Info, "loaded %q as background", path)
}
func (server *Server) exec(to *geom.Rect[float64]) {
for _, term := range server.Terms {
args := strings.Fields(term)
cmd := exec.Command(args[0], args[1:]...) // TODO: Context support?
err := cmd.Start()
if err != nil {
wlr.Log(wlr.Error, "start new with %q: %v", term, err)
continue
}
server.newViews[cmd.Process.Pid] = to
return
}
wlr.Log(wlr.Error, "no valid terminals found for new window")
}
func (server *Server) initUI() {
server.initMainMenu()
server.initSystemMenu()
}
func (server *Server) initMainMenu() {
cbs := []func(){
server.onMainMenuNew,
server.onMainMenuResize,
server.onMainMenuTile,
server.onMainMenuMove,
server.onMainMenuClose,
server.onMainMenuHide,
}
items := func(yield func(*MenuItem) bool) {
for i, text := range mainMenuText {
item := NewTextMenuItem(server.renderer, text)
item.OnSelect = cbs[i]
if !yield(item) {
return
}
}
}
server.mainMenu = NewMenuFromSeq(items, len(mainMenuText))
}
func (server *Server) onMainMenuNew() {
server.startNew()
}
func (server *Server) onMainMenuResize() {
server.startSelectView(wlr.BtnRight, func(view *View) {
server.startResize(view)
})
}
func (server *Server) onMainMenuTile() {
server.startSelectView(wlr.BtnRight, func(view *View) {
server.toggleViewTiling(view)
server.startNormal()
})
}
func (server *Server) onMainMenuMove() {
server.startSelectView(wlr.BtnRight, func(view *View) {
server.startMove(view)
})
}
func (server *Server) onMainMenuClose() {
server.startSelectView(wlr.BtnRight, func(view *View) {
server.closeView(view)
server.startNormal()
})
}
func (server *Server) onMainMenuHide() {
server.startSelectView(wlr.BtnRight, func(view *View) {
server.hideView(view)
server.startNormal()
})
}
func (server *Server) initSystemMenu() {
cbs := []func(){
server.onSystemMenuLogOut,
}
items := func(yield func(*MenuItem) bool) {
for i, text := range systemMenuText {
item := NewTextMenuItem(server.renderer, text)
item.OnSelect = cbs[i]
if !yield(item) {
return
}
}
}
server.systemMenu = NewMenuFromSeq(items, len(systemMenuText))
}
func (server *Server) onSystemMenuLogOut() {
server.Shutdown()
}