d8a238eedee97f39f8d2c523e332ef20d1c113ac
[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 struct tsm_screen_attr {
128         int8_t fccode;                  /* foreground color code or <0 for rgb */
129         int8_t bccode;                  /* background color code or <0 for rgb */
130         uint8_t fr;                     /* foreground red */
131         uint8_t fg;                     /* foreground green */
132         uint8_t fb;                     /* foreground blue */
133         uint8_t br;                     /* background red */
134         uint8_t bg;                     /* background green */
135         uint8_t bb;                     /* background blue */
136         unsigned int bold : 1;          /* bold character */
137         unsigned int underline : 1;     /* underlined character */
138         unsigned int inverse : 1;       /* inverse colors */
139         unsigned int protect : 1;       /* cannot be erased */
140         unsigned int blink : 1;         /* blinking character */
141 };
142
143 typedef int (*tsm_screen_draw_cb) (struct tsm_screen *con,
144                                    uint32_t id,
145                                    const uint32_t *ch,
146                                    size_t len,
147                                    unsigned int width,
148                                    unsigned int posx,
149                                    unsigned int posy,
150                                    const struct tsm_screen_attr *attr,
151                                    void *data);
152
153 int tsm_screen_new(struct tsm_screen **out, tsm_log_t log, void *log_data);
154 void tsm_screen_ref(struct tsm_screen *con);
155 void tsm_screen_unref(struct tsm_screen *con);
156
157 void tsm_screen_set_opts(struct tsm_screen *scr, unsigned int opts);
158 void tsm_screen_reset_opts(struct tsm_screen *scr, unsigned int opts);
159 unsigned int tsm_screen_get_opts(struct tsm_screen *scr);
160
161 unsigned int tsm_screen_get_width(struct tsm_screen *con);
162 unsigned int tsm_screen_get_height(struct tsm_screen *con);
163 int tsm_screen_resize(struct tsm_screen *con, unsigned int x,
164                       unsigned int y);
165 int tsm_screen_set_margins(struct tsm_screen *con,
166                            unsigned int top, unsigned int bottom);
167 void tsm_screen_set_max_sb(struct tsm_screen *con, unsigned int max);
168 void tsm_screen_clear_sb(struct tsm_screen *con);
169
170 void tsm_screen_sb_up(struct tsm_screen *con, unsigned int num);
171 void tsm_screen_sb_down(struct tsm_screen *con, unsigned int num);
172 void tsm_screen_sb_page_up(struct tsm_screen *con, unsigned int num);
173 void tsm_screen_sb_page_down(struct tsm_screen *con, unsigned int num);
174 void tsm_screen_sb_reset(struct tsm_screen *con);
175
176 void tsm_screen_set_def_attr(struct tsm_screen *con,
177                              const struct tsm_screen_attr *attr);
178 void tsm_screen_reset(struct tsm_screen *con);
179 void tsm_screen_set_flags(struct tsm_screen *con, unsigned int flags);
180 void tsm_screen_reset_flags(struct tsm_screen *con, unsigned int flags);
181 unsigned int tsm_screen_get_flags(struct tsm_screen *con);
182
183 unsigned int tsm_screen_get_cursor_x(struct tsm_screen *con);
184 unsigned int tsm_screen_get_cursor_y(struct tsm_screen *con);
185
186 void tsm_screen_set_tabstop(struct tsm_screen *con);
187 void tsm_screen_reset_tabstop(struct tsm_screen *con);
188 void tsm_screen_reset_all_tabstops(struct tsm_screen *con);
189
190 void tsm_screen_write(struct tsm_screen *con, tsm_symbol_t ch,
191                       const struct tsm_screen_attr *attr);
192 void tsm_screen_newline(struct tsm_screen *con);
193 void tsm_screen_scroll_up(struct tsm_screen *con, unsigned int num);
194 void tsm_screen_scroll_down(struct tsm_screen *con, unsigned int num);
195 void tsm_screen_move_to(struct tsm_screen *con, unsigned int x,
196                         unsigned int y);
197 void tsm_screen_move_up(struct tsm_screen *con, unsigned int num,
198                         bool scroll);
199 void tsm_screen_move_down(struct tsm_screen *con, unsigned int num,
200                           bool scroll);
201 void tsm_screen_move_left(struct tsm_screen *con, unsigned int num);
202 void tsm_screen_move_right(struct tsm_screen *con, unsigned int num);
203 void tsm_screen_move_line_end(struct tsm_screen *con);
204 void tsm_screen_move_line_home(struct tsm_screen *con);
205 void tsm_screen_tab_right(struct tsm_screen *con, unsigned int num);
206 void tsm_screen_tab_left(struct tsm_screen *con, unsigned int num);
207 void tsm_screen_insert_lines(struct tsm_screen *con, unsigned int num);
208 void tsm_screen_delete_lines(struct tsm_screen *con, unsigned int num);
209 void tsm_screen_insert_chars(struct tsm_screen *con, unsigned int num);
210 void tsm_screen_delete_chars(struct tsm_screen *con, unsigned int num);
211 void tsm_screen_erase_cursor(struct tsm_screen *con);
212 void tsm_screen_erase_chars(struct tsm_screen *con, unsigned int num);
213 void tsm_screen_erase_cursor_to_end(struct tsm_screen *con,
214                                     bool protect);
215 void tsm_screen_erase_home_to_cursor(struct tsm_screen *con,
216                                      bool protect);
217 void tsm_screen_erase_current_line(struct tsm_screen *con,
218                                    bool protect);
219 void tsm_screen_erase_screen_to_cursor(struct tsm_screen *con,
220                                        bool protect);
221 void tsm_screen_erase_cursor_to_screen(struct tsm_screen *con,
222                                        bool protect);
223 void tsm_screen_erase_screen(struct tsm_screen *con, bool protect);
224
225 void tsm_screen_selection_reset(struct tsm_screen *con);
226 void tsm_screen_selection_start(struct tsm_screen *con,
227                                 unsigned int posx,
228                                 unsigned int posy);
229 void tsm_screen_selection_target(struct tsm_screen *con,
230                                  unsigned int posx,
231                                  unsigned int posy);
232 int tsm_screen_selection_copy(struct tsm_screen *con, char **out);
233
234 void tsm_screen_draw(struct tsm_screen *con, tsm_screen_draw_cb draw_cb,
235                      void *data);
236
237 /* available character sets */
238
239 typedef tsm_symbol_t tsm_vte_charset[96];
240
241 extern tsm_vte_charset tsm_vte_unicode_lower;
242 extern tsm_vte_charset tsm_vte_unicode_upper;
243 extern tsm_vte_charset tsm_vte_dec_supplemental_graphics;
244 extern tsm_vte_charset tsm_vte_dec_special_graphics;
245
246 /* virtual terminal emulator */
247
248 struct tsm_vte;
249
250 /* keep in sync with shl_xkb_mods */
251 enum tsm_vte_modifier {
252         TSM_SHIFT_MASK          = (1 << 0),
253         TSM_LOCK_MASK           = (1 << 1),
254         TSM_CONTROL_MASK        = (1 << 2),
255         TSM_ALT_MASK            = (1 << 3),
256         TSM_LOGO_MASK           = (1 << 4),
257 };
258
259 /* keep in sync with TSM_INPUT_INVALID */
260 #define TSM_VTE_INVALID 0xffffffff
261
262 typedef void (*tsm_vte_write_cb) (struct tsm_vte *vte,
263                                   const char *u8,
264                                   size_t len,
265                                   void *data);
266
267 int tsm_vte_new(struct tsm_vte **out, struct tsm_screen *con,
268                 tsm_vte_write_cb write_cb, void *data,
269                 tsm_log_t log, void *log_data);
270 void tsm_vte_ref(struct tsm_vte *vte);
271 void tsm_vte_unref(struct tsm_vte *vte);
272
273 int tsm_vte_set_palette(struct tsm_vte *vte, const char *palette);
274
275 void tsm_vte_reset(struct tsm_vte *vte);
276 void tsm_vte_hard_reset(struct tsm_vte *vte);
277 void tsm_vte_input(struct tsm_vte *vte, const char *u8, size_t len);
278 bool tsm_vte_handle_keyboard(struct tsm_vte *vte, uint32_t keysym,
279                              uint32_t ascii, unsigned int mods,
280                              uint32_t unicode);
281
282 #endif /* LIBTSM_H */