Debian Codegraph @

lib/text/console/framebuffer.scm

raw
1 (define-module text.console.framebuffer
2 (use srfi.13)
3 (use text.console)
4 (use text.console.wide-char-setting)
5 (export <framebuffer-console> init-framebuffer draw-framebuffer))
6 (select-module text.console.framebuffer)
7
8 ;;; Framebuffer console.
9 ;; This is a virtual console that records all updates in an internal
10 ;; buffer, then updates a real console later with draw-framebuffer
11 ;; call. The last draw is kept, so next time it only updates the
12 ;; changed parts.
13 ;;
14 ;; This is made for text.line-edit and supports just enough operations
15 ;; for it to work. It also assumes that init-framebuffer starts with a
16 ;; cleared console. This is true for line-edit, but not always.
17 ;;
18 ;; It is undefined behavior if something is written outside the
19 ;; console's view, aka (or (> x width) (> y height)).
20 (define-class <framebuffer-console> ()
21 ((width)
22 (height)
23 (wide-char-setting :init-value #f)
24 ;; Two dimensional array of cons whose car is the character and cdr
25 ;; is the attribute. If car is #f, the position is part of a wide
26 ;; char character. If cdr is #f, the attribute is reset. If a
27 ;; vector item is #f instead of a cons, then it's the same as
28 ;; (#\space . #f)
29 (buffer :init-form (make-vector 0))
30 (prev-buffer :init-form (make-vector 0))
31 (commands :init-value '())
32
33 ;; current cursor / attribute
34 (x :init-value 0)
35 (y :init-value 0)
36 (attr :init-value #f)
37 (cursor-hidden :init-value #f)
38 (pass-through :init-value #f)
39 ))
40
41 (define-syntax yx->pos
42 (syntax-rules ()
43 ((_ con row col)
44 (+ col (* row (~ con'width))))))
45
46 (define-syntax char-width
47 (syntax-rules ()
48 ((_ con ch) (if (~ con'wide-char-setting)
49 (get-char-width (~ con'wide-char-setting) ch)
50 1))))
51
52 (define (init-framebuffer fb w h wide-char-setting)
53 (set! (~ fb'width) w)
54 (set! (~ fb'height) h)
55 (set! (~ fb'cursor-hidden) #f)
56 (set! (~ fb'wide-char-setting) wide-char-setting)
57 (set! (~ fb'commands) '())
58 (set! (~ fb'pass-through) #f)
59 (let ([size (* w h)])
60 (if (and (vector? (~ fb'buffer))
61 (>= (vector-length (~ fb'buffer)) size))
62 ;; reuse the vector to reduce GC pressure a bit
63 (begin
64 (vector-fill! (~ fb'buffer) #f)
65 (vector-fill! (~ fb'prev-buffer) #f))
66 (begin
67 (set! (~ fb'buffer) (make-vector size #f))
68 (set! (~ fb'prev-buffer) (make-vector size #f))))))
69
70 (define (draw-framebuffer con console init-row init-col)
71 (if (~ con'pass-through)
72 (begin
73 (for-each (cut <> console) (reverse (~ con'commands)))
74 (set! (~ con'pass-through) #f)
75 (init-framebuffer con
76 (~ con'width)
77 (~ con'height)
78 (~ con'wide-char-setting)))
79 (%draw-framebuffer con console init-row init-col)))
80
81 (define (%draw-framebuffer con console init-row init-col)
82 (let ([curbuf (~ con'buffer)]
83 [prevbuf (~ con'prev-buffer)]
84 [width (~ con'width)]
85 [height (~ con'height)]
86 [clear-to-eos? #f] ; FIXME
87 [empty (cons #\space #f)]
88 [rattr #f]
89 [rx 0]
90 [ry 0])
91
92 (hide-cursor console)
93
94 ;; in case we detect clearing the screen or something (e.g. the
95 ;; majority of updates is to write whitespaces) then better to
96 ;; just clear the whole thing first and skip all whitespace
97 ;; updates
98 (when clear-to-eos?
99 (move-cursor-to console init-row init-col)
100 (clear-to-eos console))
101
102 (let loop ((x init-col) (y init-row))
103 (let ([prev (or (vector-ref prevbuf (+ x (* y width))) empty)]
104 [cur (or (vector-ref curbuf (+ x (* y width))) empty)])
105 (unless (or (equal? prev cur)
106 (and (pair? cur) (eq? (car cur) #f))
107 (and clear-to-eos?
108 (eq? (car cur) #\space)))
109 (let ([newattr (if (pair? cur) (cdr cur) #f)])
110 (unless (equal? rattr newattr)
111 (if newattr
112 (set-character-attribute console newattr)
113 (reset-character-attribute console))
114 (set! rattr newattr)))
115 (unless (and (= x rx) (= y ry))
116 (move-cursor-to console y x))
117 (let ([ch (if (pair? cur) (car cur) #\space)])
118 (putch console ch)
119 (set! rx (+ x (char-width con ch))))
120 (set! ry y))
121 (let ([next-x (+ x 1)]
122 [next-y (+ y 1)])
123 (cond
124 [(and (eq? next-x width)
125 (eq? next-y height)) #t]
126 [(eq? next-x width)
127 (loop 0 next-y)]
128 [else
129 (loop next-x y)]))))
130
131 (move-cursor-to console (~ con'y) (~ con'x))
132 (unless (~ con'cursor-hidden)
133 (show-cursor console))
134 (vector-copy! (~ con'prev-buffer) 0 (~ con'buffer))
135 (set! (~ con'commands) '())))
136
137 (define-syntax save-command
138 (syntax-rules ()
139 ((_ con proc arg ...) (set! (~ con'commands) (cons (cut proc <> arg ...)
140 (~ con'commands))))))
141
142 (define-method clear-to-eos ((con <framebuffer-console>))
143 (save-command con clear-to-eos)
144 (vector-fill! (~ con'buffer)
145 '(#\space . #f)
146 (+ (~ con'x) (* (~ con'width) (~ con'y)))))
147
148 (define-method cursor-down/scroll-up ((con <framebuffer-console>)
149 :optional (y #f) (height #f)
150 (full-column-flag #f))
151 (save-command con cursor-down/scroll-up y height full-column-flag)
152 ;; Activate passthrough mode because we cannot handle this. All
153 ;; updates in (~ con'buffer) will be ignored. Instead, the command
154 ;; sequences we are storing will be replayed to the real console.
155 (set! (~ con'pass-through) #t)
156 (if (and y height (>= y (- height 1))) 0 1))
157
158 (define-method move-cursor-to ((con <framebuffer-console>) y x)
159 (save-command con move-cursor-to y x)
160 (set! (~ con'x) x)
161 (set! (~ con'y) y))
162
163 (define-method putch ((con <framebuffer-console>) c)
164 (save-command con putch c)
165 (let ([pos (yx->pos con (~ con'y) (~ con'x))])
166 (vector-set! (~ con'buffer)
167 pos
168 (cons c (~ con'attr)))
169 (let ([w (char-width con c)])
170 (when (> w 1)
171 (vector-fill! (~ con'buffer)
172 (cons #f #f) ; blank, occupied by the previous character
173 (+ pos 1)
174 (+ pos w)))
175 (inc! (~ con'x) w))))
176
177 (define-method putstr ((con <framebuffer-console>) s)
178 (string-for-each (cut putch con <>) s))
179
180 (define-method reset-character-attribute ((con <framebuffer-console>))
181 (save-command con reset-character-attribute)
182 (set! (~ con'attr) #f))
183
184 (define-method set-character-attribute ((con <framebuffer-console>) newattr)
185 (save-command con set-character-attribute newattr)
186 (set! (~ con'attr) newattr))
187
188 (define-method hide-cursor ((con <framebuffer-console>))
189 (save-command con hide-cursor)
190 (set! (~ con'cursor-hidden) #t))
191
192 (define-method show-cursor ((con <framebuffer-console>))
193 (save-command con show-cursor)
194 (set! (~ con'cursor-hidden) #f))