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

import (
	"fmt"
	"sync"
	"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 bar struct {
	id      int
	w       *drw.Window
	i       *image.RGBA
	drawing sync.Mutex
}

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)

	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(state *bspc.State) {
	var bg     color.Color
	var filled bool

	b.drawing.Lock()
	defer b.drawing.Unlock()

	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
	}

	status.Mutex.Lock()
	cx = int(mon.Rectangle.Width) - 6
	for i := len(status.Status) - 1; i >= 0; i -= 1 {
		s, ok := status.Status[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 := len(status.Status) - 1; i >= 0; i -= 1 {
		s, ok := status.Status[i]
		if ok {
			cx -= s.W

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

			cx -= config.StatusPad
		}
	}
	status.Mutex.Unlock()

	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 cleanup  chan bool

var Handle  handle

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

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

	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 <- cleanup:
				for _, b := range bars {
					go b.destroy(state)
				}
				return
			case <- status.Updated:
				for _, b := range bars {
					go b.draw(state)
				}
			case state = <- Handle.NewState:
				for _, b := range bars {
					go b.draw(state)
				}
			}
		}
	}()
}

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
	}

	go b.draw(state)

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

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

func Cleanup() {
	cleanup <- true
}