4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.7 2008/02/14 01:55:57 james
14 * *** empty log message ***
16 * Revision 1.6 2008/02/14 00:57:58 james
17 * *** empty log message ***
19 * Revision 1.5 2008/02/13 18:05:06 james
20 * *** empty log message ***
22 * Revision 1.4 2008/02/13 16:57:29 james
23 * *** empty log message ***
25 * Revision 1.3 2008/02/13 09:12:21 james
26 * *** empty log message ***
28 * Revision 1.2 2008/02/13 01:08:18 james
29 * *** empty log message ***
31 * Revision 1.1 2008/02/12 22:36:46 james
32 * *** empty log message ***
34 * Revision 1.1 2008/02/09 15:47:28 james
35 * *** empty log message ***
37 * Revision 1.2 2008/02/07 11:11:14 staffcvs
38 * *** empty log message ***
40 * Revision 1.1 2008/02/07 01:02:52 james
41 * *** empty log message ***
43 * Revision 1.3 2008/02/06 17:53:28 james
44 * *** empty log message ***
46 * Revision 1.2 2008/02/04 02:05:06 james
47 * *** empty log message ***
49 * Revision 1.1 2008/02/04 01:32:39 james
50 * *** empty log message ***
57 typedef struct TERMINAL_struct
60 struct termios orig_termios;
61 struct TERMINAL_struct *next;
65 static TERMINAL *terminal_list = NULL;
71 terminal_close (TTY * _t)
76 TERMINAL *t = (TERMINAL *) _t;
77 TERMINAL **ptr = &terminal_list;
82 /* Take out of cleanup list */
83 while (*ptr && (*ptr != t))
84 ptr = &((*ptr)->next);
89 tcsetattr (t->wfd, TCSANOW, &t->orig_termios);
91 set_nonblocking (t->wfd);
94 t->xmit (_t, "\033[r", 3);
95 t->xmit (_t, "\033[0m", 4);
96 i = sprintf (buf, "\033[%d;%dH", CRT_ROWS + 1, 1);
98 t->xmit (_t, "\033[J", 3);
100 set_blocking (t->rfd);
101 set_blocking (t->wfd);
108 terminal_atexit (void)
110 while (terminal_list)
111 terminal_close ((TTY *) terminal_list);
129 terminal_getsize (TTY * _t)
131 TERMINAL *t = (TERMINAL *) _t;
132 struct winsize sz = { 0 };
137 if (ioctl (t->wfd, TIOCGWINSZ, &sz))
139 t->size.x = CRT_COLS;
140 t->size.y = CRT_ROWS;
144 t->size.x = sz.ws_col;
145 t->size.y = sz.ws_row;
151 terminal_dispatch (void)
156 if (!terminal_winches)
159 terminal_winches = 0;
161 for (t = terminal_list; t; t = t->next)
162 terminal_getsize ((TTY *) t);
168 terminal_read (TTY * _t, void *buf, int len)
170 TERMINAL *t = (TERMINAL *) _t;
173 terminal_dispatch ();
174 set_nonblocking (t->rfd);
179 red = wrap_read (t->rfd, buf, len);
197 terminal_write (TTY * _t, void *buf, int len)
200 TERMINAL *t = (TERMINAL *) _t;
202 terminal_dispatch ();
204 set_blocking (t->wfd);
209 writ = wrap_write (t->wfd, buf, len);
228 terminal_register_handlers (void)
230 struct sigaction sa = { 0 };
232 sa.sa_handler = sigwinch;
233 sa.sa_flags = SA_RESTART;
234 sigaction (SIGWINCH, &sa, NULL);
236 sa.sa_handler = sigint;
237 sa.sa_flags = SA_RESTART;
238 sigaction (SIGINT, &sa, NULL);
243 terminal_open (int rfd, int wfd)
248 struct termios termios;
250 t = (TERMINAL *) malloc (sizeof (TERMINAL));
255 tcgetattr (wfd, &t->orig_termios);
257 t->next = terminal_list;
260 tcgetattr (wfd, &termios);
262 set_nonblocking (rfd);
263 set_nonblocking (wfd);
266 cfmakeraw (&termios);
267 //raw_termios (&termios);
269 tcsetattr (wfd, TCSANOW, &termios);
271 t->recv = terminal_read;
272 t->xmit = terminal_write;
273 t->close = terminal_close;
276 terminal_getsize ((TTY *) t);