package bar // import "hhvn.uk/hbspbar/bar" import ( "fmt" "sync" "errors" "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 var stoploop chan bool 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) xproto.DestroyWindow(b.X, b.w) } 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 } var InitErr error var Handle handle func init() { InitErr = nil bars = list.New() x, err := xgb.NewConn() if err != nil { InitErr = err return } xu, err := xgbutil.NewConnXgb(x) if err != nil { InitErr = err return } font, err = getFont(conf.Font) if err != nil { InitErr = err return } setup := xproto.Setup(x) screen := setup.DefaultScreen(x) 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) events := make(chan *eventwrap) stoploop = make(chan bool, 1) 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 close(Handle.Create) close(Handle.Destroy) close(Handle.NewState) close(Handle.Err) }() for { select { case e := <- events: if e.Ev == nil && e.Err == nil { Handle.Err <- fmt.Errorf("X connection clossed") return } if e.Err != nil { common.Perror("x.WaitForEvents", err) continue } // switch e.Ev.(type) { // case xproto.DestroyNotifyEvent: // Handle.Err <- fmt.Errorf("Window destroyed") // return // default: // } case id := <- Handle.Destroy: destroy(id) case id := <- Handle.Create: if state == nil { Handle.Err <- errors.New("attempted to create a bar with uninitialized state") return } if getbar(id) != nil { break } if err := create(x, xu, screen, state.GetMon(id)); err != nil { Handle.Err <- fmt.Errorf("Couldn't create window: %s\n", err) return } case state = <- Handle.NewState: fmt.Println(state) for e := bars.Front(); e != nil; e = e.Next() { v := e.Value.(*bar) v.Mon = state.GetMon(v.Mon.ID) go v.draw() } case <- stoploop: return } } }() } func getbar(monid int) (*bar) { for e := bars.Front(); e != nil; e = e.Next() { v := e.Value.(*bar) if v.Mon.ID == monid { return v } } return 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 == monid { v.destroy() bars.Remove(e) if monid != -1 { return true } } } if monid == -1 { return true } return false } func Cleanup() { destroy(-1) stoploop <- true }