/
i3qws.go
178 lines (163 loc) · 3.8 KB
/
i3qws.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
package i3qws
import (
"container/list"
"context"
"fmt"
"strconv"
"sync"
"github.com/sirupsen/logrus"
"go.i3wm.org/i3/v4"
)
type (
// I3qws is a synchronized window list with additional exported methods
I3qws struct {
m sync.RWMutex
l *list.List
markFormat string
lastMark int
}
)
// DoSpy starts spy for i3wm windows events
func DoSpy(ctx context.Context, markFormat string) *I3qws {
i3qws := &I3qws{
l: list.New(),
markFormat: markFormat,
}
recv := i3.Subscribe(i3.WindowEventType, i3.ShutdownEventType)
go func() {
<-ctx.Done()
if e := recv.Close(); e != nil {
logrus.WithError(e).Warn("Closing i3 communication channel with an error")
}
}()
go func() {
for recv.Next() {
i := recv.Event()
switch ev := i.(type) {
case *i3.WindowEvent:
i3qws.onWindowEvent(ev)
case *i3.ShutdownEvent:
i3qws.onShutdownEvent(ev)
}
}
}()
return i3qws
}
func (q *I3qws) onWindowEvent(ev *i3.WindowEvent) {
q.m.Lock()
le := logrus.WithFields(logrus.Fields{
"id": ev.Container.ID,
"name": ev.Container.Name,
"type": ev.Container.Type,
})
le.Debugf("Window change: %s", ev.Change)
shouldRemark := false
switch ev.Change {
case "focus":
found := false
for c := q.l.Front(); c != nil; c = c.Next() {
n := c.Value.(*i3.Node)
if n.ID == ev.Container.ID {
q.l.MoveToFront(c)
found = true
break
}
}
if !found {
q.l.PushFront(&ev.Container)
}
shouldRemark = true
case "title", "mark":
found := false
for c := q.l.Front(); c != nil; c = c.Next() {
n := c.Value.(*i3.Node)
if n.ID == ev.Container.ID {
c.Value = &ev.Container
found = true
break
}
}
if !found {
le.Debug("Container for update was not found")
}
shouldRemark = false
case "close":
for c := q.l.Front(); c != nil; c = c.Next() {
n := c.Value.(*i3.Node)
if n.ID == ev.Container.ID {
q.l.Remove(c)
break
}
}
shouldRemark = true
}
q.m.Unlock()
if len(q.markFormat) > 0 && shouldRemark {
q.remark()
}
}
func (q *I3qws) onShutdownEvent(ev *i3.ShutdownEvent) {
q.m.Lock()
logrus.Warnf("Shutdown change: %s", ev.Change)
q.l.Init()
q.m.Unlock()
}
func (q *I3qws) remark() {
q.m.RLock()
defer q.m.RUnlock()
rl := q.lastMark
i := 0
for c := q.l.Front(); c != nil; c = c.Next() {
n := c.Value.(*i3.Node)
mark := fmt.Sprintf(q.markFormat, i)
q.runCommand("[con_id=" + strconv.FormatInt(int64(n.ID), 10) + "] mark --add " + mark) // nolint: errcheck
i++
}
q.lastMark = i - 1
for ; i <= rl; i++ {
mark := fmt.Sprintf(q.markFormat, i)
q.runCommand("unmark " + mark) // nolint: errcheck
}
}
// DumpList dumps windows list
func (q *I3qws) DumpList() []*i3.Node {
nn := make([]*i3.Node, 0, q.l.Len())
q.m.RLock()
defer q.m.RUnlock()
for c := q.l.Front(); c != nil; c = c.Next() {
nn = append(nn, c.Value.(*i3.Node))
}
return nn
}
// Focus will bring to front window with number `num`.
// `num` starts from 0 — the first window, focused, 1 — the second one, last time focused and so on
// if `num` is negative, it should count from the end
func (q *I3qws) Focus(num int) (*i3.Node, error) {
q.m.RLock()
defer q.m.RUnlock()
if num < 0 && q.l.Len() > 0 {
num = q.l.Len() + num
if num < 0 {
num = 0
}
}
i := 0
for c := q.l.Front(); c != nil; c = c.Next() {
if i == num {
n := c.Value.(*i3.Node)
_, err := q.runCommand("[con_id=" + strconv.FormatInt(int64(n.ID), 10) + "] focus")
return n, err
}
i++
}
return nil, nil
}
func (q *I3qws) runCommand(command string) ([]i3.CommandResult, error) {
res, err := i3.RunCommand(command)
if err != nil {
logrus.WithField("i3Command", command).WithError(err).Error("Error while running command on i3wm")
} else {
logrus.WithField("i3Command", command).WithField("res", res).Trace("Successfully ran command on i3wm")
}
return res, err
}