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 | |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 22 | // FIXME: Don't compare pixels already marked as changed. |
| 23 | // FIXME: Use Image::copyPixels() instead of Image::updateRect()? |
| 24 | // In that case, note the fact that arguments are not checked. |
| 25 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <string.h> |
Constantin Kaplinsky | af2ebeb | 2006-02-10 12:19:11 +0000 | [diff] [blame] | 28 | #include <time.h> |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 29 | #include <X11/Xlib.h> |
| 30 | #include <rfb/VNCServer.h> |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 31 | #include <rfb/Configuration.h> |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 32 | #include <rfb/ServerCore.h> |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 33 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 34 | #include <x0vncserver/PollingManager.h> |
| 35 | |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 36 | BoolParameter PollingManager::pollPointer |
| 37 | ("PollPointer", |
Constantin Kaplinsky | a4f0736 | 2006-02-10 11:54:49 +0000 | [diff] [blame] | 38 | "DEBUG: Poll area under the pointer with higher priority", |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 39 | true); |
| 40 | |
| 41 | IntParameter PollingManager::pollingType |
| 42 | ("PollingType", |
Constantin Kaplinsky | a4f0736 | 2006-02-10 11:54:49 +0000 | [diff] [blame] | 43 | "DEBUG: Select particular polling algorithm (0..3)", |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 44 | 3); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 45 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 46 | const int PollingManager::m_pollingOrder[32] = { |
| 47 | 0, 16, 8, 24, 4, 20, 12, 28, |
| 48 | 10, 26, 18, 2, 22, 6, 30, 14, |
| 49 | 1, 17, 9, 25, 7, 23, 15, 31, |
| 50 | 19, 3, 27, 11, 29, 13, 5, 21 |
| 51 | }; |
| 52 | |
| 53 | // |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 54 | // Constructor. |
| 55 | // |
| 56 | // Note that dpy and image should remain valid during the object |
| 57 | // lifetime, while factory is used only in the constructor itself. |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 58 | // |
| 59 | |
| 60 | PollingManager::PollingManager(Display *dpy, Image *image, |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 61 | ImageFactory *factory, |
| 62 | int offsetLeft, int offsetTop) |
| 63 | : m_dpy(dpy), m_server(0), m_image(image), |
| 64 | m_offsetLeft(offsetLeft), m_offsetTop(offsetTop), |
| 65 | m_pointerPosKnown(false), m_pollingStep(0) |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 66 | { |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 67 | // Save width and height of the screen (and the image). |
| 68 | m_width = m_image->xim->width; |
| 69 | m_height = m_image->xim->height; |
| 70 | |
| 71 | // Compute width and height in 32x32 tiles. |
| 72 | m_widthTiles = (m_width + 31) / 32; |
| 73 | m_heightTiles = (m_height + 31) / 32; |
| 74 | |
Constantin Kaplinsky | 99d6514 | 2006-05-22 05:45:20 +0000 | [diff] [blame^] | 75 | // Get initial screen image. |
| 76 | m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop); |
| 77 | |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 78 | // Create additional images used in the polling algorithm. |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 79 | // 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] | 80 | m_rowImage = factory->newImage(m_dpy, m_width, 1); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 81 | m_tileImage = factory->newImage(m_dpy, 32, 32); |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 82 | m_areaImage = factory->newImage(m_dpy, 128, 128); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 83 | |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 84 | // FIXME: Extend the comment. |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 85 | // Create a matrix with one byte per each 32x32 tile. It will be |
| 86 | // used to limit the rate of updates on continuously-changed screen |
| 87 | // areas (like video). |
| 88 | int numTiles = m_widthTiles * m_heightTiles; |
| 89 | m_statusMatrix = new char[numTiles]; |
| 90 | memset(m_statusMatrix, 0, numTiles); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 91 | |
| 92 | // FIXME: Extend the comment. |
| 93 | // Create a matrix with one byte per each 32x32 tile. It will be |
| 94 | // used to limit the rate of updates on continuously-changed screen |
| 95 | // areas (like video). |
| 96 | m_rateMatrix = new char[numTiles]; |
| 97 | m_videoFlags = new char[numTiles]; |
| 98 | m_changedFlags = new char[numTiles]; |
| 99 | memset(m_rateMatrix, 0, numTiles); |
| 100 | memset(m_videoFlags, 0, numTiles); |
| 101 | memset(m_changedFlags, 0, numTiles); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 104 | PollingManager::~PollingManager() |
| 105 | { |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 106 | delete[] m_changedFlags; |
| 107 | delete[] m_videoFlags; |
| 108 | delete[] m_rateMatrix; |
| 109 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 110 | delete[] m_statusMatrix; |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 111 | |
| 112 | delete m_areaImage; |
| 113 | delete m_tileImage; |
| 114 | delete m_rowImage; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // |
| 118 | // Register VNCServer object. |
| 119 | // |
| 120 | |
| 121 | void PollingManager::setVNCServer(VNCServer *s) |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 122 | { |
| 123 | m_server = s; |
| 124 | } |
| 125 | |
| 126 | // |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 127 | // Update current pointer position which may be used as a hint for |
| 128 | // polling algorithms. |
| 129 | // |
| 130 | |
| 131 | void PollingManager::setPointerPos(const Point &pos) |
| 132 | { |
Constantin Kaplinsky | af2ebeb | 2006-02-10 12:19:11 +0000 | [diff] [blame] | 133 | m_pointerPosTime = time(NULL); |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 134 | m_pointerPos = pos; |
| 135 | m_pointerPosKnown = true; |
| 136 | } |
| 137 | |
| 138 | // |
| 139 | // Indicate that current pointer position is unknown. |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 140 | // |
| 141 | |
| 142 | void PollingManager::unsetPointerPos() |
| 143 | { |
| 144 | m_pointerPosKnown = false; |
| 145 | } |
| 146 | |
| 147 | // |
Constantin Kaplinsky | b247d7d | 2006-02-15 16:14:07 +0000 | [diff] [blame] | 148 | // DEBUG: Measuring time spent in the poll() function, |
| 149 | // as well as time intervals between poll() calls. |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 150 | // |
| 151 | |
Constantin Kaplinsky | b247d7d | 2006-02-15 16:14:07 +0000 | [diff] [blame] | 152 | #ifdef DEBUG |
| 153 | void PollingManager::debugBeforePoll() |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 154 | { |
Constantin Kaplinsky | fbaab7f | 2006-02-16 04:51:38 +0000 | [diff] [blame] | 155 | TimeMillis timeNow; |
| 156 | int diff = timeNow.diffFrom(m_timeSaved); |
Constantin Kaplinsky | b247d7d | 2006-02-15 16:14:07 +0000 | [diff] [blame] | 157 | fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32); |
| 158 | m_timeSaved = timeNow; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Constantin Kaplinsky | b247d7d | 2006-02-15 16:14:07 +0000 | [diff] [blame] | 161 | void PollingManager::debugAfterPoll() |
| 162 | { |
Constantin Kaplinsky | fbaab7f | 2006-02-16 04:51:38 +0000 | [diff] [blame] | 163 | TimeMillis timeNow; |
| 164 | int diff = timeNow.diffFrom(m_timeSaved); |
Constantin Kaplinsky | b247d7d | 2006-02-15 16:14:07 +0000 | [diff] [blame] | 165 | fprintf(stderr, "[poll%4dms]\n", diff); |
| 166 | m_timeSaved = timeNow; |
| 167 | } |
| 168 | |
| 169 | #endif |
| 170 | |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 171 | // |
| 172 | // Search for changed rectangles on the screen. |
| 173 | // |
| 174 | |
| 175 | void PollingManager::poll() |
| 176 | { |
Constantin Kaplinsky | b247d7d | 2006-02-15 16:14:07 +0000 | [diff] [blame] | 177 | #ifdef DEBUG |
| 178 | debugBeforePoll(); |
| 179 | #endif |
| 180 | |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 181 | // First step: full-screen polling. |
| 182 | |
| 183 | bool changes1 = false; |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 184 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 185 | switch((int)pollingType) { |
| 186 | case 0: |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 187 | changes1 = poll_Dumb(); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 188 | break; |
| 189 | case 1: |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 190 | changes1 = poll_Traditional(); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 191 | break; |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 192 | case 2: |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 193 | changes1 = poll_SkipCycles(); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 194 | break; |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 195 | //case 3: |
| 196 | default: |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 197 | changes1 = poll_DetectVideo(); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 198 | break; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 199 | } |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 200 | |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 201 | // Second step: optional thorough polling of the area around the pointer. |
Constantin Kaplinsky | af2ebeb | 2006-02-10 12:19:11 +0000 | [diff] [blame] | 202 | // We do that only if the pointer position is known and was set recently. |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 203 | |
Constantin Kaplinsky | af2ebeb | 2006-02-10 12:19:11 +0000 | [diff] [blame] | 204 | bool changes2 = false; |
| 205 | if (pollPointer) { |
| 206 | if (m_pointerPosKnown && time(NULL) - m_pointerPosTime >= 5) { |
| 207 | unsetPointerPos(); |
| 208 | } |
| 209 | if (m_pointerPosKnown) { |
| 210 | changes2 = pollPointerArea(); |
| 211 | } |
| 212 | } |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 213 | |
| 214 | // Update if needed. |
| 215 | |
| 216 | if (changes1 || changes2) |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 217 | m_server->tryUpdate(); |
Constantin Kaplinsky | b247d7d | 2006-02-15 16:14:07 +0000 | [diff] [blame] | 218 | |
| 219 | #ifdef DEBUG |
| 220 | debugAfterPoll(); |
| 221 | #endif |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 224 | bool PollingManager::poll_DetectVideo() |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 225 | { |
| 226 | if (!m_server) |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 227 | return false; |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 228 | |
| 229 | const int GRAND_STEP_DIVISOR = 8; |
| 230 | const int VIDEO_THRESHOLD_0 = 3; |
| 231 | const int VIDEO_THRESHOLD_1 = 5; |
| 232 | |
| 233 | bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0); |
| 234 | |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 235 | // FIXME: Save shortcuts in member variables? |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 236 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 237 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 238 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 239 | |
| 240 | Rect rect; |
| 241 | int nTilesChanged = 0; |
| 242 | int idx = 0; |
| 243 | |
| 244 | for (int y = 0; y * 32 < m_height; y++) { |
| 245 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
| 246 | if (scanLine >= tile_h) |
| 247 | break; |
| 248 | int scan_y = y * 32 + scanLine; |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 249 | getRow(scan_y); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 250 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 251 | char *ptr_new = m_rowImage->xim->data; |
| 252 | for (int x = 0; x * 32 < m_width; x++) { |
| 253 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
| 254 | int nBytes = tile_w * bytesPerPixel; |
| 255 | |
| 256 | char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0); |
| 257 | m_rateMatrix[idx] += wasChanged; |
| 258 | |
| 259 | if (grandStep) { |
| 260 | if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) { |
| 261 | m_videoFlags[idx] = 0; |
| 262 | } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) { |
| 263 | m_videoFlags[idx] = 1; |
| 264 | } |
| 265 | m_rateMatrix[idx] = 0; |
| 266 | } |
| 267 | |
| 268 | m_changedFlags[idx] |= wasChanged; |
| 269 | if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) { |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 270 | getTile32(x, y, tile_w, tile_h); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 271 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 272 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 273 | m_server->add_changed(rect); |
| 274 | nTilesChanged++; |
| 275 | m_changedFlags[idx] = 0; |
| 276 | } |
| 277 | |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 278 | ptr_old += nBytes; |
| 279 | ptr_new += nBytes; |
| 280 | idx++; |
| 281 | } |
| 282 | } |
| 283 | |
Constantin Kaplinsky | 6df6990 | 2006-02-03 11:42:00 +0000 | [diff] [blame] | 284 | if (grandStep) |
| 285 | adjustVideoArea(); |
| 286 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 287 | return (nTilesChanged != 0); |
Constantin Kaplinsky | dc54fc1 | 2005-10-19 13:57:16 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 290 | bool PollingManager::poll_SkipCycles() |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 291 | { |
| 292 | if (!m_server) |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 293 | return false; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 294 | |
| 295 | enum { |
| 296 | NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN |
| 297 | }; |
| 298 | |
| 299 | bool grandStep = (m_pollingStep % 8 == 0); |
| 300 | |
| 301 | int nTilesChanged = 0; |
| 302 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 303 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 304 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 305 | char *pstatus = m_statusMatrix; |
| 306 | bool wasChanged; |
| 307 | Rect rect; |
| 308 | |
| 309 | for (int y = 0; y * 32 < m_height; y++) { |
| 310 | int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32; |
| 311 | if (scanLine >= tile_h) |
| 312 | scanLine %= tile_h; |
| 313 | int scan_y = y * 32 + scanLine; |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 314 | getRow(scan_y); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 315 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 316 | char *ptr_new = m_rowImage->xim->data; |
| 317 | for (int x = 0; x * 32 < m_width; x++) { |
| 318 | int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32; |
| 319 | int nBytes = tile_w * bytesPerPixel; |
| 320 | |
| 321 | if (grandStep || *pstatus != CHANGED_AGAIN) { |
| 322 | wasChanged = (*pstatus == CHANGED_AGAIN) ? |
| 323 | true : (memcmp(ptr_old, ptr_new, nBytes) != 0); |
| 324 | if (wasChanged) { |
| 325 | if (grandStep || *pstatus == NOT_CHANGED) { |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 326 | getTile32(x, y, tile_w, tile_h); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 327 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 328 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 329 | m_server->add_changed(rect); |
| 330 | nTilesChanged++; |
| 331 | *pstatus = CHANGED_ONCE; |
| 332 | } else { |
| 333 | *pstatus = CHANGED_AGAIN; |
| 334 | } |
| 335 | } else if (grandStep) { |
| 336 | *pstatus = NOT_CHANGED; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | ptr_old += nBytes; |
| 341 | ptr_new += nBytes; |
| 342 | pstatus++; |
| 343 | } |
| 344 | } |
| 345 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 346 | return (nTilesChanged != 0); |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 349 | bool PollingManager::poll_Traditional() |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 350 | { |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 351 | if (!m_server) |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 352 | return false; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 353 | |
| 354 | int nTilesChanged = 0; |
| 355 | int scanLine = m_pollingOrder[m_pollingStep++ % 32]; |
| 356 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 357 | int bytesPerLine = m_image->xim->bytes_per_line; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 358 | Rect rect; |
| 359 | |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 360 | for (int y = 0; y * 32 < m_height; y++) { |
| 361 | 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] | 362 | if (scanLine >= tile_h) |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 363 | break; |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 364 | int scan_y = y * 32 + scanLine; |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 365 | getRow(scan_y); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 366 | char *ptr_old = m_image->xim->data + scan_y * bytesPerLine; |
| 367 | char *ptr_new = m_rowImage->xim->data; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 368 | for (int x = 0; x * 32 < m_width; x++) { |
| 369 | 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] | 370 | int nBytes = tile_w * bytesPerPixel; |
| 371 | if (memcmp(ptr_old, ptr_new, nBytes)) { |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 372 | getTile32(x, y, tile_w, tile_h); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 373 | m_image->updateRect(m_tileImage, x * 32, y * 32); |
| 374 | rect.setXYWH(x * 32, y * 32, tile_w, tile_h); |
| 375 | m_server->add_changed(rect); |
| 376 | nTilesChanged++; |
| 377 | } |
| 378 | ptr_old += nBytes; |
| 379 | ptr_new += nBytes; |
| 380 | } |
| 381 | } |
| 382 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 383 | return (nTilesChanged != 0); |
Constantin Kaplinsky | af1891c | 2005-09-29 06:18:28 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 386 | // |
| 387 | // Simplest polling method, from the original x0vncserver of VNC4. |
| 388 | // |
| 389 | |
| 390 | bool PollingManager::poll_Dumb() |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 391 | { |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 392 | if (!m_server) |
| 393 | return false; |
| 394 | |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 395 | getScreen(); |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 396 | Rect rect(0, 0, m_width, m_height); |
| 397 | m_server->add_changed(rect); |
| 398 | |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 399 | // Report that some changes have been detected. |
Constantin Kaplinsky | c42eab2 | 2006-02-01 05:59:21 +0000 | [diff] [blame] | 400 | return true; |
Constantin Kaplinsky | 14cd547 | 2005-09-29 17:12:11 +0000 | [diff] [blame] | 401 | } |
Constantin Kaplinsky | 6df6990 | 2006-02-03 11:42:00 +0000 | [diff] [blame] | 402 | |
| 403 | // |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 404 | // Compute coordinates of the rectangle around the pointer. |
Constantin Kaplinsky | 6df6990 | 2006-02-03 11:42:00 +0000 | [diff] [blame] | 405 | // |
Constantin Kaplinsky | 98bfcd3 | 2006-02-10 06:39:35 +0000 | [diff] [blame] | 406 | // ASSUMES: (m_pointerPosKnown != false) |
| 407 | // |
Constantin Kaplinsky | 6df6990 | 2006-02-03 11:42:00 +0000 | [diff] [blame] | 408 | |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 409 | void PollingManager::computePointerArea(Rect *r) |
| 410 | { |
| 411 | int x = m_pointerPos.x - 64; |
| 412 | int y = m_pointerPos.y - 64; |
| 413 | int w = 128; |
| 414 | int h = 128; |
| 415 | if (x < 0) { |
| 416 | w += x; x = 0; |
| 417 | } |
| 418 | if (x + w > m_width) { |
| 419 | w = m_width - x; |
| 420 | } |
| 421 | if (y < 0) { |
| 422 | h += y; y = 0; |
| 423 | } |
| 424 | if (y + h > m_height) { |
| 425 | h = m_height - y; |
| 426 | } |
| 427 | |
| 428 | r->setXYWH(x, y, w, h); |
| 429 | } |
| 430 | |
| 431 | // |
| 432 | // Poll the area under current pointer position. Each pixel of the |
| 433 | // area should be compared. Using such polling option gives higher |
| 434 | // priority to screen area under the pointer. |
| 435 | // |
| 436 | // ASSUMES: (m_server != NULL && m_pointerPosKnown != false) |
| 437 | // |
| 438 | |
| 439 | bool PollingManager::pollPointerArea() |
| 440 | { |
| 441 | Rect r; |
| 442 | computePointerArea(&r); |
| 443 | |
| 444 | // Shortcuts for coordinates. |
| 445 | int x = r.tl.x, y = r.tl.y; |
| 446 | int w = r.width(), h = r.height(); |
| 447 | |
| 448 | // Get new pixels. |
Constantin Kaplinsky | 44e9964 | 2006-05-20 12:58:38 +0000 | [diff] [blame] | 449 | getArea128(x, y, w, h); |
Constantin Kaplinsky | ce676c6 | 2006-02-08 13:36:58 +0000 | [diff] [blame] | 450 | |
| 451 | // Now, try to minimize the rectangle by cutting out unchanged |
| 452 | // borders (at top and bottom). |
| 453 | // |
| 454 | // FIXME: Perhaps we should work on 32x32 tiles (properly aligned) |
| 455 | // to produce a region instead of a rectangle. If there would |
| 456 | // be just one universal polling algorithm, it could be |
| 457 | // better to integrate pointer area polling into that |
| 458 | // algorithm, instead of a separate pollPointerArea() |
| 459 | // function. |
| 460 | |
| 461 | // Shortcuts. |
| 462 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 463 | int oldBytesPerLine = m_image->xim->bytes_per_line; |
| 464 | int newBytesPerLine = m_areaImage->xim->bytes_per_line; |
| 465 | char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel; |
| 466 | char *newPtr = m_areaImage->xim->data; |
| 467 | |
| 468 | // Check and cut out unchanged rows at the top. |
| 469 | int ty; |
| 470 | for (ty = 0; ty < h; ty++) { |
| 471 | if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0) |
| 472 | break; |
| 473 | oldPtr += oldBytesPerLine; |
| 474 | newPtr += newBytesPerLine; |
| 475 | } |
| 476 | if (ty == h) { |
| 477 | return false; // no changes at all |
| 478 | } |
| 479 | y += ty; h -= ty; |
| 480 | |
| 481 | // Check and cut out unchanged rows at the bottom. |
| 482 | oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel; |
| 483 | newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine; |
| 484 | int by; |
| 485 | for (by = 0; by < h - 1; by++) { |
| 486 | if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0) |
| 487 | break; |
| 488 | oldPtr -= oldBytesPerLine; |
| 489 | newPtr -= newBytesPerLine; |
| 490 | } |
| 491 | h -= by; |
| 492 | |
| 493 | // Copy pixels. |
| 494 | m_image->updateRect(m_areaImage, x, y, 0, ty, w, h); |
| 495 | |
| 496 | // Report updates to the server. |
| 497 | Rect rect(x, y, x+w, y+h); |
| 498 | m_server->add_changed(rect); |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | // |
| 503 | // Make video area pattern more regular. |
| 504 | // |
| 505 | // FIXME: Replace the above with a normal comment. |
| 506 | // FIXME: Is the function efficient enough? |
| 507 | // |
| 508 | |
Constantin Kaplinsky | 6df6990 | 2006-02-03 11:42:00 +0000 | [diff] [blame] | 509 | void PollingManager::adjustVideoArea() |
| 510 | { |
| 511 | char newFlags[m_widthTiles * m_heightTiles]; |
| 512 | char *ptr = newFlags; |
| 513 | int x, y; |
| 514 | |
| 515 | // DEBUG: |
| 516 | // int nVideoTiles = 0; |
| 517 | |
| 518 | for (y = 0; y < m_heightTiles; y++) { |
| 519 | for (x = 0; x < m_widthTiles; x++) { |
| 520 | |
| 521 | // DEBUG: |
| 522 | // nVideoTiles += m_videoFlags[y * m_widthTiles + x]; |
| 523 | |
| 524 | int weightedSum = 0, n; |
| 525 | if (y > 0 && x > 0) { |
| 526 | n = (m_videoFlags[ y * m_widthTiles + (x-1)] + |
| 527 | m_videoFlags[(y-1) * m_widthTiles + (x-1)] + |
| 528 | m_videoFlags[(y-1) * m_widthTiles + x ]); |
| 529 | if (n == 3) { |
| 530 | *ptr++ = 1; |
| 531 | continue; |
| 532 | } |
| 533 | weightedSum += n; |
| 534 | } |
| 535 | if (y > 0 && x < m_widthTiles - 1) { |
| 536 | n = (m_videoFlags[ y * m_widthTiles + (x+1)] + |
| 537 | m_videoFlags[(y-1) * m_widthTiles + (x+1)] + |
| 538 | m_videoFlags[(y-1) * m_widthTiles + x ]); |
| 539 | if (n == 3) { |
| 540 | *ptr++ = 1; |
| 541 | continue; |
| 542 | } |
| 543 | weightedSum += n; |
| 544 | } |
| 545 | if (y < m_heightTiles - 1 && x > 0) { |
| 546 | n = (m_videoFlags[ y * m_widthTiles + (x-1)] + |
| 547 | m_videoFlags[(y+1) * m_widthTiles + (x-1)] + |
| 548 | m_videoFlags[(y+1) * m_widthTiles + x ]); |
| 549 | if (n == 3) { |
| 550 | *ptr++ = 1; |
| 551 | continue; |
| 552 | } |
| 553 | weightedSum += n; |
| 554 | } |
| 555 | if (y < m_heightTiles - 1 && x < m_widthTiles - 1) { |
| 556 | n = (m_videoFlags[ y * m_widthTiles + (x+1)] + |
| 557 | m_videoFlags[(y+1) * m_widthTiles + (x+1)] + |
| 558 | m_videoFlags[(y+1) * m_widthTiles + x ]); |
| 559 | if (n == 3) { |
| 560 | *ptr++ = 1; |
| 561 | continue; |
| 562 | } |
| 563 | weightedSum += n; |
| 564 | } |
| 565 | *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x]; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | /// DEBUG: ------------------------------------------------------ |
| 571 | if (nVideoTiles) { |
| 572 | for (y = 0; y < m_heightTiles; y++) { |
| 573 | for (x = 0; x < m_widthTiles; x++) { |
| 574 | printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':'); |
| 575 | } |
| 576 | printf(" "); |
| 577 | for (x = 0; x < m_widthTiles; x++) { |
| 578 | printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':'); |
| 579 | } |
| 580 | printf("\n"); |
| 581 | } |
| 582 | printf("\n"); |
| 583 | } |
| 584 | /// ------------------------------------------------------------- |
| 585 | */ |
| 586 | |
| 587 | memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles); |
| 588 | } |