This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.fnl
493 lines (413 loc) · 16.5 KB
/
main.fnl
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
;; -*- fennel -*- mode
(local ffi (require "ffi"))
(local io (require "io"))
(local math (require "math"))
;; each is syntax in fennel, I am defining it as something random
;; because otherwise luafun exports it, and I would rather break
;; it in a place I know than have a library break it in a place I
;; don't know
(global each {})
((require "fun"))
(let [f (io.open "defs.h.out" "r")]
(ffi.cdef (f.read f "*all")))
(lambda from-cpath [name]
(package.searchpath name package.cpath))
(local wlroots (ffi.load (from-cpath "wlroots")))
(local wayland (ffi.load (from-cpath "wayland-server")))
(local xkbcommon (ffi.load (from-cpath "xkbcommon")))
(local fennelview (require "fennelview"))
(global pp (lambda [x] (print (fennelview x))))
(lambda now []
(let [ts (ffi.new "struct timespec")]
(ffi.C.clock_gettime ffi.C.clock_monotonic ts)
ts))
(wlroots.wlr_log_init 3 nil)
(lambda write-pid []
(let [f (io.open "/tmp/fenestra.pid" "w")
pid (ffi.C.getpid)]
(f.write f pid)))
(lambda wl-add-listener [signal func]
(let [listener (ffi.new "struct wl_listener")]
(set listener.notify (lambda [l d]
;; close over listener to stop it being GCed
(func listener d)))
(wayland.wl_list_insert signal.listener_list.prev listener.link)
listener))
;; I'm sure there are better and more efficient ways to make hash keys
;; from ffi cdata objects, and some day I will find out what they are
(lambda ffi-address [cdata]
(tonumber (ffi.cast "intptr_t" (ffi.cast "void *" cdata))))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; we are going to do a "flowing data" architecture, loosely inspired
;;; by re-frame
;;; There is application state. It is acted on by an effect handler
;;; which is told what to do by effects, which are data items conputed
;;; by event handlers. Event handlers run when something dispatches
;;; the events they're registered to handle. "Something" is probably a
;;; callback registered with/by the platform code (i.e. wlroots or wayland)
;;; Every frame, the scene is rendered by looking at the application state
;;; to see which surfaces it needs to draw
;;; re-frame concepts that I might not yet need
;;; - effect handlers for any effect other than "change app state",
;;; will add these when they torun out to be relevant
;;; - view functions: right now, the frame renderer will look at app state
;;; directly
;;; other things I have not addressed in this design
;;; - recognising "commands" from low-level input events (e.g. where should
;;; we put code to recognise that the user is doing a horizontal drag and
;;; wants[*] to maximise the window
;;; [*] for the record, personally I hate this behaviour and do not
;;; want it on my computer. But KDE is an existence proof that (more
;;; than zero) other people do like it, and fenestra should be able to
;;; encode it (and other less awful but equally complex gestures
;;; involving state machines) in a non-hacky way
;; this happens to be destructive but the caller should not depend on it
(lambda merge [old-value new-value]
(each [k v (pairs new-value)]
(tset old-value k v))
old-value)
;; this happens to be destructive but the caller should not depend on it
(lambda conj [coll v]
(table.insert coll v)
coll)
(lambda inc [x] (+ x 1))
(lambda dec [x] (- x 1))
(assert (= 6 (sum (filter (lambda [x] (< x 3)) [1 7 1 9 2 10 2 4]))))
;; these are probably not the fastest way of doing this as I suspect
;; it does a lot of copying and makes a lot of garbage
(lambda empty? [c] (is_null c))
(fn id [x] x)
(lambda first [xs] (if (is_null xs) nil (head xs)))
(lambda keys [tbl]
(let [out []]
(each [k _ (pairs tbl)]
(table.insert out k))
out))
(lambda equal? [a b]
(if (= (type a) (type b))
(if (= (type a) "table")
(and (= (length a) (length b))
(every (fn [k] (equal? (. a k) (. b k)))
(keys a)))
(= a b))
false))
(assert (not (= 1 nil)) "1 is not nil")
(assert (equal? [6 1 2 3] [6 1 2 3]))
(assert (not (equal? [1 2 3] [1 2 3 4])) "different lengths")
(assert (not (equal? {:l 2} {:l 2 :a 9})))
(assert (not (equal? {:l 2 :a 9} {:l 2})))
(assert (equal? {:l 2} {:l 2 }))
(lambda assert-equal [expected actual]
(assert (equal? expected actual)
(.. "test failed: "
(fennelview
{:expected expected
:actual actual}))))
(fn assoc [tbl k v ...]
(tset tbl k v)
(if ...
(assoc tbl (unpack [...]))
tbl))
(assert-equal {:foo 1 :bar 34}
(assoc {:foo 1}
:bar 34))
(assert-equal {:foo 1 :bar 34 :baz 6}
(assoc {:foo 1}
:bar 34
:baz 6))
(lambda assoc-in [tbl path value]
(let [k (head path)
r (tail path)]
(if (empty? r)
(assoc tbl k value)
(assoc tbl k (assoc-in (or (. tbl k) {}) r value)))))
(assert-equal {:k 2}
(assoc-in {} [:k] 2))
(assert-equal {:horse {:zebra 9} }
(assoc-in {} [:horse :zebra] 9))
(assert-equal {:horse {:zebra 9} }
(assoc-in {:horse {:zebra 11}} [:horse :zebra] 9))
(assert-equal {:k {:l 2 :z 9} }
(assoc-in {:k {:z 9}} [:k :l] 2))
(var handlers {})
(lambda listen [name handler]
(tset handlers
name
(conj (or (. handlers name) []) handler)))
(var app-state {})
(var seats {
:hotseat {}
})
(lambda seat-effect-handler [[command seat-name input-name attributes]]
(let [seat (. seats seat-name)]
(if (= command :attach)
(set seats (assoc-in seats [seat-name :inputs input-name] attributes))
(= command :detach)
(print "detach " attributes)
(= command :create)
(set seats (assoc-in seats [seat-name :wlr-seat]
(wlroots.wlr_seat_create
input-name seat-name)))
(error ["unrecognised command" command]))
(pp seats)
(let [inputs (or seat.inputs [])
keyboard? (any (fn [name attrs] attrs.keyboard) inputs)
pointer? (any (fn [name attrs] attrs.pointer) inputs)
caps
(+ (if keyboard? ffi.C.WL_SEAT_CAPABILITY_KEYBOARD 0)
(if pointer? ffi.C.WL_SEAT_CAPABILITY_POINTER 0)
)]
(wlroots.wlr_seat_set_capabilities seat.wlr-seat caps))
))
(var effect-handlers
{
:state
(lambda [new-paths]
;; again, this happens to be destructive but the caller
;; should not depend on it
(set app-state
(reduce (lambda [m path]
(assoc-in m path (. new-paths path)))
app-state
new-paths)))
:seat seat-effect-handler
})
(lambda dispatch [name ...]
(print "dispatch " name)
(let [fns (. handlers name)]
(when fns
(each [_ f (ipairs fns)]
(let [effects (f app-state (unpack [...]))]
(each [name value (pairs effects)]
(let [h (. effect-handlers name)]
(if h
(h value)
(error (.. "no effect handler for " name))))))))))
(lambda new-backend [display]
(let [be (wlroots.wlr_backend_autocreate display nil)]
(wl-add-listener
be.events.new_output
(lambda [_ data]
(dispatch :new-output (ffi.cast "struct wlr_output *" data))))
(wl-add-listener
be.events.new_input
(lambda [_ data]
(dispatch :new-input (ffi.cast "struct wlr_input_device *" data))))
be))
(lambda new-compositor [display renderer]
(let [compositor (wlroots.wlr_compositor_create display renderer)]
(wl-add-listener
compositor.events.new_surface
(lambda [l d]
(let [wlr_surface (ffi.cast "struct wlr_surface *", d)]
(dispatch :new-surface wlr_surface))))
compositor))
(lambda new-xdg-shell [display]
(let [s (wlroots.wlr_xdg_shell_create display)
s6 (wlroots.wlr_xdg_shell_v6_create display)]
(wl-add-listener s.events.new_surface
(lambda [l d]
(let [xs (ffi.cast "struct wlr_xdg_surface *" d)]
(wl-add-listener xs.events.map
(lambda [l d]
(dispatch :map-shell
xs.surface))))))
(wl-add-listener s6.events.new_surface
(lambda [l d]
(let [xs (ffi.cast "struct wlr_xdg_surface_v6 *" d)]
(wl-add-listener xs.events.map
(lambda [l d]
(dispatch :map-shell
xs.surface))))))
[s s6]))
(lambda initial-state []
(let [display (wayland.wl_display_create)
backend (new-backend display)]
{
:backend backend
:compositor (new-compositor display (wlroots.wlr_backend_get_renderer backend))
:display display
:layout (wlroots.wlr_output_layout_create)
:socket (ffi.string (wayland.wl_display_add_socket_auto display))
}))
(listen :light-blue-touchpaper
(lambda [state event]
(let [d state.display]
;; there is a little more (read: any) side-effecting code
;; being called here than I'd like in what is supposed to
;; be a purely functional event handler. We will push it
;; into some kind of effect handler just as soon as it
;; becomes more obvious *what* kind of effect handler
(wayland.wl_display_init_shm d)
;;(wlroots.wlr_gamma_control_manager_create d)
;; wlroots.wlr_screenshooter_create(display);
;;- wlroots.wlr_primary_selection_device_manager_create(display);
(wlroots.wlr_idle_create d)
{:state
{[:xdg-shell] (new-xdg-shell d)
}
:seat
[:create :hotseat d nil] })))
(global colors
{:red (ffi.new "float[4]", [1.0 0.0 0.0 1.0])
:black (ffi.new "float[4]", [0.0 0.0 0.0 1.0])})
(lambda render-surface [s output renderer]
(let [x s.x
y s.y
surface s.wlr-surface]
(when (wlroots.wlr_surface_has_buffer surface)
(let [box (ffi.new "struct wlr_box"
{
:x x
:y y
:width surface.current.width
:height surface.current.height})
texture (wlroots.wlr_surface_get_texture surface)]
(wlroots.wlr_matrix_project_box
s.matrix
box
surface.current.transform
s.rotation
output.transform_matrix)
(wlroots.wlr_render_texture_with_matrix
renderer texture s.matrix 1.0)
(wlroots.wlr_surface_send_frame_done surface (now))))))
(lambda render-frame [output]
;; ideally this wouldn't refer to the global app-state
(let [backend app-state.backend
compositor app-state.compositor
renderer (wlroots.wlr_backend_get_renderer backend)]
(wlroots.wlr_output_make_current output, nil)
(wlroots.wlr_renderer_begin renderer, output.width, output.height)
(wlroots.wlr_renderer_clear renderer colors.black)
(when app-state.surfaces
(each [_ s (pairs app-state.surfaces)]
(if (and s.x s.y)
;; not every surface is a shell, not every shell is mapped
(render-surface s output renderer))))
(wlroots.wlr_output_swap_buffers output nil nil)
(wlroots.wlr_renderer_end renderer)))
(listen :new-output
(lambda [state output]
(let [l
(wl-add-listener
output.events.frame
(lambda [_ _] (render-frame output)))]
(wlroots.wlr_output_create_global output)
{:state
{[:outputs (ffi-address output)]
{:wl-output output :frame-listener l}}})))
(lambda set-default-keymap [keyboard]
(let [rules (ffi.new "struct xkb_rule_names" {})
;; -- rules.rules = getenv("XKB_DEFAULT_RULES");
;; -- rules.model = getenv("XKB_DEFAULT_MODEL");
;; -- rules.layout = getenv("XKB_DEFAULT_LAYOUT");
;; -- rules.variant = getenv("XKB_DEFAULT_VARIANT");
;; -- rules.options = getenv("XKB_DEFAULT_OPTIONS");
context (xkbcommon.xkb_context_new 0)
keymap (and context
(xkbcommon.xkb_keymap_new_from_names context rules 0))
ret (if keymap
(wlroots.wlr_keyboard_set_keymap keyboard keymap)
(values nil (if context
"Couldn't create keymap"
"Couldn't create xkb context")))]
(and keymap (xkbcommon.xkb_keymap_unref keymap))
(and context (xkbcommon.xkb_context_unref context))
ret))
(listen :key
(lambda [state seat key-event]
(print "keypress" key-event.keycode)
(wlroots.wlr_seat_keyboard_notify_key
seat.wlr-seat
key-event.time_msec,
key-event.keycode,
key-event.state)
{}))
(lambda new-keyboard [seat input state]
(let [k input.keyboard]
(set-default-keymap k)
(wl-add-listener
k.events.key
(lambda [l d]
(dispatch :key
seat
(ffi.cast "struct wlr_event_keyboard_key *" d))))
(wlroots.wlr_seat_set_keyboard seat.wlr-seat input)
{:keyboard
{:wlr-keyboard k}}))
(lambda new-pointer [seat input state]
(let [p input.pointer]
;(wlroots.wlr_cursor_attach_input_device seat.cursor.wlr_cursor input)
{:pointer :tba}))
(local input-ctors
{
ffi.C.WLR_INPUT_DEVICE_KEYBOARD new-keyboard,
ffi.C.WLR_INPUT_DEVICE_POINTER new-pointer
})
(listen :new-input
;; the backend tells us what the connected input devices
;; are, but the choice of which seat to attach which device to
;; is ours as the compositor. For the moment we have only one seat
(lambda [state input]
(let [i {:name (ffi.string input.name)
:vendor input.vendor
:wlr-input-device input
:product input.product}
ctor (. input-ctors (tonumber input.type))]
(if (= ctor nil)
(do
(print "no support for input device " i.name
" of type " input.type)
{})
{:seat
[:attach
:hotseat
(ffi.string input.name)
(merge i (ctor seats.hotseat input state))
]}))))
(listen :new-surface
(lambda [state surface]
(print "new surface")
(let [s {:wlr-surface surface
:matrix (ffi.new "float[16]")
;; a new surface may not be a shell, and even if it is we
;; don't know how big to render it (and perhaps therefore
;; where to put it) until it's mapped.
:x nil
:y nil}]
{:state {[:surfaces (ffi-address surface)] s}})))
(listen :map-shell
(lambda [state wl-surface]
(let [id (ffi-address wl-surface)
shell-surface (. state.surfaces id)
seat seats.hotseat
; kbd-device (first (filter (fn [k v] v.keyboard) seat.inputs))
; keyboard (and kbd-device
; (. seat.inputs kbd-device :keyboard :wlr-keyboard))
]
;; I *think* this function is expecting to get an array of
;; currently-pressed keys, not something about available
;; keys. So probably we don't need access to the keyboard
;; as such here, but should to talk to the gesture
;; recogniser such that any ongoing gesture involving held
;; keys is cancelled and the key state is sent to the
;; client instead. For now we'll pretend nothing is ever
;; being typed at the moment the new window pops up
(print "map " wl-surface)
(wlroots.wlr_seat_keyboard_notify_enter
seat.wlr-seat
wl-surface
nil ; keyboard.keycodes
0 ; keyboard.num_keycodes
nil ; keyboard.modifiers
)
{:state
{[:surfaces id] (assoc shell-surface
:rotation (- (/ (math.random) 10.0) 0.05)
:x (math.random 200)
:y (math.random 100))}})))
(set app-state (initial-state))
(dispatch :light-blue-touchpaper {})
(wlroots.wlr_backend_start app-state.backend)
(wayland.wl_display_run app-state.display)