Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "github.com/BurntSushi/xgb"
- "github.com/BurntSushi/xgb/xproto"
- "github.com/Joker/go-cairo"
- )
- type XConn struct {
- *xgb.Conn
- }
- func NewXConn () (*XConn, error) {
- this := new (XConn)
- var err error
- this.Conn, err = xgb.NewConn ()
- if err != nil { this.Conn = nil }
- return this, err
- }
- func (this *XConn) SetupInfo () *xproto.SetupInfo {
- return xproto.Setup (this.Conn)
- }
- func (this *XConn) DefaultScreen () *xproto.ScreenInfo {
- setup := this.SetupInfo ()
- return setup.DefaultScreen (this.Conn)
- }
- func (this *XConn) Surface (x, y int16, width, height, border uint16) *cairo.Surface {
- wid, _ := xproto.NewWindowId (this.Conn)
- screen := this.DefaultScreen ()
- structureNotify := xproto.EventMaskStructureNotify
- keyPress := xproto.EventMaskKeyPress
- keyRelease := xproto.EventMaskKeyRelease
- masks := uint32 (structureNotify | keyPress | keyRelease)
- valList := []uint32 {0xffffffff, masks}
- xproto.CreateWindow ( this.Conn, // connection
- screen.RootDepth, // depth
- wid, // window id
- screen.Root, // parent
- x, y, width, height, border,
- xproto.WindowClassInputOutput, // class
- screen.RootVisual, // visual
- xproto.CwBackPixel | xproto.CwEventMask, // value mask
- valList) // value list
- xproto.MapWindow (this.Conn, wid)
- drawable := xproto.Drawable (wid)
- visualInfo := screen.AllowedDepths[0].Visuals[0]
- surface := cairo.NewSurfaceFromXCB (drawable, visualInfo, int (width), int(height))
- surface.Scale (float64 (width), float64(height))
- return surface
- }
- func Setup () (*XConn, error) {
- X, err := NewXConn ()
- if err != nil { return nil, err }
- surface := X.Surface (0, 0, 240, 240, 0)
- /* Drawing code goes here */
- surface.SetSourceRGB (1.0, 1.0, 1.0)
- /* baseline, descent, ascent, height */
- surface.SetLineWidth (4)
- surface.Rectangle (0, 0, 240, 240)
- surface.Stroke();
- surface.Paint();
- /* extents: width & height */
- surface.Rectangle (0, 0, 100, 100)
- surface.Stroke();
- /* text's advance */
- surface.SetSourceRGBA(0, 0, 0.75, 0.5);
- surface.Fill();
- surface.Finish()
- surface.Destroy()
- return X, nil
- }
- func main () {
- X, err := Setup ()
- if err != nil { fmt.Println (err) }
- for {
- ev, xerr := X.WaitForEvent()
- if ev == nil && xerr == nil {
- fmt.Println("Both event and error are nil. Exiting...")
- return
- }
- if ev != nil {
- fmt.Printf("Event: %s\n", ev)
- }
- if xerr != nil {
- fmt.Printf("Error: %s\n", xerr)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement