diff --git a/fx.term/sys/apple/tty.c b/fx.term/sys/apple/tty.c index 82d28a5..336e51c 100644 --- a/fx.term/sys/apple/tty.c +++ b/fx.term/sys/apple/tty.c @@ -1,6 +1,8 @@ +#define _POSIX_C_SOURCE 200809L + #include "../../tty.h" -#include +#include #include #include #include @@ -102,11 +104,11 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out) tcgetattr(fd, &tty->t_ios_default); memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw); + setvbuf(out, NULL, _IOLBF, 4096); - tty->t_ios_raw.c_iflag &= ~(BRKINT | INPCK | ISTRIP | ICRNL | IXON); + tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON); tty->t_ios_raw.c_oflag &= ~(OPOST); - tty->t_ios_raw.c_cflag |= (CS8); - tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); + tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN); tty->t_flags |= FX_TTY_INIT; } @@ -207,6 +209,11 @@ void fx_tty_reset_vmode(struct fx_tty *tty) tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL; } +void fx_tty_flush(struct fx_tty *tty) +{ + fflush(tty->t_out); +} + static int compare_colour( const struct fx_tty_colour *a, const struct fx_tty_colour *b) @@ -408,24 +415,6 @@ void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode) memcpy(&tty->t_vmode, vmode, sizeof *vmode); } -static fx_keycode read_utf8(int fd, char header, int len) -{ - char s[4] = {header, 0, 0, 0}; - - for (int i = 1; i < len; i++) { - char c; - int v = read(fd, &c, 1); - - if (v != 1) { - return FX_KEY_EOF; - } - - s[i] = c; - } - - return fx_wchar_utf8_codepoint_decode(s); -} - fx_keycode fx_tty_read_key(struct fx_tty *tty) { char c; @@ -450,11 +439,6 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty) return FX_TTY_CTRL_KEY(c + 'a' - 1); } - int utf8_len = fx_wchar_utf8_header_decode(c); - if (utf8_len > 1) { - return read_utf8(fd, c, utf8_len); - } - if (c != 0x1b) { return c; } @@ -590,3 +574,62 @@ enum fx_status fx_tty_get_dimensions( return 0; } + +enum fx_status fx_tty_get_cursor_position( + struct fx_tty *tty, + unsigned int *w, + unsigned int *h) +{ + if (!(tty->t_flags & FX_TTY_INTERACTIVE)) { + return -1; + } + + int in_fd = fileno(tty->t_in); + int out_fd = fileno(tty->t_out); + + write(out_fd, "\x1b[6n", 4); + + char c; + read(in_fd, &c, 1); + read(in_fd, &c, 1); + + char s[16] = {0}; + for (int i = 0; i < sizeof s - 1; i++) { + read(in_fd, &c, 1); + if (!isdigit(c)) { + break; + } + + s[i] = c; + s[i + 1] = 0; + } + + if (w) { + if (s[0]) { + *w = atoi(s); + } else { + *w = 0; + } + } + + s[0] = 0; + for (int i = 0; i < sizeof s - 1; i++) { + read(in_fd, &c, 1); + if (!isdigit(c)) { + break; + } + + s[i] = c; + s[i + 1] = 0; + } + + if (h) { + if (s[0]) { + *h = atoi(s); + } else { + *h = 0; + } + } + + return 0; +}