This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
/
wm.go
385 lines (351 loc) · 9.8 KB
/
wm.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
package wmutil
//go:generate go run gen.go
import (
"log"
"os"
"strings"
"sync"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
)
type Wm struct {
Conn *xgb.Conn
Setup *xproto.SetupInfo
DefaultScreen *xproto.ScreenInfo
DefaultRootId xproto.Window
Windows map[xproto.Window]*Window
CodeToSyms [][]uint32
SymToCodes map[uint32][]byte
stringToAtom map[string]xproto.Atom
atomToString map[xproto.Atom]string
logger *log.Logger
Map chan *Window
Unmap chan *Window
Stroke chan Stroke
NameChanged chan *Window
IconChanged chan *Window
Resize chan ResizeRequest
}
type ResizeRequest struct {
Width, Height int
Window *Window
}
type Window struct {
*sync.RWMutex
wm *Wm
Id xproto.Window
Parent xproto.Window
X, Y, Width, Height, Border int
Mapped bool
// properties
Name string
Icon string
Instance string
Class string
IsTransient bool
Protocols []xproto.Atom
}
type Config struct {
Logger *log.Logger
Strokes []Stroke
}
type Stroke struct {
Modifiers uint16
Sym uint32
}
type ChangeNotify struct {
Window *Window
Atom xproto.Atom
}
func New(config *Config) (*Wm, error) {
if config == nil {
config = new(Config)
}
// connect
conn, err := xgb.NewConn()
if err != nil {
return nil, err
}
// infos
setup := xproto.Setup(conn)
defaultScreen := setup.DefaultScreen(conn)
defaultRootId := defaultScreen.Root
// grab control
if err := xproto.ChangeWindowAttributesChecked(conn, defaultRootId, xproto.CwEventMask, []uint32{uint32(
xproto.EventMaskSubstructureRedirect |
xproto.EventMaskSubstructureNotify |
xproto.EventMaskPropertyChange)}).Check(); err != nil {
return nil, ef("another wm is running: %v", err)
}
// read keyboard mapping
kmReply, err := xproto.GetKeyboardMapping(conn, 8, 248).Reply()
if err != nil {
return nil, ef("get keyboard mapping: %v", err)
}
keycodeToKeysyms := make([][]uint32, 256)
keysymToKeycodes := make(map[uint32][]byte)
for keycode := 8; keycode <= 255; keycode++ {
start := (keycode - 8) * int(kmReply.KeysymsPerKeycode)
keysyms := kmReply.Keysyms[start : start+int(kmReply.KeysymsPerKeycode)]
for _, sym := range keysyms {
if sym == 0 {
continue
}
keysym := uint32(sym)
keycodeToKeysyms[keycode] = append(keycodeToKeysyms[keycode], uint32(keysym))
keysymToKeycodes[keysym] = append(keysymToKeycodes[keysym], byte(keycode))
}
}
// get modifier mask
mmReply, err := xproto.GetModifierMapping(conn).Reply()
if err != nil {
return nil, ef("get modifier mapping: %v", err)
}
symToModMask := func(sym uint32) uint16 {
code := keysymToKeycodes[sym][0]
index := 0
loop:
for ; index < 8; index++ {
start := index * int(mmReply.KeycodesPerModifier)
codes := mmReply.Keycodes[start : start+int(mmReply.KeycodesPerModifier)]
for _, c := range codes {
if byte(c) == code {
break loop
}
}
}
return []uint16{
xproto.ModMaskShift,
xproto.ModMaskLock,
xproto.ModMaskControl,
xproto.ModMask1,
xproto.ModMask2,
xproto.ModMask3,
xproto.ModMask4,
xproto.ModMask5,
}[index]
}
numlockModMask := symToModMask(Key_Num_Lock)
// grab keys
if err := xproto.UngrabKeyChecked(conn, xproto.GrabAny, defaultRootId, xproto.ModMaskAny).Check(); err != nil {
return nil, ef("ungrab keys: %v", err)
}
ignoreModifiers := []uint16{
0,
xproto.ModMaskLock,
numlockModMask,
xproto.ModMaskLock | numlockModMask,
}
for _, stroke := range config.Strokes {
for _, mod := range ignoreModifiers {
keycodes := keysymToKeycodes[stroke.Sym]
for _, code := range keycodes {
if err := xproto.GrabKeyChecked(conn, true, defaultRootId, stroke.Modifiers|mod,
xproto.Keycode(code), xproto.GrabModeAsync, xproto.GrabModeAsync).Check(); err != nil {
return nil, ef("grab key: %v", err)
}
}
}
}
wm := &Wm{
Conn: conn,
Setup: setup,
DefaultScreen: defaultScreen,
DefaultRootId: defaultRootId,
Windows: make(map[xproto.Window]*Window),
Map: make(chan *Window),
Unmap: make(chan *Window),
Stroke: make(chan Stroke),
CodeToSyms: keycodeToKeysyms,
SymToCodes: keysymToKeycodes,
stringToAtom: make(map[string]xproto.Atom),
atomToString: make(map[xproto.Atom]string),
NameChanged: make(chan *Window),
IconChanged: make(chan *Window),
Resize: make(chan ResizeRequest),
}
if config.Logger == nil {
wm.logger = log.New(os.Stdout, "==|>", log.Lmicroseconds)
} else {
wm.logger = config.Logger
}
// set supported ewmh hints
if err := wm.setSupported(); err != nil {
return nil, err
}
go wm.loop()
return wm, nil
}
func (w *Wm) Close() {
xproto.ChangeWindowAttributes(w.Conn, w.DefaultRootId, xproto.CwEventMask, []uint32{uint32(
xproto.EventMaskNoEvent)})
w.Conn.Close()
}
func (w *Wm) pt(format string, args ...interface{}) {
w.logger.Printf(format, args...)
}
func (w *Wm) loop() {
for {
ev, xerr := w.Conn.WaitForEvent()
if ev == nil && xerr == nil {
w.logger.Fatal("bad conn")
}
if xerr != nil {
w.pt("ERROR: %v\n", xerr)
}
if ev != nil {
switch ev := ev.(type) {
case xproto.ClientMessageEvent:
w.pt("client message %s\n", w.AtomName(ev.Type))
case xproto.CreateNotifyEvent:
if ev.OverrideRedirect { // do not manage override-redirect windows
continue
}
win := &Window{
RWMutex: new(sync.RWMutex),
wm: w,
Id: ev.Window,
Parent: ev.Parent,
X: int(ev.X),
Y: int(ev.Y),
Width: int(ev.Width),
Height: int(ev.Height),
Border: int(ev.BorderWidth),
}
w.Windows[win.Id] = win
// set event mask
if err := xproto.ChangeWindowAttributesChecked(w.Conn, win.Id, xproto.CwEventMask, []uint32{uint32(
xproto.EventMaskPropertyChange)}).Check(); err != nil {
w.pt("ERROR: set window event mask: %v\n", err)
}
// get class info
classInfo := win.GetStrsProperty(xproto.AtomWmClass)
if len(classInfo) > 0 {
win.Instance = classInfo[0]
win.Class = classInfo[1]
}
// whether transient window
transientFor := win.GetWindowIdProperty(xproto.AtomWmTransientFor)
win.IsTransient = transientFor != 0
// change WM_STATE
win.ChangeInt32sProperty(w.Atom("WM_STATE"), w.Atom("WM_STATE"), 1) // icccm NormalState
// get protocols
win.Protocols = win.GetAtomsProperty(w.Atom("WM_PROTOCOLS"))
case xproto.ConfigureRequestEvent:
if win, ok := w.Windows[ev.Window]; ok && win.Mapped { // managed and mapped window
win.ReadLock(func() {
notifyEv := xproto.ConfigureNotifyEvent{ // not moving or resizing now
Event: win.Id,
Window: win.Id,
X: int16(win.X),
Y: int16(win.Y),
Width: uint16(win.Width),
Height: uint16(win.Height),
}
xproto.SendEvent(w.Conn, false, win.Id, xproto.EventMaskStructureNotify, string(notifyEv.Bytes()))
})
// send fixed-sized window resize notify TODO
var width, height int
if xproto.ConfigWindowWidth&ev.ValueMask > 0 {
width = int(ev.Width)
}
if xproto.ConfigWindowHeight&ev.ValueMask > 0 {
height = int(ev.Height)
}
if width > 0 || height > 0 {
w.Resize <- ResizeRequest{
Width: width,
Height: height,
Window: win,
}
}
} else { // configure as requested
var vals []uint32
flags := ev.ValueMask
if xproto.ConfigWindowX&flags > 0 {
vals = append(vals, uint32(ev.X))
}
if xproto.ConfigWindowY&flags > 0 {
vals = append(vals, uint32(ev.Y))
}
if xproto.ConfigWindowWidth&flags > 0 {
vals = append(vals, uint32(ev.Width))
}
if xproto.ConfigWindowHeight&flags > 0 {
vals = append(vals, uint32(ev.Height))
}
if xproto.ConfigWindowBorderWidth&flags > 0 {
vals = append(vals, 0) // do not set border width
}
if xproto.ConfigWindowSibling&flags > 0 {
vals = append(vals, uint32(ev.Sibling))
}
if xproto.ConfigWindowStackMode&flags > 0 {
vals = append(vals, uint32(ev.StackMode))
}
xproto.ConfigureWindow(w.Conn, ev.Window, flags, vals)
}
case xproto.ConfigureNotifyEvent:
case xproto.MapRequestEvent:
xproto.MapWindow(w.Conn, ev.Window)
if win, ok := w.Windows[ev.Window]; ok {
win.WriteLock(func() {
win.Mapped = true
})
w.Map <- win
}
case xproto.MapNotifyEvent:
case xproto.UnmapNotifyEvent:
if win, ok := w.Windows[ev.Window]; ok {
win.WriteLock(func() {
win.Mapped = false
})
w.Unmap <- win
}
case xproto.DestroyNotifyEvent:
delete(w.Windows, ev.Window)
case xproto.KeyPressEvent:
w.Stroke <- Stroke{
Modifiers: ev.State,
Sym: w.CodeToSyms[ev.Detail][0],
}
case xproto.KeyReleaseEvent:
case xproto.PropertyNotifyEvent:
win, ok := w.Windows[ev.Window]
if !ok { // not managed
continue
}
switch ev.Atom {
case xproto.AtomWmName:
names := win.GetStrsProperty(ev.Atom)
win.WriteLock(func() {
win.Name = strings.Join(names, "")
})
w.NameChanged <- win
case w.Atom("_NET_WM_NAME"):
names := win.GetStrsProperty(ev.Atom)
win.WriteLock(func() {
win.Name = strings.Join(names, "")
})
w.NameChanged <- win
case xproto.AtomWmIconName:
names := win.GetStrsProperty(ev.Atom)
win.WriteLock(func() {
win.Icon = strings.Join(names, "")
})
w.IconChanged <- win
case w.Atom("_NET_WM_ICON_NAME"):
names := win.GetStrsProperty(ev.Atom)
win.WriteLock(func() {
win.Icon = strings.Join(names, "")
})
w.IconChanged <- win
default:
w.pt("property notify %s %v\n", w.AtomName(ev.Atom), ev)
}
default:
w.pt("EVENT: %T %v\n", ev, ev)
}
}
}
}