4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.20 2008/02/28 16:57:51 james
15 * *** empty log message ***
17 * Revision 1.19 2008/02/28 16:37:16 james
18 * *** empty log message ***
20 * Revision 1.18 2008/02/28 11:27:48 james
21 * *** empty log message ***
23 * Revision 1.17 2008/02/27 09:42:53 james
24 * *** empty log message ***
26 * Revision 1.16 2008/02/27 09:42:21 james
27 * *** empty log message ***
29 * Revision 1.15 2008/02/27 01:31:38 james
30 * *** empty log message ***
32 * Revision 1.14 2008/02/27 01:31:14 james
33 * *** empty log message ***
35 * Revision 1.13 2008/02/26 23:56:12 james
36 * *** empty log message ***
38 * Revision 1.12 2008/02/26 23:23:16 james
39 * *** empty log message ***
41 * Revision 1.11 2008/02/24 00:43:55 james
42 * *** empty log message ***
44 * Revision 1.10 2008/02/23 13:05:57 staffcvs
45 * *** empty log message ***
47 * Revision 1.9 2008/02/23 11:48:51 james
48 * *** empty log message ***
50 * Revision 1.8 2008/02/22 23:39:30 james
51 * *** empty log message ***
53 * Revision 1.7 2008/02/20 20:16:07 james
54 * *** empty log message ***
56 * Revision 1.6 2008/02/20 19:44:37 james
59 * Revision 1.5 2008/02/20 18:31:44 james
60 * *** empty log message ***
62 * Revision 1.4 2008/02/20 17:18:33 james
63 * *** empty log message ***
65 * Revision 1.3 2008/02/20 02:11:35 james
66 * *** empty log message ***
68 * Revision 1.2 2008/02/16 10:58:52 james
69 * *** empty log message ***
71 * Revision 1.13 2008/02/15 23:52:12 james
72 * *** empty log message ***
74 * Revision 1.12 2008/02/15 03:32:07 james
75 * *** empty log message ***
77 * Revision 1.11 2008/02/14 16:21:17 james
78 * *** empty log message ***
80 * Revision 1.10 2008/02/14 10:39:14 james
81 * *** empty log message ***
83 * Revision 1.9 2008/02/14 10:34:47 james
84 * *** empty log message ***
86 * Revision 1.8 2008/02/14 10:34:30 james
87 * *** empty log message ***
89 * Revision 1.7 2008/02/14 02:46:44 james
90 * *** empty log message ***
92 * Revision 1.6 2008/02/14 00:57:58 james
93 * *** empty log message ***
95 * Revision 1.5 2008/02/13 18:05:06 james
96 * *** empty log message ***
98 * Revision 1.4 2008/02/13 17:21:55 james
99 * *** empty log message ***
101 * Revision 1.3 2008/02/08 15:06:52 james
102 * *** empty log message ***
104 * Revision 1.2 2008/02/07 15:42:49 james
105 * *** empty log message ***
107 * Revision 1.1 2008/02/05 14:25:49 james
108 * *** empty log message ***
112 #include <sys/time.h>
113 #include <sympathy.h>
114 #include "mainloop.h"
132 get_status (TTY * t, Clients * cs)
134 static struct timeval last_cd_edge = { 0 };
135 static int last_cd_state = -1;
137 struct timeval now, dif;
139 TTY_Status tty_status = { 0 };
142 tty_get_status (t, &tty_status);
144 status.bootstrap = 1;
147 status.nclients = cs->n;
151 status.lines = tty_status.lines;
152 status.baud = tty_status.baud;
153 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
154 status.blocked = tty_status.blocked;
156 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
158 if (cd != last_cd_state)
160 gettimeofday (&last_cd_edge, NULL);
164 gettimeofday (&now, NULL);
165 timersub (&now, &last_cd_edge, &dif);
166 status.cd_edge_sec = dif.tv_sec;
218 log_line_changes (Context * ctx, int old, int new)
222 char buf[1024], *ptr = buf;
230 n = "<Modem lines changed:";
241 *(ptr++) = (new & c) ? '+' : '-';
242 n = line_to_name (c);
253 ctx->l->log (ctx->l, buf);
258 do_line (char *ptr, int lines, int line)
264 lname = line_to_name (line);
268 *(ptr++) = *(lname++);
276 check_status (Context * c, Clients * cs)
278 static Status old_status = { 0 };
284 status = get_status (c->t, cs);
285 if (!memcmp (&status, &old_status, sizeof (status)))
288 log_line_changes (c, old_status.lines, status.lines);
292 ptr += sprintf (ptr, "CTRL-B ");
295 if (!strncmp (t, "/dev/", 5))
300 ptr += sprintf (ptr, " %db", status.baud);
302 ptr = do_line (ptr, status.lines, TIOCM_RTS);
303 ptr = do_line (ptr, status.lines, TIOCM_CTS);
304 ptr = do_line (ptr, status.lines, TIOCM_DTR);
305 ptr = do_line (ptr, status.lines, TIOCM_DSR);
306 ptr = do_line (ptr, status.lines, TIOCM_RI);
307 ptr = do_line (ptr, status.lines, TIOCM_CD);
324 if (status.lines & TIOCM_CD)
327 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
328 status.cd_edge_sec % 60);
333 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
334 status.cd_edge_sec % 60);
339 sprintf (ptr, ", %d client%s", status.nclients,
340 (status.nclients == 1) ? "" : "s");
346 sprintf (ptr, ", %d err%s", c->tp->biterrs,
347 (c->tp->biterrs == 1) ? "" : "s");
349 if (c->tp->guessed_baud == -1)
351 ptr += sprintf (ptr, " try higher");
353 else if (c->tp->guessed_baud > 0)
355 ptr += sprintf (ptr, " try %db", c->tp->guessed_baud);
362 log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
366 send_status (cs, buf);
368 cmd_new_status (c->d, c, buf);
374 msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
379 case IPC_MSG_TYPE_NOOP:
381 case IPC_MSG_TYPE_DEBUG:
382 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
384 case IPC_MSG_TYPE_HISTORY:
385 history_add (c->h, m->history.history.line);
387 case IPC_MSG_TYPE_VT102:
388 if (sizeof (VT102) != m->vt102.len)
391 *(c->v) = m->vt102.vt102;
395 a->one_shot (a, &c->v->crt);
399 case IPC_MSG_TYPE_TERM:
400 tty_parse (c, m->term.term, m->term.len);
402 case IPC_MSG_TYPE_STATUS:
403 cmd_new_status (c->d, c, m->status.status);
406 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
413 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
414 ANSI * ansi, Log * log, int nhistory, CRT_Pos * size)
422 c.tp = tty_parser_new ();
425 c.v = vt102_new (size);
426 c.h = history_new (nhistory);
428 /* are we being fed by a tty or a socket */
433 c.k = keydis_ipc_new (client_socket);
440 c.k = keydis_vt102_new ();
443 /* do we have an upstream terminal to talk to */
444 /* if so start a command parser */
462 clients = clients_new ();
471 struct timeval tv = { 0, 250000 };
473 if ((c.d) && (c.d->disconnect))
476 /*update the status lines, locally or remotely */
478 check_status (&c, clients);
484 tty_pre_select (c.t, &rfds, &wfds);
488 FD_SET (server_socket->fd, &rfds);
489 clients_pre_select (clients, &rfds, &wfds);
493 socket_pre_select (client_socket, &rfds, &wfds);
495 if (ansi && ansi->terminal)
496 tty_pre_select (ansi->terminal, &rfds, &wfds);
498 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
500 /*any message from clients, or new connexions */
503 Socket *new_client_socket;
504 if (FD_ISSET (server_socket->fd, &rfds)
505 && ((new_client_socket = socket_accept (server_socket))))
509 /*New client connexion */
511 clients_new_client (clients, new_client_socket, &c);
513 send_history (c.h, new_client);
514 send_vt102 (c.v, new_client);
519 clients_post_select (clients, &c, &rfds, &wfds);
522 /*any data from the port */
523 if (c.t && FD_ISSET (c.t->rfd, &rfds))
525 char buf[IPC_MAX_BUF];
528 red = c.t->recv (c.t, buf, sizeof (buf));
536 send_output (clients, buf, red);
537 tty_parse (&c, buf, red);
543 /*any data from the server */
547 if (socket_post_select (client_socket, &rfds, &wfds))
550 while (client_socket->msg)
552 if (msg_from_server (ansi, client_socket->msg, &c))
555 socket_consume_msg (client_socket);
562 /*update our local screen */
566 ansi->dispatch (ansi, &c);
569 ansi->update (ansi, &c);
574 clients_shutdown (clients);