mirror of
https://github.com/lihop/godot-xterm.git
synced 2025-05-04 12:14:24 +02:00
Format c++ files using clang-format
Add git pre-commit hooks to help with automatic formatting.
This commit is contained in:
parent
99989d19e4
commit
6e455738b8
10 changed files with 981 additions and 760 deletions
|
@ -3,22 +3,20 @@
|
|||
#include "pseudoterminal.h"
|
||||
#endif
|
||||
|
||||
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
|
||||
{
|
||||
godot::Godot::gdnative_init(o);
|
||||
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
|
||||
godot::Godot::gdnative_init(o);
|
||||
}
|
||||
|
||||
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o)
|
||||
{
|
||||
godot::Godot::gdnative_terminate(o);
|
||||
extern "C" void GDN_EXPORT
|
||||
godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
|
||||
godot::Godot::gdnative_terminate(o);
|
||||
}
|
||||
|
||||
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
|
||||
{
|
||||
godot::Godot::nativescript_init(handle);
|
||||
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
|
||||
godot::Godot::nativescript_init(handle);
|
||||
|
||||
godot::register_tool_class<godot::Terminal>();
|
||||
godot::register_tool_class<godot::Terminal>();
|
||||
#if defined(PLATFORM_LINUX) || defined(PLATFORM_OSX)
|
||||
godot::register_class<godot::Pseudoterminal>();
|
||||
godot::register_class<godot::Pseudoterminal>();
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -9,168 +9,147 @@
|
|||
#include <pty.h>
|
||||
#endif
|
||||
#if defined(PLATFORM_OSX)
|
||||
#include <util.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <util.h>
|
||||
#endif
|
||||
|
||||
using namespace godot;
|
||||
|
||||
void Pseudoterminal::_register_methods()
|
||||
{
|
||||
void Pseudoterminal::_register_methods() {
|
||||
|
||||
register_method("_init", &Pseudoterminal::_init);
|
||||
register_method("_ready", &Pseudoterminal::_ready);
|
||||
register_method("_init", &Pseudoterminal::_init);
|
||||
register_method("_ready", &Pseudoterminal::_ready);
|
||||
|
||||
register_method("write", &Pseudoterminal::write);
|
||||
register_method("resize", &Pseudoterminal::resize);
|
||||
register_method("write", &Pseudoterminal::write);
|
||||
register_method("resize", &Pseudoterminal::resize);
|
||||
|
||||
register_signal<Pseudoterminal>((char *)"data_sent", "data", GODOT_VARIANT_TYPE_POOL_BYTE_ARRAY);
|
||||
register_signal<Pseudoterminal>((char *)"exited", "status", GODOT_VARIANT_TYPE_INT);
|
||||
register_signal<Pseudoterminal>((char *)"data_sent", "data",
|
||||
GODOT_VARIANT_TYPE_POOL_BYTE_ARRAY);
|
||||
register_signal<Pseudoterminal>((char *)"exited", "status",
|
||||
GODOT_VARIANT_TYPE_INT);
|
||||
}
|
||||
|
||||
Pseudoterminal::Pseudoterminal()
|
||||
{
|
||||
Pseudoterminal::Pseudoterminal() {}
|
||||
|
||||
Pseudoterminal::~Pseudoterminal() { pty_thread.join(); }
|
||||
|
||||
void Pseudoterminal::_init() {
|
||||
bytes_to_write = 0;
|
||||
pty_thread = std::thread(&Pseudoterminal::process_pty, this);
|
||||
}
|
||||
|
||||
Pseudoterminal::~Pseudoterminal()
|
||||
{
|
||||
pty_thread.join();
|
||||
}
|
||||
void Pseudoterminal::process_pty() {
|
||||
int fd;
|
||||
char *name;
|
||||
int status;
|
||||
|
||||
void Pseudoterminal::_init()
|
||||
{
|
||||
bytes_to_write = 0;
|
||||
pty_thread = std::thread(&Pseudoterminal::process_pty, this);
|
||||
}
|
||||
should_process_pty = true;
|
||||
|
||||
void Pseudoterminal::process_pty()
|
||||
{
|
||||
int fd;
|
||||
char *name;
|
||||
int status;
|
||||
struct termios termios = {};
|
||||
termios.c_iflag = IGNPAR | ICRNL;
|
||||
termios.c_oflag = 0;
|
||||
termios.c_cflag = B38400 | CRTSCTS | CS8 | CLOCAL | CREAD;
|
||||
termios.c_lflag = ICANON;
|
||||
termios.c_cc[VMIN] = 1;
|
||||
termios.c_cc[VTIME] = 0;
|
||||
|
||||
should_process_pty = true;
|
||||
pid_t pty_pid = forkpty(&fd, NULL, NULL, NULL);
|
||||
|
||||
struct termios termios = {};
|
||||
termios.c_iflag = IGNPAR | ICRNL;
|
||||
termios.c_oflag = 0;
|
||||
termios.c_cflag = B38400 | CRTSCTS | CS8 | CLOCAL | CREAD;
|
||||
termios.c_lflag = ICANON;
|
||||
termios.c_cc[VMIN] = 1;
|
||||
termios.c_cc[VTIME] = 0;
|
||||
if (pty_pid == -1) {
|
||||
ERR_PRINT(
|
||||
String("Error forking pty: {0}").format(Array::make(strerror(errno))));
|
||||
should_process_pty = false;
|
||||
return;
|
||||
} else if (pty_pid == 0) {
|
||||
/* Child */
|
||||
|
||||
pid_t pty_pid = forkpty(&fd, NULL, NULL, NULL);
|
||||
char termenv[11] = {"TERM=xterm"};
|
||||
putenv(termenv);
|
||||
|
||||
if (pty_pid == -1)
|
||||
{
|
||||
ERR_PRINT(String("Error forking pty: {0}").format(Array::make(strerror(errno))));
|
||||
should_process_pty = false;
|
||||
return;
|
||||
}
|
||||
else if (pty_pid == 0)
|
||||
{
|
||||
/* Child */
|
||||
char colortermenv[20] = {"COLORTERM=truecolor"};
|
||||
putenv(colortermenv);
|
||||
|
||||
char termenv[11] = {"TERM=xterm"};
|
||||
putenv(termenv);
|
||||
char *shell = getenv("SHELL");
|
||||
char *argv[] = {basename(shell), NULL};
|
||||
execvp(shell, argv);
|
||||
} else {
|
||||
Vector2 zero = Vector2(0, 0);
|
||||
|
||||
char colortermenv[20] = {"COLORTERM=truecolor"};
|
||||
putenv(colortermenv);
|
||||
/* Parent */
|
||||
while (1) {
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(size_mutex);
|
||||
if (size != zero) {
|
||||
struct winsize ws;
|
||||
memset(&ws, 0, sizeof(ws));
|
||||
ws.ws_col = size.x;
|
||||
ws.ws_row = size.y;
|
||||
|
||||
char *shell = getenv("SHELL");
|
||||
char *argv[] = { basename(shell), NULL };
|
||||
execvp(shell, argv);
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector2 zero = Vector2(0, 0);
|
||||
|
||||
/* Parent */
|
||||
while (1)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(size_mutex);
|
||||
if (size != zero)
|
||||
{
|
||||
struct winsize ws;
|
||||
memset(&ws, 0, sizeof(ws));
|
||||
ws.ws_col = size.x;
|
||||
ws.ws_row = size.y;
|
||||
|
||||
ioctl(fd, TIOCSWINSZ, &ws);
|
||||
}
|
||||
}
|
||||
|
||||
if (waitpid(pty_pid, &status, WNOHANG))
|
||||
{
|
||||
emit_signal("exited", status);
|
||||
return;
|
||||
}
|
||||
|
||||
int ready = -1;
|
||||
fd_set read_fds;
|
||||
fd_set write_fds;
|
||||
|
||||
FD_ZERO(&read_fds);
|
||||
FD_SET(fd, &read_fds);
|
||||
FD_SET(fd, &write_fds);
|
||||
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 10;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
ready = select(fd + 1, &read_fds, &write_fds, NULL, &timeout);
|
||||
|
||||
if (ready > 0)
|
||||
{
|
||||
if (FD_ISSET(fd, &write_fds))
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(write_buffer_mutex);
|
||||
|
||||
if (bytes_to_write > 0)
|
||||
{
|
||||
::write(fd, write_buffer, bytes_to_write);
|
||||
bytes_to_write = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (FD_ISSET(fd, &read_fds))
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(read_buffer_mutex);
|
||||
|
||||
int ret;
|
||||
int bytes_read = 0;
|
||||
|
||||
bytes_read = read(fd, read_buffer, MAX_READ_BUFFER_LENGTH);
|
||||
|
||||
// TODO: handle error (-1)
|
||||
if (bytes_read <= 0)
|
||||
continue;
|
||||
|
||||
PoolByteArray data = PoolByteArray();
|
||||
data.resize(bytes_read);
|
||||
memcpy(data.write().ptr(), read_buffer, bytes_read);
|
||||
|
||||
emit_signal("data_sent", PoolByteArray(data));
|
||||
}
|
||||
}
|
||||
ioctl(fd, TIOCSWINSZ, &ws);
|
||||
}
|
||||
}
|
||||
|
||||
if (waitpid(pty_pid, &status, WNOHANG)) {
|
||||
emit_signal("exited", status);
|
||||
return;
|
||||
}
|
||||
|
||||
int ready = -1;
|
||||
fd_set read_fds;
|
||||
fd_set write_fds;
|
||||
|
||||
FD_ZERO(&read_fds);
|
||||
FD_SET(fd, &read_fds);
|
||||
FD_SET(fd, &write_fds);
|
||||
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 10;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
ready = select(fd + 1, &read_fds, &write_fds, NULL, &timeout);
|
||||
|
||||
if (ready > 0) {
|
||||
if (FD_ISSET(fd, &write_fds)) {
|
||||
std::lock_guard<std::mutex> guard(write_buffer_mutex);
|
||||
|
||||
if (bytes_to_write > 0) {
|
||||
::write(fd, write_buffer, bytes_to_write);
|
||||
bytes_to_write = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (FD_ISSET(fd, &read_fds)) {
|
||||
std::lock_guard<std::mutex> guard(read_buffer_mutex);
|
||||
|
||||
int ret;
|
||||
int bytes_read = 0;
|
||||
|
||||
bytes_read = read(fd, read_buffer, MAX_READ_BUFFER_LENGTH);
|
||||
|
||||
// TODO: handle error (-1)
|
||||
if (bytes_read <= 0)
|
||||
continue;
|
||||
|
||||
PoolByteArray data = PoolByteArray();
|
||||
data.resize(bytes_read);
|
||||
memcpy(data.write().ptr(), read_buffer, bytes_read);
|
||||
|
||||
emit_signal("data_sent", PoolByteArray(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Pseudoterminal::_ready()
|
||||
{
|
||||
void Pseudoterminal::_ready() {}
|
||||
|
||||
void Pseudoterminal::write(PoolByteArray data) {
|
||||
std::lock_guard<std::mutex> guard(write_buffer_mutex);
|
||||
bytes_to_write = data.size();
|
||||
memcpy(write_buffer, data.read().ptr(), bytes_to_write);
|
||||
}
|
||||
|
||||
void Pseudoterminal::write(PoolByteArray data)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(write_buffer_mutex);
|
||||
bytes_to_write = data.size();
|
||||
memcpy(write_buffer, data.read().ptr(), bytes_to_write);
|
||||
}
|
||||
|
||||
void Pseudoterminal::resize(Vector2 new_size)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(size_mutex);
|
||||
size = new_size;
|
||||
void Pseudoterminal::resize(Vector2 new_size) {
|
||||
std::lock_guard<std::mutex> guard(size_mutex);
|
||||
size = new_size;
|
||||
}
|
||||
|
|
|
@ -3,49 +3,47 @@
|
|||
|
||||
#include <Godot.hpp>
|
||||
#include <Node.hpp>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
namespace godot
|
||||
{
|
||||
namespace godot {
|
||||
|
||||
class Pseudoterminal : public Node
|
||||
{
|
||||
GODOT_CLASS(Pseudoterminal, Node)
|
||||
class Pseudoterminal : public Node {
|
||||
GODOT_CLASS(Pseudoterminal, Node)
|
||||
|
||||
public:
|
||||
static const int MAX_READ_BUFFER_LENGTH = 1024;
|
||||
static const int MAX_WRITE_BUFFER_LENGTH = 1024;
|
||||
public:
|
||||
static const int MAX_READ_BUFFER_LENGTH = 1024;
|
||||
static const int MAX_WRITE_BUFFER_LENGTH = 1024;
|
||||
|
||||
private:
|
||||
std::thread pty_thread;
|
||||
bool should_process_pty;
|
||||
private:
|
||||
std::thread pty_thread;
|
||||
bool should_process_pty;
|
||||
|
||||
char write_buffer[MAX_WRITE_BUFFER_LENGTH];
|
||||
int bytes_to_write;
|
||||
std::mutex write_buffer_mutex;
|
||||
char write_buffer[MAX_WRITE_BUFFER_LENGTH];
|
||||
int bytes_to_write;
|
||||
std::mutex write_buffer_mutex;
|
||||
|
||||
char read_buffer[MAX_READ_BUFFER_LENGTH];
|
||||
int bytes_to_read;
|
||||
std::mutex read_buffer_mutex;
|
||||
char read_buffer[MAX_READ_BUFFER_LENGTH];
|
||||
int bytes_to_read;
|
||||
std::mutex read_buffer_mutex;
|
||||
|
||||
Vector2 size;
|
||||
std::mutex size_mutex;
|
||||
Vector2 size;
|
||||
std::mutex size_mutex;
|
||||
|
||||
void process_pty();
|
||||
void process_pty();
|
||||
|
||||
public:
|
||||
static void _register_methods();
|
||||
public:
|
||||
static void _register_methods();
|
||||
|
||||
Pseudoterminal();
|
||||
~Pseudoterminal();
|
||||
Pseudoterminal();
|
||||
~Pseudoterminal();
|
||||
|
||||
void _init();
|
||||
void _ready();
|
||||
void _init();
|
||||
void _ready();
|
||||
|
||||
void write(PoolByteArray data);
|
||||
void resize(Vector2 size);
|
||||
};
|
||||
void write(PoolByteArray data);
|
||||
void resize(Vector2 size);
|
||||
};
|
||||
} // namespace godot
|
||||
|
||||
#endif // PSEUDOTERMINAL_H
|
File diff suppressed because it is too large
Load diff
|
@ -1,78 +1,75 @@
|
|||
#ifndef TERMINAL_H
|
||||
#define TERMINAL_H
|
||||
|
||||
#include <libtsm.h>
|
||||
#include <Control.hpp>
|
||||
#include <Font.hpp>
|
||||
#include <Godot.hpp>
|
||||
#include <libtsm.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace godot
|
||||
{
|
||||
namespace godot {
|
||||
|
||||
class Terminal : public Control
|
||||
{
|
||||
GODOT_CLASS(Terminal, Control)
|
||||
class Terminal : public Control {
|
||||
GODOT_CLASS(Terminal, Control)
|
||||
|
||||
public:
|
||||
struct cell
|
||||
{
|
||||
char ch[5];
|
||||
struct tsm_screen_attr attr;
|
||||
};
|
||||
static const struct cell empty_cell;
|
||||
public:
|
||||
struct cell {
|
||||
char ch[5];
|
||||
struct tsm_screen_attr attr;
|
||||
};
|
||||
static const struct cell empty_cell;
|
||||
|
||||
public:
|
||||
typedef std::vector<std::vector<struct cell>> Cells;
|
||||
typedef std::vector<struct cell> Row;
|
||||
public:
|
||||
typedef std::vector<std::vector<struct cell>> Cells;
|
||||
typedef std::vector<struct cell> Row;
|
||||
|
||||
Cells cells;
|
||||
Cells cells;
|
||||
|
||||
Ref<InputEventKey> input_event_key;
|
||||
Ref<InputEventKey> input_event_key;
|
||||
|
||||
protected:
|
||||
tsm_screen *screen;
|
||||
tsm_vte *vte;
|
||||
protected:
|
||||
tsm_screen *screen;
|
||||
tsm_vte *vte;
|
||||
|
||||
private:
|
||||
static const uint8_t default_color_palette[TSM_COLOR_NUM][3];
|
||||
static const std::map<std::pair<int64_t, int64_t>, uint32_t> keymap;
|
||||
private:
|
||||
static const uint8_t default_color_palette[TSM_COLOR_NUM][3];
|
||||
static const std::map<std::pair<int64_t, int64_t>, uint32_t> keymap;
|
||||
|
||||
Vector2 cell_size;
|
||||
std::map<int, Color> palette = {};
|
||||
std::map<String, Ref<Font>> fontmap = {};
|
||||
Vector2 cell_size;
|
||||
std::map<int, Color> palette = {};
|
||||
std::map<String, Ref<Font>> fontmap = {};
|
||||
|
||||
void update_size();
|
||||
void update_size();
|
||||
|
||||
void update_theme();
|
||||
std::pair<Color, Color> get_cell_colors(int row, int col);
|
||||
void draw_background(int row, int col, Color bgcol);
|
||||
void draw_foreground(int row, int col, Color fgcol);
|
||||
void update_theme();
|
||||
std::pair<Color, Color> get_cell_colors(int row, int col);
|
||||
void draw_background(int row, int col, Color bgcol);
|
||||
void draw_foreground(int row, int col, Color fgcol);
|
||||
|
||||
public:
|
||||
static void _register_methods();
|
||||
public:
|
||||
static void _register_methods();
|
||||
|
||||
Terminal();
|
||||
~Terminal();
|
||||
Terminal();
|
||||
~Terminal();
|
||||
|
||||
void _init();
|
||||
void _ready();
|
||||
void _notification(int what);
|
||||
void _gui_input(Variant event);
|
||||
void _draw();
|
||||
void _init();
|
||||
void _ready();
|
||||
void _notification(int what);
|
||||
void _gui_input(Variant event);
|
||||
void _draw();
|
||||
|
||||
void write(Variant data);
|
||||
void write(Variant data);
|
||||
|
||||
int rows;
|
||||
int cols;
|
||||
int rows;
|
||||
int cols;
|
||||
|
||||
bool sleep;
|
||||
bool sleep;
|
||||
|
||||
uint8_t color_palette[TSM_COLOR_NUM][3];
|
||||
uint8_t color_palette[TSM_COLOR_NUM][3];
|
||||
|
||||
tsm_age_t framebuffer_age;
|
||||
};
|
||||
tsm_age_t framebuffer_age;
|
||||
};
|
||||
} // namespace godot
|
||||
|
||||
#endif // TERMINAL_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue