1#ifndef __VTERM_INTERNAL_H__
2#define __VTERM_INTERNAL_H__
3
4#include "vterm.h"
5
6#include <stdarg.h>
7
8typedef struct VTermEncoding VTermEncoding;
9
10typedef struct {
11  VTermEncoding *enc;
12
13  // This size should be increased if required by other stateful encodings
14  char           data[4*sizeof(uint32_t)];
15} VTermEncodingInstance;
16
17struct VTermPen
18{
19  VTermColor fg;
20  VTermColor bg;
21  unsigned int bold:1;
22  unsigned int underline:2;
23  unsigned int italic:1;
24  unsigned int blink:1;
25  unsigned int reverse:1;
26  unsigned int strike:1;
27  unsigned int font:4; /* To store 0-9 */
28};
29
30static inline int vterm_color_equal(VTermColor a, VTermColor b)
31{
32  return a.red == b.red && a.green == b.green && a.blue == b.blue;
33}
34
35struct VTermState
36{
37  VTerm *vt;
38
39  const VTermStateCallbacks *callbacks;
40  void *cbdata;
41
42  int rows;
43  int cols;
44
45  /* Current cursor position */
46  VTermPos pos;
47
48  int at_phantom; /* True if we're on the "81st" phantom column to defer a wraparound */
49
50  int scrollregion_top;
51  int scrollregion_bottom; /* -1 means unbounded */
52#define SCROLLREGION_BOTTOM(state) ((state)->scrollregion_bottom > -1 ? (state)->scrollregion_bottom : (state)->rows)
53  int scrollregion_left;
54#define SCROLLREGION_LEFT(state)  ((state)->mode.leftrightmargin ? (state)->scrollregion_left : 0)
55  int scrollregion_right; /* -1 means unbounded */
56#define SCROLLREGION_RIGHT(state) ((state)->mode.leftrightmargin && (state)->scrollregion_right > -1 ? (state)->scrollregion_right : (state)->cols)
57
58  /* Bitvector of tab stops */
59  unsigned char *tabstops;
60
61  /* Mouse state */
62  int mouse_col, mouse_row;
63  int mouse_buttons;
64  int mouse_flags;
65  enum { MOUSE_X10, MOUSE_UTF8, MOUSE_SGR, MOUSE_RXVT } mouse_protocol;
66
67  /* Last glyph output, for Unicode recombining purposes */
68  uint32_t *combine_chars;
69  size_t combine_chars_size; // Number of ELEMENTS in the above
70  int combine_width; // The width of the glyph above
71  VTermPos combine_pos;   // Position before movement
72
73  struct {
74    int keypad:1;
75    int cursor:1;
76    int autowrap:1;
77    int insert:1;
78    int newline:1;
79    int cursor_visible:1;
80    int cursor_blink:1;
81    unsigned int cursor_shape:2;
82    int alt_screen:1;
83    int origin:1;
84    int screen:1;
85    int leftrightmargin:1;
86  } mode;
87
88  VTermEncodingInstance encoding[4], encoding_utf8;
89  int gl_set, gr_set, gsingle_set;
90
91  struct VTermPen pen;
92
93  VTermColor default_fg;
94  VTermColor default_bg;
95  int fg_index;
96  int bg_index;
97  int bold_is_highbright;
98
99  unsigned int protected_cell : 1;
100
101  /* Saved state under DEC mode 1048/1049 */
102  struct {
103    VTermPos pos;
104    struct VTermPen pen;
105
106    struct {
107      int cursor_visible:1;
108      int cursor_blink:1;
109      unsigned int cursor_shape:2;
110    } mode;
111  } saved;
112};
113
114struct VTerm
115{
116  VTermAllocatorFunctions *allocator;
117  void *allocdata;
118
119  int rows;
120  int cols;
121
122  struct {
123    int utf8:1;
124    int ctrl8bit:1;
125  } mode;
126
127  enum VTermParserState {
128    NORMAL,
129    CSI,
130    OSC,
131    DCS,
132    ESC,
133    ESC_IN_OSC,
134    ESC_IN_DCS,
135  } parser_state;
136  const VTermParserCallbacks *parser_callbacks;
137  void *cbdata;
138
139  /* len == malloc()ed size; cur == number of valid bytes */
140  char  *strbuffer;
141  size_t strbuffer_len;
142  size_t strbuffer_cur;
143
144  char  *outbuffer;
145  size_t outbuffer_len;
146  size_t outbuffer_cur;
147
148  VTermState *state;
149  VTermScreen *screen;
150};
151
152struct VTermEncoding {
153  void (*init) (VTermEncoding *enc, void *data);
154  void (*decode)(VTermEncoding *enc, void *data,
155                 uint32_t cp[], int *cpi, int cplen,
156                 const char bytes[], size_t *pos, size_t len);
157};
158
159typedef enum {
160  ENC_UTF8,
161  ENC_SINGLE_94
162} VTermEncodingType;
163
164void *vterm_allocator_malloc(VTerm *vt, size_t size);
165void  vterm_allocator_free(VTerm *vt, void *ptr);
166
167void vterm_push_output_bytes(VTerm *vt, const char *bytes, size_t len);
168void vterm_push_output_vsprintf(VTerm *vt, const char *format, va_list args);
169void vterm_push_output_sprintf(VTerm *vt, const char *format, ...);
170void vterm_push_output_sprintf_ctrl(VTerm *vt, unsigned char ctrl, const char *fmt, ...);
171void vterm_push_output_sprintf_dcs(VTerm *vt, const char *fmt, ...);
172
173void vterm_state_free(VTermState *state);
174
175void vterm_state_resetpen(VTermState *state);
176void vterm_state_setpen(VTermState *state, const long args[], int argcount);
177int  vterm_state_getpen(VTermState *state, long args[], int argcount);
178void vterm_state_savepen(VTermState *state, int save);
179
180enum {
181  C1_SS3 = 0x8f,
182  C1_DCS = 0x90,
183  C1_CSI = 0x9b,
184  C1_ST  = 0x9c,
185};
186
187void vterm_state_push_output_sprintf_CSI(VTermState *vts, const char *format, ...);
188
189void vterm_screen_free(VTermScreen *screen);
190
191VTermEncoding *vterm_lookup_encoding(VTermEncodingType type, char designation);
192
193int vterm_unicode_width(int codepoint);
194int vterm_unicode_is_combining(int codepoint);
195
196#endif
197