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
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
75 { HEX: FF08 "BACKSPACE" }
80 { HEX: FFFF "DELETE" }
86 { HEX: FF55 "PAGE_UP" }
87 { HEX: FF56 "PAGE_DOWN" }
101 : key-code ( keysym -- keycode action? )
102 dup key-codes at [ t ] [ 1string f ] ?if ;
104 : event-modifiers ( event -- seq )
105 state>> modifiers modifier ;
107 : valid-input? ( string gesture -- ? )
108 over empty? [ 2drop f ] [
109 mods>> { f { S+ } } member? [
110 [ [ 127 = not ] [ CHAR: \s >= ] bi and ] all?
112 [ [ 127 = not ] [ CHAR: \s >= ] [ alpha? not ] tri and and ] all?
116 : key-down-event>gesture ( event world -- string gesture )
118 handle>> xic>> lookup-string
119 [ swap event-modifiers ] dip key-code <key-down> ;
121 M: world key-down-event
122 [ key-down-event>gesture ] keep
123 [ propagate-key-gesture drop ]
124 [ 2over valid-input? [ nip user-input ] [ 3drop ] if ]
127 : key-up-event>gesture ( event -- gesture )
128 [ event-modifiers ] [ 0 XLookupKeysym key-code ] bi <key-up> ;
130 M: world key-up-event
131 [ key-up-event>gesture ] dip propagate-key-gesture ;
133 : mouse-event>gesture ( event -- modifiers button loc )
134 [ event-modifiers ] [ button>> ] [ event-loc ] tri ;
136 M: world button-down-event
137 [ mouse-event>gesture [ <button-down> ] dip ] dip
140 M: world button-up-event
141 [ mouse-event>gesture [ <button-up> ] dip ] dip
144 : mouse-event>scroll-direction ( event -- pair )
153 [ [ mouse-event>scroll-direction ] [ event-loc ] bi ] dip
156 M: world enter-event motion-event ;
158 M: world leave-event 2drop forget-rollover ;
160 M: world motion-event
161 [ event-loc ] dip move-hand fire-motion ;
163 M: world focus-in-event
164 nip [ handle>> xic>> XSetICFocus ] [ focus-world ] bi ;
166 M: world focus-out-event
167 nip [ handle>> xic>> XUnsetICFocus ] [ unfocus-world ] bi ;
169 M: world selection-notify-event
170 [ handle>> window>> selection-from-event ] keep
173 : supported-type? ( atom -- ? )
174 { "UTF8_STRING" "STRING" "TEXT" }
175 [ x-atom = ] with any? ;
177 : clipboard-for-atom ( atom -- clipboard )
179 { XA_PRIMARY [ selection get ] }
180 { XA_CLIPBOARD [ clipboard get ] }
184 : encode-clipboard ( string type -- bytes )
185 target>> XA_UTF8_STRING = utf8 ascii ? encode ;
187 : set-selection-prop ( evt -- )
191 [ target>> 8 PropModeReplace ] keep
192 [ selection>> clipboard-for-atom contents>> ] keep
193 encode-clipboard dup length XChangeProperty drop ;
195 M: world selection-request-event
197 { [ dup supported-type? ] [ drop dup set-selection-prop send-notify-success ] }
198 { [ dup "TARGETS" x-atom = ] [ drop dup set-targets-prop send-notify-success ] }
199 { [ dup "TIMESTAMP" x-atom = ] [ drop dup set-timestamp-prop send-notify-success ] }
200 [ drop send-notify-failure ]
203 M: x11-ui-backend (close-window) ( handle -- )
205 [ glx>> destroy-glx ]
206 [ window>> [ unregister-window ] [ destroy-window ] bi ]
209 M: world client-event
210 swap close-box? [ ungraft ] [ drop ] if ;
212 : gadget-window ( world -- )
214 [ [ [ window-loc>> ] [ dim>> ] bi ] dip handle>> glx-window ]
215 with-world-pixel-format swap
216 dup "Factor" create-xic
218 [ window>> register-window ] [ >>handle drop ] 2bi ;
220 : wait-event ( -- event )
221 QueuedAfterFlush events-queued 0 > [
223 None XFilterEvent 0 = [ drop wait-event ] unless
224 ] [ wait-for-display wait-event ] if ;
226 M: x11-ui-backend do-events
227 wait-event dup XAnyEvent>> window>> window dup
228 [ handle-event ] [ 2drop ] if ;
230 : x-clipboard@ ( gadget clipboard -- prop win )
232 find-world handle>> window>> ;
234 M: x-clipboard copy-clipboard
235 [ x-clipboard@ own-selection ] keep
238 M: x-clipboard paste-clipboard
239 [ find-world handle>> window>> ] dip atom>> convert-selection ;
241 : init-clipboard ( -- )
242 XA_PRIMARY <x-clipboard> selection set-global
243 XA_CLIPBOARD <x-clipboard> clipboard set-global ;
245 : set-title-old ( dpy window string -- )
246 dup [ 127 <= ] all? [ XStoreName drop ] [ 3drop ] if ;
248 : set-title-new ( dpy window string -- )
249 [ XA_NET_WM_NAME XA_UTF8_STRING 8 PropModeReplace ] dip
250 utf8 encode dup length XChangeProperty drop ;
252 : set-class ( dpy window -- )
253 XA_WM_CLASS XA_UTF8_STRING 8 PropModeReplace "Factor"
254 utf8 encode dup length XChangeProperty drop ;
256 M: x11-ui-backend set-title ( string world -- )
257 handle>> window>> swap
258 [ dpy get ] 2dip [ set-title-old ] [ set-title-new ] 3bi ;
260 M: x11-ui-backend (set-fullscreen) ( world ? -- )
261 XClientMessageEvent <struct>
262 swap _NET_WM_STATE_ADD _NET_WM_STATE_REMOVE ? >>data0
263 swap handle>> window>> >>window
265 "_NET_WM_STATE" x-atom >>message_type
267 "_NET_WM_STATE_FULLSCREEN" x-atom >>data1
268 [ dpy get root get 0 SubstructureNotifyMask ] dip XSendEvent drop ;
270 M: x11-ui-backend (open-window) ( world -- )
273 [ set-closable ] [ dpy get swap set-class ] [ map-window ] tri ;
275 M: x11-ui-backend raise-window* ( world -- )
277 dpy get swap window>>
278 [ RevertToPointerRoot CurrentTime XSetInputFocus drop ]
279 [ XRaiseWindow drop ]
283 M: x11-handle select-gl-context ( handle -- )
285 [ window>> ] [ glx>> ] bi glXMakeCurrent
286 [ "Failed to set current GLX context" throw ] unless ;
288 M: x11-handle flush-gl-context ( handle -- )
289 dpy get swap window>> glXSwapBuffers ;
291 M: x11-pixmap-handle select-gl-context ( handle -- )
293 [ glx-pixmap>> ] [ glx>> ] bi glXMakeCurrent
294 [ "Failed to set current GLX context" throw ] unless ;
296 M: x11-pixmap-handle flush-gl-context ( handle -- )
299 M: x11-ui-backend (open-offscreen-buffer) ( world -- )
300 dup [ [ dim>> ] [ handle>> ] bi* glx-pixmap ] with-world-pixel-format
301 <x11-pixmap-handle> >>handle drop ;
303 M: x11-ui-backend (close-offscreen-buffer) ( handle -- )
305 [ glx-pixmap>> glXDestroyGLXPixmap ]
306 [ pixmap>> XFreePixmap drop ]
307 [ glx>> glXDestroyContext ] 2tri ;
309 M: x11-ui-backend offscreen-pixels ( world -- alien w h )
310 [ [ dim>> ] [ handle>> pixmap>> ] bi pixmap-bits ] [ dim>> first2 ] bi ;
312 M: x11-ui-backend (with-ui) ( quot -- )
323 M: x11-ui-backend beep ( -- )
324 dpy get 100 XBell drop ;
326 x11-ui-backend ui-backend set-global
328 [ "DISPLAY" os-env "ui.tools" "listener" ? ]
329 main-vocab-hook set-global