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