Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2004-2005 Constantin Kaplinsky. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | // |
| 19 | // PollingManager.cxx |
| 20 | // |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <X11/Xlib.h> |
| 26 | #include <rfb/VNCServer.h> |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 27 | #include <rfb/Configuration.h> |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 28 | |
| 29 | #include <x0vncserver/Image.h> |
| 30 | #include <x0vncserver/PollingManager.h> |
| 31 | |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 32 | IntParameter pollingType("PollingType", "Polling algorithm to use (0..3)", 3); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 33 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 34 | const int PollingManager::m_pollingOrder[32] = { |
| 35 | 0, 16, 8, 24, 4, 20, 12, 28, |
| 36 | 10, 26, 18, 2, 22, 6, 30, 14, |
| 37 | 1, 17, 9, 25, 7, 23, 15, 31, |
| 38 | 19, 3, 27, 11, 29, 13, 5, 21 |
| 39 | }; |
| 40 | |
| 41 | // |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 42 | // Constructor. |
| 43 | // |
| 44 | // Note that dpy and image should remain valid during the object |
| 45 | // lifetime, while factory is used only in the constructor itself. |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 46 | // |
| 47 | |
| 48 | PollingManager::PollingManager(Display *dpy, Image *image, |
| 49 | ImageFactory *factory) |
| 50 | : m_dpy(dpy), m_server(0), m_image(image), m_pollingStep(0) |
| 51 | { |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 52 | // Save width and height of the screen (and the image). |
| 53 | m_width = m_image->xim->width; |
| 54 | m_height = m_image->xim->height; |
| 55 | |
| 56 | // Compute width and height in 32x32 tiles. |
| 57 | m_widthTiles = (m_width + 31) / 32; |
| 58 | m_heightTiles = (m_height + 31) / 32; |
| 59 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 60 | // Create two additional images used in the polling algorithm. |
| 61 | // FIXME: verify that these images use the same pixel format as in m_image. |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 62 | m_rowImage = factory->newImage(m_dpy, m_width, 1); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 63 | m_tileImage = factory->newImage(m_dpy, 32, 32); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 64 | |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 65 | // FIXME: Extend the comment. |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 66 | // Create a matrix with one byte per each 32x32 tile. It will be |
| 67 | // used to limit the rate of updates on continuously-changed screen |
| 68 | // areas (like video). |
| 69 | int numTiles = m_widthTiles * m_heightTiles; |
| 70 | m_statusMatrix = new char[numTiles]; |
| 71 | memset(m_statusMatrix, 0, numTiles); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 72 | |
| 73 | // FIXME: Extend the comment. |
| 74 | // Create a matrix with one byte per each 32x32 tile. It will be |
| 75 | // used to limit the rate of updates on continuously-changed screen |
| 76 | // areas (like video). |
| 77 | m_rateMatrix = new char[numTiles]; |
| 78 | m_videoFlags = new char[numTiles]; |
| 79 | m_changedFlags = new char[numTiles]; |
| 80 | memset(m_rateMatrix, 0, numTiles); |
| 81 | memset(m_videoFlags, 0, numTiles); |
| 82 | memset(m_changedFlags, 0, numTiles); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 85 | PollingManager::~PollingManager() |
| 86 | { |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 87 | delete[] m_changedFlags; |
| 88 | delete[] m_videoFlags; |
| 89 | delete[] m_rateMatrix; |
| 90 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 91 | delete[] m_statusMatrix; |
| 92 | } |
| 93 | |
| 94 | // |
| 95 | // Register VNCServer object. |
| 96 | // |
| 97 | |
| 98 | void PollingManager::setVNCServer(VNCServer *s) |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 99 | { |
| 100 | m_server = s; |
| 101 | } |
| 102 | |
| 103 | // |
| 104 | // DEBUG: a version of poll() measuring time spent in the function. |
| 105 | // |
| 106 | |
| 107 | void PollingManager::pollDebug() |
| 108 | { |
| 109 | struct timeval timeSaved, timeNow; |
| 110 | struct timezone tz; |
| 111 | timeSaved.tv_sec = 0; |
| 112 | timeSaved.tv_usec = 0; |
| 113 | gettimeofday(&timeSaved, &tz); |
| 114 | |
| 115 | poll(); |
| 116 | |
| 117 | gettimeofday(&timeNow, &tz); |
| 118 | int diff = (int)((timeNow.tv_usec - timeSaved.tv_usec + 500) / 1000 + |
| 119 | (timeNow.tv_sec - timeSaved.tv_sec) * 1000); |
| 120 | if (diff != 0) |
| 121 | fprintf(stderr, "DEBUG: poll(): %4d ms\n", diff); |
| 122 | } |
| 123 | |
| 124 | // |
| 125 | // Search for changed rectangles on the screen. |
| 126 | // |
| 127 | |
| 128 | void PollingManager::poll() |
| 129 | { |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 130 | bool someChanges = false; |
| 131 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 132 | switch((int)pollingType) { |
| 133 | case 0: |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 134 | someChanges = poll_Dumb(); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 135 | break; |
| 136 | case 1: |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 137 | someChanges = poll_Traditional(); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 138 | break; |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 139 | case 2: |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 140 | someChanges = poll_SkipCycles(); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 141 | break; |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 142 | //case 3: |
| 143 | default: |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 144 | someChanges = poll_DetectVideo(); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 145 | break; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 146 | } |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 147 | |
| 148 | if (someChanges) |
| 149 | m_server->tryUpdate(); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 152 | bool PollingManager::poll_DetectVideo() |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 153 | { |
| 154 | if (!m_server) |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 155 | return false; |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 156 | |
| 157 | const int GRAND_STEP_DIVISOR = 8; |
| 158 | const int VIDEO_THRESHOLD_0 = 3; |
| 159 | const int VIDEO_THRESHOLD_1 = 5; |
| 160 | |
| 161 | bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0); |
| 162 | |
| 163 | // FIXME: Save shortcuts in member variables. |
| 164 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 165 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 166 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 167 | |
| 168 | Rect rect; |
| 169 | int nTilesChanged = 0; |
| 170 | int idx = 0; |
| 171 | |
| 172 | for (int y = 0; y * 32 < m_height; y++) { |
| 173 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
| 174 | if (scanLine >= tile_h) |
| 175 | break; |
| 176 | int scan_y = y * 32 + scanLine; |
| 177 | m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y); |
| 178 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 179 | char *ptr_new = m_rowImage->xim->data; |
| 180 | for (int x = 0; x * 32 < m_width; x++) { |
| 181 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
| 182 | int nBytes = tile_w * bytesPerPixel; |
| 183 | |
| 184 | char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0); |
| 185 | m_rateMatrix[idx] += wasChanged; |
| 186 | |
| 187 | if (grandStep) { |
| 188 | if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) { |
| 189 | m_videoFlags[idx] = 0; |
| 190 | } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) { |
| 191 | m_videoFlags[idx] = 1; |
| 192 | } |
| 193 | m_rateMatrix[idx] = 0; |
| 194 | } |
| 195 | |
| 196 | m_changedFlags[idx] |= wasChanged; |
| 197 | if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) { |
| 198 | if (tile_w == 32 && tile_h == 32) { |
| 199 | m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32); |
| 200 | } else { |
| 201 | m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32, |
| 202 | tile_w, tile_h); |
| 203 | } |
| 204 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 205 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 206 | m_server->add_changed(rect); |
| 207 | nTilesChanged++; |
| 208 | m_changedFlags[idx] = 0; |
| 209 | } |
| 210 | |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 211 | ptr_old += nBytes; |
| 212 | ptr_new += nBytes; |
| 213 | idx++; |
| 214 | } |
| 215 | } |
| 216 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 217 | return (nTilesChanged != 0); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 220 | bool PollingManager::poll_SkipCycles() |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 221 | { |
| 222 | if (!m_server) |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 223 | return false; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 224 | |
| 225 | enum { |
| 226 | NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN |
| 227 | }; |
| 228 | |
| 229 | bool grandStep = (m_pollingStep % 8 == 0); |
| 230 | |
| 231 | int nTilesChanged = 0; |
| 232 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 233 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 234 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 235 | char *pstatus = m_statusMatrix; |
| 236 | bool wasChanged; |
| 237 | Rect rect; |
| 238 | |
| 239 | for (int y = 0; y * 32 < m_height; y++) { |
| 240 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
| 241 | if (scanLine >= tile_h) |
| 242 | scanLine %= tile_h; |
| 243 | int scan_y = y * 32 + scanLine; |
| 244 | m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y); |
| 245 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 246 | char *ptr_new = m_rowImage->xim->data; |
| 247 | for (int x = 0; x * 32 < m_width; x++) { |
| 248 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
| 249 | int nBytes = tile_w * bytesPerPixel; |
| 250 | |
| 251 | if (grandStep || *pstatus != CHANGED_AGAIN) { |
| 252 | wasChanged = (*pstatus == CHANGED_AGAIN) ? |
| 253 | true : (memcmp(ptr_old, ptr_new, nBytes) != 0); |
| 254 | if (wasChanged) { |
| 255 | if (grandStep || *pstatus == NOT_CHANGED) { |
| 256 | if (tile_w == 32 && tile_h == 32) { |
| 257 | m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32); |
| 258 | } else { |
| 259 | m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32, |
| 260 | tile_w, tile_h); |
| 261 | } |
| 262 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 263 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 264 | m_server->add_changed(rect); |
| 265 | nTilesChanged++; |
| 266 | *pstatus = CHANGED_ONCE; |
| 267 | } else { |
| 268 | *pstatus = CHANGED_AGAIN; |
| 269 | } |
| 270 | } else if (grandStep) { |
| 271 | *pstatus = NOT_CHANGED; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | ptr_old += nBytes; |
| 276 | ptr_new += nBytes; |
| 277 | pstatus++; |
| 278 | } |
| 279 | } |
| 280 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 281 | return (nTilesChanged != 0); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 284 | bool PollingManager::poll_Traditional() |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 285 | { |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 286 | if (!m_server) |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 287 | return false; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 288 | |
| 289 | int nTilesChanged = 0; |
| 290 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 291 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 292 | int bytesPerLine = m_image->xim->bytes_per_line; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 293 | Rect rect; |
| 294 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 295 | for (int y = 0; y * 32 < m_height; y++) { |
| 296 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 297 | if (scanLine >= tile_h) |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 298 | break; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 299 | int scan_y = y * 32 + scanLine; |
| 300 | m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y); |
| 301 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 302 | char *ptr_new = m_rowImage->xim->data; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 303 | for (int x = 0; x * 32 < m_width; x++) { |
| 304 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 305 | int nBytes = tile_w * bytesPerPixel; |
| 306 | if (memcmp(ptr_old, ptr_new, nBytes)) { |
| 307 | if (tile_w == 32 && tile_h == 32) { |
| 308 | m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32); |
| 309 | } else { |
| 310 | m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32, |
| 311 | tile_w, tile_h); |
| 312 | } |
| 313 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 314 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 315 | m_server->add_changed(rect); |
| 316 | nTilesChanged++; |
| 317 | } |
| 318 | ptr_old += nBytes; |
| 319 | ptr_new += nBytes; |
| 320 | } |
| 321 | } |
| 322 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 323 | return (nTilesChanged != 0); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 326 | // |
| 327 | // Simplest polling method, from the original x0vncserver of VNC4. |
| 328 | // |
| 329 | |
| 330 | bool PollingManager::poll_Dumb() |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 331 | { |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame^] | 332 | if (!m_server) |
| 333 | return false; |
| 334 | |
| 335 | m_image->get(DefaultRootWindow(m_dpy)); |
| 336 | Rect rect(0, 0, m_width, m_height); |
| 337 | m_server->add_changed(rect); |
| 338 | |
| 339 | // As we have no idea if any pixels were changed, |
| 340 | // always report that some changes have been detected. |
| 341 | return true; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 342 | } |