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