diff --git a/org/text-input.org b/org/text-input.org index 7d15408..8395234 100644 --- a/org/text-input.org +++ b/org/text-input.org @@ -440,16 +440,17 @@ is interrupted by a signal. #+BEGIN_SRC lisp :tangle ../src/components/input.lisp (defun read-raw-byte (&key timeout) - (let* ((buf (sb-alien:make-alien sb-alien:unsigned-char 1)) - (fd 0)) - (unwind-protect - (if timeout - (progn (sb-unix:unix-simple-poll fd :input timeout) - (let ((n (sb-unix:unix-read fd buf 1))) - (if (= n 1) (sb-alien:deref buf 0) (values nil :eof)))) - (let ((n (sb-unix:unix-read fd buf 1))) - (if (= n 1) (sb-alien:deref buf 0) (values nil :eof)))) - (sb-alien:free-alien buf)))) + (let* ((buf (make-array 1 :element-type '(unsigned-byte 8))) + (fd 0) + (timeout-ms (when timeout (max 1 (round (* timeout 1000)))))) + (sb-sys:with-pinned-objects (buf) + (let ((sap (sb-sys:vector-sap buf))) + (if timeout-ms + (progn (sb-unix:unix-simple-poll fd :input timeout-ms) + (let ((n (sb-unix:unix-read fd sap 1))) + (if (= n 1) (aref buf 0) (values nil :eof)))) + (let ((n (sb-unix:unix-read fd sap 1))) + (if (= n 1) (aref buf 0) (values nil :eof)))))))) #+END_SRC ** Escape sequence reader diff --git a/src/components/input.lisp b/src/components/input.lisp index 5caeb0b..6e95a7d 100644 --- a/src/components/input.lisp +++ b/src/components/input.lisp @@ -68,16 +68,17 @@ :raw (format nil "~C[~{~d~};~d~C" #\Esc params terminator))))) (defun read-raw-byte (&key timeout) - (let* ((buf (sb-alien:make-alien sb-alien:unsigned-char 1)) - (fd 0)) - (unwind-protect - (if timeout - (progn (sb-unix:unix-simple-poll fd :input timeout) - (let ((n (sb-unix:unix-read fd buf 1))) - (if (= n 1) (sb-alien:deref buf 0) (values nil :eof)))) - (let ((n (sb-unix:unix-read fd buf 1))) - (if (= n 1) (sb-alien:deref buf 0) (values nil :eof)))) - (sb-alien:free-alien buf)))) + (let* ((buf (make-array 1 :element-type '(unsigned-byte 8))) + (fd 0) + (timeout-ms (when timeout (max 1 (round (* timeout 1000)))))) + (sb-sys:with-pinned-objects (buf) + (let ((sap (sb-sys:vector-sap buf))) + (if timeout-ms + (progn (sb-unix:unix-simple-poll fd :input timeout-ms) + (let ((n (sb-unix:unix-read fd sap 1))) + (if (= n 1) (aref buf 0) (values nil :eof)))) + (let ((n (sb-unix:unix-read fd sap 1))) + (if (= n 1) (aref buf 0) (values nil :eof)))))))) (defun %read-escape-sequence () (flet ((read-next (&optional (timeout nil))