Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1 2/* Copyright (C) 2022-2025 mintsuki and contributors. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#ifndef FLANTERM_H 28#define FLANTERM_H 1 29 30#ifdef __cplusplus 31extern "C" { 32#endif 33 34#include <stddef.h> 35#include <stdint.h> 36#include <stdbool.h> 37 38#define FLANTERM_MAX_ESC_VALUES 16 39 40#define FLANTERM_CB_DEC 10 41#define FLANTERM_CB_BELL 20 42#define FLANTERM_CB_PRIVATE_ID 30 43#define FLANTERM_CB_STATUS_REPORT 40 44#define FLANTERM_CB_POS_REPORT 50 45#define FLANTERM_CB_KBD_LEDS 60 46#define FLANTERM_CB_MODE 70 47#define FLANTERM_CB_LINUX 80 48 49#define FLANTERM_OOB_OUTPUT_OCRNL (1 << 0) 50#define FLANTERM_OOB_OUTPUT_OFDEL (1 << 1) 51#define FLANTERM_OOB_OUTPUT_OFILL (1 << 2) 52#define FLANTERM_OOB_OUTPUT_OLCUC (1 << 3) 53#define FLANTERM_OOB_OUTPUT_ONLCR (1 << 4) 54#define FLANTERM_OOB_OUTPUT_ONLRET (1 << 5) 55#define FLANTERM_OOB_OUTPUT_ONOCR (1 << 6) 56#define FLANTERM_OOB_OUTPUT_OPOST (1 << 7) 57 58#ifdef FLANTERM_IN_FLANTERM 59 60struct flanterm_context { 61 /* internal use */ 62 63 size_t tab_size; 64 bool autoflush; 65 bool cursor_enabled; 66 bool scroll_enabled; 67 bool control_sequence; 68 bool escape; 69 bool osc; 70 bool osc_escape; 71 bool rrr; 72 bool discard_next; 73 bool bold; 74 bool bg_bold; 75 bool reverse_video; 76 bool dec_private; 77 bool insert_mode; 78 uint64_t code_point; 79 size_t unicode_remaining; 80 uint8_t g_select; 81 uint8_t charsets[2]; 82 size_t current_charset; 83 size_t escape_offset; 84 size_t esc_values_i; 85 size_t saved_cursor_x; 86 size_t saved_cursor_y; 87 size_t current_primary; 88 size_t current_bg; 89 size_t scroll_top_margin; 90 size_t scroll_bottom_margin; 91 uint32_t esc_values[FLANTERM_MAX_ESC_VALUES]; 92 uint64_t oob_output; 93 bool saved_state_bold; 94 bool saved_state_bg_bold; 95 bool saved_state_reverse_video; 96 size_t saved_state_current_charset; 97 size_t saved_state_current_primary; 98 size_t saved_state_current_bg; 99 100 /* to be set by backend */ 101 102 size_t rows, cols; 103 104 void (*raw_putchar)(struct flanterm_context *, uint8_t c); 105 void (*clear)(struct flanterm_context *, bool move); 106 void (*set_cursor_pos)(struct flanterm_context *, size_t x, size_t y); 107 void (*get_cursor_pos)(struct flanterm_context *, size_t *x, size_t *y); 108 void (*set_text_fg)(struct flanterm_context *, size_t fg); 109 void (*set_text_bg)(struct flanterm_context *, size_t bg); 110 void (*set_text_fg_bright)(struct flanterm_context *, size_t fg); 111 void (*set_text_bg_bright)(struct flanterm_context *, size_t bg); 112 void (*set_text_fg_rgb)(struct flanterm_context *, uint32_t fg); 113 void (*set_text_bg_rgb)(struct flanterm_context *, uint32_t bg); 114 void (*set_text_fg_default)(struct flanterm_context *); 115 void (*set_text_bg_default)(struct flanterm_context *); 116 void (*set_text_fg_default_bright)(struct flanterm_context *); 117 void (*set_text_bg_default_bright)(struct flanterm_context *); 118 void (*move_character)(struct flanterm_context *, size_t new_x, size_t new_y, size_t old_x, size_t old_y); 119 void (*scroll)(struct flanterm_context *); 120 void (*revscroll)(struct flanterm_context *); 121 void (*swap_palette)(struct flanterm_context *); 122 void (*save_state)(struct flanterm_context *); 123 void (*restore_state)(struct flanterm_context *); 124 void (*double_buffer_flush)(struct flanterm_context *); 125 void (*full_refresh)(struct flanterm_context *); 126 void (*deinit)(struct flanterm_context *, void (*)(void *, size_t)); 127 128 /* to be set by client */ 129 130 void (*callback)(struct flanterm_context *, uint64_t, uint64_t, uint64_t, uint64_t); 131}; 132 133#else 134 135struct flanterm_context; 136 137#endif 138 139void flanterm_context_reinit(struct flanterm_context *ctx); 140void flanterm_write(struct flanterm_context *ctx, const char *buf, size_t count); 141 142void flanterm_deinit(struct flanterm_context *ctx, void (*_free)(void *, size_t)); 143void flanterm_set_callback(struct flanterm_context *ctx, void (*callback)(struct flanterm_context *, uint64_t, uint64_t, uint64_t, uint64_t)); 144 145#ifdef __cplusplus 146} 147#endif 148 149#endif