Code improvement -- the code from detectVideo() was split into two functions, handleVideo() and detectVideo().


git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2353 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/unix/x0vncserver/PollingManager.cxx b/unix/x0vncserver/PollingManager.cxx
index 0ec6734..b2d4757 100644
--- a/unix/x0vncserver/PollingManager.cxx
+++ b/unix/x0vncserver/PollingManager.cxx
@@ -164,9 +164,12 @@
   }
 
   // Do the work related to video area detection.
-  bool videoDetected = detectVideo(mxChanged);
+  bool haveVideoRect = handleVideo(mxChanged);
 
   // Inform the server about the changes.
+  // FIXME: It's possible that (nTilesChanged != 0) but mxChanged[]
+  //        array is empty. That's possible because handleVideo()
+  //        modifies mxChanged[].
   if (nTilesChanged)
     sendChanges(mxChanged);
 
@@ -179,7 +182,7 @@
   }
 #endif
 
-  return (nTilesChanged != 0 || videoDetected);
+  return (nTilesChanged != 0 || haveVideoRect);
 }
 
 int PollingManager::checkRow(int x, int y, int w, bool *pmxChanged)
@@ -239,53 +242,18 @@
   }
 }
 
-bool PollingManager::detectVideo(bool *pmxChanged)
+bool PollingManager::handleVideo(bool *pmxChanged)
 {
-  // Configurable parameters.
-  const int VIDEO_THRESHOLD_0 = 3;
-  const int VIDEO_THRESHOLD_1 = 5;
-
-  // Each call: update counters in m_rateMatrix.
+  // Update counters in m_rateMatrix.
   int numTiles = m_heightTiles * m_widthTiles;
   for (int i = 0; i < numTiles; i++)
     m_rateMatrix[i] += (pmxChanged[i] != false);
 
-  // Once per eight calls: detect video rectangle.
-  bool isGrandStep = (m_pollingStep % 8 == 0);
-  if (isGrandStep) {
-    //
-    // First, detect candidate region that looks like video. In other
-    // words, find a region that consists of continuously changing
-    // pixels. Save the result in m_videoFlags[] and reset counters in
-    // m_rateMatrix[].
-    //
-    for (int i = 0; i < numTiles; i++) {
-      if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
-        m_videoFlags[i] = 0;
-      } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
-        m_videoFlags[i] = 1;
-      }
-      m_rateMatrix[i] = 0;
-    }
-    //
-    // Now, choose the biggest rectangle from that candidate region.
-    //
-    Rect newRect;
-    getVideoAreaRect(&newRect);
-    //
-    // Does new rectangle differ from the previously detected one?
-    // If it does, save new rectangle and inform the server.
-    //
-    if (!newRect.equals(m_videoRect)) {
-      if (newRect.is_empty()) {
-        fprintf(stderr, "No video detected\n");
-      } else {
-        fprintf(stderr, "Video rect %dx%d\tat(%d,%d)\n",
-                newRect.width(), newRect.height(), newRect.tl.x, newRect.tl.y);
-      }
-      m_videoRect = newRect;
-      m_server->set_video_area(newRect);
-    }
+  // Once per eight calls: detect video rectangle by examining
+  // m_rateMatrix[], then reset counters in m_rateMatrix[].
+  if (m_pollingStep % 8 == 0) {
+    detectVideo();
+    memset(m_rateMatrix, 0, numTiles);
   }
 
   // Grab the pixels of video area. Also, exclude video rectangle from
@@ -306,6 +274,43 @@
 }
 
 void
+PollingManager::detectVideo()
+{
+  // Configurable parameters.
+  const int VIDEO_THRESHOLD_0 = 3;
+  const int VIDEO_THRESHOLD_1 = 5;
+
+  // First, detect candidate region that looks like video. In other
+  // words, find a region that consists of continuously changing
+  // pixels. Save the result in m_videoFlags[].
+  int numTiles = m_heightTiles * m_widthTiles;
+  for (int i = 0; i < numTiles; i++) {
+    if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
+      m_videoFlags[i] = 0;
+    } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
+      m_videoFlags[i] = 1;
+    }
+  }
+
+  // Now, choose the biggest rectangle from that candidate region.
+  Rect newRect;
+  getVideoAreaRect(&newRect);
+
+  // Does new rectangle differ from the previously detected one?
+  // If it does, save new rectangle and inform the server.
+  if (!newRect.equals(m_videoRect)) {
+    if (newRect.is_empty()) {
+      fprintf(stderr, "No video detected\n");
+    } else {
+      fprintf(stderr, "Video rect %dx%d\tat(%d,%d)\n",
+              newRect.width(), newRect.height(), newRect.tl.x, newRect.tl.y);
+    }
+    m_videoRect = newRect;
+    m_server->set_video_area(newRect);
+  }
+}
+
+void
 PollingManager::getVideoAreaRect(Rect *result)
 {
   int *mx_hlen, *mx_vlen;