Advertisement
weberc2

This Doesn't Work

Mar 9th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.56 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "github.com/BurntSushi/xgb"
  6.     "github.com/BurntSushi/xgb/xproto"
  7.     "github.com/Joker/go-cairo"
  8. )
  9.  
  10. type XConn struct {
  11.     *xgb.Conn
  12. }
  13.  
  14. func NewXConn () (*XConn, error) {
  15.     this := new (XConn)
  16.     var err error
  17.     this.Conn, err = xgb.NewConn ()
  18.     if err != nil { this.Conn = nil }
  19.     return this, err
  20. }
  21.  
  22. func (this *XConn) SetupInfo () *xproto.SetupInfo {
  23.     return xproto.Setup (this.Conn)
  24. }
  25.  
  26. func (this *XConn) DefaultScreen () *xproto.ScreenInfo {
  27.     setup := this.SetupInfo ()
  28.     return setup.DefaultScreen (this.Conn)
  29. }
  30.  
  31. func (this *XConn) Surface (x, y int16, width, height, border uint16) *cairo.Surface {
  32.     wid, _ := xproto.NewWindowId (this.Conn)
  33.    
  34.     screen := this.DefaultScreen ()
  35.    
  36.     structureNotify := xproto.EventMaskStructureNotify
  37.     keyPress := xproto.EventMaskKeyPress
  38.     keyRelease := xproto.EventMaskKeyRelease
  39.    
  40.     masks := uint32 (structureNotify | keyPress | keyRelease)
  41.     valList := []uint32 {0xffffffff, masks}
  42.    
  43.     xproto.CreateWindow ( this.Conn,                                                                // connection
  44.         screen.RootDepth,                                                   // depth
  45.         wid,                                                                            // window id
  46.         screen.Root,                                                            // parent
  47.         x, y, width, height, border,
  48.         xproto.WindowClassInputOutput,                      // class
  49.         screen.RootVisual,                                              // visual
  50.         xproto.CwBackPixel | xproto.CwEventMask,    // value mask
  51.         valList)                                                                        // value list
  52.     xproto.MapWindow (this.Conn, wid)
  53.  
  54.     drawable := xproto.Drawable (wid)
  55.     visualInfo := screen.AllowedDepths[0].Visuals[0]
  56.     surface := cairo.NewSurfaceFromXCB (drawable, visualInfo, int (width), int(height))
  57.     surface.Scale (float64 (width), float64(height))
  58.    
  59.     return surface
  60. }
  61.  
  62. func Setup () (*XConn, error) {
  63.     X, err := NewXConn ()
  64.     if err != nil { return nil, err }
  65.     surface := X.Surface (0, 0, 240, 240, 0)
  66.    
  67.     /* Drawing code goes here */
  68.     surface.SetSourceRGB (1.0, 1.0, 1.0)
  69.    
  70.     /* baseline, descent, ascent, height */
  71.     surface.SetLineWidth (4)
  72.     surface.Rectangle (0, 0, 240, 240)
  73.     surface.Stroke();
  74.     surface.Paint();
  75.  
  76.     /* extents: width & height */
  77.     surface.Rectangle (0, 0, 100, 100)
  78.     surface.Stroke();
  79.  
  80.     /* text's advance */
  81.     surface.SetSourceRGBA(0, 0, 0.75, 0.5);
  82.     surface.Fill();
  83.  
  84.     surface.Finish()
  85.     surface.Destroy()
  86.    
  87.     return X, nil
  88. }
  89.  
  90. func main () {
  91.     X, err := Setup ()
  92.     if err != nil { fmt.Println (err) }
  93.     for {
  94.         ev, xerr := X.WaitForEvent()
  95.         if ev == nil && xerr == nil {
  96.             fmt.Println("Both event and error are nil. Exiting...")
  97.             return
  98.         }
  99.  
  100.         if ev != nil {
  101.             fmt.Printf("Event: %s\n", ev)
  102.         }
  103.         if xerr != nil {
  104.             fmt.Printf("Error: %s\n", xerr)
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement