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  * Header has been stripped down to only contain the vte_ related parts.
28  * Screen/scrolling/input/multiline etc. has all moved into shmif_tui.
29  *
30  */
31 
32 #ifndef TSM_LIBTSM_H
33 #define TSM_LIBTSM_H
34 
35 /**
36  * @mainpage
37  *
38  * TSM is a Terminal-emulator State Machine. It implements all common DEC-VT100
39  * to DEC-VT520 control codes and features. A state-machine is used to parse TTY
40  * input and saved in a virtual screen. TSM does not provide any rendering,
41  * glyph/font handling or anything more advanced. TSM is just a simple
42  * state-machine for control-codes handling.
43  * The main use-case for TSM are terminal-emulators. TSM has no dependencies
44  * other than an ISO-C99 compiler and C-library. Any terminal emulator for any
45  * window-environment or rendering-pipline can make use of TSM. However, TSM can
46  * also be used for control-code validation, TTY-screen-capturing or other
47  * advanced users of terminal escape-sequences.
48  */
49 
50 typedef uint32_t tsm_symbol_t;
51 typedef uint_fast32_t tsm_age_t;
52 
53 /**
54  * @defgroup vte State Machine
55  * Virtual terminal emulation with state machine
56  *
57  * A TSM VTE object provides the terminal state machine. It takes input from the
58  * application (which usually comes from a TTY/PTY from a client), parses it,
59  * modifies the attach screen or returns data which has to be written back to
60  * the client.
61  *
62  * Furthermore, VTE objects accept keyboard or mouse input from the application
63  * which is interpreted compliant to DEV-VTs.
64  *
65  * @{
66  */
67 
68 /* virtual terminal emulator */
69 
70 struct tsm_vte;
71 
72 enum tsm_vte_modifier {
73 	TSM_SHIFT_MASK	 = (ARKMOD_LSHIFT | ARKMOD_RSHIFT),
74 	TSM_LOCK_MASK		 = (ARKMOD_NUM),
75 	TSM_CONTROL_MASK = (ARKMOD_LCTRL | ARKMOD_RCTRL),
76 	TSM_ALT_MASK		 = (ARKMOD_LALT | ARKMOD_RALT),
77 	TSM_LOGO_MASK		 = (ARKMOD_LMETA | ARKMOD_RMETA),
78 };
79 
80 enum vte_color {
81 	VTE_COLOR_INVAL = -1,
82 	VTE_COLOR_BLACK = 0,
83 	VTE_COLOR_RED,
84 	VTE_COLOR_GREEN,
85 	VTE_COLOR_YELLOW,
86 	VTE_COLOR_BLUE,
87 	VTE_COLOR_MAGENTA,
88 	VTE_COLOR_CYAN,
89 	VTE_COLOR_LIGHT_GREY,
90 	VTE_COLOR_DARK_GREY,
91 	VTE_COLOR_LIGHT_RED,
92 	VTE_COLOR_LIGHT_GREEN,
93 	VTE_COLOR_LIGHT_YELLOW,
94 	VTE_COLOR_LIGHT_BLUE,
95 	VTE_COLOR_LIGHT_MAGENTA,
96 	VTE_COLOR_LIGHT_CYAN,
97 	VTE_COLOR_WHITE,
98 	VTE_COLOR_FOREGROUND,
99 	VTE_COLOR_BACKGROUND,
100 	VTE_COLOR_NUM
101 };
102 
103 /* keep in sync with TSM_INPUT_INVALID */
104 #define TSM_VTE_INVALID 0xffffffff
105 
106 typedef void (*tsm_vte_write_cb) (struct tsm_vte *vte,
107 				  const char *u8,
108 				  size_t len,
109 				  void *data);
110 
111 enum tsm_vte_group {
112 	TSM_GROUP_FREE,
113 	TSM_GROUP_OSC
114 };
115 
116 typedef void (*tsm_str_cb) (struct tsm_vte *vte, enum tsm_vte_group group,
117 	const char *u8, size_t len, bool crop, void* data);
118 
119 void tsm_set_strhandler(struct tsm_vte *vte,
120 	tsm_str_cb cb, size_t limit, void* data);
121 
122 int tsm_vte_new(struct tsm_vte **out,
123 		struct tui_context *con, tsm_vte_write_cb write_cb, void *data);
124 
125 void debug_log(struct tsm_vte* vte, const char* msg, ...);
126 void tsm_vte_ref(struct tsm_vte *vte);
127 void tsm_vte_unref(struct tsm_vte *vte);
128 
129 /*
130  * will set *out to the new context (caller is responsible for processing)
131  */
132 bool tsm_vte_debug(
133 	struct tsm_vte* in, struct tui_context** out,
134 	arcan_tui_conn* conn, struct tui_context* c
135 );
136 
137 void tsm_vte_update_debug(struct tsm_vte*);
138 int tsm_vte_debugfd(struct tsm_vte* vte);
139 
140 int tsm_vte_set_palette(struct tsm_vte *vte, const char *palette);
141 void tsm_vte_set_color(struct tsm_vte *vte,
142 	enum vte_color ind, const uint8_t rgb[3]);
143 void tsm_vte_get_color(struct tsm_vte *vte,
144 	enum vte_color ind, uint8_t *rgb);
145 
146 void tsm_vte_reset(struct tsm_vte *vte);
147 void tsm_vte_hard_reset(struct tsm_vte *vte);
148 void tsm_vte_input(struct tsm_vte *vte, const char *u8, size_t len);
149 bool tsm_vte_handle_keyboard(struct tsm_vte *vte, uint32_t keysym,
150 			     uint32_t ascii, unsigned int mods,
151 			     uint32_t unicode);
152 void tsm_vte_mouse_button(struct tsm_vte *vte, int index, bool press, int mods);
153 void tsm_vte_mouse_motion(struct tsm_vte *vte, int x, int y, int mods);
154 void tsm_vte_paste(struct tsm_vte *vte, const char *u8, size_t len);
155 
156 /*
157  * Return true of the state machine is inside of an unfinished escape sequence
158  * and needs more data. The idea is to use this as an indicator to defer refresh
159  * or not.
160  */
161 bool tsm_vte_inseq(struct tsm_vte *vte);
162 
163 #endif /* TSM_LIBTSM_H */
164