chiark / gitweb /
de1bdcaf6b4b096a07ff1ac7c84f2c7eaafb0f77
[sympathy.git] / src / tty.c
1 /*
2  * tty.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.10  2008/02/15 23:52:12  james
14  * *** empty log message ***
15  *
16  * Revision 1.9  2008/02/15 03:32:07  james
17  * *** empty log message ***
18  *
19  * Revision 1.8  2008/02/14 10:36:18  james
20  * *** empty log message ***
21  *
22  * Revision 1.7  2008/02/14 10:34:30  james
23  * *** empty log message ***
24  *
25  * Revision 1.6  2008/02/13 16:59:34  james
26  * *** empty log message ***
27  *
28  * Revision 1.5  2008/02/13 16:57:29  james
29  * *** empty log message ***
30  *
31  * Revision 1.4  2008/02/12 22:36:46  james
32  * *** empty log message ***
33  *
34  * Revision 1.3  2008/02/09 15:47:28  james
35  * *** empty log message ***
36  *
37  */
38
39
40 #include "project.h"
41
42 static int
43 speed_t_to_baud (speed_t s)
44 {
45   switch (s)
46     {
47 #ifdef B0
48     case B0:
49       return 0;
50 #endif
51 #ifdef B50
52     case B50:
53       return 50;
54 #endif
55 #ifdef B75
56     case B75:
57       return 75;
58 #endif
59 #ifdef B110
60     case B110:
61       return 110;
62 #endif
63 #ifdef B134
64     case B134:
65       return 134;
66 #endif
67 #ifdef B150
68     case B150:
69       return 150;
70 #endif
71 #ifdef B200
72     case B200:
73       return 200;
74 #endif
75 #ifdef B300
76     case B300:
77       return 300;
78 #endif
79 #ifdef B600
80     case B600:
81       return 600;
82 #endif
83 #ifdef B1200
84     case B1200:
85       return 1200;
86 #endif
87 #ifdef B1800
88     case B1800:
89       return 1800;
90 #endif
91 #ifdef B2400
92     case B2400:
93       return 2400;
94 #endif
95 #ifdef B4800
96     case B4800:
97       return 4800;
98 #endif
99 #ifdef B9600
100     case B9600:
101       return 9600;
102 #endif
103 #ifdef B19200
104     case B19200:
105       return 19200;
106 #endif
107 #ifdef B38400
108     case B38400:
109       return 38400;
110 #endif
111 #ifdef B57600
112     case B57600:
113       return 57600;
114 #endif
115 #ifdef B115200
116     case B115200:
117       return 115200;
118 #endif
119 #ifdef B230400
120     case B230400:
121       return 230400;
122 #endif
123     }
124
125   return -1;
126 }
127
128 static speed_t
129 baud_to_speed_t (int baud)
130 {
131   switch (baud)
132     {
133 #ifdef B0
134     case 0:
135       return B0;
136 #endif
137 #ifdef B50
138     case 50:
139       return B50;
140 #endif
141 #ifdef B75
142     case 75:
143       return B75;
144 #endif
145 #ifdef B110
146     case 110:
147       return B110;
148 #endif
149 #ifdef B134
150     case 134:
151       return B134;
152 #endif
153 #ifdef B150
154     case 150:
155       return B150;
156 #endif
157 #ifdef B200
158     case 200:
159       return B200;
160 #endif
161 #ifdef B300
162     case 300:
163       return B300;
164 #endif
165 #ifdef B600
166     case 600:
167       return B600;
168 #endif
169 #ifdef B1200
170     case 1200:
171       return B1200;
172 #endif
173 #ifdef B1800
174     case 1800:
175       return B1800;
176 #endif
177 #ifdef B2400
178     case 2400:
179       return B2400;
180 #endif
181 #ifdef B4800
182     case 4800:
183       return B4800;
184 #endif
185 #ifdef B9600
186     case 9600:
187       return B9600;
188 #endif
189 #ifdef B19200
190     case 19200:
191       return B19200;
192 #endif
193 #ifdef B38400
194     case 38400:
195       return B38400;
196 #endif
197 #ifdef B57600
198     case 57600:
199       return B57600;
200 #endif
201 #ifdef B115200
202     case 115200:
203       return B115200;
204 #endif
205 #ifdef B230400
206     case 230400:
207       return B230400;
208 #endif
209     }
210   return -1;
211 }
212
213 void
214 tty_pre_select (TTY * t, fd_set * rfds, fd_set * wfds)
215 {
216   int line;
217   struct timeval now, dif;
218
219   if (t->hanging_up)
220     {
221
222       gettimeofday (&now, NULL);
223       timersub (&now, &t->hangup_clock, &dif);
224       if (dif.tv_sec)
225         {
226           fprintf (stderr, "+DTR\n");
227
228           line = TIOCM_DTR;
229           ioctl (t->rfd, TIOCMBIS, &line);
230           t->hanging_up = 0;
231         }
232     }
233
234
235   FD_SET (t->rfd, rfds);
236 }
237
238 int
239 tty_get_status (TTY * t, TTY_Status * s)
240 {
241
242   s->lines = 0;
243   ioctl (t->rfd, TIOCMGET, &s->lines);
244   if (t->hanging_up)
245     fprintf (stderr, "s->lines & TIOCM_DTR=%x\n", s->lines & TIOCM_DTR);
246
247   if (tcgetattr (t->rfd, &s->termios))
248     return -1;
249
250   s->baud = speed_t_to_baud (cfgetispeed (&s->termios));
251   s->blocked = t->blocked;
252
253   return 0;
254 }
255
256 void
257 tty_set_baud (TTY * t, int rate)
258 {
259   struct termios tios = { 0 };
260
261   speed_t s = baud_to_speed_t (rate);
262
263   if (s == (speed_t) - 1)
264     return;
265
266   if (tcgetattr (t->rfd, &tios))
267     return;
268
269   cfsetispeed (&tios, s);
270   cfsetospeed (&tios, s);
271
272   tcsetattr (t->rfd, TCSANOW, &tios);
273 }
274
275 void
276 tty_send_break (TTY * t)
277 {
278   tcsendbreak (t->wfd, 0);
279 }
280
281 void
282 tty_set_flow (TTY * t, int flow)
283 {
284   struct termios tios = { 0 };
285
286   if (tcgetattr (t->rfd, &tios))
287     return;
288
289   if (flow)
290     tios.c_cflag |= CRTSCTS;
291   else
292     tios.c_cflag &= ~CRTSCTS;
293
294   tcsetattr (t->rfd, TCSANOW, &tios);
295
296 }
297
298 void
299 tty_hangup (TTY * t)
300 {
301   int line;
302
303   line = TIOCM_DTR;
304   ioctl (t->rfd, TIOCMBIC, &line);
305
306   t->hanging_up = 1;
307   gettimeofday (&t->hangup_clock, NULL);
308   fprintf (stderr, "-DTR\n");
309
310 }
311
312
313 #if 0
314 int
315 tty_post_select (Context * c, fd_set * rfds, fd_set * wfds)
316 {
317
318   if (FD_ISSET (c->t->rfd, rfds))
319     {
320       if (vt102_dispatch (&c))
321         return -1;
322     }
323   return 0;
324 }
325
326 #endif