x.go (2210B)
1 package drw // import "hhvn.uk/hbspbar/drw" 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/icccm" 14 "github.com/jezek/xgbutil/ewmh" 15 "github.com/jezek/xgbutil/xgraphics" 16 "github.com/BurntSushi/freetype-go/freetype/truetype" 17 ) 18 19 type eventwrap struct { 20 Ev xgb.Event 21 Err error 22 } 23 24 var InitErr error 25 var Events chan *eventwrap 26 27 var xc *xgb.Conn 28 var xu *xgbutil.XUtil 29 var screen *xproto.ScreenInfo 30 var font *truetype.Font 31 32 func init() { 33 var err error 34 35 xc, err = xgb.NewConn() 36 if err != nil { 37 InitErr = err 38 return 39 } 40 41 xu, err = xgbutil.NewConnXgb(xc) 42 if err != nil { 43 InitErr = err 44 return 45 } 46 47 setup := xproto.Setup(xc) 48 screen = setup.DefaultScreen(xc) 49 50 read, err := os.Open(config.Font) 51 if err != nil { 52 InitErr = common.Perror("os.Open", err) 53 return 54 } 55 56 font, err = xgraphics.ParseFont(read) 57 if err != nil { 58 InitErr = common.Perror("xgraphics.ParseFont", err) 59 return 60 } 61 62 Events := make(chan *eventwrap) 63 64 go func() { for { 65 ev, err := xc.WaitForEvent() 66 w := eventwrap{ev, err} 67 Events <- &w 68 }}() 69 } 70 71 type Window struct { 72 id xproto.Window 73 } 74 75 func WindowCreate(x, y, w, h uint) (*Window, error) { 76 var win Window 77 var err error 78 79 win.id, err = xproto.NewWindowId(xc) 80 if err != nil { 81 return nil, common.Perror("xproto.NewWindowId", err) 82 } 83 84 xproto.CreateWindow(xc, screen.RootDepth, win.id, screen.Root, 85 int16(x), int16(y), uint16(w), uint16(h), 0, 86 xproto.WindowClassInputOutput, screen.RootVisual, 87 xproto.CwBackPixel|xproto.CwEventMask, []uint32{ 88 0x00000000, 89 xproto.EventMaskStructureNotify }) 90 91 class := icccm.WmClass{"hbspbar", "hbspbar"} 92 icccm.WmClassSet(xu, win.id, &class) 93 94 ewmh.WmWindowTypeSet(xu, win.id, []string{"_NET_WM_WINDOW_TYPE_DOCK"}) 95 96 err = xproto.MapWindowChecked(xc, win.id).Check() 97 if err != nil { 98 return nil, common.Perror("xproto.MapWindow", err) 99 } 100 101 return &win, nil 102 } 103 104 func (w *Window) Paint(i image.Image) { 105 xgr := xgraphics.NewConvert(xu, i) 106 107 xgr.XSurfaceSet(w.id) 108 xgr.XDraw() 109 xgr.XPaint(w.id) 110 xgr.Destroy() 111 } 112 113 func (w *Window) Destroy() { 114 xproto.DestroyWindow(xc, w.id) 115 }