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
|
#include <stdlib.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "framebuffer.h"
size_t term_width = 0,
term_height = 0;
struct termios initial_terminal_state = {0};
Framebuffer fb_new(size_t width, size_t height) {
Framebuffer fb;
/* In case malloc() fails and we return early */
fb.width = 0;
fb.height = 0;
fb.fb = malloc(sizeof(Pixel *) * height);
if (fb.fb == NULL) return fb;
for (size_t i = 0; i < height; i++) {
fb.fb[i] = malloc(sizeof(Pixel) * width);
if (fb.fb[i] == NULL) {
/* FIXME: Memory leak; should free all prev. malloced rows */
fb.fb = NULL;
return fb;
}
}
fb.width = width;
fb.height = height;
return fb;
}
void fb_free(Framebuffer *fb) {
if (fb->fb == NULL) goto ret;
for (size_t i = 0; i < fb->height; i++) {
free(fb->fb[i]);
}
free(fb->fb);
ret:
fb->width = 0;
fb->height = 0;
return;
}
void fb_fill(Framebuffer fb, Pixel pixel) {
for (size_t row = 0; row < fb.height; row++) {
for (size_t col = 0; col < fb.width; col++) {
fb.fb[row][col] = pixel;
}
}
}
void fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel) {
fb.fb[row][col] = pixel;
}
void term_init(void) {
tcgetattr(0, &initial_terminal_state);
struct termios new = initial_terminal_state;
new.c_lflag &= ~ICANON; /* Turn off canonical mode */
new.c_lflag &= ~ECHO; /* Turn off echoing */
new.c_cc[VMIN] = 0; /* Return read(STDIN_FILENO, ...) even if no input */
new.c_cc[VTIME] = 0; /* ^, make it return immediately, without waiting */
tcsetattr(0, TCSANOW, &new);
write(STDOUT_FILENO, "\x1b[?1049h", 8); /* Alternative screen buffer */
write(STDOUT_FILENO, "\x1b[H", 3); /* Move cursor to top-left */
write(STDOUT_FILENO, "\x1b[?25l", 6); /* Hide the cursor */
term_update_size();
}
void term_cleanup(void) {
tcsetattr(0, TCSANOW, &initial_terminal_state);
write(STDOUT_FILENO, "\x1b[?25h", 6); /* Show the cursor */
write(STDOUT_FILENO, "\x1b[?1049l", 8); /* Alternative screen buffer */
write(STDOUT_FILENO, "\x1b[0m", 4); /* Reset styling (colour) */
}
void term_update_size(void) {
#if defined(TIOCGWINSZ)
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0) {
term_height = ws.ws_row;
term_width = ws.ws_col;
return;
}
#elif defined(TIOCGSIZE)
struct ttysize ts;
if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) == 0) {
term_height = ts.ts_row;
term_width = ts.ts_col;
return;
}
#else
#error "Neither TIOCGWINSZ or TIOCGSIZE are defined, can't proceed"
#endif
}
void fb_print(Framebuffer fb) {
write(STDOUT_FILENO, "\x1b[H", 3); /* Move cursor to top-left */
for (size_t row = 0; row < fb.height; row++) {
for (size_t col = 0; col < fb.width; col++) {
set_fg(fb.fb[row][col].fg);
set_bg(fb.fb[row][col].bg);
write(STDOUT_FILENO, &(fb.fb[row][col].ch), sizeof(wchar_t));
}
}
fflush(stdout);
}
static inline void set_fg(Colour col) {
fprintf(stdout, "\x1b[38;2;%u;%u;%um", col.r, col.g, col.g);
fflush(stdout);
}
static inline void set_bg(Colour col) {
fprintf(stdout, "\x1b[48;2;%u;%u;%um", col.r, col.g, col.g);
fflush(stdout);
}
|