Task: Window creation_X11




Go: package main


















import (
    "log"
    "github.com/jezek/xgb"
    "github.com/jezek/xgb/xproto"
)

func main() {
    
    X, err := xgb.NewConn()
    if err != nil {
        log.Fatal(err)
    }

    
    points := []xproto.Point{
        {10, 10},
        {10, 20},
        {20, 10},
        {20, 20}};

    polyline := []xproto.Point{
        {50, 10},
        { 5, 20},     
        {25,-20},
        {10, 10}};

    segments := []xproto.Segment{
        {100, 10, 140, 30},
        {110, 25, 130, 60}};

    rectangles := []xproto.Rectangle{
        { 10, 50, 40, 20},
        { 80, 50, 10, 40}};

    arcs := []xproto.Arc{
        {10, 100, 60, 40, 0, 90 << 6},
        {90, 100, 55, 40, 0, 270 << 6}};

    setup := xproto.Setup(X)
    
    screen := setup.DefaultScreen(X)

    
    foreground, _ := xproto.NewGcontextId(X)
    mask := uint32(xproto.GcForeground | xproto.GcGraphicsExposures)
    values := []uint32{screen.BlackPixel, 0}
    xproto.CreateGC(X, foreground, xproto.Drawable(screen.Root), mask, values)

    
    win, _ := xproto.NewWindowId(X)
    winDrawable := xproto.Drawable(win)

    
    mask = uint32(xproto.CwBackPixel | xproto.CwEventMask)
    values = []uint32{screen.WhitePixel, xproto.EventMaskExposure}
    xproto.CreateWindow(X,                  
            screen.RootDepth,               
            win,                            
            screen.Root,                    
            0, 0,                           
            150, 150,                       
            10,                             
            xproto.WindowClassInputOutput,  
            screen.RootVisual,              
            mask, values)                   

    
    xproto.MapWindow(X, win)

    for {
        evt, err := X.WaitForEvent()
        switch evt.(type) {
            case xproto.ExposeEvent:
                
                xproto.PolyPoint(X, xproto.CoordModeOrigin, winDrawable, foreground, points)

                
                xproto.PolyLine(X, xproto.CoordModePrevious, winDrawable, foreground, polyline)

                
                xproto.PolySegment(X, winDrawable, foreground, segments)

                
                xproto.PolyRectangle(X, winDrawable, foreground, rectangles)

                
                xproto.PolyArc(X, winDrawable, foreground, arcs)

            default:
                
        }

        if err != nil {
            log.Fatal(err)
        }
    }
    return
}




Python: from Xlib import X, display

class Window:
    def __init__(self, display, msg):
        self.display = display
        self.msg = msg
        
        self.screen = self.display.screen()
        self.window = self.screen.root.create_window(
            10, 10, 100, 100, 1,
            self.screen.root_depth,
            background_pixel=self.screen.white_pixel,
            event_mask=X.ExposureMask | X.KeyPressMask,
            )
        self.gc = self.window.create_gc(
            foreground = self.screen.black_pixel,
            background = self.screen.white_pixel,
            )

        self.window.map()

    def loop(self):
        while True:
            e = self.display.next_event()
                
            if e.type == X.Expose:
                self.window.fill_rectangle(self.gc, 20, 20, 10, 10)
                self.window.draw_text(self.gc, 10, 50, self.msg)
            elif e.type == X.KeyPress:
                raise SystemExit

                
if __name__ == "__main__":
    Window(display.Display(), "Hello, World!").loop()