frontend: add a line editor for shell input
This commit is contained in:
140
frontend/line-ed/tty.h
Normal file
140
frontend/line-ed/tty.h
Normal file
@@ -0,0 +1,140 @@
|
||||
#ifndef DARWIN_TTY_H_
|
||||
#define DARWIN_TTY_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define S_TTY_CTRL_KEY(c) ((c) | S_MOD_CTRL)
|
||||
|
||||
struct s_tty;
|
||||
|
||||
#define MAKE_VMODE(fg, bg, a) \
|
||||
{ \
|
||||
.v_fg = fg, \
|
||||
.v_bg = bg, \
|
||||
.v_attrib = (a) \
|
||||
}
|
||||
|
||||
#define MAKE_COLOUR_DEFAULT(v) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_NONE, \
|
||||
}
|
||||
|
||||
#define MAKE_COLOUR_16(v) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_16, \
|
||||
.c_16 = { \
|
||||
.value = (v) \
|
||||
} \
|
||||
}
|
||||
|
||||
#define MAKE_COLOUR_256(v) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_256, \
|
||||
.c_256 = { \
|
||||
.value = (v) \
|
||||
} \
|
||||
}
|
||||
|
||||
#define MAKE_COLOUR_TRUE(cr, cg, cb) \
|
||||
{ \
|
||||
.c_mode = TTY_COLOUR_TRUE, \
|
||||
.c_true = { \
|
||||
.r = (cr), \
|
||||
.g = (cg), \
|
||||
.b = (cb) \
|
||||
} \
|
||||
}
|
||||
|
||||
typedef uint32_t s_keycode;
|
||||
|
||||
/* codepoints U+F0000 to U+FFFFD are reserved areas in Unicode, free for
|
||||
* application use. store special keycodes here */
|
||||
enum s_keys {
|
||||
S_KEY_ARROW_LEFT = 0xF0000,
|
||||
S_KEY_ARROW_RIGHT,
|
||||
S_KEY_ARROW_UP,
|
||||
S_KEY_ARROW_DOWN,
|
||||
S_KEY_BACKSPACE,
|
||||
S_KEY_RETURN,
|
||||
|
||||
S_MOD_CTRL = 0x10000000,
|
||||
|
||||
S_KEY_EOF = 0xFFFFFFFF,
|
||||
};
|
||||
|
||||
enum s_tty_colour16 {
|
||||
TTY_COLOUR16_BLACK = 0,
|
||||
TTY_COLOUR16_RED,
|
||||
TTY_COLOUR16_GREEN,
|
||||
TTY_COLOUR16_YELLOW,
|
||||
TTY_COLOUR16_BLUE,
|
||||
TTY_COLOUR16_MAGENTA,
|
||||
TTY_COLOUR16_CYAN,
|
||||
TTY_COLOUR16_WHITE,
|
||||
TTY_COLOUR16_GREY,
|
||||
TTY_COLOUR16_BRIGHT_RED,
|
||||
TTY_COLOUR16_BRIGHT_GREEN,
|
||||
TTY_COLOUR16_BRIGHT_YELLOW,
|
||||
TTY_COLOUR16_BRIGHT_BLUE,
|
||||
TTY_COLOUR16_BRIGHT_MAGENTA,
|
||||
TTY_COLOUR16_BRIGHT_CYAN,
|
||||
TTY_COLOUR16_BRIGHT_WHITE,
|
||||
};
|
||||
|
||||
enum s_tty_colour_mode {
|
||||
TTY_COLOUR_NONE = 0,
|
||||
TTY_COLOUR_16,
|
||||
TTY_COLOUR_256,
|
||||
TTY_COLOUR_TRUE,
|
||||
};
|
||||
|
||||
enum s_tty_attrib {
|
||||
TTY_ATTRIB_NORMAL = 0x00u,
|
||||
TTY_ATTRIB_BOLD = 0x01u,
|
||||
TTY_ATTRIB_UNDERLINE = 0x02u,
|
||||
TTY_ATTRIB_ITALIC = 0x04u,
|
||||
};
|
||||
|
||||
struct s_tty_colour {
|
||||
enum s_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;
|
||||
};
|
||||
};
|
||||
|
||||
struct s_tty_vmode {
|
||||
enum s_tty_attrib v_attrib;
|
||||
struct s_tty_colour v_fg;
|
||||
struct s_tty_colour v_bg;
|
||||
};
|
||||
|
||||
static inline unsigned int s_keycode_get_key(s_keycode v)
|
||||
{
|
||||
return v & 0x0FFFFFFF;
|
||||
}
|
||||
|
||||
extern struct s_tty *s_get_tty(void);
|
||||
extern bool s_tty_is_interactive(const struct s_tty *tty);
|
||||
|
||||
extern void s_tty_set_raw(struct s_tty *tty);
|
||||
extern void s_tty_set_canon(struct s_tty *tty);
|
||||
extern void s_tty_reset_vmode(struct s_tty *tty);
|
||||
extern void s_tty_set_vmode(struct s_tty *tty, const struct s_tty_vmode *vmode);
|
||||
|
||||
extern s_keycode s_tty_read_key(struct s_tty *tty);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user