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