Let the configure script generate rote{,w}.h
[librote.git] / rote.c
blobd3be84d28f61a72d1f3772e5a20968d698b9d937
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.
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 (c) 2004 Bruno T. C. de Oliveira
22 #include "rote.h"
23 #include "roteprivate.h"
24 #include <stdlib.h>
25 #ifdef USE_PTY
26 #include <pty.h>
27 #endif
28 #include <stdio.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <stdbool.h>
32 #include <sys/ioctl.h>
33 #include <termios.h>
34 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
35 # include <util.h>
36 #endif
38 #define ROTE_VT_UPDATE_ITERATIONS 5
40 RoteTerm *rote_vt_create(int rows, int cols) {
41 RoteTerm *rt;
42 int i, j;
44 if (rows <= 0 || cols <= 0) return NULL;
46 if (! (rt = (RoteTerm*) malloc(sizeof(RoteTerm))) ) return NULL;
47 memset(rt, 0, sizeof(RoteTerm));
49 /* record dimensions */
50 rt->rows = rows;
51 rt->cols = cols;
53 /* default mode is replace */
54 rt->insert = false;
56 /* create the cell matrix */
57 rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows);
58 for (i = 0; i < rt->rows; i++) {
59 /* create row */
60 rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
62 /* fill row with spaces */
63 for (j = 0; j < rt->cols; j++)
64 clear_cell(&rt->cells[i][j]);
67 /* allocate dirtiness array */
68 rt->line_dirty = (bool*) malloc(sizeof(bool) * rt->rows);
70 /* initialization of other public fields */
71 rt->crow = rt->ccol = 0;
72 rt->curattr = 0x70; /* white text over black background */
74 /* allocate private data */
75 rt->pd = (RoteTermPrivate*) malloc(sizeof(RoteTermPrivate));
76 memset(rt->pd, 0, sizeof(RoteTermPrivate));
78 rt->pd->pty = -1; /* no pty for now */
80 /* initial scrolling area is the whole window */
81 rt->pd->scrolltop = 0;
82 rt->pd->scrollbottom = rt->rows - 1;
84 #ifdef DEBUG
85 fprintf(stderr, "Created a %d x %d terminal.\n", rt->rows, rt->cols);
86 #endif
88 return rt;
91 RoteTerm *rote_vt_resize(RoteTerm *rt,int rows, int cols){
92 int i, j;
93 if (rows <= 0 || cols <= 0) return NULL;
94 if (!rt) return NULL;
96 /* allocate dirtiness array */
97 rt->line_dirty = (bool*) realloc(rt->line_dirty,sizeof(bool) * rows);
98 /* grow / shrink rows */
99 if (rows > rt->rows) {
100 rt->cells = (RoteCell**) realloc(rt->cells,sizeof(RoteCell*) * rows);
101 for (i = rt->rows; i < rows; i++) {
102 /* create row */
103 rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
104 /* fill row with spaces but only until rt->cols rest
105 * will be done later */
106 for (j = 0; j < rt->cols; j++)
107 clear_cell(&rt->cells[i][j]);
109 } else if (rows < rt->rows) {
110 for(i = rows; i < rt->rows; i++)
111 free(rt->cells[i]);
112 rt->cells = (RoteCell**) realloc(rt->cells,sizeof(RoteCell*) * rows);
115 /* grow / shrink cols */
116 if (cols > rt->cols){
117 for (i = 0; i < rows; i++) {
118 rt->cells[i] = (RoteCell*) realloc(rt->cells[i],sizeof(RoteCell) * cols);
119 /* fill new space with blanks */
120 for (j = rt->cols; j < cols; j++)
121 clear_cell(&rt->cells[i][j]);
123 } else if (cols < rt->cols) {
124 for (i = 0; i < rows; i++)
125 rt->cells[i] = (RoteCell*) realloc(rt->cells[i],sizeof(RoteCell) * cols);
128 /* record dimensions */
129 rt->rows = rows;
130 rt->cols = cols;
131 if(rt->ccol >= rt->cols)
132 rt->ccol = rt->cols - 1;
133 if(rt->crow >= rt->rows)
134 rt->crow = rt->rows - 1;
135 rt->pd->scrolltop = 0;
136 rt->pd->scrollbottom = rt->rows - 1;
138 if(rt->pd->pty){
139 struct winsize winsize;
140 winsize.ws_row = rows;
141 winsize.ws_col = cols;
142 winsize.ws_xpixel = 0;
143 winsize.ws_ypixel = 0;
144 ioctl(rt->pd->pty,TIOCSWINSZ,&winsize);
146 /* send SIGWINCH to child process to notify of the size change */
147 if(rt->childpid)
148 kill(rt->childpid,SIGWINCH);
149 #ifdef DEBUG
150 fprintf(stderr, "Resized a %d x %d terminal.\n", rt->rows, rt->cols);
151 #endif
152 return rt;
155 void rote_vt_destroy(RoteTerm *rt) {
156 int i;
157 if (!rt) return;
159 free(rt->pd);
160 free(rt->line_dirty);
161 for (i = 0; i < rt->rows; i++) free(rt->cells[i]);
162 free(rt->cells);
163 free(rt);
166 #ifdef USE_NCURSES
168 static void default_cur_set_attr(WINDOW *win, unsigned char attr) {
169 int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr);
170 if (!cp) wattrset(win, A_NORMAL);
171 else wattrset(win, COLOR_PAIR(cp));
173 if (ROTE_ATTR_BOLD(attr)) wattron(win, A_BOLD);
174 if (ROTE_ATTR_BLINK(attr)) wattron(win, A_BLINK);
177 #endif
179 #ifdef USE_NCURSES
181 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol,
182 void (*cur_set_attr)(WINDOW*,unsigned char)) {
184 int i, j;
185 rote_vt_update(rt);
187 #ifdef USE_UTF8
188 wchar_t wstr[2];
189 wstr[1] = '\0';
190 #endif
192 if (!cur_set_attr) cur_set_attr = default_cur_set_attr;
193 for (i = 0; i < rt->rows; i++) {
194 wmove(win, srow + i, scol);
195 for (j = 0; j < rt->cols; ) {
196 (*cur_set_attr)(win, rt->cells[i][j].attr);
197 #ifdef USE_UTF8
198 if (!rt->cells[i][j].empty) {
199 wstr[0] = rt->cells[i][j].ch;
200 waddwstr(win, wstr);
201 } else {
202 waddch(win, ' ');
204 j += rt->cells[i][j].fcount;
205 #else
206 waddch(win, rt->cells[i][j].ch);
207 j++;
208 #endif
212 wmove(win, srow + rt->crow, scol + rt->ccol);
215 #endif
217 #ifdef USE_PTY
219 pid_t rote_vt_forkpty(RoteTerm *rt, const char *command) {
220 struct winsize ws;
221 pid_t childpid;
223 ws.ws_row = rt->rows;
224 ws.ws_col = rt->cols;
225 ws.ws_xpixel = ws.ws_ypixel = 0;
227 childpid = forkpty(&rt->pd->pty, NULL, NULL, &ws);
228 if (childpid < 0) return -1;
230 if (childpid == 0) {
231 /* we are the child, running under the slave side of the pty. */
233 /* Cajole application into using linux-console-compatible escape
234 * sequences (which is what we are prepared to interpret) */
235 setenv("TERM", "linux", 1);
237 /* Now we will exec /bin/sh -c command. */
238 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
240 fprintf(stderr, "\nexecl() failed.\nCommand: '%s'\n", command);
241 exit(127); /* error exec'ing */
244 /* if we got here we are the parent process */
245 rt->childpid = childpid;
246 return childpid;
249 void rote_vt_forsake_child(RoteTerm *rt) {
250 if (rt->pd->pty >= 0) close(rt->pd->pty);
251 rt->pd->pty = -1;
252 rt->childpid = 0;
255 #endif
257 void rote_vt_update(RoteTerm *rt) {
258 fd_set ifs;
259 struct timeval tvzero;
260 char buf[512];
261 int bytesread;
262 int n = ROTE_VT_UPDATE_ITERATIONS;
263 if (rt->pd->pty < 0) return; /* nothing to pump */
265 while (n--) { /* iterate at most ROVE_VT_UPDATE_ITERATIONS times.
266 * As Phil Endecott pointed out, if we don't restrict this,
267 * a program that floods the terminal with output
268 * could cause this loop to iterate forever, never
269 * being able to catch up. So we'll rely on the client
270 * calling rote_vt_update often, as the documentation
271 * recommends :-) */
273 /* check if pty has something to say */
274 FD_ZERO(&ifs); FD_SET(rt->pd->pty, &ifs);
275 tvzero.tv_sec = 0; tvzero.tv_usec = 0;
277 if (select(rt->pd->pty + 1, &ifs, NULL, NULL, &tvzero) <= 0)
278 return; /* nothing to read, or select() failed */
280 /* read what we can. This is guaranteed not to block, since
281 * select() told us there was something to read. */
282 bytesread = read(rt->pd->pty, buf, 512);
283 if (bytesread <= 0) return;
285 /* inject the data into the terminal */
286 rote_vt_inject(rt, buf, bytesread);
290 void rote_vt_write(RoteTerm *rt, const char *data, int len) {
291 if (rt->pd->pty < 0) {
292 /* no pty, so just inject the data plain and simple */
293 rote_vt_inject(rt, data, len);
294 return;
297 /* write data to pty. Keep calling write() until we have written
298 * everything. */
299 while (len > 0) {
300 int byteswritten = write(rt->pd->pty, data, len);
301 if (byteswritten < 0) {
302 /* very ugly way to inform the error. Improvements welcome! */
303 static char errormsg[] = "\n(ROTE: pty write() error)\n";
304 rote_vt_inject(rt, errormsg, strlen(errormsg));
305 return;
308 data += byteswritten;
309 len -= byteswritten;
313 void rote_vt_install_handler(RoteTerm *rt, rote_es_handler_t handler) {
314 rt->pd->handler = handler;
317 void *rote_vt_take_snapshot(RoteTerm *rt) {
318 int i;
319 int bytes_per_row = sizeof(RoteCell) * rt->cols;
320 void *buf = malloc(bytes_per_row * rt->rows);
321 void *ptr = buf;
323 for (i = 0; i < rt->rows; i++, ptr += bytes_per_row)
324 memcpy(ptr, rt->cells[i], bytes_per_row);
326 return buf;
329 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf) {
330 int i;
331 int bytes_per_row = sizeof(RoteCell) * rt->cols;
333 for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) {
334 rt->line_dirty[i] = true;
335 memcpy(rt->cells[i], snapbuf, bytes_per_row);
339 int rote_vt_get_pty_fd(RoteTerm *rt) {
340 return rt->pd->pty;