factor/basis/ui/backend/x11/x11.factor

361 lines
11 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2010 Eduardo Cavazos and Slava Pestov
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types ascii assocs classes.struct combinators
combinators.short-circuit command-line environment io.encodings.ascii
io.encodings.string io.encodings.utf8 kernel literals locals math
namespaces sequences specialized-arrays.instances.alien.c-types.uchar
strings ui ui.backend ui.clipboards ui.event-loop ui.gadgets
ui.gadgets.private ui.gadgets.worlds ui.gestures ui.pixel-formats
ui.pixel-formats.private ui.private x11 x11.clipboard x11.constants
x11.events x11.glx x11.io x11.windows x11.xim x11.xlib ;
FROM: unix.ffi => system ;
IN: ui.backend.x11
2007-09-20 18:09:08 -04:00
2008-04-02 20:44:01 -04:00
SINGLETON: x11-ui-backend
2007-09-20 18:09:08 -04:00
2008-06-11 22:14:20 -04:00
: XA_NET_WM_NAME ( -- atom ) "_NET_WM_NAME" x-atom ;
2007-09-20 18:09:08 -04:00
2008-12-11 19:50:37 -05:00
TUPLE: x11-handle-base glx ;
2009-03-02 03:55:54 -05:00
TUPLE: x11-handle < x11-handle-base window xic ;
2008-12-11 19:50:37 -05:00
TUPLE: x11-pixmap-handle < x11-handle-base pixmap glx-pixmap ;
2007-09-20 18:09:08 -04:00
C: <x11-handle> x11-handle
2008-12-11 19:50:37 -05:00
C: <x11-pixmap-handle> x11-pixmap-handle
2007-09-20 18:09:08 -04:00
M: world expose-event nip relayout ;
M: world configure-event
swap [ event-loc >>window-loc ] [ event-dim >>dim ] bi
2007-09-20 18:09:08 -04:00
! In case dimensions didn't change
relayout-1 ;
2009-05-02 17:39:31 -04:00
PIXEL-FORMAT-ATTRIBUTE-TABLE: glx-visual { $ GLX_USE_GL $ GLX_RGBA } H{
{ double-buffered { $ GLX_DOUBLEBUFFER } }
{ stereo { $ GLX_STEREO } }
{ color-bits { $ GLX_BUFFER_SIZE } }
{ red-bits { $ GLX_RED_SIZE } }
{ green-bits { $ GLX_GREEN_SIZE } }
{ blue-bits { $ GLX_BLUE_SIZE } }
{ alpha-bits { $ GLX_ALPHA_SIZE } }
{ accum-red-bits { $ GLX_ACCUM_RED_SIZE } }
{ accum-green-bits { $ GLX_ACCUM_GREEN_SIZE } }
{ accum-blue-bits { $ GLX_ACCUM_BLUE_SIZE } }
{ accum-alpha-bits { $ GLX_ACCUM_ALPHA_SIZE } }
{ depth-bits { $ GLX_DEPTH_SIZE } }
{ stencil-bits { $ GLX_STENCIL_SIZE } }
{ aux-buffers { $ GLX_AUX_BUFFERS } }
{ sample-buffers { $ GLX_SAMPLE_BUFFERS } }
{ samples { $ GLX_SAMPLES } }
}
M: x11-ui-backend (make-pixel-format)
[ drop dpy get scr get ] dip
>glx-visual-int-array glXChooseVisual ;
2009-05-02 17:39:31 -04:00
M: x11-ui-backend (free-pixel-format)
handle>> XFree ;
M: x11-ui-backend (pixel-format-attribute)
[ dpy get ] 2dip
[ handle>> ] [ >glx-visual ] bi*
2009-05-02 22:55:19 -04:00
[ 2drop f ] [
first
2009-05-02 17:39:31 -04:00
0 <int> [ glXGetConfig drop ] keep *int
] if-empty ;
CONSTANT: modifiers
2007-09-20 18:09:08 -04:00
{
{ S+ HEX: 1 }
{ C+ HEX: 4 }
{ A+ HEX: 8 }
}
CONSTANT: key-codes
2007-09-20 18:09:08 -04:00
H{
{ HEX: FF08 "BACKSPACE" }
{ HEX: FF09 "TAB" }
{ HEX: FF0D "RET" }
{ HEX: FF8D "ENTER" }
{ HEX: FF1B "ESC" }
{ HEX: FFFF "DELETE" }
{ HEX: FF50 "HOME" }
{ HEX: FF51 "LEFT" }
{ HEX: FF52 "UP" }
{ HEX: FF53 "RIGHT" }
{ HEX: FF54 "DOWN" }
{ HEX: FF55 "PAGE_UP" }
{ HEX: FF56 "PAGE_DOWN" }
{ HEX: FF57 "END" }
{ HEX: FF58 "BEGIN" }
{ HEX: FFBE "F1" }
{ HEX: FFBF "F2" }
{ HEX: FFC0 "F3" }
{ HEX: FFC1 "F4" }
{ HEX: FFC2 "F5" }
{ HEX: FFC3 "F6" }
{ HEX: FFC4 "F7" }
{ HEX: FFC5 "F8" }
{ HEX: FFC6 "F9" }
}
2007-09-20 18:09:08 -04:00
: key-code ( keysym -- keycode action? )
dup key-codes at [ t ] [ 1string f ] ?if ;
: event-modifiers ( event -- seq )
state>> modifiers modifier ;
2007-09-20 18:09:08 -04:00
2008-11-22 01:23:56 -05:00
: valid-input? ( string gesture -- ? )
over empty? [ 2drop f ] [
mods>> { f { S+ } } member? [
[ { [ 127 = not ] [ CHAR: \s >= ] } 1&& ] all?
2008-11-22 01:23:56 -05:00
] [
[ { [ 127 = not ] [ CHAR: \s >= ] [ alpha? not ] } 1&& ] all?
2008-11-22 01:23:56 -05:00
] if
] if ;
2007-09-20 18:09:08 -04:00
: key-down-event>gesture ( event world -- string gesture )
dupd
2008-09-01 19:29:04 -04:00
handle>> xic>> lookup-string
[ swap event-modifiers ] dip key-code <key-down> ;
2007-09-20 18:09:08 -04:00
M: world key-down-event
[ key-down-event>gesture ] keep
[ propagate-key-gesture drop ]
2008-11-22 01:23:56 -05:00
[ 2over valid-input? [ nip user-input ] [ 3drop ] if ]
3bi ;
2007-09-20 18:09:08 -04:00
: key-up-event>gesture ( event -- gesture )
[ event-modifiers ] [ 0 XLookupKeysym key-code ] bi <key-up> ;
2007-09-20 18:09:08 -04:00
M: world key-up-event
[ key-up-event>gesture ] dip propagate-key-gesture ;
2007-09-20 18:09:08 -04:00
: mouse-event>gesture ( event -- modifiers button loc )
[ event-modifiers ] [ button>> ] [ event-loc ] tri ;
2007-09-20 18:09:08 -04:00
M: world button-down-event
[ mouse-event>gesture [ <button-down> ] dip ] dip
2007-09-20 18:09:08 -04:00
send-button-down ;
M: world button-up-event
[ mouse-event>gesture [ <button-up> ] dip ] dip
2007-09-20 18:09:08 -04:00
send-button-up ;
: mouse-event>scroll-direction ( event -- pair )
button>> {
2007-10-10 02:16:42 -04:00
{ 4 { 0 -1 } }
{ 5 { 0 1 } }
{ 6 { -1 0 } }
{ 7 { 1 0 } }
} at ;
2007-09-20 18:09:08 -04:00
2010-04-29 03:59:31 -04:00
M: world scroll-event
[ [ mouse-event>scroll-direction ] [ event-loc ] bi ] dip
send-scroll ;
2007-09-20 18:09:08 -04:00
M: world enter-event motion-event ;
M: world leave-event 2drop forget-rollover ;
M: world motion-event
[ event-loc ] dip move-hand fire-motion ;
2007-09-20 18:09:08 -04:00
M: world focus-in-event
nip [ handle>> xic>> XSetICFocus ] [ focus-world ] bi ;
2007-09-20 18:09:08 -04:00
M: world focus-out-event
nip [ handle>> xic>> XUnsetICFocus ] [ unfocus-world ] bi ;
2007-09-20 18:09:08 -04:00
M: world selection-notify-event
2008-09-01 19:29:04 -04:00
[ handle>> window>> selection-from-event ] keep
2008-12-10 18:33:29 -05:00
user-input ;
2007-09-20 18:09:08 -04:00
: supported-type? ( atom -- ? )
{ "UTF8_STRING" "STRING" "TEXT" }
[ x-atom = ] with any? ;
2007-09-20 18:09:08 -04:00
: clipboard-for-atom ( atom -- clipboard )
{
{ XA_PRIMARY [ selection get ] }
{ XA_CLIPBOARD [ clipboard get ] }
2008-04-11 13:54:33 -04:00
[ drop <clipboard> ]
} case ;
2007-09-20 18:09:08 -04:00
: encode-clipboard ( string type -- bytes )
target>> XA_UTF8_STRING = utf8 ascii ? encode ;
2007-09-20 18:09:08 -04:00
: set-selection-prop ( evt -- )
dpy get swap
[ requestor>> ] keep
[ property>> ] keep
[ target>> 8 PropModeReplace ] keep
[ selection>> clipboard-for-atom contents>> ] keep
encode-clipboard dup length XChangeProperty drop ;
2007-09-20 18:09:08 -04:00
M: world selection-request-event
drop dup target>> {
2007-09-20 18:09:08 -04:00
{ [ dup supported-type? ] [ drop dup set-selection-prop send-notify-success ] }
{ [ dup "TARGETS" x-atom = ] [ drop dup set-targets-prop send-notify-success ] }
{ [ dup "TIMESTAMP" x-atom = ] [ drop dup set-timestamp-prop send-notify-success ] }
2008-04-11 13:54:33 -04:00
[ drop send-notify-failure ]
2007-09-20 18:09:08 -04:00
} cond ;
2007-11-24 16:29:13 -05:00
M: x11-ui-backend (close-window) ( handle -- )
2009-01-26 17:30:17 -05:00
[ xic>> XDestroyIC ]
[ glx>> destroy-glx ]
[ window>> [ unregister-window ] [ destroy-window ] bi ]
tri ;
2007-09-20 18:09:08 -04:00
M: world client-event
2007-11-24 16:29:13 -05:00
swap close-box? [ ungraft ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: gadget-window ( world -- )
2009-03-02 03:55:54 -05:00
dup
2009-05-02 17:39:31 -04:00
[ [ [ window-loc>> ] [ dim>> ] bi ] dip handle>> glx-window ]
with-world-pixel-format swap
2009-03-02 03:55:54 -05:00
dup "Factor" create-xic
<x11-handle>
2009-01-26 17:30:17 -05:00
[ window>> register-window ] [ >>handle drop ] 2bi ;
2007-09-20 18:09:08 -04:00
: wait-event ( -- event )
QueuedAfterFlush events-queued 0 > [
next-event dup
2009-01-26 17:30:17 -05:00
None XFilterEvent 0 = [ drop wait-event ] unless
] [ wait-for-display wait-event ] if ;
2007-09-20 18:09:08 -04:00
2008-05-08 17:58:13 -04:00
M: x11-ui-backend do-events
wait-event dup XAnyEvent>> window>> window dup
[ handle-event ] [ 2drop ] if ;
2007-09-20 18:09:08 -04:00
: x-clipboard@ ( gadget clipboard -- prop win )
2008-09-02 04:00:25 -04:00
atom>> swap
2008-09-01 19:29:04 -04:00
find-world handle>> window>> ;
2007-09-20 18:09:08 -04:00
M: x-clipboard copy-clipboard
[ x-clipboard@ own-selection ] keep
2008-09-02 10:00:37 -04:00
(>>contents) ;
2007-09-20 18:09:08 -04:00
M: x-clipboard paste-clipboard
[ find-world handle>> window>> ] dip atom>> convert-selection ;
2007-09-20 18:09:08 -04:00
: init-clipboard ( -- )
XA_PRIMARY <x-clipboard> selection set-global
XA_CLIPBOARD <x-clipboard> clipboard set-global ;
: set-title-old ( dpy window string -- )
dup [ 127 <= ] all? [ XStoreName drop ] [ 3drop ] if ;
: set-title-new ( dpy window string -- )
[ XA_NET_WM_NAME XA_UTF8_STRING 8 PropModeReplace ] dip
utf8 encode dup length XChangeProperty drop ;
2007-09-20 18:09:08 -04:00
: set-class ( dpy window -- )
XA_WM_CLASS XA_UTF8_STRING 8 PropModeReplace "Factor"
utf8 encode dup length XChangeProperty drop ;
2007-09-20 18:09:08 -04:00
M: x11-ui-backend set-title ( string world -- )
handle>> window>> swap
[ dpy get ] 2dip [ set-title-old ] [ set-title-new ] 3bi ;
2009-09-04 12:35:45 -04:00
: make-fullscreen-msg ( world ? -- msg )
XClientMessageEvent <struct>
2009-09-04 12:35:45 -04:00
ClientMessage >>type
dpy get >>display
"_NET_WM_STATE" x-atom >>message_type
2009-09-04 12:35:45 -04:00
swap _NET_WM_STATE_ADD _NET_WM_STATE_REMOVE ? >>data0
swap handle>> window>> >>window
32 >>format
2009-09-04 12:35:45 -04:00
"_NET_WM_STATE_FULLSCREEN" x-atom >>data1 ;
M: x11-ui-backend (set-fullscreen) ( world ? -- )
[ dpy get root get 0 SubstructureNotifyMask ] 2dip
make-fullscreen-msg XSendEvent drop ;
2007-09-20 18:09:08 -04:00
2007-11-24 16:29:13 -05:00
M: x11-ui-backend (open-window) ( world -- )
2007-09-20 18:09:08 -04:00
dup gadget-window
handle>> window>>
[ set-closable ] [ dpy get swap set-class ] [ map-window ] tri ;
2007-09-20 18:09:08 -04:00
2008-02-21 00:13:31 -05:00
M: x11-ui-backend raise-window* ( world -- )
2008-08-31 02:42:30 -04:00
handle>> [
dpy get swap window>>
[ RevertToPointerRoot CurrentTime XSetInputFocus drop ]
[ XRaiseWindow drop ]
2bi
2007-09-20 18:09:08 -04:00
] when* ;
M: x11-handle select-gl-context ( handle -- )
2007-09-20 18:09:08 -04:00
dpy get swap
2008-12-11 19:50:37 -05:00
[ window>> ] [ glx>> ] bi glXMakeCurrent
2007-09-20 18:09:08 -04:00
[ "Failed to set current GLX context" throw ] unless ;
M: x11-handle flush-gl-context ( handle -- )
2008-09-01 19:29:04 -04:00
dpy get swap window>> glXSwapBuffers ;
2007-09-20 18:09:08 -04:00
2008-12-11 19:50:37 -05:00
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>> ] [ handle>> ] bi* glx-pixmap ] with-world-pixel-format
2009-05-02 17:39:31 -04:00
<x11-pixmap-handle> >>handle drop ;
2008-12-11 19:50:37 -05:00
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 (with-ui) ( quot -- )
2007-09-20 18:09:08 -04:00
[
f [
[
init-clipboard
start-ui
event-loop
] with-xim
] with-x
] ui-running ;
M: x11-ui-backend beep ( -- )
2008-06-11 22:22:35 -04:00
dpy get 100 XBell drop ;
<PRIVATE
: escape-' ( string -- string' )
2010-02-24 03:51:02 -05:00
[ dup CHAR: ' = [ drop "'\\''" ] [ 1string ] if ] { } map-as concat ;
: xmessage ( string -- )
escape-' "/usr/bin/env xmessage '" "'" surround system drop ;
PRIVATE>
M: x11-ui-backend system-alert
"\n\n" glue xmessage ;
: black ( -- xcolor ) 0 0 0 0 0 0 XColor <struct-boa> ; inline
M:: x11-ui-backend (grab-input) ( handle -- )
handle window>> :> wnd
dpy get :> dpy
dpy wnd uchar-array{ 0 0 0 0 0 0 0 0 } 8 8 XCreateBitmapFromData :> pixmap
dpy pixmap dup black dup 0 0 XCreatePixmapCursor :> cursor
dpy wnd 1 NoEventMask GrabModeAsync dup wnd cursor CurrentTime XGrabPointer drop
dpy cursor XFreeCursor drop
dpy pixmap XFreePixmap drop ;
M: x11-ui-backend (ungrab-input)
drop dpy get CurrentTime XUngrabPointer drop ;
2008-04-02 20:44:01 -04:00
x11-ui-backend ui-backend set-global
2007-09-20 18:09:08 -04:00
[ "DISPLAY" os-env "ui.tools" "listener" ? ]
2007-09-20 18:09:08 -04:00
main-vocab-hook set-global