luv

Workshop wiki

terminal.lisp

ghostty/terminal.lisp

system luv/ghostty · 19 definitions · on GitHub

in-package#:luv.ghostty
define-conditionghostty-error
error
operation:initarg:operation:readerghostty-error-operation
result:initarg:result:readerghostty-error-result
:report
lambda
conditionstream
formatstream"libghostty-vt operation ~S failed with ~S."
ghostty-error-operationcondition
ghostty-error-resultcondition
defuncheck-result
resultoperation

cffi-libffi returns the formatter constructor's scalar result through raw storage because that call also has a structure-by-value argument.

when
integerpresult
setfresult
cffi:foreign-enum-keyword'ghostty-resultresult
unless
eqresult:success
error'ghostty-error:operationoperation:resultresult
result
defclassterminal
pointer:initarg:pointer:readerterminal-pointer
response-function:initformnil:accessorterminal-response-function
callback-condition:initformnil:accessorterminal-callback-condition
:documentation

An explicitly owned libghostty-vt terminal.

defvar*terminal-callbacks*
make-hash-table:test#'eql
defvar*terminal-callbacks-lock*
sb-thread:make-mutex:name"libghostty-vt terminal callbacks"
defunterminal-for-native-pointer
pointer
sb-thread:with-mutex
gethash
cffi:pointer-addresspointer
*terminal-callbacks*
cffi:defcallback%terminal-write-pty-callback:void
pointer:pointer
userdata:pointer
data:pointer
length:size
declare
ignoreuserdata
let
whenterminal
let
function
terminal-response-functionterminal
whenfunction

Ghostty's bytes are borrowed only for this callback. Copy before entering application code, which may enqueue them for later IO.

let
bytes
make-arraylength:element-type'
unsigned-byte8
dotimes
indexlength
setf
arefbytesindex
cffi:mem-arefdata:uint8index

A Lisp condition must never unwind through Ghostty's C stack. Retain it on the terminal so the owner can surface it safely.

handler-case
funcallfunctionbytes
error
condition
setf
terminal-callback-conditionterminal
condition
defunterminal-open-p
not
cffi:null-pointer-p
terminal-pointerterminal
defunensure-terminal-open
unless
error"The libghostty-vt terminal ~S is closed."terminal
terminal
defunmake-terminal
&key
columns80
rows24

Create an owned libghostty-vt terminal using its default allocator.

cffi:with-foreign-object
output:pointer
setf
cffi:mem-refoutput:pointer
cffi:null-pointer
check-result
%terminal-new
cffi:null-pointer
outputcolumnsrows
'make-terminal
make-instance'terminal:pointer
cffi:mem-refoutput:pointer
defunclose-terminal

Release terminal. Repeated calls are harmless.

when
%terminal-free
terminal-pointerterminal
setf
slot-valueterminal'pointer
cffi:null-pointer
terminal
defmacrowith-terminal
variable&restinitargs
&bodybody
`
let
unwind-protect
progn,@body
defunwrite-terminal

Feed the UTF-8 encoding of text to terminal's VT stream parser.

cffi:with-foreign-string
datalength
text:encoding:utf-8
%terminal-vt-write
terminal-pointerterminal
datalength
terminal
defunwrite-terminal-bytes
terminalbytes&key
start0
end

Feed the octets in bytes from start to end to terminal's VT parser.

check-typebytes
simple-array
unsigned-byte8
let
end
orend
lengthbytes
unless
<=0startend
lengthbytes
error"Invalid terminal byte range [~D,~D) for ~D octets."startend
lengthbytes
unless
=startend
cffi:with-pointer-to-vector-data
databytes
%terminal-vt-write
terminal-pointerterminal
cffi:inc-pointerdatastart
-endstart
terminal
defunresize-terminal
terminalcolumnsrows&key
cell-width-pixels0
cell-height-pixels0

Resize terminal in cells and record its optional cell pixel geometry.

check-typecolumns
integer165535
check-typerows
integer165535
check-typecell-width-pixels
unsigned-byte32
check-typecell-height-pixels
unsigned-byte32
check-result
%terminal-resize
terminal-pointerterminal
columnsrowscell-width-pixelscell-height-pixels
'resize-terminal
terminal
defunterminal-size

Return terminal's column and row counts.

cffi:with-foreign-objects
columns:uint16
rows:uint16
check-result
%terminal-get
terminal-pointerterminal
:columnscolumns
'terminal-size
check-result
%terminal-get
terminal-pointerterminal
:rowsrows
'terminal-size
values
cffi:mem-refcolumns:uint16
cffi:mem-refrows:uint16
defunset-terminal-response-function

Route terminal-generated response octets to function, or disable with NIL.

function is called synchronously during write-terminal or write-terminal-bytes. Its octet vector is an owned copy of Ghostty's borrowed callback bytes. function must enqueue or copy promptly and must not feed the same terminal recursively.

check-typefunction
let
key
cffi:pointer-address
terminal-pointerterminal
iffunction
progn
setf
terminal-response-functionterminal
function
terminal-callback-conditionterminal
nil
handler-case
check-result
%terminal-set
terminal-pointerterminal
:write-pty
'set-terminal-response-function
error
condition
sb-thread:with-mutex
setf
terminal-response-functionterminal
nil
errorcondition
progn
check-result
%terminal-set
terminal-pointerterminal
:write-pty
cffi:null-pointer
'set-terminal-response-function
setf
terminal-response-functionterminal
nil
sb-thread:with-mutex
function
defunmake-plain-formatter
cffi:with-foreign-objects
output:pointer
loopforindexbelow
cffi:foreign-type-size'
do
setf
cffi:mem-arefoptions:uint8index
0
setf
cffi:foreign-slot-valueoptions''size
cffi:foreign-type-size'
cffi:foreign-slot-valueoptions''emit
:plain
cffi:foreign-slot-valueoptions''trim
t
check-result
%formatter-terminal-new
cffi:null-pointer
output
terminal-pointerterminal
cffi:mem-refoptions'
'terminal-text
cffi:mem-refoutput:pointer
defunterminal-text

Return the active screen of terminal as trimmed plain UTF-8 text.

let
unwind-protect
cffi:with-foreign-objects
output:pointer
length:size
setf
cffi:mem-refoutput:pointer
cffi:null-pointer
cffi:mem-reflength:size
0
check-result
%formatter-format-allocformatter
cffi:null-pointer
outputlength
'terminal-text
let
pointer
cffi:mem-refoutput:pointer
byte-count
cffi:mem-reflength:size
unwind-protect
cffi:foreign-string-to-lisppointer:countbyte-count:encoding:utf-8
%ghostty-free
cffi:null-pointer
pointerbyte-count
%formatter-freeformatter