Corrected algorithm of adjusting polling intervals, to make sudden
changes more graceful.


git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@491 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/x0vncserver/x0vncserver.cxx b/x0vncserver/x0vncserver.cxx
index 7b8c660..af63ad0 100644
--- a/x0vncserver/x0vncserver.cxx
+++ b/x0vncserver/x0vncserver.cxx
@@ -332,19 +332,37 @@
 {
   int coeff = mon->check();
   if (coeff < 90 || coeff > 110) {
-#ifdef DEBUG
-    int oldPollingCycle = *cycle;
-#endif
-    *cycle = (*cycle * 100 + coeff/2) / coeff;
-    if (*cycle < (int)pollingCycle) {
-      *cycle = (int)pollingCycle;
-    } else if (*cycle > (int)pollingCycle * 32) {
-      *cycle = (int)pollingCycle * 32;
+    int oldValue = *cycle;
+    int newValue = (oldValue * 100 + coeff/2) / coeff;
+
+    // Correct sudden changes.
+    if (newValue < oldValue / 2) {
+      newValue = oldValue / 2;
+    } else if (newValue > oldValue * 2) {
+      newValue = oldValue * 2;
     }
+
+    // Compute upper limit.
+    int upperLimit = (int)pollingCycle * 32;
+    if (upperLimit < 100) {
+      upperLimit = 100;
+    } else if (upperLimit > 1000) {
+      upperLimit = 1000;
+    }
+
+    // Limit lower and upper bounds.
+    if (newValue < (int)pollingCycle) {
+      newValue = (int)pollingCycle;
+    } else if (newValue > upperLimit) {
+      newValue = upperLimit;
+    }
+
+    if (newValue != oldValue) {
+      *cycle = newValue;
 #ifdef DEBUG
-    if (*cycle != oldPollingCycle)
-      fprintf(stderr, "\t[new cycle %dms]\n", *cycle);
+      fprintf(stderr, "\t[new cycle %dms]\n", newValue);
 #endif
+    }
   }
 }