1 /*
2  * TSM - Main internal header
3  *
4  * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef TSM_LIBTSM_INT_H
27 #define TSM_LIBTSM_INT_H
28 
29 #include <inttypes.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include "libtsm.h"
33 #include "shl-llog.h"
34 
35 #define SHL_EXPORT
36 
37 /* max combined-symbol length */
38 #define TSM_UCS4_MAXLEN 10
39 
40 int tsm_wcwidth(wchar_t wc);
41 
42 /* symbols */
43 
44 struct tsm_symbol_table;
45 
46 extern const tsm_symbol_t tsm_symbol_default;
47 
48 int tsm_symbol_table_new(struct tsm_symbol_table **out);
49 void tsm_symbol_table_ref(struct tsm_symbol_table *tbl);
50 void tsm_symbol_table_unref(struct tsm_symbol_table *tbl);
51 
52 tsm_symbol_t tsm_symbol_make(uint32_t ucs4);
53 tsm_symbol_t tsm_symbol_append(struct tsm_symbol_table *tbl,
54 			       tsm_symbol_t sym, uint32_t ucs4);
55 const uint32_t *tsm_symbol_get(struct tsm_symbol_table *tbl,
56 			       tsm_symbol_t *sym, size_t *size);
57 int tsm_symbol_get_width(struct tsm_symbol_table *tbl, tsm_symbol_t sym);
58 
59 /* utf8 state machine */
60 
61 struct tsm_utf8_mach;
62 
63 enum tsm_utf8_mach_state {
64 	TSM_UTF8_START,
65 	TSM_UTF8_ACCEPT,
66 	TSM_UTF8_REJECT,
67 	TSM_UTF8_EXPECT1,
68 	TSM_UTF8_EXPECT2,
69 	TSM_UTF8_EXPECT3,
70 };
71 
72 int tsm_utf8_mach_new(struct tsm_utf8_mach **out);
73 void tsm_utf8_mach_free(struct tsm_utf8_mach *mach);
74 
75 int tsm_utf8_mach_feed(struct tsm_utf8_mach *mach, char c);
76 uint32_t tsm_utf8_mach_get(struct tsm_utf8_mach *mach);
77 void tsm_utf8_mach_reset(struct tsm_utf8_mach *mach);
78 
79 /* TSM screen */
80 
81 struct cell {
82 	tsm_symbol_t ch;		/* stored character */
83 	int width;			/* character width */
84 	struct tsm_screen_attr attr;	/* cell attributes */
85 	tsm_age_t age;			/* age of the single cell */
86 };
87 
88 struct line {
89 	struct line *next;		/* next line (NULL if not sb) */
90 	struct line *prev;		/* prev line (NULL if not sb) */
91 
92 	int size;			/* real width */
93 	struct cell *cells;		/* actuall cells */
94 	uint64_t sb_id;			/* sb ID */
95 	tsm_age_t age;			/* age of the whole line */
96 };
97 
98 #define SELECTION_TOP -1
99 struct selection_pos {
100 	struct line *line;
101 	int x;
102 	int y;
103 };
104 
105 struct tsm_screen {
106 	size_t ref;
107 	unsigned int opts;
108 	unsigned int flags;
109 	struct tsm_symbol_table *sym_table;
110 
111 	/* default attributes for new cells */
112 	struct tsm_screen_attr def_attr;
113 
114 	/* ageing */
115 	tsm_age_t age_cnt;		/* current age counter */
116 	unsigned int age_reset : 1;	/* age-overflow flag */
117 
118 	/* current buffer */
119 	int size_x;			/* width of screen */
120 	int size_y;			/* height of screen */
121 	int margin_top;			/* top-margin index */
122 	int margin_bottom;		/* bottom-margin index */
123 	int line_num;			/* real number of allocated lines */
124 	struct line **lines;		/* active lines; copy of main/alt */
125 	struct line **main_lines;	/* real main lines */
126 	struct line **alt_lines;	/* real alternative lines */
127 	tsm_age_t age;			/* whole screen age */
128 	int vanguard;			/* lowest non-empty line on screen */
129 
130 	/* scroll-back buffer */
131 	int sb_count;			/* number of lines in sb */
132 	struct line *sb_first;		/* first line; was moved first */
133 	struct line *sb_last;		/* last line; was moved last*/
134 	int sb_max;			/* max-limit of lines in sb */
135 	struct line *sb_pos;		/* current position in sb or NULL */
136 	uint64_t sb_last_id;		/* last id given to sb-line */
137 
138 	/* cursor: positions are always in-bound, but cursor_x might be
139 	 * bigger than size_x if new-line is pending */
140 	int cursor_x;			/* current cursor x-pos */
141 	int cursor_y;			/* current cursor y-pos */
142 
143 	/* tab ruler */
144 	bool *tab_ruler;		/* tab-flag for all cells of one row */
145 
146 	/* selection */
147 	bool sel_active;
148 	struct selection_pos sel_start;
149 	struct selection_pos sel_end;
150 };
151 
152 void screen_cell_init(struct tsm_screen *con, struct cell *cell);
153 
154 void tsm_screen_set_opts(struct tsm_screen *scr, unsigned int opts);
155 void tsm_screen_reset_opts(struct tsm_screen *scr, unsigned int opts);
156 unsigned int tsm_screen_get_opts(struct tsm_screen *scr);
157 
screen_inc_age(struct tsm_screen * con)158 static inline void screen_inc_age(struct tsm_screen *con)
159 {
160 	if (!++con->age_cnt) {
161 		con->age_reset = 1;
162 		++con->age_cnt;
163 	}
164 }
165 
166 /* available character sets */
167 
168 typedef tsm_symbol_t tsm_vte_charset[96];
169 
170 extern tsm_vte_charset tsm_vte_unicode_lower;
171 extern tsm_vte_charset tsm_vte_unicode_upper;
172 extern tsm_vte_charset tsm_vte_dec_supplemental_graphics;
173 extern tsm_vte_charset tsm_vte_dec_special_graphics;
174 
175 #endif /* TSM_LIBTSM_INT_H */
176