/
chain.go
332 lines (295 loc) · 5.33 KB
/
chain.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
package main
import (
"bytes"
"time"
"github.com/BurntSushi/xgb/xproto"
)
func Configure(h Handlr, chains []Chain) error {
var err error
for _, c := range chains {
err = c.Attach(h, h.Root())
}
if err != nil {
return err
}
return nil
}
type Chain interface {
Detailable
Commandable
Activitable
Keyable
Chainable
}
type Detailable interface {
Mechanic() int
Raw() string
String() string
Chained() string
}
type Activitable interface {
Clear()
Active() (int64, bool)
Activated() bool
}
type Keyable interface {
Attach(Handlr, xproto.Window) error
Run(Handlr, string) error
}
type Chainable interface {
Head() Chain
Tail() Chain
SetPrev(Chain)
Prev() Chain
SetNext(Chain)
Next() Chain
Append(Chain)
}
type Commandable interface {
AddCommand(*Cmd)
Command() *Cmd
}
const (
NoMechanic = 1
KeyPress = xproto.KeyPress
KeyRelease = xproto.KeyRelease
ButtonPress = xproto.ButtonPress
ButtonRelease = xproto.ButtonRelease
)
type chain struct {
mechanic int
expires int64
prev Chain
next Chain
khord string
chord string
command *Cmd
}
func (c *chain) expired() bool {
if time.Now().Unix() < c.expires {
return false
}
return true
}
func (c *chain) touch() {
c.expires = time.Now().Add(time.Duration(provided.ChainExpiry) * time.Second).Unix()
}
func clear(c Chain) {
head := c.Head()
head.Clear()
n := head.Next()
for n != nil {
n.Clear()
n = n.Next()
}
}
func (c *chain) Clear() {
c.expires = 0
}
func (c *chain) Active() (int64, bool) {
if !c.expired() {
return c.expires, true
}
return c.expires, false
}
func (c *chain) Activated() bool {
head := c.Head()
if last, atv := head.Active(); atv {
for head.Next() != nil {
head = head.Next()
curr, atv := head.Active()
if !atv {
return false
}
if curr < last {
return false
}
last = curr
}
return true
}
return false
}
func (c *chain) Head() Chain {
var head Chain
head = c
for head.Prev() != nil {
head = head.Prev()
}
return head
}
func (c *chain) Tail() Chain {
var tail Chain
tail = c
for tail.Next() != nil {
tail = tail.Next()
}
return tail
}
func (c *chain) SetPrev(o Chain) {
c.prev = o
}
func (c *chain) Prev() Chain {
return c.prev
}
func (c *chain) SetNext(o Chain) {
o.SetPrev(c)
c.next = o
}
func (c *chain) Next() Chain {
return c.next
}
func (c *chain) Append(o Chain) {
c.Tail().SetNext(o)
}
func (c *chain) Command() *Cmd {
return c.command
}
func (c *chain) AddCommand(cmd *Cmd) {
c.command = cmd
}
func attach(c Chain, h Handlr, w xproto.Window) error {
switch c.Mechanic() {
case KeyPress, KeyRelease:
mods, keycodes, err := ParseKeyInput(h.Keyboard(), c.String())
if err != nil {
return err
}
for _, kc := range keycodes {
GrabKeyChecked(h.Conn(), w, mods, kc)
ky := mkInput(c.Mechanic(), w, mods, byte(kc), 0)
h.Put(ky, c)
}
case ButtonPress, ButtonRelease:
mods, button, err := ParseMouseInput(c.String())
if err != nil {
return err
}
MouseGrabChecked(h.Conn(), w, mods, button)
ky := mkInput(c.Mechanic(), w, mods, 0, byte(button))
h.Put(ky, c)
}
return nil
}
func (c *chain) Attach(h Handlr, w xproto.Window) error {
var err error
err = attach(c, h, w)
if err != nil {
return err
}
if c.Next() != nil {
n := c.Next()
for n != nil {
err = attach(n, h, w)
if err != nil {
return err
}
n = n.Next()
}
}
return nil
}
func (c *chain) Run(h Handlr, param string) error {
c.touch()
if c.Activated() {
cmd := c.Command()
return cmd.Exec(param).Run()
}
return nil
}
func (c *chain) Mechanic() int {
return c.mechanic
}
func (c *chain) Raw() string {
return c.khord
}
func (c *chain) String() string {
return c.chord
}
func (c *chain) Chained() string {
cmdstring := func(c Chain, b *bytes.Buffer) {
if cmd := c.Command(); cmd != nil {
b.Write(cmd.Bytes())
}
}
ret := new(bytes.Buffer)
ret.WriteString(c.khord)
cmdstring(c, ret)
n := c.Next()
for n != nil {
ret.WriteString("; ")
ret.WriteString(n.String())
cmdstring(n, ret)
n = n.Next()
}
return ret.String()
}
func newChain(in []byte) *chain {
chord, khord, mechanic := chordMechanic(in)
return &chain{
mechanic: mechanic,
khord: khord,
chord: chord,
}
}
func chordMechanic(in []byte) (string, string, int) {
var chord, khord string
var mec int
var isRelease, isButton bool
if spl := bytes.Split(in, []byte("+")); spl[len(spl)-1][0] == RELEASE {
isRelease = true
}
if bytes.Contains(in, []byte("button")) {
isButton = true
}
cut := func(r rune) rune {
if r == '@' {
return -1
}
return r
}
switch {
case !isRelease && !isButton:
s := string(in)
chord = s
khord = s
mec = KeyPress
case isRelease && !isButton:
chord = string(bytes.Map(cut, in))
khord = string(in)
mec = KeyRelease
case !isRelease && isButton:
s := string(in)
chord = s
khord = s
mec = ButtonPress
case isRelease && isButton:
chord = string(bytes.Map(cut, in))
khord = string(in)
mec = ButtonRelease
default:
s := string(in)
chord = s
khord = s
mec = KeyPress
}
return chord, khord, mec
}
func mkChain(in [][]byte) Chain {
head := newChain(in[0])
curr := head
for _, v := range in[1:] {
n := newChain(v)
n.prev = curr
curr.next = n
curr = n
}
return head
}
func mkChains(in [][][]byte) []Chain {
var ret []Chain
for _, v := range in {
ret = append(ret, mkChain(v))
}
return ret
}