Skip to content

Commit

Permalink
pty: use uint16_t for resize params
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Mar 5, 2021
1 parent d6921b2 commit 52adaba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
12 changes: 5 additions & 7 deletions src/protocol.c
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 9 additions & 13 deletions src/pty.c
Expand Up @@ -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
}

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/pty.h
Expand Up @@ -2,6 +2,7 @@
#define TTYD_PTY_H

#include <stdbool.h>
#include <stdint.h>
#include <uv.h>

#ifdef _WIN32
Expand Down Expand Up @@ -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

0 comments on commit 52adaba

Please sign in to comment.