hbspbar

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

main.go (2057B)


      1 package main // import "hhvn.uk/hbspbar"
      2 
      3 import (
      4 	"os"
      5 	"os/signal"
      6 	"strings"
      7 	"syscall"
      8 
      9 	"hhvn.uk/hbspbar/bspc"
     10 	"hhvn.uk/hbspbar/common"
     11 	"hhvn.uk/hbspbar/drw"
     12 	"hhvn.uk/hbspbar/status"
     13 )
     14 
     15 func main() {
     16 	signals := make(chan os.Signal, 1)
     17 	signal.Notify(signals,
     18 		syscall.SIGINT,
     19 		syscall.SIGHUP,
     20 		syscall.SIGTERM)
     21 
     22 	if drw.InitErr != nil {
     23 		common.Error("Couldn't initialize X: %s\n", drw.InitErr)
     24 		return
     25 	}
     26 
     27 	if bspc.InitErr != nil {
     28 		common.Error("Couldn't subscribe to bspwm: %s\n", bspc.InitErr)
     29 		return
     30 	}
     31 	defer bspc.Cleanup()
     32 
     33 	state := <- bspc.NewState
     34 
     35 	for _, m := range state.Monitors {
     36 		if err := Create(state, m.ID); err != nil {
     37 			common.Error("%s\n", err)
     38 		}
     39 	}
     40 
     41 	var blocks []*status.Block = nil
     42 
     43 	mainloop: for {
     44 		select {
     45 		case s := <-signals:
     46 			common.Error("%s\n", s)
     47 			break mainloop
     48 		case e := <- drw.Events:
     49 			if e.Ev == nil && e.Err == nil {
     50 				common.Error("X connection clossed\n")
     51 				break mainloop
     52 			}
     53 
     54 			if e.Err != nil {
     55 				common.Perror("X.WaitForEvents", e.Err)
     56 				continue
     57 			}
     58 
     59 			// switch e.Ev.(type) {
     60 			// case xproto.DestroyNotifyEvent:
     61 			// 	Err <- fmt.Errorf("Window destroyed")
     62 			// 	return
     63 			// default:
     64 			// }
     65 		case err := <-bspc.EvErr:
     66 			common.Error("Couldn't read event: %s\n", err)
     67 			break mainloop
     68 		case event := <-bspc.Event:
     69 			if strings.HasPrefix(event.Name, "monitor_") {
     70 				id, err := common.Intify(event.Tokens[0])
     71 				if err == nil {
     72 					switch event.Name {
     73 					case "monitor_add":
     74 						fallthrough
     75 					case "monitor_geometry":
     76 						err = Create(state, id)
     77 						if err != nil {
     78 							common.Error("%s\n", err)
     79 						}
     80 					case "monitor_remove":
     81 						err = Destroy(state, id)
     82 						if err != nil {
     83 							common.Error("%s\n", err)
     84 						}
     85 					}
     86 				} else {
     87 					common.Perror("Intify", err)
     88 				}
     89 			}
     90 		case err := <-bspc.StErr:
     91 			common.Error("Couldn't load bspwm state: %s\n", err)
     92 			break mainloop
     93 		case blocks = <- status.NewBlocks:
     94 			Redraw(state, blocks)
     95 		case state = <- bspc.NewState:
     96 			Redraw(state, blocks)
     97 		}
     98 	}
     99 
    100 	Cleanup(state)
    101 }