public static void select(GlkEvent e)
  {
    long cur = 0l;
    GlkEvent ev = null;
    boolean done = false;

    synchronized(EVENT_QUEUE)
    {
      if (Window.root != null)
        Window.root.doLayout();

      while (!done)
      {
        if (!EVENT_QUEUE.isEmpty())
        {
          ev = (GlkEvent) EVENT_QUEUE.removeFirst();
          if (ev != null)
            done = true;
        }
        else if (TIMER > 0 && 
                 (cur = System.currentTimeMillis()) - TIMESTAMP >= TIMER)
        {
          e.type = EVTYPE_TIMER;
          e.win = null;
          e.val1 = 0;
          e.val2 = 0;
          TIMESTAMP = cur;
          done = true;
        }
        else
        {
          try {
            if (TIMER > 0)
              EVENT_QUEUE.wait(TIMER - (cur - TIMESTAMP));
            else
              EVENT_QUEUE.wait();
          } catch(InterruptedException ex) {}
        }
      }
      if (ev != null)
      {
        e.type = ev.type;
        e.win = ev.win;
        e.val1 = ev.val1;
        e.val2 = ev.val2;
      }
    }
  }