3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License (LGPL) as published by the Free Software Foundation.
7 Please refer to the COPYING file for more information.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 Copyright (c) 2004 Bruno T. C. de Oliveira
23 #include "roteprivate.h"
24 #include "inject_csi.h"
28 static void cursor_line_down(RoteTerm *rt) {
31 rt->curpos_dirty = true;
32 if (rt->crow <= rt->pd->scrollbottom) return;
34 /* must scroll the scrolling region up by 1 line, and put cursor on
36 rt->crow = rt->pd->scrollbottom;
38 for (i = rt->pd->scrolltop; i < rt->pd->scrollbottom; i++) {
39 rt->line_dirty[i] = true;
40 memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
43 rt->line_dirty[rt->pd->scrollbottom] = true;
45 /* clear last row of the scrolling region */
46 for (i = 0; i < rt->cols; i++) {
47 rt->cells[rt->pd->scrollbottom][i].ch = 0x20;
48 rt->cells[rt->pd->scrollbottom][i].attr = 0x70;
53 static void cursor_line_up(RoteTerm *rt) {
56 rt->curpos_dirty = true;
57 if (rt->crow >= rt->pd->scrolltop) return;
59 /* must scroll the scrolling region up by 1 line, and put cursor on
61 rt->crow = rt->pd->scrolltop;
63 for (i = rt->pd->scrollbottom; i > rt->pd->scrolltop; i--) {
64 rt->line_dirty[i] = true;
65 memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
68 rt->line_dirty[rt->pd->scrolltop] = true;
70 /* clear first row of the scrolling region */
71 for (i = 0; i < rt->cols; i++) {
72 rt->cells[rt->pd->scrolltop][i].ch = 0x20;
73 rt->cells[rt->pd->scrolltop][i].attr = 0x70;
78 static inline void put_normal_char(RoteTerm *rt, char c) {
79 if (rt->ccol >= rt->cols) {
87 for(i = rt->cols - 1; i >= rt->ccol+1; i--)
88 rt->cells[rt->crow][i] = rt->cells[rt->crow][i-1];
91 rt->cells[rt->crow][rt->ccol].ch = c;
92 rt->cells[rt->crow][rt->ccol].attr = rt->curattr;
95 rt->line_dirty[rt->crow] = true;
96 rt->curpos_dirty = true;
99 static inline void put_graphmode_char(RoteTerm *rt, char c) {
101 /* do some very pitiful translation to regular ascii chars */
103 case 'j': case 'k': case 'l': case 'm': case 'n': case 't':
104 case 'u': case 'v': case 'w':
112 put_normal_char(rt, nc);
115 static inline void new_escape_sequence(RoteTerm *rt) {
116 rt->pd->escaped = true;
117 rt->pd->esbuf_len = 0;
118 rt->pd->esbuf[0] = '\0';
121 static inline void cancel_escape_sequence(RoteTerm *rt) {
122 rt->pd->escaped = false;
123 rt->pd->esbuf_len = 0;
124 rt->pd->esbuf[0] = '\0';
127 static void handle_control_char(RoteTerm *rt, char c) {
129 case '\r': rt->ccol = 0; break; /* carriage return */
130 case '\n': /* line feed */
131 rt->ccol = 0; cursor_line_down(rt);
132 rt->curpos_dirty = true;
134 case '\b': /* backspace */
135 if (rt->ccol > 0) rt->ccol--;
136 rt->curpos_dirty = true;
139 rt->ccol += 8 - (rt->ccol % 8);
140 clamp_cursor_to_bounds(rt);
142 case '\x1B': /* begin escape sequence (aborting previous one if any) */
143 new_escape_sequence(rt);
145 case '\x0E': /* enter graphical character mode */
146 rt->pd->graphmode = true;
148 case '\x0F': /* exit graphical character mode */
149 rt->pd->graphmode = false;
151 case '\x9B': /* CSI character. Equivalent to ESC [ */
152 new_escape_sequence(rt);
153 rt->pd->esbuf[rt->pd->esbuf_len++] = '[';
155 case '\x18': case '\x1A': /* these interrupt escape sequences */
156 cancel_escape_sequence(rt);
158 case '\a': /* bell */
159 /* do nothing for now... maybe a visual bell would be nice? */
163 fprintf(stderr, "Unrecognized control char: %d (^%c)\n", c, c + '@');
169 static inline bool is_valid_csi_ender(char c) {
170 return (c >= 'a' && c <= 'z') ||
171 (c >= 'A' && c <= 'Z') ||
172 c == '@' || c == '`';
175 static void try_interpret_escape_seq(RoteTerm *rt) {
176 char firstchar = rt->pd->esbuf[0];
177 char lastchar = rt->pd->esbuf[rt->pd->esbuf_len-1];
179 if (!firstchar) return; /* too early to do anything */
181 if (rt->pd->handler) {
182 /* call custom handler */
184 fprintf(stderr, "Calling custom handler for ES <%s>.\n", rt->pd->esbuf);
187 int answer = (*(rt->pd->handler))(rt, rt->pd->esbuf);
188 if (answer == ROTE_HANDLERESULT_OK) {
189 /* successfully handled */
191 fprintf(stderr, "Handler returned OK. Done with escape sequence.\n");
194 cancel_escape_sequence(rt);
197 else if (answer == ROTE_HANDLERESULT_NOTYET) {
198 /* handler might handle it when more characters are appended to
199 * it. So for now we don't interpret it */
201 fprintf(stderr, "Handler returned NOTYET. Waiting for more chars.\n");
207 /* If we got here then answer == ROTE_HANDLERESULT_NOWAY */
208 /* handler said it can't handle that escape sequence,
209 * but we can still try handling it ourselves, so
210 * we proceed normally. */
212 fprintf(stderr, "Handler returned NOWAY. Trying our handlers.\n");
216 /* interpret ESC-M as reverse line-feed */
217 if (firstchar == 'M') {
219 cancel_escape_sequence(rt);
223 if (firstchar != '[' && firstchar != ']') {
224 /* unrecognized escape sequence. Let's forget about it. */
226 fprintf(stderr, "Unrecognized ES: <%s>\n", rt->pd->esbuf);
229 cancel_escape_sequence(rt);
233 if (firstchar == '[' && is_valid_csi_ender(lastchar)) {
234 /* we have a csi escape sequence: interpret it */
235 rote_es_interpret_csi(rt);
236 cancel_escape_sequence(rt);
238 else if (firstchar == ']' && lastchar == '\a') {
239 /* we have an xterm escape sequence: interpret it */
241 /* rote_es_interpret_xterm_es(rt); -- TODO!*/
243 fprintf(stderr, "Ignored XTerm ES.\n");
245 cancel_escape_sequence(rt);
248 /* if the escape sequence took up all available space and could
249 * not yet be parsed, abort it */
250 if (rt->pd->esbuf_len + 1 >= ESEQ_BUF_SIZE) cancel_escape_sequence(rt);
253 void rote_vt_inject(RoteTerm *rt, const char *data, int len) {
255 for (i = 0; i < len; i++, data++) {
256 if (*data == 0) continue; /* completely ignore NUL */
257 if (*data >= 1 && *data <= 31) {
258 handle_control_char(rt, *data);
262 if (rt->pd->escaped && rt->pd->esbuf_len < ESEQ_BUF_SIZE) {
263 /* append character to ongoing escape sequence */
264 rt->pd->esbuf[rt->pd->esbuf_len] = *data;
265 rt->pd->esbuf[++rt->pd->esbuf_len] = 0;
267 try_interpret_escape_seq(rt);
269 else if (rt->pd->graphmode)
270 put_graphmode_char(rt, *data);
272 put_normal_char(rt, *data);