2024-11-22 22:29:40 +00:00
|
|
|
#include "../../tty.h"
|
2024-11-20 22:12:36 +00:00
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
#include <blue/term/tty.h>
|
2024-11-20 22:12:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#define ANSI_BOLD_ON "1"
|
|
|
|
|
#define ANSI_BOLD_OFF "22"
|
|
|
|
|
#define ANSI_ITALIC_ON "3"
|
|
|
|
|
#define ANSI_ITALIC_OFF "23"
|
|
|
|
|
#define ANSI_UNDERLINE_ON "4"
|
|
|
|
|
#define ANSI_UNDERLINE_OFF "24"
|
|
|
|
|
|
|
|
|
|
#define ANSI_DEFAULTCOLOUR_FG "39"
|
|
|
|
|
#define ANSI_DEFAULTCOLOUR_BG "49"
|
|
|
|
|
|
|
|
|
|
#define ANSI_256COLOUR_FG "38;5"
|
|
|
|
|
#define ANSI_256COLOUR_BG "48;5"
|
|
|
|
|
|
|
|
|
|
#define ANSI_TRUECOLOUR_FG "38;2"
|
|
|
|
|
#define ANSI_TRUECOLOUR_BG "48;2"
|
|
|
|
|
|
|
|
|
|
enum tty_flags {
|
2024-11-22 22:29:40 +00:00
|
|
|
B_TTY_INIT = 0x01u,
|
|
|
|
|
B_TTY_INTERACTIVE = 0x02u,
|
2024-11-20 22:12:36 +00:00
|
|
|
};
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
struct b_tty {
|
2024-11-20 22:12:36 +00:00
|
|
|
FILE *t_in, *t_out;
|
|
|
|
|
enum tty_flags t_flags;
|
|
|
|
|
struct termios t_ios_raw, t_ios_default;
|
2024-11-22 22:29:40 +00:00
|
|
|
struct b_tty_vmode t_vmode;
|
2024-11-20 22:12:36 +00:00
|
|
|
unsigned char t_mcount;
|
2024-11-22 22:29:40 +00:00
|
|
|
struct tty_format_buf t_format_buf;
|
2024-11-20 22:12:36 +00:00
|
|
|
};
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
static struct b_tty std = {0};
|
|
|
|
|
static struct b_tty err = {0};
|
2024-11-20 22:12:36 +00:00
|
|
|
|
|
|
|
|
static const char *ansi_colour16_fg[] = {
|
2024-11-22 22:29:40 +00:00
|
|
|
[B_TTY_COLOUR16_BLACK] = "30",
|
|
|
|
|
[B_TTY_COLOUR16_RED] = "31",
|
|
|
|
|
[B_TTY_COLOUR16_GREEN] = "32",
|
|
|
|
|
[B_TTY_COLOUR16_YELLOW] = "33",
|
|
|
|
|
[B_TTY_COLOUR16_BLUE] = "34",
|
|
|
|
|
[B_TTY_COLOUR16_MAGENTA] = "35",
|
|
|
|
|
[B_TTY_COLOUR16_CYAN] = "36",
|
|
|
|
|
[B_TTY_COLOUR16_WHITE] = "37",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_BLACK] = "90",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_RED] = "91",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_GREEN] = "92",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_YELLOW] = "93",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_BLUE] = "94",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_MAGENTA] = "95",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_CYAN] = "96",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_WHITE] = "97",
|
2024-11-20 22:12:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char *ansi_colour16_bg[] = {
|
2024-11-22 22:29:40 +00:00
|
|
|
[B_TTY_COLOUR16_BLACK] = "40",
|
|
|
|
|
[B_TTY_COLOUR16_RED] = "41",
|
|
|
|
|
[B_TTY_COLOUR16_GREEN] = "42",
|
|
|
|
|
[B_TTY_COLOUR16_YELLOW] = "43",
|
|
|
|
|
[B_TTY_COLOUR16_BLUE] = "44",
|
|
|
|
|
[B_TTY_COLOUR16_MAGENTA] = "45",
|
|
|
|
|
[B_TTY_COLOUR16_CYAN] = "46",
|
|
|
|
|
[B_TTY_COLOUR16_WHITE] = "47",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_BLACK] = "100",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_RED] = "101",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_GREEN] = "102",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_YELLOW] = "103",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_BLUE] = "104",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_MAGENTA] = "105",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_CYAN] = "106",
|
|
|
|
|
[B_TTY_COLOUR16_BRIGHT_WHITE] = "107",
|
2024-11-20 22:12:36 +00:00
|
|
|
};
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
static void init_tty(struct b_tty *tty, FILE *in, FILE *out)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
tty->t_in = in;
|
|
|
|
|
tty->t_out = out;
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
int fd = -1;
|
|
|
|
|
if (in) {
|
|
|
|
|
fd = fileno(in);
|
|
|
|
|
} else if (out) {
|
|
|
|
|
fd = fileno(out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-20 22:12:36 +00:00
|
|
|
|
|
|
|
|
if (isatty(fd)) {
|
2024-11-22 22:29:40 +00:00
|
|
|
tty->t_flags |= B_TTY_INTERACTIVE;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tcgetattr(fd, &tty->t_ios_default);
|
|
|
|
|
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
|
|
|
|
|
|
|
|
|
|
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
|
|
|
|
|
tty->t_ios_raw.c_oflag &= ~(OPOST);
|
|
|
|
|
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
tty->t_flags |= B_TTY_INIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct b_tty *z__b_tty_get_std(void)
|
|
|
|
|
{
|
|
|
|
|
if (!(std.t_flags & B_TTY_INIT)) {
|
|
|
|
|
init_tty(&std, stdin, stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &std;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
struct b_tty *z__b_tty_get_err(void)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
2024-11-22 22:29:40 +00:00
|
|
|
if (!(err.t_flags & B_TTY_INIT)) {
|
|
|
|
|
init_tty(&err, NULL, stderr);
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
return &err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tty_format_buf *z__b_tty_get_format_buf(struct b_tty *tty)
|
|
|
|
|
{
|
|
|
|
|
return &tty->t_format_buf;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
void z__b_tty_putc(struct b_tty *tty, char c)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
2024-11-22 22:29:40 +00:00
|
|
|
fputc(c, tty->t_out);
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
bool b_tty_is_interactive(const struct b_tty *tty)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
2024-11-22 22:29:40 +00:00
|
|
|
return (tty->t_flags & B_TTY_INTERACTIVE) == B_TTY_INTERACTIVE;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
static void set_raw(struct b_tty *tty)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
2024-11-22 22:29:40 +00:00
|
|
|
int fd = -1;
|
|
|
|
|
if (tty->t_in) {
|
|
|
|
|
fd = fileno(tty->t_in);
|
|
|
|
|
} else if (tty->t_out) {
|
|
|
|
|
fd = fileno(tty->t_out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tcsetattr(fd, TCSAFLUSH, &tty->t_ios_raw);
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
static void set_canon(struct b_tty *tty)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
2024-11-22 22:29:40 +00:00
|
|
|
int fd = -1;
|
|
|
|
|
if (tty->t_in) {
|
|
|
|
|
fd = fileno(tty->t_in);
|
|
|
|
|
} else if (tty->t_out) {
|
|
|
|
|
fd = fileno(tty->t_out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fd == -1) {
|
2024-11-20 22:12:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
tcsetattr(fd, TCSAFLUSH, &tty->t_ios_default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode)
|
|
|
|
|
{
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case B_TTY_CANONICAL:
|
|
|
|
|
set_canon(tty);
|
|
|
|
|
break;
|
|
|
|
|
case B_TTY_RAW:
|
|
|
|
|
set_raw(tty);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void b_tty_reset_vmode(struct b_tty *tty)
|
|
|
|
|
{
|
|
|
|
|
if (tty->t_vmode.v_fg.c_mode == B_TTY_COLOUR_NONE
|
|
|
|
|
&& tty->t_vmode.v_bg.c_mode == B_TTY_COLOUR_NONE
|
|
|
|
|
&& tty->t_vmode.v_attrib == B_TTY_ATTRIB_NORMAL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(tty->t_out, "\033[0m");
|
2024-11-20 22:12:36 +00:00
|
|
|
|
|
|
|
|
memset(&tty->t_vmode, 0x0, sizeof tty->t_vmode);
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
tty->t_vmode.v_fg.c_mode = B_TTY_COLOUR_NONE;
|
|
|
|
|
tty->t_vmode.v_bg.c_mode = B_TTY_COLOUR_NONE;
|
|
|
|
|
tty->t_vmode.v_attrib = B_TTY_ATTRIB_NORMAL;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int compare_colour(
|
2024-11-22 22:29:40 +00:00
|
|
|
const struct b_tty_colour *a, const struct b_tty_colour *b)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
if (a->c_mode != b->c_mode) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (a->c_mode) {
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_16:
|
2024-11-20 22:12:36 +00:00
|
|
|
if (a->c_16.value != b->c_16.value) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_256:
|
2024-11-20 22:12:36 +00:00
|
|
|
if (a->c_256.value != b->c_256.value) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_TRUE:
|
2024-11-20 22:12:36 +00:00
|
|
|
if (a->c_true.r != b->c_true.r) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a->c_true.g != b->c_true.g) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a->c_true.b != b->c_true.b) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
static int compare_vmode(const struct b_tty_vmode *a, const struct b_tty_vmode *b)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
if (a->v_attrib != b->v_attrib) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (compare_colour(&a->v_fg, &b->v_fg) != 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (compare_colour(&a->v_bg, &b->v_bg) != 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
static void put_ansi_attrib(struct b_tty *tty, const char *s)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
if (tty->t_mcount > 0) {
|
|
|
|
|
fputs(";", tty->t_out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fputs(s, tty->t_out);
|
|
|
|
|
tty->t_mcount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void set_attrib(
|
2024-11-22 22:29:40 +00:00
|
|
|
struct b_tty *tty, enum b_tty_attrib old, enum b_tty_attrib new)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
if (old == new) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Bold ON */
|
2024-11-22 22:29:40 +00:00
|
|
|
if (!(old & B_TTY_ATTRIB_BOLD) && new &B_TTY_ATTRIB_BOLD) {
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_BOLD_ON);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Bold OFF */
|
2024-11-22 22:29:40 +00:00
|
|
|
if (old & B_TTY_ATTRIB_BOLD && !(new &B_TTY_ATTRIB_BOLD)) {
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_BOLD_OFF);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Underline ON */
|
2024-11-22 22:29:40 +00:00
|
|
|
if (!(old & B_TTY_ATTRIB_UNDERLINE) && new &B_TTY_ATTRIB_UNDERLINE) {
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Underline OFF */
|
2024-11-22 22:29:40 +00:00
|
|
|
if (old & B_TTY_ATTRIB_UNDERLINE && !(new &B_TTY_ATTRIB_UNDERLINE)) {
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Italic ON */
|
2024-11-22 22:29:40 +00:00
|
|
|
if (!(old & B_TTY_ATTRIB_ITALIC) && new &B_TTY_ATTRIB_ITALIC) {
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_ITALIC_ON);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Italic OFF */
|
2024-11-22 22:29:40 +00:00
|
|
|
if (old & B_TTY_ATTRIB_ITALIC && !(new &B_TTY_ATTRIB_ITALIC)) {
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_ITALIC_OFF);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void set_fg(
|
2024-11-22 22:29:40 +00:00
|
|
|
struct b_tty *tty, const struct b_tty_colour *old,
|
|
|
|
|
const struct b_tty_colour *new)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
if (compare_colour(old, new) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
|
|
switch (new->c_mode) {
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_NONE:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_FG);
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_16:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ansi_colour16_fg[new->c_16.value]);
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_256:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_256COLOUR_FG);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_256.value);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_TRUE:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_TRUECOLOUR_FG);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_true.r);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_true.g);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_true.b);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void set_bg(
|
2024-11-22 22:29:40 +00:00
|
|
|
struct b_tty *tty, const struct b_tty_colour *old,
|
|
|
|
|
const struct b_tty_colour *new)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
if (compare_colour(old, new) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
|
|
switch (new->c_mode) {
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_NONE:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_DEFAULTCOLOUR_BG);
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_16:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ansi_colour16_bg[new->c_16.value]);
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_256:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_256COLOUR_BG);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_256.value);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
break;
|
2024-11-22 22:29:40 +00:00
|
|
|
case B_TTY_COLOUR_TRUE:
|
2024-11-20 22:12:36 +00:00
|
|
|
put_ansi_attrib(tty, ANSI_TRUECOLOUR_BG);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_true.r);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_true.g);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
snprintf(buf, sizeof buf, "%u", new->c_true.b);
|
|
|
|
|
put_ansi_attrib(tty, buf);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
if (compare_vmode(&tty->t_vmode, vmode) == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tty->t_mcount = 0;
|
|
|
|
|
|
|
|
|
|
fprintf(tty->t_out, "\033[");
|
|
|
|
|
|
|
|
|
|
set_attrib(tty, tty->t_vmode.v_attrib, vmode->v_attrib);
|
|
|
|
|
set_fg(tty, &tty->t_vmode.v_fg, &vmode->v_fg);
|
|
|
|
|
set_bg(tty, &tty->t_vmode.v_bg, &vmode->v_bg);
|
|
|
|
|
|
|
|
|
|
fprintf(tty->t_out, "m");
|
|
|
|
|
|
|
|
|
|
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
b_keycode b_tty_read_key(struct b_tty *tty)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
int v;
|
|
|
|
|
int fd = fileno(tty->t_in);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
v = read(fd, &c, 1);
|
|
|
|
|
if (v < 1) {
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_EOF;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == '\r' || c == '\n') {
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_RETURN;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == '\b' || c == 0x7F) {
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_BACKSPACE;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c >= 1 && c <= 26) {
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_TTY_CTRL_KEY(c + 'a' - 1);
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c != 0x1b) {
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = read(fd, &c, 1);
|
|
|
|
|
if (v < 1) {
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_EOF;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c != '[') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = read(fd, &c, 1);
|
|
|
|
|
if (v < 1) {
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_EOF;
|
2024-11-20 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
case 'A':
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_ARROW_UP;
|
2024-11-20 22:12:36 +00:00
|
|
|
case 'B':
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_ARROW_DOWN;
|
2024-11-20 22:12:36 +00:00
|
|
|
case 'C':
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_ARROW_RIGHT;
|
2024-11-20 22:12:36 +00:00
|
|
|
case 'D':
|
2024-11-22 22:29:40 +00:00
|
|
|
return B_KEY_ARROW_LEFT;
|
2024-11-20 22:12:36 +00:00
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
void b_tty_move_cursor_x(struct b_tty *tty, enum b_tty_position_base base, int pos)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
2024-11-22 22:29:40 +00:00
|
|
|
if (base == B_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
|
2024-11-20 22:12:36 +00:00
|
|
|
for (int i = 0; i > pos; i--) {
|
|
|
|
|
fputc('\b', tty->t_out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
if (base == B_TTY_POS_START) {
|
2024-11-20 22:12:36 +00:00
|
|
|
if (pos == 0) {
|
|
|
|
|
fputs("\033[G", tty->t_out);
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(tty->t_out, "\033[%dG", pos + 1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (pos > 1) {
|
|
|
|
|
fprintf(tty->t_out, "\033[%dC", pos);
|
|
|
|
|
} else if (pos == 1) {
|
|
|
|
|
fputs("\033[C", tty->t_out);
|
|
|
|
|
} else if (pos == -1) {
|
|
|
|
|
fputs("\033[D", tty->t_out);
|
|
|
|
|
} else if (pos < -1) {
|
|
|
|
|
fprintf(tty->t_out, "\033[%dD", -pos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
void b_tty_move_cursor_y(struct b_tty *tty, enum b_tty_position_base base, int pos)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
2024-11-22 22:29:40 +00:00
|
|
|
if (base == B_TTY_POS_START) {
|
2024-11-20 22:12:36 +00:00
|
|
|
/* we don't need this functionality right now */
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pos > 1) {
|
|
|
|
|
fprintf(tty->t_out, "\033[%dB", pos);
|
|
|
|
|
} else if (pos == 1) {
|
|
|
|
|
fputs("\033[B", tty->t_out);
|
|
|
|
|
} else if (pos == -1) {
|
|
|
|
|
fputs("\033[A", tty->t_out);
|
|
|
|
|
} else if (pos < -1) {
|
|
|
|
|
fprintf(tty->t_out, "\033[%dA", -pos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode)
|
2024-11-20 22:12:36 +00:00
|
|
|
{
|
|
|
|
|
const char *arg;
|
2024-11-22 22:29:40 +00:00
|
|
|
if (mode & B_TTY_CLEAR_ALL) {
|
2024-11-20 22:12:36 +00:00
|
|
|
arg = "2";
|
2024-11-22 22:29:40 +00:00
|
|
|
} else if (mode & B_TTY_CLEAR_TO_CURSOR) {
|
2024-11-20 22:12:36 +00:00
|
|
|
arg = "1";
|
2024-11-22 22:29:40 +00:00
|
|
|
} else if (mode & B_TTY_CLEAR_FROM_CURSOR) {
|
2024-11-20 22:12:36 +00:00
|
|
|
arg = "";
|
|
|
|
|
} else {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 22:29:40 +00:00
|
|
|
if (mode & B_TTY_CLEAR_SCREEN) {
|
2024-11-20 22:12:36 +00:00
|
|
|
fprintf(tty->t_out, "\033[%sJ", arg);
|
2024-11-22 22:29:40 +00:00
|
|
|
} else if (mode & B_TTY_CLEAR_LINE) {
|
2024-11-20 22:12:36 +00:00
|
|
|
fprintf(tty->t_out, "\033[%sK", arg);
|
|
|
|
|
} else {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-22 22:29:40 +00:00
|
|
|
|
|
|
|
|
enum b_status b_tty_get_dimensions(
|
|
|
|
|
struct b_tty *tty, unsigned int *w, unsigned int *h)
|
|
|
|
|
{
|
|
|
|
|
if (!(tty->t_flags & B_TTY_INTERACTIVE)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fd = fileno(tty->t_out);
|
|
|
|
|
struct winsize ws;
|
|
|
|
|
if (ioctl(fd, TIOCGWINSZ, &ws) == -1) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (w) {
|
|
|
|
|
*w = ws.ws_col;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (h) {
|
|
|
|
|
*h = ws.ws_row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|