-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathxinit.go
361 lines (331 loc) · 8.39 KB
/
xinit.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
package main
import (
"bytes"
"log"
"os/exec"
"github.com/BurntSushi/xgb/xinerama"
xp "github.com/BurntSushi/xgb/xproto"
)
var (
atomNetActiveWindow xp.Atom
atomNetWMName xp.Atom
atomWindow xp.Atom
atomWMClass xp.Atom
atomWMDeleteWindow xp.Atom
atomWMProtocols xp.Atom
atomWMTakeFocus xp.Atom
atomWMTransientFor xp.Atom
desktopXWin xp.Window
desktopXGC xp.Gcontext
desktopWidth uint16
desktopHeight uint16
keysyms [256][2]xp.Keysym
)
func becomeTheWM() {
if err := xp.ChangeWindowAttributesChecked(xConn, rootXWin, xp.CwEventMask, []uint32{
xp.EventMaskButtonPress |
xp.EventMaskButtonRelease |
xp.EventMaskPointerMotion |
xp.EventMaskStructureNotify |
xp.EventMaskSubstructureRedirect,
}).Check(); err != nil {
if _, ok := err.(xp.AccessError); ok {
log.Fatal("could not become the window manager. Is another window manager running?")
}
log.Fatal(err)
}
}
func initAtoms() {
atomNetActiveWindow = internAtom("_NET_ACTIVE_WINDOW")
atomNetWMName = internAtom("_NET_WM_NAME")
atomWindow = internAtom("WINDOW")
atomWMClass = internAtom("WM_CLASS")
atomWMDeleteWindow = internAtom("WM_DELETE_WINDOW")
atomWMProtocols = internAtom("WM_PROTOCOLS")
atomWMTakeFocus = internAtom("WM_TAKE_FOCUS")
atomWMTransientFor = internAtom("WM_TRANSIENT_FOR")
}
func internAtom(name string) xp.Atom {
r, err := xp.InternAtom(xConn, false, uint16(len(name)), name).Reply()
if err != nil {
log.Fatal(err)
}
if r == nil {
return 0
}
return r.Atom
}
func initDesktop(xScreen *xp.ScreenInfo) {
xCursorFont, err := xp.NewFontId(xConn)
if err != nil {
log.Fatal(err)
}
xCursor, err := xp.NewCursorId(xConn)
if err != nil {
log.Fatal(err)
}
err = xp.OpenFontChecked(xConn, xCursorFont, uint16(len("cursor")), "cursor").Check()
if err != nil {
log.Fatal(err)
}
const xcLeftPtr = 68 // XC_left_ptr from cursorfont.h.
err = xp.CreateGlyphCursorChecked(
xConn, xCursor, xCursorFont, xCursorFont, xcLeftPtr, xcLeftPtr+1,
0xffff, 0xffff, 0xffff, 0, 0, 0).Check()
if err != nil {
log.Fatal(err)
}
err = xp.CloseFontChecked(xConn, xCursorFont).Check()
if err != nil {
log.Fatal(err)
}
xTextFont, err := xp.NewFontId(xConn)
if err != nil {
log.Fatal(err)
}
err = xp.OpenFontChecked(xConn, xTextFont, uint16(len(fontName)), fontName).Check()
if err != nil {
log.Fatal(err)
}
defer xp.CloseFont(xConn, xTextFont)
desktopXWin, err = xp.NewWindowId(xConn)
if err != nil {
log.Fatal(err)
}
desktopXGC, err = xp.NewGcontextId(xConn)
if err != nil {
log.Fatal(err)
}
desktopWidth = xScreen.WidthInPixels
desktopHeight = xScreen.HeightInPixels
if err := xp.CreateWindowChecked(
xConn, xScreen.RootDepth, desktopXWin, xScreen.Root,
0, 0, desktopWidth, desktopHeight, 0,
xp.WindowClassInputOutput,
xScreen.RootVisual,
xp.CwOverrideRedirect|xp.CwEventMask,
[]uint32{
1,
xp.EventMaskExposure,
},
).Check(); err != nil {
log.Fatal(err)
}
if len(xSettings) != 0 {
initXSettings()
}
if err := xp.ConfigureWindowChecked(
xConn,
desktopXWin,
xp.ConfigWindowStackMode,
[]uint32{
xp.StackModeBelow,
},
).Check(); err != nil {
log.Fatal(err)
}
if err := xp.ChangeWindowAttributesChecked(
xConn,
desktopXWin,
xp.CwBackPixel|xp.CwCursor,
[]uint32{
xScreen.BlackPixel,
uint32(xCursor),
},
).Check(); err != nil {
log.Fatal(err)
}
if err := xp.CreateGCChecked(
xConn,
desktopXGC,
xp.Drawable(xScreen.Root),
xp.GcFont,
[]uint32{
uint32(xTextFont),
},
).Check(); err != nil {
log.Fatal(err)
}
if err := xp.MapWindowChecked(xConn, desktopXWin).Check(); err != nil {
log.Fatal(err)
}
}
func initKeyboardMapping() {
const (
keyLo = 8
keyHi = 255
)
km, err := xp.GetKeyboardMapping(xConn, keyLo, keyHi-keyLo+1).Reply()
if err != nil {
log.Fatal(err)
}
if km == nil {
log.Fatal("couldn't get keyboard mapping")
}
n := int(km.KeysymsPerKeycode)
if n < 2 {
log.Fatalf("too few keysyms per keycode: %d", n)
}
for i := keyLo; i <= keyHi; i++ {
keysyms[i][0] = km.Keysyms[(i-keyLo)*n+0]
keysyms[i][1] = km.Keysyms[(i-keyLo)*n+1]
}
toGrabs := []xp.Keysym{wmKeysym}
if doAudioActions {
toGrabs = append(toGrabs, xkAudioLowerVolume, xkAudioMute, xkAudioRaiseVolume)
}
for _, toGrab := range toGrabs {
keycode := xp.Keycode(0)
for i := keyLo; i <= keyHi; i++ {
if keysyms[i][0] == toGrab || keysyms[i][1] == toGrab {
keycode = xp.Keycode(i)
break
}
}
if keycode == 0 {
if toGrab != wmKeysym {
continue
}
log.Fatalf("could not find the window manager key %s", keysymString(toGrab))
}
if err := xp.GrabKeyChecked(xConn, false, rootXWin, xp.ModMaskAny, keycode,
xp.GrabModeAsync, xp.GrabModeAsync).Check(); err != nil {
log.Fatal(err)
}
}
// Disable Caps Lock if it is the wmKeysym.
if wmKeysym == xkCapsLock {
// On Ubuntu 12.04, disabling Caps Lock involved the equivalent of
// `xmodmap -e "clear lock"`. On Ubuntu 14.04, XKB has replaced xmodmap,
// possibly because this facilitates per-window keyboard layouts, so the
// equivalent of `xmodmap -e "clear lock"` doesn't work. As of October
// 2014, github.com/BurntSushi/xgb doesn't support XKB, so we exec the
// setxkbmap program instead of speaking the X11 protocol directly to
// disable Caps Lock.
if err := exec.Command("setxkbmap", "-option", "caps:none").Run(); err != nil {
log.Fatalf("setxkbmap failed: %v", err)
}
}
}
func findKeycode(keysym xp.Keysym) (keycode xp.Keycode, shift bool) {
for i, k := range keysyms {
if k[0] == keysym {
return xp.Keycode(i), false
}
if k[1] == keysym {
return xp.Keycode(i), true
}
}
return 0, false
}
func initScreens() {
oldScreens := screens
xine, err := xinerama.QueryScreens(xConn).Reply()
if err != nil {
log.Fatal(err)
}
if len(xine.ScreenInfo) > 0 {
screens = make([]*screen, len(xine.ScreenInfo))
for i, si := range xine.ScreenInfo {
screens[i] = &screen{
rect: xp.Rectangle{
X: si.XOrg,
Y: si.YOrg,
Width: si.Width - 1,
Height: si.Height - 1,
},
}
}
} else {
screens = make([]*screen, 1)
screens[0] = &screen{
rect: xp.Rectangle{
X: 0,
Y: 0,
Width: desktopWidth - 1,
Height: desktopHeight - 1,
},
}
}
for i, s := range screens {
k := (*workspace)(nil)
if i < len(oldScreens) {
k = oldScreens[i].workspace
oldScreens[i].workspace = nil
} else {
k = newWorkspace(s.rect, dummyWorkspace.link[prev])
}
s.workspace, k.screen = k, s
}
if len(oldScreens) > len(screens) {
for _, s := range oldScreens[len(screens):] {
s.workspace.screen = nil
s.workspace = nil
}
}
}
func initXSettings() {
a0 := internAtom("_XSETTINGS_S0")
if err := xp.SetSelectionOwnerChecked(xConn, desktopXWin, a0,
xp.TimeCurrentTime).Check(); err != nil {
log.Printf("could not set xsettings: %v", err)
return
}
a1 := internAtom("_XSETTINGS_SETTINGS")
encoded := makeEncodedXSettings()
if err := xp.ChangePropertyChecked(xConn, xp.PropModeReplace, desktopXWin, a1, a1,
8, uint32(len(encoded)), encoded).Check(); err != nil {
log.Printf("could not set xsettings: %v", err)
return
}
}
func makeEncodedXSettings() []byte {
b := new(bytes.Buffer)
b.WriteString("\x00\x00\x00\x00") // Zero means little-endian.
b.WriteString("\x00\x00\x00\x00") // Serial number.
writeUint32(b, uint32(len(xSettings)))
for _, s := range xSettings {
switch s.value.(type) {
case int:
b.WriteString("\x00\x00")
case float64:
b.WriteString("\x00\x00")
case string:
b.WriteString("\x01\x00")
default:
log.Fatalf("unsupported XSettings type %T", s.value)
}
writeUint16(b, uint16(len(s.name)))
b.WriteString(s.name)
if x := len(s.name) % 4; x != 0 {
b.WriteString("\x00\x00\x00\x00"[:4-x]) // Padding.
}
b.WriteString("\x00\x00\x00\x00") // Serial number.
switch v := s.value.(type) {
case int:
writeUint32(b, uint32(v))
case float64:
writeUint32(b, uint32(v))
case string:
writeUint32(b, uint32(len(v)))
b.WriteString(v)
if x := len(v) % 4; x != 0 {
b.WriteString("\x00\x00\x00\x00"[:4-x]) // Padding.
}
}
}
return b.Bytes()
}
func writeUint16(b *bytes.Buffer, u uint16) {
b.WriteByte(byte(u >> 0))
b.WriteByte(byte(u >> 8))
}
func writeUint32(b *bytes.Buffer, u uint32) {
b.WriteByte(byte(u >> 0))
b.WriteByte(byte(u >> 8))
b.WriteByte(byte(u >> 16))
b.WriteByte(byte(u >> 24))
}
func u32(b []byte) uint32 {
return uint32(b[0])<<0 | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
}