-
Notifications
You must be signed in to change notification settings - Fork 16
/
draw.go
293 lines (272 loc) · 5.48 KB
/
draw.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
package main
import (
"bytes"
"fmt"
"os/exec"
"sync"
"time"
xp "github.com/BurntSushi/xgb/xproto"
)
var desktopColor uint32
func setForeground(c uint32) {
if desktopColor == c {
return
}
desktopColor = c
check(xp.ChangeGCChecked(xConn, desktopXGC, xp.GcForeground, []uint32{c}))
}
func drawText(x, y int16, text string) {
check(xp.ImageText8Checked(xConn, uint8(len(text)),
xp.Drawable(desktopXWin), desktopXGC, x, y, text))
}
func clip(k *workspace) (int16, int16) {
r := k.focusedFrame.rect
if k.fullscreen || k.listing == listWorkspaces {
r = k.mainFrame.rect
}
r.X, r.Y, r.Width, r.Height = r.X+2, r.Y+2, r.Width-3, r.Height-3
check(xp.SetClipRectanglesChecked(
xConn, xp.ClipOrderingUnsorted, desktopXGC, 0, 0, []xp.Rectangle{r}))
return r.X, r.Y
}
func unclip() {
r := xp.Rectangle{X: 0, Y: 0, Width: desktopWidth, Height: desktopHeight}
check(xp.SetClipRectanglesChecked(
xConn, xp.ClipOrderingUnsorted, desktopXGC, 0, 0, []xp.Rectangle{r}))
}
func handleExpose(e xp.ExposeEvent) {
if e.Count != 0 {
return
}
for _, s := range screens {
k := s.workspace
k.drawFrameBorders()
if k.listing == listNone {
continue
}
x, y := clip(k)
y += int16(fontHeight1)
setForeground(colorPulseUnfocused)
info := time.Now().Format("2006-01-02 15:04 Monday")
if showBatteryPercentage {
info = fmt.Sprintf("Bat: %4s %s", batteryPercentage(), info)
}
drawText(x, y, info)
y += int16(fontHeight)
if k.listing == listWindows {
setForeground(colorPulseFocused)
}
wNum := 0
for i, item := range k.list {
if iw, ok := item.(*window); ok {
c0, c1 := ' ', ' '
if k.listing == listWindows {
if iw.frame == k.focusedFrame {
c0 = '+'
} else if iw.frame != nil {
c0 = '-'
} else if !iw.seen {
c0 = '@'
}
}
if iw.selected {
c1 = '#'
}
drawText(x+int16(3*fontWidth), y+int16(i*fontHeight),
fmt.Sprintf("%c%c %c %s", c0, c1, windowNames[wNum], iw.name))
if wNum < len(windowNames)-1 {
wNum++
}
} else {
wNum = 0
}
}
if k.listing == listWorkspaces {
setForeground(colorPulseFocused)
kNum := 0
for i, item := range k.list {
if ik, ok := item.(*workspace); ok {
c := ' '
if ik.screen == s {
c = '+'
} else if ik.screen != nil {
c = '-'
}
drawText(x+int16(3*fontWidth), y+int16(i*fontHeight),
fmt.Sprintf("%c %s", c, workspaceNames[kNum]))
if kNum < len(workspaceNames)-1 {
kNum++
}
}
}
}
if k.index >= 0 {
drawText(x+int16(fontWidth), y+int16(k.index*fontHeight), ">")
}
unclip()
}
}
var percentage = []byte("percentage:")
func batteryPercentage() string {
b, err := exec.Command("/usr/bin/upower", "--show-info", "/org/freedesktop/UPower/devices/battery_BAT0").Output()
if err != nil {
return "???"
}
if i := bytes.Index(b, percentage); i >= 0 {
b = b[i+len(percentage):]
} else {
return "???"
}
if i := bytes.IndexByte(b, '%'); i >= 0 {
b = b[:i+1]
} else {
return "???"
}
return string(bytes.TrimSpace(b))
}
var (
pulseTimeLock sync.Mutex
pulseTime time.Time
)
var (
pulseChan = make(chan time.Time)
pulseDoneChan = make(chan struct{})
colorUnfocused uint32 = colorBaseUnfocused
colorFocused uint32 = colorBaseFocused
)
func init() {
go runPulses()
}
func runPulses() {
tChan := (<-chan time.Time)(nil)
fChan := (chan func())(nil)
for {
select {
case when := <-pulseChan:
pulseTimeLock.Lock()
pulseTime = when
pulseTimeLock.Unlock()
if tChan == nil {
fChan = proactiveChan
}
case <-pulseDoneChan:
if tChan == nil {
tChan = time.After(pulseFrameDuration)
}
case <-tChan:
tChan = nil
fChan = proactiveChan
case fChan <- pulse:
fChan = nil
}
}
}
func pulse() {
pulseTimeLock.Lock()
t := pulseTime
pulseTimeLock.Unlock()
i := int(time.Since(t) * time.Duration(len(cos)) / pulseTotalDuration)
if i < 0 {
i = 0
}
anyUnseenWindows := findWindow(func(w *window) bool { return !w.seen }) != nil
if !anyUnseenWindows && i > len(cos)/2 {
i = len(cos) / 2
}
if quitting {
colorFocused = colorQuitFocused
colorUnfocused = colorQuitUnfocused
} else {
colorFocused = blend(colorPulseFocused, colorBaseFocused, uint32(i))
colorUnfocused = blend(colorPulseUnfocused, colorBaseUnfocused, uint32(i))
}
for _, s := range screens {
s.workspace.drawFrameBorders()
}
if i < len(cos)/2 || anyUnseenWindows {
pulseDoneChan <- struct{}{}
}
}
func blend(c0, c1, i uint32) uint32 {
x := uint32(cos[i%uint32(len(cos))])
y := 256 - x
r0 := (c0 >> 16) & 0xff
g0 := (c0 >> 8) & 0xff
b0 := (c0 >> 0) & 0xff
r1 := (c1 >> 16) & 0xff
g1 := (c1 >> 8) & 0xff
b1 := (c1 >> 0) & 0xff
r2 := ((r0 * x) + (r1 * y)) / 256
g2 := ((g0 * x) + (g1 * y)) / 256
b2 := ((b0 * x) + (b1 * y)) / 256
return r2<<16 | g2<<8 | b2
}
// cos was generated by:
//
// const N = 32
// for i := 0; i < N; i++ {
// c := math.Cos(float64(i) * 2 * math.Pi / N)
// fmt.Printf("%v,\n", int(0.5+(c+1)*256/2))
// }
var cos = [32]uint16{
256,
254,
246,
234,
219,
199,
177,
153,
128,
103,
79,
57,
37,
22,
10,
2,
0,
2,
10,
22,
37,
57,
79,
103,
128,
153,
177,
199,
219,
234,
246,
254,
}
var windowNames = [...]byte{
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
':',
}
var workspaceNames = [...][3]byte{
{'F', '1', ' '},
{'F', '2', ' '},
{'F', '3', ' '},
{'F', '4', ' '},
{'F', '5', ' '},
{'F', '6', ' '},
{'F', '7', ' '},
{'F', '8', ' '},
{'F', '9', ' '},
{'F', '1', '0'},
{'F', '1', '1'},
{'F', '1', '2'},
{':', ':', ':'},
}