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