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 © 2004 Bruno T. C. de Oliveira
19 Copyright © 2006 Pierre Habouzit
26 #include "roteprivate.h"
27 #include "inject_csi.h"
29 static void cursor_line_down(RoteTerm *rt) {
32 rt->curpos_dirty = true;
33 if (rt->crow <= rt->pd->scrollbottom) return;
35 /* must scroll the scrolling region up by 1 line, and put cursor on
37 rt->crow = rt->pd->scrollbottom;
39 for (i = rt->pd->scrolltop; i < rt->pd->scrollbottom; i++) {
40 rt->line_dirty[i] = true;
41 memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
44 rt->line_dirty[rt->pd->scrollbottom] = true;
46 /* clear last row of the scrolling region */
47 for (i = 0; i < rt->cols; i++) {
48 rt->cells[rt->pd->scrollbottom][i].ch = 0x20;
49 rt->cells[rt->pd->scrollbottom][i].attr = 0x70;
54 static void cursor_line_up(RoteTerm *rt) {
57 rt->curpos_dirty = true;
58 if (rt->crow >= rt->pd->scrolltop) return;
60 /* must scroll the scrolling region up by 1 line, and put cursor on
62 rt->crow = rt->pd->scrolltop;
64 for (i = rt->pd->scrollbottom; i > rt->pd->scrolltop; i--) {
65 rt->line_dirty[i] = true;
66 memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
69 rt->line_dirty[rt->pd->scrolltop] = true;
71 /* clear first row of the scrolling region */
72 for (i = 0; i < rt->cols; i++) {
73 rt->cells[rt->pd->scrolltop][i].ch = 0x20;
74 rt->cells[rt->pd->scrolltop][i].attr = 0x70;
79 static inline void put_normal_char(RoteTerm *rt, char c) {
80 if (rt->ccol >= rt->cols) {
88 for(i = rt->cols - 1; i >= rt->ccol+1; i--)
89 rt->cells[rt->crow][i] = rt->cells[rt->crow][i-1];
92 rt->cells[rt->crow][rt->ccol].ch = c;
93 rt->cells[rt->crow][rt->ccol].attr = rt->curattr;
96 rt->line_dirty[rt->crow] = true;
97 rt->curpos_dirty = true;
100 static inline void put_graphmode_char(RoteTerm *rt, char c) {
102 /* do some very pitiful translation to regular ascii chars */
104 case 'j': case 'k': case 'l': case 'm': case 'n': case 't':
105 case 'u': case 'v': case 'w':
113 put_normal_char(rt, nc);
116 static inline void new_escape_sequence(RoteTerm *rt) {
117 rt->pd->escaped = true;
118 rt->pd->esbuf_len = 0;
119 rt->pd->esbuf[0] = '\0';
122 static inline void cancel_escape_sequence(RoteTerm *rt) {
123 rt->pd->escaped = false;
124 rt->pd->esbuf_len = 0;
125 rt->pd->esbuf[0] = '\0';
128 static void handle_control_char(RoteTerm *rt, char c) {
130 case '\r': rt->ccol = 0; break; /* carriage return */
131 case '\n': /* line feed */
132 rt->ccol = 0; cursor_line_down(rt);
133 rt->curpos_dirty = true;
135 case '\b': /* backspace */
136 if (rt->ccol > 0) rt->ccol--;
137 rt->curpos_dirty = true;
140 rt->ccol += 8 - (rt->ccol % 8);
141 clamp_cursor_to_bounds(rt);
143 case '\x1B': /* begin escape sequence (aborting previous one if any) */
144 new_escape_sequence(rt);
146 case '\x0E': /* enter graphical character mode */
147 rt->pd->graphmode = true;
149 case '\x0F': /* exit graphical character mode */
150 rt->pd->graphmode = false;
152 case '\x9B': /* CSI character. Equivalent to ESC [ */
153 new_escape_sequence(rt);
154 rt->pd->esbuf[rt->pd->esbuf_len++] = '[';
156 case '\x18': case '\x1A': /* these interrupt escape sequences */
157 cancel_escape_sequence(rt);
159 case '\a': /* bell */
160 /* do nothing for now... maybe a visual bell would be nice? */
164 fprintf(stderr, "Unrecognized control char: %d (^%c)\n", c, c + '@');
170 static inline bool is_valid_csi_ender(char c) {
171 return (c >= 'a' && c <= 'z') ||
172 (c >= 'A' && c <= 'Z') ||
173 c == '@' || c == '`';
176 static void try_interpret_escape_seq(RoteTerm *rt) {
177 char firstchar = rt->pd->esbuf[0];
178 char lastchar = rt->pd->esbuf[rt->pd->esbuf_len-1];
180 if (!firstchar) return; /* too early to do anything */
182 if (rt->pd->handler) {
183 /* call custom handler */
185 fprintf(stderr, "Calling custom handler for ES <%s>.\n", rt->pd->esbuf);
188 int answer = (*(rt->pd->handler))(rt, rt->pd->esbuf);
189 if (answer == ROTE_HANDLERESULT_OK) {
190 /* successfully handled */
192 fprintf(stderr, "Handler returned OK. Done with escape sequence.\n");
195 cancel_escape_sequence(rt);
198 else if (answer == ROTE_HANDLERESULT_NOTYET) {
199 /* handler might handle it when more characters are appended to
200 * it. So for now we don't interpret it */
202 fprintf(stderr, "Handler returned NOTYET. Waiting for more chars.\n");
208 /* If we got here then answer == ROTE_HANDLERESULT_NOWAY */
209 /* handler said it can't handle that escape sequence,
210 * but we can still try handling it ourselves, so
211 * we proceed normally. */
213 fprintf(stderr, "Handler returned NOWAY. Trying our handlers.\n");
217 /* interpret ESC-M as reverse line-feed */
218 if (firstchar == 'M') {
220 cancel_escape_sequence(rt);
224 if (firstchar != '[' && firstchar != ']') {
225 /* unrecognized escape sequence. Let's forget about it. */
227 fprintf(stderr, "Unrecognized ES: <%s>\n", rt->pd->esbuf);
230 cancel_escape_sequence(rt);
234 if (firstchar == '[' && is_valid_csi_ender(lastchar)) {
235 /* we have a csi escape sequence: interpret it */
236 rote_es_interpret_csi(rt);
237 cancel_escape_sequence(rt);
239 else if (firstchar == ']' && lastchar == '\a') {
240 /* we have an xterm escape sequence: interpret it */
242 /* rote_es_interpret_xterm_es(rt); -- TODO!*/
244 fprintf(stderr, "Ignored XTerm ES.\n");
246 cancel_escape_sequence(rt);
249 /* if the escape sequence took up all available space and could
250 * not yet be parsed, abort it */
251 if (rt->pd->esbuf_len + 1 >= ESEQ_BUF_SIZE) cancel_escape_sequence(rt);
254 void rote_vt_inject(RoteTerm *rt, const char *data, int len) {
256 for (i = 0; i < len; i++, data++) {
257 if (*data == 0) continue; /* completely ignore NUL */
258 if (*data >= 1 && *data <= 31) {
259 handle_control_char(rt, *data);
263 if (rt->pd->escaped && rt->pd->esbuf_len < ESEQ_BUF_SIZE) {
264 /* append character to ongoing escape sequence */
265 rt->pd->esbuf[rt->pd->esbuf_len] = *data;
266 rt->pd->esbuf[++rt->pd->esbuf_len] = 0;
268 try_interpret_escape_seq(rt);
270 else if (rt->pd->graphmode)
271 put_graphmode_char(rt, *data);
273 put_normal_char(rt, *data);