Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1/* Copyright (C) 2022-2023 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#ifndef _FLANTERM_FR_H
27#define _FLANTERM_FB_H 1
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#include <stdint.h>
34#include <stddef.h>
35#include <stdbool.h>
36
37#include "../flanterm.h"
38
39#define FLANTERM_FB_FONT_GLYPHS 256
40
41struct flanterm_fb_char {
42 uint32_t c;
43 uint32_t fg;
44 uint32_t bg;
45};
46
47struct flanterm_fb_queue_item {
48 size_t x, y;
49 struct flanterm_fb_char c;
50};
51
52struct flanterm_fb_context {
53 struct flanterm_context term;
54
55 size_t font_width;
56 size_t font_height;
57 size_t glyph_width;
58 size_t glyph_height;
59
60 size_t font_scale_x;
61 size_t font_scale_y;
62
63 size_t offset_x, offset_y;
64
65 volatile uint32_t *framebuffer;
66 size_t pitch;
67 size_t width;
68 size_t height;
69 size_t bpp;
70
71#ifdef FLANTERM_FB_SUPPORT_BPP
72 uint8_t red_mask_size, red_mask_shift;
73 uint8_t green_mask_size, green_mask_shift;
74 uint8_t blue_mask_size, blue_mask_shift;
75#endif
76
77 size_t font_bits_size;
78 uint8_t *font_bits;
79 size_t font_bool_size;
80 bool *font_bool;
81
82 uint32_t ansi_colours[8];
83 uint32_t ansi_bright_colours[8];
84 uint32_t default_fg, default_bg;
85 uint32_t default_fg_bright, default_bg_bright;
86
87#ifndef FLANTERM_FB_DISABLE_CANVAS
88 size_t canvas_size;
89 uint32_t *canvas;
90#endif
91
92 size_t grid_size;
93 size_t queue_size;
94 size_t map_size;
95
96 struct flanterm_fb_char *grid;
97
98 struct flanterm_fb_queue_item *queue;
99 size_t queue_i;
100
101 struct flanterm_fb_queue_item **map;
102
103 uint32_t text_fg;
104 uint32_t text_bg;
105 size_t cursor_x;
106 size_t cursor_y;
107
108 uint32_t saved_state_text_fg;
109 uint32_t saved_state_text_bg;
110 size_t saved_state_cursor_x;
111 size_t saved_state_cursor_y;
112
113 size_t old_cursor_x;
114 size_t old_cursor_y;
115};
116
117struct flanterm_context *flanterm_fb_init(
118 void *(*_malloc)(size_t),
119 void (*_free)(void *, size_t),
120 uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
121#ifdef FLANTERM_FB_SUPPORT_BPP
122 uint8_t red_mask_size, uint8_t red_mask_shift,
123 uint8_t green_mask_size, uint8_t green_mask_shift,
124 uint8_t blue_mask_size, uint8_t blue_mask_shift,
125#endif
126#ifndef FLANTERM_FB_DISABLE_CANVAS
127 uint32_t *canvas,
128#endif
129 uint32_t *ansi_colours, uint32_t *ansi_bright_colours,
130 uint32_t *default_bg, uint32_t *default_fg,
131 uint32_t *default_bg_bright, uint32_t *default_fg_bright,
132 void *font, size_t font_width, size_t font_height, size_t font_spacing,
133 size_t font_scale_x, size_t font_scale_y,
134 size_t margin
135);
136
137#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
138static inline struct flanterm_context *flanterm_fb_simple_init(
139 uint32_t *framebuffer, size_t width, size_t height, size_t pitch
140#ifdef FLANTERM_FB_SUPPORT_BPP
141 ,
142 uint8_t red_mask_size, uint8_t red_mask_shift,
143 uint8_t green_mask_size, uint8_t green_mask_shift,
144 uint8_t blue_mask_size, uint8_t blue_mask_shift
145#endif
146) {
147 size_t font_scale_x = 1;
148 size_t font_scale_y = 1;
149 if (width >= (1920 + 1920 / 3) && height >= (1080 + 1080 / 3)) {
150 font_scale_x = 2;
151 font_scale_y = 2;
152 }
153 if (width >= (3840 + 3840 / 3) && height >= (2160 + 2160 / 3)) {
154 font_scale_x = 4;
155 font_scale_y = 4;
156 }
157
158 return flanterm_fb_init(
159 NULL,
160 NULL,
161 framebuffer, width, height, pitch,
162#ifdef FLANTERM_FB_SUPPORT_BPP
163 red_mask_size, red_mask_shift,
164 green_mask_size, green_mask_shift,
165 blue_mask_size, blue_mask_shift,
166#endif
167#ifndef FLANTERM_FB_DISABLE_CANVAS
168 NULL,
169#endif
170 NULL, NULL,
171 NULL, NULL,
172 NULL, NULL,
173 NULL, 0, 0, 1,
174 font_scale_x, font_scale_y,
175 0
176 );
177}
178#endif
179
180#ifdef __cplusplus
181}
182#endif
183
184#endif