chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / terminal.c
1 /*
2  * terminal.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.5  2008/02/13 18:05:06  james
14  * *** empty log message ***
15  *
16  * Revision 1.4  2008/02/13 16:57:29  james
17  * *** empty log message ***
18  *
19  * Revision 1.3  2008/02/13 09:12:21  james
20  * *** empty log message ***
21  *
22  * Revision 1.2  2008/02/13 01:08:18  james
23  * *** empty log message ***
24  *
25  * Revision 1.1  2008/02/12 22:36:46  james
26  * *** empty log message ***
27  *
28  * Revision 1.1  2008/02/09 15:47:28  james
29  * *** empty log message ***
30  *
31  * Revision 1.2  2008/02/07 11:11:14  staffcvs
32  * *** empty log message ***
33  *
34  * Revision 1.1  2008/02/07 01:02:52  james
35  * *** empty log message ***
36  *
37  * Revision 1.3  2008/02/06 17:53:28  james
38  * *** empty log message ***
39  *
40  * Revision 1.2  2008/02/04 02:05:06  james
41  * *** empty log message ***
42  *
43  * Revision 1.1  2008/02/04 01:32:39  james
44  * *** empty log message ***
45  *
46  */
47
48 #include "project.h"
49
50
51 typedef struct TERMINAL_struct
52 {
53   TTY_SIGNATURE;
54   struct termios orig_termios;
55   struct TERMINAL_struct *next;
56 } TERMINAL;
57
58
59 static TERMINAL *terminal_list = NULL;
60 int terminal_winches;
61
62
63
64 static void
65 terminal_close (TTY * _t)
66 {
67   char buf[32];
68   int i;
69
70   TERMINAL *t = (TERMINAL *) _t;
71   TERMINAL **ptr = &terminal_list;
72
73   if (!t)
74     return;
75
76   /* Take out of cleanup list */
77   while (*ptr && (*ptr != t))
78     ptr = &((*ptr)->next);
79
80   if (*ptr)
81     *ptr = t->next;
82
83   tcsetattr (t->wfd, TCSANOW, &t->orig_termios);
84
85   set_nonblocking (t->wfd);
86
87
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);
91   t->xmit (_t, buf, i);
92   t->xmit (_t, "\033[J", 3);
93
94   set_blocking (t->rfd);
95   set_blocking (t->wfd);
96
97   free (t);
98 }
99
100
101 void
102 terminal_atexit (void)
103 {
104   while (terminal_list)
105     terminal_close ((TTY *) terminal_list);
106 }
107
108 static void
109 sigint (int dummy)
110 {
111   terminal_atexit ();
112   exit (-1);
113 }
114
115 static void
116 sigwinch (int not)
117 {
118   terminal_winches++;
119 }
120
121
122 void
123 terminal_getsize (TTY * _t)
124 {
125   TERMINAL *t = (TERMINAL *) _t;
126   struct winsize sz = { 0 };
127
128   if (!t)
129     return;
130
131   if (ioctl (t->wfd, TIOCGWINSZ, &sz))
132     {
133       t->size.x = CRT_COLS;
134       t->size.y = CRT_ROWS;
135     }
136   else
137     {
138       t->size.x = sz.ws_col;
139       t->size.y = sz.ws_row;
140     }
141 }
142
143
144 void
145 terminal_dispatch (void)
146 {
147   TERMINAL *t;
148
149
150   if (!terminal_winches)
151     return;
152
153   terminal_winches = 0;
154
155   for (t = terminal_list; t; t = t->next)
156     terminal_getsize ((TTY *)t);
157
158 }
159
160
161 static int
162 terminal_read (TTY * _t, void *buf, int len)
163 {
164   TERMINAL *t = (TERMINAL *) _t;
165   int red, done = 0;
166
167   terminal_dispatch ();
168   set_nonblocking (t->rfd);
169
170   do
171     {
172
173       red = wrap_read (t->rfd, buf, len);
174       if (red < 0)
175         return -1;
176       if (!red)
177         return done;
178
179       buf += red;
180       len -= red;
181       done += red;
182     }
183   while (len);
184
185
186   return done;
187 }
188
189
190 static int
191 terminal_write (TTY * _t, void *buf, int len)
192 {
193   int writ, done = 0;
194   TERMINAL *t = (TERMINAL *) _t;
195
196   terminal_dispatch ();
197
198   set_blocking (t->wfd);
199
200   do
201     {
202
203       writ = wrap_write (t->wfd, buf, len);
204       if (writ < 0)
205         return -1;
206
207       if (!writ)
208         usleep (1000);
209
210       buf += writ;
211       len -= writ;
212       done += writ;
213     }
214   while (len);
215
216
217   return done;
218 }
219
220
221 void
222 terminal_register_handlers (void)
223 {
224   struct sigaction sa = { 0 };
225
226   sa.sa_handler = sigwinch;
227   sa.sa_flags = SA_RESTART;
228   sigaction (SIGWINCH, &sa, NULL);
229
230   sa.sa_handler = sigint;
231   sa.sa_flags = SA_RESTART;
232   sigaction (SIGINT, &sa, NULL);
233 }
234
235 TTY *
236 terminal_open (int rfd, int wfd)
237 {
238   TERMINAL *t;
239   pid_t child;
240   char name[1024];
241   struct termios termios;
242
243   t = (TERMINAL *) malloc (sizeof (TERMINAL));
244
245   t->rfd = rfd;
246   t->wfd = wfd;
247
248   tcgetattr (wfd, &t->orig_termios);
249
250   t->next = terminal_list;
251   terminal_list = t;
252
253   tcgetattr (wfd, &termios);
254
255   set_nonblocking (rfd);
256   set_nonblocking (wfd);
257
258
259   cfmakeraw (&termios);
260   //raw_termios (&termios);
261
262   tcsetattr (wfd, TCSANOW, &termios);
263
264   t->recv = terminal_read;
265   t->xmit = terminal_write;
266   t->close = terminal_close;
267
268
269   terminal_getsize ((TTY *) t);
270
271   return (TTY *) t;
272 }