chiark / gitweb /
*** empty log message ***
[sympathy.git] / apps / mainloop.c
index 851f2966af00ba191a1e60022b894aadedbb30c5..9737bd29439f545c8a3ebc162708715bf74615a0 100644 (file)
@@ -6,10 +6,59 @@
  *
  */
 
-static char rcsid[] = "$Id$";
+static char rcsid[] =
+  "$Id$";
 
 /*
  * $Log$
+ * Revision 1.20  2008/02/28 16:57:51  james
+ * *** empty log message ***
+ *
+ * Revision 1.19  2008/02/28 16:37:16  james
+ * *** empty log message ***
+ *
+ * Revision 1.18  2008/02/28 11:27:48  james
+ * *** empty log message ***
+ *
+ * Revision 1.17  2008/02/27 09:42:53  james
+ * *** empty log message ***
+ *
+ * Revision 1.16  2008/02/27 09:42:21  james
+ * *** empty log message ***
+ *
+ * Revision 1.15  2008/02/27 01:31:38  james
+ * *** empty log message ***
+ *
+ * Revision 1.14  2008/02/27 01:31:14  james
+ * *** empty log message ***
+ *
+ * Revision 1.13  2008/02/26 23:56:12  james
+ * *** empty log message ***
+ *
+ * Revision 1.12  2008/02/26 23:23:16  james
+ * *** empty log message ***
+ *
+ * Revision 1.11  2008/02/24 00:43:55  james
+ * *** empty log message ***
+ *
+ * Revision 1.10  2008/02/23 13:05:57  staffcvs
+ * *** empty log message ***
+ *
+ * Revision 1.9  2008/02/23 11:48:51  james
+ * *** empty log message ***
+ *
+ * Revision 1.8  2008/02/22 23:39:30  james
+ * *** empty log message ***
+ *
+ * Revision 1.7  2008/02/20 20:16:07  james
+ * *** empty log message ***
+ *
+ * Revision 1.6  2008/02/20 19:44:37  james
+ * @@
+ *
+ * Revision 1.5  2008/02/20 18:31:44  james
+ * *** empty log message ***
+ *
  * Revision 1.4  2008/02/20 17:18:33  james
  * *** empty log message ***
  *
@@ -62,10 +111,12 @@ static char rcsid[] = "$Id$";
 
 #include <sys/time.h>
 #include <sympathy.h>
+#include "mainloop.h"
 
-#include "client.h"
 #include "clients.h"
 
+Context *context;
+
 typedef struct
 {
   int nclients;
@@ -233,11 +284,11 @@ check_status (Context * c, Clients * cs)
   status = get_status (c->t, cs);
   if (!memcmp (&status, &old_status, sizeof (status)))
     return;
-  old_status = status;
-
 
   log_line_changes (c, old_status.lines, status.lines);
 
+  old_status = status;
+
   ptr += sprintf (ptr, "CTRL-B ");
 
   t = c->t->name;
@@ -288,18 +339,39 @@ check_status (Context * c, Clients * cs)
     sprintf (ptr, ", %d client%s", status.nclients,
              (status.nclients == 1) ? "" : "s");
 
+  if (c->tp->biterrs)
+    {
+
+      ptr +=
+        sprintf (ptr, ", %d err%s", c->tp->biterrs,
+                 (c->tp->biterrs == 1) ? "" : "s");
+
+      if (c->tp->guessed_baud == -1)
+        {
+          ptr += sprintf (ptr, " try higher");
+        }
+      else if (c->tp->guessed_baud > 0)
+        {
+          ptr += sprintf (ptr, " try %db", c->tp->guessed_baud);
+        }
+    }
+
   *ptr = 0;
 
+#if 0
+  log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
+#endif
+
   if (cs)
     send_status (cs, buf);
   else
-    cmd_new_status (c->d, c, m->status.status);
+    cmd_new_status (c->d, c, buf);
 
 }
 
 
-static void
-msg_from_server (IPC_Msg * m, Context * c)
+static int
+msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
 {
   switch (m->hdr.type)
     {
@@ -317,10 +389,15 @@ msg_from_server (IPC_Msg * m, Context * c)
         abort ();
 
       *(c->v) = m->vt102.vt102;
-       //FIXME HTML hook
+
+      if (a->one_shot)
+        {
+          a->one_shot (a, &c->v->crt);
+          return 1;
+        }
       break;
     case IPC_MSG_TYPE_TERM:
-      vt102_parse (c, m->term.term, m->term.len);
+      tty_parse (c, m->term.term, m->term.len);
       break;
     case IPC_MSG_TYPE_STATUS:
       cmd_new_status (c->d, c, m->status.status);
@@ -328,27 +405,31 @@ msg_from_server (IPC_Msg * m, Context * c)
     default:
       fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
     }
+  return 0;
 }
 
 
 void
-mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a,
-          Log * log)
+mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
+          ANSI * ansi, Log * log, int nhistory, CRT_Pos * size)
 {
   fd_set rfds, wfds;
-  Context c={0};
+  Context c = { 0 };
   Clients *clients;
 
+  context = &c;
+
+  c.tp = tty_parser_new ();
+  c.u = utf8_new ();
 
-  c.v = vt102_new ();
-  c.h = history_new (200);
+  c.v = vt102_new (size);
+  c.h = history_new (nhistory);
   c.l = log;
-    /* are we being fed by a tty or a socket */
-    if (client_socket)
+  /* are we being fed by a tty or a socket */
+  if (client_socket)
     {
       if (server_socket)
         abort ();
-      c.s = client_socket;
       c.k = keydis_ipc_new (client_socket);
     }
   else
