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