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
30 #include "roteprivate.h"
32 #define ROTE_VT_UPDATE_ITERATIONS 5
34 RoteTerm *rote_vt_create(int rows, int cols)
39 if (rows <= 0 || cols <= 0)
42 rt = (RoteTerm*)calloc(sizeof(RoteTerm), 1);
46 /* record dimensions */
50 /* default mode is replace */
53 /* create the cell matrix */
54 rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows);
55 for (i = 0; i < rt->rows; i++) {
57 rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
59 /* fill row with spaces */
60 for (j = 0; j < rt->cols; j++) {
61 rt->cells[i][j].ch = 0x20; /* a space */
62 rt->cells[i][j].attr = 0x70; /* white text, black background */
66 /* allocate dirtiness array */
67 rt->line_dirty = (bool*)calloc(sizeof(bool), rt->rows);
69 /* initialization of other public fields */
70 rt->crow = rt->ccol = 0;
71 rt->curattr = 0x70; /* white text over black background */
73 /* allocate private data */
74 rt->pd = (RoteTermPrivate*)calloc(sizeof(RoteTermPrivate), 1);
76 rt->pd->pty = -1; /* no pty for now */
78 /* initial scrolling area is the whole window */
79 rt->pd->scrolltop = 0;
80 rt->pd->scrollbottom = rt->rows - 1;
85 void rote_vt_destroy(RoteTerm *rt)
93 for (i = 0; i < rt->rows; i++) {
102 static void default_cur_set_attr(WINDOW *win, unsigned char attr)
104 int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr);
105 if (!cp) wattrset(win, A_NORMAL);
106 else wattrset(win, COLOR_PAIR(cp));
108 if (ROTE_ATTR_BOLD(attr)) wattron(win, A_BOLD);
109 if (ROTE_ATTR_BLINK(attr)) wattron(win, A_BLINK);
112 static inline unsigned char ensure_printable(unsigned char ch)
114 return ch >= 32 ? ch : 32;
117 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol,
118 void (*cur_set_attr)(WINDOW*,unsigned char)) {
123 if (!cur_set_attr) cur_set_attr = default_cur_set_attr;
124 for (i = 0; i < rt->rows; i++) {
125 wmove(win, srow + i, scol);
126 for (j = 0; j < rt->cols; j++) {
127 (*cur_set_attr)(win, rt->cells[i][j].attr);
128 waddch(win, ensure_printable(rt->cells[i][j].ch));
132 wmove(win, srow + rt->crow, scol + rt->ccol);
139 pid_t rote_vt_forkpty(RoteTerm *rt, const char *command)
144 ws.ws_row = rt->rows;
145 ws.ws_col = rt->cols;
146 ws.ws_xpixel = ws.ws_ypixel = 0;
148 childpid = forkpty(&rt->pd->pty, NULL, NULL, &ws);
153 /* we are the child, running under the slave side of the pty. */
155 /* Cajole application into using linux-console-compatible escape
156 * sequences (which is what we are prepared to interpret) */
157 setenv("TERM", "linux", 1);
159 /* Now we will exec /bin/sh -c command. */
160 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
162 fprintf(stderr, "\nexecl() failed.\nCommand: '%s'\n", command);
163 exit(127); /* error exec'ing */
166 /* if we got here we are the parent process */
167 return (rt->childpid = childpid);
170 void rote_vt_forsake_child(RoteTerm *rt)
172 if (rt->pd->pty >= 0)
180 void rote_vt_update(RoteTerm *rt)
183 struct timeval tvzero;
186 int n = ROTE_VT_UPDATE_ITERATIONS;
188 return; /* nothing to pump */
190 while (n--) { /* iterate at most ROVE_VT_UPDATE_ITERATIONS times.
191 * As Phil Endecott pointed out, if we don't restrict this,
192 * a program that floods the terminal with output
193 * could cause this loop to iterate forever, never
194 * being able to catch up. So we'll rely on the client
195 * calling rote_vt_update often, as the documentation
198 /* check if pty has something to say */
199 FD_ZERO(&ifs); FD_SET(rt->pd->pty, &ifs);
200 tvzero.tv_sec = 0; tvzero.tv_usec = 0;
202 if (select(rt->pd->pty + 1, &ifs, NULL, NULL, &tvzero) <= 0)
203 return; /* nothing to read, or select() failed */
205 /* read what we can. This is guaranteed not to block, since
206 * select() told us there was something to read. */
207 bytesread = read(rt->pd->pty, buf, 512);
208 if (bytesread <= 0) return;
210 /* inject the data into the terminal */
211 rote_vt_inject(rt, buf, bytesread);
215 void rote_vt_write(RoteTerm *rt, const char *data, int len)
217 if (rt->pd->pty < 0) {
218 /* no pty, so just inject the data plain and simple */
219 rote_vt_inject(rt, data, len);
223 /* write data to pty. Keep calling write() until we have written
226 int byteswritten = write(rt->pd->pty, data, len);
227 if (byteswritten < 0) {
228 /* very ugly way to inform the error. Improvements welcome! */
229 static char errormsg[] = "\n(ROTE: pty write() error)\n";
230 rote_vt_inject(rt, errormsg, strlen(errormsg));
234 data += byteswritten;
239 void *rote_vt_take_snapshot(RoteTerm *rt)
241 const int bytes_per_row = sizeof(RoteCell) * rt->cols;
242 void *buf = malloc(bytes_per_row * rt->rows);
246 for (i = 0; i < rt->rows; i++, ptr += bytes_per_row)
247 memcpy(ptr, rt->cells[i], bytes_per_row);
252 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf)
254 const int bytes_per_row = sizeof(RoteCell) * rt->cols;
258 for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) {
259 rt->line_dirty[i] = true;
260 memcpy(rt->cells[i], snapbuf, bytes_per_row);
264 int rote_vt_get_pty_fd(RoteTerm *rt)
269 static const char *keytable[KEY_MAX+1];
271 static void keytable_init()
273 memset(keytable, 0, KEY_MAX+1 * sizeof(const char*));
275 keytable['\n'] = "\r";
276 keytable[KEY_UP] = "\e[A";
277 keytable[KEY_DOWN] = "\e[B";
278 keytable[KEY_RIGHT] = "\e[C";
279 keytable[KEY_LEFT] = "\e[D";
280 keytable[KEY_BACKSPACE] = "\b";
281 keytable[KEY_HOME] = "\e[1~";
282 keytable[KEY_IC] = "\e[2~";
283 keytable[KEY_DC] = "\e[3~";
284 keytable[KEY_END] = "\e[4~";
285 keytable[KEY_PPAGE] = "\e[5~";
286 keytable[KEY_NPAGE] = "\e[6~";
287 keytable[KEY_SUSPEND] = "\x1A"; /* Ctrl+Z gets mapped to this */
288 keytable[KEY_F(1)] = "\e[[A";
289 keytable[KEY_F(2)] = "\e[[B";
290 keytable[KEY_F(3)] = "\e[[C";
291 keytable[KEY_F(4)] = "\e[[D";
292 keytable[KEY_F(5)] = "\e[[E";
293 keytable[KEY_F(6)] = "\e[17~";
294 keytable[KEY_F(7)] = "\e[18~";
295 keytable[KEY_F(8)] = "\e[19~";
296 keytable[KEY_F(9)] = "\e[20~";
297 keytable[KEY_F(10)] = "\e[21~";
300 void rote_vt_keypress(RoteTerm *rt, int keycode)
302 char c = (char) keycode;
304 if (keytable['\n'] != NULL)
307 if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
308 rote_vt_write(rt, keytable[keycode], strlen(keytable[keycode]));
310 rote_vt_write(rt, &c, 1); /* not special, just write it */