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