4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.8 2008/02/14 10:39:14 james
14 * *** empty log message ***
16 * Revision 1.7 2008/02/14 01:55:57 james
17 * *** empty log message ***
19 * Revision 1.6 2008/02/14 00:57:58 james
20 * *** empty log message ***
22 * Revision 1.5 2008/02/13 18:05:06 james
23 * *** empty log message ***
25 * Revision 1.4 2008/02/13 16:57:29 james
26 * *** empty log message ***
28 * Revision 1.3 2008/02/13 09:12:21 james
29 * *** empty log message ***
31 * Revision 1.2 2008/02/13 01:08:18 james
32 * *** empty log message ***
34 * Revision 1.1 2008/02/12 22:36:46 james
35 * *** empty log message ***
37 * Revision 1.1 2008/02/09 15:47:28 james
38 * *** empty log message ***
40 * Revision 1.2 2008/02/07 11:11:14 staffcvs
41 * *** empty log message ***
43 * Revision 1.1 2008/02/07 01:02:52 james
44 * *** empty log message ***
46 * Revision 1.3 2008/02/06 17:53:28 james
47 * *** empty log message ***
49 * Revision 1.2 2008/02/04 02:05:06 james
50 * *** empty log message ***
52 * Revision 1.1 2008/02/04 01:32:39 james
53 * *** empty log message ***
60 typedef struct TERMINAL_struct
63 struct termios orig_termios;
64 struct TERMINAL_struct *next;
68 static TERMINAL *terminal_list = NULL;
74 terminal_close (TTY * _t)
79 TERMINAL *t = (TERMINAL *) _t;
80 TERMINAL **ptr = &terminal_list;
85 /* Take out of cleanup list */
86 while (*ptr && (*ptr != t))
87 ptr = &((*ptr)->next);
92 tcsetattr (t->wfd, TCSANOW, &t->orig_termios);
94 set_nonblocking (t->wfd);
97 t->xmit (_t, "\033[r", 3);
98 t->xmit (_t, "\033[0m", 4);
99 i = sprintf (buf, "\033[%d;%dH", CRT_ROWS + 1, 1);
100 t->xmit (_t, buf, i);
101 t->xmit (_t, "\033[J", 3);
103 set_blocking (t->rfd);
104 set_blocking (t->wfd);
111 terminal_atexit (void)
113 while (terminal_list)
114 terminal_close ((TTY *) terminal_list);
132 terminal_getsize (TTY * _t)
134 TERMINAL *t = (TERMINAL *) _t;
135 struct winsize sz = { 0 };
140 if (ioctl (t->wfd, TIOCGWINSZ, &sz))
142 t->size.x = CRT_COLS;
143 t->size.y = CRT_ROWS;
147 t->size.x = sz.ws_col;
148 t->size.y = sz.ws_row;
154 terminal_dispatch (void)
159 if (!terminal_winches)
162 terminal_winches = 0;
164 for (t = terminal_list; t; t = t->next)
165 terminal_getsize ((TTY *) t);
171 terminal_read (TTY * _t, void *buf, int len)
173 TERMINAL *t = (TERMINAL *) _t;
176 terminal_dispatch ();
177 set_nonblocking (t->rfd);
182 red = wrap_read (t->rfd, buf, len);
200 terminal_write (TTY * _t, void *buf, int len)
203 TERMINAL *t = (TERMINAL *) _t;
205 terminal_dispatch ();
207 set_blocking (t->wfd);
212 writ = wrap_write (t->wfd, buf, len);
231 terminal_register_handlers (void)
233 struct sigaction sa = { 0 };
235 sa.sa_handler = sigwinch;
236 sa.sa_flags = SA_RESTART;
237 sigaction (SIGWINCH, &sa, NULL);
239 sa.sa_handler = sigint;
240 sa.sa_flags = SA_RESTART;
241 sigaction (SIGINT, &sa, NULL);
246 terminal_open (int rfd, int wfd)
250 struct termios termios;
252 t = (TERMINAL *) malloc (sizeof (TERMINAL));
254 strcpy(t->name,"terminal");
258 tcgetattr (wfd, &t->orig_termios);
260 t->next = terminal_list;
263 tcgetattr (wfd, &termios);
265 set_nonblocking (rfd);
266 set_nonblocking (wfd);
269 cfmakeraw (&termios);
270 //raw_termios (&termios);
272 tcsetattr (wfd, TCSANOW, &termios);
274 t->recv = terminal_read;
275 t->xmit = terminal_write;
276 t->close = terminal_close;
280 terminal_getsize ((TTY *) t);