hbspbar

[WIP] bspwm status bar
git clone https://hhvn.uk/hbspbar
git clone git://hhvn.uk/hbspbar
Log | Files | Refs

commit 851758d4a04c4cb05f802ea3127841e94456ee44
parent 76dc78859b31cd15c8327e64b7684fbc2063a0b4
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 15 Nov 2023 19:36:40 +0000

bar pkg uses init()

Diffstat:
Mbar/bar.go | 64+++++++++++++++++++++++++++++++++++++++++++++-------------------
Mmain.go | 25+++++++++++++------------
2 files changed, 58 insertions(+), 31 deletions(-)

diff --git a/bar/bar.go b/bar/bar.go @@ -3,6 +3,7 @@ package bar // import "hhvn.uk/hbspbar/bar" import ( "fmt" "sync" + "errors" "container/list" "hhvn.uk/hbspbar/common" @@ -172,33 +173,49 @@ type eventwrap struct { } // External interface -type Handle struct { +type handle struct { Create chan int Destroy chan int NewState chan *bspc.State Err chan error } -func Init(state *bspc.State) (*Handle, error) { +var InitErr error +var Handle handle + +func init() { + InitErr = nil + bars = list.New() x, err := xgb.NewConn() - if err != nil { return nil, err } + if err != nil { + InitErr = err + return + } xu, err := xgbutil.NewConnXgb(x) - if err != nil { return nil, err } + if err != nil { + InitErr = err + return + } font, err = getFont(conf.Font) - if err != nil { return nil, err } + if err != nil { + InitErr = err + return + } 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) + 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) @@ -217,13 +234,19 @@ func Init(state *bspc.State) (*Handle, error) { }}() go func() { - defer func() { xstop <- true }() + 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 { - h.Err <- fmt.Errorf("X connection clossed") + Handle.Err <- fmt.Errorf("X connection clossed") return } @@ -234,19 +257,24 @@ func Init(state *bspc.State) (*Handle, error) { // switch e.Ev.(type) { // case xproto.DestroyNotifyEvent: - // h.Err <- fmt.Errorf("Window destroyed") + // Handle.Err <- fmt.Errorf("Window destroyed") // return // default: // } - case id := <- h.Destroy: + case id := <- Handle.Destroy: destroy(id) - case id := <- h.Create: + 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 { - h.Err <- fmt.Errorf("Couldn't create window: %s\n", err) + Handle.Err <- fmt.Errorf("Couldn't create window: %s\n", err) return } - case state := <- h.NewState: + 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) @@ -257,8 +285,6 @@ func Init(state *bspc.State) (*Handle, error) { } } }() - - return &h, nil } func getbar(monid int) (*bar) { diff --git a/main.go b/main.go @@ -40,7 +40,7 @@ func reloadState(current *bspc.State) (*bspc.State, error) { } } -func handleEvents(events *bspc.Subscriber, bar *bar.Handle) (chan error, chan bool) { +func handleEvents(events *bspc.Subscriber) (chan error, chan bool) { var event *bspc.Event errors := make(chan error) @@ -60,9 +60,10 @@ func handleEvents(events *bspc.Subscriber, bar *bar.Handle) (chan error, chan bo switch (event.Name) { case "monitor_add": case "monitor_geometry": - bar.Create <- id + common.Error("create2") + bar.Handle.Create <- id case "monitor_remove": - bar.Destroy <- id + bar.Handle.Destroy <- id } } } @@ -97,18 +98,18 @@ func main() { } defer events.Close() - barhandle, err := bar.Init(state) - if err != nil { + everr, evreload := handleEvents(events) + + if bar.InitErr != nil { common.Error("Couldn't initialize X: %s\n", err) return } + defer bar.Cleanup() + bar.Handle.NewState <- state for _, m := range state.Monitors { - barhandle.Create <- m.ID + bar.Handle.Create <- m.ID } - defer bar.Cleanup() - - everr, evreload := handleEvents(events, barhandle) for { select { @@ -118,7 +119,7 @@ func main() { case s := <- signals: common.Error("%s\n", s) return - case err := <- barhandle.Err: + case err := <- bar.Handle.Err: common.Error("%s\n", err) return case <- evreload: @@ -130,12 +131,12 @@ func main() { // Check if there's been an error before sending the // new state (or else the program gets stuck) select { - case err := <- barhandle.Err: + case err := <- bar.Handle.Err: common.Error("%s\n", err) return default: } - barhandle.NewState <- state + bar.Handle.NewState <- state } } }