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