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