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