Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1/* Copyright (C) 2022-2025 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_PRIVATE_H
27#define FLANTERM_PRIVATE_H 1
28
29#ifndef FLANTERM_IN_FLANTERM
30#error "Do not use flanterm_private.h. Use interfaces defined in flanterm.h only."
31#endif
32
33#include <stddef.h>
34#include <stdint.h>
35#include <stdbool.h>
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41#define FLANTERM_MAX_ESC_VALUES 16
42
43struct flanterm_context {
44 /* internal use */
45
46 size_t tab_size;
47 bool autoflush;
48 bool cursor_enabled;
49 bool scroll_enabled;
50 bool control_sequence;
51 bool escape;
52 bool osc;
53 bool osc_escape;
54 bool rrr;
55 bool discard_next;
56 bool bold;
57 bool bg_bold;
58 bool reverse_video;
59 bool dec_private;
60 bool insert_mode;
61 bool csi_unhandled;
62 uint64_t code_point;
63 size_t unicode_remaining;
64 uint8_t g_select;
65 uint8_t charsets[2];
66 size_t current_charset;
67 size_t escape_offset;
68 size_t esc_values_i;
69 size_t saved_cursor_x;
70 size_t saved_cursor_y;
71 size_t current_primary;
72 size_t current_bg;
73 size_t scroll_top_margin;
74 size_t scroll_bottom_margin;
75 uint32_t esc_values[FLANTERM_MAX_ESC_VALUES];
76 uint64_t oob_output;
77 bool saved_state_bold;
78 bool saved_state_bg_bold;
79 bool saved_state_reverse_video;
80 size_t saved_state_current_charset;
81 size_t saved_state_current_primary;
82 size_t saved_state_current_bg;
83
84 /* to be set by backend */
85
86 size_t rows, cols;
87
88 void (*raw_putchar)(struct flanterm_context *, uint8_t c);
89 void (*clear)(struct flanterm_context *, bool move);
90 void (*set_cursor_pos)(struct flanterm_context *, size_t x, size_t y);
91 void (*get_cursor_pos)(struct flanterm_context *, size_t *x, size_t *y);
92 void (*set_text_fg)(struct flanterm_context *, size_t fg);
93 void (*set_text_bg)(struct flanterm_context *, size_t bg);
94 void (*set_text_fg_bright)(struct flanterm_context *, size_t fg);
95 void (*set_text_bg_bright)(struct flanterm_context *, size_t bg);
96 void (*set_text_fg_rgb)(struct flanterm_context *, uint32_t fg);
97 void (*set_text_bg_rgb)(struct flanterm_context *, uint32_t bg);
98 void (*set_text_fg_default)(struct flanterm_context *);
99 void (*set_text_bg_default)(struct flanterm_context *);
100 void (*set_text_fg_default_bright)(struct flanterm_context *);
101 void (*set_text_bg_default_bright)(struct flanterm_context *);
102 void (*move_character)(struct flanterm_context *, size_t new_x, size_t new_y, size_t old_x, size_t old_y);
103 void (*scroll)(struct flanterm_context *);
104 void (*revscroll)(struct flanterm_context *);
105 void (*swap_palette)(struct flanterm_context *);
106 void (*save_state)(struct flanterm_context *);
107 void (*restore_state)(struct flanterm_context *);
108 void (*double_buffer_flush)(struct flanterm_context *);
109 void (*full_refresh)(struct flanterm_context *);
110 void (*deinit)(struct flanterm_context *, void (*)(void *, size_t));
111
112 /* to be set by client */
113
114 void (*callback)(struct flanterm_context *, uint64_t, uint64_t, uint64_t, uint64_t);
115};
116
117void flanterm_context_reinit(struct flanterm_context *ctx);
118
119#ifdef __cplusplus
120}
121#endif
122
123#endif