-
Notifications
You must be signed in to change notification settings - Fork 0
/
i3.go
227 lines (169 loc) · 5.27 KB
/
i3.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
package main
import (
"log"
"reflect"
"regexp"
"go.i3wm.org/i3"
)
type WinList struct {
Instance Collection
Class Collection
}
// WL contains collections of all windows currently running, collection of their classes and collection of their
// instances.
var WL WinList
// UpdateI3WinList subsribes to all window-related events and send them to I3EventParser() for more detailed parsing.
func UpdateI3WinList() {
// TODO: Здесь получить начальный список окон, управляемых i3wm (через GetTree()) и сложить их атрибутику в переменную WL
Tree, err := i3.GetTree()
if err != nil {
log.Printf("Unable to get list of windows: %s", err)
}
ExtractProps(Tree.Root)
i3wm := i3.Subscribe(i3.WindowEventType)
for i3wm.Next() {
e := i3wm.Event().(*i3.WindowEvent)
go I3EventParser(e)
}
log.Fatal(i3wm.Close())
}
// I3EventParser parses i3wm events and updates WL (aka Windows List).
func I3EventParser(e *i3.WindowEvent) {
switch e.Change {
case "new":
var (
count uint64
)
countI, exist := WL.Class.Get(e.Container.WindowProperties.Class)
if exist {
count = interfaceToUint64(countI)
} else {
count = 0
}
count++
WL.Class.Set(e.Container.WindowProperties.Class, count)
countI, exist = WL.Instance.Get(e.Container.WindowProperties.Instance)
if exist {
count = interfaceToUint64(countI)
} else {
count = 0
}
count++
WL.Instance.Set(e.Container.WindowProperties.Instance, count)
UpdateReady <- true
case "close":
countI, exist := WL.Class.Get(e.Container.WindowProperties.Class)
if exist {
count := interfaceToUint64(countI)
if count > 1 {
count--
WL.Class.Set(e.Container.WindowProperties.Class, count)
} else {
WL.Class.Delete(e.Container.WindowProperties.Class)
}
}
countI, exist = WL.Instance.Get(e.Container.WindowProperties.Instance)
if exist {
count := interfaceToUint64(countI)
if count > 1 {
count--
WL.Instance.Set(e.Container.WindowProperties.Instance, count)
} else {
WL.Instance.Delete(e.Container.WindowProperties.Instance)
}
}
UpdateReady <- true
}
}
// interfaceToUint превращает данный интерфейс в Uint64.
// Если может, конечно :) .
func interfaceToUint64(iface interface{}) uint64 {
// А теперь мы начинаем дурдом, нам надо превратить ёбанный interface{} в []string
// Поскольку interface{} может быть чем угодно, перестрахуемся
if reflect.TypeOf(iface).Kind() == reflect.Uint64 {
return reflect.ValueOf(iface).Uint()
}
return 0
}
// HasWindows returns true if given Window Class and/or Instance has more than 0 windows according our observations.
func HasWindows(className string, instanceName string) bool {
var (
ret bool
myclass bool
myinstance bool
)
switch {
case className != "" && instanceName != "":
// Since we agree that we use regexp for searching window class and instance, but not yet decide what to do
// with exact match, comment it out and leave it here.
/*
_, myclass = WL.Class.Get(className)
_, myinstance = WL.Instance.Get(instanceName)
*/
myclass = FindWindowClass(className)
myinstance = FindWindowInstance(instanceName)
ret = myclass && myinstance
case className != "":
// _, ret = WL.Class.Get(className)
ret = FindWindowClass(className)
case instanceName != "":
// _, ret = WL.Instance.Get(instanceName)
ret = FindWindowInstance(instanceName)
}
return ret
}
// FindWindowInstance returns true if given regex matces at least one pattern in WL.Class collection.
// Otherwise, returns false.
func FindWindowInstance(regexpStr string) bool {
var found = false
f := func(key interface{}, value interface{}) bool {
keyStr := reflect.ValueOf(key).String()
re, err := regexp.Compile(regexpStr) //nolint:nolintlint, gocritic, we do not want shit our pants and panic on
// erroneous regexp, we want log it to stderr, that it.
if err != nil {
log.Printf("Unable to perform regexp match: %s", err)
return true
}
if re.Match([]byte(keyStr)) { //nolint:mirror
found = true
return false
}
return true
}
WL.Instance.Range(f)
return found
}
// FindWindowClass returns true if given regex matces at least one pattern in WL.Class collection.
// Otherwise, returns false.
func FindWindowClass(regexpStr string) bool {
var found = false
f := func(key interface{}, value interface{}) bool {
keyStr := reflect.ValueOf(key).String()
re, err := regexp.Compile(regexpStr) //nolint:nolintlint, gocritic, we do not want shit our pants and panic on
// erroneous regexp, we want log it to stderr, that it.
if err != nil {
log.Printf("Unable to perform regexp match: %s", err)
return true
}
if re.Match([]byte(keyStr)) { //nolint:mirror
found = true
return false
}
return true
}
WL.Class.Range(f)
return found
}
// ExtractProps fills WL stricture collections with instance name and class name of given node (x11, that is).
func ExtractProps(n *i3.Node) {
var e i3.WindowEvent
e.Change = "new"
e.Container = *n
I3EventParser(&e)
for _, node := range n.Nodes {
ExtractProps(node)
}
for _, node := range n.FloatingNodes {
ExtractProps(node)
}
}