Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1#ifndef _TERM_H
2#define _TERM_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <stddef.h>
9#include <stdint.h>
10#include <stdbool.h>
11
12#define TERM_MAX_ESC_VALUES 16
13
14#define TERM_CB_DEC 10
15#define TERM_CB_BELL 20
16#define TERM_CB_PRIVATE_ID 30
17#define TERM_CB_STATUS_REPORT 40
18#define TERM_CB_POS_REPORT 50
19#define TERM_CB_KBD_LEDS 60
20#define TERM_CB_MODE 70
21#define TERM_CB_LINUX 80
22
23#define TERM_OOB_OUTPUT_OCRNL (1 << 0)
24#define TERM_OOB_OUTPUT_OFDEL (1 << 1)
25#define TERM_OOB_OUTPUT_OFILL (1 << 2)
26#define TERM_OOB_OUTPUT_OLCUC (1 << 3)
27#define TERM_OOB_OUTPUT_ONLCR (1 << 4)
28#define TERM_OOB_OUTPUT_ONLRET (1 << 5)
29#define TERM_OOB_OUTPUT_ONOCR (1 << 6)
30#define TERM_OOB_OUTPUT_OPOST (1 << 7)
31
32struct term_context {
33 /* internal use */
34
35 size_t tab_size;
36 bool autoflush;
37 bool cursor_enabled;
38 bool scroll_enabled;
39 bool control_sequence;
40 bool csi;
41 bool escape;
42 bool osc;
43 bool osc_escape;
44 bool rrr;
45 bool discard_next;
46 bool bold;
47 bool bg_bold;
48 bool reverse_video;
49 bool dec_private;
50 bool insert_mode;
51 uint64_t code_point;
52 size_t unicode_remaining;
53 uint8_t g_select;
54 uint8_t charsets[2];
55 size_t current_charset;
56 size_t escape_offset;
57 size_t esc_values_i;
58 size_t saved_cursor_x;
59 size_t saved_cursor_y;
60 size_t current_primary;
61 size_t current_bg;
62 size_t scroll_top_margin;
63 size_t scroll_bottom_margin;
64 uint32_t esc_values[TERM_MAX_ESC_VALUES];
65 uint64_t oob_output;
66 bool saved_state_bold;
67 bool saved_state_bg_bold;
68 bool saved_state_reverse_video;
69 size_t saved_state_current_charset;
70 size_t saved_state_current_primary;
71 size_t saved_state_current_bg;
72
73 /* to be set by backend */
74
75 size_t rows, cols;
76 bool in_bootloader;
77
78 void (*raw_putchar)(struct term_context *, uint8_t c);
79 void (*clear)(struct term_context *, bool move);
80 void (*set_cursor_pos)(struct term_context *, size_t x, size_t y);
81 void (*get_cursor_pos)(struct term_context *, size_t *x, size_t *y);
82 void (*set_text_fg)(struct term_context *, size_t fg);
83 void (*set_text_bg)(struct term_context *, size_t bg);
84 void (*set_text_fg_bright)(struct term_context *, size_t fg);
85 void (*set_text_bg_bright)(struct term_context *, size_t bg);
86 void (*set_text_fg_rgb)(struct term_context *, uint32_t fg);
87 void (*set_text_bg_rgb)(struct term_context *, uint32_t bg);
88 void (*set_text_fg_default)(struct term_context *);
89 void (*set_text_bg_default)(struct term_context *);
90 void (*set_text_fg_default_bright)(struct term_context *);
91 void (*set_text_bg_default_bright)(struct term_context *);
92 void (*move_character)(struct term_context *, size_t new_x, size_t new_y, size_t old_x, size_t old_y);
93 void (*scroll)(struct term_context *);
94 void (*revscroll)(struct term_context *);
95 void (*swap_palette)(struct term_context *);
96 void (*save_state)(struct term_context *);
97 void (*restore_state)(struct term_context *);
98 void (*double_buffer_flush)(struct term_context *);
99 void (*full_refresh)(struct term_context *);
100 void (*deinit)(struct term_context *, void (*)(void *, size_t));
101
102 /* to be set by client */
103
104 void (*callback)(struct term_context *, uint64_t, uint64_t, uint64_t, uint64_t);
105};
106
107void term_context_reinit(struct term_context *ctx);
108void term_write(struct term_context *ctx, const char *buf, size_t count);
109
110#ifdef __cplusplus
111}
112#endif
113
114#endif