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
23 #include "roteprivate.h"
29 #include <sys/ioctl.h>
31 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
33 #elif defined(__linux__) && defined(USE_PTY)
37 #define ROTE_VT_UPDATE_ITERATIONS 5
39 RoteTerm
*rote_vt_create(int rows
, int cols
) {
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 */
52 /* default mode is replace */
55 /* create the cell matrix */
56 rt
->cells
= (RoteCell
**) malloc(sizeof(RoteCell
*) * rt
->rows
);
57 for (i
= 0; i
< rt
->rows
; i
++) {
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;
84 fprintf(stderr
, "Created a %d x %d terminal.\n", rt
->rows
, rt
->cols
);
90 RoteTerm
*rote_vt_resize(RoteTerm
*rt
,int rows
, int cols
){
92 if (rows
<= 0 || cols
<= 0) 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
++) {
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
++)
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 */
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;
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 */
147 kill(rt
->childpid
,SIGWINCH
);
149 fprintf(stderr
, "Resized a %d x %d terminal.\n", rt
->rows
, rt
->cols
);
154 void rote_vt_destroy(RoteTerm
*rt
) {
159 free(rt
->line_dirty
);
160 for (i
= 0; i
< rt
->rows
; i
++) free(rt
->cells
[i
]);
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
);
180 void rote_vt_draw(RoteTerm
*rt
, WINDOW
*win
, int srow
, int scol
,
181 void (*cur_set_attr
)(WINDOW
*,unsigned char)) {
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
);
197 if (!rt
->cells
[i
][j
].empty
) {
198 wstr
[0] = rt
->cells
[i
][j
].ch
;
203 j
+= rt
->cells
[i
][j
].fcount
;
205 waddch(win
, rt
->cells
[i
][j
].ch
);
211 wmove(win
, srow
+ rt
->crow
, scol
+ rt
->ccol
);
218 pid_t
rote_vt_forkpty(RoteTerm
*rt
, const char *command
) {
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;
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
;
248 void rote_vt_forsake_child(RoteTerm
*rt
) {
249 if (rt
->pd
->pty
>= 0) close(rt
->pd
->pty
);
256 void rote_vt_update(RoteTerm
*rt
) {
258 struct timeval tvzero
;
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
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
);
296 /* write data to pty. Keep calling write() until we have written
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
));
307 data
+= 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
) {
318 int bytes_per_row
= sizeof(RoteCell
) * rt
->cols
;
319 void *buf
= malloc(bytes_per_row
* rt
->rows
);
322 for (i
= 0; i
< rt
->rows
; i
++, ptr
+= bytes_per_row
)
323 memcpy(ptr
, rt
->cells
[i
], bytes_per_row
);
328 void rote_vt_restore_snapshot(RoteTerm
*rt
, void *snapbuf
) {
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
) {