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
11 combinators.short-circuit ;
14 SINGLETON: x11-ui-backend
16 : XA_NET_WM_NAME ( -- atom ) "_NET_WM_NAME" x-atom ;
18 TUPLE: x11-handle-base glx ;
19 TUPLE: x11-handle < x11-handle-base window xic ;
20 TUPLE: x11-pixmap-handle < x11-handle-base pixmap glx-pixmap ;
22 C: <x11-handle> x11-handle
23 C: <x11-pixmap-handle> x11-pixmap-handle
25 M: world expose-event nip relayout ;
27 M: world configure-event
28 swap [ event-loc >>window-loc ] [ event-dim >>dim ] bi
29 ! In case dimensions didn't change
32 PIXEL-FORMAT-ATTRIBUTE-TABLE: glx-visual { $ GLX_USE_GL $ GLX_RGBA } H{
33 { double-buffered { $ GLX_DOUBLEBUFFER } }
34 { stereo { $ GLX_STEREO } }
35 { color-bits { $ GLX_BUFFER_SIZE } }
36 { red-bits { $ GLX_RED_SIZE } }
37 { green-bits { $ GLX_GREEN_SIZE } }
38 { blue-bits { $ GLX_BLUE_SIZE } }
39 { alpha-bits { $ GLX_ALPHA_SIZE } }
40 { accum-red-bits { $ GLX_ACCUM_RED_SIZE } }
41 { accum-green-bits { $ GLX_ACCUM_GREEN_SIZE } }
42 { accum-blue-bits { $ GLX_ACCUM_BLUE_SIZE } }
43 { accum-alpha-bits { $ GLX_ACCUM_ALPHA_SIZE } }
44 { depth-bits { $ GLX_DEPTH_SIZE } }
45 { stencil-bits { $ GLX_STENCIL_SIZE } }
46 { aux-buffers { $ GLX_AUX_BUFFERS } }
47 { sample-buffers { $ GLX_SAMPLE_BUFFERS } }
48 { samples { $ GLX_SAMPLES } }
51 M: x11-ui-backend (make-pixel-format)
52 [ drop dpy get scr get ] dip
53 >glx-visual-int-array glXChooseVisual
54 XVisualInfo memory>struct ;
56 M: x11-ui-backend (free-pixel-format)
59 M: x11-ui-backend (pixel-format-attribute)
61 [ handle>> ] [ >glx-visual ] bi*
64 0 <int> [ glXGetConfig drop ] keep *int
76 { HEX: FF08 "BACKSPACE" }
81 { HEX: FFFF "DELETE" }
87 { HEX: FF55 "PAGE_UP" }
88 { HEX: FF56 "PAGE_DOWN" }
102 : key-code ( keysym -- keycode action? )
103 dup key-codes at [ t ] [ 1string f ] ?if ;
105 : event-modifiers ( event -- seq )
106 state>> modifiers modifier ;
108 : valid-input? ( string gesture -- ? )
109 over empty? [ 2drop f ] [
110 mods>> { f { S+ } } member? [
111 [ { [ 127 = not ] [ CHAR: \s >= ] } 1&& ] all?
113 [ { [ 127 = not ] [ CHAR: \s >= ] [ alpha? not ] } 1&& ] all?
117 : key-down-event>gesture ( event world -- string gesture )
119 handle>> xic>> lookup-string
120 [ swap event-modifiers ] dip key-code <key-down> ;
122 M: world key-down-event
123 [ key-down-event>gesture ] keep
124 [ propagate-key-gesture drop ]
125 [ 2over valid-input? [ nip user-input ] [ 3drop ] if ]
128 : key-up-event>gesture ( event -- gesture )
129 [ event-modifiers ] [ 0 XLookupKeysym key-code ] bi <key-up> ;
131 M: world key-up-event
132 [ key-up-event>gesture ] dip propagate-key-gesture ;
134 : mouse-event>gesture ( event -- modifiers button loc )
135 [ event-modifiers ] [ button>> ] [ event-loc ] tri ;
137 M: world button-down-event
138 [ mouse-event>gesture [ <button-down> ] dip ] dip
141 M: world button-up-event
142 [ mouse-event>gesture [ <button-up> ] dip ] dip
145 : mouse-event>scroll-direction ( event -- pair )
154 [ [ mouse-event>scroll-direction ] [ event-loc ] bi ] dip
157 M: world enter-event motion-event ;
159 M: world leave-event 2drop forget-rollover ;
161 M: world motion-event
162 [ event-loc ] dip move-hand fire-motion ;
164 M: world focus-in-event
165 nip [ handle>> xic>> XSetICFocus ] [ focus-world ] bi ;
167 M: world focus-out-event
168 nip [ handle>> xic>> XUnsetICFocus ] [ unfocus-world ] bi ;
170 M: world selection-notify-event
171 [ handle>> window>> selection-from-event ] keep
174 : supported-type? ( atom -- ? )
175 { "UTF8_STRING" "STRING" "TEXT" }
176 [ x-atom = ] with any? ;
178 : clipboard-for-atom ( atom -- clipboard )
180 { XA_PRIMARY [ selection get ] }
181 { XA_CLIPBOARD [ clipboard get ] }
185 : encode-clipboard ( string type -- bytes )
186 target>> XA_UTF8_STRING = utf8 ascii ? encode ;
188 : set-selection-prop ( evt -- )
192 [ target>> 8 PropModeReplace ] keep
193 [ selection>> clipboard-for-atom contents>> ] keep
194 encode-clipboard dup length XChangeProperty drop ;
196 M: world selection-request-event
198 { [ dup supported-type? ] [ drop dup set-selection-prop send-notify-success ] }
199 { [ dup "TARGETS" x-atom = ] [ drop dup set-targets-prop send-notify-success ] }
200 { [ dup "TIMESTAMP" x-atom = ] [ drop dup set-timestamp-prop send-notify-success ] }
201 [ drop send-notify-failure ]
204 M: x11-ui-backend (close-window) ( handle -- )
206 [ glx>> destroy-glx ]
207 [ window>> [ unregister-window ] [ destroy-window ] bi ]
210 M: world client-event
211 swap close-box? [ ungraft ] [ drop ] if ;
213 : gadget-window ( world -- )
215 [ [ [ window-loc>> ] [ dim>> ] bi ] dip handle>> glx-window ]
216 with-world-pixel-format swap
217 dup "Factor" create-xic
219 [ window>> register-window ] [ >>handle drop ] 2bi ;
221 : wait-event ( -- event )
222 QueuedAfterFlush events-queued 0 > [
224 None XFilterEvent 0 = [ drop wait-event ] unless
225 ] [ wait-for-display wait-event ] if ;
227 M: x11-ui-backend do-events
228 wait-event dup XAnyEvent>> window>> window dup
229 [ handle-event ] [ 2drop ] if ;
231 : x-clipboard@ ( gadget clipboard -- prop win )
233 find-world handle>> window>> ;
235 M: x-clipboard copy-clipboard
236 [ x-clipboard@ own-selection ] keep
239 M: x-clipboard paste-clipboard
240 [ find-world handle>> window>> ] dip atom>> convert-selection ;
242 : init-clipboard ( -- )
243 XA_PRIMARY <x-clipboard> selection set-global
244 XA_CLIPBOARD <x-clipboard> clipboard set-global ;
246 : set-title-old ( dpy window string -- )
247 dup [ 127 <= ] all? [ XStoreName drop ] [ 3drop ] if ;
249 : set-title-new ( dpy window string -- )
250 [ XA_NET_WM_NAME XA_UTF8_STRING 8 PropModeReplace ] dip
251 utf8 encode dup length XChangeProperty drop ;
253 : set-class ( dpy window -- )
254 XA_WM_CLASS XA_UTF8_STRING 8 PropModeReplace "Factor"
255 utf8 encode dup length XChangeProperty drop ;
257 M: x11-ui-backend set-title ( string world -- )
258 handle>> window>> swap
259 [ dpy get ] 2dip [ set-title-old ] [ set-title-new ] 3bi ;
261 : make-fullscreen-msg ( world ? -- msg )
262 XClientMessageEvent <struct>
265 "_NET_WM_STATE" x-atom >>message_type
266 swap _NET_WM_STATE_ADD _NET_WM_STATE_REMOVE ? >>data0
267 swap handle>> window>> >>window
269 "_NET_WM_STATE_FULLSCREEN" x-atom >>data1 ;
271 M: x11-ui-backend (set-fullscreen) ( world ? -- )
272 [ dpy get root get 0 SubstructureNotifyMask ] 2dip
273 make-fullscreen-msg XSendEvent drop ;
275 M: x11-ui-backend (open-window) ( world -- )
278 [ set-closable ] [ dpy get swap set-class ] [ map-window ] tri ;
280 M: x11-ui-backend raise-window* ( world -- )
282 dpy get swap window>>
283 [ RevertToPointerRoot CurrentTime XSetInputFocus drop ]
284 [ XRaiseWindow drop ]
288 M: x11-handle select-gl-context ( handle -- )
290 [ window>> ] [ glx>> ] bi glXMakeCurrent
291 [ "Failed to set current GLX context" throw ] unless ;
293 M: x11-handle flush-gl-context ( handle -- )
294 dpy get swap window>> glXSwapBuffers ;
296 M: x11-pixmap-handle select-gl-context ( handle -- )
298 [ glx-pixmap>> ] [ glx>> ] bi glXMakeCurrent
299 [ "Failed to set current GLX context" throw ] unless ;
301 M: x11-pixmap-handle flush-gl-context ( handle -- )
304 M: x11-ui-backend (open-offscreen-buffer) ( world -- )
305 dup [ [ dim>> ] [ handle>> ] bi* glx-pixmap ] with-world-pixel-format
306 <x11-pixmap-handle> >>handle drop ;
308 M: x11-ui-backend (close-offscreen-buffer) ( handle -- )
310 [ glx-pixmap>> glXDestroyGLXPixmap ]
311 [ pixmap>> XFreePixmap drop ]
312 [ glx>> glXDestroyContext ] 2tri ;
314 M: x11-ui-backend offscreen-pixels ( world -- alien w h )
315 [ [ dim>> ] [ handle>> pixmap>> ] bi pixmap-bits ] [ dim>> first2 ] bi ;
317 M: x11-ui-backend (with-ui) ( quot -- )
328 M: x11-ui-backend beep ( -- )
329 dpy get 100 XBell drop ;
331 x11-ui-backend ui-backend set-global
333 [ "DISPLAY" os-env "ui.tools" "listener" ? ]
334 main-vocab-hook set-global