summaryrefslogtreecommitdiff
path: root/vui.h
blob: 67c1ca35a51033ad6cb181fa096eb2ea6c3854e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef VUI_H
#define VUI_H

#include <stdarg.h>
#include <stdint.h>

typedef enum {
	KEY_EOF      = 0,
	KEY_BKSP     = 0x08,
	KEY_RET      = 0x0d,
	KEY_ESC      = 0x1b,
	KEY_DEL      = 0x7f,
	KEY_UTF8_MAX = 0x10ffff,

	KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN,
	KEY_HOME, KEY_END, KEY_PGUP, KEY_PGDN,

	KEY_F1,  KEY_F2,  KEY_F3,  KEY_F4,
	KEY_F5,  KEY_F6,  KEY_F7,  KEY_F8,
	KEY_F9,  KEY_F10, KEY_F11, KEY_F12,

	KEY_SHIFT_BIT = 0x200000,
	KEY_CTRL_BIT  = 0x400000,
	KEY_ALT_BIT   = 0x800000,
	KEY_META_BIT  = 0x1000000,
	KEY_INVALID   = 0x7fffffff,

	KEY_CTRL_MASK = 0xe000000,
	KEY_BASE_MASK = ~KEY_CTRL_MASK,
} VuiKey;

typedef enum {
	FG_BLACK     = 0,
	FG_RED       = 1,
	FG_GREEN     = 2,
	FG_YELLOW    = 3,
	FG_BLUE      = 4,
	FG_MAGENTA   = 5,
	FG_CYAN      = 6,
	FG_WHITE     = 7,
	FG_DEFAULT   = 9,

	FG_BBLACK    = FG_BLACK    + 9,
	FG_BRED      = FG_RED      + 9,
	FG_BGREEN    = FG_GREEN    + 9,
	FG_BYELLOW   = FG_YELLOW   + 9,
	FG_BBLUE     = FG_BLUE     + 9,
	FG_BMAGENTA  = FG_MAGENTA  + 9,
	FG_BCYAN     = FG_CYAN     + 9,
	FG_BWHITE    = FG_WHITE    + 9,

	BG_BLACK     = FG_BLACK    << 5,
	BG_RED       = FG_RED      << 5,
	BG_GREEN     = FG_GREEN    << 5,
	BG_YELLOW    = FG_YELLOW   << 5,
	BG_BLUE      = FG_BLUE     << 5,
	BG_MAGENTA   = FG_MAGENTA  << 5,
	BG_CYAN      = FG_CYAN     << 5,
	BG_WHITE     = FG_WHITE    << 5,

	BG_DEFAULT   = FG_DEFAULT  << 5,

	BG_BBLACK    = FG_BBLACK   << 5,
	BG_BRED      = FG_BRED     << 5,
	BG_BGREEN    = FG_BGREEN   << 5,
	BG_BYELLOW   = FG_BYELLOW  << 5,
	BG_BBLUE     = FG_BBLUE    << 5,
	BG_BMAGENTA  = FG_BMAGENTA << 5,
	BG_BCYAN     = FG_BCYAN    << 5,
	BG_BWHITE    = FG_BWHITE   << 5,

	A_BOLD       = 1 << 10,
	A_DIM        = 1 << 11,
	A_ITALIC     = 1 << 12,
	A_UNDERSCORE = 1 << 13,
	A_BLINK      = 1 << 14,
	A_REVERSE    = 1 << 15,
} VuiAttr;

#define A_DEFAULT (FG_DEFAULT | BG_DEFAULT)

typedef enum {
	VUI_CURS_BLOCK_BLINK,
	VUI_CURS_DEFAULT,
	VUI_CURS_BLOCK,
	VUI_CURS_UNDERLINE_BLINK,
	VUI_CURS_UNDERLINE,
	VUI_CURS_BAR_BLINK,
	VUI_CURS_BAR,
} VuiCursorShape;

#define ATTR_FG(a) ((a) & 0x1f)
#define ATTR_BG(a) (((a)>>5) & 0x1f)
#define ATTR_A(a) ((a) & ~0xff)

typedef uint32_t VuiChar;

typedef struct {
	unsigned width, height;
	VuiChar *chr;
	uint16_t *attr;
} VuiBuffer;

typedef struct {
	unsigned width, height;
	VuiBuffer buf1, buf2;
	VuiBuffer *front, *back;
	void (*redraw_fn)(void *ctx);
	void *redraw_ctx;
	int redraw_all;
	int scroll_x, scroll_y;
	int curs_vis, curs_x, curs_y;
} VuiWindow;

VuiWindow *vui_win(void);

#define LINES (vui_win()->height)
#define COLS  (vui_win()->width)

#define BCHR(b,x,y)  ((b)->chr[(x) + (y) * (b)->width])
#define BATTR(b,x,y) ((b)->attr[(x) + (y) * (b)->width])
#define CHR(x,y)  BCHR(vui_win()->front, x, y)
#define ATTR(x,y) BATTR(vui_win()->front, x, y)

void vui_init(void);
void vui_fini(void);

void vui_enable(void);
void vui_disable(void);

void vui_redraw(void);
void vui_blit(void);
void vui_clear(void);
void vui_scroll(int dx, int dy);

void vui_chr(int x, int y, VuiChar c);
void vui_chra(int x, int y, VuiChar c, VuiAttr a);

unsigned vui_puts(int x, int y, const char *s);
unsigned vui_putsa(int x, int y, const char *s, VuiAttr a);
unsigned vui_putsn(int x, int y, const char *s, unsigned n);
unsigned vui_putsna(int x, int y, const char *s, unsigned n, VuiAttr a);

int vui_avprintf(int x, int y, VuiAttr a, const char *fmt, va_list ap);
int vui_aprintf(int x, int y, VuiAttr a, const char *fmt, ...);
int vui_printf(int x, int y, const char *fmt, ...);

void vui_fill(VuiChar c, VuiAttr a);
void vui_fill_rect(VuiChar c, VuiAttr a, int x0, int y0, int width, int height);

void vui_curs_vis(int vis);
void vui_curs_pos(int x, int y);
void vui_curs_shape(VuiCursorShape sh);

void vui_redraw_fn(void (*fn)(void *ctx));
void vui_redraw_ctx(void *ctx);

int vui_wait_for_input(int ms);
int vui_has_input(void);

VuiKey vui_key(void);

#endif