blob: 2d8b87af4c0e5cb7e1bd597cc5ebd6647e8645a2 [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
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000022// 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 Kaplinskyaf1891c2005-09-29 06:18:28 +000026#include <stdio.h>
27#include <string.h>
28#include <sys/time.h>
29#include <X11/Xlib.h>
30#include <rfb/VNCServer.h>
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000031#include <rfb/Configuration.h>
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000032#include <rfb/ServerCore.h>
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000033
34#include <x0vncserver/Image.h>
35#include <x0vncserver/PollingManager.h>
36
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000037BoolParameter PollingManager::pollPointer
38("PollPointer",
Constantin Kaplinskya4f07362006-02-10 11:54:49 +000039 "DEBUG: Poll area under the pointer with higher priority",
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000040 true);
41
42IntParameter PollingManager::pollingType
43("PollingType",
Constantin Kaplinskya4f07362006-02-10 11:54:49 +000044 "DEBUG: Select particular polling algorithm (0..3)",
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000045 3);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000046
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000047const int PollingManager::m_pollingOrder[32] = {
48 0, 16, 8, 24, 4, 20, 12, 28,
49 10, 26, 18, 2, 22, 6, 30, 14,
50 1, 17, 9, 25, 7, 23, 15, 31,
51 19, 3, 27, 11, 29, 13, 5, 21
52};
53
54//
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000055// Constructor.
56//
57// Note that dpy and image should remain valid during the object
58// lifetime, while factory is used only in the constructor itself.
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000059//
60
61PollingManager::PollingManager(Display *dpy, Image *image,
62 ImageFactory *factory)
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000063 : m_dpy(dpy), m_server(0), m_image(image), m_pointerPosKnown(false),
64 m_pollingStep(0)
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000065{
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000066 // Save width and height of the screen (and the image).
67 m_width = m_image->xim->width;
68 m_height = m_image->xim->height;
69
70 // Compute width and height in 32x32 tiles.
71 m_widthTiles = (m_width + 31) / 32;
72 m_heightTiles = (m_height + 31) / 32;
73
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000074 // Create additional images used in the polling algorithm.
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000075 // FIXME: verify that these images use the same pixel format as in m_image.
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000076 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000077 m_tileImage = factory->newImage(m_dpy, 32, 32);
Constantin Kaplinskyce676c62006-02-08 13:36:58 +000078 m_areaImage = factory->newImage(m_dpy, 128, 128);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000079
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +000080 // FIXME: Extend the comment.
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +000081 // Create a matrix with one byte per each 32x32 tile. It will be
82 // used to limit the rate of updates on continuously-changed screen
83 // areas (like video).
84 int numTiles = m_widthTiles * m_heightTiles;
85 m_statusMatrix = new char[numTiles];
86 memset(m_statusMatrix, 0, numTiles);
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +000087
88 // FIXME: Extend the comment.
89 // Create a matrix with one byte per each 32x32 tile. It will be
90 // used to limit the rate of updates on continuously-changed screen
91 // areas (like video).
92 m_rateMatrix = new char[numTiles];
93 m_videoFlags = new char[numTiles];
94 m_changedFlags = new char[numTiles];
95 memset(m_rateMatrix, 0, numTiles);
96 memset(m_videoFlags, 0, numTiles);
97 memset(m_changedFlags, 0, numTiles);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000098}
99
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000100PollingManager::~PollingManager()
101{
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000102 delete[] m_changedFlags;
103 delete[] m_videoFlags;
104 delete[] m_rateMatrix;
105
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000106 delete[] m_statusMatrix;
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000107
108 delete m_areaImage;
109 delete m_tileImage;
110 delete m_rowImage;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000111}
112
113//
114// Register VNCServer object.
115//
116
117void PollingManager::setVNCServer(VNCServer *s)
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000118{
119 m_server = s;
120}
121
122//
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000123// Update current pointer position which may be used as a hint for
124// polling algorithms.
125//
126
127void PollingManager::setPointerPos(const Point &pos)
128{
129 m_pointerPos = pos;
130 m_pointerPosKnown = true;
131}
132
133//
134// Indicate that current pointer position is unknown.
135// FIXME: Perhaps this should be done automatically after a number of
136// polling cycles if the cursor position have not been changed?
137//
138
139void PollingManager::unsetPointerPos()
140{
141 m_pointerPosKnown = false;
142}
143
144//
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000145// DEBUG: a version of poll() measuring time spent in the function.
146//
147
148void PollingManager::pollDebug()
149{
150 struct timeval timeSaved, timeNow;
151 struct timezone tz;
152 timeSaved.tv_sec = 0;
153 timeSaved.tv_usec = 0;
154 gettimeofday(&timeSaved, &tz);
Constantin Kaplinsky6e34af42006-02-10 06:43:56 +0000155 int step = m_pollingStep;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000156
157 poll();
158
159 gettimeofday(&timeNow, &tz);
160 int diff = (int)((timeNow.tv_usec - timeSaved.tv_usec + 500) / 1000 +
161 (timeNow.tv_sec - timeSaved.tv_sec) * 1000);
162 if (diff != 0)
Constantin Kaplinsky6e34af42006-02-10 06:43:56 +0000163 fprintf(stderr, "DEBUG: poll(): %4d ms [step %2d]\n", diff, step % 32);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000164}
165
166//
167// Search for changed rectangles on the screen.
168//
169
170void PollingManager::poll()
171{
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000172 // First step: full-screen polling.
173
174 bool changes1 = false;
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000175
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000176 switch((int)pollingType) {
177 case 0:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000178 changes1 = poll_Dumb();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000179 break;
180 case 1:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000181 changes1 = poll_Traditional();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000182 break;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000183 case 2:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000184 changes1 = poll_SkipCycles();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000185 break;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000186//case 3:
187 default:
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000188 changes1 = poll_DetectVideo();
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000189 break;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000190 }
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000191
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000192 // Second step: optional thorough polling of the area around the pointer.
193
194 bool changes2 = pollPointer && m_pointerPosKnown && pollPointerArea();
195
196 // Update if needed.
197
198 if (changes1 || changes2)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000199 m_server->tryUpdate();
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000200}
201
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000202bool PollingManager::poll_DetectVideo()
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000203{
204 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000205 return false;
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000206
207 const int GRAND_STEP_DIVISOR = 8;
208 const int VIDEO_THRESHOLD_0 = 3;
209 const int VIDEO_THRESHOLD_1 = 5;
210
211 bool grandStep = (m_pollingStep % GRAND_STEP_DIVISOR == 0);
212
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000213 // FIXME: Save shortcuts in member variables?
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000214 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
215 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
216 int bytesPerLine = m_image->xim->bytes_per_line;
217
218 Rect rect;
219 int nTilesChanged = 0;
220 int idx = 0;
221
222 for (int y = 0; y * 32 < m_height; y++) {
223 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
224 if (scanLine >= tile_h)
225 break;
226 int scan_y = y * 32 + scanLine;
227 m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y);
228 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
229 char *ptr_new = m_rowImage->xim->data;
230 for (int x = 0; x * 32 < m_width; x++) {
231 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
232 int nBytes = tile_w * bytesPerPixel;
233
234 char wasChanged = (memcmp(ptr_old, ptr_new, nBytes) != 0);
235 m_rateMatrix[idx] += wasChanged;
236
237 if (grandStep) {
238 if (m_rateMatrix[idx] <= VIDEO_THRESHOLD_0) {
239 m_videoFlags[idx] = 0;
240 } else if (m_rateMatrix[idx] >= VIDEO_THRESHOLD_1) {
241 m_videoFlags[idx] = 1;
242 }
243 m_rateMatrix[idx] = 0;
244 }
245
246 m_changedFlags[idx] |= wasChanged;
247 if ( m_changedFlags[idx] && (!m_videoFlags[idx] || grandStep) ) {
248 if (tile_w == 32 && tile_h == 32) {
249 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32);
250 } else {
251 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32,
252 tile_w, tile_h);
253 }
254 m_image->updateRect(m_tileImage, x * 32, y * 32);
255 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
256 m_server->add_changed(rect);
257 nTilesChanged++;
258 m_changedFlags[idx] = 0;
259 }
260
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000261 ptr_old += nBytes;
262 ptr_new += nBytes;
263 idx++;
264 }
265 }
266
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000267 if (grandStep)
268 adjustVideoArea();
269
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000270 return (nTilesChanged != 0);
Constantin Kaplinskydc54fc12005-10-19 13:57:16 +0000271}
272
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000273bool PollingManager::poll_SkipCycles()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000274{
275 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000276 return false;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000277
278 enum {
279 NOT_CHANGED, CHANGED_ONCE, CHANGED_AGAIN
280 };
281
282 bool grandStep = (m_pollingStep % 8 == 0);
283
284 int nTilesChanged = 0;
285 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
286 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
287 int bytesPerLine = m_image->xim->bytes_per_line;
288 char *pstatus = m_statusMatrix;
289 bool wasChanged;
290 Rect rect;
291
292 for (int y = 0; y * 32 < m_height; y++) {
293 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
294 if (scanLine >= tile_h)
295 scanLine %= tile_h;
296 int scan_y = y * 32 + scanLine;
297 m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y);
298 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
299 char *ptr_new = m_rowImage->xim->data;
300 for (int x = 0; x * 32 < m_width; x++) {
301 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
302 int nBytes = tile_w * bytesPerPixel;
303
304 if (grandStep || *pstatus != CHANGED_AGAIN) {
305 wasChanged = (*pstatus == CHANGED_AGAIN) ?
306 true : (memcmp(ptr_old, ptr_new, nBytes) != 0);
307 if (wasChanged) {
308 if (grandStep || *pstatus == NOT_CHANGED) {
309 if (tile_w == 32 && tile_h == 32) {
310 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32);
311 } else {
312 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32,
313 tile_w, tile_h);
314 }
315 m_image->updateRect(m_tileImage, x * 32, y * 32);
316 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
317 m_server->add_changed(rect);
318 nTilesChanged++;
319 *pstatus = CHANGED_ONCE;
320 } else {
321 *pstatus = CHANGED_AGAIN;
322 }
323 } else if (grandStep) {
324 *pstatus = NOT_CHANGED;
325 }
326 }
327
328 ptr_old += nBytes;
329 ptr_new += nBytes;
330 pstatus++;
331 }
332 }
333
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000334 return (nTilesChanged != 0);
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000335}
336
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000337bool PollingManager::poll_Traditional()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000338{
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000339 if (!m_server)
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000340 return false;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000341
342 int nTilesChanged = 0;
343 int scanLine = m_pollingOrder[m_pollingStep++ % 32];
344 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
345 int bytesPerLine = m_image->xim->bytes_per_line;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000346 Rect rect;
347
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000348 for (int y = 0; y * 32 < m_height; y++) {
349 int tile_h = (m_height - y * 32 >= 32) ? 32 : m_height - y * 32;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000350 if (scanLine >= tile_h)
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000351 break;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000352 int scan_y = y * 32 + scanLine;
353 m_rowImage->get(DefaultRootWindow(m_dpy), 0, scan_y);
354 char *ptr_old = m_image->xim->data + scan_y * bytesPerLine;
355 char *ptr_new = m_rowImage->xim->data;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000356 for (int x = 0; x * 32 < m_width; x++) {
357 int tile_w = (m_width - x * 32 >= 32) ? 32 : m_width - x * 32;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000358 int nBytes = tile_w * bytesPerPixel;
359 if (memcmp(ptr_old, ptr_new, nBytes)) {
360 if (tile_w == 32 && tile_h == 32) {
361 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32);
362 } else {
363 m_tileImage->get(DefaultRootWindow(m_dpy), x * 32, y * 32,
364 tile_w, tile_h);
365 }
366 m_image->updateRect(m_tileImage, x * 32, y * 32);
367 rect.setXYWH(x * 32, y * 32, tile_w, tile_h);
368 m_server->add_changed(rect);
369 nTilesChanged++;
370 }
371 ptr_old += nBytes;
372 ptr_new += nBytes;
373 }
374 }
375
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000376 return (nTilesChanged != 0);
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000377}
378
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000379//
380// Simplest polling method, from the original x0vncserver of VNC4.
381//
382
383bool PollingManager::poll_Dumb()
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000384{
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000385 if (!m_server)
386 return false;
387
388 m_image->get(DefaultRootWindow(m_dpy));
389 Rect rect(0, 0, m_width, m_height);
390 m_server->add_changed(rect);
391
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000392 // Report that some changes have been detected.
Constantin Kaplinskyc42eab22006-02-01 05:59:21 +0000393 return true;
Constantin Kaplinsky14cd5472005-09-29 17:12:11 +0000394}
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000395
396//
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000397// Compute coordinates of the rectangle around the pointer.
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000398//
Constantin Kaplinsky98bfcd32006-02-10 06:39:35 +0000399// ASSUMES: (m_pointerPosKnown != false)
400//
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000401
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000402void PollingManager::computePointerArea(Rect *r)
403{
404 int x = m_pointerPos.x - 64;
405 int y = m_pointerPos.y - 64;
406 int w = 128;
407 int h = 128;
408 if (x < 0) {
409 w += x; x = 0;
410 }
411 if (x + w > m_width) {
412 w = m_width - x;
413 }
414 if (y < 0) {
415 h += y; y = 0;
416 }
417 if (y + h > m_height) {
418 h = m_height - y;
419 }
420
421 r->setXYWH(x, y, w, h);
422}
423
424//
425// Poll the area under current pointer position. Each pixel of the
426// area should be compared. Using such polling option gives higher
427// priority to screen area under the pointer.
428//
429// ASSUMES: (m_server != NULL && m_pointerPosKnown != false)
430//
431
432bool PollingManager::pollPointerArea()
433{
434 Rect r;
435 computePointerArea(&r);
436
437 // Shortcuts for coordinates.
438 int x = r.tl.x, y = r.tl.y;
439 int w = r.width(), h = r.height();
440
441 // Get new pixels.
442 if (w == 128 && h == 128) {
443 m_areaImage->get(DefaultRootWindow(m_dpy), x, y);
444 } else {
445 m_areaImage->get(DefaultRootWindow(m_dpy), x, y, w, h);
446 }
447
448 // Now, try to minimize the rectangle by cutting out unchanged
449 // borders (at top and bottom).
450 //
451 // FIXME: Perhaps we should work on 32x32 tiles (properly aligned)
452 // to produce a region instead of a rectangle. If there would
453 // be just one universal polling algorithm, it could be
454 // better to integrate pointer area polling into that
455 // algorithm, instead of a separate pollPointerArea()
456 // function.
457
458 // Shortcuts.
459 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
460 int oldBytesPerLine = m_image->xim->bytes_per_line;
461 int newBytesPerLine = m_areaImage->xim->bytes_per_line;
462 char *oldPtr = m_image->xim->data + y * oldBytesPerLine + x * bytesPerPixel;
463 char *newPtr = m_areaImage->xim->data;
464
465 // Check and cut out unchanged rows at the top.
466 int ty;
467 for (ty = 0; ty < h; ty++) {
468 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
469 break;
470 oldPtr += oldBytesPerLine;
471 newPtr += newBytesPerLine;
472 }
473 if (ty == h) {
474 return false; // no changes at all
475 }
476 y += ty; h -= ty;
477
478 // Check and cut out unchanged rows at the bottom.
479 oldPtr = m_image->xim->data + (y+h-1) * oldBytesPerLine + x * bytesPerPixel;
480 newPtr = m_areaImage->xim->data + (ty+h-1) * newBytesPerLine;
481 int by;
482 for (by = 0; by < h - 1; by++) {
483 if (memcmp(oldPtr, newPtr, w * bytesPerPixel) != 0)
484 break;
485 oldPtr -= oldBytesPerLine;
486 newPtr -= newBytesPerLine;
487 }
488 h -= by;
489
490 // Copy pixels.
491 m_image->updateRect(m_areaImage, x, y, 0, ty, w, h);
492
493 // Report updates to the server.
494 Rect rect(x, y, x+w, y+h);
495 m_server->add_changed(rect);
496 return true;
497}
498
499//
500// Make video area pattern more regular.
501//
502// FIXME: Replace the above with a normal comment.
503// FIXME: Is the function efficient enough?
504//
505
Constantin Kaplinsky6df69902006-02-03 11:42:00 +0000506void PollingManager::adjustVideoArea()
507{
508 char newFlags[m_widthTiles * m_heightTiles];
509 char *ptr = newFlags;
510 int x, y;
511
512 // DEBUG:
513 // int nVideoTiles = 0;
514
515 for (y = 0; y < m_heightTiles; y++) {
516 for (x = 0; x < m_widthTiles; x++) {
517
518 // DEBUG:
519 // nVideoTiles += m_videoFlags[y * m_widthTiles + x];
520
521 int weightedSum = 0, n;
522 if (y > 0 && x > 0) {
523 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
524 m_videoFlags[(y-1) * m_widthTiles + (x-1)] +
525 m_videoFlags[(y-1) * m_widthTiles + x ]);
526 if (n == 3) {
527 *ptr++ = 1;
528 continue;
529 }
530 weightedSum += n;
531 }
532 if (y > 0 && x < m_widthTiles - 1) {
533 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
534 m_videoFlags[(y-1) * m_widthTiles + (x+1)] +
535 m_videoFlags[(y-1) * m_widthTiles + x ]);
536 if (n == 3) {
537 *ptr++ = 1;
538 continue;
539 }
540 weightedSum += n;
541 }
542 if (y < m_heightTiles - 1 && x > 0) {
543 n = (m_videoFlags[ y * m_widthTiles + (x-1)] +
544 m_videoFlags[(y+1) * m_widthTiles + (x-1)] +
545 m_videoFlags[(y+1) * m_widthTiles + x ]);
546 if (n == 3) {
547 *ptr++ = 1;
548 continue;
549 }
550 weightedSum += n;
551 }
552 if (y < m_heightTiles - 1 && x < m_widthTiles - 1) {
553 n = (m_videoFlags[ y * m_widthTiles + (x+1)] +
554 m_videoFlags[(y+1) * m_widthTiles + (x+1)] +
555 m_videoFlags[(y+1) * m_widthTiles + x ]);
556 if (n == 3) {
557 *ptr++ = 1;
558 continue;
559 }
560 weightedSum += n;
561 }
562 *ptr++ = (weightedSum <= 3) ? 0 : m_videoFlags[y * m_widthTiles + x];
563 }
564 }
565
566 /*
567 /// DEBUG: ------------------------------------------------------
568 if (nVideoTiles) {
569 for (y = 0; y < m_heightTiles; y++) {
570 for (x = 0; x < m_widthTiles; x++) {
571 printf("%c", m_videoFlags[y * m_widthTiles + x] ? '@' : ':');
572 }
573 printf(" ");
574 for (x = 0; x < m_widthTiles; x++) {
575 printf("%c", newFlags[y * m_widthTiles + x] ? '@' : ':');
576 }
577 printf("\n");
578 }
579 printf("\n");
580 }
581 /// -------------------------------------------------------------
582 */
583
584 memcpy(m_videoFlags, newFlags, m_widthTiles * m_heightTiles);
585}