4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.17 2008/02/27 09:42:53 james
15 * *** empty log message ***
17 * Revision 1.16 2008/02/27 09:42:21 james
18 * *** empty log message ***
20 * Revision 1.15 2008/02/27 01:31:38 james
21 * *** empty log message ***
23 * Revision 1.14 2008/02/27 01:31:14 james
24 * *** empty log message ***
26 * Revision 1.13 2008/02/26 23:56:12 james
27 * *** empty log message ***
29 * Revision 1.12 2008/02/26 23:23:16 james
30 * *** empty log message ***
32 * Revision 1.11 2008/02/24 00:43:55 james
33 * *** empty log message ***
35 * Revision 1.10 2008/02/23 13:05:57 staffcvs
36 * *** empty log message ***
38 * Revision 1.9 2008/02/23 11:48:51 james
39 * *** empty log message ***
41 * Revision 1.8 2008/02/22 23:39:30 james
42 * *** empty log message ***
44 * Revision 1.7 2008/02/20 20:16:07 james
45 * *** empty log message ***
47 * Revision 1.6 2008/02/20 19:44:37 james
50 * Revision 1.5 2008/02/20 18:31:44 james
51 * *** empty log message ***
53 * Revision 1.4 2008/02/20 17:18:33 james
54 * *** empty log message ***
56 * Revision 1.3 2008/02/20 02:11:35 james
57 * *** empty log message ***
59 * Revision 1.2 2008/02/16 10:58:52 james
60 * *** empty log message ***
62 * Revision 1.13 2008/02/15 23:52:12 james
63 * *** empty log message ***
65 * Revision 1.12 2008/02/15 03:32:07 james
66 * *** empty log message ***
68 * Revision 1.11 2008/02/14 16:21:17 james
69 * *** empty log message ***
71 * Revision 1.10 2008/02/14 10:39:14 james
72 * *** empty log message ***
74 * Revision 1.9 2008/02/14 10:34:47 james
75 * *** empty log message ***
77 * Revision 1.8 2008/02/14 10:34:30 james
78 * *** empty log message ***
80 * Revision 1.7 2008/02/14 02:46:44 james
81 * *** empty log message ***
83 * Revision 1.6 2008/02/14 00:57:58 james
84 * *** empty log message ***
86 * Revision 1.5 2008/02/13 18:05:06 james
87 * *** empty log message ***
89 * Revision 1.4 2008/02/13 17:21:55 james
90 * *** empty log message ***
92 * Revision 1.3 2008/02/08 15:06:52 james
93 * *** empty log message ***
95 * Revision 1.2 2008/02/07 15:42:49 james
96 * *** empty log message ***
98 * Revision 1.1 2008/02/05 14:25:49 james
99 * *** empty log message ***
103 #include <sys/time.h>
104 #include <sympathy.h>
105 #include "mainloop.h"
123 get_status (TTY * t, Clients * cs)
125 static struct timeval last_cd_edge = { 0 };
126 static int last_cd_state = -1;
128 struct timeval now, dif;
130 TTY_Status tty_status = { 0 };
133 tty_get_status (t, &tty_status);
135 status.bootstrap = 1;
138 status.nclients = cs->n;
142 status.lines = tty_status.lines;
143 status.baud = tty_status.baud;
144 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
145 status.blocked = tty_status.blocked;
147 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
149 if (cd != last_cd_state)
151 gettimeofday (&last_cd_edge, NULL);
155 gettimeofday (&now, NULL);
156 timersub (&now, &last_cd_edge, &dif);
157 status.cd_edge_sec = dif.tv_sec;
209 log_line_changes (Context * ctx, int old, int new)
213 char buf[1024], *ptr = buf;
221 n = "<Modem lines changed:";
232 *(ptr++) = (new & c) ? '+' : '-';
233 n = line_to_name (c);
244 ctx->l->log (ctx->l, buf);
249 do_line (char *ptr, int lines, int line)
255 lname = line_to_name (line);
259 *(ptr++) = *(lname++);
267 check_status (Context * c, Clients * cs)
269 static Status old_status = { 0 };
275 status = get_status (c->t, cs);
276 if (!memcmp (&status, &old_status, sizeof (status)))
279 log_line_changes (c, old_status.lines, status.lines);
283 ptr += sprintf (ptr, "CTRL-B ");
286 if (!strncmp (t, "/dev/", 5))
291 ptr += sprintf (ptr, " %db", status.baud);
293 ptr = do_line (ptr, status.lines, TIOCM_RTS);
294 ptr = do_line (ptr, status.lines, TIOCM_CTS);
295 ptr = do_line (ptr, status.lines, TIOCM_DTR);
296 ptr = do_line (ptr, status.lines, TIOCM_DSR);
297 ptr = do_line (ptr, status.lines, TIOCM_RI);
298 ptr = do_line (ptr, status.lines, TIOCM_CD);
315 if (status.lines & TIOCM_CD)
318 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
319 status.cd_edge_sec % 60);
324 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
325 status.cd_edge_sec % 60);
330 sprintf (ptr, ", %d client%s", status.nclients,
331 (status.nclients == 1) ? "" : "s");
337 sprintf (ptr, ", %d err%s", c->tp->biterrs,
338 (c->tp->biterrs == 1) ? "" : "s");
340 if (c->tp->guessed_baud == -1)
342 ptr += sprintf (ptr, " try higher");
344 else if (c->tp->guessed_baud > 0)
346 ptr += sprintf (ptr, " try %db", c->tp->guessed_baud);
353 log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
357 send_status (cs, buf);
359 cmd_new_status (c->d, c, buf);
365 msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
370 case IPC_MSG_TYPE_NOOP:
372 case IPC_MSG_TYPE_DEBUG:
373 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
375 case IPC_MSG_TYPE_HISTORY:
376 history_add (c->h, m->history.history.line);
378 case IPC_MSG_TYPE_VT102:
379 if (sizeof (VT102) != m->vt102.len)
382 *(c->v) = m->vt102.vt102;
386 a->one_shot (a, &c->v->crt);
391 case IPC_MSG_TYPE_TERM:
392 tty_parse (c, m->term.term, m->term.len);
394 case IPC_MSG_TYPE_STATUS:
395 cmd_new_status (c->d, c, m->status.status);
398 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
405 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
406 ANSI * ansi, Log * log, int nhistory, CRT_Pos * size)
414 c.tp = tty_parser_new ();
417 c.v = vt102_new (size);
418 c.h = history_new (nhistory);
420 /* are we being fed by a tty or a socket */
425 c.k = keydis_ipc_new (client_socket);
432 c.k = keydis_vt102_new ();
435 /* do we have an upstream terminal to talk to */
436 /* if so start a command parser */
451 clients = clients_new ();
460 struct timeval tv = { 0, 250000 };
462 if ((c.d) && (c.d->disconnect))
465 /*update the status lines, locally or remotely */
467 check_status (&c, clients);
473 tty_pre_select (c.t, &rfds, &wfds);
477 FD_SET (server_socket->fd, &rfds);
478 clients_pre_select (clients, &rfds, &wfds);
482 socket_pre_select (client_socket, &rfds, &wfds);
484 if (ansi && ansi->terminal)
485 tty_pre_select (ansi->terminal, &rfds, &wfds);
487 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
489 /*any message from clients, or new connexions */
492 Socket *new_client_socket;
493 if (FD_ISSET (server_socket->fd, &rfds)
494 && ((new_client_socket = socket_accept (server_socket))))
498 /*New client connexion */
500 clients_new_client (clients, new_client_socket, &c);
502 send_history (c.h, new_client);
503 send_vt102 (c.v, new_client);
508 clients_post_select (clients, &c, &rfds, &wfds);
511 /*any data from the port */
512 if (c.t && FD_ISSET (c.t->rfd, &rfds))
514 char buf[IPC_MAX_BUF];
517 red = c.t->recv (c.t, buf, sizeof (buf));
525 send_output (clients, buf, red);
526 tty_parse (&c, buf, red);
532 /*any data from the server */
536 if (socket_post_select (client_socket, &rfds, &wfds))
539 while (client_socket->msg)
541 if (msg_from_server (ansi, client_socket->msg, &c))
544 socket_consume_msg (client_socket);
551 /*update our local screen */
555 ansi->dispatch (ansi, &c);
558 ansi->update (ansi, &c);
563 clients_shutdown (clients);