chiark / gitweb /
*** empty log message ***
[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[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.9  2008/02/23 11:48:51  james
14  * *** empty log message ***
15  *
16  * Revision 1.8  2008/02/22 23:39:30  james
17  * *** empty log message ***
18  *
19  * Revision 1.7  2008/02/20 20:16:07  james
20  * *** empty log message ***
21  *
22  * Revision 1.6  2008/02/20 19:44:37  james
23  * @@
24  *
25  * Revision 1.5  2008/02/20 18:31:44  james
26  * *** empty log message ***
27  *
28  * Revision 1.4  2008/02/20 17:18:33  james
29  * *** empty log message ***
30  *
31  * Revision 1.3  2008/02/20 02:11:35  james
32  * *** empty log message ***
33  *
34  * Revision 1.2  2008/02/16 10:58:52  james
35  * *** empty log message ***
36  *
37  * Revision 1.13  2008/02/15 23:52:12  james
38  * *** empty log message ***
39  *
40  * Revision 1.12  2008/02/15 03:32:07  james
41  * *** empty log message ***
42  *
43  * Revision 1.11  2008/02/14 16:21:17  james
44  * *** empty log message ***
45  *
46  * Revision 1.10  2008/02/14 10:39:14  james
47  * *** empty log message ***
48  *
49  * Revision 1.9  2008/02/14 10:34:47  james
50  * *** empty log message ***
51  *
52  * Revision 1.8  2008/02/14 10:34:30  james
53  * *** empty log message ***
54  *
55  * Revision 1.7  2008/02/14 02:46:44  james
56  * *** empty log message ***
57  *
58  * Revision 1.6  2008/02/14 00:57:58  james
59  * *** empty log message ***
60  *
61  * Revision 1.5  2008/02/13 18:05:06  james
62  * *** empty log message ***
63  *
64  * Revision 1.4  2008/02/13 17:21:55  james
65  * *** empty log message ***
66  *
67  * Revision 1.3  2008/02/08 15:06:52  james
68  * *** empty log message ***
69  *
70  * Revision 1.2  2008/02/07 15:42:49  james
71  * *** empty log message ***
72  *
73  * Revision 1.1  2008/02/05 14:25:49  james
74  * *** empty log message ***
75  *
76  */
77
78 #include <sys/time.h>
79 #include <sympathy.h>
80 #include "mainloop.h"
81
82 #include "clients.h"
83
84 typedef struct
85 {
86   int nclients;
87   int lines;
88   int baud;
89   int crtscts;
90   int cd_edge_sec;
91   int blocked;
92   int bootstrap;
93 } Status;
94
95 static Status
96 get_status (TTY * t, Clients * cs)
97 {
98   static struct timeval last_cd_edge = { 0 };
99   static int last_cd_state = -1;
100   int cd;
101   struct timeval now, dif;
102
103   TTY_Status tty_status = { 0 };
104   Status status;
105
106   tty_get_status (t, &tty_status);
107
108   status.bootstrap = 1;
109
110   if (cs)
111     status.nclients = cs->n;
112   else
113     status.nclients = 0;
114
115   status.lines = tty_status.lines;
116   status.baud = tty_status.baud;
117   status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
118   status.blocked = tty_status.blocked;
119
120   cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
121
122   if (cd != last_cd_state)
123     {
124       gettimeofday (&last_cd_edge, NULL);
125       last_cd_state = cd;
126     }
127
128   gettimeofday (&now, NULL);
129   timersub (&now, &last_cd_edge, &dif);
130   status.cd_edge_sec = dif.tv_sec;
131
132   return status;
133 }
134
135 static char *
136 line_to_name (int l)
137 {
138
139   switch (l)
140     {
141 #ifdef TIOCM_LE
142     case TIOCM_LE:
143       return "LE";
144 #endif
145 #ifdef TIOCM_DTR
146     case TIOCM_DTR:
147       return "DTR";
148 #endif
149 #ifdef TIOCM_RTS
150     case TIOCM_RTS:
151       return "RTS";
152 #endif
153 #ifdef TIOCM_ST
154     case TIOCM_ST:
155       return "ST";
156 #endif
157 #ifdef TIOCM_SR
158     case TIOCM_SR:
159       return "SR";
160 #endif
161 #ifdef TIOCM_CTS
162     case TIOCM_CTS:
163       return "CTS";
164 #endif
165 #ifdef TIOCM_CD
166     case TIOCM_CD:
167       return "CD";
168 #endif
169 #ifdef TIOCM_RI
170     case TIOCM_RI:
171       return "RI";
172 #endif
173 #ifdef TIOCM_DSR
174     case TIOCM_DSR:
175       return "DSR";
176 #endif
177     }
178   return "??";
179 }
180
181 static void
182 log_line_changes (Context * ctx, int old, int new)
183 {
184   int dif = old ^ new;
185   int c = 1;
186   char buf[1024], *ptr = buf;
187   char *n;
188
189   if (!dif)
190     return;
191   if (!ctx->l)
192     return;
193
194   n = "<Modem lines changed:";
195
196   while (*n)
197     *(ptr++) = *(n++);
198
199   while (dif >= c)
200     {
201
202       if (dif & c)
203         {
204           *(ptr++) = ' ';
205           *(ptr++) = (new & c) ? '+' : '-';
206           n = line_to_name (c);
207           while (*n)
208             *(ptr++) = *(n++);
209         }
210
211       c <<= 1;
212     }
213   *(ptr++) = '>';
214   *ptr = 0;
215
216
217   ctx->l->log (ctx->l, buf);
218
219 }
220
221 static char *
222 do_line (char *ptr, int lines, int line)
223 {
224   char *lname;
225
226   if (!(lines & line))
227     return ptr;
228   lname = line_to_name (line);
229
230   *(ptr++) = ' ';
231   while (*lname)
232     *(ptr++) = *(lname++);
233
234   return ptr;
235 }
236
237
238
239 static void
240 check_status (Context * c, Clients * cs)
241 {
242   static Status old_status = { 0 };
243   Status status;
244   char buf[1024];
245   char *ptr = buf;
246   char *t;
247
248   status = get_status (c->t, cs);
249   if (!memcmp (&status, &old_status, sizeof (status)))
250     return;
251
252   log_line_changes (c, old_status.lines, status.lines);
253
254   old_status = status;
255
256   ptr += sprintf (ptr, "CTRL-B ");
257
258   t = c->t->name;
259   if (!strncmp (t, "/dev/", 5))
260     t += 5;
261   while (*t)
262     *(ptr++) = *(t++);
263
264   ptr += sprintf (ptr, " %db", status.baud);
265
266   ptr = do_line (ptr, status.lines, TIOCM_RTS);
267   ptr = do_line (ptr, status.lines, TIOCM_CTS);
268   ptr = do_line (ptr, status.lines, TIOCM_DTR);
269   ptr = do_line (ptr, status.lines, TIOCM_DSR);
270   ptr = do_line (ptr, status.lines, TIOCM_RI);
271   ptr = do_line (ptr, status.lines, TIOCM_CD);
272
273   if (status.blocked)
274     {
275       t = ", Locked";
276       while (*t)
277         *(ptr++) = *(t++);
278     }
279
280   if (status.crtscts)
281     {
282       t = ", Flow";
283       while (*t)
284         *(ptr++) = *(t++);
285     }
286
287 #if 0
288   if (status.lines & TIOCM_CD)
289     {
290       ptr +=
291         sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
292                  status.cd_edge_sec % 60);
293     }
294   else
295     {
296       ptr +=
297         sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
298                  status.cd_edge_sec % 60);
299     }
300 #endif
301
302   ptr +=
303     sprintf (ptr, ", %d client%s", status.nclients,
304              (status.nclients == 1) ? "" : "s");
305
306   if (c->tp->biterrs)
307     {
308
309       ptr +=
310         sprintf (ptr, ", %d err%s", c->tp->biterrs,
311                  (c->tp->biterrs == 1) ? "" : "s");
312
313       if (c->tp->guessed_baud == -1)
314         {
315           ptr += sprintf (ptr, " try higher");
316         }
317       else if (c->tp->guessed_baud > 0)
318         {
319           ptr += sprintf (ptr, " try %d", status.baud / c->tp->guessed_baud);
320         }
321     }
322
323   *ptr = 0;
324
325 #if 0
326   log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
327 #endif
328
329   if (cs)
330     send_status (cs, buf);
331   else
332     cmd_new_status (c->d, c, buf);
333
334 }
335
336
337 static int
338 msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
339 {
340   switch (m->hdr.type)
341     {
342
343     case IPC_MSG_TYPE_NOOP:
344       break;
345     case IPC_MSG_TYPE_DEBUG:
346 //          fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
347       break;
348     case IPC_MSG_TYPE_HISTORY:
349       history_add (c->h, m->history.history.line);
350       break;
351     case IPC_MSG_TYPE_VT102:
352       if (sizeof (VT102) != m->vt102.len)
353         abort ();
354
355       *(c->v) = m->vt102.vt102;
356
357       if (a->one_shot)
358         {
359           a->one_shot (a, &c->v->crt);
360           return 1;
361         }
362       //FIXME HTML hook
363       break;
364     case IPC_MSG_TYPE_TERM:
365       tty_parse (c, m->term.term, m->term.len);
366       break;
367     case IPC_MSG_TYPE_STATUS:
368       cmd_new_status (c->d, c, m->status.status);
369       break;
370     default:
371       fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
372     }
373   return 0;
374 }
375
376
377 void
378 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
379           ANSI * ansi, Log * log, int nhistory)
380 {
381   fd_set rfds, wfds;
382   Context c = { 0 };
383   Clients *clients;
384
385   c.tp = tty_parser_new ();
386   c.u = utf8_new ();
387
388   c.v = vt102_new ();
389   c.h = history_new (nhistory);
390   c.l = log;
391   /* are we being fed by a tty or a socket */
392   if (client_socket)
393     {
394       if (server_socket)
395         abort ();
396       c.k = keydis_ipc_new (client_socket);
397     }
398   else
399     {
400       if (!tty)
401         abort ();
402       c.t = tty;
403       c.k = keydis_vt102_new ();
404     }
405
406   /* do we have an upstream terminal to talk to */
407   /* if so start a command parser */
408   if (ansi)
409     {
410       c.d = cmd_new ();
411     }
412   else
413     {
414       c.d = NULL;
415     }
416
417
418   if (server_socket)
419     {
420       if (client_socket)
421         abort ();
422       clients = clients_new ();
423     }
424   else
425     {
426       clients = NULL;
427     }
428
429   for (;;)
430     {
431       struct timeval tv = { 0, 250000 };
432
433       if ((c.d) && (c.d->disconnect))
434         break;
435
436       /*update the status lines, locally or remotely */
437       if (c.t)
438         check_status (&c, clients);
439
440       FD_ZERO (&rfds);
441       FD_ZERO (&wfds);
442
443       if (c.t)
444         tty_pre_select (c.t, &rfds, &wfds);
445
446       if (server_socket)
447         {
448           FD_SET (server_socket->fd, &rfds);
449           clients_pre_select (clients, &rfds, &wfds);
450         }
451
452       if (client_socket)
453         socket_pre_select (client_socket, &rfds, &wfds);
454
455       if (ansi && ansi->terminal)
456         tty_pre_select (ansi->terminal, &rfds, &wfds);
457
458       select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
459
460       /*any message from clients, or new connexions */
461       if (server_socket)
462         {
463           Socket *new_client_socket;
464           if (FD_ISSET (server_socket->fd, &rfds)
465               && ((new_client_socket = socket_accept (server_socket))))
466             {
467               {
468                 Client *new_client;
469                 /*New client connexion */
470                 new_client =
471                   clients_new_client (clients, new_client_socket, &c);
472
473                 send_history (c.h, new_client);
474                 send_vt102 (c.v, new_client);
475
476               }
477             }
478
479           clients_post_select (clients, &c, &rfds, &wfds);
480         }
481
482       /*any data from the port */
483       if (c.t && FD_ISSET (c.t->rfd, &rfds))
484         {
485           char buf[IPC_MAX_BUF];
486           int red;
487
488           red = c.t->recv (c.t, buf, sizeof (buf));
489
490           if (red < 0)
491             break;
492
493           if (red)
494             {
495               if (clients)
496                 send_output (clients, buf, red);
497               tty_parse (&c, buf, red);
498             }
499         }
500
501
502
503       /*any data from the server */
504       if (client_socket)
505         {
506           int one_shot;
507           if (socket_post_select (client_socket, &rfds, &wfds))
508             break;
509
510           while (client_socket->msg)
511             {
512               if (msg_from_server (ansi, client_socket->msg, &c))
513                 one_shot++;
514
515               socket_consume_msg (client_socket);
516             }
517           if (one_shot)
518             break;
519         }
520
521
522       /*update our local screen */
523       if (ansi)
524         {
525           if (ansi->dispatch)
526             ansi->dispatch (ansi, &c);
527
528           if (ansi->update)
529             ansi->update (ansi, &c);
530         }
531     }
532
533   if (clients)
534     clients_shutdown (clients);
535 }