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