Index: owl.c
===================================================================
--- owl.c	(revision eebef194307e93a0e2956946794840c21dd8c4ba)
+++ owl.c	(revision 9c7a701363179369d764454b0d99e559ab89ed9e)
@@ -46,4 +46,5 @@
 #include <sys/param.h>
 #include <sys/types.h>
+#include <sys/time.h>
 #include <termios.h>
 #include <sys/stat.h>
@@ -60,4 +61,6 @@
 #endif
 
+#define STDIN 0
+
 static const char fileIdent[] = "$Id$";
 
@@ -69,5 +72,5 @@
   owl_editwin *tw;
   owl_popwin *pw;
-  int j, ret, initialsubs, debug, argcsave, followlast;
+  int ret, initialsubs, debug, argcsave, followlast;
   int newmsgs, nexttimediff;
   struct sigaction sigact;
@@ -206,9 +209,25 @@
   owl_global_set_haveaim(&g);
 
+  /* prepare stdin dispatch */
+  {
+    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
+    d->fd = STDIN;
+    d->cfunc = &owl_process_input;
+    d->pfunc = NULL;
+    owl_select_add_dispatch(d);
+  }
+  
 #ifdef HAVE_LIBZEPHYR
   /* zephyr init */
   ret=owl_zephyr_initialize();
-  if (!ret)
-      owl_global_set_havezephyr(&g);
+  if (!ret) {
+    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
+    d->fd = ZGetFD();
+    d->cfunc = &owl_zephyr_process_events;
+    d->pfunc = NULL;
+    owl_select_add_dispatch(d);
+    owl_global_set_havezephyr(&g);
+  }
+
 #endif
 
@@ -425,16 +444,4 @@
     followlast=owl_global_should_followlast(&g);
     
-    /* Do AIM stuff */
-    if (owl_global_is_doaimevents(&g)) {
-      owl_aim_process_events();
-
-      if (owl_global_is_aimloggedin(&g)) {
-	if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
-	  /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
-	  owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
-	}
-      }
-    }
-
     owl_perlconfig_mainloop();
 
@@ -454,6 +461,4 @@
     }
 
-    owl_zephyr_process_events();
-    
     /* Grab incoming messages. */
     newmsgs=0;
@@ -530,37 +535,7 @@
      * little bit, but otherwise do not.  This lets input be grabbed
      * as quickly as possbile */
-    j=wgetch(typwin);
-    if (j==ERR) {
-      usleep(10000);
-    } else {
-      owl_global_set_lastinputtime(&g, now);
-      /* find and activate the current keymap.
-       * TODO: this should really get fixed by activating
-       * keymaps as we switch between windows... 
-       */
-      if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
-	owl_context_set_popless(owl_global_get_context(&g), 
-				owl_global_get_viewwin(&g));
-	owl_function_activate_keymap("popless");
-      } else if (owl_global_is_typwin_active(&g) 
-		 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
-	/*
-	  owl_context_set_editline(owl_global_get_context(&g), tw);
-	  owl_function_activate_keymap("editline");
-	*/
-      } else if (owl_global_is_typwin_active(&g) 
-		 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
-	owl_context_set_editmulti(owl_global_get_context(&g), tw);
-	owl_function_activate_keymap("editmulti");
-      } else {
-	owl_context_set_recv(owl_global_get_context(&g));
-	owl_function_activate_keymap("recv");
-      }
-      /* now actually handle the keypress */
-      ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
-      if (ret!=0 && ret!=1) {
-	owl_function_makemsg("Unable to handle keypress");
-      }
-    }
+
+    /* select on FDs we know about. */
+    owl_select();
 
     /* Log any error signals */
@@ -680,4 +655,60 @@
 }
 
+void owl_process_aim()
+{
+  if (owl_global_is_doaimevents(&g)) {
+    owl_aim_process_events();
+    
+    if (owl_global_is_aimloggedin(&g)) {
+      if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
+        /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
+        owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
+      }
+    }
+  }
+}
+
+void owl_process_input()
+{
+  int ret, j;
+  owl_popwin *pw;
+  owl_editwin *tw;
+
+  j = wgetch(owl_global_get_curs_typwin(&g));
+  if (j == ERR) return;
+
+  owl_global_set_lastinputtime(&g, time(NULL));
+  pw=owl_global_get_popwin(&g);
+  tw=owl_global_get_typwin(&g);
+
+  /* find and activate the current keymap.
+   * TODO: this should really get fixed by activating
+   * keymaps as we switch between windows... 
+   */
+  if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
+    owl_context_set_popless(owl_global_get_context(&g), 
+                            owl_global_get_viewwin(&g));
+    owl_function_activate_keymap("popless");
+  } else if (owl_global_is_typwin_active(&g) 
+             && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_ONELINE) {
+    /*
+      owl_context_set_editline(owl_global_get_context(&g), tw);
+      owl_function_activate_keymap("editline");
+    */
+  } else if (owl_global_is_typwin_active(&g) 
+             && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_MULTILINE) {
+    owl_context_set_editmulti(owl_global_get_context(&g), tw);
+    owl_function_activate_keymap("editmulti");
+  } else {
+    owl_context_set_recv(owl_global_get_context(&g));
+    owl_function_activate_keymap("recv");
+  }
+  /* now actually handle the keypress */
+  ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
+  if (ret != 0 && ret != 1) {
+    owl_function_makemsg("Unable to handle keypress");
+  }
+}
+
 void sig_handler(int sig, siginfo_t *si, void *data)
 {
@@ -694,5 +725,4 @@
     owl_function_quit();
   }
-
 }
 
