-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
xorg.go
193 lines (168 loc) · 3.9 KB
/
xorg.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
// Gone Time Tracker -or- Where has my time gone?
package main
import (
"bytes"
"errors"
"log"
"time"
"github.com/jezek/xgb"
"github.com/jezek/xgb/screensaver"
"github.com/jezek/xgb/xproto"
)
type Xorg struct {
conn *xgb.Conn
root xproto.Window
activeAtom *xproto.InternAtomReply
netNameAtom *xproto.InternAtomReply
nameAtom *xproto.InternAtomReply
classAtom *xproto.InternAtomReply
}
type Window struct {
Class string
Name string
}
type Tracker interface {
Update(Window)
Snooze(time.Duration)
Wakeup()
}
var (
ErrNoValue = errors.New("empty value")
ErrNoClass = errors.New("empty class")
)
func (x Xorg) atom(aname string) *xproto.InternAtomReply {
a, err := xproto.InternAtom(x.conn, true, uint16(len(aname)), aname).Reply()
if err != nil {
log.Fatal("atom: ", err)
}
return a
}
func (x Xorg) property(w xproto.Window, a *xproto.InternAtomReply) (*xproto.GetPropertyReply, error) {
return xproto.GetProperty(x.conn, false, w, a.Atom, xproto.GetPropertyTypeAny, 0, (1<<32)-1).Reply()
}
func (x Xorg) active() xproto.Window {
p, err := x.property(x.root, x.activeAtom)
if err != nil {
return x.root
}
return xproto.Window(xgb.Get32(p.Value))
}
func (x Xorg) name(w xproto.Window) (string, error) {
name, err := x.property(w, x.netNameAtom)
if err != nil {
return "", err
}
if string(name.Value) == "" {
name, err = x.property(w, x.nameAtom)
if err != nil {
return "", err
}
if string(name.Value) == "" {
return "", ErrNoValue
}
}
return string(name.Value), nil
}
func (x Xorg) class(w xproto.Window) (string, error) {
class, err := x.property(w, x.classAtom)
if err != nil {
return "", err
}
zero := []byte{0}
s := bytes.Split(bytes.TrimSuffix(class.Value, zero), zero)
if l := len(s); l > 0 && len(s[l-1]) != 0 {
return string(s[l-1]), nil
}
return "", ErrNoClass
}
func (x Xorg) window() (Window, bool) {
id := x.active()
/* skip invalid window id */
if id == 0 {
return Window{}, false
}
class, err := x.class(id)
if err != nil {
return Window{}, false
}
name, err := x.name(id)
if err != nil {
return Window{}, false
}
x.spy(id)
return Window{Class: class, Name: name}, true
}
func (x Xorg) spy(w xproto.Window) {
xproto.ChangeWindowAttributes(x.conn, w, xproto.CwEventMask,
[]uint32{xproto.EventMaskPropertyChange})
}
func (x Xorg) Close() {
x.conn.Close()
}
func Connect(display string) Xorg {
var x Xorg
var err error
x.conn, err = xgb.NewConnDisplay(display)
if err != nil {
log.Fatal("xgb: ", err)
}
err = screensaver.Init(x.conn)
if err != nil {
log.Fatal("screensaver: ", err)
}
setup := xproto.Setup(x.conn)
x.root = setup.DefaultScreen(x.conn).Root
drw := xproto.Drawable(x.root)
screensaver.SelectInput(x.conn, drw, screensaver.EventNotifyMask)
x.activeAtom = x.atom("_NET_ACTIVE_WINDOW")
x.netNameAtom = x.atom("_NET_WM_NAME")
x.nameAtom = x.atom("WM_NAME")
x.classAtom = x.atom("WM_CLASS")
x.spy(x.root)
return x
}
func (x Xorg) waitForEvent(events chan<- xgb.Event) {
for {
ev, err := x.conn.WaitForEvent()
if err != nil {
log.Println("wait for event:", err)
}
events <- ev
}
}
func (x Xorg) queryIdle() time.Duration {
info, err := screensaver.QueryInfo(x.conn, xproto.Drawable(x.root)).Reply()
if err != nil {
log.Println("query idle:", err)
return 0
}
return time.Duration(info.MsSinceUserInput) * time.Millisecond
}
func (x Xorg) Collect(t Tracker, timeout time.Duration) {
if win, ok := x.window(); ok {
t.Update(win)
}
events := make(chan xgb.Event, 1)
go x.waitForEvent(events)
for {
select {
case event := <-events:
switch e := event.(type) {
case xproto.PropertyNotifyEvent:
if win, ok := x.window(); ok {
t.Wakeup()
t.Update(win)
}
case screensaver.NotifyEvent:
switch e.State {
case screensaver.StateOn:
t.Snooze(x.queryIdle())
default:
t.Wakeup()
}
}
case <-time.After(timeout):
t.Snooze(x.queryIdle())
}
}
}