Update PTY to match publically documented API

Remove unused variables and enums and make private what's not documented
as public.
This commit is contained in:
Leroy Hopson 2021-07-25 22:57:55 +07:00
parent d75c26cec6
commit 14dd045b66
No known key found for this signature in database
GPG key ID: D2747312A6DB51AA
2 changed files with 37 additions and 72 deletions

View file

@ -22,12 +22,6 @@ const DEFAULT_ENV := {TERM = DEFAULT_NAME, COLORTERM = "truecolor"}
#const FLOW_CONTROL_PAUSE = char(0x13) # defaults to XOFF #const FLOW_CONTROL_PAUSE = char(0x13) # defaults to XOFF
#const FLOW_CONTROL_RESUME = char(0x11) # defaults to XON #const FLOW_CONTROL_RESUME = char(0x11) # defaults to XON
enum Status { NONE, OPEN, EXITED, ERROR }
const STATUS_NONE = Status.NONE
const STATUS_OPEN = Status.OPEN
const STATUS_EXITED = Status.EXITED
const STATUS_ERROR = Status.ERROR
# Any signal_number can be sent to the pty's process using the kill() function, # Any signal_number can be sent to the pty's process using the kill() function,
# these are just the signals with numbers specified in the POSIX standard. # these are just the signals with numbers specified in the POSIX standard.
enum Signal { enum Signal {
@ -47,22 +41,16 @@ enum Signal {
signal data_received(data) signal data_received(data)
signal exited(exit_code, signum) signal exited(exit_code, signum)
signal errored(message)
export (NodePath) var terminal_path := NodePath() setget set_terminal_path export (NodePath) var terminal_path := NodePath() setget set_terminal_path
var status := STATUS_NONE var _terminal: Terminal = null setget _set_terminal
var error_string := ""
var terminal: Terminal = null setget set_terminal
## Name of the terminal to be set in environment ($TERM variable).
#export (String) var term_name: String
# The name of the process. # The name of the process.
var process: String #var process: String
# The process ID. # The process ID.
var pid: int var _pid: int
# The column size in characters. # The column size in characters.
export (int) var cols: int = DEFAULT_COLS setget set_cols export (int) var cols: int = DEFAULT_COLS setget set_cols
@ -70,9 +58,6 @@ export (int) var cols: int = DEFAULT_COLS setget set_cols
# The row size in characters. # The row size in characters.
export (int) var rows: int = DEFAULT_ROWS setget set_rows export (int) var rows: int = DEFAULT_ROWS setget set_rows
# Working directory to be set for the child program.
#export (String) var cwd := LibuvUtils.get_cwd()
# Environment to be set for the child program. # Environment to be set for the child program.
export (Dictionary) var env := DEFAULT_ENV export (Dictionary) var env := DEFAULT_ENV
@ -81,9 +66,6 @@ export (Dictionary) var env := DEFAULT_ENV
# former taking precedence in the case of conflicts. # former taking precedence in the case of conflicts.
export (bool) var use_os_env := true export (bool) var use_os_env := true
# If true, pty will call fork() in in _ready().
export (bool) var autostart := false
# (EXPERIMENTAL) # (EXPERIMENTAL)
# If true, PTY node will create a blocking libuv loop in a new thread. # If true, PTY node will create a blocking libuv loop in a new thread.
# signals will be emitted using call_deferred. # signals will be emitted using call_deferred.
@ -112,7 +94,6 @@ export (bool) var autostart := false
## The string that should resume the pty when `handle_flow_control` is true. Default is XON ("\u0011"). ## The string that should resume the pty when `handle_flow_control` is true. Default is XON ("\u0011").
#var flow_control_resume: String = FLOW_CONTROL_RESUME #var flow_control_resume: String = FLOW_CONTROL_RESUME
#var _native_term # Platform appropriate instance of this class.
var _pipe: Pipe var _pipe: Pipe
@ -126,32 +107,32 @@ func set_rows(value: int):
func set_terminal_path(value := NodePath()): func set_terminal_path(value := NodePath()):
terminal_path = value terminal_path = value
set_terminal(get_node_or_null(terminal_path)) _set_terminal(get_node_or_null(terminal_path))
func set_terminal(value: Terminal): func _set_terminal(value: Terminal):
if terminal == value: if _terminal == value:
return return
# Disconect the current terminal, if any. # Disconect the current terminal, if any.
if terminal: if _terminal:
disconnect("data_received", terminal, "write") disconnect("data_received", _terminal, "write")
terminal.disconnect("data_sent", self, "write") _terminal.disconnect("data_sent", self, "write")
terminal.disconnect("size_changed", self, "resize") _terminal.disconnect("size_changed", self, "resizev")
terminal = value _terminal = value
if not terminal: if not _terminal:
return return
# Connect the new terminal. # Connect the new terminal.
# FIXME! resize(terminal.get_cols(), terminal.get_rows()) # FIXME! resize(terminal.get_cols(), terminal.get_rows())
if not terminal.is_connected("size_changed", self, "resize"): if not _terminal.is_connected("size_changed", self, "resizev"):
terminal.connect("size_changed", self, "resize") _terminal.connect("size_changed", self, "resizev")
if not terminal.is_connected("data_sent", self, "write"): if not _terminal.is_connected("data_sent", self, "write"):
terminal.connect("data_sent", self, "write") _terminal.connect("data_sent", self, "write")
if not is_connected("data_received", terminal, "write"): if not is_connected("data_received", _terminal, "write"):
connect("data_received", terminal, "write") connect("data_received", _terminal, "write")
# Writes data to the socket. # Writes data to the socket.
@ -185,38 +166,28 @@ func _write(data: String) -> void:
# Also accepts a single Vector2 argument where x is the the number of columns # Also accepts a single Vector2 argument where x is the the number of columns
# and y is the number of rows. # and y is the number of rows.
func resize(cols, rows = null) -> void: func resize(cols, rows = null) -> void:
assert(
(cols is Vector2 and rows == null) or (cols is int and rows is int),
"Usage: resize(size: Vector2) or resize(cols: int, rows: int)"
)
if cols is Vector2:
rows = cols.y # Must get rows before reassigning cols!
cols = cols.x
if cols <= 0 or rows <= 0 or cols == NAN or rows == NAN or cols == INF or rows == INF: if cols <= 0 or rows <= 0 or cols == NAN or rows == NAN or cols == INF or rows == INF:
push_error("Resizing must be done using positive cols and rows.") push_error("Resizing must be done using positive cols and rows.")
_resize(cols, rows) _resize(cols, rows)
# Same as resize() but takes a Vector2.
func resizev(size: Vector2) -> void:
resize(size.x, size.y)
func _resize(cols: int, rows: int) -> void: func _resize(cols: int, rows: int) -> void:
assert(false, "Not implemented.") assert(false, "Not implemented.")
# Close, kill and destroy the pipe.
func destroy() -> void:
pass
# Kill the pty. # Kill the pty.
# sigint: The signal to send. By default this is SIGHUP. # sigint: The signal to send. By default this is SIGHUP.
# This is not supported on Windows. # This is not supported on Windows.
func kill(signum: int = Signal.SIGHUP) -> void: func kill(signum: int = Signal.SIGHUP) -> void:
if _pipe: if _pipe:
_pipe.close() _pipe.close()
if pid > 0: if _pid > 0:
LibuvUtils.kill(pid, signum) LibuvUtils.kill(_pid, signum)
func fork( func fork(
@ -225,8 +196,9 @@ func fork(
p_cwd: String = LibuvUtils.get_cwd(), p_cwd: String = LibuvUtils.get_cwd(),
p_cols: int = DEFAULT_COLS, p_cols: int = DEFAULT_COLS,
p_rows: int = DEFAULT_ROWS p_rows: int = DEFAULT_ROWS
): ) -> int:
push_error("Not implemented.") assert(false, "Not implemented.")
return FAILED
func open(cols: int = DEFAULT_COLS, rows: int = DEFAULT_ROWS) -> Array: func open(cols: int = DEFAULT_COLS, rows: int = DEFAULT_ROWS) -> Array:

View file

@ -21,16 +21,11 @@ var thread: Thread
var _fd: int = -1 var _fd: int = -1
var _exit_cb: FuncRef var _exit_cb: FuncRef
static func get_uid() -> int: #static func get_uid() -> int:
return -1 # Not implemented. # return -1 # Not implemented.
static func get_gid() -> int: #static func get_gid() -> int:
return -1 # Not implemented. # return -1 # Not implemented.
func _ready():
if autostart:
fork()
func _resize(cols: int, rows: int) -> void: func _resize(cols: int, rows: int) -> void:
@ -54,7 +49,7 @@ func fork(
uid: int = -1, uid: int = -1,
gid: int = -1, gid: int = -1,
utf8 = true utf8 = true
): ) -> int:
# File. # File.
if file.empty(): if file.empty():
file = FALLBACK_FILE file = FALLBACK_FILE
@ -78,16 +73,14 @@ func fork(
if result[0] != OK: if result[0] != OK:
push_error("Fork failed.") push_error("Fork failed.")
status = STATUS_ERROR
return FAILED return FAILED
_fd = result[1].fd _fd = result[1].fd
if _fd < 0: if _fd < 0:
push_error("File descriptor must be a non-negative integer value.") push_error("File descriptor must be a non-negative integer value.")
status = STATUS_ERROR
return FAILED return FAILED
pid = result[1].pid _pid = result[1].pid
_pipe = Pipe.new() _pipe = Pipe.new()
_pipe.open(_fd) _pipe.open(_fd)
@ -104,8 +97,8 @@ func open(cols: int = DEFAULT_COLS, rows: int = DEFAULT_ROWS) -> Array:
func _exit_tree(): func _exit_tree():
_exit_cb = null _exit_cb = null
if pid > 1: if _pid > 1:
LibuvUtils.kill(pid, Signal.SIGHUP) LibuvUtils.kill(_pid, Signal.SIGHUP)
func _on_pipe_data_received(data): func _on_pipe_data_received(data):
@ -114,7 +107,7 @@ func _on_pipe_data_received(data):
func _on_exit(exit_code: int, signum: int) -> void: func _on_exit(exit_code: int, signum: int) -> void:
if is_instance_valid(self): if is_instance_valid(self):
pid = -1 _pid = -1
emit_signal("exited", exit_code, signum) emit_signal("exited", exit_code, signum)