From 52adaba9e10b0372b3664ccda35e6e89c4b81dec Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Fri, 5 Mar 2021 23:02:37 +0800 Subject: [PATCH] pty: use uint16_t for resize params --- src/protocol.c | 12 +++++------- src/pty.c | 22 +++++++++------------- src/pty.h | 5 +++-- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/protocol.c b/src/protocol.c index 51b3df2..240260f 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -35,7 +35,7 @@ static int send_initial_message(struct lws *wsi, int index) { return lws_write(wsi, p, (size_t)n, LWS_WRITE_BINARY); } -static bool parse_window_size(struct pss_tty *pss, int *cols, int *rows) { +static bool parse_window_size(struct pss_tty *pss, uint16_t *cols, uint16_t *rows) { char json[pss->len]; strncpy(json, pss->buffer + 1, pss->len - 1); json[pss->len - 1] = '\0'; @@ -47,12 +47,12 @@ static bool parse_window_size(struct pss_tty *pss, int *cols, int *rows) { lwsl_err("columns field not exists, json: %s\n", json); return false; } - *cols = json_object_get_int(o); + *cols = (uint16_t) json_object_get_int(o); if (!json_object_object_get_ex(obj, "rows", &o)) { lwsl_err("rows field not exists, json: %s\n", json); return false; } - *rows = json_object_get_int(o); + *rows = (uint16_t) json_object_get_int(o); json_object_put(obj); return true; @@ -276,11 +276,9 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, } break; case RESIZE_TERMINAL: { - int cols, rows; + uint16_t cols, rows; if (parse_window_size(pss, &cols, &rows)) { - if (pty_resize(pss->process, rows, cols) < 0) { - lwsl_err("pty_resize: %d (%s)\n", errno, strerror(errno)); - } + pty_resize(pss->process, cols, rows); } } break; case PAUSE: diff --git a/src/pty.c b/src/pty.c index cc2aa2d..2d39208 100644 --- a/src/pty.c +++ b/src/pty.c @@ -126,26 +126,22 @@ int pty_write(pty_process *process, pty_buf_t *buf) { return uv_write(req, (uv_stream_t *) io->in, &b, 1, write_cb); } -int pty_resize(pty_process *process, int width, int height) { +bool pty_resize(pty_process *process, uint16_t width, uint16_t height) { #ifdef _WIN32 - COORD size = { height, width }; - return pResizePseudoConsole(process->pty, size) == S_OK ? 0 : -1; + COORD size = { (int16_t) width, (int16_t) height }; + return pResizePseudoConsole(process->pty, size) == S_OK; #else - struct winsize size; - size.ws_col = (unsigned short) height; - size.ws_row = (unsigned short) width; - size.ws_xpixel = 0; - size.ws_ypixel = 0; - return ioctl(process->pty, TIOCSWINSZ, &size); + struct winsize size = { height, width, 0, 0 }; + return ioctl(process->pty, TIOCSWINSZ, &size) == 0; #endif } -int pty_close(pty_process *process, int sig) { +bool pty_close(pty_process *process, int sig) { process->killed = true; #ifdef _WIN32 - return TerminateProcess(process->handle, 1) ? 0 : 1; + return TerminateProcess(process->handle, 1) != 0; #else - return uv_kill(-process->pid, sig); + return uv_kill(-process->pid, sig) == 0; #endif } @@ -325,7 +321,7 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) { goto cleanup; } - process->pid = GetProcessId(pi.hProcess); + process->pid = pi.dwProcessId; process->handle = pi.hProcess; process->exit_cb = exit_cb; diff --git a/src/pty.h b/src/pty.h index 08b9993..44a4120 100644 --- a/src/pty.h +++ b/src/pty.h @@ -2,6 +2,7 @@ #define TTYD_PTY_H #include +#include #include #ifdef _WIN32 @@ -65,7 +66,7 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb); void pty_pause(pty_process *process); void pty_resume(pty_process *process); int pty_write(pty_process *process, pty_buf_t *buf); -int pty_resize(pty_process *process, int width, int height); -int pty_close(pty_process *process, int sig); +bool pty_resize(pty_process *process, uint16_t width, uint16_t height); +bool pty_close(pty_process *process, int sig); #endif // TTYD_PTY_H -- 2.43.4