blob: 015b3ca63583a31325e90b125039787f76b8b9f7 [file] [log] [blame]
Constantin Kaplinsky82f7b012008-05-30 10:03:30 +00001/* Copyright (C) 2004-2008 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00002 *
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//
20// PollingManager.h
21//
22
23#ifndef __POLLINGMANAGER_H__
24#define __POLLINGMANAGER_H__
25
26#include <X11/Xlib.h>
27#include <rfb/VNCServer.h>
28
29#include <x0vncserver/Image.h>
Constantin Kaplinsky2c019832008-05-30 11:02:04 +000030#include <x0vncserver/XPixelBuffer.h>
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000031
32#ifdef DEBUG
33#include <x0vncserver/TimeMillis.h>
34#endif
35
36using namespace rfb;
37
38class PollingManager {
39
40public:
41
Constantin Kaplinsky2c019832008-05-30 11:02:04 +000042 PollingManager(Display *dpy, XPixelBuffer *buffer, ImageFactory *factory,
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000043 int offsetLeft = 0, int offsetTop = 0);
44 virtual ~PollingManager();
45
46 void setVNCServer(VNCServer *s);
47
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000048 // Currently, these functions do nothing. In past versions, we used
49 // to poll area around the pointer if its position has been changed
50 // recently. But that rather decreased overal polling performance,
51 // so we don't do that any more. However, pointer position may be a
52 // useful hint and might be used in future code, so we do not remove
53 // these functions, just in case.
54 void setPointerPos(const Point &pos) {}
55 void unsetPointerPos() {}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000056
57 void poll();
58
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000059protected:
60
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +000061 // Screen polling. Returns true if some changes were detected.
62 bool pollScreen();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000063
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000064 Display *m_dpy;
65 VNCServer *m_server;
66
67 Image *m_image;
Constantin Kaplinskyadebffb2008-01-18 14:33:05 +000068 const int m_bytesPerPixel;
Constantin Kaplinskyec45c482008-01-18 14:13:16 +000069
Constantin Kaplinskyadebffb2008-01-18 14:33:05 +000070 const int m_offsetLeft;
71 const int m_offsetTop;
72 const int m_width;
73 const int m_height;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000074
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000075private:
76
77 inline void getScreen() {
78 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
79 }
80
Constantin Kaplinskya119b482007-10-04 06:02:02 +000081 inline void getScreenRect(const Rect& r) {
82 m_image->get(DefaultRootWindow(m_dpy),
83 m_offsetLeft + r.tl.x, m_offsetTop + r.tl.y,
84 r.width(), r.height(), r.tl.x, r.tl.y);
85 }
86
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +000087 inline void getRow(int x, int y, int w) {
Constantin Kaplinsky41de2422008-01-19 09:03:47 +000088 if (w == m_width) {
89 // Getting full row may be more efficient.
90 m_rowImage->get(DefaultRootWindow(m_dpy),
91 m_offsetLeft, m_offsetTop + y);
92 } else {
93 m_rowImage->get(DefaultRootWindow(m_dpy),
94 m_offsetLeft + x, m_offsetTop + y, w, 1);
95 }
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +000096 }
97
98 inline void getColumn(int x, int y, int h) {
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +000099 m_columnImage->get(DefaultRootWindow(m_dpy),
100 m_offsetLeft + x, m_offsetTop + y, 1, h);
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000101 }
102
Constantin Kaplinsky04aa5202008-01-18 15:37:15 +0000103 inline int getTileIndex(int x, int y) {
104 int tile_x = x / 32;
105 int tile_y = y / 32;
106 return tile_y * m_widthTiles + tile_x;
107 }
108
Constantin Kaplinsky9d37e5c2008-01-18 11:17:26 +0000109 int checkRow(int x, int y, int w);
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000110 int checkColumn(int x, int y, int h, bool *pChangeFlags);
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000111 int sendChanges();
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000112
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000113 // Check neighboring tiles and update m_changeFlags[].
114 void checkNeighbors();
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000115
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000116 // DEBUG: Print the list of changed tiles.
Constantin Kaplinsky850de2b2008-01-17 19:14:37 +0000117 void printChanges(const char *header) const;
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000118
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000119 // Additional images used in polling algorithms.
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +0000120 Image *m_rowImage; // one row of the framebuffer
121 Image *m_columnImage; // one column of the framebuffer
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000122
Constantin Kaplinskyadebffb2008-01-18 14:33:05 +0000123 const int m_widthTiles; // shortcut for ((m_width + 31) / 32)
124 const int m_heightTiles; // shortcut for ((m_height + 31) / 32)
125 const int m_numTiles; // shortcut for (m_widthTiles * m_heightTiles)
Constantin Kaplinsky20390a22008-01-17 15:17:36 +0000126
Constantin Kaplinsky85b5eb92008-01-17 17:41:48 +0000127 // m_changeFlags[] array will hold boolean values corresponding to
128 // each 32x32 tile. If a value is true, then we've detected a change
129 // in that tile.
130 bool *m_changeFlags;
131
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000132 unsigned int m_pollingStep;
133 static const int m_pollingOrder[];
134
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000135#ifdef DEBUG
136private:
137
138 void debugBeforePoll();
139 void debugAfterPoll();
140
141 TimeMillis m_timeSaved;
142#endif
143
144};
145
146#endif // __POLLINGMANAGER_H__