1 ! Copyright (C) 2005, 2009 Eduardo Cavazos and Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types arrays ascii assocs colors
4 classes.struct combinators io.encodings.ascii
5 io.encodings.string io.encodings.utf8 kernel literals math
6 namespaces sequences strings ui ui.backend ui.clipboards
7 ui.event-loop ui.gadgets ui.gadgets.private ui.gadgets.worlds
8 ui.gestures ui.pixel-formats ui.pixel-formats.private
9 ui.private x11 x11.clipboard x11.constants x11.events x11.glx
10 x11.io x11.windows x11.xim x11.xlib environment command-line ;
13 SINGLETON: x11-ui-backend
15 : XA_NET_WM_NAME ( -- atom ) "_NET_WM_NAME" x-atom ;
17 TUPLE: x11-handle-base glx ;
18 TUPLE: x11-handle < x11-handle-base window xic ;
19 TUPLE: x11-pixmap-handle < x11-handle-base pixmap glx-pixmap ;
21 C: <x11-handle> x11-handle
22 C: <x11-pixmap-handle> x11-pixmap-handle
24 M: world expose-event nip relayout ;
26 M: world configure-event
27 swap [ event-loc >>window-loc ] [ event-dim >>dim ] bi
28 ! In case dimensions didn't change
31 PIXEL-FORMAT-ATTRIBUTE-TABLE: glx-visual { $ GLX_USE_GL $ GLX_RGBA } H{
32 { double-buffered { $ GLX_DOUBLEBUFFER } }
33 { stereo { $ GLX_STEREO } }
34 { color-bits { $ GLX_BUFFER_SIZE } }
35 { red-bits { $ GLX_RED_SIZE } }
36 { green-bits { $ GLX_GREEN_SIZE } }
37 { blue-bits { $ GLX_BLUE_SIZE } }
38 { alpha-bits { $ GLX_ALPHA_SIZE } }
39 { accum-red-bits { $ GLX_ACCUM_RED_SIZE } }
40 { accum-green-bits { $ GLX_ACCUM_GREEN_SIZE } }
41 { accum-blue-bits { $ GLX_ACCUM_BLUE_SIZE } }
42 { accum-alpha-bits { $ GLX_ACCUM_ALPHA_SIZE } }
43 { depth-bits { $ GLX_DEPTH_SIZE } }
44 { stencil-bits { $ GLX_STENCIL_SIZE } }
45 { aux-buffers { $ GLX_AUX_BUFFERS } }
46 { sample-buffers { $ GLX_SAMPLE_BUFFERS } }
47 { samples { $ GLX_SAMPLES } }
50 M: x11-ui-backend (make-pixel-format)
51 [ drop dpy get scr get ] dip
52 >glx-visual-int-array glXChooseVisual
53 XVisualInfo memory>struct ;
55 M: x11-ui-backend (free-pixel-format)
58 M: x11-ui-backend (pixel-format-attribute)
60 [ handle>> ] [ >glx-visual ] bi*
63 0 <int> [ glXGetConfig drop ] keep *int
66 M: x11-ui-backend system-background-color
67 T{ rgba f 0.0 0.0 0.0 0.0 } ; inline
78 { HEX: FF08 "BACKSPACE" }
83 { HEX: FFFF "DELETE" }
89 { HEX: FF55 "PAGE_UP" }
90 { HEX: FF56 "PAGE_DOWN" }
104 : key-code ( keysym -- keycode action? )
105 dup key-codes at [ t ] [ 1string f ] ?if ;
107 : event-modifiers ( event -- seq )
108 state>> modifiers modifier ;
110 : valid-input? ( string gesture -- ? )
111 over empty? [ 2drop f ] [
112 mods>> { f { S+ } } member? [
113 [ [ 127 = not ] [ CHAR: \s >= ] bi and ] all?
115 [ [ 127 = not ] [ CHAR: \s >= ] [ alpha? not ] tri and and ] all?
119 : key-down-event>gesture ( event world -- string gesture )
121 handle>> xic>> lookup-string
122 [ swap event-modifiers ] dip key-code <key-down> ;
124 M: world key-down-event
125 [ key-down-event>gesture ] keep
126 [ propagate-key-gesture drop ]
127 [ 2over valid-input? [ nip user-input ] [ 3drop ] if ]
130 : key-up-event>gesture ( event -- gesture )
131 [ event-modifiers ] [ 0 XLookupKeysym key-code ] bi <key-up> ;
133 M: world key-up-event
134 [ key-up-event>gesture ] dip propagate-key-gesture ;
136 : mouse-event>gesture ( event -- modifiers button loc )
137 [ event-modifiers ] [ button>> ] [ event-loc ] tri ;
139 M: world button-down-event
140 [ mouse-event>gesture [ <button-down> ] dip ] dip
143 M: world button-up-event
144 [ mouse-event>gesture [ <button-up> ] dip ] dip
147 : mouse-event>scroll-direction ( event -- pair )
156 [ [ mouse-event>scroll-direction ] [ event-loc ] bi ] dip
159 M: world enter-event motion-event ;
161 M: world leave-event 2drop forget-rollover ;
163 M: world motion-event
164 [ event-loc ] dip move-hand fire-motion ;
166 M: world focus-in-event
167 nip [ handle>> xic>> XSetICFocus ] [ focus-world ] bi ;
169 M: world focus-out-event
170 nip [ handle>> xic>> XUnsetICFocus ] [ unfocus-world ] bi ;
172 M: world selection-notify-event
173 [ handle>> window>> selection-from-event ] keep
176 : supported-type? ( atom -- ? )
177 { "UTF8_STRING" "STRING" "TEXT" }
178 [ x-atom = ] with any? ;
180 : clipboard-for-atom ( atom -- clipboard )
182 { XA_PRIMARY [ selection get ] }
183 { XA_CLIPBOARD [ clipboard get ] }
187 : encode-clipboard ( string type -- bytes )
188 target>> XA_UTF8_STRING = utf8 ascii ? encode ;
190 : set-selection-prop ( evt -- )
194 [ target>> 8 PropModeReplace ] keep
195 [ selection>> clipboard-for-atom contents>> ] keep
196 encode-clipboard dup length XChangeProperty drop ;
198 M: world selection-request-event
200 { [ dup supported-type? ] [ drop dup set-selection-prop send-notify-success ] }
201 { [ dup "TARGETS" x-atom = ] [ drop dup set-targets-prop send-notify-success ] }
202 { [ dup "TIMESTAMP" x-atom = ] [ drop dup set-timestamp-prop send-notify-success ] }
203 [ drop send-notify-failure ]
206 M: x11-ui-backend (close-window) ( handle -- )
208 [ glx>> destroy-glx ]
209 [ window>> [ unregister-window ] [ destroy-window ] bi ]
212 M: world client-event
213 swap close-box? [ ungraft ] [ drop ] if ;
215 : gadget-window ( world -- )
217 [ [ [ window-loc>> ] [ dim>> ] bi ] dip handle>> glx-window ]
218 with-world-pixel-format swap
219 dup "Factor" create-xic
221 [ window>> register-window ] [ >>handle drop ] 2bi ;
223 : wait-event ( -- event )
224 QueuedAfterFlush events-queued 0 > [
226 None XFilterEvent 0 = [ drop wait-event ] unless
227 ] [ wait-for-display wait-event ] if ;
229 M: x11-ui-backend do-events
230 wait-event dup XAnyEvent>> window>> window dup
231 [ handle-event ] [ 2drop ] if ;
233 : x-clipboard@ ( gadget clipboard -- prop win )
235 find-world handle>> window>> ;
237 M: x-clipboard copy-clipboard
238 [ x-clipboard@ own-selection ] keep
241 M: x-clipboard paste-clipboard
242 [ find-world handle>> window>> ] dip atom>> convert-selection ;
244 : init-clipboard ( -- )
245 XA_PRIMARY <x-clipboard> selection set-global
246 XA_CLIPBOARD <x-clipboard> clipboard set-global ;
248 : set-title-old ( dpy window string -- )
249 dup [ 127 <= ] all? [ XStoreName drop ] [ 3drop ] if ;
251 : set-title-new ( dpy window string -- )
252 [ XA_NET_WM_NAME XA_UTF8_STRING 8 PropModeReplace ] dip
253 utf8 encode dup length XChangeProperty drop ;
255 : set-class ( dpy window -- )
256 XA_WM_CLASS XA_UTF8_STRING 8 PropModeReplace "Factor"
257 utf8 encode dup length XChangeProperty drop ;
259 M: x11-ui-backend set-title ( string world -- )
260 handle>> window>> swap
261 [ dpy get ] 2dip [ set-title-old ] [ set-title-new ] 3bi ;
263 : make-fullscreen-msg ( world ? -- msg )
264 XClientMessageEvent <struct>
267 "_NET_WM_STATE" x-atom >>message_type
268 swap _NET_WM_STATE_ADD _NET_WM_STATE_REMOVE ? >>data0
269 swap handle>> window>> >>window
271 "_NET_WM_STATE_FULLSCREEN" x-atom >>data1 ;
273 M: x11-ui-backend (set-fullscreen) ( world ? -- )
274 [ dpy get root get 0 SubstructureNotifyMask ] 2dip
275 make-fullscreen-msg XSendEvent drop ;
277 M: x11-ui-backend (open-window) ( world -- )
280 [ set-closable ] [ dpy get swap set-class ] [ map-window ] tri ;
282 M: x11-ui-backend raise-window* ( world -- )
284 dpy get swap window>>
285 [ RevertToPointerRoot CurrentTime XSetInputFocus drop ]
286 [ XRaiseWindow drop ]
290 M: x11-handle select-gl-context ( handle -- )
292 [ window>> ] [ glx>> ] bi glXMakeCurrent
293 [ "Failed to set current GLX context" throw ] unless ;
295 M: x11-handle flush-gl-context ( handle -- )
296 dpy get swap window>> glXSwapBuffers ;
298 M: x11-pixmap-handle select-gl-context ( handle -- )
300 [ glx-pixmap>> ] [ glx>> ] bi glXMakeCurrent
301 [ "Failed to set current GLX context" throw ] unless ;
303 M: x11-pixmap-handle flush-gl-context ( handle -- )
306 M: x11-ui-backend (open-offscreen-buffer) ( world -- )
307 dup [ [ dim>> ] [ handle>> ] bi* glx-pixmap ] with-world-pixel-format
308 <x11-pixmap-handle> >>handle drop ;
310 M: x11-ui-backend (close-offscreen-buffer) ( handle -- )
312 [ glx-pixmap>> glXDestroyGLXPixmap ]
313 [ pixmap>> XFreePixmap drop ]
314 [ glx>> glXDestroyContext ] 2tri ;
316 M: x11-ui-backend offscreen-pixels ( world -- alien w h )
317 [ [ dim>> ] [ handle>> pixmap>> ] bi pixmap-bits ] [ dim>> first2 ] bi ;
319 M: x11-ui-backend (with-ui) ( quot -- )
330 M: x11-ui-backend beep ( -- )
331 dpy get 100 XBell drop ;
333 x11-ui-backend ui-backend set-global
335 [ "DISPLAY" os-env "ui.tools" "listener" ? ]
336 main-vocab-hook set-global