| 1 |
;;; |
| 2 |
;;; text.console.windows - windows console control |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2015 Hamayama https://github.com/Hamayama |
| 5 |
;;; Copyright (c) 2016-2024 Shiro Kawai <[email protected]> |
| 6 |
;;; |
| 7 |
;;; Redistribution and use in source and binary forms, with or without |
| 8 |
;;; modification, are permitted provided that the following conditions |
| 9 |
;;; are met: |
| 10 |
;;; |
| 11 |
;;; 1. Redistributions of source code must retain the above copyright |
| 12 |
;;; notice, this list of conditions and the following disclaimer. |
| 13 |
;;; |
| 14 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
;;; notice, this list of conditions and the following disclaimer in the |
| 16 |
;;; documentation and/or other materials provided with the distribution. |
| 17 |
;;; |
| 18 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 19 |
;;; may be used to endorse or promote products derived from this |
| 20 |
;;; software without specific prior written permission. |
| 21 |
;;; |
| 22 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 28 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 29 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
;;; |
| 34 |
|
| 35 |
;; This is autoloaded from text.console. |
| 36 |
;; The code is based on the works by Hamayama |
| 37 |
;; https://github.com/Hamayama/line-editor-gw |
| 38 |
|
| 39 |
(define-module text.console.windows |
| 40 |
(use os.windows) |
| 41 |
(use text.console) |
| 42 |
(use util.match) |
| 43 |
(use data.queue) |
| 44 |
(export <windows-console>)) |
| 45 |
(select-module text.console.windows) |
| 46 |
|
| 47 |
(define-class <windows-console> () |
| 48 |
(;; all slots are private |
| 49 |
(keybuf :init-form (make-queue)) |
| 50 |
(high-surrogate :init-value 0) |
| 51 |
)) |
| 52 |
|
| 53 |
;; These handles should not be cached. |
| 54 |
(define (get-ihandle) (sys-get-std-handle STD_INPUT_HANDLE)) |
| 55 |
(define (get-ohandle) (sys-get-std-handle STD_OUTPUT_HANDLE)) |
| 56 |
|
| 57 |
(define-method call-with-console ((con <windows-console>) proc |
| 58 |
:allow-other-keys) |
| 59 |
(unwind-protect (proc con) |
| 60 |
(reset-character-attribute con) |
| 61 |
(show-cursor con))) |
| 62 |
|
| 63 |
(define *win-virtual-key-table* |
| 64 |
(alist->hash-table |
| 65 |
'([38 . KEY_UP] |
| 66 |
[40 . KEY_DOWN] |
| 67 |
[39 . KEY_RIGHT] |
| 68 |
[37 . KEY_LEFT] |
| 69 |
[35 . KEY_END] |
| 70 |
[36 . KEY_HOME] |
| 71 |
[45 . KEY_INS] |
| 72 |
[46 . KEY_DEL] |
| 73 |
[34 . KEY_PGDN] |
| 74 |
[33 . KEY_PGUP] |
| 75 |
[112 . KEY_F1] |
| 76 |
[113 . KEY_F2] |
| 77 |
[114 . KEY_F3] |
| 78 |
[115 . KEY_F4] |
| 79 |
[116 . KEY_F5] |
| 80 |
[117 . KEY_F6] |
| 81 |
[118 . KEY_F7] |
| 82 |
[119 . KEY_F8] |
| 83 |
[120 . KEY_F9] |
| 84 |
[121 . KEY_F10] |
| 85 |
[122 . KEY_F11] |
| 86 |
[123 . KEY_F12]) |
| 87 |
eqv-comparator)) |
| 88 |
|
| 89 |
(define-method putch ((con <windows-console>) c) |
| 90 |
(sys-write-console (get-ohandle) (string c))) |
| 91 |
(define-method putstr ((con <windows-console>) s) |
| 92 |
(sys-write-console (get-ohandle) s)) |
| 93 |
|
| 94 |
;; some keyboard event constants |
| 95 |
(define-constant KEY_EVENT 1) |
| 96 |
(define-constant RIGHT_ALT_PRESSED #x01) |
| 97 |
(define-constant LEFT_ALT_PRESSED #x02) |
| 98 |
(define-constant RIGHT_CTRL_PRESSED #x04) |
| 99 |
(define-constant LEFT_CTRL_PRESSED #x08) |
| 100 |
(define-constant SHIFT_PRESSED #x10) |
| 101 |
(define-constant ALT_PRESSED (logior RIGHT_ALT_PRESSED LEFT_ALT_PRESSED)) |
| 102 |
(define-constant CTRL_PRESSED (logior RIGHT_CTRL_PRESSED LEFT_CTRL_PRESSED)) |
| 103 |
|
| 104 |
;; Obtain keyboard status |
| 105 |
(define (win-keystate) |
| 106 |
(let ([hdl (get-ihandle)] |
| 107 |
[kslist (make-queue)]) |
| 108 |
(let loop ([irlist (sys-peek-console-input hdl)]) |
| 109 |
(unless (null? irlist) |
| 110 |
(sys-read-console-input hdl) |
| 111 |
(dolist [ir irlist] |
| 112 |
(if (= (slot-ref ir'event-type) KEY_EVENT) |
| 113 |
(let* ([kdown (if (slot-ref ir'key.down) 1 0)] |
| 114 |
[ch (slot-ref ir'key.unicode-char)] |
| 115 |
[vk (slot-ref ir'key.virtual-key-code)] |
| 116 |
[ctls (slot-ref ir'key.control-key-state)]) |
| 117 |
(enqueue! kslist (list kdown ch vk ctls)) |
| 118 |
))) |
| 119 |
(loop (sys-peek-console-input hdl)))) |
| 120 |
(dequeue-all! kslist))) |
| 121 |
|
| 122 |
(define (%getch-sub con) |
| 123 |
(define ignorevk '(#x10 ; VK_SHIFT |
| 124 |
#x11 ; VK_CONTROL |
| 125 |
#x12 ; VK_MENU |
| 126 |
#x5b ; VK_LWIN |
| 127 |
#x5c ; VK_RWIN |
| 128 |
#x5d ; VK_APPS |
| 129 |
#xa0 ; VK_LSHIFT |
| 130 |
#xa1 ; VK_RSHIFT |
| 131 |
#xa2 ; VK_LCONTROL |
| 132 |
#xa3 ; VK_RCONTROL |
| 133 |
#xa4 ; VK_LMENU |
| 134 |
#xa5 ; VK_RMENU |
| 135 |
)) |
| 136 |
(define (get-ctrl-char vk) |
| 137 |
(cond [(<= #x41 vk #x5a) ; #\A-#\Z |
| 138 |
(integer->char (- (logand vk (lognot #x20)) #x40))] |
| 139 |
[else (case vk |
| 140 |
[(192) #\x00] ; #\@ |
| 141 |
[(219) #\x1b] ; #\[ |
| 142 |
[(220) #\x1c] ; #\\ |
| 143 |
[(221) #\x1d] ; #\] |
| 144 |
[(222) #\x1e] ; #\^ |
| 145 |
[(226) #\x1f] ; #\_ |
| 146 |
[else #\x00])])) |
| 147 |
(define (enqueue-keybuffer ch vk ctls) |
| 148 |
(cond |
| 149 |
[(hash-table-get *win-virtual-key-table* vk #f) |
| 150 |
=> (cut enqueue! (~ con'keybuf) <>)] |
| 151 |
[(and (logtest ctls ALT_PRESSED) (logtest ctls CTRL_PRESSED)) |
| 152 |
(enqueue! (~ con'keybuf) `(ALT ,(get-ctrl-char vk)))] |
| 153 |
[(logtest ctls ALT_PRESSED) |
| 154 |
(enqueue! (~ con'keybuf) `(ALT ,(integer->char ch)))] |
| 155 |
[(logtest ctls CTRL_PRESSED) |
| 156 |
(enqueue! (~ con'keybuf) (get-ctrl-char vk))] |
| 157 |
[else |
| 158 |
(enqueue! (~ con'keybuf) (integer->char ch))])) |
| 159 |
(dolist [ks (win-keystate)] |
| 160 |
(match-let1 (kdown ch vk ctls) ks |
| 161 |
(if (or (and (= kdown 1) (not (memv vk ignorevk))) |
| 162 |
;; workaround for windows terminal (windows 10) |
| 163 |
;; (this is required when we input surrogate pair characters) |
| 164 |
(and (= kdown 0) (= vk #x12) (not (= ch 0)))) |
| 165 |
;; process a surrogate pair |
| 166 |
(case (logand ch #xfc00) |
| 167 |
[(#xd800) ; high surrogate |
| 168 |
(set! (~ con'high-surrogate) ch)] |
| 169 |
[(#xdc00) ; low surrogate |
| 170 |
(unless (= (~ con'high-surrogate) 0) |
| 171 |
(set! ch (+ #x10000 |
| 172 |
(* (- (~ con'high-surrogate) #xd800) #x400) |
| 173 |
(- ch #xdc00))) |
| 174 |
(enqueue-keybuffer ch vk ctls) |
| 175 |
(set! (~ con'high-surrogate) 0))] |
| 176 |
[else |
| 177 |
;; drop some input for windows terminal (windows 10) |
| 178 |
;; (they appear when we input surrogate pair characters) |
| 179 |
(unless (and (= ch 0) |
| 180 |
(or (= vk #x66) (= vk #x63)) |
| 181 |
(logtest ctls LEFT_ALT_PRESSED)) |
| 182 |
(enqueue-keybuffer ch vk ctls) |
| 183 |
(set! (~ con'high-surrogate) 0))]) |
| 184 |
)))) |
| 185 |
|
| 186 |
;; Default - gray foreground, black background |
| 187 |
(define *win-default-cattr* |
| 188 |
(logior FOREGROUND_BLUE FOREGROUND_GREEN FOREGROUND_RED)) |
| 189 |
|
| 190 |
;; Get a char; returns a char, or #f on timeout. |
| 191 |
;; The timeout argument is in us. |
| 192 |
(define-method getch ((con <windows-console>) :optional (timeout #f)) |
| 193 |
;; Windows timer has rather coarse resolution (10ms) |
| 194 |
(define resolution-us 10000) |
| 195 |
(let loop ([t 0]) |
| 196 |
(%getch-sub con) |
| 197 |
(if (not (queue-empty? (~ con'keybuf))) |
| 198 |
(dequeue! (~ con'keybuf)) |
| 199 |
(if (and timeout (>= t timeout)) |
| 200 |
#f ; timeout |
| 201 |
(begin (sys-nanosleep (* resolution-us 1000)) |
| 202 |
(if timeout |
| 203 |
(loop (+ t resolution-us)) |
| 204 |
(loop 0))))))) |
| 205 |
|
| 206 |
(define-method get-raw-chars ((con <windows-console>)) |
| 207 |
;; Windows timer has rather coarse resolution (10ms) |
| 208 |
(define resolution-us 10000) |
| 209 |
(define q (make-queue)) |
| 210 |
(while (queue-empty? q) |
| 211 |
(sys-nanosleep (* resolution-us 1000)) |
| 212 |
(dolist [ks (win-keystate)] |
| 213 |
(match-let1 (kdown ch vk ctls) ks |
| 214 |
(when (= kdown 1) |
| 215 |
(enqueue! q (list (integer->char ch) vk (logand ctls #x1f))))))) |
| 216 |
(dequeue-all! q)) |
| 217 |
|
| 218 |
(define-method chready? ((con <windows-console>)) |
| 219 |
(%getch-sub con) |
| 220 |
(not (queue-empty? (~ con'keybuf)))) |
| 221 |
|
| 222 |
;; Windows console doesn't have different modes. It's mostly irrelevant |
| 223 |
;; for it also lacks shell job control. We just return #t for the completeness. |
| 224 |
(define-method canonical-mode? ((con <windows-console>)) |
| 225 |
#t) |
| 226 |
|
| 227 |
(define-method query-cursor-position ((con <windows-console>)) |
| 228 |
(let1 cinfo (sys-get-console-screen-buffer-info (get-ohandle)) |
| 229 |
(values (slot-ref cinfo'cursor-position.y) |
| 230 |
(slot-ref cinfo'cursor-position.x)))) |
| 231 |
|
| 232 |
(define-method move-cursor-to ((con <windows-console>) y x) |
| 233 |
(sys-set-console-cursor-position (get-ohandle) x y)) |
| 234 |
|
| 235 |
(define-method reset-terminal ((con <windows-console>)) |
| 236 |
(clear-screen con) |
| 237 |
(reset-character-attribute con) |
| 238 |
(show-cursor con)) |
| 239 |
|
| 240 |
(define-method clear-screen ((con <windows-console>)) |
| 241 |
(let* ([hdl (get-ohandle)] |
| 242 |
[cinfo (sys-get-console-screen-buffer-info hdl)] |
| 243 |
[sbw (slot-ref cinfo'size.x)] |
| 244 |
[sbh (slot-ref cinfo'size.y)]) |
| 245 |
(let1 n (* sbw sbh) |
| 246 |
(sys-fill-console-output-attribute hdl *win-default-cattr* n 0 0) |
| 247 |
(sys-fill-console-output-character hdl #\space n 0 0)) |
| 248 |
(sys-set-console-cursor-position hdl 0 0))) |
| 249 |
|
| 250 |
(define-method clear-to-eol ((con <windows-console>)) |
| 251 |
(let* ([hdl (get-ohandle)] |
| 252 |
[cinfo (sys-get-console-screen-buffer-info hdl)] |
| 253 |
[x (slot-ref cinfo'cursor-position.x)] |
| 254 |
[y (slot-ref cinfo'cursor-position.y)] |
| 255 |
[sbw (slot-ref cinfo'size.x)]) |
| 256 |
(let1 n (- sbw x) |
| 257 |
(sys-fill-console-output-attribute hdl *win-default-cattr* n x y) |
| 258 |
(sys-fill-console-output-character hdl #\space n x y)))) |
| 259 |
|
| 260 |
(define-method clear-to-eos ((con <windows-console>)) |
| 261 |
(let* ([hdl (get-ohandle)] |
| 262 |
[cinfo (sys-get-console-screen-buffer-info hdl)] |
| 263 |
[x (slot-ref cinfo'cursor-position.x)] |
| 264 |
[y (slot-ref cinfo'cursor-position.y)] |
| 265 |
[sbw (slot-ref cinfo'size.x)] |
| 266 |
[sbh (slot-ref cinfo'size.y)]) |
| 267 |
(let1 n (* sbw (- sbh y)) |
| 268 |
(sys-fill-console-output-attribute hdl *win-default-cattr* n x y) |
| 269 |
(sys-fill-console-output-character hdl #\space n x y)))) |
| 270 |
|
| 271 |
(define-method hide-cursor ((con <windows-console>)) |
| 272 |
(let1 hdl (get-ohandle) |
| 273 |
(receive (sz v) (sys-get-console-cursor-info hdl) |
| 274 |
(sys-set-console-cursor-info hdl sz #f)))) |
| 275 |
|
| 276 |
(define-method show-cursor ((con <windows-console>)) |
| 277 |
(let1 hdl (get-ohandle) |
| 278 |
(receive (sz v) (sys-get-console-cursor-info hdl) |
| 279 |
(sys-set-console-cursor-info hdl sz #t)))) |
| 280 |
|
| 281 |
;; If the cursor is on the last line, scroll up and make a room |
| 282 |
;; at the bottom. This is to workaround windows IME bug: |
| 283 |
;; When windows ime is on, writing a newline character |
| 284 |
;; to the last line causes a system error. |
| 285 |
(define-method ensure-bottom-room ((con <windows-console>) |
| 286 |
:optional (full-column-flag #f)) |
| 287 |
(let* ([hdl (get-ohandle)] |
| 288 |
[cinfo (sys-get-console-screen-buffer-info hdl)] |
| 289 |
[sbw (slot-ref cinfo'size.x)] |
| 290 |
[sbh (slot-ref cinfo'size.y)]) |
| 291 |
(receive (y1 x1) (query-cursor-position con) |
| 292 |
(when (>= y1 (- sbh 1)) |
| 293 |
(guard (e [(<system-error> e) |
| 294 |
;; When windows ime is on, a full column wrapping |
| 295 |
;; causes one more line scroll-up. |
| 296 |
;; So we don't write a newline character in this case. |
| 297 |
(if (not full-column-flag) |
| 298 |
;; When windows ime is on, the space character |
| 299 |
;; before a newline character is important |
| 300 |
;; in order to avoid a system error. |
| 301 |
(sys-write-console hdl " \n"))]) |
| 302 |
(sys-write-console hdl "\n")) |
| 303 |
(receive (y2 x2) (query-cursor-position con) |
| 304 |
(move-cursor-to con (- y2 1) x1)) |
| 305 |
)))) |
| 306 |
|
| 307 |
(define-method cursor-down/scroll-up ((con <windows-console>) |
| 308 |
:optional (y #f) (height #f) |
| 309 |
(full-column-flag #f)) |
| 310 |
;; When windows ime is on, a full column wrapping |
| 311 |
;; causes one more line scroll-up. |
| 312 |
;; So we must deal with this problem. |
| 313 |
(ensure-bottom-room con full-column-flag) |
| 314 |
|
| 315 |
;; move cursor to the next line |
| 316 |
(receive (y1 x1) (query-cursor-position con) |
| 317 |
(move-cursor-to con (+ y1 1) x1)) |
| 318 |
|
| 319 |
;; We have to make a room on the last line of console, |
| 320 |
;; because windows ime overwrites the last line and causes |
| 321 |
;; a system error. |
| 322 |
(ensure-bottom-room con full-column-flag) |
| 323 |
|
| 324 |
;; return the difference of the cursor position y |
| 325 |
(receive (y2 x2) (query-cursor-position con) |
| 326 |
(if y (- y2 y) 1))) |
| 327 |
|
| 328 |
(define-method cursor-up/scroll-down ((con <windows-console>) |
| 329 |
:optional (y #f)) |
| 330 |
;; move cursor to the previous line |
| 331 |
(receive (y1 x1) (query-cursor-position con) |
| 332 |
(move-cursor-to con (max (- y1 1) 0) x1)) |
| 333 |
|
| 334 |
;; return the difference of the cursor position y |
| 335 |
(if (and y (<= y 0)) 0 -1)) |
| 336 |
|
| 337 |
(define-method query-screen-size ((con <windows-console>) |
| 338 |
:optional (clear-cache #f)) |
| 339 |
;; we assume querying screen size is light enough, so we don't cache |
| 340 |
;; the result. |
| 341 |
(let1 cinfo (sys-get-console-screen-buffer-info (get-ohandle)) |
| 342 |
(values (+ 1 (- (slot-ref cinfo'window.bottom) |
| 343 |
(slot-ref cinfo'window.top))) |
| 344 |
(+ 1 (- (slot-ref cinfo'window.right) |
| 345 |
(slot-ref cinfo'window.left)))))) |
| 346 |
|
| 347 |
(define-method set-character-attribute ((con <windows-console>) spec) |
| 348 |
(define B 1) |
| 349 |
(define G 2) |
| 350 |
(define R 4) |
| 351 |
(define I 8) ; intensity |
| 352 |
(define (get-color-code color default-color) |
| 353 |
(case color |
| 354 |
[(black) 0] |
| 355 |
[(red) R] |
| 356 |
[(green) G] |
| 357 |
[(yellow) (logior R G)] |
| 358 |
[(blue) B] |
| 359 |
[(magenta) (logior B R)] |
| 360 |
[(cyan) (logior B G)] |
| 361 |
[(white) (logior R G B)] |
| 362 |
[else default-color])) |
| 363 |
(define (get-optional-code opt) |
| 364 |
(case opt |
| 365 |
[(bright) I] |
| 366 |
[else 0])) ; underscore not supported |
| 367 |
(define (get-color-attr fc bc) |
| 368 |
(logior (if (logtest fc B) FOREGROUND_BLUE 0) |
| 369 |
(if (logtest fc G) FOREGROUND_GREEN 0) |
| 370 |
(if (logtest fc R) FOREGROUND_RED 0) |
| 371 |
(if (logtest fc I) FOREGROUND_INTENSITY 0) |
| 372 |
(if (logtest bc B) BACKGROUND_BLUE 0) |
| 373 |
(if (logtest bc G) BACKGROUND_GREEN 0) |
| 374 |
(if (logtest bc R) BACKGROUND_RED 0) |
| 375 |
(if (logtest bc I) BACKGROUND_INTENSITY 0))) |
| 376 |
(match-let1 (fgcolor bgcolor . opts) spec |
| 377 |
(let ([fc (get-color-code fgcolor (logior R G B))] |
| 378 |
[bc (get-color-code bgcolor 0)]) |
| 379 |
(dolist [opt opts] |
| 380 |
(set! fc (logior fc (get-optional-code opt)))) |
| 381 |
(if (memq 'reverse opts) |
| 382 |
(set!-values (fc bc) (values bc fc))) |
| 383 |
(sys-set-console-text-attribute (get-ohandle) |
| 384 |
(get-color-attr fc bc))))) |
| 385 |
|
| 386 |
(define-method reset-character-attribute ((con <windows-console>)) |
| 387 |
(sys-set-console-text-attribute (get-ohandle) *win-default-cattr*)) |
| 388 |
|
| 389 |
(define-method with-character-attribute ((con <windows-console>) |
| 390 |
attrs thunk) |
| 391 |
(unwind-protect |
| 392 |
(begin |
| 393 |
(set-character-attribute con attrs) |
| 394 |
(thunk)) |
| 395 |
(reset-character-attribute con))) |
| 396 |
|
| 397 |
(define-method beep ((con <windows-console>)) |
| 398 |
(putch con #\alarm)) |