-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
406 lines (379 loc) · 9.79 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"log"
"os"
"time"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xinerama"
xp "github.com/BurntSushi/xgb/xproto"
)
var (
xConn *xgb.Conn
rootXWin xp.Window
eventTime xp.Timestamp
keyRootX int16
keyRootY int16
keyState uint16
// proactiveChan carries X operations that happen of the program's
// own accord, such as animations. These are sent to the main goroutine
// from other goroutines. In comparison, examples of reactive operations
// are responding to window creation and key presses.
proactiveChan = make(chan func())
)
type checker interface {
Check() error
}
var checkers []checker
func check(c checker) {
checkers = append(checkers, c)
}
func sendClientMessage(xWin xp.Window, atom xp.Atom) {
check(xp.SendEventChecked(xConn, false, xWin, xp.EventMaskNoEvent,
string(xp.ClientMessageEvent{
Format: 32,
Window: xWin,
Type: atomWMProtocols,
Data: xp.ClientMessageDataUnionData32New([]uint32{
uint32(atom),
uint32(eventTime),
0,
0,
0,
}),
}.Bytes()),
))
}
func handleConfigureNotify(e xp.ConfigureNotifyEvent) {
if rootXWin != e.Window || (desktopWidth == e.Width && desktopHeight == e.Height) {
return
}
desktopWidth, desktopHeight = e.Width, e.Height
check(xp.ConfigureWindowChecked(
xConn,
desktopXWin,
xp.ConfigWindowWidth|xp.ConfigWindowHeight,
[]uint32{
uint32(desktopWidth),
uint32(desktopHeight),
},
))
check(xp.SetClipRectanglesChecked(
xConn, xp.ClipOrderingUnsorted, desktopXGC, 0, 0, []xp.Rectangle{{
X: 0, Y: 0, Width: desktopWidth, Height: desktopHeight,
}}))
check(xp.ClearAreaChecked(xConn, true, desktopXWin, 0, 0, desktopWidth, desktopHeight))
initScreens()
for k := dummyWorkspace.link[next]; k != &dummyWorkspace; k = k.link[next] {
k.layout()
}
makeLists()
}
func handleConfigureRequest(e xp.ConfigureRequestEvent) {
if w := findWindow(func(w *window) bool { return w.xWin == e.Window }); w != nil {
cne := xp.ConfigureNotifyEvent{
Event: w.xWin,
Window: w.xWin,
X: w.rect.X,
Y: w.rect.Y,
Width: w.rect.Width,
Height: w.rect.Height,
}
check(xp.SendEventChecked(xConn, false, w.xWin,
xp.EventMaskStructureNotify, string(cne.Bytes())))
return
}
mask, values := uint16(0), []uint32(nil)
if e.ValueMask&xp.ConfigWindowX != 0 {
mask |= xp.ConfigWindowX
values = append(values, uint32(e.X))
}
if e.ValueMask&xp.ConfigWindowY != 0 {
mask |= xp.ConfigWindowY
values = append(values, uint32(e.Y))
}
if e.ValueMask&xp.ConfigWindowWidth != 0 {
mask |= xp.ConfigWindowWidth
values = append(values, uint32(e.Width))
}
if e.ValueMask&xp.ConfigWindowHeight != 0 {
mask |= xp.ConfigWindowHeight
values = append(values, uint32(e.Height))
}
if e.ValueMask&xp.ConfigWindowBorderWidth != 0 {
mask |= xp.ConfigWindowBorderWidth
values = append(values, uint32(e.BorderWidth))
}
if e.ValueMask&xp.ConfigWindowSibling != 0 {
mask |= xp.ConfigWindowSibling
values = append(values, uint32(e.Sibling))
}
if e.ValueMask&xp.ConfigWindowStackMode != 0 {
mask |= xp.ConfigWindowStackMode
values = append(values, uint32(e.StackMode))
}
check(xp.ConfigureWindowChecked(xConn, e.Window, mask, values))
}
func manage(xWin xp.Window, mapRequest bool) {
callFocus := false
w := findWindow(func(w *window) bool { return w.xWin == xWin })
if w == nil {
wmDeleteWindow, wmTakeFocus := false, false
if prop, err := xp.GetProperty(xConn, false, xWin, atomWMProtocols,
xp.GetPropertyTypeAny, 0, 64).Reply(); err != nil {
log.Println(err)
} else if prop != nil {
for v := prop.Value; len(v) >= 4; v = v[4:] {
switch xp.Atom(u32(v)) {
case atomWMDeleteWindow:
wmDeleteWindow = true
case atomWMTakeFocus:
wmTakeFocus = true
}
}
}
transientFor := (*window)(nil)
if prop, err := xp.GetProperty(xConn, false, xWin, atomWMTransientFor,
xp.GetPropertyTypeAny, 0, 64).Reply(); err != nil {
log.Println(err)
} else if prop != nil {
if v := prop.Value; len(v) == 4 {
transientForXWin := xp.Window(u32(v))
transientFor = findWindow(func(w *window) bool {
return w.xWin == transientForXWin
})
}
}
k := screens[0].workspace
if p, err := xp.QueryPointer(xConn, rootXWin).Reply(); err != nil {
log.Println(err)
} else if p != nil {
k = screenContaining(p.RootX, p.RootY).workspace
}
w = &window{
transientFor: transientFor,
xWin: xWin,
rect: xp.Rectangle{
X: offscreenXY,
Y: offscreenXY,
Width: 1,
Height: 1,
},
wmDeleteWindow: wmDeleteWindow,
wmTakeFocus: wmTakeFocus,
}
f := k.focusedFrame
previous := k.dummyWindow.link[prev]
if transientFor != nil {
previous = transientFor
} else if f.window != nil {
previous = f.window
}
w.link[next] = previous.link[next]
w.link[prev] = previous
w.link[next].link[prev] = w
w.link[prev].link[next] = w
if transientFor != nil && transientFor.frame != nil {
f = transientFor.frame
f.window, transientFor.frame = nil, nil
} else if f.window != nil {
f = k.mainFrame.firstEmptyFrame()
}
if f != nil {
f.window, w.frame = w, f
callFocus = f == k.focusedFrame
} else {
pulseChan <- time.Now()
}
check(xp.ChangeWindowAttributesChecked(xConn, xWin, xp.CwEventMask,
[]uint32{xp.EventMaskEnterWindow | xp.EventMaskStructureNotify},
))
w.configure()
if transientFor != nil {
transientFor.hasTransientFor = true
transientFor.configure()
}
}
if mapRequest {
check(xp.MapWindowChecked(xConn, xWin))
}
if callFocus {
focus(w)
}
makeLists()
pulseChan <- time.Now()
}
func unmanage(xWin xp.Window) {
w := findWindow(func(w *window) bool { return w.xWin == xWin })
if w == nil {
return
}
if quitting && findWindow(func(w *window) bool { return true }) == nil {
os.Exit(0)
}
if w.hasTransientFor {
for {
w1 := findWindow(func(w2 *window) bool { return w2.transientFor == w })
if w1 == nil {
break
}
w1.transientFor = nil
}
}
if f := w.frame; f != nil {
k := f.workspace
replacement := (*window)(nil)
if w.transientFor != nil && w.transientFor.frame == nil {
replacement = w.transientFor
} else {
bestOffscreenSeqNum := uint32(0)
for w1 := w.link[next]; w1 != w; w1 = w1.link[next] {
if w1.offscreenSeqNum <= bestOffscreenSeqNum {
continue
}
if k.fullscreen {
if w1.frame == k.focusedFrame {
continue
}
} else if w1.frame != nil {
continue
}
replacement, bestOffscreenSeqNum = w1, w1.offscreenSeqNum
}
}
if replacement != nil {
if f0 := replacement.frame; f0 != nil {
f0.window, replacement.frame = nil, nil
}
f.window, replacement.frame = replacement, f
replacement.configure()
if p, err := xp.QueryPointer(xConn, rootXWin).Reply(); err != nil {
log.Println(err)
} else if p != nil && contains(f.rect, p.RootX, p.RootY) {
focus(replacement)
}
} else {
f.window = nil
if k.fullscreen && f == k.focusedFrame {
doFullscreen(k, nil)
}
}
}
w.link[next].link[prev] = w.link[prev]
w.link[prev].link[next] = w.link[next]
*w = window{}
makeLists()
pulseChan <- time.Now()
}
type xEventOrError struct {
event xgb.Event
error xgb.Error
}
func main() {
var err error
xConn, err = xgb.NewConn()
if err != nil {
log.Fatal(err)
}
if err = xinerama.Init(xConn); err != nil {
log.Fatal(err)
}
xSetup := xp.Setup(xConn)
if len(xSetup.Roots) != 1 {
log.Fatalf("X setup has unsupported number of roots: %d", len(xSetup.Roots))
}
rootXWin = xSetup.Roots[0].Root
becomeTheWM()
initAtoms()
initDesktop(&xSetup.Roots[0])
initKeyboardMapping()
initScreens()
// Manage any existing windows.
if tree, err := xp.QueryTree(xConn, rootXWin).Reply(); err != nil {
log.Fatal(err)
} else if tree != nil {
for _, c := range tree.Children {
if c == desktopXWin {
continue
}
attrs, err := xp.GetWindowAttributes(xConn, c).Reply()
if attrs == nil || err != nil {
continue
}
if attrs.OverrideRedirect || attrs.MapState == xp.MapStateUnmapped {
continue
}
manage(c, false)
}
}
// Focus the last frame of the first screen. The first screen because
// there's typically only one screen. The last frame because, for
// left-to-right languages, the last frame's text is typically closer to
// the screen center than the first frame's text.
if len(screens) > 0 {
warpPointerTo(screens[0].workspace.mainFrame.lastDescendent())
}
// Process X events.
eeChan := make(chan xEventOrError)
go func() {
for {
e, err := xConn.WaitForEvent()
eeChan <- xEventOrError{e, err}
}
}()
for {
for i, c := range checkers {
if err := c.Check(); err != nil {
log.Println(err)
}
checkers[i] = nil
}
checkers = checkers[:0]
select {
case f := <-proactiveChan:
f()
case ee := <-eeChan:
if ee.error != nil {
log.Println(ee.error)
continue
}
switch e := ee.event.(type) {
case xp.ButtonPressEvent:
eventTime = e.Time
handleButtonPress(e)
case xp.ButtonReleaseEvent:
eventTime = e.Time
case xp.ClientMessageEvent:
// No-op.
case xp.ConfigureNotifyEvent:
handleConfigureNotify(e)
case xp.ConfigureRequestEvent:
handleConfigureRequest(e)
case xp.DestroyNotifyEvent:
// No-op.
case xp.EnterNotifyEvent:
eventTime = e.Time
handleEnterNotify(e)
case xp.ExposeEvent:
handleExpose(e)
case xp.KeyPressEvent:
eventTime, keyRootX, keyRootY, keyState = e.Time, e.RootX, e.RootY, e.State
handleKeyPress(e)
case xp.KeyReleaseEvent:
eventTime, keyRootX, keyRootY, keyState = e.Time, 0, 0, 0
case xp.MapNotifyEvent:
// No-op.
case xp.MappingNotifyEvent:
// No-op.
case xp.MapRequestEvent:
manage(e.Window, true)
case xp.MotionNotifyEvent:
eventTime = e.Time
handleMotionNotify(e)
case xp.UnmapNotifyEvent:
unmanage(e.Window)
default:
log.Printf("unhandled event: %v", ee.event)
}
}
}
}