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