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