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