chiark / gitweb /
e3d2190420e2a95c94a8de9c4d87dee351954a91
[sympathy.git] / apps / mainloop.c
1 /*
2  * mainloop.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] =
10   "$Id$";
11
12 /*
13  * $Log$
14  * Revision 1.2  2008/02/16 10:58:52  james
15  * *** empty log message ***
16  *
17  * Revision 1.13  2008/02/15 23:52:12  james
18  * *** empty log message ***
19  *
20  * Revision 1.12  2008/02/15 03:32:07  james
21  * *** empty log message ***
22  *
23  * Revision 1.11  2008/02/14 16:21:17  james
24  * *** empty log message ***
25  *
26  * Revision 1.10  2008/02/14 10:39:14  james
27  * *** empty log message ***
28  *
29  * Revision 1.9  2008/02/14 10:34:47  james
30  * *** empty log message ***
31  *
32  * Revision 1.8  2008/02/14 10:34:30  james
33  * *** empty log message ***
34  *
35  * Revision 1.7  2008/02/14 02:46:44  james
36  * *** empty log message ***
37  *
38  * Revision 1.6  2008/02/14 00:57:58  james
39  * *** empty log message ***
40  *
41  * Revision 1.5  2008/02/13 18:05:06  james
42  * *** empty log message ***
43  *
44  * Revision 1.4  2008/02/13 17:21:55  james
45  * *** empty log message ***
46  *
47  * Revision 1.3  2008/02/08 15:06:52  james
48  * *** empty log message ***
49  *
50  * Revision 1.2  2008/02/07 15:42:49  james
51  * *** empty log message ***
52  *
53  * Revision 1.1  2008/02/05 14:25:49  james
54  * *** empty log message ***
55  *
56  */
57
58 #include <sys/time.h>
59 #include <sympathy.h>
60
61 #include "client.h"
62 #include "clients.h"
63
64 typedef struct
65 {
66   int nclients;
67   int lines;
68   int baud;
69   int crtscts;
70   int cd_edge_sec;
71   int blocked;
72   int bootstrap;
73 } Status;
74
75 static Status
76 get_status (TTY * t, Clients * cs)
77 {
78   static struct timeval last_cd_edge = { 0 };
79   static int last_cd_state = -1;
80   int cd;
81   struct timeval now, dif;
82
83   TTY_Status tty_status = { 0 };
84   Status status;
85
86   tty_get_status (t, &tty_status);
87
88   status.bootstrap = 1;
89   status.nclients = cs->n;
90   status.lines = tty_status.lines;
91   status.baud = tty_status.baud;
92   status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
93   status.blocked = tty_status.blocked;
94
95   cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
96
97   if (cd != last_cd_state)
98     {
99       gettimeofday (&last_cd_edge, NULL);
100       last_cd_state = cd;
101     }
102
103   gettimeofday (&now, NULL);
104   timersub (&now, &last_cd_edge, &dif);
105   status.cd_edge_sec = dif.tv_sec;
106
107   return status;
108 }
109
110 static char *
111 line_to_name (int l)
112 {
113
114   switch (l)
115     {
116 #ifdef TIOCM_LE
117     case TIOCM_LE:
118       return "LE";
119 #endif
120 #ifdef TIOCM_DTR
121     case TIOCM_DTR:
122       return "DTR";
123 #endif
124 #ifdef TIOCM_RTS
125     case TIOCM_RTS:
126       return "RTS";
127 #endif
128 #ifdef TIOCM_ST
129     case TIOCM_ST:
130       return "ST";
131 #endif
132 #ifdef TIOCM_SR
133     case TIOCM_SR:
134       return "SR";
135 #endif
136 #ifdef TIOCM_CTS
137     case TIOCM_CTS:
138       return "CTS";
139 #endif
140 #ifdef TIOCM_CD
141     case TIOCM_CD:
142       return "CD";
143 #endif
144 #ifdef TIOCM_RI
145     case TIOCM_RI:
146       return "RI";
147 #endif
148 #ifdef TIOCM_DSR
149     case TIOCM_DSR:
150       return "DSR";
151 #endif
152     }
153   return "??";
154 }
155
156 static void
157 log_line_changes (Context * ctx, int old, int new)
158 {
159   int dif = old ^ new;
160   int c = 1;
161   char buf[1024], *ptr = buf;
162   char *n;
163
164   if (!dif)
165     return;
166   if (!ctx->l)
167     return;
168
169   n = "<Modem lines changed:";
170
171   while (*n)
172     *(ptr++) = *(n++);
173
174   while (dif >= c)
175     {
176
177       if (dif & c)
178         {
179           *(ptr++) = ' ';
180           *(ptr++) = (new & c) ? '+' : '-';
181           n = line_to_name (c);
182           while (*n)
183             *(ptr++) = *(n++);
184         }
185
186       c <<= 1;
187     }
188   *(ptr++) = '>';
189   *ptr = 0;
190
191
192   ctx->l->log (ctx->l, buf);
193
194 }
195
196 static char *
197 do_line (char *ptr, int lines, int line)
198 {
199   char *lname;
200
201   if (!(lines & line))
202     return ptr;
203   lname = line_to_name (line);
204
205   *(ptr++) = ' ';
206   while (*lname)
207     *(ptr++) = *(lname++);
208
209   return ptr;
210 }
211
212
213
214 static void
215 check_status (Context * c, Clients * cs)
216 {
217   static Status old_status = { 0 };
218   Status status;
219   char buf[1024];
220   char *ptr = buf;
221   char *t;
222
223   status = get_status (c->t, cs);
224   if (!memcmp (&status, &old_status, sizeof (status)))
225     return;
226   old_status = status;
227
228
229   log_line_changes (c, old_status.lines, status.lines);
230
231   ptr += sprintf (ptr, "CTRL-B ");
232
233   t = c->t->name;
234   if (!strncmp (t, "/dev/", 5))
235     t += 5;
236   while (*t)
237     *(ptr++) = *(t++);
238
239   ptr += sprintf (ptr, " %db", status.baud);
240
241   ptr = do_line (ptr, status.lines, TIOCM_RTS);
242   ptr = do_line (ptr, status.lines, TIOCM_CTS);
243   ptr = do_line (ptr, status.lines, TIOCM_DTR);
244   ptr = do_line (ptr, status.lines, TIOCM_DSR);
245   ptr = do_line (ptr, status.lines, TIOCM_RI);
246   ptr = do_line (ptr, status.lines, TIOCM_CD);
247
248   if (status.blocked)
249     {
250       t = ", Locked";
251       while (*t)
252         *(ptr++) = *(t++);
253     }
254
255   if (status.crtscts)
256     {
257       t = ", Flow";
258       while (*t)
259         *(ptr++) = *(t++);
260     }
261
262 #if 0
263   if (status.lines & TIOCM_CD)
264     {
265       ptr +=
266         sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
267                  status.cd_edge_sec % 60);
268     }
269   else
270     {
271       ptr +=
272         sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
273                  status.cd_edge_sec % 60);
274     }
275 #endif
276
277   ptr +=
278     sprintf (ptr, ", %d client%s", status.nclients,
279              (status.nclients == 1) ? "" : "s");
280
281   *ptr = 0;
282
283   send_status (cs, buf);
284 }
285
286
287   TTY *t;
288   Log *l;
289   t= ptty_open (NULL, NULL);
290   //t = serial_open ("/dev/cellmodem", 0);
291 l=file_log_new ("log");
292
293
294 void mainloop (TTY *tty,Socket *server_socket,Socket *client_socket,Ansi *a,Log *log)
295 {
296   fd_set rfds, wfds;
297   Context c;
298   Clients *clients;
299
300   c.v = vt102_new ();
301   c.h = history_new (200);
302   c.l = log
303
304   /* are we being fed by a tty or a socket */
305   if (client_socket) {
306         if (server_socket) abort();
307         c.s = client_socket;  
308         c.k = keydis_ipc_new (client_socket);
309   } else {
310         if (!tty) abort();
311         c.t = tty;
312         c.k = keydis_vt102_new ();
313   }
314
315   /* do we have an upstream terminal to talk to*/
316   if (ansi) {
317         c.d = cmd_new ();
318   } else {
319         c.d = NULL;
320   }
321
322
323   if (server_socket) {
324         if(client_socket) abort();
325         clients = clients_new ();
326   } else {
327         clients=NULL;
328   }
329
330   for (;;)
331     {
332       struct timeval tv = { 1, 0 };
333
334       check_status (&c, clients);
335
336       FD_ZERO (&rfds);
337       FD_ZERO (&wfds);
338
339         if (c.t)
340           tty_pre_select (c.t, &rfds, &wfds);
341
342         if (server_socket) {
343       FD_SET (server_socket->fd, &rfds);
344       clients_pre_select (clients, &rfds, &wfds);
345         }
346
347         if (client_socket)
348       socket_pre_select (client_socket, &rfds, &wfds);
349
350       select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
351
352       /*any message from clients, or new connexions*/
353       if (server_socket) {
354       Socket *cs;
355       if (FD_ISSET (server_socket->fd, &rfds) && ((cs = socket_accept (s))))
356         {
357           {
358             Client *cl;
359             /*New client connexion */
360             cl = clients_new_client (clients, cs, &c);
361
362             send_history (c.h, cl);
363             send_vt102 (c.v, cl);
364
365           }
366         }
367
368       clients_post_select (clients, &c, &rfds, &wfds);
369       }
370
371       /*any data from the port*/
372       if (c.t && FD_ISSET (c.t->rfd, &rfds))
373         {
374           char buf[IPC_MAX_BUF];
375           int red;
376
377           red = c.t->recv (c.t, buf, sizeof (buf));
378
379           if (red < 0)
380             break;
381
382           if (red)
383             {
384               send_output (clients, buf, red);
385               vt102_parse (&c, buf, red);
386             }
387         }
388
389
390
391     }
392
393   clients_shutdown (clients);
394   terminal_atexit ();
395   printf ("QUAT\n");
396 }