4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.2 2008/02/13 01:08:18 james
14 * *** empty log message ***
16 * Revision 1.1 2008/02/12 22:36:46 james
17 * *** empty log message ***
19 * Revision 1.1 2008/02/09 15:47:28 james
20 * *** empty log message ***
22 * Revision 1.2 2008/02/07 11:11:14 staffcvs
23 * *** empty log message ***
25 * Revision 1.1 2008/02/07 01:02:52 james
26 * *** empty log message ***
28 * Revision 1.3 2008/02/06 17:53:28 james
29 * *** empty log message ***
31 * Revision 1.2 2008/02/04 02:05:06 james
32 * *** empty log message ***
34 * Revision 1.1 2008/02/04 01:32:39 james
35 * *** empty log message ***
42 typedef struct TERMINAL_struct
45 struct termios orig_termios;
46 struct TERMINAL_struct *next;
50 static TERMINAL terminal_list=NULL;
56 terminal_close (TTY * _t)
58 TERMINAL *t = (TERMINAL *) _t;
59 TERMINAL **ptr=&terminal_list;
64 /* Take out of cleanup list */
65 while (*ptr && (*ptr != t)) ptr=&((*ptr)->next);
70 tcsetattr(t->wfd,TCSANOW,&t->orig_termios);
79 void terminal_atexit(void)
82 terminal_close(terminal_list);
85 static void sigint(int dummy)
99 terminal_getsize (TTY *_t)
101 TERMINAL *t=(TTY *) _t;
102 struct winsize sz = { 0 };
106 if (ioctl (a->wfd, TIOCGWINSZ, &sz))
108 t->size.x = CRT_COLS;
109 t->size.y = CRT_ROWS;
113 t->size.x = sz.ws_col;
114 t->size.y = sz.ws_row;
119 void terminal_dispatch(void)
124 if (!terminal_winches) return;
128 for (t=terminal_list;t;t=t->next)
135 terminal_read (TTY * _t, void *buf, int len)
137 TERMINAL *t = (TERMINAL *) _t;
145 red = wrap_read (t->fd, buf, len);
163 terminal_write (TTY * _t, void *buf, int len)
166 TERMINAL *t = (TERMINAL *) _t;
173 writ = wrap_write (t->fd, buf, len);
190 void terminal_register_handlers(void)
192 struct sigaction sa = { 0 };
194 sa.sa_handler = sigwinch;
195 sa.sa_flags = SA_RESTART;
196 sigaction (SIGWINCH, &sa, NULL);
198 sa.sa_handler = sigint;
199 sa.sa_flags = SA_RESTART;
200 sigaction (SIGINT, &sa, NULL);
204 terminal_open (int rfd,int wfd)
209 struct termios termios;
211 t = (TERMINAL *) malloc (sizeof (TERMINAL));
216 tcgetattr(wfd,&t->orig_termios);
218 t->next=terminal_list;
221 tcgetattr(tfd,&termios);
223 set_nonblocking (rfd);
224 set_nonblocking (wfd);
227 raw_termios (&termios);
229 tcsetattr(wfd,TCSANOW,&termios);
231 t->read = terminal_read;
232 t->write = terminal_write;
233 t->close = terminal_close;
236 terminal_getsize((TTY*) t);