Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1/* Copyright (C) 2022-2024 mintsuki and contributors.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this list of conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 * POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <stdint.h>
27#include <stddef.h>
28#include <stdbool.h>
29
30#include "../flanterm.h"
31#include "fb.h"
32
33void *memset(void *, int, size_t);
34void *memcpy(void *, const void *, size_t);
35
36#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
37
38#ifndef FLANTERM_FB_BUMP_ALLOC_POOL_SIZE
39#define FLANTERM_FB_BUMP_ALLOC_POOL_SIZE 873000
40
41#define FLANTERM_FB_WIDTH_LIMIT 1920
42#define FLANTERM_FB_HEIGHT_LIMIT 1200
43#endif
44
45static uint8_t bump_alloc_pool[FLANTERM_FB_BUMP_ALLOC_POOL_SIZE];
46static size_t bump_alloc_ptr = 0;
47
48static void *bump_alloc(size_t s) {
49 size_t next_ptr = bump_alloc_ptr + s;
50 if (next_ptr > FLANTERM_FB_BUMP_ALLOC_POOL_SIZE) {
51 return NULL;
52 }
53 void *ret = &bump_alloc_pool[bump_alloc_ptr];
54 bump_alloc_ptr = next_ptr;
55 return ret;
56}
57
58static bool bump_allocated_instance = false;
59
60#endif
61
62// Builtin font originally taken from:
63// https://github.com/viler-int10h/vga-text-mode-fonts/raw/master/FONTS/PC-OTHER/TOSH-SAT.F16
64static const uint8_t builtin_font[] = {
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x42, 0x81, 0x81, 0xa5, 0xa5, 0x81,
67 0x81, 0xa5, 0x99, 0x81, 0x42, 0x3c, 0x00, 0x00, 0x00, 0x3c, 0x7e, 0xff,
68 0xff, 0xdb, 0xdb, 0xff, 0xff, 0xdb, 0xe7, 0xff, 0x7e, 0x3c, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10,
70 0x10, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x38, 0x38, 0x7c, 0x7c, 0xfe,
71 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18,
72 0x3c, 0x3c, 0xdb, 0xff, 0xff, 0xdb, 0x18, 0x18, 0x3c, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x18, 0x3c, 0x7e, 0xff, 0xff, 0xff, 0x66, 0x18, 0x18,
74 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x78,
75 0x78, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xe7, 0xc3, 0xc3, 0xe7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xcc, 0x84, 0x84, 0xcc, 0x78, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc3, 0x99, 0xbd,
79 0xbd, 0x99, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x1e,
80 0x0e, 0x1e, 0x32, 0x78, 0xcc, 0xcc, 0xcc, 0x78, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0xfc, 0x30, 0x30,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x18, 0x1c, 0x1e, 0x16, 0x12,
83 0x10, 0x10, 0x70, 0xf0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x30, 0x38, 0x2c,
84 0x26, 0x32, 0x3a, 0x2e, 0x26, 0x22, 0x62, 0xe2, 0xc6, 0x0e, 0x0c, 0x00,
85 0x00, 0x00, 0x00, 0x18, 0x18, 0xdb, 0x3c, 0xe7, 0x3c, 0xdb, 0x18, 0x18,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0xe0, 0xf8, 0xfe,
87 0xf8, 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
88 0x06, 0x0e, 0x3e, 0xfe, 0x3e, 0x0e, 0x06, 0x02, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x30, 0x78, 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30, 0xfc, 0x78,
90 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
91 0xcc, 0xcc, 0x00, 0xcc, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xdb,
92 0xdb, 0xdb, 0xdb, 0x7b, 0x1b, 0x1b, 0x1b, 0x1b, 0x1b, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x7c, 0xc6, 0x60, 0x38, 0x6c, 0xc6, 0xc6, 0x6c, 0x38, 0x0c,
94 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0xfe, 0xfe, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x78,
96 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30, 0xfc, 0x78, 0x30, 0xfc, 0x00, 0x00,
97 0x00, 0x00, 0x30, 0x78, 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
98 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
99 0x30, 0x30, 0xfc, 0x78, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0x00, 0x18, 0x0c, 0xfe, 0xfe, 0x0c, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x60, 0xfe, 0xfe, 0x60, 0x30, 0x00,
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
103 0xc0, 0xc0, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 0x00, 0x24, 0x66, 0xff, 0xff, 0x66, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x38, 0x38, 0x7c, 0x7c, 0xfe, 0xfe,
106 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe, 0x7c, 0x7c,
107 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x00, 0x00, 0x30, 0x78, 0x78, 0x78, 0x78, 0x30, 0x30, 0x30, 0x00, 0x30,
110 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6c,
112 0x6c, 0xfe, 0x6c, 0x6c, 0x6c, 0xfe, 0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x00,
113 0x00, 0x18, 0x18, 0x7c, 0xc6, 0xc0, 0xc0, 0x7c, 0x06, 0x06, 0xc6, 0x7c,
114 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0x0c, 0x0c, 0x18, 0x38,
115 0x30, 0x60, 0x60, 0xc6, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x6c,
116 0x6c, 0x38, 0x30, 0x76, 0xde, 0xcc, 0xcc, 0xde, 0x76, 0x00, 0x00, 0x00,
117 0x00, 0x00, 0x18, 0x18, 0x18, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x30, 0x60, 0x60, 0x60, 0x60,
119 0x60, 0x60, 0x60, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x30,
120 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x30, 0x60, 0x00, 0x00, 0x00,
121 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x38, 0xfe, 0x38, 0x6c, 0x00, 0x00,
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x7e,
123 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06,
128 0x0c, 0x0c, 0x18, 0x38, 0x30, 0x60, 0x60, 0xc0, 0xc0, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xd6, 0xd6, 0xd6, 0xc6, 0xc6, 0xc6,
130 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x38, 0x78, 0x18, 0x18, 0x18,
131 0x18, 0x18, 0x18, 0x18, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6,
132 0x06, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0xc0, 0xfe, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x7c, 0xc6, 0x06, 0x06, 0x3c, 0x06, 0x06, 0x06, 0x06, 0xc6,
134 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x0c, 0x1c, 0x3c, 0x6c, 0xcc,
135 0xfe, 0x0c, 0x0c, 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xc0,
136 0xc0, 0xc0, 0xfc, 0x06, 0x06, 0x06, 0x06, 0xc6, 0x7c, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x3c, 0x60, 0xc0, 0xc0, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
138 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xc6, 0x06, 0x06, 0x0c, 0x18,
139 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6,
140 0xc6, 0xc6, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00,
141 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0x7e, 0x06, 0x06, 0x06, 0x0c,
142 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18,
143 0x18, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00, 0x00,
145 0x00, 0x00, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x60, 0x30, 0x18, 0x0c,
146 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00,
147 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x60,
148 0x30, 0x18, 0x0c, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x00, 0x00, 0x00,
149 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0x06, 0x0c, 0x18, 0x30, 0x30, 0x00, 0x30,
150 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xde, 0xde,
151 0xde, 0xde, 0xc0, 0xc0, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x6c,
152 0xc6, 0xc6, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xc6, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
154 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6, 0xc0, 0xc0, 0xc0, 0xc0,
155 0xc0, 0xc0, 0xc0, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xcc,
156 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xcc, 0xf8, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0xfe, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
158 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0,
159 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6,
160 0xc0, 0xc0, 0xc0, 0xde, 0xc6, 0xc6, 0xc6, 0xc6, 0x7e, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0xc6,
162 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30,
163 0x30, 0x30, 0x30, 0x30, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x0c,
164 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0xc6, 0xc6, 0xcc, 0xd8, 0xf0, 0xe0, 0xf0, 0xd8, 0xcc, 0xc6,
166 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
167 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6,
168 0xee, 0xfe, 0xd6, 0xd6, 0xd6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00,
169 0x00, 0x00, 0xc6, 0xc6, 0xe6, 0xe6, 0xf6, 0xde, 0xce, 0xce, 0xc6, 0xc6,
170 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
171 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xc6,
172 0xc6, 0xc6, 0xc6, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00,
173 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xf6, 0xda,
174 0x6c, 0x06, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0xfc,
175 0xd8, 0xcc, 0xcc, 0xc6, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6,
176 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x06, 0xc6, 0x7c, 0x00, 0x00, 0x00,
177 0x00, 0x00, 0xff, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
178 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
179 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6,
180 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x10, 0x00, 0x00, 0x00,
181 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xd6, 0xd6, 0xd6, 0xd6, 0xfe, 0x6c,
182 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x38,
183 0x38, 0x6c, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc,
184 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00,
185 0x00, 0x00, 0xfe, 0x06, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0xc0, 0xc0,
186 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x60, 0x60, 0x60, 0x60, 0x60,
187 0x60, 0x60, 0x60, 0x60, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0,
188 0x60, 0x60, 0x30, 0x38, 0x18, 0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
190 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x38, 0x6c, 0xc6, 0x00, 0x00,
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00,
193 0x00, 0x00, 0x18, 0x18, 0x18, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x06, 0x06,
195 0x7e, 0xc6, 0xc6, 0xc6, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0,
196 0xc0, 0xdc, 0xe6, 0xc6, 0xc6, 0xc6, 0xc6, 0xe6, 0xdc, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6, 0xc0, 0xc0, 0xc0, 0xc0, 0xc6,
198 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x06, 0x76, 0xce, 0xc6,
199 0xc6, 0xc6, 0xc6, 0xce, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 0x00, 0x7c, 0xc6, 0xc6, 0xfe, 0xc0, 0xc0, 0xc0, 0x7e, 0x00, 0x00, 0x00,
201 0x00, 0x00, 0x1c, 0x36, 0x30, 0x30, 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30,
202 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0xce, 0xc6,
203 0xc6, 0xc6, 0xce, 0x76, 0x06, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0xc0, 0xc0,
204 0xc0, 0xdc, 0xe6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00,
205 0x00, 0x00, 0x18, 0x00, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
206 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x1e, 0x06, 0x06,
207 0x06, 0x06, 0x06, 0x06, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0xc0, 0xc0,
208 0xc0, 0xc6, 0xcc, 0xd8, 0xf0, 0xf0, 0xd8, 0xcc, 0xc6, 0x00, 0x00, 0x00,
209 0x00, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
210 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xec, 0xfe, 0xd6,
211 0xd6, 0xd6, 0xd6, 0xc6, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 0x00, 0xdc, 0xe6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
214 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdc, 0xe6, 0xc6,
215 0xc6, 0xc6, 0xe6, 0xdc, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
216 0x00, 0x76, 0xce, 0xc6, 0xc6, 0xc6, 0xce, 0x76, 0x06, 0x06, 0x06, 0x00,
217 0x00, 0x00, 0x00, 0x00, 0x00, 0xdc, 0xe6, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
218 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6, 0xc0,
219 0x70, 0x1c, 0x06, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30,
220 0x30, 0xfe, 0x30, 0x30, 0x30, 0x30, 0x30, 0x36, 0x1c, 0x00, 0x00, 0x00,
221 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
222 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0xc6,
223 0xc6, 0xc6, 0x6c, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224 0x00, 0xc6, 0xc6, 0xd6, 0xd6, 0xd6, 0xd6, 0xfe, 0x6c, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0x6c, 0x38, 0x38, 0x6c, 0xc6,
226 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0xc6,
227 0xc6, 0xc6, 0xce, 0x76, 0x06, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0xfe, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0xfe, 0x00, 0x00, 0x00,
229 0x00, 0x00, 0x1c, 0x30, 0x30, 0x30, 0x30, 0xe0, 0x30, 0x30, 0x30, 0x30,
230 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00,
231 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x30,
232 0x30, 0x30, 0x30, 0x1c, 0x30, 0x30, 0x30, 0x30, 0xe0, 0x00, 0x00, 0x00,
233 0x00, 0x00, 0x00, 0x76, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x38, 0x38, 0x6c,
235 0x6c, 0xc6, 0xc6, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x66,
236 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x66, 0x3c, 0x18, 0xcc, 0x78, 0x00,
237 0x00, 0x00, 0x6c, 0x6c, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
238 0x7c, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x18, 0x30, 0x00, 0x7c, 0xc6, 0xc6,
239 0xfe, 0xc0, 0xc0, 0xc0, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x10, 0x38, 0x6c,
240 0x00, 0x7c, 0x06, 0x06, 0x7e, 0xc6, 0xc6, 0xc6, 0x7e, 0x00, 0x00, 0x00,
241 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x7c, 0x06, 0x06, 0x7e, 0xc6, 0xc6, 0xc6,
242 0x7e, 0x00, 0x00, 0x00, 0x00, 0x60, 0x30, 0x18, 0x00, 0x7c, 0x06, 0x06,
243 0x7e, 0xc6, 0xc6, 0xc6, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x38, 0x6c, 0x38,
244 0x00, 0x7c, 0x06, 0x06, 0x7e, 0xc6, 0xc6, 0xc6, 0x7e, 0x00, 0x00, 0x00,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xc6, 0xc0, 0xc0, 0xc0, 0xc0, 0xc6,
246 0x7c, 0x18, 0x0c, 0x38, 0x00, 0x10, 0x38, 0x6c, 0x00, 0x7c, 0xc6, 0xc6,
247 0xfe, 0xc0, 0xc0, 0xc0, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6c,
248 0x00, 0x7c, 0xc6, 0xc6, 0xfe, 0xc0, 0xc0, 0xc0, 0x7e, 0x00, 0x00, 0x00,
249 0x00, 0x60, 0x30, 0x18, 0x00, 0x7c, 0xc6, 0xc6, 0xfe, 0xc0, 0xc0, 0xc0,
250 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x38, 0x18, 0x18,
251 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x10, 0x38, 0x6c,
252 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00, 0x00, 0x00,
253 0x00, 0x60, 0x30, 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
254 0x3c, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0x10, 0x38, 0x6c, 0xc6, 0xc6, 0xc6,
255 0xfe, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00, 0x38, 0x6c, 0x38, 0x00,
256 0x38, 0x6c, 0xc6, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00,
257 0x18, 0x30, 0x60, 0x00, 0xfe, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0,
258 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xec, 0x36, 0x36,
259 0x76, 0xde, 0xd8, 0xd8, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x3c,
260 0x6c, 0xcc, 0xcc, 0xfe, 0xcc, 0xcc, 0xcc, 0xcc, 0xce, 0x00, 0x00, 0x00,
261 0x00, 0x10, 0x38, 0x6c, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
262 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x7c, 0xc6, 0xc6,
263 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x60, 0x30, 0x18,
264 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00,
265 0x00, 0x10, 0x38, 0x6c, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
266 0x7c, 0x00, 0x00, 0x00, 0x00, 0x60, 0x30, 0x18, 0x00, 0xc6, 0xc6, 0xc6,
267 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6c,
268 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xce, 0x76, 0x06, 0xc6, 0x7c, 0x00,
269 0x6c, 0x6c, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
270 0x7c, 0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
271 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
272 0x30, 0x78, 0xcc, 0xc0, 0xc0, 0xcc, 0x78, 0x30, 0x30, 0x00, 0x00, 0x00,
273 0x00, 0x00, 0x38, 0x6c, 0x60, 0x60, 0x60, 0xf8, 0x60, 0x60, 0x60, 0xe6,
274 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0xfc,
275 0x30, 0xfc, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xcc,
276 0xcc, 0xf8, 0xc4, 0xcc, 0xde, 0xcc, 0xcc, 0xcc, 0xc6, 0x00, 0x00, 0x00,
277 0x00, 0x00, 0x0e, 0x1b, 0x18, 0x18, 0x18, 0x7e, 0x18, 0x18, 0x18, 0x18,
278 0x18, 0xd8, 0x70, 0x00, 0x00, 0x0c, 0x18, 0x30, 0x00, 0x7c, 0x06, 0x06,
279 0x7e, 0xc6, 0xc6, 0xc6, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x18, 0x30,
280 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00, 0x00, 0x00,
281 0x00, 0x0c, 0x18, 0x30, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6,
282 0x7c, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x18, 0x30, 0x00, 0xc6, 0xc6, 0xc6,
283 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x76, 0xdc, 0x00,
284 0x00, 0xdc, 0xe6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00,
285 0x76, 0xdc, 0x00, 0xc6, 0xc6, 0xe6, 0xf6, 0xfe, 0xde, 0xce, 0xc6, 0xc6,
286 0xc6, 0x00, 0x00, 0x00, 0x00, 0x78, 0xd8, 0xd8, 0x6c, 0x00, 0xfc, 0x00,
287 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x6c, 0x6c,
288 0x38, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x30, 0x60, 0xc0, 0xc6, 0xc6,
290 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0xfe, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0xfe, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0xc0, 0xc2, 0xc6, 0xcc, 0xd8, 0x30, 0x60, 0xdc, 0x86, 0x0c,
294 0x18, 0x3e, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc2, 0xc6, 0xcc, 0xd8, 0x30,
295 0x66, 0xce, 0x9e, 0x3e, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30,
296 0x00, 0x30, 0x30, 0x30, 0x78, 0x78, 0x78, 0x78, 0x30, 0x00, 0x00, 0x00,
297 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x6c, 0xd8, 0x6c, 0x36, 0x00, 0x00,
298 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x6c, 0x36,
299 0x6c, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x88, 0x22, 0x88,
300 0x22, 0x88, 0x22, 0x88, 0x22, 0x88, 0x22, 0x88, 0x22, 0x88, 0x22, 0x88,
301 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa,
302 0x55, 0xaa, 0x55, 0xaa, 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77,
303 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77, 0x18, 0x18, 0x18, 0x18,
304 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
305 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8, 0xf8, 0x18, 0x18, 0x18,
306 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8, 0xf8, 0x18,
307 0x18, 0xf8, 0xf8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x36, 0x36, 0x36, 0x36,
308 0x36, 0x36, 0x36, 0xf6, 0xf6, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe, 0x36, 0x36, 0x36,
310 0x36, 0x36, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xf8, 0x18,
311 0x18, 0xf8, 0xf8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x36, 0x36, 0x36, 0x36,
312 0x36, 0xf6, 0xf6, 0x06, 0x06, 0xf6, 0xf6, 0x36, 0x36, 0x36, 0x36, 0x36,
313 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
314 0x36, 0x36, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe, 0x06,
315 0x06, 0xf6, 0xf6, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
316 0x36, 0xf6, 0xf6, 0x06, 0x06, 0xfe, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
317 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0xfe, 0xfe, 0x00, 0x00, 0x00,
318 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8, 0xf8, 0x18,
319 0x18, 0xf8, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0xf8, 0xf8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
321 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1f, 0x1f, 0x00, 0x00, 0x00,
322 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff,
323 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
324 0x00, 0x00, 0x00, 0xff, 0xff, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
325 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1f, 0x1f, 0x18, 0x18, 0x18,
326 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
327 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18,
328 0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
329 0x18, 0x18, 0x18, 0x18, 0x18, 0x1f, 0x1f, 0x18, 0x18, 0x1f, 0x1f, 0x18,
330 0x18, 0x18, 0x18, 0x18, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x37,
331 0x37, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
332 0x36, 0x37, 0x37, 0x30, 0x30, 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
333 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f, 0x30, 0x30, 0x37, 0x37, 0x36,
334 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0xf7, 0xf7, 0x00,
335 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
336 0x00, 0xff, 0xff, 0x00, 0x00, 0xf7, 0xf7, 0x36, 0x36, 0x36, 0x36, 0x36,
337 0x36, 0x36, 0x36, 0x36, 0x36, 0x37, 0x37, 0x30, 0x30, 0x37, 0x37, 0x36,
338 0x36, 0x36, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00,
339 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x36, 0x36, 0x36,
340 0x36, 0xf7, 0xf7, 0x00, 0x00, 0xf7, 0xf7, 0x36, 0x36, 0x36, 0x36, 0x36,
341 0x18, 0x18, 0x18, 0x18, 0x18, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00,
342 0x00, 0x00, 0x00, 0x00, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0xff,
343 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x18, 0x18, 0x18, 0x18, 0x18,
345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x36, 0x36, 0x36,
346 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x3f,
347 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18,
348 0x18, 0x1f, 0x1f, 0x18, 0x18, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00,
349 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x18, 0x18, 0x1f, 0x1f, 0x18,
350 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f,
351 0x3f, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
352 0x36, 0x36, 0x36, 0xff, 0xff, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
353 0x18, 0x18, 0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0xff, 0xff, 0x18,
354 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8,
355 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
356 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
357 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
358 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0,
360 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
361 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
362 0x0f, 0x0f, 0x0f, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
364 0x00, 0x76, 0xd6, 0xdc, 0xc8, 0xc8, 0xdc, 0xd6, 0x76, 0x00, 0x00, 0x00,
365 0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0xd8, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
366 0xd8, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0xfe, 0xc6, 0xc6, 0xc0, 0xc0, 0xc0,
367 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
368 0x00, 0x7e, 0xfe, 0x24, 0x24, 0x24, 0x24, 0x66, 0xc6, 0x00, 0x00, 0x00,
369 0x00, 0x00, 0xfe, 0xfe, 0xc2, 0x60, 0x30, 0x18, 0x30, 0x60, 0xc2, 0xfe,
370 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0xc8, 0xcc,
371 0xcc, 0xcc, 0xcc, 0xcc, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
372 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x76, 0x6c, 0x60, 0xc0, 0x00,
373 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0xfc, 0x98, 0x18, 0x18, 0x18, 0x18,
374 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x30, 0x30, 0x78, 0xcc, 0xcc,
375 0xcc, 0x78, 0x30, 0x30, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x6c,
376 0xc6, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00, 0x00, 0x00,
377 0x00, 0x00, 0x38, 0x6c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x6c, 0x6c, 0x6c,
378 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xcc, 0x60, 0x30, 0x78, 0xcc,
379 0xcc, 0xcc, 0xcc, 0xcc, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
380 0x00, 0x76, 0xbb, 0x99, 0x99, 0xdd, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00,
381 0x00, 0x00, 0x02, 0x06, 0x3c, 0x6c, 0xce, 0xd6, 0xd6, 0xe6, 0x6c, 0x78,
382 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x30, 0x60, 0xc0, 0xc0, 0xfe,
383 0xc0, 0xc0, 0x60, 0x30, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x6c,
384 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00, 0x00,
385 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0xfe, 0x00, 0x00, 0xfe, 0x00,
386 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0xfc,
387 0x30, 0x30, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
388 0x60, 0x30, 0x18, 0x0c, 0x18, 0x30, 0x60, 0x00, 0xfc, 0x00, 0x00, 0x00,
389 0x00, 0x00, 0x00, 0x00, 0x18, 0x30, 0x60, 0xc0, 0x60, 0x30, 0x18, 0x00,
390 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x36, 0x36, 0x30, 0x30, 0x30,
391 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x18, 0x18, 0x18, 0x18,
392 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xd8, 0xd8, 0x70, 0x00, 0x00, 0x00,
393 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0xfc, 0x00, 0x30, 0x30, 0x00,
394 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0xdc, 0x00,
395 0x76, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xcc, 0xcc,
396 0xcc, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00,
398 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
399 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0c, 0x0c,
400 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0xcc, 0x6c, 0x3c, 0x1c, 0x0c, 0x00, 0x00,
401 0x00, 0xd8, 0xec, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x00, 0x00, 0x00, 0x00,
402 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x6c, 0x0c, 0x18, 0x30, 0x60, 0x7c,
403 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
404 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00,
405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406 0x00, 0x00, 0x00, 0x00
407};
408
409#ifdef FLANTERM_FB_SUPPORT_BPP
410static inline __attribute__((always_inline)) uint32_t convert_colour(struct flanterm_context *_ctx, uint32_t colour) {
411 struct flanterm_fb_context *ctx = (void *)_ctx;
412 uint32_t r = (colour >> 16) & 0xff;
413 uint32_t g = (colour >> 8) & 0xff;
414 uint32_t b = colour & 0xff;
415 return (r << ctx->red_mask_shift) | (g << ctx->green_mask_shift) | (b << ctx->blue_mask_shift);
416}
417#else
418#define convert_colour(CTX, COLOUR) (COLOUR)
419#endif
420
421static void flanterm_fb_save_state(struct flanterm_context *_ctx) {
422 struct flanterm_fb_context *ctx = (void *)_ctx;
423 ctx->saved_state_text_fg = ctx->text_fg;
424 ctx->saved_state_text_bg = ctx->text_bg;
425 ctx->saved_state_cursor_x = ctx->cursor_x;
426 ctx->saved_state_cursor_y = ctx->cursor_y;
427}
428
429static void flanterm_fb_restore_state(struct flanterm_context *_ctx) {
430 struct flanterm_fb_context *ctx = (void *)_ctx;
431 ctx->text_fg = ctx->saved_state_text_fg;
432 ctx->text_bg = ctx->saved_state_text_bg;
433 ctx->cursor_x = ctx->saved_state_cursor_x;
434 ctx->cursor_y = ctx->saved_state_cursor_y;
435}
436
437static void flanterm_fb_swap_palette(struct flanterm_context *_ctx) {
438 struct flanterm_fb_context *ctx = (void *)_ctx;
439 uint32_t tmp = ctx->text_bg;
440 ctx->text_bg = ctx->text_fg;
441 ctx->text_fg = tmp;
442}
443
444static void plot_char(struct flanterm_context *_ctx, struct flanterm_fb_char *c, size_t x, size_t y) {
445 struct flanterm_fb_context *ctx = (void *)_ctx;
446
447 if (x >= _ctx->cols || y >= _ctx->rows) {
448 return;
449 }
450
451#ifdef FLANTERM_FB_DISABLE_CANVAS
452 uint32_t default_bg = ctx->default_bg;
453#endif
454
455 x = ctx->offset_x + x * ctx->glyph_width;
456 y = ctx->offset_y + y * ctx->glyph_height;
457
458 bool *glyph = &ctx->font_bool[c->c * ctx->font_height * ctx->font_width];
459 // naming: fx,fy for font coordinates, gx,gy for glyph coordinates
460 for (size_t gy = 0; gy < ctx->glyph_height; gy++) {
461 uint8_t fy = gy / ctx->font_scale_y;
462 volatile uint32_t *fb_line = ctx->framebuffer + x + (y + gy) * (ctx->pitch / 4);
463
464#ifndef FLANTERM_FB_DISABLE_CANVAS
465 uint32_t *canvas_line = ctx->canvas + x + (y + gy) * ctx->width;
466#endif
467
468 for (size_t fx = 0; fx < ctx->font_width; fx++) {
469 bool draw = glyph[fy * ctx->font_width + fx];
470 for (size_t i = 0; i < ctx->font_scale_x; i++) {
471 size_t gx = ctx->font_scale_x * fx + i;
472#ifndef FLANTERM_FB_DISABLE_CANVAS
473 uint32_t bg = c->bg == 0xffffffff ? canvas_line[gx] : c->bg;
474 uint32_t fg = c->fg == 0xffffffff ? canvas_line[gx] : c->fg;
475#else
476 uint32_t bg = c->bg == 0xffffffff ? default_bg : c->bg;
477 uint32_t fg = c->fg == 0xffffffff ? default_bg : c->fg;
478#endif
479 fb_line[gx] = draw ? fg : bg;
480 }
481 }
482 }
483}
484
485#ifdef FLANTERM_FB_ENABLE_MASKING
486static void plot_char_masked(struct flanterm_context *_ctx, struct flanterm_fb_char *old, struct flanterm_fb_char *c, size_t x, size_t y) {
487 struct flanterm_fb_context *ctx = (void *)_ctx;
488
489 if (x >= _ctx->cols || y >= _ctx->rows) {
490 return;
491 }
492
493 x = ctx->offset_x + x * ctx->glyph_width;
494 y = ctx->offset_y + y * ctx->glyph_height;
495
496#ifdef FLANTERM_FB_DISABLE_CANVAS
497 uint32_t default_bg = ctx->default_bg;
498#endif
499
500 bool *new_glyph = &ctx->font_bool[c->c * ctx->font_height * ctx->font_width];
501 bool *old_glyph = &ctx->font_bool[old->c * ctx->font_height * ctx->font_width];
502 for (size_t gy = 0; gy < ctx->glyph_height; gy++) {
503 uint8_t fy = gy / ctx->font_scale_y;
504 volatile uint32_t *fb_line = ctx->framebuffer + x + (y + gy) * (ctx->pitch / 4);
505#ifndef FLANTERM_FB_DISABLE_CANVAS
506 uint32_t *canvas_line = ctx->canvas + x + (y + gy) * ctx->width;
507#endif
508 for (size_t fx = 0; fx < ctx->font_width; fx++) {
509 bool old_draw = old_glyph[fy * ctx->font_width + fx];
510 bool new_draw = new_glyph[fy * ctx->font_width + fx];
511 if (old_draw == new_draw)
512 continue;
513 for (size_t i = 0; i < ctx->font_scale_x; i++) {
514 size_t gx = ctx->font_scale_x * fx + i;
515#ifndef FLANTERM_FB_DISABLE_CANVAS
516 uint32_t bg = c->bg == 0xffffffff ? canvas_line[gx] : c->bg;
517 uint32_t fg = c->fg == 0xffffffff ? canvas_line[gx] : c->fg;
518#else
519 uint32_t bg = c->bg == 0xffffffff ? default_bg : c->bg;
520 uint32_t fg = c->fg == 0xffffffff ? default_bg : c->fg;
521#endif
522 fb_line[gx] = new_draw ? fg : bg;
523 }
524 }
525 }
526}
527#endif
528
529static inline bool compare_char(struct flanterm_fb_char *a, struct flanterm_fb_char *b) {
530 return !(a->c != b->c || a->bg != b->bg || a->fg != b->fg);
531}
532
533static void push_to_queue(struct flanterm_context *_ctx, struct flanterm_fb_char *c, size_t x, size_t y) {
534 struct flanterm_fb_context *ctx = (void *)_ctx;
535
536 if (x >= _ctx->cols || y >= _ctx->rows) {
537 return;
538 }
539
540 size_t i = y * _ctx->cols + x;
541
542 struct flanterm_fb_queue_item *q = ctx->map[i];
543
544 if (q == NULL) {
545 if (compare_char(&ctx->grid[i], c)) {
546 return;
547 }
548 q = &ctx->queue[ctx->queue_i++];
549 q->x = x;
550 q->y = y;
551 ctx->map[i] = q;
552 }
553
554 q->c = *c;
555}
556
557static void flanterm_fb_revscroll(struct flanterm_context *_ctx) {
558 struct flanterm_fb_context *ctx = (void *)_ctx;
559
560 for (size_t i = (_ctx->scroll_bottom_margin - 1) * _ctx->cols - 1;
561 i >= _ctx->scroll_top_margin * _ctx->cols; i--) {
562 if (i == (size_t)-1) {
563 break;
564 }
565 struct flanterm_fb_char *c;
566 struct flanterm_fb_queue_item *q = ctx->map[i];
567 if (q != NULL) {
568 c = &q->c;
569 } else {
570 c = &ctx->grid[i];
571 }
572 push_to_queue(_ctx, c, (i + _ctx->cols) % _ctx->cols, (i + _ctx->cols) / _ctx->cols);
573 }
574
575 // Clear the first line of the screen.
576 struct flanterm_fb_char empty;
577 empty.c = ' ';
578 empty.fg = ctx->text_fg;
579 empty.bg = ctx->text_bg;
580 for (size_t i = 0; i < _ctx->cols; i++) {
581 push_to_queue(_ctx, &empty, i, _ctx->scroll_top_margin);
582 }
583}
584
585static void flanterm_fb_scroll(struct flanterm_context *_ctx) {
586 struct flanterm_fb_context *ctx = (void *)_ctx;
587
588 for (size_t i = (_ctx->scroll_top_margin + 1) * _ctx->cols;
589 i < _ctx->scroll_bottom_margin * _ctx->cols; i++) {
590 struct flanterm_fb_char *c;
591 struct flanterm_fb_queue_item *q = ctx->map[i];
592 if (q != NULL) {
593 c = &q->c;
594 } else {
595 c = &ctx->grid[i];
596 }
597 push_to_queue(_ctx, c, (i - _ctx->cols) % _ctx->cols, (i - _ctx->cols) / _ctx->cols);
598 }
599
600 // Clear the last line of the screen.
601 struct flanterm_fb_char empty;
602 empty.c = ' ';
603 empty.fg = ctx->text_fg;
604 empty.bg = ctx->text_bg;
605 for (size_t i = 0; i < _ctx->cols; i++) {
606 push_to_queue(_ctx, &empty, i, _ctx->scroll_bottom_margin - 1);
607 }
608}
609
610static void flanterm_fb_clear(struct flanterm_context *_ctx, bool move) {
611 struct flanterm_fb_context *ctx = (void *)_ctx;
612
613 struct flanterm_fb_char empty;
614 empty.c = ' ';
615 empty.fg = ctx->text_fg;
616 empty.bg = ctx->text_bg;
617 for (size_t i = 0; i < _ctx->rows * _ctx->cols; i++) {
618 push_to_queue(_ctx, &empty, i % _ctx->cols, i / _ctx->cols);
619 }
620
621 if (move) {
622 ctx->cursor_x = 0;
623 ctx->cursor_y = 0;
624 }
625}
626
627static void flanterm_fb_set_cursor_pos(struct flanterm_context *_ctx, size_t x, size_t y) {
628 struct flanterm_fb_context *ctx = (void *)_ctx;
629
630 if (x >= _ctx->cols) {
631 if ((int)x < 0) {
632 x = 0;
633 } else {
634 x = _ctx->cols - 1;
635 }
636 }
637 if (y >= _ctx->rows) {
638 if ((int)y < 0) {
639 y = 0;
640 } else {
641 y = _ctx->rows - 1;
642 }
643 }
644 ctx->cursor_x = x;
645 ctx->cursor_y = y;
646}
647
648static void flanterm_fb_get_cursor_pos(struct flanterm_context *_ctx, size_t *x, size_t *y) {
649 struct flanterm_fb_context *ctx = (void *)_ctx;
650
651 *x = ctx->cursor_x >= _ctx->cols ? _ctx->cols - 1 : ctx->cursor_x;
652 *y = ctx->cursor_y >= _ctx->rows ? _ctx->rows - 1 : ctx->cursor_y;
653}
654
655static void flanterm_fb_move_character(struct flanterm_context *_ctx, size_t new_x, size_t new_y, size_t old_x, size_t old_y) {
656 struct flanterm_fb_context *ctx = (void *)_ctx;
657
658 if (old_x >= _ctx->cols || old_y >= _ctx->rows
659 || new_x >= _ctx->cols || new_y >= _ctx->rows) {
660 return;
661 }
662
663 size_t i = old_x + old_y * _ctx->cols;
664
665 struct flanterm_fb_char *c;
666 struct flanterm_fb_queue_item *q = ctx->map[i];
667 if (q != NULL) {
668 c = &q->c;
669 } else {
670 c = &ctx->grid[i];
671 }
672
673 push_to_queue(_ctx, c, new_x, new_y);
674}
675
676static void flanterm_fb_set_text_fg(struct flanterm_context *_ctx, size_t fg) {
677 struct flanterm_fb_context *ctx = (void *)_ctx;
678
679 ctx->text_fg = ctx->ansi_colours[fg];
680}
681
682static void flanterm_fb_set_text_bg(struct flanterm_context *_ctx, size_t bg) {
683 struct flanterm_fb_context *ctx = (void *)_ctx;
684
685 ctx->text_bg = ctx->ansi_colours[bg];
686}
687
688static void flanterm_fb_set_text_fg_bright(struct flanterm_context *_ctx, size_t fg) {
689 struct flanterm_fb_context *ctx = (void *)_ctx;
690
691 ctx->text_fg = ctx->ansi_bright_colours[fg];
692}
693
694static void flanterm_fb_set_text_bg_bright(struct flanterm_context *_ctx, size_t bg) {
695 struct flanterm_fb_context *ctx = (void *)_ctx;
696
697 ctx->text_bg = ctx->ansi_bright_colours[bg];
698}
699
700static void flanterm_fb_set_text_fg_rgb(struct flanterm_context *_ctx, uint32_t fg) {
701 struct flanterm_fb_context *ctx = (void *)_ctx;
702
703 ctx->text_fg = convert_colour(_ctx, fg);
704}
705
706static void flanterm_fb_set_text_bg_rgb(struct flanterm_context *_ctx, uint32_t bg) {
707 struct flanterm_fb_context *ctx = (void *)_ctx;
708
709 ctx->text_bg = convert_colour(_ctx, bg);
710}
711
712static void flanterm_fb_set_text_fg_default(struct flanterm_context *_ctx) {
713 struct flanterm_fb_context *ctx = (void *)_ctx;
714
715 ctx->text_fg = ctx->default_fg;
716}
717
718static void flanterm_fb_set_text_bg_default(struct flanterm_context *_ctx) {
719 struct flanterm_fb_context *ctx = (void *)_ctx;
720
721 ctx->text_bg = 0xffffffff;
722}
723
724static void flanterm_fb_set_text_fg_default_bright(struct flanterm_context *_ctx) {
725 struct flanterm_fb_context *ctx = (void *)_ctx;
726
727 ctx->text_fg = ctx->default_fg_bright;
728}
729
730static void flanterm_fb_set_text_bg_default_bright(struct flanterm_context *_ctx) {
731 struct flanterm_fb_context *ctx = (void *)_ctx;
732
733 ctx->text_bg = ctx->default_bg_bright;
734}
735
736static void draw_cursor(struct flanterm_context *_ctx) {
737 struct flanterm_fb_context *ctx = (void *)_ctx;
738
739 if (ctx->cursor_x >= _ctx->cols || ctx->cursor_y >= _ctx->rows) {
740 return;
741 }
742
743 size_t i = ctx->cursor_x + ctx->cursor_y * _ctx->cols;
744
745 struct flanterm_fb_char c;
746 struct flanterm_fb_queue_item *q = ctx->map[i];
747 if (q != NULL) {
748 c = q->c;
749 } else {
750 c = ctx->grid[i];
751 }
752 uint32_t tmp = c.fg;
753 c.fg = c.bg;
754 c.bg = tmp;
755 plot_char(_ctx, &c, ctx->cursor_x, ctx->cursor_y);
756 if (q != NULL) {
757 ctx->grid[i] = q->c;
758 ctx->map[i] = NULL;
759 }
760}
761
762static void flanterm_fb_double_buffer_flush(struct flanterm_context *_ctx) {
763 struct flanterm_fb_context *ctx = (void *)_ctx;
764
765 if (_ctx->cursor_enabled) {
766 draw_cursor(_ctx);
767 }
768
769 for (size_t i = 0; i < ctx->queue_i; i++) {
770 struct flanterm_fb_queue_item *q = &ctx->queue[i];
771 size_t offset = q->y * _ctx->cols + q->x;
772 if (ctx->map[offset] == NULL) {
773 continue;
774 }
775 #ifdef FLANTERM_FB_ENABLE_MASKING
776 struct flanterm_fb_char *old = &ctx->grid[offset];
777 if (q->c.bg == old->bg && q->c.fg == old->fg) {
778 plot_char_masked(_ctx, old, &q->c, q->x, q->y);
779 } else {
780 plot_char(_ctx, &q->c, q->x, q->y);
781 }
782 #else
783 plot_char(_ctx, &q->c, q->x, q->y);
784 #endif
785 ctx->grid[offset] = q->c;
786 ctx->map[offset] = NULL;
787 }
788
789 if ((ctx->old_cursor_x != ctx->cursor_x || ctx->old_cursor_y != ctx->cursor_y) || _ctx->cursor_enabled == false) {
790 if (ctx->old_cursor_x < _ctx->cols && ctx->old_cursor_y < _ctx->rows) {
791 plot_char(_ctx, &ctx->grid[ctx->old_cursor_x + ctx->old_cursor_y * _ctx->cols], ctx->old_cursor_x, ctx->old_cursor_y);
792 }
793 }
794
795 ctx->old_cursor_x = ctx->cursor_x;
796 ctx->old_cursor_y = ctx->cursor_y;
797
798 ctx->queue_i = 0;
799}
800
801static void flanterm_fb_raw_putchar(struct flanterm_context *_ctx, uint8_t c) {
802 struct flanterm_fb_context *ctx = (void *)_ctx;
803
804 if (ctx->cursor_x >= _ctx->cols && (ctx->cursor_y < _ctx->scroll_bottom_margin - 1 || _ctx->scroll_enabled)) {
805 ctx->cursor_x = 0;
806 ctx->cursor_y++;
807 if (ctx->cursor_y == _ctx->scroll_bottom_margin) {
808 ctx->cursor_y--;
809 flanterm_fb_scroll(_ctx);
810 }
811 if (ctx->cursor_y >= _ctx->cols) {
812 ctx->cursor_y = _ctx->cols - 1;
813 }
814 }
815
816 struct flanterm_fb_char ch;
817 ch.c = c;
818 ch.fg = ctx->text_fg;
819 ch.bg = ctx->text_bg;
820 push_to_queue(_ctx, &ch, ctx->cursor_x++, ctx->cursor_y);
821}
822
823static void flanterm_fb_full_refresh(struct flanterm_context *_ctx) {
824 struct flanterm_fb_context *ctx = (void *)_ctx;
825
826#ifdef FLANTERM_FB_DISABLE_CANVAS
827 uint32_t default_bg = ctx->default_bg;
828#endif
829
830 for (size_t y = 0; y < ctx->height; y++) {
831 for (size_t x = 0; x < ctx->width; x++) {
832#ifndef FLANTERM_FB_DISABLE_CANVAS
833 ctx->framebuffer[y * (ctx->pitch / sizeof(uint32_t)) + x] = ctx->canvas[y * ctx->width + x];
834#else
835 ctx->framebuffer[y * (ctx->pitch / sizeof(uint32_t)) + x] = default_bg;
836#endif
837 }
838 }
839
840 for (size_t i = 0; i < (size_t)_ctx->rows * _ctx->cols; i++) {
841 size_t x = i % _ctx->cols;
842 size_t y = i / _ctx->cols;
843
844 plot_char(_ctx, &ctx->grid[i], x, y);
845 }
846
847 if (_ctx->cursor_enabled) {
848 draw_cursor(_ctx);
849 }
850}
851
852static void flanterm_fb_deinit(struct flanterm_context *_ctx, void (*_free)(void *, size_t)) {
853 struct flanterm_fb_context *ctx = (void *)_ctx;
854
855 if (_free == NULL) {
856#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
857 if (bump_allocated_instance == true) {
858 bump_alloc_ptr = 0;
859 bump_allocated_instance = false;
860 }
861#endif
862 return;
863 }
864
865 _free(ctx->font_bits, ctx->font_bits_size);
866 _free(ctx->font_bool, ctx->font_bool_size);
867 _free(ctx->grid, ctx->grid_size);
868 _free(ctx->queue, ctx->queue_size);
869 _free(ctx->map, ctx->map_size);
870
871#ifndef FLANTERM_FB_DISABLE_CANVAS
872 _free(ctx->canvas, ctx->canvas_size);
873#endif
874
875 _free(ctx, sizeof(struct flanterm_fb_context));
876}
877
878struct flanterm_context *flanterm_fb_init(
879 void *(*_malloc)(size_t),
880 void (*_free)(void *, size_t),
881 uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
882#ifdef FLANTERM_FB_SUPPORT_BPP
883 uint8_t red_mask_size, uint8_t red_mask_shift,
884 uint8_t green_mask_size, uint8_t green_mask_shift,
885 uint8_t blue_mask_size, uint8_t blue_mask_shift,
886#endif
887#ifndef FLANTERM_FB_DISABLE_CANVAS
888 uint32_t *canvas,
889#endif
890 uint32_t *ansi_colours, uint32_t *ansi_bright_colours,
891 uint32_t *default_bg, uint32_t *default_fg,
892 uint32_t *default_bg_bright, uint32_t *default_fg_bright,
893 void *font, size_t font_width, size_t font_height, size_t font_spacing,
894 size_t font_scale_x, size_t font_scale_y,
895 size_t margin
896) {
897#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
898 // Limit terminal size if needed
899 if (width > FLANTERM_FB_WIDTH_LIMIT || height > FLANTERM_FB_HEIGHT_LIMIT) {
900 size_t width_limit = width > FLANTERM_FB_WIDTH_LIMIT ? FLANTERM_FB_WIDTH_LIMIT : width;
901 size_t height_limit = height > FLANTERM_FB_HEIGHT_LIMIT ? FLANTERM_FB_HEIGHT_LIMIT : height;
902
903 framebuffer = (uint32_t *)((uintptr_t)framebuffer + ((((height / 2) - (height_limit / 2)) * pitch) + (((width / 2) - (width_limit / 2)) * 4)));
904
905 width = width_limit;
906 height = height_limit;
907 }
908#endif
909
910#ifdef FLANTERM_FB_SUPPORT_BPP
911 if (red_mask_size < 8 || red_mask_size != green_mask_size || red_mask_size != blue_mask_size) {
912 return NULL;
913 }
914#endif
915
916 if (_malloc == NULL) {
917#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
918 if (bump_allocated_instance == true) {
919 return NULL;
920 }
921 _malloc = bump_alloc;
922#else
923 return NULL;
924#endif
925 }
926
927 struct flanterm_fb_context *ctx = NULL;
928 ctx = _malloc(sizeof(struct flanterm_fb_context));
929 if (ctx == NULL) {
930 goto fail;
931 }
932
933 struct flanterm_context *_ctx = (void *)ctx;
934 memset(ctx, 0, sizeof(struct flanterm_fb_context));
935
936#ifdef FLANTERM_FB_SUPPORT_BPP
937 ctx->red_mask_size = red_mask_size;
938 ctx->red_mask_shift = red_mask_shift + (red_mask_size - 8);
939 ctx->green_mask_size = green_mask_size;
940 ctx->green_mask_shift = green_mask_shift + (green_mask_size - 8);
941 ctx->blue_mask_size = blue_mask_size;
942 ctx->blue_mask_shift = blue_mask_shift + (blue_mask_size - 8);
943#endif
944
945 if (ansi_colours != NULL) {
946 for (size_t i = 0; i < 8; i++) {
947 ctx->ansi_colours[i] = convert_colour(_ctx, ansi_colours[i]);
948 }
949 } else {
950 ctx->ansi_colours[0] = convert_colour(_ctx, 0x00000000); // black
951 ctx->ansi_colours[1] = convert_colour(_ctx, 0x00aa0000); // red
952 ctx->ansi_colours[2] = convert_colour(_ctx, 0x0000aa00); // green
953 ctx->ansi_colours[3] = convert_colour(_ctx, 0x00aa5500); // brown
954 ctx->ansi_colours[4] = convert_colour(_ctx, 0x000000aa); // blue
955 ctx->ansi_colours[5] = convert_colour(_ctx, 0x00aa00aa); // magenta
956 ctx->ansi_colours[6] = convert_colour(_ctx, 0x0000aaaa); // cyan
957 ctx->ansi_colours[7] = convert_colour(_ctx, 0x00aaaaaa); // grey
958 }
959
960 if (ansi_bright_colours != NULL) {
961 for (size_t i = 0; i < 8; i++) {
962 ctx->ansi_bright_colours[i] = convert_colour(_ctx, ansi_bright_colours[i]);
963 }
964 } else {
965 ctx->ansi_bright_colours[0] = convert_colour(_ctx, 0x00555555); // black
966 ctx->ansi_bright_colours[1] = convert_colour(_ctx, 0x00ff5555); // red
967 ctx->ansi_bright_colours[2] = convert_colour(_ctx, 0x0055ff55); // green
968 ctx->ansi_bright_colours[3] = convert_colour(_ctx, 0x00ffff55); // brown
969 ctx->ansi_bright_colours[4] = convert_colour(_ctx, 0x005555ff); // blue
970 ctx->ansi_bright_colours[5] = convert_colour(_ctx, 0x00ff55ff); // magenta
971 ctx->ansi_bright_colours[6] = convert_colour(_ctx, 0x0055ffff); // cyan
972 ctx->ansi_bright_colours[7] = convert_colour(_ctx, 0x00ffffff); // grey
973 }
974
975 if (default_bg != NULL) {
976 ctx->default_bg = convert_colour(_ctx, *default_bg);
977 } else {
978 ctx->default_bg = 0x00000000; // background (black)
979 }
980
981 if (default_fg != NULL) {
982 ctx->default_fg = convert_colour(_ctx, *default_fg);
983 } else {
984 ctx->default_fg = convert_colour(_ctx, 0x00aaaaaa); // foreground (grey)
985 }
986
987 if (default_bg_bright != NULL) {
988 ctx->default_bg_bright = convert_colour(_ctx, *default_bg_bright);
989 } else {
990 ctx->default_bg_bright = convert_colour(_ctx, 0x00555555); // background (black)
991 }
992
993 if (default_fg_bright != NULL) {
994 ctx->default_fg_bright = convert_colour(_ctx, *default_fg_bright);
995 } else {
996 ctx->default_fg_bright = convert_colour(_ctx, 0x00ffffff); // foreground (grey)
997 }
998
999 ctx->text_fg = ctx->default_fg;
1000 ctx->text_bg = 0xffffffff;
1001
1002 ctx->framebuffer = (void *)framebuffer;
1003 ctx->width = width;
1004 ctx->height = height;
1005 ctx->pitch = pitch;
1006
1007#define FONT_BYTES ((font_width * font_height * FLANTERM_FB_FONT_GLYPHS) / 8)
1008
1009 if (font != NULL) {
1010 ctx->font_width = font_width;
1011 ctx->font_height = font_height;
1012 ctx->font_bits_size = FONT_BYTES;
1013 ctx->font_bits = _malloc(ctx->font_bits_size);
1014 if (ctx->font_bits == NULL) {
1015 goto fail;
1016 }
1017 memcpy(ctx->font_bits, font, ctx->font_bits_size);
1018 } else {
1019 ctx->font_width = font_width = 8;
1020 ctx->font_height = font_height = 16;
1021 ctx->font_bits_size = FONT_BYTES;
1022 font_spacing = 1;
1023 ctx->font_bits = _malloc(ctx->font_bits_size);
1024 if (ctx->font_bits == NULL) {
1025 goto fail;
1026 }
1027 memcpy(ctx->font_bits, builtin_font, ctx->font_bits_size);
1028 }
1029
1030#undef FONT_BYTES
1031
1032 ctx->font_width += font_spacing;
1033
1034 ctx->font_bool_size = FLANTERM_FB_FONT_GLYPHS * font_height * ctx->font_width * sizeof(bool);
1035 ctx->font_bool = _malloc(ctx->font_bool_size);
1036 if (ctx->font_bool == NULL) {
1037 goto fail;
1038 }
1039
1040 for (size_t i = 0; i < FLANTERM_FB_FONT_GLYPHS; i++) {
1041 uint8_t *glyph = &ctx->font_bits[i * font_height];
1042
1043 for (size_t y = 0; y < font_height; y++) {
1044 // NOTE: the characters in VGA fonts are always one byte wide.
1045 // 9 dot wide fonts have 8 dots and one empty column, except
1046 // characters 0xC0-0xDF replicate column 9.
1047 for (size_t x = 0; x < 8; x++) {
1048 size_t offset = i * font_height * ctx->font_width + y * ctx->font_width + x;
1049
1050 if ((glyph[y] & (0x80 >> x))) {
1051 ctx->font_bool[offset] = true;
1052 } else {
1053 ctx->font_bool[offset] = false;
1054 }
1055 }
1056 // fill columns above 8 like VGA Line Graphics Mode does
1057 for (size_t x = 8; x < ctx->font_width; x++) {
1058 size_t offset = i * font_height * ctx->font_width + y * ctx->font_width + x;
1059
1060 if (i >= 0xc0 && i <= 0xdf) {
1061 ctx->font_bool[offset] = (glyph[y] & 1);
1062 } else {
1063 ctx->font_bool[offset] = false;
1064 }
1065 }
1066 }
1067 }
1068
1069 ctx->font_scale_x = font_scale_x;
1070 ctx->font_scale_y = font_scale_y;
1071
1072 ctx->glyph_width = ctx->font_width * font_scale_x;
1073 ctx->glyph_height = font_height * font_scale_y;
1074
1075 _ctx->cols = (ctx->width - margin * 2) / ctx->glyph_width;
1076 _ctx->rows = (ctx->height - margin * 2) / ctx->glyph_height;
1077
1078 ctx->offset_x = margin + ((ctx->width - margin * 2) % ctx->glyph_width) / 2;
1079 ctx->offset_y = margin + ((ctx->height - margin * 2) % ctx->glyph_height) / 2;
1080
1081 ctx->grid_size = _ctx->rows * _ctx->cols * sizeof(struct flanterm_fb_char);
1082 ctx->grid = _malloc(ctx->grid_size);
1083 if (ctx->grid == NULL) {
1084 goto fail;
1085 }
1086 for (size_t i = 0; i < _ctx->rows * _ctx->cols; i++) {
1087 ctx->grid[i].c = ' ';
1088 ctx->grid[i].fg = ctx->text_fg;
1089 ctx->grid[i].bg = ctx->text_bg;
1090 }
1091
1092 ctx->queue_size = _ctx->rows * _ctx->cols * sizeof(struct flanterm_fb_queue_item);
1093 ctx->queue = _malloc(ctx->queue_size);
1094 if (ctx->queue == NULL) {
1095 goto fail;
1096 }
1097 ctx->queue_i = 0;
1098 memset(ctx->queue, 0, ctx->queue_size);
1099
1100 ctx->map_size = _ctx->rows * _ctx->cols * sizeof(struct flanterm_fb_queue_item *);
1101 ctx->map = _malloc(ctx->map_size);
1102 if (ctx->map == NULL) {
1103 goto fail;
1104 }
1105 memset(ctx->map, 0, ctx->map_size);
1106
1107#ifndef FLANTERM_FB_DISABLE_CANVAS
1108 ctx->canvas_size = ctx->width * ctx->height * sizeof(uint32_t);
1109 ctx->canvas = _malloc(ctx->canvas_size);
1110 if (ctx->canvas == NULL) {
1111 goto fail;
1112 }
1113 if (canvas != NULL) {
1114 for (size_t i = 0; i < ctx->width * ctx->height; i++) {
1115 ctx->canvas[i] = convert_colour(_ctx, canvas[i]);
1116 }
1117 } else {
1118 for (size_t i = 0; i < ctx->width * ctx->height; i++) {
1119 ctx->canvas[i] = ctx->default_bg;
1120 }
1121 }
1122#endif
1123
1124 _ctx->raw_putchar = flanterm_fb_raw_putchar;
1125 _ctx->clear = flanterm_fb_clear;
1126 _ctx->set_cursor_pos = flanterm_fb_set_cursor_pos;
1127 _ctx->get_cursor_pos = flanterm_fb_get_cursor_pos;
1128 _ctx->set_text_fg = flanterm_fb_set_text_fg;
1129 _ctx->set_text_bg = flanterm_fb_set_text_bg;
1130 _ctx->set_text_fg_bright = flanterm_fb_set_text_fg_bright;
1131 _ctx->set_text_bg_bright = flanterm_fb_set_text_bg_bright;
1132 _ctx->set_text_fg_rgb = flanterm_fb_set_text_fg_rgb;
1133 _ctx->set_text_bg_rgb = flanterm_fb_set_text_bg_rgb;
1134 _ctx->set_text_fg_default = flanterm_fb_set_text_fg_default;
1135 _ctx->set_text_bg_default = flanterm_fb_set_text_bg_default;
1136 _ctx->set_text_fg_default_bright = flanterm_fb_set_text_fg_default_bright;
1137 _ctx->set_text_bg_default_bright = flanterm_fb_set_text_bg_default_bright;
1138 _ctx->move_character = flanterm_fb_move_character;
1139 _ctx->scroll = flanterm_fb_scroll;
1140 _ctx->revscroll = flanterm_fb_revscroll;
1141 _ctx->swap_palette = flanterm_fb_swap_palette;
1142 _ctx->save_state = flanterm_fb_save_state;
1143 _ctx->restore_state = flanterm_fb_restore_state;
1144 _ctx->double_buffer_flush = flanterm_fb_double_buffer_flush;
1145 _ctx->full_refresh = flanterm_fb_full_refresh;
1146 _ctx->deinit = flanterm_fb_deinit;
1147
1148 flanterm_context_reinit(_ctx);
1149 flanterm_fb_full_refresh(_ctx);
1150
1151#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
1152 if (_malloc == bump_alloc) {
1153 bump_allocated_instance = true;
1154 }
1155#endif
1156
1157 return _ctx;
1158
1159fail:
1160#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
1161 if (_malloc == bump_alloc) {
1162 bump_alloc_ptr = 0;
1163 return NULL;
1164 }
1165#endif
1166
1167 if (_free == NULL) {
1168 return NULL;
1169 }
1170
1171#ifndef FLANTERM_FB_DISABLE_CANVAS
1172 if (ctx->canvas != NULL) {
1173 _free(ctx->canvas, ctx->canvas_size);
1174 }
1175#endif
1176 if (ctx->map != NULL) {
1177 _free(ctx->map, ctx->map_size);
1178 }
1179 if (ctx->queue != NULL) {
1180 _free(ctx->queue, ctx->queue_size);
1181 }
1182 if (ctx->grid != NULL) {
1183 _free(ctx->grid, ctx->grid_size);
1184 }
1185 if (ctx->font_bool != NULL) {
1186 _free(ctx->font_bool, ctx->font_bool_size);
1187 }
1188 if (ctx->font_bits != NULL) {
1189 _free(ctx->font_bits, ctx->font_bits_size);
1190 }
1191 if (ctx != NULL) {
1192 _free(ctx, sizeof(struct flanterm_fb_context));
1193 }
1194
1195 return NULL;
1196}