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