4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.4 2008/02/13 16:57:29 james
14 * *** empty log message ***
16 * Revision 1.3 2008/02/13 09:12:21 james
17 * *** empty log message ***
19 * Revision 1.2 2008/02/13 01:08:18 james
20 * *** empty log message ***
22 * Revision 1.1 2008/02/12 22:36:46 james
23 * *** empty log message ***
25 * Revision 1.1 2008/02/09 15:47:28 james
26 * *** empty log message ***
28 * Revision 1.2 2008/02/07 11:11:14 staffcvs
29 * *** empty log message ***
31 * Revision 1.1 2008/02/07 01:02:52 james
32 * *** empty log message ***
34 * Revision 1.3 2008/02/06 17:53:28 james
35 * *** empty log message ***
37 * Revision 1.2 2008/02/04 02:05:06 james
38 * *** empty log message ***
40 * Revision 1.1 2008/02/04 01:32:39 james
41 * *** empty log message ***
48 typedef struct TERMINAL_struct
51 struct termios orig_termios;
52 struct TERMINAL_struct *next;
56 static TERMINAL *terminal_list = NULL;
62 terminal_close (TTY * _t)
67 TERMINAL *t = (TERMINAL *) _t;
68 TERMINAL **ptr = &terminal_list;
73 /* Take out of cleanup list */
74 while (*ptr && (*ptr != t))
75 ptr = &((*ptr)->next);
80 tcsetattr (t->wfd, TCSANOW, &t->orig_termios);
82 set_nonblocking (t->wfd);
85 t->xmit (t, "\033[r", 3);
86 t->xmit (t, "\033[0m", 4);
87 i = sprintf (buf, "\033[%d;%dH", CRT_ROWS + 1, 1);
90 set_blocking (t->rfd);
91 set_blocking (t->wfd);
98 terminal_atexit (void)
100 while (terminal_list)
101 terminal_close ((TTY *) terminal_list);
119 terminal_getsize (TTY * _t)
121 TERMINAL *t = (TERMINAL *) _t;
122 struct winsize sz = { 0 };
127 if (ioctl (t->wfd, TIOCGWINSZ, &sz))
129 t->size.x = CRT_COLS;
130 t->size.y = CRT_ROWS;
134 t->size.x = sz.ws_col;
135 t->size.y = sz.ws_row;
141 terminal_dispatch (void)
146 if (!terminal_winches)
149 terminal_winches = 0;
151 for (t = terminal_list; t; t = t->next)
152 terminal_getsize (t);
158 terminal_read (TTY * _t, void *buf, int len)
160 TERMINAL *t = (TERMINAL *) _t;
163 terminal_dispatch ();
164 set_nonblocking (t->rfd);
169 red = wrap_read (t->rfd, buf, len);
187 terminal_write (TTY * _t, void *buf, int len)
190 TERMINAL *t = (TERMINAL *) _t;
192 terminal_dispatch ();
194 set_blocking (t->wfd);
199 writ = wrap_write (t->wfd, buf, len);
218 terminal_register_handlers (void)
220 struct sigaction sa = { 0 };
222 sa.sa_handler = sigwinch;
223 sa.sa_flags = SA_RESTART;
224 sigaction (SIGWINCH, &sa, NULL);
226 sa.sa_handler = sigint;
227 sa.sa_flags = SA_RESTART;
228 sigaction (SIGINT, &sa, NULL);
232 terminal_open (int rfd, int wfd)
237 struct termios termios;
239 t = (TERMINAL *) malloc (sizeof (TERMINAL));
244 tcgetattr (wfd, &t->orig_termios);
246 t->next = terminal_list;
249 tcgetattr (wfd, &termios);
251 set_nonblocking (rfd);
252 set_nonblocking (wfd);
255 cfmakeraw (&termios);
256 //raw_termios (&termios);
258 tcsetattr (wfd, TCSANOW, &termios);
260 t->recv = terminal_read;
261 t->xmit = terminal_write;
262 t->close = terminal_close;
265 terminal_getsize ((TTY *) t);