godot-xterm/scenes/demo.gd

80 lines
2.4 KiB
GDScript3
Raw Normal View History

2020-05-09 19:07:31 +02:00
# Copyright (c) 2020 The GodotTerm authors. All rights reserved.
# License MIT
extends Control
signal data_received(data)
# The user must have these programs installed for this to work.
const dependencies = PoolStringArray(['which', 'socat', 'bash'])
const host = '127.0.0.1'
2020-05-10 12:56:49 +02:00
const port = 7154
2020-05-09 19:07:31 +02:00
2020-05-10 12:56:49 +02:00
# Enable recording of all data send to the psuedoterminal master.
# This is useful if you want to record a session if you are trying
# to make a showcase of the terminal ;-)
export var record: bool = false
export(String) var record_file_path = '/tmp/godot-xterm-record.json'
2020-05-09 19:07:31 +02:00
var socat_pid = -1
var stream_peer = StreamPeerTCP.new()
2020-05-10 12:56:49 +02:00
var record_file
2020-05-09 19:07:31 +02:00
func _ready():
# First check that dependencies are installed and in $PATH.
var exit_code = OS.execute("which", dependencies)
if exit_code != 0:
OS.alert("Make sure the following programs are installed and in your $PATH: " + \
dependencies.join(", ") + ".", "Misssing Dependencies!")
2020-05-10 12:56:49 +02:00
else:
# Start socat.
socat_pid = OS.execute("socat",
["-d", "-d", "tcp-l:%d,bind=%s,reuseaddr,fork" % [port, host],
"exec:bash,pty,setsid,stderr,login,ctty"], false)
2020-05-09 19:07:31 +02:00
# Create a StreamPeerTCP to connect to socat.
var err = stream_peer.connect_to_host(host, port)
if err != OK:
OS.alert("Couldn't connect to socat on %s:%d" % [host, port], "Connection Failed!")
var status = stream_peer.get_status()
var connected = stream_peer.is_connected_to_host()
2020-05-10 12:56:49 +02:00
# Set the TERM environment variable, so that the correct escape sequences
# are sent to Terminal. By default this is set to dumb, which lacks support
# for even simple commands such as clear and reset.
stream_peer.put_data("export TERM=xterm\n".to_ascii())
stream_peer.put_data("clear\n".to_ascii())
2020-05-09 19:07:31 +02:00
# Connect the Terminal and StreamPeer.
2020-05-10 12:56:49 +02:00
$Terminal.connect('output', self, 'send_data')
connect("data_received", $Terminal, "write")
2020-05-09 19:07:31 +02:00
func send_data(data: PoolByteArray):
2020-05-10 12:56:49 +02:00
if record:
# Save the data and timestamp to a file
record_file.write()
2020-05-09 19:07:31 +02:00
stream_peer.put_data(data)
func _process(delta):
if stream_peer.get_status() == StreamPeerTCP.STATUS_CONNECTED:
var res = stream_peer.get_data(stream_peer.get_available_bytes())
var error = res[0]
var data = res[1]
if error != OK:
OS.alert("Something went wrong with the TCP connection to socat.",
"Connection Error!")
elif not data.empty():
2020-05-09 19:07:31 +02:00
emit_signal("data_received", data)
func _exit_tree():
2020-05-10 12:56:49 +02:00
if record:
record_file.close()
2020-05-09 19:07:31 +02:00
if socat_pid != -1:
OS.execute("kill", ["-9", socat_pid], false)