Hopefully fix compilation on *BSD.
[librote.git] / rote.c
blobc5d298565546099127d841e344d31211eb3f82fe
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 #include <stdio.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <stdbool.h>
29 #include <sys/ioctl.h>
30 #include <termios.h>
31 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
32 # include <util.h>
33 #elif defined(__linux__) && defined(USE_PTY)
34 # include <pty.h>
35 #endif
37 #define ROTE_VT_UPDATE_ITERATIONS 5
39 RoteTerm *rote_vt_create(int rows, int cols) {
40 RoteTerm *rt;
41 int i, j;
43 if (rows <= 0 || cols <= 0) return NULL;
45 if (! (rt = (RoteTerm*) malloc(sizeof(RoteTerm))) ) return NULL;
46 memset(rt, 0, sizeof(RoteTerm));
48 /* record dimensions */
49 rt->rows = rows;
50 rt->cols = cols;
52 /* default mode is replace */
53 rt->insert = false;
55 /* create the cell matrix */
56 rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows);
57 for (i = 0; i < rt->rows; i++) {
58 /* create row */
59 rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
61 /* fill row with spaces */
62 for (j = 0; j < rt->cols; j++)
63 clear_cell(&rt->cells[i][j]);
66 /* allocate dirtiness array */
67 rt->line_dirty = (bool*) malloc(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*) malloc(sizeof(RoteTermPrivate));
75 memset(rt->pd, 0, sizeof(RoteTermPrivate));
77 rt->pd->pty = -1; /* no pty for now */
79 /* initial scrolling area is the whole window */
80 rt->pd->scrolltop = 0;
81 rt->pd->scrollbottom = rt->rows - 1;
83 #ifdef DEBUG
84 fprintf(stderr, "Created a %d x %d terminal.\n", rt->rows, rt->cols);
85 #endif
87 return rt;
90 RoteTerm *rote_vt_resize(RoteTerm *rt,int rows, int cols){
91 int i, j;
92 if (rows <= 0 || cols <= 0) return NULL;
93 if (!rt) return NULL;
95 /* allocate dirtiness array */
96 rt->line_dirty = (bool*) realloc(rt->line_dirty,sizeof(bool) * rows);
97 /* grow / shrink rows */
98 if (rows > rt->rows) {
99 rt->cells = (RoteCell**) realloc(rt->cells,sizeof(RoteCell*) * rows);
100 for (i = rt->rows; i < rows; i++) {
101 /* create row */
102 rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
103 /* fill row with spaces but only until rt->cols rest
104 * will be done later */
105 for (j = 0; j < rt->cols; j++)
106 clear_cell(&rt->cells[i][j]);
108 } else if (rows < rt->rows) {
109 for(i = rows; i < rt->rows; i++)
110 free(rt->cells[i]);
111 rt->cells = (RoteCell**) realloc(rt->cells,sizeof(RoteCell*) * rows);
114 /* grow / shrink cols */
115 if (cols > rt->cols){
116 for (i = 0; i < rows; i++) {
117 rt->cells[i] = (RoteCell*) realloc(rt->cells[i],sizeof(RoteCell) * cols);
118 /* fill new space with blanks */
119 for (j = rt->cols; j < cols; j++)
120 clear_cell(&rt->cells[i][j]);
122 } else if (cols < rt->cols) {
123 for (i = 0; i < rows; i++)
124 rt->cells[i] = (RoteCell*) realloc(rt->cells[i],sizeof(RoteCell) * cols);
127 /* record dimensions */
128 rt->rows = rows;
129 rt->cols = cols;
130 if(rt->ccol >= rt->cols)
131 rt->ccol = rt->cols - 1;
132 if(rt->crow >= rt->rows)
133 rt->crow = rt->rows - 1;
134 rt->pd->scrolltop = 0;
135 rt->pd->scrollbottom = rt->rows - 1;
137 if(rt->pd->pty){
138 struct winsize winsize;
139 winsize.ws_row = rows;
140 winsize.ws_col = cols;
141 winsize.ws_xpixel = 0;
142 winsize.ws_ypixel = 0;
143 ioctl(rt->pd->pty,TIOCSWINSZ,&winsize);
145 /* send SIGWINCH to child process to notify of the size change */
146 if(rt->childpid)
147 kill(rt->childpid,SIGWINCH);
148 #ifdef DEBUG
149 fprintf(stderr, "Resized a %d x %d terminal.\n", rt->rows, rt->cols);
150 #endif
151 return rt;
154 void rote_vt_destroy(RoteTerm *rt) {
155 int i;
156 if (!rt) return;
158 free(rt->pd);
159 free(rt->line_dirty);
160 for (i = 0; i < rt->rows; i++) free(rt->cells[i]);
161 free(rt->cells);
162 free(rt);
165 #ifdef USE_NCURSES
167 static void default_cur_set_attr(WINDOW *win, unsigned char attr) {
168 int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr);
169 if (!cp) wattrset(win, A_NORMAL);
170 else wattrset(win, COLOR_PAIR(cp));
172 if (ROTE_ATTR_BOLD(attr)) wattron(win, A_BOLD);
173 if (ROTE_ATTR_BLINK(attr)) wattron(win, A_BLINK);
176 #endif
178 #ifdef USE_NCURSES
180 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol,
181 void (*cur_set_attr)(WINDOW*,unsigned char)) {
183 int i, j;
184 rote_vt_update(rt);
186 #ifdef USE_UTF8
187 wchar_t wstr[2];
188 wstr[1] = '\0';
189 #endif
191 if (!cur_set_attr) cur_set_attr = default_cur_set_attr;
192 for (i = 0; i < rt->rows; i++) {
193 wmove(win, srow + i, scol);
194 for (j = 0; j < rt->cols; ) {
195 (*cur_set_attr)(win, rt->cells[i][j].attr);
196 #ifdef USE_UTF8
197 if (!rt->cells[i][j].empty) {
198 wstr[0] = rt->cells[i][j].ch;
199 waddwstr(win, wstr);
200 } else {
201 waddch(win, ' ');
203 j += rt->cells[i][j].fcount;
204 #else
205 waddch(win, rt->cells[i][j].ch);
206 j++;
207 #endif
211 wmove(win, srow + rt->crow, scol + rt->ccol);
214 #endif
216 #ifdef USE_PTY
218 pid_t rote_vt_forkpty(RoteTerm *rt, const char *command) {
219 struct winsize ws;
220 pid_t childpid;
222 ws.ws_row = rt->rows;
223 ws.ws_col = rt->cols;
224 ws.ws_xpixel = ws.ws_ypixel = 0;
226 childpid = forkpty(&rt->pd->pty, NULL, NULL, &ws);
227 if (childpid < 0) return -1;
229 if (childpid == 0) {
230 /* we are the child, running under the slave side of the pty. */
232 /* Cajole application into using linux-console-compatible escape
233 * sequences (which is what we are prepared to interpret) */
234 setenv("TERM", "linux", 1);
236 /* Now we will exec /bin/sh -c command. */
237 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
239 fprintf(stderr, "\nexecl() failed.\nCommand: '%s'\n", command);
240 exit(127); /* error exec'ing */
243 /* if we got here we are the parent process */
244 rt->childpid = childpid;
245 return childpid;
248 void rote_vt_forsake_child(RoteTerm *rt) {
249 if (rt->pd->pty >= 0) close(rt->pd->pty);
250 rt->pd->pty = -1;
251 rt->childpid = 0;
254 #endif
256 void rote_vt_update(RoteTerm *rt) {
257 fd_set ifs;
258 struct timeval tvzero;
259 char buf[512];
260 int bytesread;
261 int n = ROTE_VT_UPDATE_ITERATIONS;
262 if (rt->pd->pty < 0) return; /* nothing to pump */
264 while (n--) { /* iterate at most ROVE_VT_UPDATE_ITERATIONS times.
265 * As Phil Endecott pointed out, if we don't restrict this,
266 * a program that floods the terminal with output
267 * could cause this loop to iterate forever, never
268 * being able to catch up. So we'll rely on the client
269 * calling rote_vt_update often, as the documentation
270 * recommends :-) */
272 /* check if pty has something to say */
273 FD_ZERO(&ifs); FD_SET(rt->pd->pty, &ifs);
274 tvzero.tv_sec = 0; tvzero.tv_usec = 0;
276 if (select(rt->pd->pty + 1, &ifs, NULL, NULL, &tvzero) <= 0)
277 return; /* nothing to read, or select() failed */
279 /* read what we can. This is guaranteed not to block, since
280 * select() told us there was something to read. */
281 bytesread = read(rt->pd->pty, buf, 512);
282 if (bytesread <= 0) return;
284 /* inject the data into the terminal */
285 rote_vt_inject(rt, buf, bytesread);
289 void rote_vt_write(RoteTerm *rt, const char *data, int len) {
290 if (rt->pd->pty < 0) {
291 /* no pty, so just inject the data plain and simple */
292 rote_vt_inject(rt, data, len);
293 return;
296 /* write data to pty. Keep calling write() until we have written
297 * everything. */
298 while (len > 0) {
299 int byteswritten = write(rt->pd->pty, data, len);
300 if (byteswritten < 0) {
301 /* very ugly way to inform the error. Improvements welcome! */
302 static char errormsg[] = "\n(ROTE: pty write() error)\n";
303 rote_vt_inject(rt, errormsg, strlen(errormsg));
304 return;
307 data += byteswritten;
308 len -= byteswritten;
312 void rote_vt_install_handler(RoteTerm *rt, rote_es_handler_t handler) {
313 rt->pd->handler = handler;
316 void *rote_vt_take_snapshot(RoteTerm *rt) {
317 int i;
318 int bytes_per_row = sizeof(RoteCell) * rt->cols;
319 void *buf = malloc(bytes_per_row * rt->rows);
320 void *ptr = buf;
322 for (i = 0; i < rt->rows; i++, ptr += bytes_per_row)
323 memcpy(ptr, rt->cells[i], bytes_per_row);
325 return buf;
328 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf) {
329 int i;
330 int bytes_per_row = sizeof(RoteCell) * rt->cols;
332 for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) {
333 rt->line_dirty[i] = true;
334 memcpy(rt->cells[i], snapbuf, bytes_per_row);
338 int rote_vt_get_pty_fd(RoteTerm *rt) {
339 return rt->pd->pty;