Qt 4.8
qvncserver.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QSCREENVNC_P_H
43 #define QSCREENVNC_P_H
44 
45 //
46 // W A R N I N G
47 // -------------
48 //
49 // This file is not part of the Qt API. It exists for the convenience
50 // of the QLibrary class. This header file may change from version to version
51 // without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include "qvncintegration.h"
57 #include "qvnccursor.h"
58 #define QT_NO_QWS_CURSOR
59 
60 #ifndef QT_NO_QWS_VNC
61 
62 #include <QtCore/qvarlengtharray.h>
63 #include <QtCore/qsharedmemory.h>
64 #include <QtNetwork/qtcpsocket.h>
65 #include <QtNetwork/qtcpserver.h>
66 
68 
69 class QVNCServer;
70 
71 #ifndef QT_NO_QWS_CURSOR
72 class QVNCCursor : public QProxyScreenCursor
73 {
74 public:
76  ~QVNCCursor();
77 
78  void hide();
79  void show();
80  void set(const QImage &image, int hotx, int hoty);
81  void move(int x, int y);
82 
83 private:
84  void setDirty(const QRect &r) const;
86 };
87 
89 {
90 public:
93 
94  void set(const QImage &image, int hotx, int hoty);
95  void write() const;
96 
97 private:
99 };
100 #endif // QT_NO_QWS_CURSOR
101 
102 #define MAP_TILE_SIZE 16
103 #define MAP_WIDTH 1280 / MAP_TILE_SIZE
104 #define MAP_HEIGHT 1024 / MAP_TILE_SIZE
105 
106 class QVNCDirtyMap
107 {
108 public:
110  virtual ~QVNCDirtyMap();
111 
112  void reset();
113  bool dirty(int x, int y) const;
114  virtual void setDirty(int x, int y, bool force = false) = 0;
115  void setClean(int x, int y);
116 
117  int bytesPerPixel;
118 
119  int numDirty;
120  int mapWidth;
121  int mapHeight;
122 
123 protected:
124  uchar *map;
126  uchar *buffer;
127  int bufferWidth;
128  int bufferHeight;
129  int bufferStride;
130  int numTiles;
131 };
132 
133 template <class T>
134 class QVNCDirtyMapOptimized : public QVNCDirtyMap
135 {
136 public:
139 
140  void setDirty(int x, int y, bool force = false);
141 };
142 
143 class QRfbRect
144 {
145 public:
146  QRfbRect() {}
148  x = _x; y = _y; w = _w; h = _h;
149  }
150 
151  void read(QTcpSocket *s);
152  void write(QTcpSocket *s) const;
153 
154  quint16 x;
155  quint16 y;
156  quint16 w;
157  quint16 h;
158 };
159 
160 class QRfbPixelFormat
161 {
162 public:
163  static int size() { return 16; }
164 
165  void read(QTcpSocket *s);
166  void write(QTcpSocket *s);
167 
168  int bitsPerPixel;
169  int depth;
170  bool bigEndian;
171  bool trueColor;
172  int redBits;
173  int greenBits;
174  int blueBits;
175  int redShift;
176  int greenShift;
177  int blueShift;
178 };
179 
180 class QRfbServerInit
181 {
182 public:
183  QRfbServerInit() { name = 0; }
184  ~QRfbServerInit() { delete[] name; }
185 
186  int size() const { return QRfbPixelFormat::size() + 8 + strlen(name); }
187  void setName(const char *n);
188 
189  void read(QTcpSocket *s);
190  void write(QTcpSocket *s);
191 
192  quint16 width;
193  quint16 height;
195  char *name;
196 };
197 
198 class QRfbSetEncodings
199 {
200 public:
201  bool read(QTcpSocket *s);
202 
203  quint16 count;
204 };
205 
207 {
208 public:
209  bool read(QTcpSocket *s);
210 
211  char incremental;
212  QRfbRect rect;
213 };
214 
215 class QRfbKeyEvent
216 {
217 public:
218  bool read(QTcpSocket *s);
219 
220  char down;
221  int keycode;
222  int unicode;
223 };
224 
225 class QRfbPointerEvent
226 {
227 public:
228  bool read(QTcpSocket *s);
229 
230  Qt::MouseButtons buttons;
231  enum { WheelNone,
235  WheelRight
236  } wheelDirection;
237  quint16 x;
238  quint16 y;
239 };
240 
241 class QRfbClientCutText
242 {
243 public:
244  bool read(QTcpSocket *s);
245 
246  quint32 length;
247 };
248 
249 class QVNCScreenPrivate : public QObject
250 {
251 public:
252  QVNCScreenPrivate(QVNCScreen *parent, int screenId);
254 
255  void setDirty(const QRect &rect, bool force = false);
256  void configure();
257 
258  qreal dpiX;
259  qreal dpiY;
260  bool doOnScreenSurface;
262  int refreshRate;
263  QVNCServer *vncServer;
264 
265 #if !defined(QT_NO_QWS_MULTIPROCESS) && !defined(QT_NO_SHAREDMEMORY)
266  QSharedMemory shm;
267 #endif
268 
269  QVNCScreen *q_ptr;
270 };
271 
272 class QRfbEncoder
273 {
274 public:
276  virtual ~QRfbEncoder() {}
277 
278  virtual void write() = 0;
279 
280 protected:
282 };
283 
284 class QRfbRawEncoder : public QRfbEncoder
285 {
286 public:
288 
289  void write();
290 
291 private:
292  QByteArray buffer;
293 };
294 
295 template <class SRC> class QRfbHextileEncoder;
296 
297 template <class SRC>
299 {
300 public:
302  bool read(const uchar *data, int width, int height, int stride);
303  void write(QTcpSocket *socket) const;
304 
305 private:
306  QRfbHextileEncoder<SRC> *encoder;
307 };
308 
309 template <class SRC>
311 {
312 public:
314  bool read(const uchar *data, int width, int height, int stride);
315  void write(QTcpSocket *socket) const;
316 
317 private:
318  struct Rect {
319  quint8 xy;
320  quint8 wh;
321  } Q_PACKED rects[8 * 16];
322 
323  quint8 numRects;
324  QRfbHextileEncoder<SRC> *encoder;
325 
326 private:
327  inline int lastx() const { return rectx(numRects); }
328  inline int lasty() const { return recty(numRects); }
329  inline int rectx(int r) const { return rects[r].xy >> 4; }
330  inline int recty(int r) const { return rects[r].xy & 0x0f; }
331  inline int width(int r) const { return (rects[r].wh >> 4) + 1; }
332  inline int height(int r) const { return (rects[r].wh & 0x0f) + 1; }
333 
334  inline void setX(int r, int x) {
335  rects[r].xy = (x << 4) | (rects[r].xy & 0x0f);
336  }
337  inline void setY(int r, int y) {
338  rects[r].xy = (rects[r].xy & 0xf0) | y;
339  }
340  inline void setWidth(int r, int width) {
341  rects[r].wh = ((width - 1) << 4) | (rects[r].wh & 0x0f);
342  }
343  inline void setHeight(int r, int height) {
344  rects[r].wh = (rects[r].wh & 0xf0) | (height - 1);
345  }
346 
347  inline void setWidth(int width) { setWidth(numRects, width); }
348  inline void setHeight(int height) { setHeight(numRects, height); }
349  inline void setX(int x) { setX(numRects, x); }
350  inline void setY(int y) { setY(numRects, y); }
351  void next();
352 };
353 
354 template <class SRC>
356 {
357 public:
359  bool read(const uchar *data, int width, int height, int stride);
360  void write(QTcpSocket *socket) const;
361 
362 private:
363  inline quint8* rect(int r) {
364  return rects.data() + r * (bpp + 2);
365  }
366  inline const quint8* rect(int r) const {
367  return rects.constData() + r * (bpp + 2);
368  }
369  inline void setX(int r, int x) {
370  quint8 *ptr = rect(r) + bpp;
371  *ptr = (x << 4) | (*ptr & 0x0f);
372  }
373  inline void setY(int r, int y) {
374  quint8 *ptr = rect(r) + bpp;
375  *ptr = (*ptr & 0xf0) | y;
376  }
377  void setColor(SRC color);
378  inline int rectx(int r) const {
379  const quint8 *ptr = rect(r) + bpp;
380  return *ptr >> 4;
381  }
382  inline int recty(int r) const {
383  const quint8 *ptr = rect(r) + bpp;
384  return *ptr & 0x0f;
385  }
386  inline void setWidth(int r, int width) {
387  quint8 *ptr = rect(r) + bpp + 1;
388  *ptr = ((width - 1) << 4) | (*ptr & 0x0f);
389  }
390  inline void setHeight(int r, int height) {
391  quint8 *ptr = rect(r) + bpp + 1;
392  *ptr = (*ptr & 0xf0) | (height - 1);
393  }
394 
395  bool beginRect();
396  void endRect();
397 
398  static const int maxRectsSize = 16 * 16;
400 
401  quint8 bpp;
402  quint8 numRects;
403  QRfbHextileEncoder<SRC> *encoder;
404 };
405 
406 template <class SRC>
407 class QRfbHextileEncoder : public QRfbEncoder
408 {
409 public:
411  void write();
412 
413 private:
414  enum SubEncoding {
415  Raw = 1,
416  BackgroundSpecified = 2,
417  ForegroundSpecified = 4,
418  AnySubrects = 8,
419  SubrectsColoured = 16
420  };
421 
422  QByteArray buffer;
423  QRfbSingleColorHextile<SRC> singleColorHextile;
424  QRfbDualColorHextile<SRC> dualColorHextile;
425  QRfbMultiColorHextile<SRC> multiColorHextile;
426 
427  SRC bg;
428  SRC fg;
429  bool newBg;
430  bool newFg;
431 
432  friend class QRfbSingleColorHextile<SRC>;
433  friend class QRfbDualColorHextile<SRC>;
434  friend class QRfbMultiColorHextile<SRC>;
435 };
436 
437 class QVNCServer : public QObject
438 {
439  Q_OBJECT
440 public:
442  QVNCServer(QVNCScreen *screen, int id);
443  ~QVNCServer();
444 
445  void setDirty();
446  void setDirtyCursor() { dirtyCursor = true; setDirty(); }
447  inline bool isConnected() const { return state == Connected; }
448  inline void setRefreshRate(int rate) { refreshRate = rate; }
449 
450  enum ClientMsg { SetPixelFormat = 0,
451  FixColourMapEntries = 1,
452  SetEncodings = 2,
453  FramebufferUpdateRequest = 3,
454  KeyEvent = 4,
455  PointerEvent = 5,
456  ClientCutText = 6 };
457 
458  enum ServerMsg { FramebufferUpdate = 0,
459  SetColourMapEntries = 1 };
460 
461  void convertPixels(char *dst, const char *src, int count) const;
462 
463  inline int clientBytesPerPixel() const {
464  return pixelFormat.bitsPerPixel / 8;
465  }
466 
467  inline QVNCScreen* screen() const { return qvnc_screen; }
468  inline QVNCDirtyMap* dirtyMap() const { return qvnc_screen->dirtyMap(); }
469  inline QTcpSocket* clientSocket() const { return client; }
470  QImage *screenImage() const;
471  inline bool doPixelConversion() const { return needConversion; }
472 #ifndef QT_NO_QWS_CURSOR
473  inline bool hasClientCursor() const { return qvnc_cursor != 0; }
474 #endif
475 
476  void setCursor(QVNCCursor * c) { cursor = c; }
477 private:
478  void setPixelFormat();
479  void setEncodings();
480  void frameBufferUpdateRequest();
481  void pointerEvent();
482  void keyEvent();
483  void clientCutText();
484  bool pixelConversionNeeded() const;
485 
486 private slots:
487  void newConnection();
488  void readClient();
489  void checkUpdate();
490  void discardClient();
491 
492 private:
493  void init(uint port);
494  enum ClientState { Unconnected, Protocol, Init, Connected };
495  QTimer *timer;
496  QTcpServer *serverSocket;
497  QTcpSocket *client;
498  ClientState state;
499  quint8 msgType;
500  bool handleMsg;
501  QRfbPixelFormat pixelFormat;
502  Qt::KeyboardModifiers keymod;
503  Qt::MouseButtons buttons;
504  int encodingsPending;
505  int cutTextPending;
506  uint supportCopyRect : 1;
507  uint supportRRE : 1;
508  uint supportCoRRE : 1;
509  uint supportHextile : 1;
510  uint supportZRLE : 1;
511  uint supportCursor : 1;
512  uint supportDesktopSize : 1;
513  bool wantUpdate;
514  bool sameEndian;
515  bool needConversion;
516 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
517  bool swapBytes;
518 #endif
519  bool dirtyCursor;
520  int refreshRate;
521  QVNCScreen *qvnc_screen;
522 #ifndef QT_NO_QWS_CURSOR
523  QVNCClientCursor *qvnc_cursor;
524 #endif
525 
526  QRfbEncoder *encoder;
528 };
529 
530 
532 #endif // QT_NO_QWS_VNC
533 #endif // QSCREENVNC_P_H
virtual ~QRfbEncoder()
Definition: qvncserver.h:276
virtual void pointerEvent(const QMouseEvent &event)
This method is called by Qt whenever a QMouseEvent is generated by the underlying pointer input...
Definition: fb_base.cpp:67
#define Q_PACKED
Definition: qglobal.h:815
quint8 * rect(int r)
Definition: qvncserver.h:363
The QProxyScreenCursor class provides a generic interface to QScreenCursor implementations.
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
QRfbSingleColorHextile(QRfbHextileEncoder< SRC > *e)
Definition: qvncserver.h:301
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int keycode
static void keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
static int size()
Definition: qvncserver.h:163
bool isConnected() const
Definition: qvncserver.h:447
void setCursor(QVNCCursor *c)
Definition: qvncserver.h:476
void setX(int x)
Definition: qvncserver.h:349
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Q_CORE_EXPORT QTextStream & reset(QTextStream &s)
const quint8 * rect(int r) const
Definition: qvncserver.h:366
void setWidth(int r, int width)
Definition: qvncserver.h:386
static int bytesPerPixel(QImage::Format format)
Qt::MouseButtons buttons
Definition: qvncserver.h:230
int rectx(int r) const
Definition: qvncserver.h:378
QRfbRawEncoder(QVNCServer *s)
Definition: qvncserver.h:287
unsigned char quint8
Definition: qglobal.h:934
EventLoopTimerRef timer
int width(int r) const
Definition: qvncserver.h:331
bool doPixelConversion() const
Definition: qvncserver.h:471
void setHeight(int r, int height)
Definition: qvncserver.h:343
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QVNCScreen * screen
Definition: qscreenvnc_p.h:83
void setHeight(int height)
Definition: qvncserver.h:348
QVNCDirtyMapOptimized(QVNCScreen *screen)
Definition: qvncserver.h:137
QImage image() const
Returns the cursor&#39;s image.
Definition: qscreen_qws.h:151
QVNCScreen * screen() const
Definition: qvncserver.h:467
int height(int r) const
Definition: qvncserver.h:332
unsigned char uchar
Definition: qglobal.h:994
QFuture< void > map(Sequence &sequence, MapFunction function)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setRefreshRate(int rate)
Definition: qvncserver.h:448
The QVNCScreen class implements a screen driver for VNC servers.
QVNCCursor(QVNCScreen *s)
static bool init
void setX(int r, int x)
Definition: qvncserver.h:369
const char * name
void show()
Reimplemented Function
void setWidth(int r, int width)
Definition: qvncserver.h:340
unsigned short quint16
Definition: qglobal.h:936
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
int recty(int r) const
Definition: qvncserver.h:382
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
void setX(int r, int x)
Definition: qvncserver.h:334
The QTcpSocket class provides a TCP socket.
Definition: qtcpsocket.h:56
const T * ptr(const T &t)
void setHeight(int r, int height)
Definition: qvncserver.h:390
void setWidth(int width)
Definition: qvncserver.h:347
#define Q_OBJECT
Definition: qobjectdefs.h:157
void setDirtyCursor()
Definition: qvncserver.h:446
int lastx() const
Definition: qvncserver.h:327
void setY(int y)
Definition: qvncserver.h:350
Qt::MouseButtons buttons
Definition: qvncserver.h:503
QVNCCursor * cursor
Definition: qvncserver.h:527
int rectx(int r) const
Definition: qvncserver.h:329
QTcpSocket * clientSocket() const
Definition: qvncserver.h:469
QVNCDirtyMap * dirtyMap() const
Definition: qvncserver.h:468
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void hide()
Reimplemented Function
QVNCServer * server
Definition: qvnccursor.h:71
unsigned int quint32
Definition: qglobal.h:938
QVNCScreen * screen
Definition: qvncserver.h:125
The QTcpServer class provides a TCP-based server.
Definition: qtcpserver.h:61
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57
void setY(int r, int y)
Definition: qvncserver.h:337
void setY(int r, int y)
Definition: qvncserver.h:373
int size() const
Definition: qvncserver.h:186
QRfbEncoder(QVNCServer *s)
Definition: qvncserver.h:275
QRfbMultiColorHextile(QRfbHextileEncoder< SRC > *e)
Definition: qvncserver.h:358
The QTimer class provides repetitive and single-shot timers.
Definition: qtimer.h:56
void move(int x, int y)
Reimplemented Function
#define slots
Definition: qobjectdefs.h:68
QRfbRect(quint16 _x, quint16 _y, quint16 _w, quint16 _h)
Definition: qvncserver.h:147
virtual void setDirty()
Definition: fb_base.h:73
int lasty() const
Definition: qvncserver.h:328
QRfbDualColorHextile(QRfbHextileEncoder< SRC > *e)
Definition: qvncserver.h:313
int recty(int r) const
Definition: qvncserver.h:330
int clientBytesPerPixel() const
Definition: qvncserver.h:463
The KeyEvent object provides information about a key event.