• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __VTERM_INTERNAL_H__
2 #define __VTERM_INTERNAL_H__
3 
4 #include "vterm.h"
5 
6 #include <stdarg.h>
7 
8 #if defined(__GNUC__)
9 # define INTERNAL __attribute__((visibility("internal")))
10 #else
11 # define INTERNAL
12 #endif
13 
14 #ifdef DEBUG
15 # define DEBUG_LOG(...) fprintf(stderr, __VA_ARGS__)
16 #else
17 # define DEBUG_LOG(...)
18 #endif
19 
20 #define ESC_S "\x1b"
21 
22 #define INTERMED_MAX 16
23 
24 #define CSI_ARGS_MAX 16
25 #define CSI_LEADER_MAX 16
26 
27 typedef struct VTermEncoding VTermEncoding;
28 
29 typedef struct {
30   VTermEncoding *enc;
31 
32   // This size should be increased if required by other stateful encodings
33   char           data[4*sizeof(uint32_t)];
34 } VTermEncodingInstance;
35 
36 struct VTermPen
37 {
38   VTermColor fg;
39   VTermColor bg;
40   unsigned int bold:1;
41   unsigned int underline:2;
42   unsigned int italic:1;
43   unsigned int blink:1;
44   unsigned int reverse:1;
45   unsigned int strike:1;
46   unsigned int font:4; /* To store 0-9 */
47 };
48 
49 struct VTermState
50 {
51   VTerm *vt;
52 
53   const VTermStateCallbacks *callbacks;
54   void *cbdata;
55 
56   const VTermParserCallbacks *fallbacks;
57   void *fbdata;
58 
59   int rows;
60   int cols;
61 
62   /* Current cursor position */
63   VTermPos pos;
64 
65   int at_phantom; /* True if we're on the "81st" phantom column to defer a wraparound */
66 
67   int scrollregion_top;
68   int scrollregion_bottom; /* -1 means unbounded */
69 #define SCROLLREGION_BOTTOM(state) ((state)->scrollregion_bottom > -1 ? (state)->scrollregion_bottom : (state)->rows)
70   int scrollregion_left;
71 #define SCROLLREGION_LEFT(state)  ((state)->mode.leftrightmargin ? (state)->scrollregion_left : 0)
72   int scrollregion_right; /* -1 means unbounded */
73 #define SCROLLREGION_RIGHT(state) ((state)->mode.leftrightmargin && (state)->scrollregion_right > -1 ? (state)->scrollregion_right : (state)->cols)
74 
75   /* Bitvector of tab stops */
76   unsigned char *tabstops;
77 
78   VTermLineInfo *lineinfo;
79 #define ROWWIDTH(state,row) ((state)->lineinfo[(row)].doublewidth ? ((state)->cols / 2) : (state)->cols)
80 #define THISROWWIDTH(state) ROWWIDTH(state, (state)->pos.row)
81 
82   /* Mouse state */
83   int mouse_col, mouse_row;
84   int mouse_buttons;
85   int mouse_flags;
86 #define MOUSE_WANT_CLICK 0x01
87 #define MOUSE_WANT_DRAG  0x02
88 #define MOUSE_WANT_MOVE  0x04
89 
90   enum { MOUSE_X10, MOUSE_UTF8, MOUSE_SGR, MOUSE_RXVT } mouse_protocol;
91 
92   /* Last glyph output, for Unicode recombining purposes */
93   uint32_t *combine_chars;
94   size_t combine_chars_size; // Number of ELEMENTS in the above
95   int combine_width; // The width of the glyph above
96   VTermPos combine_pos;   // Position before movement
97 
98   struct {
99     unsigned int keypad:1;
100     unsigned int cursor:1;
101     unsigned int autowrap:1;
102     unsigned int insert:1;
103     unsigned int newline:1;
104     unsigned int cursor_visible:1;
105     unsigned int cursor_blink:1;
106     unsigned int cursor_shape:2;
107     unsigned int alt_screen:1;
108     unsigned int origin:1;
109     unsigned int screen:1;
110     unsigned int leftrightmargin:1;
111     unsigned int bracketpaste:1;
112     unsigned int report_focus:1;
113   } mode;
114 
115   VTermEncodingInstance encoding[4], encoding_utf8;
116   int gl_set, gr_set, gsingle_set;
117 
118   struct VTermPen pen;
119 
120   VTermColor default_fg;
121   VTermColor default_bg;
122   VTermColor colors[16]; // Store the 8 ANSI and the 8 ANSI high-brights only
123 
124   int bold_is_highbright;
125 
126   unsigned int protected_cell : 1;
127 
128   /* Saved state under DEC mode 1048/1049 */
129   struct {
130     VTermPos pos;
131     struct VTermPen pen;
132 
133     struct {
134       unsigned int cursor_visible:1;
135       unsigned int cursor_blink:1;
136       unsigned int cursor_shape:2;
137     } mode;
138   } saved;
139 };
140 
141 typedef enum {
142   VTERM_PARSER_OSC,
143   VTERM_PARSER_DCS,
144 
145   VTERM_N_PARSER_TYPES
146 } VTermParserStringType;
147 
148 struct VTerm
149 {
150   VTermAllocatorFunctions *allocator;
151   void *allocdata;
152 
153   int rows;
154   int cols;
155 
156   struct {
157     unsigned int utf8:1;
158     unsigned int ctrl8bit:1;
159   } mode;
160 
161   struct {
162     enum VTermParserState {
163       NORMAL,
164       CSI_LEADER,
165       CSI_ARGS,
166       CSI_INTERMED,
167       ESC,
168       /* below here are the "string states" */
169       STRING,
170       ESC_IN_STRING,
171     } state;
172 
173     int intermedlen;
174     char intermed[INTERMED_MAX];
175 
176     int csi_leaderlen;
177     char csi_leader[CSI_LEADER_MAX];
178 
179     int csi_argi;
180     long csi_args[CSI_ARGS_MAX];
181 
182     const VTermParserCallbacks *callbacks;
183     void *cbdata;
184 
185     VTermParserStringType stringtype;
186     char  *strbuffer;
187     size_t strbuffer_len;
188     size_t strbuffer_cur;
189   } parser;
190 
191   /* len == malloc()ed size; cur == number of valid bytes */
192 
193   char  *outbuffer;
194   size_t outbuffer_len;
195   size_t outbuffer_cur;
196 
197   VTermState *state;
198   VTermScreen *screen;
199 };
200 
201 struct VTermEncoding {
202   void (*init) (VTermEncoding *enc, void *data);
203   void (*decode)(VTermEncoding *enc, void *data,
204                  uint32_t cp[], int *cpi, int cplen,
205                  const char bytes[], size_t *pos, size_t len);
206 };
207 
208 typedef enum {
209   ENC_UTF8,
210   ENC_SINGLE_94
211 } VTermEncodingType;
212 
213 void *vterm_allocator_malloc(VTerm *vt, size_t size);
214 void  vterm_allocator_free(VTerm *vt, void *ptr);
215 
216 void vterm_push_output_bytes(VTerm *vt, const char *bytes, size_t len);
217 void vterm_push_output_vsprintf(VTerm *vt, const char *format, va_list args);
218 void vterm_push_output_sprintf(VTerm *vt, const char *format, ...);
219 void vterm_push_output_sprintf_ctrl(VTerm *vt, unsigned char ctrl, const char *fmt, ...);
220 void vterm_push_output_sprintf_dcs(VTerm *vt, const char *fmt, ...);
221 
222 void vterm_state_free(VTermState *state);
223 
224 void vterm_state_newpen(VTermState *state);
225 void vterm_state_resetpen(VTermState *state);
226 void vterm_state_setpen(VTermState *state, const long args[], int argcount);
227 int  vterm_state_getpen(VTermState *state, long args[], int argcount);
228 void vterm_state_savepen(VTermState *state, int save);
229 
230 enum {
231   C1_SS3 = 0x8f,
232   C1_DCS = 0x90,
233   C1_CSI = 0x9b,
234   C1_ST  = 0x9c,
235 };
236 
237 void vterm_state_push_output_sprintf_CSI(VTermState *vts, const char *format, ...);
238 
239 void vterm_screen_free(VTermScreen *screen);
240 
241 VTermEncoding *vterm_lookup_encoding(VTermEncodingType type, char designation);
242 
243 int vterm_unicode_width(uint32_t codepoint);
244 int vterm_unicode_is_combining(uint32_t codepoint);
245 
246 #endif
247