| 1 |
;;; |
| 2 |
;;; console.stub - console-related apis |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2010-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; NB: It appears that, on MinGW, you cannot obtain an usable console |
| 35 |
;; output handle by GetStdHandle() if you allocated a console by |
| 36 |
;; AllocConsole(). Maybe it is caused from the difference of startup |
| 37 |
;; code between MinGW and MSVC. The fact makes these procedures |
| 38 |
;; little usable. Nevertheless, I put it here hoping someday the |
| 39 |
;; issue is addressed. |
| 40 |
|
| 41 |
(in-module os.windows) |
| 42 |
|
| 43 |
(inline-stub |
| 44 |
(.include "gauche/extend.h") |
| 45 |
(.when (defined GAUCHE_WINDOWS) |
| 46 |
|
| 47 |
;; ConsoleBufferHandle |
| 48 |
;(define-cfn make_console_buffer (h::HANDLE) :static |
| 49 |
; (return (Scm_MakeWinHandle h 'console-buffer))) |
| 50 |
;(define-cfn console_buffer (h) ::HANDLE :static |
| 51 |
; (return (Scm_WinHandle h 'console-buffer))) |
| 52 |
|
| 53 |
(define-cise-stmt handle |
| 54 |
[(_ type expr) |
| 55 |
(let1 r (gensym) |
| 56 |
`(let* ([,r :: HANDLE ,expr]) |
| 57 |
(when (== ,r INVALID_HANDLE_VALUE) |
| 58 |
(Scm_SysError ,#`",(car expr) failed")) |
| 59 |
(result (Scm_MakeWinHandle ,r ,type))))]) |
| 60 |
|
| 61 |
(define-cise-stmt check |
| 62 |
[(_ expr) |
| 63 |
(let1 r (gensym) |
| 64 |
`(let* ([,r :: BOOL ,expr]) |
| 65 |
(when (== ,r 0) (Scm_SysError ,#`",(car expr) failed"))))]) |
| 66 |
|
| 67 |
(define-cise-expr DWORD [(_ expr) `(cast DWORD ,expr)]) |
| 68 |
|
| 69 |
;; |
| 70 |
;; Console procedures |
| 71 |
;; |
| 72 |
(define-cproc sys-alloc-console () ::<void> (check (AllocConsole))) |
| 73 |
(define-cproc sys-free-console () ::<void> (check (FreeConsole))) |
| 74 |
|
| 75 |
(define-enum CTRL_C_EVENT) |
| 76 |
(define-enum CTRL_BREAK_EVENT) |
| 77 |
|
| 78 |
(define-cproc sys-generate-console-ctrl-event (event::<int> pgid::<uint>) |
| 79 |
::<void> |
| 80 |
(check (GenerateConsoleCtrlEvent (DWORD event) (DWORD pgid)))) |
| 81 |
|
| 82 |
;; |
| 83 |
;; Console Buffers |
| 84 |
;; |
| 85 |
|
| 86 |
(define-enum GENERIC_READ) |
| 87 |
(define-enum GENERIC_WRITE) |
| 88 |
(define-enum FILE_SHARE_READ) |
| 89 |
(define-enum FILE_SHARE_WRITE) |
| 90 |
|
| 91 |
(define-cproc sys-create-console-screen-buffer (desired-access::<int> |
| 92 |
share-mode::<uint> |
| 93 |
inheritable::<boolean>) |
| 94 |
(let* ([sa::SECURITY_ATTRIBUTES]) |
| 95 |
(when inheritable |
| 96 |
(= (ref sa nLength) (sizeof SECURITY_ATTRIBUTES) |
| 97 |
(ref sa lpSecurityDescriptor) NULL |
| 98 |
(ref sa bInheritHandle) TRUE)) |
| 99 |
(handle 'console-buffer |
| 100 |
(CreateConsoleScreenBuffer desired-access share-mode |
| 101 |
(?: inheritable (& sa) NULL) |
| 102 |
CONSOLE_TEXTMODE_BUFFER NULL)))) |
| 103 |
|
| 104 |
(define-cproc sys-set-console-active-screen-buffer (h) ::<void> |
| 105 |
(check (SetConsoleActiveScreenBuffer (Scm_WinHandle h '#f)))) |
| 106 |
|
| 107 |
(define-cproc sys-scroll-console-screen-buffer (handle |
| 108 |
scroll-rectangle::<s16vector> |
| 109 |
clip-rectangle::<s16vector>? |
| 110 |
x::<short> y::<short> |
| 111 |
fill::<ulong>) |
| 112 |
::<void> |
| 113 |
(when (< (SCM_UVECTOR_SIZE scroll-rectangle) 4) |
| 114 |
(Scm_Error "s16vector of minimum length 4 required for scroll-rectangle: %S" |
| 115 |
scroll-rectangle)) |
| 116 |
(when (and clip-rectangle (< (SCM_UVECTOR_SIZE clip-rectangle) 4)) |
| 117 |
(Scm_Error "s16vector of minimum length 4 required for clip-rectangle: %S" |
| 118 |
clip-rectangle)) |
| 119 |
(let* ([c::COORD] [ci::CHAR_INFO]) |
| 120 |
(= (ref c X) x (ref c Y) y) |
| 121 |
(memcpy (& ci) (& fill) (sizeof CHAR_INFO)) |
| 122 |
(check (ScrollConsoleScreenBuffer |
| 123 |
(Scm_WinHandle handle '#f) |
| 124 |
(cast (SMALL_RECT*) (SCM_UVECTOR_ELEMENTS scroll-rectangle)) |
| 125 |
(?: clip-rectangle (cast (SMALL_RECT*) (SCM_UVECTOR_ELEMENTS clip-rectangle)) NULL) |
| 126 |
c (& ci))))) |
| 127 |
|
| 128 |
;; |
| 129 |
;; Console Code Page |
| 130 |
;; |
| 131 |
(define-cproc sys-get-console-cp () ::<uint> GetConsoleCP) |
| 132 |
(define-cproc sys-get-console-output-cp () ::<uint> GetConsoleOutputCP) |
| 133 |
(define-cproc sys-set-console-cp (cp::<uint>) ::<void> |
| 134 |
(check (SetConsoleCP cp))) |
| 135 |
(define-cproc sys-set-console-output-cp (cp::<uint>) ::<void> |
| 136 |
(check (SetConsoleOutputCP cp))) |
| 137 |
|
| 138 |
|
| 139 |
;; |
| 140 |
;; Console Cursor Info |
| 141 |
;; |
| 142 |
(define-cproc sys-get-console-cursor-info (h) ::(<int> <boolean>) |
| 143 |
(let* ([ci::CONSOLE_CURSOR_INFO]) |
| 144 |
(check (GetConsoleCursorInfo (Scm_WinHandle h '#f) (& ci))) |
| 145 |
(result (cast int (ref ci dwSize)) (ref ci bVisible)))) |
| 146 |
(define-cproc sys-set-console-cursor-info (h size::<int> visible::<boolean>) |
| 147 |
::<void> |
| 148 |
(let* ([ci::CONSOLE_CURSOR_INFO]) |
| 149 |
(= (ref ci dwSize) size (ref ci bVisible) visible) |
| 150 |
(check (SetConsoleCursorInfo (Scm_WinHandle h '#f) (& ci))))) |
| 151 |
|
| 152 |
(define-cproc sys-set-console-cursor-position (h x::<short> y::<short>) |
| 153 |
::<void> |
| 154 |
(let* ([c::COORD]) |
| 155 |
(= (ref c X) x (ref c Y) y) |
| 156 |
(check (SetConsoleCursorPosition (Scm_WinHandle h '#f) c)))) |
| 157 |
|
| 158 |
;; |
| 159 |
;; Console Mode |
| 160 |
;; |
| 161 |
(define-enum ENABLE_LINE_INPUT) |
| 162 |
(define-enum ENABLE_ECHO_INPUT) |
| 163 |
(define-enum ENABLE_PROCESSED_INPUT) |
| 164 |
(define-enum ENABLE_WINDOW_INPUT) |
| 165 |
(define-enum ENABLE_MOUSE_INPUT) |
| 166 |
(define-enum ENABLE_PROCESSED_OUTPUT) |
| 167 |
(define-enum ENABLE_WRAP_AT_EOL_OUTPUT) |
| 168 |
|
| 169 |
(define-cproc sys-get-console-mode (h) ::<uint> |
| 170 |
(let* ([m::DWORD 0]) |
| 171 |
(check (GetConsoleMode (Scm_WinHandle h '#f) (& m))) |
| 172 |
(result m))) |
| 173 |
|
| 174 |
(define-cproc sys-set-console-mode (h mode::<uint>) ::<void> |
| 175 |
(check (SetConsoleMode (Scm_WinHandle h '#f) mode))) |
| 176 |
|
| 177 |
|
| 178 |
;; |
| 179 |
;; Console Screen Buffer Info |
| 180 |
;; |
| 181 |
(declcode "#include <gauche/class.h>" |
| 182 |
"typedef struct ScmWinConsoleScreenBufferInfoRec {" |
| 183 |
" SCM_HEADER;" |
| 184 |
" CONSOLE_SCREEN_BUFFER_INFO info;" |
| 185 |
"} ScmWinConsoleScreenBufferInfo;" |
| 186 |
"SCM_CLASS_DECL(Scm_WinConsoleScreenBufferInfoClass);" |
| 187 |
"#define SCM_WIN_CONSOLE_SCREEN_BUFFER_INFO_P(obj) \ |
| 188 |
SCM_XTYPEP(obj, &Scm_WinConsoleScreenBufferInfoClass)" |
| 189 |
"#define SCM_WIN_CONSOLE_SCREEN_BUFFER_INFO(obj) \ |
| 190 |
((ScmWinConsoleScreenBufferInfo*)obj)") |
| 191 |
|
| 192 |
(define-cfn make-console-screen-buffer-info () :static |
| 193 |
(let* ([z::ScmWinConsoleScreenBufferInfo* |
| 194 |
(SCM_NEW ScmWinConsoleScreenBufferInfo)]) |
| 195 |
(SCM_SET_CLASS z (& Scm_WinConsoleScreenBufferInfoClass)) |
| 196 |
(return (SCM_OBJ z)))) |
| 197 |
|
| 198 |
(declare-stub-type <win:console-screen-buffer-info> |
| 199 |
"ScmWinConsoleScreenBufferInfo*" "CONSOLE_SCREEN_BUFFER_INFO" |
| 200 |
"SCM_WIN_CONSOLE_SCREEN_BUFFER_INFO_P" |
| 201 |
"SCM_WIN_CONSOLE_SCREEN_BUFFER_INFO") |
| 202 |
|
| 203 |
(define-cclass <win:console-screen-buffer-info> |
| 204 |
"ScmWinConsoleScreenBufferInfo*" "Scm_WinConsoleScreenBufferInfoClass" () |
| 205 |
((size.x :c-name "info.dwSize.X" :type <short>) |
| 206 |
(size.y :c-name "info.dwSize.Y" :type <short>) |
| 207 |
(cursor-position.x :c-name "info.dwCursorPosition.X" :type <short>) |
| 208 |
(cursor-position.y :c-name "info.dwCursorPosition.Y" :type <short>) |
| 209 |
(attributes :c-name "info.wAttributes" :type <uint>) |
| 210 |
(window.left :c-name "info.srWindow.Left" :type <short>) |
| 211 |
(window.top :c-name "info.srWindow.Top" :type <short>) |
| 212 |
(window.right :c-name "info.srWindow.Right" :type <short>) |
| 213 |
(window.bottom :c-name "info.srWindow.Bottom" :type <short>) |
| 214 |
(maximum-window-size.x :c-name "info.dwMaximumWindowSize.X" :type <short>) |
| 215 |
(maximum-window-size.y :c-name "info.dwMaximumWindowSize.Y" :type <short>)) |
| 216 |
(allocator (c "make_console_screen_buffer_info"))) |
| 217 |
|
| 218 |
(define-enum FOREGROUND_BLUE) |
| 219 |
(define-enum FOREGROUND_GREEN) |
| 220 |
(define-enum FOREGROUND_RED) |
| 221 |
(define-enum FOREGROUND_INTENSITY) |
| 222 |
(define-enum BACKGROUND_BLUE) |
| 223 |
(define-enum BACKGROUND_GREEN) |
| 224 |
(define-enum BACKGROUND_RED) |
| 225 |
(define-enum BACKGROUND_INTENSITY) |
| 226 |
|
| 227 |
(define-cproc sys-get-console-screen-buffer-info (h) |
| 228 |
(let* ([info (make-console-screen-buffer-info)]) |
| 229 |
(check (GetConsoleScreenBufferInfo |
| 230 |
(Scm_WinHandle h '#f) |
| 231 |
(& (-> (SCM_WIN_CONSOLE_SCREEN_BUFFER_INFO info) info)))) |
| 232 |
(result info))) |
| 233 |
|
| 234 |
(define-cproc sys-get-largest-console-window-size (h) ::(<int> <int>) |
| 235 |
(let* ([c::COORD (GetLargestConsoleWindowSize (Scm_WinHandle h '#f))]) |
| 236 |
(result (ref c X) (ref c Y)))) |
| 237 |
|
| 238 |
(define-cproc sys-set-screen-buffer-size (h x::<short> y::<short>) ::<void> |
| 239 |
(let* ([c::COORD]) |
| 240 |
(= (ref c X) x (ref c Y) y) |
| 241 |
(check (SetConsoleScreenBufferSize (Scm_WinHandle h '#f) c)))) |
| 242 |
|
| 243 |
;; |
| 244 |
;; Console input |
| 245 |
;; |
| 246 |
|
| 247 |
(declcode "typedef struct ScmWinInputRecordRec {" |
| 248 |
" SCM_HEADER;" |
| 249 |
" INPUT_RECORD rec;" |
| 250 |
"} ScmWinInputRecord;" |
| 251 |
"SCM_CLASS_DECL(Scm_WinInputRecordClass);" |
| 252 |
"#define SCM_WIN_INPUT_RECORD_P(obj) \ |
| 253 |
SCM_XTYPEP(obj, &Scm_WinInputRecordClass)" |
| 254 |
"#define SCM_WIN_INPUT_RECORD(obj) \ |
| 255 |
((ScmWinInputRecord*)obj)") |
| 256 |
|
| 257 |
(define-cfn make-input-record () :static |
| 258 |
(let* ([z::ScmWinInputRecord* (SCM_NEW ScmWinInputRecord)]) |
| 259 |
(SCM_SET_CLASS z (& Scm_WinInputRecordClass)) |
| 260 |
(return (SCM_OBJ z)))) |
| 261 |
|
| 262 |
(declare-stub-type <win:input-record> |
| 263 |
"ScmWinInputRecord*" "INPUT_RECORD" |
| 264 |
"SCM_WIN_INPUT_RECORD_P" |
| 265 |
"SCM_WIN_INPUT_RECORD") |
| 266 |
|
| 267 |
(define-cclass <win:input-record> |
| 268 |
"ScmWinInputRecord*" "Scm_WinInputRecordClass" () |
| 269 |
((event-type :c-name "rec.EventType" :type <int>) |
| 270 |
(key.down :c-name "rec.Event.KeyEvent.bKeyDown" |
| 271 |
:type <boolean>) |
| 272 |
(key.repeat-count :c-name "rec.Event.KeyEvent.wRepeatCount" |
| 273 |
:type <int>) |
| 274 |
(key.virtual-key-code :c-name "rec.Event.KeyEvent.wVirtualKeyCode" |
| 275 |
:type <int>) |
| 276 |
(key.virtual-scan-code :c-name "rec.Event.KeyEvent.wVirtualScanCode" |
| 277 |
:type <int>) |
| 278 |
(key.unicode-char :c-name "rec.Event.KeyEvent.uChar.UnicodeChar" |
| 279 |
:type <uint>) |
| 280 |
(key.ascii-char :c-name "rec.Event.KeyEvent.uChar.AsciiChar" |
| 281 |
:type <uint>) |
| 282 |
(key.control-key-state :c-name "rec.Event.KeyEvent.dwControlKeyState" |
| 283 |
:type <uint>) |
| 284 |
(mouse.x :c-name "rec.Event.MouseEvent.dwMousePosition.X" |
| 285 |
:type <short>) |
| 286 |
(mouse.y :c-name "rec.Event.MouseEvent.dwMousePosition.Y" |
| 287 |
:type <short>) |
| 288 |
(mouse.button-state :c-name "rec.Event.MouseEvent.dwButtonState" |
| 289 |
:type <uint>) |
| 290 |
(mouse.control-key-state :c-name "rec.Event.MouseEvent.dwControlKeyState" |
| 291 |
:type <uint>) |
| 292 |
(mouse.event-flags :c-name "rec.Event.MouseEvent.dwEventFlags" |
| 293 |
:type <uint>) |
| 294 |
(window-buffer-size.x :c-name "rec.Event.WindowBufferSizeEvent.dwSize.X" |
| 295 |
:type <short>) |
| 296 |
(window-buffer-size.y :c-name "rec.Event.WindowBufferSizeEvent.dwSize.Y" |
| 297 |
:type <short>) |
| 298 |
(menu.command-id :c-name "rec.Event.MenuEvent.dwCommandId" |
| 299 |
:type <uint>) |
| 300 |
(focus.set-focus :c-name "rec.Event.FocusEvent.bSetFocus" |
| 301 |
:type <boolean>)) |
| 302 |
(allocator (c "make_input_record"))) |
| 303 |
|
| 304 |
(define-cproc sys-get-number-of-console-input-events (h) ::<uint> |
| 305 |
(let* ([num::DWORD 0]) |
| 306 |
(check (GetNumberOfConsoleInputEvents (Scm_WinHandle h '#f) (& num))) |
| 307 |
(result num))) |
| 308 |
|
| 309 |
(define-cproc sys-get-number-of-console-mouse-buttons () ::<uint> |
| 310 |
(let* ([num::DWORD 0]) |
| 311 |
(check (GetNumberOfConsoleMouseButtons (& num))) |
| 312 |
(result num))) |
| 313 |
|
| 314 |
;; Later we may support passing input record buffers and/or |
| 315 |
;; retrieving multiple input records at once. |
| 316 |
(define-cise-stmt peek/read-console-input |
| 317 |
[(_ proc) |
| 318 |
`(let* ([rec (make-input-record)] [cnt::DWORD 0]) |
| 319 |
(check (,proc (Scm_WinHandle h '#f) |
| 320 |
(& (-> (SCM_WIN_INPUT_RECORD rec) rec)) |
| 321 |
1 (& cnt))) |
| 322 |
(if (== cnt 0) (result SCM_NIL) (result (list rec))))]) |
| 323 |
|
| 324 |
(define-cproc sys-peek-console-input (h) |
| 325 |
(peek/read-console-input PeekConsoleInput)) |
| 326 |
(define-cproc sys-read-console-input (h) |
| 327 |
(peek/read-console-input ReadConsoleInput)) |
| 328 |
|
| 329 |
(define-cproc sys-read-console (h buf::<uvector>) ::<uint> |
| 330 |
(unless (or (SCM_U8VECTORP buf) (SCM_U16VECTORP buf)) |
| 331 |
(Scm_TypeError "buf" "u8vector or u16vector" (SCM_OBJ buf))) |
| 332 |
(SCM_UVECTOR_CHECK_MUTABLE buf) |
| 333 |
(let* ([nc::DWORD (/ (Scm_UVectorSizeInBytes buf) (sizeof TCHAR))] |
| 334 |
[nread::DWORD 0]) |
| 335 |
(check (ReadConsole (Scm_WinHandle h '#f) |
| 336 |
(SCM_UVECTOR_ELEMENTS buf) |
| 337 |
nc (& nread) NULL)) |
| 338 |
(result nread))) |
| 339 |
|
| 340 |
(define-cproc sys-read-console-output (handle |
| 341 |
buf::<u32vector> |
| 342 |
w::<short> h::<short> |
| 343 |
x::<short> y::<short> |
| 344 |
region::<s16vector>) |
| 345 |
(when (< (SCM_UVECTOR_SIZE buf) (* w h)) |
| 346 |
(Scm_Error "ReadConsoleOutput: buffer argument too small \ |
| 347 |
(required at least %dx%d)" w h)) |
| 348 |
(when (< (SCM_UVECTOR_SIZE region) 4) |
| 349 |
(Scm_Error "ReadConsoleOutput: region argument must be at least \ |
| 350 |
4 elements long: %S" region)) |
| 351 |
(SCM_UVECTOR_CHECK_MUTABLE buf) |
| 352 |
(SCM_UVECTOR_CHECK_MUTABLE region) |
| 353 |
(let* ([siz::COORD] [coord::COORD]) |
| 354 |
(= (ref siz X) w (ref siz Y) h |
| 355 |
(ref coord X) x (ref coord Y) y) |
| 356 |
(check (ReadConsoleOutput (Scm_WinHandle handle '#f) |
| 357 |
(cast PCHAR_INFO (SCM_UVECTOR_ELEMENTS buf)) |
| 358 |
siz coord |
| 359 |
(cast PSMALL_RECT (SCM_UVECTOR_ELEMENTS region)))) |
| 360 |
(result (SCM_OBJ buf)))) |
| 361 |
|
| 362 |
(define-cproc sys-read-console-output-attribute (handle |
| 363 |
buf::<u16vector> |
| 364 |
x::<short> y::<short>) |
| 365 |
::<uint> |
| 366 |
(let* ([len::DWORD (SCM_UVECTOR_SIZE buf)] [nread::DWORD] [coord::COORD]) |
| 367 |
(= (ref coord X) x (ref coord Y) y) |
| 368 |
(check (ReadConsoleOutputAttribute (Scm_WinHandle handle '#f) |
| 369 |
(cast LPWORD (SCM_UVECTOR_ELEMENTS buf)) |
| 370 |
len coord (& nread))) |
| 371 |
(result nread))) |
| 372 |
|
| 373 |
|
| 374 |
(define-cproc sys-read-console-output-character (handle |
| 375 |
len::<uint> |
| 376 |
x::<short> y::<short>) |
| 377 |
::<const-cstring> |
| 378 |
(when (> len USHRT_MAX) |
| 379 |
(Scm_Error "ReadConsoleOutputCharacter: length argument too large")) |
| 380 |
(let* ([coord::COORD] [nread::DWORD 0] |
| 381 |
[pbuf::LPTSTR (SCM_NEW_ATOMIC_ARRAY TCHAR (+ len 1))]) |
| 382 |
(= (ref coord X) x (ref coord Y) y) |
| 383 |
(check (ReadConsoleOutputCharacter (Scm_WinHandle handle '#f) pbuf len coord (& nread))) |
| 384 |
(= (aref pbuf nread) 0) |
| 385 |
(result (SCM_WCS2MBS pbuf)))) |
| 386 |
|
| 387 |
(define-cproc sys-set-console-text-attribute (h attr::<ushort>) ::<void> |
| 388 |
(check (SetConsoleTextAttribute (Scm_WinHandle h '#f) attr))) |
| 389 |
|
| 390 |
(define-cproc sys-set-console-window-info (h absolute::<boolean> |
| 391 |
window::<s16vector>) |
| 392 |
::<void> |
| 393 |
(when (< (SCM_UVECTOR_SIZE window) 4) |
| 394 |
(Scm_Error "s16vector of minimum length 4 required for window: %S" |
| 395 |
window)) |
| 396 |
(check (SetConsoleWindowInfo (Scm_WinHandle h '#f) absolute |
| 397 |
(cast (SMALL_RECT*) |
| 398 |
(SCM_UVECTOR_ELEMENTS window))))) |
| 399 |
|
| 400 |
(define-cproc sys-write-console (h s::<string>) ::<uint> |
| 401 |
(let* ([wcs::LPCTSTR (SCM_MBS2WCS (Scm_GetStringConst s))] |
| 402 |
[nwritten::DWORD 0]) |
| 403 |
(check (WriteConsole (Scm_WinHandle h '#f) |
| 404 |
wcs |
| 405 |
(_tcslen wcs) |
| 406 |
(& nwritten) NULL)) |
| 407 |
(result nwritten))) |
| 408 |
|
| 409 |
(define-cproc sys-write-console-output-character (h s::<string> |
| 410 |
x::<short> y::<short>) |
| 411 |
::<uint> |
| 412 |
(let* ([wcs::LPCTSTR (SCM_MBS2WCS (Scm_GetStringConst s))] |
| 413 |
[c::COORD] [nwritten::DWORD 0]) |
| 414 |
(= (ref c X) x (ref c Y) y) |
| 415 |
(check (WriteConsoleOutputCharacter (Scm_WinHandle h '#f) |
| 416 |
wcs |
| 417 |
(_tcslen wcs) |
| 418 |
c (& nwritten))) |
| 419 |
(result nwritten))) |
| 420 |
|
| 421 |
(define-cproc sys-fill-console-output-character (h c::<char> len::<uint> |
| 422 |
x::<short> y::<short>) |
| 423 |
::<uint> |
| 424 |
(let* ([ch::ScmChar (Scm_CharToUcs c)] |
| 425 |
[coord::COORD] [nwritten::DWORD 0]) |
| 426 |
(= (ref coord X) x (ref coord Y) y) |
| 427 |
(check (FillConsoleOutputCharacter (Scm_WinHandle h '#f) |
| 428 |
(cast TCHAR ch) |
| 429 |
len |
| 430 |
coord (& nwritten))) |
| 431 |
(result nwritten))) |
| 432 |
|
| 433 |
(define-cproc sys-fill-console-output-attribute (h attr::<ushort> len::<uint> |
| 434 |
x::<short> y::<short>) |
| 435 |
::<uint> |
| 436 |
(let* ([c::COORD] [nwritten::DWORD 0]) |
| 437 |
(= (ref c X) x (ref c Y) y) |
| 438 |
(check (FillConsoleOutputAttribute (Scm_WinHandle h '#f) |
| 439 |
attr |
| 440 |
len |
| 441 |
c (& nwritten))) |
| 442 |
(result nwritten))) |
| 443 |
|
| 444 |
(define-cproc sys-flush-console-input-buffer (h) ::<void> |
| 445 |
(check (FlushConsoleInputBuffer (Scm_WinHandle h '#f)))) |
| 446 |
|
| 447 |
;; |
| 448 |
;; Console Title |
| 449 |
;; |
| 450 |
(define-cproc sys-get-console-title () ::<const-cstring> |
| 451 |
(let* ([buf::(.array TCHAR (1024))] |
| 452 |
[r::DWORD (GetConsoleTitle buf 1023)]) |
| 453 |
(when (== r 0) (Scm_SysError "GetConsoleTitle failed")) |
| 454 |
(= (aref buf 1023) 0) |
| 455 |
(result (SCM_WCS2MBS buf)))) |
| 456 |
|
| 457 |
(define-cproc sys-set-console-title (s::<string>) ::<void> |
| 458 |
(let* ([wcs::LPCTSTR (SCM_MBS2WCS (Scm_GetStringConst s))]) |
| 459 |
(when (>= (_tcslen wcs) 1024) |
| 460 |
(Scm_Error "SetConsoleTitle: string argument too long")) |
| 461 |
(check (SetConsoleTitle wcs)))) |
| 462 |
|
| 463 |
;; |
| 464 |
;; Std Handles |
| 465 |
;; |
| 466 |
(define-enum STD_INPUT_HANDLE) |
| 467 |
(define-enum STD_OUTPUT_HANDLE) |
| 468 |
(define-enum STD_ERROR_HANDLE) |
| 469 |
|
| 470 |
(define-cproc sys-get-std-handle (which::<int>) |
| 471 |
(let* ([h::HANDLE (GetStdHandle (DWORD which))]) |
| 472 |
(when (== h INVALID_HANDLE_VALUE) |
| 473 |
(Scm_SysError "GetStdHandle failed")) |
| 474 |
(let* ([p::HANDLE (GetCurrentProcess)] |
| 475 |
[h2::HANDLE INVALID_HANDLE_VALUE]) |
| 476 |
(unless (DuplicateHandle p h p (& h2) 0 TRUE DUPLICATE_SAME_ACCESS) |
| 477 |
(Scm_SysError "DuplicateHandle failed")) |
| 478 |
(result (Scm_MakeWinHandle h2 '#f))))) |
| 479 |
(define-cproc sys-set-std-handle (which::<int> handle) ::<void> |
| 480 |
(check (SetStdHandle (DWORD which) (Scm_WinHandle handle '#f)))) |
| 481 |
|
| 482 |
;; not supported yet |
| 483 |
;; SetConsoleHandler - Windows API doesn't allow passing extra user data |
| 484 |
;; WriteConsoleInput |
| 485 |
;; WriteConsoleOutput |
| 486 |
;; WriteConsoleOutputAttribute |
| 487 |
|
| 488 |
) ;; defined(GAUCHE_WINDOWS) |
| 489 |
) ;; inline-stub |
| 490 |
|
| 491 |
;; Local variables: |
| 492 |
;; mode: scheme |
| 493 |
;; end: |