]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/x11/x11.factor
4c977f17a4110a00a2c3b0a770a771a18b13da86
[factor.git] / basis / ui / backend / x11 / x11.factor
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 ;
12 IN: ui.backend.x11
13
14 SINGLETON: x11-ui-backend
15
16 : XA_NET_WM_NAME ( -- atom ) "_NET_WM_NAME" x-atom ;
17
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 ;
21
22 C: <x11-handle> x11-handle
23 C: <x11-pixmap-handle> x11-pixmap-handle
24
25 M: world expose-event nip relayout ;
26
27 M: world configure-event
28     swap [ event-loc >>window-loc ] [ event-dim >>dim ] bi
29     ! In case dimensions didn't change
30     relayout-1 ;
31
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 } }
49 }
50
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 ;
55
56 M: x11-ui-backend (free-pixel-format)
57     handle>> XFree ;
58
59 M: x11-ui-backend (pixel-format-attribute)
60     [ dpy get ] 2dip
61     [ handle>> ] [ >glx-visual ] bi*
62     [ 2drop f ] [
63         first
64         0 <int> [ glXGetConfig drop ] keep *int
65     ] if-empty ;
66
67 CONSTANT: modifiers
68     {
69         { S+ HEX: 1 }
70         { C+ HEX: 4 }
71         { A+ HEX: 8 }
72     }
73
74 CONSTANT: key-codes
75     H{
76         { HEX: FF08 "BACKSPACE" }
77         { HEX: FF09 "TAB"       }
78         { HEX: FF0D "RET"       }
79         { HEX: FF8D "ENTER"     }
80         { HEX: FF1B "ESC"       }
81         { HEX: FFFF "DELETE"    }
82         { HEX: FF50 "HOME"      }
83         { HEX: FF51 "LEFT"      }
84         { HEX: FF52 "UP"        }
85         { HEX: FF53 "RIGHT"     }
86         { HEX: FF54 "DOWN"      }
87         { HEX: FF55 "PAGE_UP"   }
88         { HEX: FF56 "PAGE_DOWN" }
89         { HEX: FF57 "END"       }
90         { HEX: FF58 "BEGIN"     }
91         { HEX: FFBE "F1"        }
92         { HEX: FFBF "F2"        }
93         { HEX: FFC0 "F3"        }
94         { HEX: FFC1 "F4"        }
95         { HEX: FFC2 "F5"        }
96         { HEX: FFC3 "F6"        }
97         { HEX: FFC4 "F7"        }
98         { HEX: FFC5 "F8"        }
99         { HEX: FFC6 "F9"        }
100     }
101
102 : key-code ( keysym -- keycode action? )
103     dup key-codes at [ t ] [ 1string f ] ?if ;
104
105 : event-modifiers ( event -- seq )
106     state>> modifiers modifier ;
107
108 : valid-input? ( string gesture -- ? )
109     over empty? [ 2drop f ] [
110         mods>> { f { S+ } } member? [
111             [ { [ 127 = not ] [ CHAR: \s >= ] } 1&& ] all?
112         ] [
113             [ { [ 127 = not ] [ CHAR: \s >= ] [ alpha? not ] } 1&& ] all?
114         ] if
115     ] if ;
116
117 : key-down-event>gesture ( event world -- string gesture )
118     dupd
119     handle>> xic>> lookup-string
120     [ swap event-modifiers ] dip key-code <key-down> ;
121
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 ]
126     3bi ;
127
128 : key-up-event>gesture ( event -- gesture )
129     [ event-modifiers ] [ 0 XLookupKeysym key-code ] bi <key-up> ;
130
131 M: world key-up-event
132     [ key-up-event>gesture ] dip propagate-key-gesture ;
133
134 : mouse-event>gesture ( event -- modifiers button loc )
135     [ event-modifiers ] [ button>> ] [ event-loc ] tri ;
136
137 M: world button-down-event
138     [ mouse-event>gesture [ <button-down> ] dip ] dip
139     send-button-down ;
140
141 M: world button-up-event
142     [ mouse-event>gesture [ <button-up> ] dip ] dip
143     send-button-up ;
144
145 : mouse-event>scroll-direction ( event -- pair )
146     button>> {
147         { 4 { 0 -1 } }
148         { 5 { 0 1 } }
149         { 6 { -1 0 } }
150         { 7 { 1 0 } }
151     } at ;
152
153 M: world wheel-event
154     [ [ mouse-event>scroll-direction ] [ event-loc ] bi ] dip
155     send-wheel ;
156
157 M: world enter-event motion-event ;
158
159 M: world leave-event 2drop forget-rollover ;
160
161 M: world motion-event
162     [ event-loc ] dip move-hand fire-motion ;
163
164 M: world focus-in-event
165     nip [ handle>> xic>> XSetICFocus ] [ focus-world ] bi ;
166
167 M: world focus-out-event
168     nip [ handle>> xic>> XUnsetICFocus ] [ unfocus-world ] bi ;
169
170 M: world selection-notify-event
171     [ handle>> window>> selection-from-event ] keep
172     user-input ;
173
174 : supported-type? ( atom -- ? )
175     { "UTF8_STRING" "STRING" "TEXT" }
176     [ x-atom = ] with any? ;
177
178 : clipboard-for-atom ( atom -- clipboard )
179     {
180         { XA_PRIMARY [ selection get ] }
181         { XA_CLIPBOARD [ clipboard get ] }
182         [ drop <clipboard> ]
183     } case ;
184
185 : encode-clipboard ( string type -- bytes )
186     target>> XA_UTF8_STRING = utf8 ascii ? encode ;
187
188 : set-selection-prop ( evt -- )
189     dpy get swap
190     [ requestor>> ] keep
191     [ property>> ] keep
192     [ target>> 8 PropModeReplace ] keep
193     [ selection>> clipboard-for-atom contents>> ] keep
194     encode-clipboard dup length XChangeProperty drop ;
195
196 M: world selection-request-event
197     drop dup target>> {
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 ]
202     } cond ;
203
204 M: x11-ui-backend (close-window) ( handle -- )
205     [ xic>> XDestroyIC ]
206     [ glx>> destroy-glx ]
207     [ window>> [ unregister-window ] [ destroy-window ] bi ]
208     tri ;
209
210 M: world client-event
211     swap close-box? [ ungraft ] [ drop ] if ;
212
213 : gadget-window ( world -- )
214     dup
215     [ [ [ window-loc>> ] [ dim>> ] bi ] dip handle>> glx-window ]
216     with-world-pixel-format swap
217     dup "Factor" create-xic
218     <x11-handle>
219     [ window>> register-window ] [ >>handle drop ] 2bi ;
220
221 : wait-event ( -- event )
222     QueuedAfterFlush events-queued 0 > [
223         next-event dup
224         None XFilterEvent 0 = [ drop wait-event ] unless
225     ] [ wait-for-display wait-event ] if ;
226
227 M: x11-ui-backend do-events
228     wait-event dup XAnyEvent>> window>> window dup
229     [ handle-event ] [ 2drop ] if ;
230
231 : x-clipboard@ ( gadget clipboard -- prop win )
232     atom>> swap
233     find-world handle>> window>> ;
234
235 M: x-clipboard copy-clipboard
236     [ x-clipboard@ own-selection ] keep
237     (>>contents) ;
238
239 M: x-clipboard paste-clipboard
240     [ find-world handle>> window>> ] dip atom>> convert-selection ;
241
242 : init-clipboard ( -- )
243     XA_PRIMARY <x-clipboard> selection set-global
244     XA_CLIPBOARD <x-clipboard> clipboard set-global ;
245
246 : set-title-old ( dpy window string -- )
247     dup [ 127 <= ] all? [ XStoreName drop ] [ 3drop ] if ;
248
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 ;
252
253 : set-class ( dpy window -- )
254     XA_WM_CLASS XA_UTF8_STRING 8 PropModeReplace "Factor"
255     utf8 encode dup length XChangeProperty drop ;
256
257 M: x11-ui-backend set-title ( string world -- )
258     handle>> window>> swap
259     [ dpy get ] 2dip [ set-title-old ] [ set-title-new ] 3bi ;
260
261 : make-fullscreen-msg ( world ? -- msg )
262     XClientMessageEvent <struct>
263     ClientMessage >>type
264     dpy get >>display
265     "_NET_WM_STATE" x-atom >>message_type
266     swap _NET_WM_STATE_ADD _NET_WM_STATE_REMOVE ? >>data0
267     swap handle>> window>> >>window
268     32 >>format
269     "_NET_WM_STATE_FULLSCREEN" x-atom >>data1 ;
270
271 M: x11-ui-backend (set-fullscreen) ( world ? -- )
272     [ dpy get root get 0 SubstructureNotifyMask ] 2dip
273     make-fullscreen-msg XSendEvent drop ;
274
275 M: x11-ui-backend (open-window) ( world -- )
276     dup gadget-window
277     handle>> window>>
278     [ set-closable ] [ dpy get swap set-class ] [ map-window ] tri ;
279
280 M: x11-ui-backend raise-window* ( world -- )
281     handle>> [
282         dpy get swap window>>
283         [ RevertToPointerRoot CurrentTime XSetInputFocus drop ]
284         [ XRaiseWindow drop ]
285         2bi
286     ] when* ;
287
288 M: x11-handle select-gl-context ( handle -- )
289     dpy get swap
290     [ window>> ] [ glx>> ] bi glXMakeCurrent
291     [ "Failed to set current GLX context" throw ] unless ;
292
293 M: x11-handle flush-gl-context ( handle -- )
294     dpy get swap window>> glXSwapBuffers ;
295
296 M: x11-pixmap-handle select-gl-context ( handle -- )
297     dpy get swap
298     [ glx-pixmap>> ] [ glx>> ] bi glXMakeCurrent
299     [ "Failed to set current GLX context" throw ] unless ;
300
301 M: x11-pixmap-handle flush-gl-context ( handle -- )
302     drop ;
303
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 ;
307
308 M: x11-ui-backend (close-offscreen-buffer) ( handle -- )
309     dpy get swap
310     [ glx-pixmap>> glXDestroyGLXPixmap ]
311     [ pixmap>> XFreePixmap drop ]
312     [ glx>> glXDestroyContext ] 2tri ;
313
314 M: x11-ui-backend offscreen-pixels ( world -- alien w h )
315     [ [ dim>> ] [ handle>> pixmap>> ] bi pixmap-bits ] [ dim>> first2 ] bi ;
316
317 M: x11-ui-backend (with-ui) ( quot -- )
318     [
319         f [
320             [
321                 init-clipboard
322                 start-ui
323                 event-loop
324             ] with-xim
325         ] with-x
326     ] ui-running ;
327
328 M: x11-ui-backend beep ( -- )
329     dpy get 100 XBell drop ;
330
331 x11-ui-backend ui-backend set-global
332
333 [ "DISPLAY" os-env "ui.tools" "listener" ? ]
334 main-vocab-hook set-global