aboutsummaryrefslogtreecommitdiff
path: root/host/main.c
blob: d8cb37198deb8cb3d4cbca00121094cd1d893aae (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "config.h"
#include "main.h"
#include "color.h"
#include "font.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
#include <sys/timeb.h>
#include <sys/queue.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>


void free_framebuffer(framebuffer_t *fb){
	free(fb->data);
	free(fb);
}

/* CAUTION: REQUIRES INPUT TO BE \0-TERMINATED
 * ...also, it does a hardcoded setlocale of LC_CTYPE to en_US.utf8 for... reasons. */
framebuffer_t *framebuffer_render_text(char *s, glyphtable_t *glyph_table){
	unsigned int len = strlen(s);

	color_t *gbuf = NULL;
	unsigned int gbufwidth = 0;
	unsigned int gbufheight = 0;
	char *p = s;

	/* Calculate screen width of string prior to allocating memory for the frame buffer */
	wchar_t c;
	mbstate_t ps = {0};
	memset(&ps, 0, sizeof(mbstate_t));
	if(!setlocale(LC_CTYPE, "en_US.utf8")){
		fprintf(stderr, "Cannot set locale\n");
		goto error;
	}
	for(;;){
		while(*p == '\033'){
			p++;
			/* Jump over escape sequences */
			for(;;p++){
				if(!(*p == ';' || *p == '[' || ('0' <= *p && *p <= '9'))){
					p++;
					break;
				}
			}
			memset(&ps, 0, sizeof(mbstate_t));
		}

		size_t inc = mbrtowc(&c, p, MB_CUR_MAX, &ps); /* MB_CUR_MAX is safe since p is \0-terminated */
		if(inc == -1 || inc == -2){
			fprintf(stderr, "Error rendering string: No valid UTF-8 input.\n");
			goto error;
		}
		if(inc == 0) /* Reached end of string */
			break;
		p += inc;

		if(c > glyph_table->size){
			fprintf(stderr, "Error rendering string: Codepoint 0x%lx out of valid range (0-%zd).\n", (long int)c, glyph_table->size);
			goto error;
		}

		glyph_t *g = glyph_table->data[c];
		if(!g){
			fprintf(stderr, "Error rendering string: Codepoint 0x%lx not in font.\n", (long int)c);
			goto error;
		}

		if(g->height > gbufheight)
			gbufheight = g->height;


		gbufwidth += g->width;
	}
	/* For easier rendering on the terminal, round up to multiples of two */
	gbufheight += gbufheight&1;

	size_t gbufsize = gbufwidth*gbufheight;
	gbuf = calloc(gbufsize, sizeof(color_t));
	if(!gbuf){
		fprintf(stderr, "Cannot malloc() %zu bytes.\n", gbufsize*sizeof(color_t));
		goto error;
	}
	memset(gbuf, 0, gbufsize*sizeof(color_t));

	unsigned int x = 0;
	p = s;
	memset(&ps, 0, sizeof(mbstate_t));
	struct {
		color_t fg;
		color_t bg;
		unsigned int blink:4;
		unsigned int bold:1; /* TODO */
		unsigned int underline:1;
		unsigned int strikethrough:1;
		unsigned int fraktur:1; /* TODO See: Flat10 Fraktur font */
		unsigned int invert:1;
	} style = {
		colortable[DEFAULT_FG_COLOR], colortable[DEFAULT_BG_COLOR], 0, 0, 0, 0, 0, 0
	};
	/* Render glyphs (now with escape sequence rendering!) */
	for(;;){
		/* NOTE: This nested escape sequence parsing does not contain any unicode-awareness whatsoever */
		if(*p == '\033'){ /* Escape sequence YAY */
			char *sequence_start = ++p;
			if(*p == '['){ /* This was a CSI! */
				/* Disassemble the list of numbers, only accept SGR sequences (those ending with 'm') */
				long elems[MAX_CSI_ELEMENTS];
				int nelems;
				for(nelems = 0; nelems<MAX_CSI_ELEMENTS; nelems++){
					p++;
					char *endptr;
					elems[nelems] = strtol(p, &endptr, 10);
					if(p == endptr){
						fprintf(stderr, "Invalid escape sequence: \"\\e%s\"\n", sequence_start);
						goto error;
					}
					p = endptr;
					if(*endptr == 'm')
						break;
					if(*endptr != ';'){
						fprintf(stderr, "Invalid escape sequence: \"\\e%s\"\n", sequence_start);
						goto error;
					}
				}
				p++; /* gobble up trailing 'm' of "\033[23;42m" */
				nelems++;
				/* By now we know it's a SGR since we error'ed out on anything else */
				if(nelems < 1){
					fprintf(stderr, "Unsupported escape sequence: \"\\e%s\"\n", sequence_start);
					goto error;
				}
				/* Parse the sequence numbers */
				for(int i=0; i<nelems; i++){
					switch(elems[i]){
						case 0: /* reset style */
							style.fg = colortable[DEFAULT_FG_COLOR];
							style.bg = colortable[DEFAULT_BG_COLOR];
							style.bold = 0;
							style.underline = 0;
							style.blink = 0;
							style.strikethrough = 0;
							style.fraktur = 0;
							style.invert = 0;
							break;
						case 1: /* bold */
							style.bold = 1;
							break;
						case 4: /* underline */
							style.underline = 1;
							break;
						case 5: /* slow blink */
							style.blink = 1;
							break;
						case 6: /* rapid blink */
							style.blink = 8;
							break;
						case 7: /* color invert on */
							style.invert = 1;
							break;
						case 9: /* strike-through */
							style.strikethrough = 1;
							break;
						case 20:/* Fraktur */
							style.fraktur = 1;
							break;
						case 22:/* Bold off */
							style.bold = 0;
							break;
						case 24:/* Underline off */
							style.underline = 0;
							break;
						case 25:/* Blink off */
							style.blink = 0;
							break;
						case 27:/* color invert off */
							style.invert = 0;
							break;
						case 29:/* strike-through off */
							style.strikethrough = 0;
							break;
						case 30: /* Set foreground color, "dim" colors */
						case 31:
						case 32:
						case 33:
						case 34:
						case 35:
						case 36:
						case 37:
							style.fg = colortable[elems[i]-30];
							break;
						case 38: /* Set xterm-256 foreground color */
							i++;
							if(nelems-i < 2 || elems[i] != 5){
								fprintf(stderr, "Invalid ANSI escape code: \"\\e%s\"\n", sequence_start);
								goto error;
							}
							style.fg = colortable[elems[++i]];
							break;
						case 39: /* Reset foreground color to default */
							style.bg = colortable[DEFAULT_FG_COLOR];
							break;
						case 40: /* Set background color, "dim" colors */
						case 41:
						case 42:
						case 43:
						case 44:
						case 45:
						case 46:
						case 47:
							style.bg = colortable[elems[i]-40];
							break;
						case 48: /* Set xterm-256 background color */
							i++;
							if(nelems-i < 2 || elems[i] != 5){
								fprintf(stderr, "Invalid ANSI escape code: \"\\e%s\"\n", sequence_start);
								goto error;
							}
							style.bg = colortable[elems[++i]];
							break;
						case 49: /* Reset background color to default */
							style.bg = colortable[DEFAULT_BG_COLOR];
							break;
						case 90: /* Set foreground color, "bright" colors */
						case 91:
						case 92:
						case 93:
						case 94:
						case 95:
						case 96:
						case 97:
							style.fg = colortable[elems[i]-90+8];
							break;
						case 100: /* Set background color, "bright" colors */
						case 101:
						case 102:
						case 103:
						case 104:
						case 105:
						case 106:
						case 107:
							style.bg = colortable[elems[i]-100+8];
							break;
						default:
							fprintf(stderr, "Unsupported escape sequence: \"\\e%s\"\n", sequence_start);
							goto error;
					}
				}

			}else{
				fprintf(stderr, "Unsupported escape sequence: \"\\e%s\"\n", sequence_start);
				goto error;
			}

			continue;
		}

		size_t inc = mbrtowc(&c, p, (s+len+1)-p, NULL);
		/* If p contained */
		if(inc == 0) /* Reached end of string */
			break;
		p += inc;

		/* Render glyph into frame buffer */
		struct timeb time = {0};
		ftime(&time);
		unsigned long int t = time.time*1000 + time.millitm;
		int blink = style.blink && (t % (1000/style.blink) < (333/style.blink));
		int inv = !(style.invert ^ blink);
		color_t fg = inv ? style.fg : style.bg;
		color_t bg = inv ? style.bg : style.fg;

		glyph_t *g = glyph_table->data[c];
		render_glyph(g, gbuf, gbufwidth, x, 0, fg, bg);
		if(style.strikethrough || style.underline){
			int sty = gbufheight/2;
			/* g->y usually is a negative index of the glyph's baseline measured from the glyph's bottom */
			int uly = gbufheight + g->y;
			for(int i=0; i<g->width; i++){
				if(style.strikethrough)
					gbuf[sty*gbufwidth + x + i] = fg;
				if(style.underline)
					gbuf[uly*gbufwidth + x + i] = fg;
			}
		}
		x += g->width;
	}
	framebuffer_t *fb = malloc(sizeof(framebuffer_t));
	if(!fb){
		fprintf(stderr, "Cannot malloc() %zu bytes.\n", sizeof(framebuffer_t));
		goto error;
	}
	fb->w = gbufwidth;
	fb->h = gbufheight;
	fb->data = gbuf;
	return fb;
error:
	free(gbuf);
	return 0;
}

void console_render_buffer(color_t *data, size_t w, size_t h){
	/* Render framebuffer to terminal, two pixels per character using Unicode box drawing stuff */
	color_t lastfg = {0, 0, 0}, lastbg = {0, 0, 0};
	printf("\e[38;5;0;48;5;0m\e[K");
	for(size_t y=0; y < h; y+=2){
		for(size_t x=0; x < w; x++){
			/* Da magicks: ▀█▄ */
			color_t ct = data[y*w + x]; /* Top pixel */
			color_t cb = data[(y+1)*w + x]; /* Bottom pixel */
			/* The following, rather convoluted logic tries to "save" escape sequences when rendering. */
			if(!memcmp(&ct, &lastfg, sizeof(color_t))){
				if(!memcmp(&cb, &lastbg, sizeof(color_t))){
					printf("▀");
				}else if(!memcmp(&cb, &lastfg, sizeof(color_t))){
					printf("█");
				}else{
					printf("\033[48;5;%dm▀", xterm_color_index(cb));
					lastbg = cb;
				}
			}else if(!memcmp(&ct, &lastbg, sizeof(color_t))){
				if(!memcmp(&cb, &lastfg, sizeof(color_t))){
					printf("▄");
				}else if(!memcmp(&cb, &lastbg, sizeof(color_t))){
					printf(" ");
				}else{
					printf("\033[38;5;%dm▄", xterm_color_index(cb));
					lastfg = cb;
				}
			}else{ /* No matches for the upper pixel */
				if(!memcmp(&cb, &lastfg, sizeof(color_t))){
					printf("\033[48;5;%dm▄", xterm_color_index(ct));
					lastbg = ct;
				}else if(!memcmp(&cb, &lastbg, sizeof(color_t))){
					printf("\033[38;5;%dm▀", xterm_color_index(ct));
					lastfg = ct;
				}else{
					printf("\033[38;5;%d;48;5;%dm▀", xterm_color_index(ct), xterm_color_index(cb));
					lastfg = ct;
					lastbg = cb;
				}
			}
		}
		printf("\n\e[K");
	}
	printf("\033[0m");
}