/
atoms.go
64 lines (57 loc) · 1.47 KB
/
atoms.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
package xutil
import (
"fmt"
"reflect"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
)
// Tags can be used with: `loadAtoms:"atomname"`.
// "st" should be a pointer to a struct with xproto.Atom fields.
// "onlyIfExists" asks the x server to assign a value only if the atom exists.
func LoadAtoms(conn *xgb.Conn, st interface{}, onlyIfExists bool) error {
// request atoms
// use reflection to get atoms names
typ := reflect.Indirect(reflect.ValueOf(st)).Type()
var cookies []xproto.InternAtomCookie
for i := 0; i < typ.NumField(); i++ {
sf := typ.Field(i)
name := sf.Name
tagStr := sf.Tag.Get("loadAtoms")
if tagStr != "" {
name = tagStr
}
// request value
cookie := xproto.InternAtom(conn, onlyIfExists, uint16(len(name)), name)
cookies = append(cookies, cookie)
}
// get atoms
val := reflect.Indirect(reflect.ValueOf(st))
for i := 0; i < val.NumField(); i++ {
reply, err := cookies[i].Reply() // get value
if err != nil {
return err
}
v := val.Field(i)
v.Set(reflect.ValueOf(reply.Atom))
}
return nil
}
//----------
func GetAtomName(conn *xgb.Conn, atom xproto.Atom) (string, error) {
cookie := xproto.GetAtomName(conn, atom)
r, err := cookie.Reply()
if err != nil {
return "", err
}
return r.Name, nil
}
func PrintAtomsNames(conn *xgb.Conn, atoms ...xproto.Atom) {
for _, a := range atoms {
name, err := GetAtomName(conn, a)
if err != nil {
fmt.Println(err)
continue
}
fmt.Printf("%d: %s\n", a, name)
}
}