-
Notifications
You must be signed in to change notification settings - Fork 19
/
ulubis.lisp
271 lines (233 loc) · 9.71 KB
/
ulubis.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
(in-package :ulubis)
(defparameter *compositor* nil)
#|
We currently just ask the current-view to render itself.
This does not allow animation. Instead, if on the compositor
level (when we support multiple monitors it will be on the level
of the desktop...the compositor will have a list of desktops),
we also have the concept of modes, we can ask the current mode
to render itself. That mode will have the virtual desktops (views)
as "surfaces" and can render them as it pleases.
Instead of (texture-of (current-view *compositor*))
we'll want (texture-of (current-mode *compositor*))
At the moment only views have fbos.
Scratch that, effects also have fbos. But let's ignore effects
for the moment.
In the way that we have fbos on view we will have fbos on desktops.
Or are fbos on desktops just the default fbo that we have from our
GL context? I think the latter...we don't need an explicit fbo.
However, I think we want our views to also behave like surfaces.
I.e. they have x,y position widht height (albeit that of the screen).
We currently define isurface within waylisp. I can't remember exactly why
it's defined in there rather than in ulubis itself. Maybe I should think about
moving it back. If views are also isurfaces that should be within ulubis.
|#
(defun draw-screen ()
(with-screen (vs)
(gl:clear-color 0.3 0.3 0.3 0.0)
(cepl:clear)
;; We are just rendering into the default fbo
(render (current-mode (screen *compositor*)))
(gl:enable :blend)
(draw-cursor (cursor-surface *compositor*)
nil
(pointer-x *compositor*)
(pointer-y *compositor*)
(ortho 0 (screen-width *compositor*) (screen-height *compositor*) 0 1 -1))
(swap-buffers (backend *compositor*))
(setf (render-needed *compositor*) nil)
(process-callbacks)
(setf (callbacks *compositor*) nil)))
(defun process-callbacks ()
(loop :for callback :in (callbacks *compositor*) :do
(when (find (client callback) waylisp::*clients*)
;; We can end up getting a frame request after the client has been deleted
;; if we try and send-done or destroy we will get a memory fault
(wl-callback-send-done (->resource callback) (get-milliseconds))
(wl-resource-destroy (->resource callback)))
(remove-resource callback)))
(defcallback input-callback :void ((fd :int) (mask :int) (data :pointer))
(process-events (backend *compositor*)))
(defun main-loop-drm (event-loop)
(let ((libinput-fd (get-fd (backend *compositor*))))
(init-egl (backend *compositor*) (display *compositor*))
(initialize-animation event-loop)
(wl-event-loop-add-fd event-loop libinput-fd 1 (callback input-callback) (null-pointer))
(event-loop-add-drm-fd (backend *compositor*) event-loop)
(loop :while (running *compositor*)
:do (progn
(when (and (render-needed *compositor*) (not (get-scheduled (backend *compositor*))))
(draw-screen))
(wl-display-flush-clients (display *compositor*))
(wl-event-loop-dispatch event-loop -1)
(animation::update-animations (lambda () (setf (render-needed *compositor*) t)))))))
(defun main-loop-sdl (event-loop)
(let ((wayland-fd (wl-event-loop-get-fd event-loop)))
(syscall:with-pollfds (pollfds
(wayland-pollfd wayland-fd syscall:pollin syscall:pollpri))
(initialize-animation event-loop)
(loop :while (running *compositor*)
:do (progn
(when (render-needed *compositor*)
(draw-screen))
(wl-event-loop-dispatch event-loop 0)
(wl-display-flush-clients (display *compositor*))
(alexandria:ignore-some-conditions (nix:eintr)
(let ((event (syscall:poll pollfds 1 5)))
(wl-event-loop-dispatch event-loop 0)
(wl-display-flush-clients (display *compositor*))
(animation::update-animations (lambda ()
(setf (render-needed *compositor*) t)))
(process-events (backend *compositor*)))))))))
(defun call-mouse-motion-handler (time x y)
(when (show-cursor *compositor*)
(setf (render-needed *compositor*) t))
(mouse-motion-handler (screen *compositor*) time x y))
;; Should be able to have "active" window without raising (focus follows mouse)
(defun call-mouse-button-handler (time button state)
(mouse-button-handler (screen *compositor*) time button state))
(defun window-event-handler ()
(new-xkb-state *compositor*)
(setf (render-needed *compositor*) t))
;; Kludge for SDL backend on multi-desktop WMs to avoid sticky mods.
;; Seems like sway uses wl_keyboard_listener. We probably should use
;; it too instead of the whole thing.
(defvar *depressed-keys* (list))
(defun call-keyboard-handler (time keycode state)
(let ((keysym (xkb:xkb-state-key-get-one-sym (xkb-state *compositor*) (+ keycode 8))))
(if (and (= state 1)
(member keycode *depressed-keys*))
(progn
;;(format t "!!! Not updating key state~%")
)
(xkb:xkb-state-update-key (xkb-state *compositor*) (+ keycode 8) state))
(if (= state 1)
(push keycode *depressed-keys*)
(setf *depressed-keys* (delete keycode *depressed-keys*)))
(setf (mods-depressed *compositor*) (xkb:xkb-state-serialize-mods (xkb-state *compositor*) 1))
(setf (mods-latched *compositor*) (xkb:xkb-state-serialize-mods (xkb-state *compositor*) 2))
(setf (mods-locked *compositor*) (xkb:xkb-state-serialize-mods (xkb-state *compositor*) 4))
(setf (mods-group *compositor*) (xkb:xkb-state-serialize-layout (xkb-state *compositor*) 64))
(when (and (numberp keysym) (numberp state))
(keyboard-handler (screen *compositor*)
time
keycode
keysym
state))))
(defun initialise ()
(unwind-protect
(block main-handler
(handler-bind ((error #'(lambda (e)
(format t "~%Oops! Something went wrong with ulubis...we throw ourselves at your mercy! Exiting wih error:~%")
(trivial-backtrace:print-backtrace e)
(return-from main-handler))))
#+sbcl
(sb-int:set-floating-point-modes :traps nil)
;; Make our compositor class
(setf *compositor* (make-instance 'compositor))
(when (probe-file "~/.ulubis.lisp")
(load "~/.ulubis.lisp"))
;; Initialise backend
(setf (backend *compositor*) (make-instance 'backend))
(initialise-backend (backend *compositor*)
(screen-width *compositor*)
(screen-height *compositor*)
(devices *compositor*))
;; ulubis will attempt to run the function STARTUP
;; This should be defined in the user's ~/.ulubis.lisp
;; And is intended to set up things like the number
;; of virtual desktops (views), etc.
(handler-case (startup)
(undefined-function ()
(progn
(make-screen 'virtual-desktop-mode)
(push-view 'desktop-mode))
(setf (active-surface (screen *compositor*)) (first (surfaces (screen *compositor*))))))
(register-mouse-motion-handler (backend *compositor*) 'call-mouse-motion-handler)
(register-mouse-button-handler (backend *compositor*) 'call-mouse-button-handler)
(register-window-event-handler (backend *compositor*) 'window-event-handler)
(register-keyboard-handler (backend *compositor*) 'call-keyboard-handler)
;; Create our wayland display
(setf (display *compositor*) (wl-display-create))
(format t "Opened socket: ~A~%" (wl-display-add-socket-auto (display *compositor*)))
;; Initialise shared memory
(initialize-wayland-server-interfaces)
(initialize-xdg-shell-server-interfaces)
(initialize-zxdg-shell-v6-server-interfaces)
;;(set-implementations)
(set-implementation-wl-surface)
(set-implementation-wl-seat)
(set-implementation-wl-pointer)
(set-implementation-wl-seat)
;;(set-implementation-wl-callback)
(set-implementation-wl-region)
(set-implementation-wl-compositor)
(set-implementation-wl-subcompositor)
(set-implementation-wl-subsurface)
(set-implementation-wl-output)
(set-implementation-wl-shell)
(set-implementation-wl-shell-surface)
(set-implementation-wl-data-device-manager)
(set-implementation-wl-data-device)
(set-implementation-wl-data-source)
(set-implementation-zxdg-shell-v6)
(set-implementation-zxdg-surface-v6)
(set-implementation-zxdg-toplevel-v6)
(set-implementation-zxdg-positioner-v6)
(set-implementation-zxdg-popup-v6)
(set-implementation-xdg-shell)
(set-implementation-xdg-surface)
(wl-display-init-shm (display *compositor*))
(wl-global-create (display *compositor*)
wl-output-interface
2
(null-pointer)
(callback output-bind))
(wl-global-create (display *compositor*)
wl-compositor-interface
3
(null-pointer)
(callback compositor-bind))
(wl-global-create (display *compositor*)
wl-shell-interface
1
(null-pointer)
(callback shell-bind))
(wl-global-create (display *compositor*)
wl-seat-interface
3
(null-pointer)
(callback seat-bind))
(wl-global-create (display *compositor*)
wl-data-device-manager-interface
3
(null-pointer)
(callback device-manager-bind))
(wl-global-create (display *compositor*)
wl-subcompositor-interface
1
(null-pointer)
(callback subcompositor-bind))
(wl-global-create (display *compositor*)
zxdg-shell-v6-interface
1
(null-pointer)
(callback zxdg-shell-v6-bind))
(wl-global-create (display *compositor*)
xdg-shell-interface
1
(null-pointer)
(callback xdg-shell-bind))
;; Run main loop
;; (format t "Running main loop~%")
(setf (running *compositor*) t)
(if (string-equal (symbol-name backend-name) "backend-drm-gbm")
(main-loop-drm (wl-display-get-event-loop (display *compositor*)))
(main-loop-sdl (wl-display-get-event-loop (display *compositor*))))))
(when (display *compositor*)
(wl-display-destroy (display *compositor*))
(setf (display *compositor*) nil))
(destroy-backend (backend *compositor*))
(setf *compositor* nil)))
(defun run-compositor ()
(initialise))