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