/
atomic.go
350 lines (290 loc) · 8.22 KB
/
atomic.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
package atomic
import (
"fmt"
"sync"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
)
type Atomic interface {
Atom(string) (xproto.Atom, error)
AtomName(xproto.Atom) (string, error)
GetProp(xproto.Window, string) (*xproto.GetPropertyReply, error)
ChangeProp(xproto.Window, byte, string, string, []byte) error
ChangeProp32(xproto.Window, string, string, ...uint) error
Converter
AtomVals
WindowVals
NumVals
StringVals
}
type Converter interface {
AtomToUint(ids []xproto.Atom) []uint
StrToAtoms([]string) ([]uint, error)
WindowToInt([]xproto.Window) []uint
}
type AtomVals interface {
PropValAtom(*xproto.GetPropertyReply, error) (string, error)
PropValAtoms(*xproto.GetPropertyReply, error) ([]string, error)
}
type WindowVals interface {
PropValWindow(*xproto.GetPropertyReply, error) (xproto.Window, error)
PropValWindows(*xproto.GetPropertyReply, error) ([]xproto.Window, error)
}
type NumVals interface {
PropValNum(*xproto.GetPropertyReply, error) (uint, error)
PropValNums(*xproto.GetPropertyReply, error) ([]uint, error)
PropValNum64(*xproto.GetPropertyReply, error) (int64, error)
}
type StringVals interface {
PropValStr(*xproto.GetPropertyReply, error) (string, error)
PropValStrs(*xproto.GetPropertyReply, error) ([]string, error)
}
type atomic struct {
c *xgb.Conn
Atoms map[string]xproto.Atom
AtomsLck *sync.RWMutex
AtomNames map[xproto.Atom]string
AtomNamesLck *sync.RWMutex
}
func New(c *xgb.Conn) Atomic {
return &atomic{
c: c,
Atoms: make(map[string]xproto.Atom, 50),
AtomsLck: &sync.RWMutex{},
AtomNames: make(map[xproto.Atom]string, 50),
AtomNamesLck: &sync.RWMutex{},
}
}
var AtomIdError = Xrror("atom: '%s' returned an identifier of 0.").Out
func (a *atomic) Atom(name string) (xproto.Atom, error) {
aid, err := a.atom(name, false)
if err != nil {
return 0, err
}
if aid == 0 {
return 0, AtomIdError(name)
}
return aid, err
}
var AtomInternError = Xrror("Atom: Error interning atom '%s': %s").Out
func (a *atomic) atom(name string, onlyIfExists bool) (xproto.Atom, error) {
if aid, ok := a.atomGet(name); ok {
return aid, nil
}
reply, err := xproto.InternAtom(a.c, onlyIfExists, uint16(len(name)), name).Reply()
if err != nil {
return 0, AtomInternError(name, err)
}
a.cacheAtom(name, reply.Atom)
return reply.Atom, nil
}
var AtomNameError = Xrror("AtomName: Error fetching name for ATOM id '%d': %s").Out
func (a *atomic) AtomName(aid xproto.Atom) (string, error) {
if atomName, ok := a.atomNameGet(aid); ok {
return string(atomName), nil
}
reply, err := xproto.GetAtomName(a.c, aid).Reply()
if err != nil {
return "", AtomNameError(aid, err)
}
atomName := string(reply.Name)
a.cacheAtom(atomName, aid)
return atomName, nil
}
func (a *atomic) atomGet(name string) (xproto.Atom, bool) {
a.AtomsLck.RLock()
defer a.AtomsLck.RUnlock()
aid, ok := a.Atoms[name]
return aid, ok
}
func (a *atomic) atomNameGet(aid xproto.Atom) (string, bool) {
name, ok := a.AtomNames[aid]
a.AtomNamesLck.RLock()
defer a.AtomNamesLck.RUnlock()
return name, ok
}
func (a *atomic) cacheAtom(name string, aid xproto.Atom) {
a.AtomsLck.Lock()
a.AtomNamesLck.Lock()
defer a.AtomsLck.Unlock()
defer a.AtomNamesLck.Unlock()
a.Atoms[name] = aid
a.AtomNames[aid] = name
}
var GetPropertyError = Xrror("%s").Out
func (a *atomic) GetProp(w xproto.Window, s string) (*xproto.GetPropertyReply, error) {
atomId, err := a.Atom(s)
if err != nil {
return nil, err
}
reply, err := xproto.GetProperty(a.c, false, w, atomId, xproto.GetPropertyTypeAny, 0, (1<<32)-1).Reply()
if err != nil {
msg := fmt.Sprintf("Error retrieving property '%s' on window %x: %s", s, w, err)
return nil, GetPropertyError(msg)
}
if reply.Format == 0 {
msg := fmt.Sprintf("No such property '%s' on window %x", s, w)
return nil, GetPropertyError(msg)
}
return reply, nil
}
func (a *atomic) ChangeProp(w xproto.Window, format byte, prop, typ string, data []byte) error {
propAtom, err := a.Atom(prop)
if err != nil {
return err
}
typAtom, err := a.Atom(typ)
if err != nil {
return err
}
return xproto.ChangePropertyChecked(a.c, xproto.PropModeReplace, w, propAtom, typAtom, format, uint32(len(data)/(int(format)/8)), data).Check()
}
func (a *atomic) ChangeProp32(w xproto.Window, prop, typ string, data ...uint) error {
buf := make([]byte, len(data)*4)
for i, datum := range data {
xgb.Put32(buf[(i*4):], uint32(datum))
}
return a.ChangeProp(w, 32, prop, typ, buf)
}
func (a *atomic) WindowToInt(ids []xproto.Window) []uint {
ids32 := make([]uint, len(ids))
for i, v := range ids {
ids32[i] = uint(v)
}
return ids32
}
func (a *atomic) AtomToUint(ids []xproto.Atom) []uint {
ids32 := make([]uint, len(ids))
for i, v := range ids {
ids32[i] = uint(v)
}
return ids32
}
func (a *atomic) StrToAtoms(atomNames []string) ([]uint, error) {
var err error
atoms := make([]uint, len(atomNames))
for i, atomName := range atomNames {
a, err := a.Atom(atomName)
if err != nil {
return nil, err
}
atoms[i] = uint(a)
}
return atoms, err
}
func (a *atomic) PropValAtom(reply *xproto.GetPropertyReply, err error) (string, error) {
if err != nil {
return "", err
}
if reply.Format != 32 {
return "", fmt.Errorf("PropValAtom: Expected format 32 but got %d", reply.Format)
}
return a.AtomName(xproto.Atom(xgb.Get32(reply.Value)))
}
func (a *atomic) PropValAtoms(reply *xproto.GetPropertyReply, err error) ([]string, error) {
if err != nil {
return nil, err
}
if reply.Format != 32 {
return nil, fmt.Errorf("PropValAtoms: Expected format 32 but got %d", reply.Format)
}
ids := make([]string, reply.ValueLen)
vals := reply.Value
for i := 0; len(vals) >= 4; i++ {
ids[i], err = a.AtomName(xproto.Atom(xgb.Get32(vals)))
if err != nil {
return nil, err
}
vals = vals[4:]
}
return ids, nil
}
func (a *atomic) PropValWindow(reply *xproto.GetPropertyReply, err error) (xproto.Window, error) {
if err != nil {
return 0, err
}
if reply.Format != 32 {
return 0, fmt.Errorf("PropValId: Expected format 32 but got %d", reply.Format)
}
return xproto.Window(xgb.Get32(reply.Value)), nil
}
func (a *atomic) PropValWindows(reply *xproto.GetPropertyReply, err error) ([]xproto.Window, error) {
if err != nil {
return nil, err
}
if reply.Format != 32 {
return nil, fmt.Errorf("PropValIds: Expected format 32 but got %d", reply.Format)
}
ids := make([]xproto.Window, reply.ValueLen)
vals := reply.Value
for i := 0; len(vals) >= 4; i++ {
ids[i] = xproto.Window(xgb.Get32(vals))
vals = vals[4:]
}
return ids, nil
}
func (a *atomic) PropValNum(reply *xproto.GetPropertyReply, err error) (uint, error) {
if err != nil {
return 0, err
}
if reply.Format != 32 {
return 0, fmt.Errorf("PropValNum: Expected format 32 but got %d",
reply.Format)
}
return uint(xgb.Get32(reply.Value)), nil
}
func (a *atomic) PropValNums(reply *xproto.GetPropertyReply, err error) ([]uint, error) {
if err != nil {
return nil, err
}
if reply.Format != 32 {
return nil, fmt.Errorf("PropValIds: Expected format 32 but got %d",
reply.Format)
}
nums := make([]uint, reply.ValueLen)
vals := reply.Value
for i := 0; len(vals) >= 4; i++ {
nums[i] = uint(xgb.Get32(vals))
vals = vals[4:]
}
return nums, nil
}
func (a *atomic) PropValNum64(reply *xproto.GetPropertyReply, err error) (int64, error) {
if err != nil {
return 0, err
}
if reply.Format != 32 {
return 0, fmt.Errorf("PropValNum: Expected format 32 but got %d",
reply.Format)
}
return int64(xgb.Get32(reply.Value)), nil
}
func (a *atomic) PropValStr(reply *xproto.GetPropertyReply, err error) (string, error) {
if err != nil {
return "", err
}
if reply.Format != 8 {
return "", fmt.Errorf("PropValStr: Expected format 8 but got %d", reply.Format)
}
return string(reply.Value), nil
}
func (a *atomic) PropValStrs(reply *xproto.GetPropertyReply, err error) ([]string, error) {
if err != nil {
return nil, err
}
if reply.Format != 8 {
return nil, fmt.Errorf("PropValStrs: Expected format 8 but got %d", reply.Format)
}
var strs []string
sstart := 0
for i, c := range reply.Value {
if c == 0 {
strs = append(strs, string(reply.Value[sstart:i]))
sstart = i + 1
}
}
if sstart < int(reply.ValueLen) {
strs = append(strs, string(reply.Value[sstart:]))
}
return strs, nil
}