Added copyright notices to files
[apps/madtty.git] / rote.c
1 /*
2 LICENSE INFORMATION:
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.
6
7 Please refer to the COPYING file for more information.
8
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.
13
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
17
18 Copyright (c) 2004 Bruno T. C. de Oliveira
19 */
20
21
22 #include "rote.h"
23 #include "roteprivate.h"
24 #include <stdlib.h>
25 #include <pty.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 RoteTerm *rote_vt_create(int rows, int cols) {
30    RoteTerm *rt;
31    int i, j;
32
33    if (rows <= 0 || cols <= 0) return NULL;
34
35    if (! (rt = (RoteTerm*) malloc(sizeof(RoteTerm))) ) return NULL;
36    memset(rt, 0, sizeof(RoteTerm));
37
38    /* record dimensions */
39    rt->rows = rows;
40    rt->cols = cols;
41
42    /* create the cell matrix */
43    rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows);
44    for (i = 0; i < rt->rows; i++) {
45       /* create row */
46       rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
47
48       /* fill row with spaces */
49       for (j = 0; j < rt->cols; j++) {
50          rt->cells[i][j].ch = 0x20;    /* a space */
51          rt->cells[i][j].attr = 0x70;  /* white text, black background */
52       }
53    }
54    
55    /* allocate dirtiness array */
56    rt->line_dirty = (bool*) malloc(sizeof(bool) * rt->rows);
57
58    /* initialization of other public fields */
59    rt->crow = rt->ccol = 0;
60    rt->curattr = 0x70;  /* white text over black background */
61
62    /* allocate private data */
63    rt->pd = (RoteTermPrivate*) malloc(sizeof(RoteTermPrivate));
64    memset(rt->pd, 0, sizeof(RoteTermPrivate));
65
66    rt->pd->pty = -1;  /* no pty for now */
67
68    /* initial scrolling area is the whole window */
69    rt->pd->scrolltop = 0;
70    rt->pd->scrollbottom = rt->rows - 1;
71
72    #ifdef DEBUG
73    fprintf(stderr, "Created a %d x %d terminal.\n", rt->rows, rt->cols);
74    #endif
75    
76    return rt;
77 }
78
79 void rote_vt_destroy(RoteTerm *rt) {
80    int i;
81    if (!rt) return;
82
83    free(rt->pd);
84    free(rt->line_dirty);
85    for (i = 0; i < rt->rows; i++) free(rt->cells[i]);
86    free(rt->cells);
87    free(rt);
88 }
89
90 static void default_cur_set_attr(WINDOW *win, unsigned char attr) {
91    int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr);
92    if (!cp) wattrset(win, A_NORMAL);
93    else     wattrset(win, COLOR_PAIR(cp));
94
95    if (ROTE_ATTR_BOLD(attr))     wattron(win, A_BOLD);
96    if (ROTE_ATTR_BLINK(attr))    wattron(win, A_BLINK);
97 }
98
99 static inline unsigned char ensure_printable(unsigned char ch) 
100                                         { return ch >= 32 ? ch : 32; }
101
102 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol, 
103                                 void (*cur_set_attr)(WINDOW*,unsigned char)) {
104
105    int i, j;
106    rote_vt_update(rt);
107    
108    if (!cur_set_attr) cur_set_attr = default_cur_set_attr;
109    for (i = 0; i < rt->rows; i++) {
110       wmove(win, srow + i, scol);
111       for (j = 0; j < rt->cols; j++) {
112          (*cur_set_attr)(win, rt->cells[i][j].attr);
113          waddch(win, ensure_printable(rt->cells[i][j].ch));
114       }
115    }
116
117    wmove(win, srow + rt->crow, scol + rt->ccol);
118 }
119
120 pid_t rote_vt_forkpty(RoteTerm *rt, const char *command) {
121    struct winsize ws;
122    pid_t childpid;
123    
124    ws.ws_row = rt->rows;
125    ws.ws_col = rt->cols;
126    ws.ws_xpixel = ws.ws_ypixel = 0;
127
128    childpid = forkpty(&rt->pd->pty, NULL, NULL, &ws);
129    if (childpid < 0) return -1;
130
131    if (childpid == 0) {
132       /* we are the child, running under the slave side of the pty. */
133
134       /* Cajole application into using linux-console-compatible escape
135        * sequences (which is what we are prepared to interpret) */
136       setenv("TERM", "linux", 1);
137
138       /* Now we will exec /bin/sh -c command. */
139       execl("/bin/sh", "/bin/sh", "-c", command, NULL);
140
141       fprintf(stderr, "\nexecl() failed.\nCommand: '%s'\n", command);
142       exit(127);  /* error exec'ing */
143    }
144
145    /* if we got here we are the parent process */
146    rt->childpid = childpid;
147    return childpid;
148 }
149
150 void rote_vt_forsake_child(RoteTerm *rt) {
151    if (rt->pd->pty >= 0) close(rt->pd->pty);
152    rt->pd->pty = -1;
153    rt->childpid = 0;
154 }
155
156 void rote_vt_update(RoteTerm *rt) {
157    fd_set ifs;
158    struct timeval tvzero;
159    char buf[512];
160    int bytesread;
161    if (rt->pd->pty < 0) return;  /* nothing to pump */
162
163    while (1) {
164       /* check if pty has something to say */
165       FD_ZERO(&ifs); FD_SET(rt->pd->pty, &ifs);
166       tvzero.tv_sec = 0; tvzero.tv_usec = 0;
167    
168       if (select(rt->pd->pty + 1, &ifs, NULL, NULL, &tvzero) <= 0)
169          return; /* nothing to read, or select() failed */
170
171       /* read what we can. This is guaranteed not to block, since
172        * select() told us there was something to read. */
173       bytesread = read(rt->pd->pty, buf, 512);
174       if (bytesread <= 0) return;   
175
176       /* inject the data into the terminal */
177       rote_vt_inject(rt, buf, bytesread);
178    }
179 }
180
181 void rote_vt_write(RoteTerm *rt, const char *data, int len) {
182    if (rt->pd->pty < 0) {
183       /* no pty, so just inject the data plain and simple */
184       rote_vt_inject(rt, data, len);
185       return;
186    }
187
188    /* write data to pty. Keep calling write() until we have written
189     * everything. */
190    while (len > 0) {
191       int byteswritten = write(rt->pd->pty, data, len);
192       if (byteswritten < 0) {
193          /* very ugly way to inform the error. Improvements welcome! */
194          static char errormsg[] = "\n(ROTE: pty write() error)\n";
195          rote_vt_inject(rt, errormsg, strlen(errormsg));
196          return;
197       }
198
199       data += byteswritten;
200       len  -= byteswritten;
201    }
202 }
203
204 void rote_vt_install_handler(RoteTerm *rt, rote_es_handler_t handler) {
205    rt->pd->handler = handler;
206 }
207
208 void *rote_vt_take_snapshot(RoteTerm *rt) {
209    int i;
210    int bytes_per_row = sizeof(RoteCell) * rt->cols;
211    void *buf = malloc(bytes_per_row * rt->rows);
212    void *ptr = buf;
213
214    for (i = 0; i < rt->rows; i++, ptr += bytes_per_row)
215       memcpy(ptr, rt->cells[i], bytes_per_row);
216
217    return buf;
218 }
219
220 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf) {
221    int i;
222    int bytes_per_row = sizeof(RoteCell) * rt->cols;
223
224    for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) {
225       rt->line_dirty[i] = true;
226       memcpy(rt->cells[i], snapbuf, bytes_per_row);
227    }
228 }
229
230