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