fx.term: tty: implement get_cursor_position() on apple
This commit is contained in:
+70
-27
@@ -1,6 +1,8 @@
|
|||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include "../../tty.h"
|
#include "../../tty.h"
|
||||||
|
|
||||||
#include <fx/encoding.h>
|
#include <ctype.h>
|
||||||
#include <fx/term/tty.h>
|
#include <fx/term/tty.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -102,11 +104,11 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
|
|||||||
|
|
||||||
tcgetattr(fd, &tty->t_ios_default);
|
tcgetattr(fd, &tty->t_ios_default);
|
||||||
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
|
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_oflag &= ~(OPOST);
|
||||||
tty->t_ios_raw.c_cflag |= (CS8);
|
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
|
||||||
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
|
||||||
|
|
||||||
tty->t_flags |= FX_TTY_INIT;
|
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;
|
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(
|
static int compare_colour(
|
||||||
const struct fx_tty_colour *a,
|
const struct fx_tty_colour *a,
|
||||||
const struct fx_tty_colour *b)
|
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);
|
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)
|
fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
@@ -450,11 +439,6 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
|||||||
return FX_TTY_CTRL_KEY(c + 'a' - 1);
|
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) {
|
if (c != 0x1b) {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@@ -590,3 +574,62 @@ enum fx_status fx_tty_get_dimensions(
|
|||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user