-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathactions.go
658 lines (617 loc) · 12.7 KB
/
actions.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package main
import (
"log"
"os"
"os/exec"
"strings"
"time"
xp "github.com/BurntSushi/xgb/xproto"
)
func doExec(_ *workspace, cmd1 interface{}) bool {
cmd, ok := cmd1.([]string)
if !ok {
return false
}
if len(cmd) == 0 {
return false
}
go func() {
c := exec.Command(cmd[0], cmd[1:]...)
if err := c.Start(); err != nil {
log.Printf("could not start command %q: %v", cmd, err)
}
// Ignore any error from the program itself.
c.Wait()
}()
return false
}
func doAudio(k *workspace, cmd1 interface{}) bool {
if !doAudioActions {
return false
}
doExec(k, cmd1)
return true
}
func doScreen(k *workspace, t1 interface{}) bool {
t, ok := t1.(traversal)
if !ok {
return false
}
i := -1
for j, s := range screens {
if s.workspace == k {
i = j
break
}
}
if i < 0 {
return true
}
if t == next {
i = (i + 1) % len(screens)
} else {
i = (i + len(screens) - 1) % len(screens)
}
warpPointerTo(screens[i].workspace.focusedFrame)
return true
}
func doFrame(k *workspace, t1 interface{}) bool {
t, ok := t1.(traversal)
if !ok {
return false
}
if k.fullscreen || k.listing != listNone {
return false
}
k.focusedFrame = k.focusedFrame.traverse(t)
warpPointerTo(k.focusedFrame)
return true
}
func warpPointerTo(f *frame) {
f.workspace.focusFrame(f)
check(xp.WarpPointerChecked(xConn, xp.WindowNone, rootXWin, 0, 0, 0, 0,
f.rect.X+int16(f.rect.Width/2),
f.rect.Y+int16(f.rect.Height/2),
))
makeLists()
}
func doWindow(k *workspace, t1 interface{}) bool {
t, ok := t1.(traversal)
if !ok {
return false
}
f0 := k.focusedFrame
dummy := &k.dummyWindow
w0 := f0.window
if w0 == nil {
w0 = dummy
}
w1 := w0
for {
w1 = w1.link[t]
if w1 == w0 {
return true
}
if w1 == dummy {
continue
}
if w1.frame == nil || k.fullscreen {
break
}
}
if w0 == dummy {
w0 = nil
}
changeWindow(f0, w0, w1)
return true
}
func doWindowN(k *workspace, n1 interface{}) bool {
n, ok := n1.(int)
if !ok {
return false
}
f0, w0 := k.focusedFrame, k.focusedFrame.window
w1 := k.dummyWindow.link[next]
for ; n > 0 && w1 != &k.dummyWindow; n-- {
w1 = w1.link[next]
}
if w1 == &k.dummyWindow || w1 == w0 {
return true
}
changeWindow(f0, w0, w1)
return true
}
func changeWindow(f0 *frame, w0, w1 *window) {
if f0 == nil || w1 == nil {
return
}
if w0 != w1 {
if f1 := w1.frame; f1 != nil {
if w0 != nil {
f1.window, w0.frame = w0, f1
} else {
f1.window = nil
}
} else if w0 != nil {
w0.frame = nil
}
f0.window, w1.frame = w1, f0
}
w1.configure()
if w0 != nil {
w0.configure()
}
focus(w1)
makeLists()
}
func doWorkspace(k0 *workspace, t1 interface{}) bool {
t, ok := t1.(traversal)
if !ok {
return false
}
k1 := k0
for {
k1 = k1.link[t]
if k1 == k0 {
return true
}
if k1 == &dummyWorkspace {
continue
}
if k1.screen == nil {
break
}
}
changeWorkspace(k0.screen, k0, k1)
return true
}
func doWorkspaceN(k0 *workspace, n1 interface{}) bool {
n, ok := n1.(int)
if !ok {
return false
}
k1 := dummyWorkspace.link[next]
for ; n > 0 && k1 != &dummyWorkspace; n-- {
k1 = k1.link[next]
}
if k1 == &dummyWorkspace || k1 == k0 {
return true
}
changeWorkspace(k0.screen, k0, k1)
return true
}
func doWorkspaceNew(k0 *workspace, _ interface{}) bool {
s := k0.screen
changeWorkspace(s, k0, newWorkspace(s.rect, k0))
return true
}
func changeWorkspace(s0 *screen, k0, k1 *workspace) {
if s0 == nil || k0 == nil || k1 == nil {
return
}
k0.listing = listNone
s1 := k1.screen
if k0 != k1 {
if s1 != nil {
s1.workspace, k0.screen = k0, s1
} else {
k0.screen = nil
}
s0.workspace, k1.screen = k1, s0
}
k1.layout()
k0.layout()
p, err := xp.QueryPointer(xConn, rootXWin).Reply()
if err != nil {
log.Println(err)
}
if p == nil {
k1.focusFrame(k1.mainFrame.firstDescendent())
} else {
k1.focusFrame(k1.frameContaining(p.RootX, p.RootY))
}
s0.repaint()
if s1 != nil {
s1.repaint()
}
makeLists()
}
func doWindowDelete(k *workspace, _ interface{}) bool {
w := k.focusedFrame.window
if w != nil && w.wmDeleteWindow {
sendClientMessage(w.xWin, atomWMDeleteWindow)
}
return true
}
func doWorkspaceDelete(k0 *workspace, _ interface{}) bool {
if k0.dummyWindow.link[next] != &k0.dummyWindow {
// Workspace-delete fails if the workspace contains a window.
return true
}
k1 := k0
for {
k1 = k1.link[next]
if k1 == k0 {
return true
}
if k1 == &dummyWorkspace {
continue
}
if k1.screen == nil {
break
}
}
k0.link[prev].link[next] = k0.link[next]
k0.link[next].link[prev] = k0.link[prev]
s := k0.screen
s.workspace, k1.screen = k1, s
*k0 = workspace{}
k1.layout()
focus(k1.focusedFrame.window)
s.repaint()
makeLists()
return true
}
func doList(k *workspace, l1 interface{}) bool {
l, ok := l1.(listing)
if !ok {
return false
}
if k.listing != l {
k.listing = l
} else {
k.listing = listNone
}
k.makeList()
k.focusFrame(k.focusedFrame)
return false
}
func doWindowNudge(k *workspace, t1 interface{}) bool {
t, ok := t1.(traversal)
if !ok {
return false
}
if w := k.focusedFrame.window; w != nil {
wn, wp := w.link[next], w.link[prev]
wn.link[prev] = wp
wp.link[next] = wn
if t == next {
w.link[next] = wn.link[next]
w.link[prev] = wn
} else {
w.link[next] = wp
w.link[prev] = wp.link[prev]
}
w.link[next].link[prev] = w
w.link[prev].link[next] = w
}
makeLists()
return true
}
func doWorkspaceNudge(k *workspace, t1 interface{}) bool {
t, ok := t1.(traversal)
if !ok {
return false
}
kn, kp := k.link[next], k.link[prev]
kn.link[prev] = kp
kp.link[next] = kn
if t == next {
k.link[next] = kn.link[next]
k.link[prev] = kn
} else {
k.link[next] = kp
k.link[prev] = kp.link[prev]
}
k.link[next].link[prev] = k
k.link[prev].link[next] = k
makeLists()
return true
}
func doWindowSelect(k *workspace, b1 interface{}) bool {
b, ok := b1.(bool)
if !ok {
return false
}
if b {
allSelected := true
for w := k.dummyWindow.link[next]; w != &k.dummyWindow; w = w.link[next] {
if !w.selected {
allSelected = false
break
}
}
for w := k.dummyWindow.link[next]; w != &k.dummyWindow; w = w.link[next] {
w.selected = !allSelected
}
} else {
if w := k.focusedFrame.window; w != nil {
w.selected = !w.selected
}
}
makeLists()
return true
}
func doWorkspaceMigrate(k *workspace, _ interface{}) bool {
previous := k.dummyWindow.link[prev]
if k.focusedFrame.window != nil {
previous = k.focusedFrame.window
}
var migrants []*window
for k0 := dummyWorkspace.link[next]; k0 != &dummyWorkspace; k0 = k0.link[next] {
migrants = migrants[:0]
for w := k0.dummyWindow.link[next]; w != &k0.dummyWindow; w = w.link[next] {
if !w.selected {
continue
}
w.selected = false
if k0 == k {
continue
}
migrants = append(migrants, w)
}
for _, w := range migrants {
if f := w.frame; f != nil {
f.window, w.frame = nil, nil
}
wn, wp := w.link[next], w.link[prev]
wn.link[prev] = wp
wp.link[next] = wn
wn, wp = previous.link[next], previous
wn.link[prev] = w
wp.link[next] = w
w.link[next], w.link[prev] = wn, wp
previous = w
f := k.focusedFrame
if f.window != nil {
f = k.mainFrame.firstEmptyFrame()
}
if f != nil {
f.window, w.frame = w, f
}
w.configure()
}
if k0.fullscreen && k0.focusedFrame.window == nil {
if k0.screen != nil {
doFullscreen(k0, nil)
} else {
k0.fullscreen = false
}
}
}
makeLists()
return true
}
func doFullscreen(k *workspace, _ interface{}) bool {
if !k.fullscreen && k.focusedFrame.window == nil {
return true
}
k.fullscreen = !k.fullscreen
if p, err := xp.QueryPointer(xConn, rootXWin).Reply(); err != nil {
log.Println(err)
} else if p != nil {
k.focusFrame(k.frameContaining(p.RootX, p.RootY))
}
k.configure()
if k.screen != nil {
k.screen.repaint()
}
return true
}
func doHide(k *workspace, _ interface{}) bool {
f := k.focusedFrame
w := f.window
if w != nil {
f.window, w.frame = nil, nil
w.configure()
}
if k.fullscreen {
doFullscreen(k, nil)
}
makeLists()
return true
}
func doMerge(k *workspace, _ interface{}) bool {
if k.fullscreen || k.listing == listWorkspaces {
return false
}
f := k.focusedFrame
if f.parent == nil {
// Merge fails if the frame is the main frame.
return true
}
w := f.window
if w != nil {
f.window, w.frame = nil, nil
w.configure()
}
if f.prevSibling != nil {
f.prevSibling.nextSibling = f.nextSibling
k.focusedFrame = f.prevSibling.lastDescendent()
}
if f.nextSibling != nil {
f.nextSibling.prevSibling = f.prevSibling
k.focusedFrame = f.nextSibling.firstDescendent()
}
parent := f.parent
if parent.firstChild == f {
parent.firstChild = f.nextSibling
}
if parent.lastChild == f {
parent.lastChild = f.prevSibling
}
if f.parent.numChildren() == 1 {
// Hoist the sibling frame into the parent frame.
sibling := parent.firstChild
parent.firstChild = sibling.firstChild
parent.lastChild = sibling.lastChild
for c := parent.firstChild; c != nil; c = c.nextSibling {
c.parent = parent
}
parent.orientation = sibling.orientation
if w := sibling.window; w != nil {
parent.window, w.frame = w, parent
}
if k.focusedFrame == sibling {
k.focusedFrame = parent.firstDescendent()
}
*f, *sibling = frame{}, frame{}
}
parent.layout()
finishMergeSplit(k)
return true
}
func doSplit(k *workspace, o1 interface{}) bool {
o, ok := o1.(orientation)
if !ok {
return false
}
if k.fullscreen || k.listing == listWorkspaces {
return false
}
k.focusedFrame.split(o)
finishMergeSplit(k)
return true
}
func finishMergeSplit(k *workspace) {
p, err := xp.QueryPointer(xConn, rootXWin).Reply()
if err != nil {
log.Println(err)
}
if p == nil {
k.focusFrame(k.mainFrame.firstDescendent())
} else {
k.focusFrame(k.frameContaining(p.RootX, p.RootY))
}
k.screen.repaint()
makeLists()
}
func doProgramAction(k *workspace, pa1 interface{}) bool {
pa, ok := pa1.(programAction)
if !ok {
return false
}
w := k.focusedFrame.window
if w == nil {
return false
}
class := w.property(atomWMClass)
if i := strings.Index(class, "\x00"); i >= 0 {
class = class[:i]
}
a := programActions[class][pa]
if a.keysym == 0 {
return false
}
sendSynthetic(w, a.state, a.keysym)
return true
}
func doSynthetic(k *workspace, buttonOrKeysym interface{}) bool {
if w := k.focusedFrame.window; w != nil {
sendSynthetic(w, keyState, buttonOrKeysym)
}
return false
}
func sendSynthetic(w *window, state uint16, buttonOrKeysym interface{}) {
// {Button,Key}{Press,Release}Event types all have the same wire format,
// except for the first byte (the X11 message type).
e := xp.KeyPressEvent{
Time: eventTime,
Root: rootXWin,
Event: w.xWin,
Child: w.xWin,
RootX: keyRootX,
RootY: keyRootY,
EventX: keyRootX - w.rect.X,
EventY: keyRootY - w.rect.Y,
State: state,
SameScreen: true,
}
var msg0, msg1 byte
switch bk := buttonOrKeysym.(type) {
case xp.Button:
msg0 = xp.ButtonPress
msg1 = xp.ButtonRelease
e.Detail = xp.Keycode(bk)
case xp.Keysym:
msg0 = xp.KeyPress
msg1 = xp.KeyRelease
keycode, shift := findKeycode(bk)
if keycode == 0 {
return
}
e.Detail = keycode
if shift {
e.State |= xp.KeyButMaskShift
}
default:
return
}
b := e.Bytes()
b[0] = msg0
check(xp.SendEventChecked(xConn, false, w.xWin, xp.EventMaskNoEvent, string(b)))
b[0] = msg1
check(xp.SendEventChecked(xConn, false, w.xWin, xp.EventMaskNoEvent, string(b)))
}
var (
quitTimes [2]time.Time
quitIndex int
quitting bool
)
func doQuit(_ *workspace, _ interface{}) bool {
if quitting {
return false
}
now := time.Now()
since := now.Sub(quitTimes[quitIndex])
quitTimes[quitIndex] = now
quitIndex = (quitIndex + 1) % len(quitTimes)
if since > 5*time.Second {
return true
}
quitting = true
waiting := false
for k := dummyWorkspace.link[next]; k != &dummyWorkspace; k = k.link[next] {
for w := k.dummyWindow.link[next]; w != &k.dummyWindow; w = w.link[next] {
if w.wmDeleteWindow {
waiting = true
sendClientMessage(w.xWin, atomWMDeleteWindow)
}
}
}
if waiting {
go func() {
time.Sleep(quitDuration)
os.Exit(0)
}()
} else {
os.Exit(0)
}
return true
}
var previousFocusXWin xp.Window
func focus(w *window) {
xWin := desktopXWin
if w != nil {
xWin = w.xWin
}
if previousFocusXWin == xWin {
return
}
previousFocusXWin = xWin
active := []byte{
byte(xWin >> 0),
byte(xWin >> 8),
byte(xWin >> 16),
byte(xWin >> 24),
}
if err := xp.ChangePropertyChecked(xConn, xp.PropModeReplace, rootXWin,
atomNetActiveWindow, atomWindow, 32, 1, active).Check(); err != nil {
log.Printf("could not set _NET_ACTIVE_WINDOW: %v", err)
}
if w != nil && w.wmTakeFocus {
sendClientMessage(xWin, atomWMTakeFocus)
} else {
check(xp.SetInputFocusChecked(xConn, xp.InputFocusParent, xWin, eventTime))
}
}