From e795571639f0309424e37fd01a00cedcbe6c78af Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 11 Dec 2008 16:50:37 -0800 Subject: [PATCH] seed of X11 offscreen backend --- basis/ui/gadgets/worlds/worlds.factor | 4 -- basis/ui/x11/x11.factor | 28 ++++++++++++-- basis/x11/glx/glx.factor | 12 +++--- basis/x11/windows/windows.factor | 28 +++++++++++++- basis/x11/xlib/xlib.factor | 54 +++++++++++++++++++++++++++ extra/ui/offscreen/offscreen.factor | 3 +- 6 files changed, 113 insertions(+), 16 deletions(-) diff --git a/basis/ui/gadgets/worlds/worlds.factor b/basis/ui/gadgets/worlds/worlds.factor index 9290af1f64..732a438203 100644 --- a/basis/ui/gadgets/worlds/worlds.factor +++ b/basis/ui/gadgets/worlds/worlds.factor @@ -13,8 +13,6 @@ title status fonts handle window-loc ; -TUPLE: offscreen-world < world ; - : find-world ( gadget -- world/f ) [ world? ] find-parent ; : show-status ( string/f gadget -- ) @@ -53,8 +51,6 @@ M: world request-focus-on ( child gadget -- ) : ( gadget title status -- world ) world new-world ; -: ( gadget title status -- world ) - offscreen-world new-world ; M: world layout* dup call-next-method diff --git a/basis/ui/x11/x11.factor b/basis/ui/x11/x11.factor index e5a3a8306b..b65185967a 100755 --- a/basis/ui/x11/x11.factor +++ b/basis/ui/x11/x11.factor @@ -14,9 +14,12 @@ SINGLETON: x11-ui-backend : XA_NET_WM_NAME ( -- atom ) "_NET_WM_NAME" x-atom ; -TUPLE: x11-handle window glx xic ; +TUPLE: x11-handle-base glx ; +TUPLE: x11-handle < x11-handle-base xic window ; +TUPLE: x11-pixmap-handle < x11-handle-base pixmap glx-pixmap ; C: x11-handle +C: x11-pixmap-handle M: world expose-event nip relayout ; @@ -185,7 +188,7 @@ M: world client-event : gadget-window ( world -- ) dup window-loc>> over rect-dim glx-window - over "Factor" create-xic + over "Factor" create-xic rot 2dup window>> register-window >>handle drop ; @@ -250,12 +253,31 @@ M: x11-ui-backend raise-window* ( world -- ) M: x11-handle select-gl-context ( handle -- ) dpy get swap - dup window>> swap glx>> glXMakeCurrent + [ window>> ] [ glx>> ] bi glXMakeCurrent [ "Failed to set current GLX context" throw ] unless ; M: x11-handle flush-gl-context ( handle -- ) dpy get swap window>> glXSwapBuffers ; +M: x11-pixmap-handle select-gl-context ( handle -- ) + dpy get swap + [ glx-pixmap>> ] [ glx>> ] bi glXMakeCurrent + [ "Failed to set current GLX context" throw ] unless ; + +M: x11-pixmap-handle flush-gl-context ( handle -- ) + drop ; + +M: x11-ui-backend (open-offscreen-buffer) ( world -- ) + dup dim>> glx-pixmap >>handle drop ; +M: x11-ui-backend (close-offscreen-buffer) ( handle -- ) + dpy get swap + [ glx-pixmap>> glXDestroyGLXPixmap ] + [ pixmap>> XFreePixmap drop ] + [ glx>> glXDestroyContext ] 2tri ; + +M: x11-ui-backend offscreen-pixels ( world -- alien w h ) + [ [ dim>> ] [ handle>> pixmap>> ] bi pixmap-bits ] [ dim>> first2 ] bi ; + M: x11-ui-backend ui ( -- ) [ f [ diff --git a/basis/x11/glx/glx.factor b/basis/x11/glx/glx.factor index 1fab283242..e0b786ce7d 100644 --- a/basis/x11/glx/glx.factor +++ b/basis/x11/glx/glx.factor @@ -84,13 +84,13 @@ FUNCTION: void* glXGetProcAddress ( char* procname ) ; FUNCTION: void* glXGetProcAddressARB ( char* procname ) ; ! GLX Events -! (also skipped for now. only has GLXPbufferClobberEvent, the rest is handled by xlib methinks +! (also skipped for now. only has GLXPbufferClobberEvent, the rest is handled by xlib methinks) -: choose-visual ( -- XVisualInfo* ) - dpy get scr get +: choose-visual ( flags -- XVisualInfo* ) + [ dpy get scr get ] dip [ + % GLX_RGBA , - GLX_DOUBLEBUFFER , GLX_DEPTH_SIZE , 16 , 0 , ] int-array{ } make underlying>> @@ -98,8 +98,8 @@ FUNCTION: void* glXGetProcAddressARB ( char* procname ) ; [ "Could not get a double-buffered GLX RGBA visual" throw ] unless* ; : create-glx ( XVisualInfo* -- GLXContext ) - >r dpy get r> f 1 glXCreateContext + [ dpy get ] dip f 1 glXCreateContext [ "Failed to create GLX context" throw ] unless* ; : destroy-glx ( GLXContext -- ) - dpy get swap glXDestroyContext ; \ No newline at end of file + dpy get swap glXDestroyContext ; diff --git a/basis/x11/windows/windows.factor b/basis/x11/windows/windows.factor index aed45655f6..3c41a78584 100644 --- a/basis/x11/windows/windows.factor +++ b/basis/x11/windows/windows.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2005, 2006 Eduardo Cavazos and Slava Pestov ! See http://factorcode.org/license.txt for BSD license. USING: alien alien.c-types hashtables kernel math math.vectors -math.bitwise namespaces sequences x11.xlib x11.constants x11.glx ; +math.bitwise namespaces sequences x11.xlib x11.constants x11.glx +arrays fry ; IN: x11.windows : create-window-mask ( -- n ) @@ -50,11 +51,30 @@ IN: x11.windows dup r> auto-position ; : glx-window ( loc dim -- window glx ) - choose-visual + GLX_DOUBLEBUFFER 1array choose-visual [ create-window ] keep [ create-glx ] keep XFree ; +: create-pixmap ( dim visual -- pixmap ) + [ [ { 0 0 } swap ] dip create-window ] [ + drop [ dpy get ] 2dip first2 24 XCreatePixmap + [ "Failed to create offscreen pixmap" throw ] unless* + ] 2bi ; + +: (create-glx-pixmap) ( pixmap visual -- pixmap glx-pixmap ) + [ drop ] [ + [ dpy get ] 2dip swap glXCreateGLXPixmap + [ "Failed to create offscreen GLXPixmap" throw ] unless* + ] 2bi ; + +: create-glx-pixmap ( dim visual -- pixmap glx-pixmap ) + [ create-pixmap ] [ (create-glx-pixmap) ] bi ; + +: glx-pixmap ( dim -- glx pixmap glx-pixmap ) + { } choose-visual + [ nip create-glx ] [ create-glx-pixmap ] [ nip XFree ] 2tri ; + : destroy-window ( win -- ) dpy get swap XDestroyWindow drop ; @@ -65,3 +85,7 @@ IN: x11.windows : map-window ( win -- ) dpy get swap XMapWindow drop ; : unmap-window ( win -- ) dpy get swap XUnmapWindow drop ; + +: pixmap-bits ( dim pixmap -- alien ) + swap first2 '[ dpy get _ 0 0 _ _ AllPlanes ZPixmap XGetImage ] call + [ XImage-pixels ] [ XDestroyImage drop ] bi ; diff --git a/basis/x11/xlib/xlib.factor b/basis/x11/xlib/xlib.factor index 555eb573fc..996932e697 100644 --- a/basis/x11/xlib/xlib.factor +++ b/basis/x11/xlib/xlib.factor @@ -272,6 +272,17 @@ FUNCTION: Window XGetSelectionOwner ( Display* display, Atom selection ) ; FUNCTION: int XConvertSelection ( Display* display, Atom selection, Atom target, Atom property, Window requestor, Time time ) ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +! 5 - Pixmap and Cursor Functions +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +! 5.1 - Creating and Freeing Pixmaps + +FUNCTION: Pixmap XCreatePixmap ( Display* display, Drawable d, uint width, uint height, uint depth ) ; +FUNCTION: int XFreePixmap ( Display* display, Pixmap pixmap ) ; + + ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! 6 - Color Management Functions ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -429,6 +440,49 @@ FUNCTION: Status XDrawString ( char* string, int length ) ; +! 8.7 - Transferring Images between Client and Server + +: XYBitmap 0 ; inline +: XYPixmap 1 ; inline +: ZPixmap 2 ; inline +: AllPlanes -1 ; inline + +C-STRUCT: XImage-funcs + { "void*" "create_image" } + { "void*" "destroy_image" } + { "void*" "get_pixel" } + { "void*" "put_pixel" } + { "void*" "sub_image" } + { "void*" "add_pixel" } ; + +C-STRUCT: XImage + { "int" "width" } + { "int" "height" } + { "int" "xoffset" } + { "int" "format" } + { "char*" "data" } + { "int" "byte_order" } + { "int" "bitmap_unit" } + { "int" "bitmap_bit_order" } + { "int" "bitmap_pad" } + { "int" "depth" } + { "int" "bytes_per_line" } + { "int" "bits_per_pixel" } + { "ulong" "red_mask" } + { "ulong" "green_mask" } + { "ulong" "blue_mask" } + { "XPointer" "obdata" } + { "XImage-funcs" "f" } ; + +FUNCTION: XImage* XGetImage ( Display* display, Drawable d, int x, int y, uint width, uint height, ulong plane_mask, int format ) ; +FUNCTION: int XDestroyImage ( XImage *ximage ) ; + +: XImage-size ( ximage -- size ) + [ XImage-height ] [ XImage-bytes_per_line ] bi * ; + +: XImage-pixels ( ximage -- byte-array ) + [ XImage-data ] [ XImage-size ] bi memory>byte-array ; + ! ! 9 - Window and Session Manager Functions ! diff --git a/extra/ui/offscreen/offscreen.factor b/extra/ui/offscreen/offscreen.factor index 33d4a92f37..be6638bed8 100755 --- a/extra/ui/offscreen/offscreen.factor +++ b/extra/ui/offscreen/offscreen.factor @@ -16,7 +16,8 @@ M: offscreen-world ungraft* [ reset-world ] tri ; : open-offscreen ( gadget -- world ) - "" f [ open-world-window ] keep + "" f + [ open-world-window dup relayout-1 ] keep notify-queued ; : close-offscreen ( world -- )