Files
bluelib/term/include/blue/term/tty.h

181 lines
4.9 KiB
C

#ifndef BLUELIB_TERM_TTY_H_
#define BLUELIB_TERM_TTY_H_
#include <blue/core/misc.h>
#include <blue/core/status.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#define b_stdtty (z__b_tty_get_std())
#define b_stderr (z__b_tty_get_err())
#define B_TTY_CTRL_KEY(c) ((c) | B_MOD_CTRL)
#define B_MAKE_VMODE(fg, bg, a) \
{ \
.v_fg = fg, .v_bg = bg, .v_attrib = (a) \
}
#define B_MAKE_COLOUR_DEFAULT(v) \
{ \
.c_mode = TTY_COLOUR_NONE, \
}
#define B_MAKE_COLOUR_16(v) \
{ \
.c_mode = TTY_COLOUR_16, .c_16 = {.value = (v) } \
}
#define B_MAKE_COLOUR_256(v) \
{ \
.c_mode = TTY_COLOUR_256, .c_256 = {.value = (v) } \
}
#define B_MAKE_COLOUR_TRUE(cr, cg, cb) \
{ \
.c_mode = TTY_COLOUR_TRUE, .c_true \
= {.r = (cr), \
.g = (cg), \
.b = (cb) } \
}
typedef struct b_tty b_tty;
typedef uint32_t b_keycode;
/* codepoints U+F0000 to U+FFFFD are reserved areas in Unicode, free for
* application use. store special keycodes here */
enum {
B_KEY_ARROW_LEFT = 0xF0000,
B_KEY_ARROW_RIGHT,
B_KEY_ARROW_UP,
B_KEY_ARROW_DOWN,
B_KEY_BACKSPACE,
B_KEY_RETURN,
B_MOD_CTRL = 0x10000000,
B_KEY_EOF = 0xFFFFFFFF,
};
typedef enum b_tty_colour16 {
B_TTY_COLOUR16_BLACK = 0,
B_TTY_COLOUR16_RED,
B_TTY_COLOUR16_GREEN,
B_TTY_COLOUR16_YELLOW,
B_TTY_COLOUR16_BLUE,
B_TTY_COLOUR16_MAGENTA,
B_TTY_COLOUR16_CYAN,
B_TTY_COLOUR16_WHITE,
B_TTY_COLOUR16_BRIGHT_BLACK,
B_TTY_COLOUR16_BRIGHT_RED,
B_TTY_COLOUR16_BRIGHT_GREEN,
B_TTY_COLOUR16_BRIGHT_YELLOW,
B_TTY_COLOUR16_BRIGHT_BLUE,
B_TTY_COLOUR16_BRIGHT_MAGENTA,
B_TTY_COLOUR16_BRIGHT_CYAN,
B_TTY_COLOUR16_BRIGHT_WHITE,
} b_tty_colour16;
typedef enum b_tty_colour_mode {
B_TTY_COLOUR_NONE = 0,
B_TTY_COLOUR_16,
B_TTY_COLOUR_256,
B_TTY_COLOUR_TRUE,
} b_tty_colour_mode;
typedef enum b_tty_position_base {
B_TTY_POS_START,
B_TTY_POS_CURSOR,
} b_tty_position_base;
typedef enum b_tty_attrib {
B_TTY_ATTRIB_NORMAL = 0x00u,
B_TTY_ATTRIB_BOLD = 0x01u,
B_TTY_ATTRIB_UNDERLINE = 0x02u,
B_TTY_ATTRIB_ITALIC = 0x04u,
B_TTY_ATTRIB_INVERT = 0x08u,
} b_tty_attrib;
typedef enum b_tty_clear_mode {
B_TTY_CLEAR_LINE = 0x01u,
B_TTY_CLEAR_SCREEN = 0x02,
B_TTY_CLEAR_TO_CURSOR = 0x0100u,
B_TTY_CLEAR_FROM_CURSOR = 0x0200u,
B_TTY_CLEAR_ALL = 0x0400u,
} b_tty_clear_mode;
typedef enum b_tty_mode {
B_TTY_CANONICAL = 0x00u,
B_TTY_RAW = 0x01u,
} b_tty_mode;
typedef enum b_tty_print_flags {
B_TTY_DISABLE_FORMATTING = 0x01u,
B_TTY_DISABLE_INTERPOLATED_FORMATTING = 0x02u,
} b_tty_print_flags;
typedef struct b_tty_colour {
enum b_tty_colour_mode c_mode;
union {
struct {
uint8_t value;
} c_16;
struct {
uint8_t value;
} c_256;
struct {
uint8_t r;
uint8_t g;
uint8_t b;
} c_true;
};
} b_tty_colour;
typedef struct b_tty_vmode {
enum b_tty_attrib v_attrib;
struct b_tty_colour v_fg;
struct b_tty_colour v_bg;
} b_tty_vmode;
BLUE_API b_tty *z__b_tty_get_std(void);
BLUE_API b_tty *z__b_tty_get_err(void);
static inline unsigned int b_keycode_get_key(b_keycode v)
{
return v & 0x0FFFFFFF;
}
BLUE_API bool b_tty_is_interactive(const struct b_tty *tty);
BLUE_API void b_tty_set_mode(struct b_tty *tty, enum b_tty_mode mode);
BLUE_API void b_tty_set_vmode(struct b_tty *tty, const struct b_tty_vmode *vmode);
BLUE_API void b_tty_reset_vmode(struct b_tty *tty);
BLUE_API enum b_status b_tty_get_dimensions(
struct b_tty *tty, unsigned int *w, unsigned int *h);
BLUE_API enum b_status b_tty_get_cursor_position(
struct b_tty *tty, unsigned int *x, unsigned int *y);
BLUE_API b_keycode b_tty_read_key(struct b_tty *tty);
BLUE_API void b_tty_move_cursor_x(
struct b_tty *tty, enum b_tty_position_base base, int pos);
BLUE_API void b_tty_move_cursor_y(
struct b_tty *tty, enum b_tty_position_base base, int pos);
BLUE_API void b_tty_clear(struct b_tty *tty, enum b_tty_clear_mode mode);
BLUE_API int b_tty_putc(struct b_tty *tty, enum b_tty_print_flags flags, char c);
BLUE_API int b_tty_puts(
struct b_tty *tty, enum b_tty_print_flags flags, const char *s);
BLUE_API int b_tty_printf(
struct b_tty *tty, enum b_tty_print_flags flags, const char *s, ...);
BLUE_API int b_tty_vprintf(
struct b_tty *tty, enum b_tty_print_flags flags, const char *s,
va_list args);
#endif