-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
334 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#include "pty.h" | ||
|
||
#include <uv.h> | ||
|
||
using namespace godot; | ||
|
||
PTY::PTY() { | ||
os = OS::get_singleton(); | ||
|
||
env["TERM"] = "xterm-256color"; | ||
env["COLORTERM"] = "truecolor"; | ||
|
||
#if defined(__linux__) || defined(__APPLE__) | ||
pty_unix = memnew(PTYUnix); | ||
#endif | ||
} | ||
|
||
PTY::~PTY() { | ||
} | ||
|
||
int PTY::get_cols() const { | ||
return cols; | ||
} | ||
|
||
int PTY::get_rows() const { | ||
return rows; | ||
} | ||
|
||
Dictionary PTY::get_env() const { | ||
return env; | ||
} | ||
|
||
void PTY::set_env(const Dictionary &value) { | ||
env = value; | ||
} | ||
|
||
bool PTY::get_use_os_env() const { | ||
return use_os_env; | ||
} | ||
|
||
void PTY::set_use_os_env(const bool value) { | ||
use_os_env = value; | ||
} | ||
|
||
Error PTY::fork(const String &file, const PackedStringArray &args, const String &cwd, const int cols, const int rows) { | ||
String fork_file = _get_fork_file(file); | ||
Dictionary fork_env = _get_fork_env(); | ||
Array result; | ||
|
||
#if defined(__linux__) || defined(__APPLE__) | ||
// TODO: Convert for_env to PackedStringArray. | ||
// TODO: On exit callback. | ||
result = pty_unix->fork(fork_file, 0, args, PackedStringArray(), cwd, cols, rows, 0, 0, true, Callable()); | ||
#endif | ||
|
||
return static_cast<Error>((int)result[0]); | ||
} | ||
|
||
void PTY::kill(const int signum) const { | ||
} | ||
|
||
Error PTY::open(const int cols, const int rows) const { | ||
Array result; | ||
|
||
#if defined(__linux__) || defined(__APPLE__) | ||
result = pty_unix->open(cols, rows); | ||
#endif | ||
|
||
return static_cast<Error>((int)result[0]); | ||
} | ||
|
||
void PTY::resize(const int cols, const int rows) const { | ||
} | ||
|
||
void PTY::write(const Variant &data) const { | ||
} | ||
|
||
void PTY::_bind_methods() { | ||
ClassDB::bind_method(D_METHOD("get_env"), &PTY::get_env); | ||
ClassDB::bind_method(D_METHOD("set_env", "env"), &PTY::set_env); | ||
ClassDB::add_property("PTY", PropertyInfo(Variant::DICTIONARY, "env"), "set_env", "get_env"); | ||
|
||
ClassDB::bind_method(D_METHOD("get_use_os_env"), &PTY::get_use_os_env); | ||
ClassDB::bind_method(D_METHOD("set_use_os_env", "use_os_env"), &PTY::set_use_os_env); | ||
ClassDB::add_property("PTY", PropertyInfo(Variant::BOOL, "use_os_env"), "set_use_os_env", "get_use_os_env"); | ||
|
||
ClassDB::bind_method(D_METHOD("fork", "file", "args", "cwd", "cols", "rows"), &PTY::fork, DEFVAL(""), DEFVAL(PackedStringArray()), DEFVAL("."), DEFVAL(80), DEFVAL(24)); | ||
ClassDB::bind_method(D_METHOD("open", "cols", "rows"), &PTY::open, DEFVAL(80), DEFVAL(24)); | ||
ClassDB::bind_method(D_METHOD("write", "data"), &PTY::write); | ||
} | ||
|
||
String PTY::_get_fork_file(const String &file) const { | ||
if (!file.is_empty()) return file; | ||
|
||
String shell_env = os->get_environment("SHELL"); | ||
if (!shell_env.is_empty()) { | ||
return shell_env; | ||
} | ||
|
||
#if defined(__linux__) | ||
return "sh"; | ||
#endif | ||
#if defined(__APPLE__) | ||
return "zsh"; | ||
#endif | ||
#if defined(_WIN32) | ||
return "cmd.exe"; | ||
#endif | ||
|
||
return ""; | ||
} | ||
|
||
Dictionary PTY::_get_fork_env() const { | ||
if (!use_os_env) return env; | ||
|
||
#if defined(_PTY_DISABLED) | ||
return env; | ||
#endif | ||
|
||
Dictionary os_env; | ||
uv_env_item_t *uv_env; | ||
int count; | ||
|
||
uv_os_environ(&uv_env, &count); | ||
for (int i = 0; i < count; i++) { | ||
os_env[uv_env[i].name] = uv_env[i].value; | ||
} | ||
uv_os_free_environ(uv_env, count); | ||
|
||
// Make sure we didn't start our server from inside tmux. | ||
os_env.erase("TMUX"); | ||
os_env.erase("TMUX_PANE"); | ||
|
||
// Make sure we didn't start our server from inside screen. | ||
// http://web.mit.edu/gnu/doc/html/screen_20.html | ||
os_env.erase("STY"); | ||
os_env.erase("WINDOW"); | ||
|
||
// Delete some variables that might confuse our terminal. | ||
os_env.erase("WINDOWID"); | ||
os_env.erase("TERMCAP"); | ||
os_env.erase("COLUMNS"); | ||
os_env.erase("LINES"); | ||
|
||
// Merge in our custom environment. | ||
PackedStringArray keys = PackedStringArray(env.keys()); | ||
for (int i = 0; i < keys.size(); i++) { | ||
String key = keys[i]; | ||
os_env[key] = env[key]; | ||
} | ||
|
||
return os_env; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-FileCopyrightText: 2021-2024 Leroy Hopson <[email protected]> | ||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#if defined(__linux__) || defined(__APPLE__) | ||
#include "pty_unix.h" | ||
#endif | ||
|
||
#include <godot_cpp/classes/node.hpp> | ||
#include <godot_cpp/classes/os.hpp> | ||
|
||
namespace godot | ||
{ | ||
class PTY : public Node | ||
{ | ||
GDCLASS(PTY, Node) | ||
|
||
public: | ||
|
||
enum Signal { | ||
// FIXME: Formatted like this as an apple macro already defines the non underscored version. | ||
SIG_HUP = 1, | ||
SIG_INT = 2, | ||
SIG_QUIT = 3, | ||
SIG_ILL = 4, | ||
SIG_TRAP = 5, | ||
SIG_ABRT = 6, | ||
SIG_BUS = 7, | ||
SIG_FPE = 8, | ||
SIG_KILL = 9, | ||
SIG_USR1 = 10, | ||
SIG_SEGV = 11, | ||
SIG_USR2 = 12, | ||
SIG_PIPE = 13, | ||
SIG_ALRM = 14, | ||
SIG_TERM = 15, | ||
}; | ||
|
||
PTY(); | ||
~PTY(); | ||
|
||
int get_cols() const; | ||
int get_rows() const; | ||
|
||
Dictionary get_env() const; | ||
void set_env(const Dictionary &value); | ||
|
||
bool get_use_os_env() const; | ||
void set_use_os_env(const bool value); | ||
|
||
Error fork(const String &file = "", const PackedStringArray &args = PackedStringArray(), const String &cwd = ".", const int cols = 80, const int rows = 24); | ||
void kill(const int signum = Signal::SIGHUP) const; | ||
Error open(const int cols = 80, const int rows = 24) const; | ||
void resize(const int cols, const int rows) const; | ||
void resizev(const Vector2i &size) const { resize(size.x, size.y); }; | ||
void write(const Variant &data) const; | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
private: | ||
OS *os; | ||
|
||
unsigned int cols = 0; | ||
unsigned int rows = 0; | ||
|
||
Dictionary env = Dictionary(); | ||
bool use_os_env = true; | ||
|
||
String _get_fork_file(const String &file) const; | ||
Dictionary _get_fork_env() const; | ||
|
||
#if defined(__linux__) || defined(__APPLE__) | ||
PTYUnix *pty_unix; | ||
#endif | ||
}; | ||
} // namespace godot | ||
|
||
VARIANT_ENUM_CAST(PTY::Signal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.