| 1 | ;; A deterministic headless WM. Clients are [id workspace x y width height flags]. |
| 2 | ;; State is [clients focus workspace ticks commands geometry managed config hooks seed]. |
| 3 | (define wm-modulus 1000000007) |
| 4 | (define wm-config-generation 0) |
| 5 | (define wm-active-keys '()) |
| 6 | (define wm-loaded-policy (lambda (s id) #f)) |
| 7 | |
| 8 | (define (wm-new seed) |
| 9 | (let ((clients (make-vector 64 #f))) |
| 10 | (let loop ((id 0)) |
| 11 | (if (< id 64) |
| 12 | (begin (vector-set! clients id (vector id (modulo id 4) 0 0 0 0 1)) |
| 13 | (loop (+ id 1))))) |
| 14 | (vector clients (modulo seed 64) (modulo seed 4) 0 0 0 64 0 0 seed))) |
| 15 | |
| 16 | (define (wm-command s id code) |
| 17 | (vector-set! s 4 (modulo (+ (* (vector-ref s 4) 17) (* id 3) code) wm-modulus)) |
| 18 | (let ((client (vector-ref (vector-ref s 0) id))) |
| 19 | (if client (vector-set! client 6 (modulo (+ (vector-ref client 6) code) 256)))) |
| 20 | code) |
| 21 | |
| 22 | (define (wm-focus s start) |
| 23 | (let ((clients (vector-ref s 0)) (workspace (vector-ref s 2))) |
| 24 | (let loop ((left 64) (id (modulo start 64))) |
| 25 | (if (= left 0) (vector-set! s 1 -1) |
| 26 | (let ((client (vector-ref clients id))) |
| 27 | (if (and client (= (vector-ref client 1) workspace)) (vector-set! s 1 id) |
| 28 | (loop (- left 1) (modulo (+ id 1) 64)))))))) |
| 29 | |
| 30 | (define (wm-switch s workspace) |
| 31 | (vector-set! s 2 (modulo workspace 4)) |
| 32 | (wm-focus s (+ (vector-ref s 1) 1))) |
| 33 | |
| 34 | (define (wm-move s id workspace) |
| 35 | (let ((client (vector-ref (vector-ref s 0) id))) |
| 36 | (if client |
| 37 | (begin (vector-set! client 1 (modulo workspace 4)) |
| 38 | (if (and (= id (vector-ref s 1)) (not (= (vector-ref client 1) (vector-ref s 2)))) |
| 39 | (wm-focus s (+ id 1)))))) |
| 40 | (wm-command s id 3)) |
| 41 | |
| 42 | (define (wm-map s id workspace) |
| 43 | (let ((clients (vector-ref s 0))) |
| 44 | (if (not (vector-ref clients id)) (vector-set! s 6 (+ 1 (vector-ref s 6)))) |
| 45 | (vector-set! clients id (vector id (modulo workspace 4) 0 0 0 0 1)))) |
| 46 | |
| 47 | (define (wm-unmap s id) |
| 48 | (let ((clients (vector-ref s 0))) |
| 49 | (if (vector-ref clients id) |
| 50 | (begin (vector-set! clients id #f) (vector-set! s 6 (- (vector-ref s 6) 1))))) |
| 51 | (if (= id (vector-ref s 1)) (wm-focus s (+ id 1)))) |
| 52 | |
| 53 | (define (wm-layout s frame) |
| 54 | (let* ((clients (vector-ref s 0)) (workspace (vector-ref s 2)) |
| 55 | (width (+ 1280 (* 8 (vector-ref s 9)))) (gap (+ 4 (modulo (vector-ref s 7) 5))) |
| 56 | (master (quotient (* width 3) 5)) |
| 57 | (count (let scan ((id 0) (n 0)) |
| 58 | (if (= id 64) n |
| 59 | (let ((c (vector-ref clients id))) |
| 60 | (scan (+ id 1) (if (and c (= (vector-ref c 1) workspace)) (+ n 1) n))))))) |
| 61 | (let place ((id 0) (rank 0)) |
| 62 | (if (< id 64) |
| 63 | (let ((c (vector-ref clients id))) |
| 64 | (if (and c (= (vector-ref c 1) workspace)) |
| 65 | (begin |
| 66 | (if (or (= count 1) (= (modulo frame 2) 1)) |
| 67 | (begin (vector-set! c 2 gap) (vector-set! c 3 gap) |
| 68 | (vector-set! c 4 (- width (* 2 gap))) (vector-set! c 5 (- 720 (* 2 gap)))) |
| 69 | (if (= rank 0) |
| 70 | (begin (vector-set! c 2 gap) (vector-set! c 3 gap) |
| 71 | (vector-set! c 4 (- master (* 2 gap))) (vector-set! c 5 (- 720 (* 2 gap)))) |
| 72 | (let ((height (quotient 720 (- count 1)))) |
| 73 | (vector-set! c 2 (+ master gap)) (vector-set! c 3 (+ gap (* (- rank 1) height))) |
| 74 | (vector-set! c 4 (- width master (* 2 gap))) (vector-set! c 5 (- height (* 2 gap)))))) |
| 75 | (place (+ id 1) (+ rank 1))) |
| 76 | (place (+ id 1) rank))))) |
| 77 | (vector-set! s 5 (modulo (+ (vector-ref s 5) (* count 97) frame) wm-modulus)))) |
| 78 | |
| 79 | (define wm-dispatch |
| 80 | (vector (lambda(s id p)(wm-focus s id)) (lambda(s id p)(wm-switch s p)) |
| 81 | (lambda(s id p)(wm-move s id p)) (lambda(s id p)(wm-command s id p)) |
| 82 | (lambda(s id p)(wm-layout s p)) (lambda(s id p)(wm-map s id p)) |
| 83 | (lambda(s id p)(wm-unmap s id)) |
| 84 | (lambda(s id p)(vector-set! s 8 (+ (vector-ref s 8) p))))) |
| 85 | |
| 86 | (define (wm-event s kind id payload) |
| 87 | (vector-set! s 3 (+ (vector-ref s 3) 1)) |
| 88 | ((vector-ref wm-dispatch kind) s id payload)) |
| 89 | |
| 90 | (define (wm-leaf s id code) (wm-command s id code) (+ code 1)) |
| 91 | (define (wm-one s id code) (wm-leaf s id (+ code 1))) |
| 92 | (define (wm-two s id code) (wm-one s id (+ code 1))) |
| 93 | (define (wm-three s id code) (wm-two s id (+ code 1))) |
| 94 | (define (wm-four s id code) (wm-three s id (+ code 1))) |
| 95 | (define (wm-callback bias) (lambda(s id code)(wm-four s id (+ code bias)))) |
| 96 | (define (wm-invoke callback s id code) (callback s id code)) |
| 97 | |
| 98 | (define-syntax wm-binding |
| 99 | (syntax-rules () ((_ key modifiers action) (cons (cons key modifiers) action)))) |
| 100 | (define-syntax wm-keyset |
| 101 | (syntax-rules () ((_ (key modifiers code) ...) |
| 102 | (list (wm-binding key modifiers (wm-action code)) ...)))) |
| 103 | (define (wm-action code) (lambda(s id)(wm-command s id code))) |
| 104 | (define (wm-keymap bias) |
| 105 | (let loop ((id 31) (keys '())) |
| 106 | (if (< id 0) keys |
| 107 | (loop (- id 1) (cons (wm-binding (modulo id 8) (quotient id 8) |
| 108 | (wm-action (+ (* (+ id 1) 3) bias))) keys))))) |
| 109 | (define (wm-key s keys key modifiers id) |
| 110 | (let ((binding (assoc (cons key modifiers) keys))) |
| 111 | (if binding ((cdr binding) s id)))) |
| 112 | |
| 113 | (define (wm-hook tag) |
| 114 | (let ((count 0)) |
| 115 | (lambda(s id code) |
| 116 | (set! count (+ count 1)) |
| 117 | (wm-command s id (+ tag code count)) |
| 118 | (vector-set! s 8 (+ (vector-ref s 8) count)) |
| 119 | (= (modulo code (+ tag 2)) 0)))) |
| 120 | (define (wm-deliver hooks s id code) |
| 121 | (if (pair? hooks) |
| 122 | (if (not ((car hooks) s id code)) (wm-deliver (cdr hooks) s id code)))) |
| 123 | |
| 124 | (define (wm-macro-form bias) |
| 125 | (cons 'wm-keyset |
| 126 | (let loop ((id 31) (forms '())) |
| 127 | (if (< id 0) forms |
| 128 | (loop (- id 1) (cons (list (modulo id 8) (quotient id 8) (+ (* (+ id 1) 3) bias)) forms)))))) |
| 129 | (define (wm-load-config) |
| 130 | (load (string-append wm-fixture-root "/root.scm"))) |
| 131 | (define (wm-apply-config s id) |
| 132 | (vector-set! s 7 wm-config-generation) |
| 133 | (wm-loaded-policy s id) |
| 134 | (wm-key s wm-active-keys (modulo id 8) (modulo (quotient id 8) 4) id)) |
| 135 | |
| 136 | (define (wm-score s) |
| 137 | (let ((initial (let state ((i 1) (hash 19)) |
| 138 | (if (= i 10) hash (state (+ i 1) (modulo (+ (* hash 33) (vector-ref s i)) wm-modulus)))))) |
| 139 | (let clients ((id 0) (hash initial)) |
| 140 | (if (= id 64) hash |
| 141 | (let ((c (vector-ref (vector-ref s 0) id))) |
| 142 | (clients (+ id 1) |
| 143 | (if c |
| 144 | (let fields ((i 0) (h hash)) |
| 145 | (if (= i 7) h (fields (+ i 1) (modulo (+ (* h 33) (vector-ref c i)) wm-modulus)))) |
| 146 | (modulo (+ (* hash 33) 257) wm-modulus)))))))) |
| 147 | |
| 148 | (define (benchmark seed) |
| 149 | (let ((s (wm-new seed)) ) |
| 150 | |
| 151 | (let loop ((i 0)) |
| 152 | (if (< i (+ 1024 (* 8 seed))) (begin (wm-move s (modulo (+ (* i 11) seed) 64) (+ i seed)) |
| 153 | (if (= (modulo i 31) 0) (wm-switch s (quotient i 31))) (loop (+ i 1))))) |
| 154 | (wm-score s))) |
| 155 | (define (run-batch count) |
| 156 | (let loop ((left count) (seed 0)) |
| 157 | (if (= left 0) 944497161 |
| 158 | (if (= (benchmark seed) (if (= seed 0) 944497161 (if (= seed 1) 402600027 (if (= seed 2) 341068392 211255284)))) |
| 159 | (loop (- left 1) (if (= seed 3) 0 (+ seed 1))) #f)))) |
| 160 | |