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"
32 #include <sys/ioctl.h>
34 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
38 #define ROTE_VT_UPDATE_ITERATIONS 5
40 RoteTerm
*rote_vt_create(int rows
, int cols
) {
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 */
53 /* default mode is replace */
56 /* create the cell matrix */
57 rt
->cells
= (RoteCell
**) malloc(sizeof(RoteCell
*) * rt
->rows
);
58 for (i
= 0; i
< rt
->rows
; i
++) {
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;
85 fprintf(stderr
, "Created a %d x %d terminal.\n", rt
->rows
, rt
->cols
);
91 RoteTerm
*rote_vt_resize(RoteTerm
*rt
,int rows
, int cols
){
93 if (rows
<= 0 || cols
<= 0) 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
++) {
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
++)
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 */
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;
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 */
148 kill(rt
->childpid
,SIGWINCH
);
150 fprintf(stderr
, "Resized a %d x %d terminal.\n", rt
->rows
, rt
->cols
);
155 void rote_vt_destroy(RoteTerm
*rt
) {
160 free(rt
->line_dirty
);
161 for (i
= 0; i
< rt
->rows
; i
++) free(rt
->cells
[i
]);
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
);
181 void rote_vt_draw(RoteTerm
*rt
, WINDOW
*win
, int srow
, int scol
,
182 void (*cur_set_attr
)(WINDOW
*,unsigned char)) {
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
);
198 if (!rt
->cells
[i
][j
].empty
) {
199 wstr
[0] = rt
->cells
[i
][j
].ch
;
204 j
+= rt
->cells
[i
][j
].fcount
;
206 waddch(win
, rt
->cells
[i
][j
].ch
);
212 wmove(win
, srow
+ rt
->crow
, scol
+ rt
->ccol
);
219 pid_t
rote_vt_forkpty(RoteTerm
*rt
, const char *command
) {
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;
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
;
249 void rote_vt_forsake_child(RoteTerm
*rt
) {
250 if (rt
->pd
->pty
>= 0) close(rt
->pd
->pty
);
257 void rote_vt_update(RoteTerm
*rt
) {
259 struct timeval tvzero
;
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
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
);
297 /* write data to pty. Keep calling write() until we have written
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
));
308 data
+= 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
) {
319 int bytes_per_row
= sizeof(RoteCell
) * rt
->cols
;
320 void *buf
= malloc(bytes_per_row
* rt
->rows
);
323 for (i
= 0; i
< rt
->rows
; i
++, ptr
+= bytes_per_row
)
324 memcpy(ptr
, rt
->cells
[i
], bytes_per_row
);
329 void rote_vt_restore_snapshot(RoteTerm
*rt
, void *snapbuf
) {
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
) {