More precise computing of time to spend in select(). This fixes the
problem with CPU underload with very small values of PollingCycle.


git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@490 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/x0vncserver/x0vncserver.cxx b/x0vncserver/x0vncserver.cxx
index b9a44cc..7b8c660 100644
--- a/x0vncserver/x0vncserver.cxx
+++ b/x0vncserver/x0vncserver.cxx
@@ -401,17 +401,10 @@
     int dynPollingCycle = (int)pollingCycle;
 
     TimeMillis timeSaved, timeNow;
+    fd_set rfds;
+    struct timeval tv;
 
     while (true) {
-      fd_set rfds;
-      struct timeval tv;
-
-      // FIXME: This seems to be wrong.
-      tv.tv_sec = 0;
-      tv.tv_usec = dynPollingCycle * 1000;
-      if (tv.tv_usec > 500000) {
-        tv.tv_usec = 500000;
-      }
 
       FD_ZERO(&rfds);
       FD_SET(listener.getFd(), &rfds);
@@ -419,10 +412,32 @@
       std::list<Socket*> sockets;
       server.getSockets(&sockets);
       std::list<Socket*>::iterator i;
+      int clients_connected = 0;
       for (i = sockets.begin(); i != sockets.end(); i++) {
         FD_SET((*i)->getFd(), &rfds);
+        clients_connected++;
       }
 
+      if (clients_connected) {
+        int poll_ms = 20;
+        if (timeNow.update()) {
+          poll_ms = timeNow.diffFrom(timeSaved);
+        }
+        int wait_ms = dynPollingCycle - poll_ms;
+        if (wait_ms < 0) {
+          wait_ms = 0;
+        } else if (wait_ms > 500) {
+          wait_ms = 500;
+        }
+        tv.tv_usec = wait_ms * 1000;
+#ifdef DEBUG
+        fprintf(stderr, "[%d]\t", wait_ms);
+#endif
+      } else {
+        tv.tv_usec = 50000;
+      }
+      tv.tv_sec = 0;
+
       int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
       if (n < 0) {
         if (errno == EINTR) {