blob: c02e509254487cd9c20a722af0c29c9481b2611d [file] [log] [blame]
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +00001/* 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 Kaplinsky14cd5472005-09-29 17:12:11 +000027#include <rfb/Configuration.h>
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000028
29#include <x0vncserver/Image.h>
30#include <x0vncserver/PollingManager.h>
31
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +000032IntParameter pollingType("PollingType", "Polling algorithm to use (0..3)", 3);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000033
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000034const 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 Kaplinsky14cd5472005-09-29 17:12:11 +000042// 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 Kaplinskyaf1891c2005-09-29 06:18:28 +000046//
47
48PollingManager::PollingManager(Display *dpy, Image *image,
49 ImageFactory *factory)
50 : m_dpy(dpy), m_server(0), m_image(image), m_pollingStep(0)
51{
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000052 // 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 Kaplinskyaf1891c2005-09-29 06:18:28 +000060 // 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 Kaplinsky14cd5472005-09-29 17:12:11 +000062 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000063 m_tileImage = factory->newImage(m_dpy, 32, 32);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000064
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +000065 // FIXME: Extend the comment.
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000066 // 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 Kaplinskydc54fc12005-10-19 13:57:16 +000072
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 Kaplinskyaf1891c2005-09-29 06:18:28 +000083}
84
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000085PollingManager::~PollingManager()
86{
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +000087 delete[] m_changedFlags;
88 delete[] m_videoFlags;
89 delete[] m_rateMatrix;
90
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000091 delete[] m_statusMatrix;
92}
93
94//
95// Register VNCServer object.
96//
97
98void PollingManager::setVNCServer(VNCServer *s)
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000099{
100 m_server = s;
101}
102
103//
104// DEBUG: a version of poll() measuring time spent in the function.
105//
106
107void 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
128void PollingManager::poll()
129{
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000130 bool someChanges = false;
131
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000132 switch((int)pollingType) {
133 case 0:
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000134 someChanges = poll_Dumb();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000135 break;
136 case 1:
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000137 someChanges = poll_Traditional();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000138 break;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000139 case 2:
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000140 someChanges = poll_SkipCycles();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000141 break;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000142//case 3:
143 default:
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000144 someChanges = poll_DetectVideo();
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000145 break;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000146 }
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000147
148 if (someChanges)
149 m_server->tryUpdate();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000150}
151
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000152bool PollingManager::poll_DetectVideo()
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000153{
154 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000155 return false;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000156
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 Kaplinskydc54fc12005-10-19 13:57:16 +0000211 ptr_old += nBytes;
212 ptr_new += nBytes;
213 idx++;
214 }
215 }
216
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000217 return (nTilesChanged != 0);
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000218}
219
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000220bool PollingManager::poll_SkipCycles()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000221{
222 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000223 return false;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000224
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 Kaplinskyc42eab22006-02-01 05:59:21 +0000281 return (nTilesChanged != 0);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000282}
283
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000284bool PollingManager::poll_Traditional()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000285{
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000286 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000287 return false;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000288
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 Kaplinskyaf1891c2005-09-29 06:18:28 +0000293 Rect rect;
294
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000295 for (int y = 0; y * 32 < m_height; y++) {
296 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000297 if (scanLine >= tile_h)
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000298 break;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000299 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 Kaplinsky14cd5472005-09-29 17:12:11 +0000303 for (int x = 0; x * 32 < m_width; x++) {
304 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000305 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 Kaplinskyc42eab22006-02-01 05:59:21 +0000323 return (nTilesChanged != 0);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000324}
325
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000326//
327// Simplest polling method, from the original x0vncserver of VNC4.
328//
329
330bool PollingManager::poll_Dumb()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000331{
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000332 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 Kaplinsky14cd5472005-09-29 17:12:11 +0000342}