 : gadget-window ( world -- )
     dup window-loc>> over rect-dim glx-window
     over "Factor" create-xic <x11-handle>
-    2dup x11-handle-window register-window
+    2dup window>> register-window
     swap (>>handle) ;
 
 : wait-event ( -- event )

    r> utf8 encode dup length XChangeProperty drop ;

M: x11-ui-backend set-title ( string world -- )
    handle>> x11-handle-window swap dpy get -rot
    3dup set-title-old set-title-new ;
    
M: x11-ui-backend set-fullscreen* ( ? world -- )
    handle>> x11-handle-window "XClientMessageEvent" <c-object>
    tuck set-XClientMessageEvent-window
    swap _NET_WM_STATE_ADD _NET_WM_STATE_REMOVE ?
    over set-XClientMessageEvent-data0


M: x11-ui-backend (open-window) ( world -- )
    dup gadget-window
    handle>> window>> dup set-closable map-window ;

M: x11-ui-backend raise-window* ( world -- )
    handle>> [
        dpy get swap window>> XRaiseWindow drop
    ] when* ;

M: x11-ui-backend select-gl-context ( handle -- )
    dpy get swap
    dup window>> swap glx>> glXMakeCurrent
    [ "Failed to set current GLX context" throw ] unless ;

M: x11-ui-backend flush-gl-context ( handle -- )
    dpy get swap window>> glXSwapBuffers ;

M: x11-ui-backend ui ( -- )
    [
