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
|
package bar
import (
"fmt"
"sync"
"container/list"
"hhvn.uk/hbspbar/common"
"hhvn.uk/hbspbar/bspc"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
"github.com/jezek/xgbutil"
"github.com/jezek/xgbutil/icccm"
"github.com/jezek/xgbutil/ewmh"
"github.com/jezek/xgbutil/xgraphics"
"github.com/BurntSushi/freetype-go/freetype/truetype"
)
var conf = struct {
H uint
Bg uint32
Bg2 uint32
Bg3 uint32
Fg uint32
FgDark uint32
Sel uint32
Urg uint32
Font string
FontSize float64
FontYPad int
} { 17,
0xff050a10,
0xff0c1726,
0xff14283c,
0xffeeeeee,
0xff444444,
0xff1b364b,
0xff90222b,
"/usr/share/fonts/TTF/DejaVuSansMono.ttf",
11.0, 1 }
var bars *list.List
var font *truetype.Font
type bar struct {
X *xgb.Conn
Xu *xgbutil.XUtil
Scr *xproto.ScreenInfo
Mon *bspc.Monitor
w xproto.Window
i *xgraphics.Image
tw, th int
tbpad int
drawing sync.Mutex
}
func (b *bar) init() error {
var err error
go b.Mon.TopPadding(conf.H)
b.w, err = xproto.NewWindowId(b.X)
if err != nil {
return common.Perror("xproto.NewWindowId", err)
}
geom := b.Mon.Rectangle
xproto.CreateWindow(b.X, b.Scr.RootDepth, b.w, b.Scr.Root,
int16(geom.X), int16(geom.Y), uint16(geom.Width), uint16(conf.H), 0,
xproto.WindowClassInputOutput, b.Scr.RootVisual,
xproto.CwBackPixel|xproto.CwEventMask, []uint32{
conf.Bg,
xproto.EventMaskStructureNotify })
class := icccm.WmClass{"hbspbar", "hbspbar"}
icccm.WmClassSet(b.Xu, b.w, &class)
ewmh.WmWindowTypeSet(b.Xu, b.w, []string{"_NET_WM_WINDOW_TYPE_DOCK"})
ewmh.WmStateSet(b.Xu, b.w, []string{"_NET_WM_STATE_STICKY", "_NET_WM_STATE_ABOVE"})
err = xproto.MapWindowChecked(b.X, b.w).Check()
if err != nil {
return common.Perror("xproto.MapWindow", err)
}
b.drawReInit()
b.tw, b.th, _ = b.i.Text(0, 0, int2BGRA(conf.Fg), conf.FontSize, font, "X")
b.tbpad = (int(conf.H) - b.th) / 2
return nil
}
func (b *bar) draw() {
var bg uint32
var filled bool
b.drawing.Lock()
b.drawReInit()
// Dimensions of drawing space
cx := 0
w := int(b.Mon.Rectangle.Width)
h := int(conf.H)
// Draw background
b.drawRect(0, 0, w, h, conf.Bg, true)
// Monitor
montext := b.Mon.Name
monw := 6 + b.textWidth(montext) + 8
if b.Mon.Focused {
bg = conf.Sel
} else {
bg = conf.Bg2
}
b.drawRect(cx, 0, monw, h, bg, true)
b.drawText(cx + 6, conf.Fg, montext)
cx += monw
// Desktops
boxw := 4
ds := len(b.Mon.Desktops)
dw := 4 + (4 + boxw + 2 + 3) * ds + 5
for i := 0; i < ds; i++ {
dw += b.textWidth(b.Mon.Desktops[i].Name)
}
cx += 4
for i := 0; i < ds; i++ {
d := b.Mon.Desktops[i]
cx += 5
if d.Focused {
bg = conf.Sel
} else {
bg = conf.FgDark
}
filled = d.Root != nil
b.drawRect(cx, 3, boxw, h - 7, bg, filled)
cx += boxw + 2
ax, _ := b.drawText(cx, conf.Fg, d.Name)
cx += ax + 2
}
cx += 5
b.i.XSurfaceSet(b.w)
b.i.XDraw()
b.i.XPaint(b.w)
b.drawing.Unlock()
}
func (b *bar) destroy() {
b.Mon.TopPadding(0)
}
type eventwrap struct {
Ev xgb.Event
Err error
}
// External interface
type Handle struct {
Create chan int
Destroy chan int
NewState chan *bspc.State
Err chan error
}
func Init(state *bspc.State) (*Handle, error) {
bars = list.New()
x, err := xgb.NewConn()
if err != nil { return nil, err }
xu, err := xgbutil.NewConnXgb(x)
if err != nil { return nil, err }
font, err = getFont(conf.Font)
if err != nil { return nil, err }
setup := xproto.Setup(x)
screen := setup.DefaultScreen(x)
var h Handle
h.Create = make(chan int)
h.Destroy = make(chan int)
h.NewState = make(chan *bspc.State)
h.Err = make(chan error)
events := make(chan *eventwrap)
xstop := make(chan bool, 1)
go func() { for {
ev, err := x.WaitForEvent()
w := eventwrap{ev, err}
select {
case <- xstop:
return
default:
events <- &w
}
}}()
go func() {
defer func() { xstop <- true }()
for {
select {
case e := <- events:
if e.Ev == nil && e.Err == nil {
h.Err <- fmt.Errorf("X connection clossed")
return
}
if e.Err != nil {
common.Perror("x.WaitForEvents", err)
continue
}
switch e.Ev.(type) {
case xproto.DestroyNotifyEvent:
h.Err <- fmt.Errorf("Window destroyed")
return
default:
}
case id := <- h.Destroy:
destroy(id)
case id := <- h.Create:
if err := create(x, xu, screen, state.GetMon(id)); err != nil {
h.Err <- fmt.Errorf("Couldn't create window: %s\n", err)
return
}
case state := <- h.NewState:
for e := bars.Front(); e != nil; e = e.Next() {
v := e.Value.(*bar)
v.Mon = state.GetMon(v.Mon.ID)
go v.draw()
}
}
}
}()
return &h, nil
}
func create(x *xgb.Conn, xu *xgbutil.XUtil,
scr *xproto.ScreenInfo, m *bspc.Monitor) (error) {
var b bar
b.X = x
b.Xu = xu
b.Scr = scr
b.Mon = m
b.i = nil
if err := b.init(); err != nil {
return err
}
go b.draw()
bars.PushBack(&b)
return nil
}
func destroy(monid int) (bool) {
var next *list.Element
for e := bars.Front(); e != nil; e = next {
next = e.Next()
v := e.Value.(*bar)
/* A monid of -1 means that we're doing
the bar.Cleanup schermoodle today */
if monid == -1 || monid > 0 && v.Mon.ID == int(monid) {
v.destroy()
bars.Remove(e)
if monid != -1 { return true }
}
}
if monid == -1 { return true }
return false
}
func Cleanup() {
destroy(-1)
}
|