Add support for blink screen attribute
[platform/upstream/libtsm.git] / src / libtsm.h
1 /*
2  * TSM - Main 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 /*
27  * Main Header
28  * For convenience, you can include this header which then includes all
29  * available TSM headers for you.
30  */
31
32 #ifndef LIBTSM_H
33 #define LIBTSM_H
34
35 #include <inttypes.h>
36 #include <stdarg.h>
37 #include <stdbool.h>
38 #include <stdlib.h>
39
40 /**
41  * tsm_log_t:
42  * @data: user-provided data
43  * @file: Source code file where the log message originated or NULL
44  * @line: Line number in source code or 0
45  * @func: C function name or NULL
46  * @subs: Subsystem where the message came from or NULL
47  * @sev: Kernel-style severity between 0=FATAL and 7=DEBUG
48  * @format: printf-formatted message
49  * @args: arguments for printf-style @format
50  *
51  * This is the type of a logging callback function. You can always pass NULL
52  * instead of such a function to disable logging.
53  */
54 typedef void (*tsm_log_t) (void *data,
55                            const char *file,
56                            int line,
57                            const char *func,
58                            const char *subs,
59                            unsigned int sev,
60                            const char *format,
61                            va_list args);
62
63 /* UCS4 helpers */
64
65 #define TSM_UCS4_MAX (0x7fffffffUL)
66 #define TSM_UCS4_INVALID (TSM_UCS4_MAX + 1)
67 #define TSM_UCS4_REPLACEMENT (0xfffdUL)
68 #define TSM_UCS4_MAXLEN 10
69
70 /* symbols */
71
72 struct tsm_symbol_table;
73 typedef uint32_t tsm_symbol_t;
74
75 extern const tsm_symbol_t tsm_symbol_default;
76
77 int tsm_symbol_table_new(struct tsm_symbol_table **out);
78 void tsm_symbol_table_ref(struct tsm_symbol_table *tbl);
79 void tsm_symbol_table_unref(struct tsm_symbol_table *tbl);
80
81 tsm_symbol_t tsm_symbol_make(uint32_t ucs4);
82 tsm_symbol_t tsm_symbol_append(struct tsm_symbol_table *tbl,
83                                tsm_symbol_t sym, uint32_t ucs4);
84 const uint32_t *tsm_symbol_get(struct tsm_symbol_table *tbl,
85                                tsm_symbol_t *sym, size_t *size);
86 unsigned int tsm_symbol_get_width(struct tsm_symbol_table *tbl,
87                                   tsm_symbol_t sym);
88
89 /* ucs4 to utf8 converter */
90
91 unsigned int tsm_ucs4_get_width(uint32_t ucs4);
92 size_t tsm_ucs4_to_utf8(uint32_t ucs4, char *out);
93 char *tsm_ucs4_to_utf8_alloc(const uint32_t *ucs4, size_t len, size_t *len_out);
94
95 /* utf8 state machine */
96
97 struct tsm_utf8_mach;
98
99 enum tsm_utf8_mach_state {
100         TSM_UTF8_START,
101         TSM_UTF8_ACCEPT,
102         TSM_UTF8_REJECT,
103         TSM_UTF8_EXPECT1,
104         TSM_UTF8_EXPECT2,
105         TSM_UTF8_EXPECT3,
106 };
107
108 int tsm_utf8_mach_new(struct tsm_utf8_mach **out);
109 void tsm_utf8_mach_free(struct tsm_utf8_mach *mach);
110
111 int tsm_utf8_mach_feed(struct tsm_utf8_mach *mach, char c);
112 uint32_t tsm_utf8_mach_get(struct tsm_utf8_mach *mach);
113 void tsm_utf8_mach_reset(struct tsm_utf8_mach *mach);
114
115 /* screen objects */
116
117 struct tsm_screen;
118
119 #define TSM_SCREEN_INSERT_MODE  0x01
120 #define TSM_SCREEN_AUTO_WRAP    0x02
121 #define TSM_SCREEN_REL_ORIGIN   0x04
122 #define TSM_SCREEN_INVERSE      0x08
123 #define TSM_SCREEN_HIDE_CURSOR  0x10
124 #define TSM_SCREEN_FIXED_POS    0x20
125 #define TSM_SCREEN_ALTERNATE    0x40
126
127 #define TSM_SCREEN_OPT_RENDER_TIMING    0x01
128
129 struct tsm_screen_attr {
130         int8_t fccode;                  /* foreground color code or <0 for rgb */
131         int8_t bccode;                  /* background color code or <0 for rgb */
132         uint8_t fr;                     /* foreground red */
133         uint8_t fg;                     /* foreground green */
134         uint8_t fb;                     /* foreground blue */
135         uint8_t br;                     /* background red */
136         uint8_t bg;                     /* background green */
137         uint8_t bb;                     /* background blue */
138         unsigned int bold : 1;          /* bold character */
139         unsigned int underline : 1;     /* underlined character */
140         unsigned int inverse : 1;       /* inverse colors */
141         unsigned int protect : 1;       /* cannot be erased */
142         unsigned int blink : 1;         /* blinking character */
143 };
144
145 typedef int (*tsm_screen_prepare_cb) (struct tsm_screen *con,
146                                       void *data);
147 typedef int (*tsm_screen_draw_cb) (struct tsm_screen *con,
148                                    uint32_t id,
149                                    const uint32_t *ch,
150                                    size_t len,
151                                    unsigned int width,
152                                    unsigned int posx,
153                                    unsigned int posy,
154                                    const struct tsm_screen_attr *attr,
155                                    void *data);
156 typedef int (*tsm_screen_render_cb) (struct tsm_screen *con,
157                                      void *data);
158
159 int tsm_screen_new(struct tsm_screen **out, tsm_log_t log, void *log_data);
160 void tsm_screen_ref(struct tsm_screen *con);
161 void tsm_screen_unref(struct tsm_screen *con);
162
163 void tsm_screen_set_opts(struct tsm_screen *scr, unsigned int opts);
164 void tsm_screen_reset_opts(struct tsm_screen *scr, unsigned int opts);
165 unsigned int tsm_screen_get_opts(struct tsm_screen *scr);
166
167 unsigned int tsm_screen_get_width(struct tsm_screen *con);
168 unsigned int tsm_screen_get_height(struct tsm_screen *con);
169 int tsm_screen_resize(struct tsm_screen *con, unsigned int x,
170                       unsigned int y);
171 int tsm_screen_set_margins(struct tsm_screen *con,
172                            unsigned int top, unsigned int bottom);
173 void tsm_screen_set_max_sb(struct tsm_screen *con, unsigned int max);
174 void tsm_screen_clear_sb(struct tsm_screen *con);
175
176 void tsm_screen_sb_up(struct tsm_screen *con, unsigned int num);
177 void tsm_screen_sb_down(struct tsm_screen *con, unsigned int num);
178 void tsm_screen_sb_page_up(struct tsm_screen *con, unsigned int num);
179 void tsm_screen_sb_page_down(struct tsm_screen *con, unsigned int num);
180 void tsm_screen_sb_reset(struct tsm_screen *con);
181
182 void tsm_screen_set_def_attr(struct tsm_screen *con,
183                              const struct tsm_screen_attr *attr);
184 void tsm_screen_reset(struct tsm_screen *con);
185 void tsm_screen_set_flags(struct tsm_screen *con, unsigned int flags);
186 void tsm_screen_reset_flags(struct tsm_screen *con, unsigned int flags);
187 unsigned int tsm_screen_get_flags(struct tsm_screen *con);
188
189 unsigned int tsm_screen_get_cursor_x(struct tsm_screen *con);
190 unsigned int tsm_screen_get_cursor_y(struct tsm_screen *con);
191
192 void tsm_screen_set_tabstop(struct tsm_screen *con);
193 void tsm_screen_reset_tabstop(struct tsm_screen *con);
194 void tsm_screen_reset_all_tabstops(struct tsm_screen *con);
195
196 void tsm_screen_write(struct tsm_screen *con, tsm_symbol_t ch,
197                       const struct tsm_screen_attr *attr);
198 void tsm_screen_newline(struct tsm_screen *con);
199 void tsm_screen_scroll_up(struct tsm_screen *con, unsigned int num);
200 void tsm_screen_scroll_down(struct tsm_screen *con, unsigned int num);
201 void tsm_screen_move_to(struct tsm_screen *con, unsigned int x,
202                         unsigned int y);
203 void tsm_screen_move_up(struct tsm_screen *con, unsigned int num,
204                         bool scroll);
205 void tsm_screen_move_down(struct tsm_screen *con, unsigned int num,
206                           bool scroll);
207 void tsm_screen_move_left(struct tsm_screen *con, unsigned int num);
208 void tsm_screen_move_right(struct tsm_screen *con, unsigned int num);
209 void tsm_screen_move_line_end(struct tsm_screen *con);
210 void tsm_screen_move_line_home(struct tsm_screen *con);
211 void tsm_screen_tab_right(struct tsm_screen *con, unsigned int num);
212 void tsm_screen_tab_left(struct tsm_screen *con, unsigned int num);
213 void tsm_screen_insert_lines(struct tsm_screen *con, unsigned int num);
214 void tsm_screen_delete_lines(struct tsm_screen *con, unsigned int num);
215 void tsm_screen_insert_chars(struct tsm_screen *con, unsigned int num);
216 void tsm_screen_delete_chars(struct tsm_screen *con, unsigned int num);
217 void tsm_screen_erase_cursor(struct tsm_screen *con);
218 void tsm_screen_erase_chars(struct tsm_screen *con, unsigned int num);
219 void tsm_screen_erase_cursor_to_end(struct tsm_screen *con,
220                                     bool protect);
221 void tsm_screen_erase_home_to_cursor(struct tsm_screen *con,
222                                      bool protect);
223 void tsm_screen_erase_current_line(struct tsm_screen *con,
224                                    bool protect);
225 void tsm_screen_erase_screen_to_cursor(struct tsm_screen *con,
226                                        bool protect);
227 void tsm_screen_erase_cursor_to_screen(struct tsm_screen *con,
228                                        bool protect);
229 void tsm_screen_erase_screen(struct tsm_screen *con, bool protect);
230
231 void tsm_screen_selection_reset(struct tsm_screen *con);
232 void tsm_screen_selection_start(struct tsm_screen *con,
233                                 unsigned int posx,
234                                 unsigned int posy);
235 void tsm_screen_selection_target(struct tsm_screen *con,
236                                  unsigned int posx,
237                                  unsigned int posy);
238 int tsm_screen_selection_copy(struct tsm_screen *con, char **out);
239
240 void tsm_screen_draw(struct tsm_screen *con,
241                      tsm_screen_prepare_cb prepare_cb,
242                      tsm_screen_draw_cb draw_cb,
243                      tsm_screen_render_cb render_cb,
244                      void *data);
245
246 /* available character sets */
247
248 typedef tsm_symbol_t tsm_vte_charset[96];
249
250 extern tsm_vte_charset tsm_vte_unicode_lower;
251 extern tsm_vte_charset tsm_vte_unicode_upper;
252 extern tsm_vte_charset tsm_vte_dec_supplemental_graphics;
253 extern tsm_vte_charset tsm_vte_dec_special_graphics;
254
255 /* virtual terminal emulator */
256
257 struct tsm_vte;
258
259 /* keep in sync with shl_xkb_mods */
260 enum tsm_vte_modifier {
261         TSM_SHIFT_MASK          = (1 << 0),
262         TSM_LOCK_MASK           = (1 << 1),
263         TSM_CONTROL_MASK        = (1 << 2),
264         TSM_ALT_MASK            = (1 << 3),
265         TSM_LOGO_MASK           = (1 << 4),
266 };
267
268 /* keep in sync with TSM_INPUT_INVALID */
269 #define TSM_VTE_INVALID 0xffffffff
270
271 typedef void (*tsm_vte_write_cb) (struct tsm_vte *vte,
272                                   const char *u8,
273                                   size_t len,
274                                   void *data);
275
276 int tsm_vte_new(struct tsm_vte **out, struct tsm_screen *con,
277                 tsm_vte_write_cb write_cb, void *data,
278                 tsm_log_t log, void *log_data);
279 void tsm_vte_ref(struct tsm_vte *vte);
280 void tsm_vte_unref(struct tsm_vte *vte);
281
282 int tsm_vte_set_palette(struct tsm_vte *vte, const char *palette);
283
284 void tsm_vte_reset(struct tsm_vte *vte);
285 void tsm_vte_hard_reset(struct tsm_vte *vte);
286 void tsm_vte_input(struct tsm_vte *vte, const char *u8, size_t len);
287 bool tsm_vte_handle_keyboard(struct tsm_vte *vte, uint32_t keysym,
288                              uint32_t ascii, unsigned int mods,
289                              uint32_t unicode);
290
291 #endif /* LIBTSM_H */