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