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