4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.5 2008/02/13 18:05:06 james
14 * *** empty log message ***
16 * Revision 1.4 2008/02/13 16:57:29 james
17 * *** empty log message ***
19 * Revision 1.3 2008/02/13 09:12:21 james
20 * *** empty log message ***
22 * Revision 1.2 2008/02/13 01:08:18 james
23 * *** empty log message ***
25 * Revision 1.1 2008/02/12 22:36:46 james
26 * *** empty log message ***
28 * Revision 1.1 2008/02/09 15:47:28 james
29 * *** empty log message ***
31 * Revision 1.2 2008/02/07 11:11:14 staffcvs
32 * *** empty log message ***
34 * Revision 1.1 2008/02/07 01:02:52 james
35 * *** empty log message ***
37 * Revision 1.3 2008/02/06 17:53:28 james
38 * *** empty log message ***
40 * Revision 1.2 2008/02/04 02:05:06 james
41 * *** empty log message ***
43 * Revision 1.1 2008/02/04 01:32:39 james
44 * *** empty log message ***
51 typedef struct TERMINAL_struct
54 struct termios orig_termios;
55 struct TERMINAL_struct *next;
59 static TERMINAL *terminal_list = NULL;
65 terminal_close (TTY * _t)
70 TERMINAL *t = (TERMINAL *) _t;
71 TERMINAL **ptr = &terminal_list;
76 /* Take out of cleanup list */
77 while (*ptr && (*ptr != t))
78 ptr = &((*ptr)->next);
83 tcsetattr (t->wfd, TCSANOW, &t->orig_termios);
85 set_nonblocking (t->wfd);
88 t->xmit (_t, "\033[r", 3);
89 t->xmit (_t, "\033[0m", 4);
90 i = sprintf (buf, "\033[%d;%dH", CRT_ROWS + 1, 1);
92 t->xmit (_t, "\033[J", 3);
94 set_blocking (t->rfd);
95 set_blocking (t->wfd);
102 terminal_atexit (void)
104 while (terminal_list)
105 terminal_close ((TTY *) terminal_list);
123 terminal_getsize (TTY * _t)
125 TERMINAL *t = (TERMINAL *) _t;
126 struct winsize sz = { 0 };
131 if (ioctl (t->wfd, TIOCGWINSZ, &sz))
133 t->size.x = CRT_COLS;
134 t->size.y = CRT_ROWS;
138 t->size.x = sz.ws_col;
139 t->size.y = sz.ws_row;
145 terminal_dispatch (void)
150 if (!terminal_winches)
153 terminal_winches = 0;
155 for (t = terminal_list; t; t = t->next)
156 terminal_getsize ((TTY *)t);
162 terminal_read (TTY * _t, void *buf, int len)
164 TERMINAL *t = (TERMINAL *) _t;
167 terminal_dispatch ();
168 set_nonblocking (t->rfd);
173 red = wrap_read (t->rfd, buf, len);
191 terminal_write (TTY * _t, void *buf, int len)
194 TERMINAL *t = (TERMINAL *) _t;
196 terminal_dispatch ();
198 set_blocking (t->wfd);
203 writ = wrap_write (t->wfd, buf, len);
222 terminal_register_handlers (void)
224 struct sigaction sa = { 0 };
226 sa.sa_handler = sigwinch;
227 sa.sa_flags = SA_RESTART;
228 sigaction (SIGWINCH, &sa, NULL);
230 sa.sa_handler = sigint;
231 sa.sa_flags = SA_RESTART;
232 sigaction (SIGINT, &sa, NULL);
236 terminal_open (int rfd, int wfd)
241 struct termios termios;
243 t = (TERMINAL *) malloc (sizeof (TERMINAL));
248 tcgetattr (wfd, &t->orig_termios);
250 t->next = terminal_list;
253 tcgetattr (wfd, &termios);
255 set_nonblocking (rfd);
256 set_nonblocking (wfd);
259 cfmakeraw (&termios);
260 //raw_termios (&termios);
262 tcsetattr (wfd, TCSANOW, &termios);
264 t->recv = terminal_read;
265 t->xmit = terminal_write;
266 t->close = terminal_close;
269 terminal_getsize ((TTY *) t);