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
27 #include <sys/ioctl.h>
30 #include "madtty_priv.h"
32 RoteTerm *rote_vt_create(int rows, int cols)
37 if (rows <= 0 || cols <= 0)
40 rt = (RoteTerm*)calloc(sizeof(RoteTerm), 1);
44 /* record dimensions */
48 /* default mode is replace */
51 /* create the cell matrix */
52 rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows);
53 for (i = 0; i < rt->rows; i++) {
55 rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
57 /* fill row with spaces */
58 for (j = 0; j < rt->cols; j++) {
59 rt->cells[i][j].ch = 0x20; /* a space */
60 rt->cells[i][j].attr = 0x70; /* white text, black background */
64 /* allocate dirtiness array */
65 rt->line_dirty = (bool*)calloc(sizeof(bool), rt->rows);
67 /* initialization of other public fields */
68 rt->crow = rt->ccol = 0;
69 rt->curattr = 0x70; /* white text over black background */
71 /* allocate private data */
72 rt->pd = (RoteTermPrivate*)calloc(sizeof(RoteTermPrivate), 1);
74 rt->pty = -1; /* no pty for now */
76 /* initial scrolling area is the whole window */
77 rt->pd->scrolltop = 0;
78 rt->pd->scrollbottom = rt->rows - 1;
83 void rote_vt_destroy(RoteTerm *rt)
91 for (i = 0; i < rt->rows; i++) {
100 static void default_cur_set_attr(WINDOW *win, unsigned char attr)
102 int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr);
103 if (!cp) wattrset(win, A_NORMAL);
104 else wattrset(win, COLOR_PAIR(cp));
106 if (ROTE_ATTR_BOLD(attr)) wattron(win, A_BOLD);
107 if (ROTE_ATTR_BLINK(attr)) wattron(win, A_BLINK);
110 static inline unsigned char ensure_printable(unsigned char ch)
112 return ch >= 32 ? ch : 32;
115 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol,
116 void (*cur_set_attr)(WINDOW*,unsigned char)) {
121 cur_set_attr = default_cur_set_attr;
123 for (i = 0; i < rt->rows; i++) {
124 wmove(win, srow + i, scol);
125 for (j = 0; j < rt->cols; j++) {
126 (*cur_set_attr)(win, rt->cells[i][j].attr);
127 waddch(win, ensure_printable(rt->cells[i][j].ch));
131 wmove(win, srow + rt->crow, scol + rt->ccol);
136 /******************************************************/
138 #define PTYCHAR1 "pqrstuvwxyz"
139 #define PTYCHAR2 "0123456789abcdef"
141 /* allocate one pty/tty pair */
142 static int get_pty(char *tty_str)
145 char ptydev[] = "/dev/pty??";
146 char ttydev[] = "/dev/tty??";
147 int len = strlen(ttydev);
150 for (c1 = PTYCHAR1; *c1; c1++) {
151 ptydev[len-2] = ttydev[len-2] = *c1;
152 for (c2 = PTYCHAR2; *c2; c2++) {
153 ptydev[len-1] = ttydev[len-1] = *c2;
154 if ((fd = open(ptydev, O_RDWR)) >= 0) {
155 if (access(ttydev, R_OK|W_OK) == 0) {
156 strcpy(tty_str, ttydev);
167 run_process(const char *path, const char **argv, int *fd_ptr, int *pid_ptr)
169 int pty_fd, pid, i, nb_fds;
173 pty_fd = get_pty(tty_name);
177 fcntl(pty_fd, F_SETFL, O_NONBLOCK);
179 /* set dummy screen size */
182 ws.ws_xpixel = ws.ws_col;
183 ws.ws_ypixel = ws.ws_row;
184 ioctl(pty_fd, TIOCSWINSZ, &ws);
192 nb_fds = getdtablesize();
193 for (i = 0; i < nb_fds; i++)
195 /* open pseudo tty for standard i/o */
196 open(tty_name, O_RDWR);
202 setenv("TERM", "linux", 1);
203 execv(path, (char *const*)argv);
204 fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
207 /* return file info */
213 pid_t rote_vt_forkpty(RoteTerm *rt, const char *path, const char *argv[])
217 ws.ws_row = rt->rows;
218 ws.ws_col = rt->cols;
219 ws.ws_xpixel = ws.ws_ypixel = 0;
221 if (run_process(path, argv, &rt->pty, &rt->childpid)) {
225 ioctl(rt->pty, TIOCSWINSZ, &ws);
229 void rote_vt_forsake_child(RoteTerm *rt)
237 int rote_vt_read(RoteTerm *rt, char *buf, int buflen)
244 return read(rt->pty, buf, buflen);
247 void rote_vt_write(RoteTerm *rt, const char *data, int len)
250 /* no pty, so just inject the data plain and simple */
251 rote_vt_inject(rt, data, len);
255 /* write data to pty. Keep calling write() until we have written
258 int byteswritten = write(rt->pty, data, len);
259 if (byteswritten < 0) {
260 /* very ugly way to inform the error. Improvements welcome! */
261 static char errormsg[] = "\n(ROTE: pty write() error)\n";
262 rote_vt_inject(rt, errormsg, strlen(errormsg));
266 data += byteswritten;
271 void *rote_vt_take_snapshot(RoteTerm *rt)
273 const int bytes_per_row = sizeof(RoteCell) * rt->cols;
274 void *buf = malloc(bytes_per_row * rt->rows);
278 for (i = 0; i < rt->rows; i++, ptr += bytes_per_row)
279 memcpy(ptr, rt->cells[i], bytes_per_row);
284 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf)
286 const int bytes_per_row = sizeof(RoteCell) * rt->cols;
290 for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) {
291 rt->line_dirty[i] = true;
292 memcpy(rt->cells[i], snapbuf, bytes_per_row);
296 static const char *keytable[KEY_MAX+1];
298 static void keytable_init()
300 memset(keytable, 0, KEY_MAX+1 * sizeof(const char*));
302 keytable['\n'] = "\r";
303 keytable[KEY_UP] = "\e[A";
304 keytable[KEY_DOWN] = "\e[B";
305 keytable[KEY_RIGHT] = "\e[C";
306 keytable[KEY_LEFT] = "\e[D";
307 keytable[KEY_BACKSPACE] = "\b";
308 keytable[KEY_HOME] = "\e[1~";
309 keytable[KEY_IC] = "\e[2~";
310 keytable[KEY_DC] = "\e[3~";
311 keytable[KEY_END] = "\e[4~";
312 keytable[KEY_PPAGE] = "\e[5~";
313 keytable[KEY_NPAGE] = "\e[6~";
314 keytable[KEY_SUSPEND] = "\x1A"; /* Ctrl+Z gets mapped to this */
315 keytable[KEY_F(1)] = "\e[[A";
316 keytable[KEY_F(2)] = "\e[[B";
317 keytable[KEY_F(3)] = "\e[[C";
318 keytable[KEY_F(4)] = "\e[[D";
319 keytable[KEY_F(5)] = "\e[[E";
320 keytable[KEY_F(6)] = "\e[17~";
321 keytable[KEY_F(7)] = "\e[18~";
322 keytable[KEY_F(8)] = "\e[19~";
323 keytable[KEY_F(9)] = "\e[20~";
324 keytable[KEY_F(10)] = "\e[21~";
327 void rote_vt_keypress(RoteTerm *rt, int keycode)
329 char c = (char) keycode;
331 if (keytable['\n'] == NULL)
334 if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
335 rote_vt_write(rt, keytable[keycode], strlen(keytable[keycode]));
337 rote_vt_write(rt, &c, 1); /* not special, just write it */