@@ -371,6 +452,9 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a,
     }
 
 
+  vt102_reset (&c);
+
+
   if (server_socket)
     {
       if (client_socket)
@@ -384,10 +468,14 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a,
 
   for (;;)
     {
-      struct timeval tv = { 1, 0 };
+      struct timeval tv = { 0, 250000 };
 
-      /*update the status lines, locally or remotely*/
-      check_status (&c, clients);
+      if ((c.d) && (c.d->disconnect))
+        break;
+
+      /*update the status lines, locally or remotely */
+      if (c.t)
+        check_status (&c, clients);
 
       FD_ZERO (&rfds);
       FD_ZERO (&wfds);
@@ -404,7 +492,7 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a,
       if (client_socket)
         socket_pre_select (client_socket, &rfds, &wfds);
 
-      if (ansi)
+      if (ansi && ansi->terminal)
         tty_pre_select (ansi->terminal, &rfds, &wfds);
 
       select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
@@ -414,7 +502,7 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a,
         {
           Socket *new_client_socket;
           if (FD_ISSET (server_socket->fd, &rfds)
-              && ((new_client_socket = socket_accept (s))))
+              && ((new_client_socket = socket_accept (server_socket))))
             {
               {
                 Client *new_client;
@@ -444,40 +532,44 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a,
 
           if (red)
             {
-              send_output (clients, buf, red);
-              vt102_parse (&c, buf, red);
+              if (clients)
+                send_output (clients, buf, red);
+              tty_parse (&c, buf, red);
             }
         }
 
 
 
-      /*any data from the server*/
+      /*any data from the server */
       if (client_socket)
         {
+          int one_shot;
           if (socket_post_select (client_socket, &rfds, &wfds))
             break;
 
           while (client_socket->msg)
             {
-              msg_from_server (client_socket->msg, &c);
+              if (msg_from_server (ansi, client_socket->msg, &c))
+                one_shot++;
+
               socket_consume_msg (client_socket);
             }
+          if (one_shot)
+            break;
         }
 
 
-      /*update our local screen*/
+      /*update our local screen */
       if (ansi)
         {
-          if (ansi_dispatch (ansi, &c))
-            break;
+          if (ansi->dispatch)
+            ansi->dispatch (ansi, &c);
 
-          ansi_update (ansi, &c);
+          if (ansi->update)
+            ansi->update (ansi, &c);
         }
     }
 
   if (clients)
-    {
-      clients_shutdown (clients);
-    }
-  printf ("QUAT\n");
+    clients_shutdown (clients);
 }