factor/library/x11/utilities.factor

117 lines
3.1 KiB
Factor
Raw Normal View History

2006-03-19 19:37:01 -05:00
! Copyright (C) 2005, 2006 Eduardo Cavazos and Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
IN: x11
2006-03-19 23:17:14 -05:00
USING: alien arrays errors io kernel math namespaces prettyprint
sequences threads ;
2006-03-19 19:37:01 -05:00
SYMBOL: dpy
SYMBOL: scr
SYMBOL: root
! Window management
2006-03-19 23:17:14 -05:00
: create-window-mask ( -- n )
CWBackPixel CWBorderPixel bitor
CWColormap bitor CWEventMask bitor ;
: create-colormap ( visinfo -- colormap )
dpy get root get rot XVisualInfo-visual AllocNone
XCreateColormap ;
: window-attributes ( visinfo -- attributes )
"XSetWindowAttributes" <c-object>
0 over set-XSetWindowAttributes-background_pixel
0 over set-XSetWindowAttributes-border_pixel
[ >r create-colormap r> set-XSetWindowAttributes-colormap ] keep
StructureNotifyMask ExposureMask bitor over set-XSetWindowAttributes-event_mask ;
: create-window ( w h visinfo -- window )
>r >r >r dpy get root get 0 0 r> r> 0 r>
[ XVisualInfo-depth InputOutput ] keep
[ XVisualInfo-visual create-window-mask ] keep
window-attributes XCreateWindow ;
2006-03-19 19:37:01 -05:00
: destroy-window ( win -- )
dpy get swap XDestroyWindow drop ;
: map-window ( win -- )
dpy get swap XMapWindow drop ;
: map-subwindows ( win -- )
dpy get swap XMapSubwindows drop ;
: unmap-window ( win -- )
dpy get swap XUnmapWindow drop ;
: unmap-subwindows ( win -- )
dpy get swap XUnmapSubwindows drop ;
! Event handling
: select-input ( win mask -- )
>r dpy get swap r> XSelectInput drop ;
: flush-dpy ( -- ) dpy get XFlush drop ;
: sync-dpy ( discard -- ) >r dpy get r> XSync ;
: next-event ( -- event )
dpy get "XEvent" <c-object> dup >r XNextEvent drop r> ;
: mask-event ( mask -- event )
2006-03-19 23:17:14 -05:00
>r dpy get r> "XEvent" <c-object> dup >r XMaskEvent drop r> ;
2006-03-19 19:37:01 -05:00
: events-queued ( mode -- n ) >r dpy get r> XEventsQueued ;
2006-03-19 23:17:14 -05:00
: next-event ( -- event )
dpy get "XEvent" <c-object> dup >r XNextEvent drop r> ;
: wait-event ( -- event )
QueuedAfterFlush events-queued 0 >
[ next-event ] [ 10 sleep wait-event ] if ;
: handle-event ( event -- )
XAnyEvent-type . flush ;
: event-loop ( -- )
wait-event handle-event event-loop ;
2006-03-19 19:37:01 -05:00
! GLX
: >int-array ( seq -- <int-array> )
2006-03-19 23:17:14 -05:00
dup length dup "int" <c-array> -rot
2006-03-19 19:37:01 -05:00
[ pick set-int-nth ] 2each ;
: choose-visual ( -- XVisualInfo* )
dpy get scr get
GLX_RGBA GLX_DOUBLEBUFFER 0 3array >int-array
2006-03-19 23:17:14 -05:00
glXChooseVisual
[ "Could not get a double-buffered GLX RGBA visual" throw ] unless* ;
2006-03-19 19:37:01 -05:00
: create-context ( XVisualInfo* -- GLXContext )
2006-03-19 23:17:14 -05:00
>r dpy get r> f 1 glXCreateContext
[ "Failed to create GLX context" throw ] unless* ;
2006-03-19 19:37:01 -05:00
: make-current ( win GLXContext -- )
2006-03-19 23:17:14 -05:00
>r dpy get swap r> glXMakeCurrent
[ "Failed to set current GLX context" throw ] unless ;
2006-03-19 19:37:01 -05:00
: swap-buffers ( win -- )
dpy get swap glXSwapBuffers ;
2006-03-19 23:17:14 -05:00
! Initialization
: check-display
[ "Cannot connect to X server - check $DISPLAY" throw ] unless* ;
: (initialize-x) ( display-string -- )
XOpenDisplay check-display dpy set
dpy get XDefaultScreen scr set
dpy get scr get XRootWindow root set ;
: initialize-x ( display-string -- )
dpy get [
drop
] [
(initialize-x) [ event-loop ] in-thread
] if ;