-
Notifications
You must be signed in to change notification settings - Fork 18
/
tracker.go
140 lines (117 loc) · 3.17 KB
/
tracker.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
package main
import (
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/ewmh"
"github.com/BurntSushi/xgbutil/xevent"
"github.com/BurntSushi/xgbutil/xprop"
"github.com/blrsn/zentile/state"
)
type tracker struct {
clients map[xproto.Window]Client // List of clients that are being tracked.
workspaces map[uint]*Workspace
}
func initTracker(ws map[uint]*Workspace) *tracker {
t := tracker{
clients: make(map[xproto.Window]Client),
workspaces: ws,
}
xevent.PropertyNotifyFun(t.handleClientUpdates).Connect(state.X, state.X.RootWin())
t.populateClients()
return &t
}
// UpdateClients updates the list of tracked clients with the most up to date list of clients.
func (tr *tracker) populateClients() {
clientList, _ := ewmh.ClientListStackingGet(state.X)
for _, w := range clientList {
if isHidden(w) || shouldIgnore(w) {
continue
}
tr.trackWindow(w)
}
// If window is tracked, but not in client list
for wid := range tr.clients {
found := false
for _, w := range clientList {
if w == wid {
found = true
break
}
}
if !found {
tr.unTrack(wid)
}
}
}
func (tr *tracker) IsTracked(w xproto.Window) bool {
_, ok := tr.clients[w]
return ok
}
// Adds window to Tracked Clients and layouts.
func (tr *tracker) trackWindow(w xproto.Window) {
if tr.IsTracked(w) {
return
}
c := newClient(w)
if c.Desk > state.DeskCount {
return
}
tr.attachHandlers(&c)
tr.clients[c.window.Id] = c
ws := tr.workspaces[c.Desk]
ws.AddClient(c)
}
func (tr *tracker) unTrack(w xproto.Window) {
c, ok := tr.clients[w]
if ok {
ws := tr.workspaces[c.Desk]
ws.RemoveClient(c)
xevent.Detach(state.X, w)
delete(tr.clients, w)
}
}
func (tr *tracker) handleClientUpdates(X *xgbutil.XUtil, ev xevent.PropertyNotifyEvent) {
if aname, _ := xprop.AtomName(state.X, ev.Atom); aname != "_NET_CLIENT_LIST_STACKING" {
return
}
tr.populateClients()
tr.workspaces[state.CurrentDesk].Tile()
}
func (tr *tracker) handleMinimizedClient(c *Client) {
states, _ := ewmh.WmStateGet(state.X, c.window.Id)
for _, state := range states {
if state == "_NET_WM_STATE_HIDDEN" {
tr.workspaces[c.Desk].RemoveClient(*c)
tr.unTrack(c.window.Id)
tr.workspaces[c.Desk].Tile()
}
}
}
func (tr *tracker) handleDesktopChange(c *Client) {
newDesk, _ := ewmh.WmDesktopGet(state.X, c.window.Id)
oldDesk := c.Desk
tr.workspaces[oldDesk].RemoveClient(*c)
tr.workspaces[newDesk].AddClient(*c)
c.Desk = newDesk
if tr.workspaces[oldDesk].IsTiling {
tr.workspaces[oldDesk].Tile()
}
if tr.workspaces[newDesk].IsTiling {
tr.workspaces[newDesk].Tile()
} else {
c.Restore()
}
}
func (tr *tracker) attachHandlers(c *Client) {
c.window.Listen(xproto.EventMaskPropertyChange)
xevent.PropertyNotifyFun(func(x *xgbutil.XUtil, ev xevent.PropertyNotifyEvent) {
if aname, _ := xprop.AtomName(state.X, ev.Atom); aname == "_NET_WM_STATE" {
tr.handleMinimizedClient(c)
}
}).Connect(state.X, c.window.Id)
xevent.PropertyNotifyFun(func(x *xgbutil.XUtil, ev xevent.PropertyNotifyEvent) {
if aname, _ := xprop.AtomName(state.X, ev.Atom); aname == "_NET_WM_DESKTOP" {
tr.handleDesktopChange(c)
}
}).Connect(state.X, c.window.Id)
}