summaryrefslogtreecommitdiff
path: root/bar/bar.go
blob: c77e2e918092805ad70894dffd9f01e1cbd6b662 (plain)
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
package bar // import "hhvn.uk/hbspbar/bar"

import (
	"fmt"
	"image"
	"errors"
	"image/color"

	"hhvn.uk/hbspbar/config"
	"hhvn.uk/hbspbar/common"
	"hhvn.uk/hbspbar/status"
	"hhvn.uk/hbspbar/bspc"
	"hhvn.uk/hbspbar/drw"
)


var bars     map[int]*bar

type drawinfo struct {
	State  *bspc.State
	Blocks *status.Blocks
}

type bar struct {
	id      int
	w       *drw.Window
	i       *image.RGBA
	redraw  chan drawinfo
}

func (b *bar) init(state *bspc.State) error {
	mon, err := b.getmon(state)
	if err != nil { return err }

	go mon.TopPadding(config.H)

	geom := mon.Rectangle

	b.w, err = drw.WindowCreate(geom.X, geom.Y, geom.Width, config.H)
	if err != nil { return err }

	rect := image.Rect(0, 0, int(mon.Rectangle.Width), int(config.H))
	b.i = image.NewRGBA(rect)

	b.redraw = make(chan drawinfo, 20)

	go func(){
		for d := range b.redraw {
			// Gobble up excess redraws
		gobble: for {
				select {
				case d = <- b.redraw:
				default: break gobble
				}
			}

			b.draw(d)
		}
	}()

	return nil
}

func (b bar) getmon(state *bspc.State) (*bspc.Monitor, error) {
	return state.GetMon(b.id)
}

func (b *bar) drawText(x int, col color.Color, text string) (int, error) {
	return drw.DrawText(b.i, x, col, text)
}

func (b *bar) drawRect(x, y, w, h int, c color.Color, fill bool) {
	drw.DrawRect(b.i, x, y, w, h, c, fill)
}

func (b bar) draw(d drawinfo) {
	var bg     color.Color
	var filled bool

	state  := d.State
	blocks := d.Blocks

	mon, err := b.getmon(state)
	if err != nil {
		return
	}

	// Dimensions of drawing space
	cx := 0
	w  := int(mon.Rectangle.Width)
	h  := int(config.H)


	// Draw background
	b.drawRect(0, 0, w, h, config.Bg, true)

	// Monitor
	montext := mon.Name
	monw    := 6 + drw.TextWidth(montext) + 8

	if mon.Focused {
		bg = config.Sel
	} else {
		bg = config.UnselMon
	}

	b.drawRect(cx, 0, monw, h, bg, true)
	b.drawText(cx + 6, config.Fg, montext)
	cx += monw

	// Desktops
	boxw := 4
	ds   := len(mon.Desktops)
	dw   := 4 + (4 + boxw + 2 + 3) * ds + 5

	for i := 0; i < ds; i++ {
		dw += drw.TextWidth(mon.Desktops[i].Name)
	}

	cx += 4
	for i := 0; i < ds; i++ {
		d := mon.Desktops[i]

		cx += 5

		if d.Focused {
			bg = config.Sel
		} else {
			bg = config.FgDark
		}

		filled = d.Root != nil

		b.drawRect(cx, 3, boxw, h - 7, bg, filled)
		cx += boxw + 2
		ax, _ := b.drawText(cx, config.Fg, d.Name)
		cx += ax + 2
	}

	cx = int(mon.Rectangle.Width) - 6
	for i := blocks.Len() - 1; i >= 0; i -= 1 {
		s, ok := blocks.Get(i)
		if ok {
			cx -= s.W
			cx -= config.StatusPad
		}
	}
	cx += config.StatusPad
	cx -= 6

	b.drawRect(cx, 0, int(mon.Rectangle.Width) - cx, int(config.H),
		config.Status, true)

	cx = int(mon.Rectangle.Width) - 6
	for i := blocks.Len() - 1; i >= 0; i -= 1 {
		s, ok := blocks.Get(i)
		if ok {
			cx -= s.W

			drw.AddImg(b.i, cx, s.W, s.I)

			cx -= config.StatusPad
		}
	}

	b.w.Paint(b.i)
}

func (b *bar) destroy(state *bspc.State) {
	mon, _ := b.getmon(state)
	if (mon != nil) {
		mon.TopPadding(0)
	}
}

// External interface
type handle struct {
	Create   chan int
	Destroy  chan int
	NewState chan *bspc.State
	Err      chan error
}

var Handle  handle

// Store the state for when bar.Cleanup() is called
// This is a bit hacky but I don't really see any alternative
var finalstate *bspc.State = nil

func init() {
	var state  *bspc.State    = nil
	var blocks *status.Blocks = nil 

	Handle.Create   = make(chan int)
	Handle.Destroy  = make(chan int)
	Handle.NewState = make(chan *bspc.State)
	Handle.Err      = make(chan error)

	go func() {
		defer func() {
			close(Handle.Create)
			close(Handle.Destroy)
			close(Handle.NewState)
			close(Handle.Err)
		}()

		for {
			select {
			case e := <- drw.Events:
				if e.Ev == nil && e.Err == nil {
					Handle.Err <- fmt.Errorf("X connection clossed")
					return
				}

				if e.Err != nil {
					common.Perror("X.WaitForEvents", e.Err)
					continue
				}

				// switch e.Ev.(type) {
				// case xproto.DestroyNotifyEvent:
				// 	Handle.Err <- fmt.Errorf("Window destroyed")
				// 	return
				// default:
				// }
			case id := <- Handle.Destroy:
				bars[id].destroy(state)
				delete(bars, id)
			case id := <- Handle.Create:
				if state == nil {
					Handle.Err <- errors.New("attempted to create a bar with uninitialized state")
					return
				}
				if _, ok := bars[id]; ok { break }
				if err := create(state, id); err != nil {
					Handle.Err <- fmt.Errorf("Couldn't create window: %s\n", err)
					return
				}
			case blocks = <- status.NewBlocks:
				for _, b := range bars {
					b.redraw <- drawinfo{state, blocks}
				}
			case state = <- Handle.NewState:
				finalstate = state
				for _, b := range bars {
					b.redraw <- drawinfo{state, blocks}
				}
			}
		}
	}()
}

func create(state *bspc.State, id int) (error) {
	var b bar

	b.id = id
	b.i  = nil

	if err := b.init(state); err != nil {
		return err
	}

	if bars == nil {
		bars = make(map[int]*bar)
	}

	bars[b.id] = &b
	return nil
}

func Cleanup() {
	if finalstate != nil {
		for _, b := range bars {
			b.destroy(finalstate)
		}
	}
}