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