hbspbar

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

x.go (2035B)


      1 package x // import "hhvn.uk/hbspbar/x"
      2 
      3 import (
      4 	"os"
      5 	"image"
      6 
      7 	"hhvn.uk/hbspbar/config"
      8 	"hhvn.uk/hbspbar/common"
      9 
     10 	"github.com/jezek/xgb"
     11 	"github.com/jezek/xgb/xproto"
     12 	"github.com/jezek/xgbutil"
     13 	"github.com/jezek/xgbutil/xgraphics"
     14 	"github.com/BurntSushi/freetype-go/freetype/truetype"
     15 )
     16 
     17 var InitErr error
     18 var X       *xgb.Conn
     19 var XU      *xgbutil.XUtil
     20 var Screen  *xproto.ScreenInfo
     21 
     22 var font    *truetype.Font
     23 var tw, th  int
     24 var tbpad   int
     25 
     26 func init() {
     27 	var err error
     28 
     29 	X, err = xgb.NewConn()
     30 	if err != nil {
     31 		InitErr = err
     32 		return
     33 	}
     34 
     35 	XU, err = xgbutil.NewConnXgb(X)
     36 	if err != nil {
     37 		InitErr = err
     38 		return
     39 	}
     40 
     41 	setup := xproto.Setup(X)
     42 	Screen = setup.DefaultScreen(X)
     43 
     44 	read, err := os.Open(config.Font)
     45 	if err != nil {
     46 		InitErr = common.Perror("os.Open", err)
     47 		return
     48 	}
     49 
     50 	font, err = xgraphics.ParseFont(read)
     51 	if err != nil {
     52 		InitErr = common.Perror("xgraphics.ParseFont", err)
     53 		return
     54 	}
     55 
     56 	test := xgraphics.New(XU, image.Rect(0, 0, int(config.FontSize * 10), int(config.FontSize * 10)))
     57 	tw, th, _ = test.Text(0, 0, int2BGRA(config.Fg), config.FontSize, font, "X")
     58 	tbpad = (int(config.H) - th) / 2
     59 }
     60 
     61 func int2BGRA(argb uint32) (xgraphics.BGRA) {
     62 	return xgraphics.BGRA{
     63 		B: uint8( argb & 0x000000ff),
     64 		G: uint8((argb & 0x0000ff00) >> 8),
     65 		R: uint8((argb & 0x00ff0000) >> 16),
     66 		A: uint8((argb & 0xff000000) >> 24) }
     67 }
     68 
     69 func GraphicReInit(i **xgraphics.Image, r image.Rectangle) {
     70 	if *i != nil {
     71 		(*i).Destroy()
     72 	}
     73 
     74 	*i = xgraphics.New(XU, r)
     75 }
     76 
     77 func DrawText(i *xgraphics.Image, x int, col uint32, text string) (int, error) {
     78 	nx, _, err := i.Text(x, config.FontYPad,
     79 		int2BGRA(col), config.FontSize, font, text)
     80 	return nx - x, err
     81 }
     82 
     83 func TextWidth(text string) (int) {
     84 	return tw * len(text)
     85 }
     86 
     87 func DrawRect(i *xgraphics.Image, x, y, w, h int, c uint32, fill bool) {
     88 	col := int2BGRA(c)
     89 
     90 	var ix, iy int
     91 
     92 	for ix = x; ix < x + w; ix++ {
     93 		for iy = y; iy < y + h; iy++ {
     94 			if fill || ix == x || ix == x + w - 1 ||
     95 					iy == y || iy == y + h - 1 {
     96 				i.Set(ix, iy, col)
     97 			}
     98 		}
     99 	}
    100 }