Qt 4.8
qvncintegration.cpp
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 #include "qvncintegration.h"
43 #include "../fb_base/fb_base.h"
44 #include <private/qapplication_p.h>
45 #include <QtGui/private/qpixmap_raster_p.h>
46 #include <QtCore/qdebug.h>
47 
48 #include <qvncserver.h>
49 #include <QtGui/QPainter>
50 
51 #include <QtCore/QTimer>
53 
54 QVNCScreen::QVNCScreen(QRect screenSize, int screenId)
55  : QFbScreen::QFbScreen()
56 {
57  setGeometry(screenSize);
58  setDepth(32);
60  setPhysicalSize((geometry().size()*254)/720);
61 
62 
63  d_ptr = new QVNCScreenPrivate(this, screenId);
64 
65  cursor = new QVNCCursor(d_ptr->vncServer, this);
66  d_ptr->vncServer->setCursor(static_cast<QVNCCursor *>(cursor));
67 }
68 
70 {
71  return d_ptr->dirty;
72 }
73 
75 {
76  QRegion touched;
77  touched = QFbScreen::doRedraw();
78 
79  QVector<QRect> rects = touched.rects();
80  for (int i = 0; i < rects.size(); i++)
81  d_ptr->setDirty(rects[i]);
82  return touched;
83 }
84 
85 static inline int defaultWidth() { return 800; }
86 static inline int defaultHeight() { return 600; }
87 static inline int defaultDisplay() { return 0; }
88 
89 static void usage()
90 {
91  qWarning() << "VNC Platform Integration options:";
92  qWarning() << " size=<Width>x<Height> - set the display width and height";
93  qWarning() << " defaults to" << defaultWidth() << "x" << defaultHeight();
94  qWarning() << " display=<ID> - set the VNC display port to ID + 5900";
95  qWarning() << " defaults to" << defaultDisplay();
96  qWarning() << " offset=<X>x<Y> - set the current screens offset";
97  qWarning() << " vnc - start configuration of a new screen";
98  qWarning() << " size and offset are inherited from the previous screen if not set";
99  qWarning() << " display id is incremented from the previous screen if not set";
100  qWarning() << " virtual - manage the set of screens as a virtual desktop";
101 }
102 
104  : virtualDesktop(false), fontDb(new QGenericUnixFontDatabase())
105 {
106  int sizeX = defaultWidth();
107  int sizeY = defaultHeight();
108  int offsetX = 0;
109  int offsetY = 0;
110  int display = defaultDisplay();
111  bool showUsage = false;
112 
113  foreach(QString confString, paramList) {
114  if (confString.startsWith(QLatin1String("size="))) {
115  QString val = confString.section(QLatin1Char('='), 1, 1);
116  sizeX = val.section(QLatin1Char('x'), 0, 0).toInt();
117  sizeY = val.section(QLatin1Char('x'), 1, 1).toInt();
118  }
119  else if (confString.startsWith(QLatin1String("display="))) {
120  display = confString.section(QLatin1Char('='), 1, 1).toInt();
121  }
122  else if (confString.startsWith(QLatin1String("offset="))) {
123  QString val = confString.section(QLatin1Char('='), 1, 1);
124  offsetX = val.section(QLatin1Char('x'), 0, 0).toInt();
125  offsetY = val.section(QLatin1Char('x'), 1, 1).toInt();
126  }
127  else if (confString == QLatin1String("vnc")) {
128  QRect screenRect(offsetX, offsetY, sizeX, sizeY);
129  QVNCScreen *screen = new QVNCScreen(screenRect, display);
130  mScreens.append(screen);
131  screen->setObjectName(QString("screen %1").arg(display));
132  screen->setDirty(screenRect);
133  ++display;
134  }
135  else if (confString == QLatin1String("virtual")) {
136  virtualDesktop = true;
137  }
138  else {
139  qWarning() << "Unknown VNC option:" << confString;
140  showUsage = true;
141  }
142  }
143 
144  if (showUsage)
145  usage();
146 
147  QRect screenRect(offsetX, offsetY, sizeX, sizeY);
148  QVNCScreen *screen = new QVNCScreen(screenRect, display);
149  mScreens.append(screen);
151  screen->setObjectName(QString("screen %1").arg(display));
152  screen->setDirty(screenRect);
153 }
154 
156 {
157  switch (cap) {
158  case ThreadedPixmaps: return true;
159  default: return QPlatformIntegration::hasCapability(cap);
160  }
161 }
162 
163 
165 {
166  return new QRasterPixmapData(type);
167 }
168 
170 {
171  QFbWindowSurface * surface;
172  surface = new QFbWindowSurface(mPrimaryScreen, widget);
173  return surface;
174 }
175 
176 
178 {
179  QFbWindow *w = new QFbWindow(widget);
180  if (virtualDesktop) {
183  QFbScreen *screen;
184  while (i != end) {
185  screen = static_cast<QFbScreen *>(*i);
186  screen->addWindow(w);
187  ++i;
188  }
189  }
190  else
192  return w;
193 }
194 
195 QPixmap QVNCIntegration::grabWindow(WId window, int x, int y, int width, int height) const
196 {
197 // qDebug() << "QVNCIntegration::grabWindow" << window << x << y << width << height;
198 
199  if (window == 0) { //desktop
200  QImage *desktopImage = mPrimaryScreen->image();
201  if (x==0 && y == 0 && width < 0 && height < 0) {
202  return QPixmap::fromImage(*desktopImage);
203  }
204  if (width < 0)
205  width = desktopImage->width() - x;
206  if (height < 0)
207  height = desktopImage->height() - y;
208  int bytesPerPixel = desktopImage->depth()/8; //We don't support 1, 2, or 4 bpp
209  QImage img(desktopImage->scanLine(y) + bytesPerPixel*x, width, height, desktopImage->bytesPerLine(), desktopImage->format());
210  return QPixmap::fromImage(img);
211  }
212  QWidget *win = QWidget::find(window);
213  if (win) {
214  QRect r = win->geometry();
215  if (width < 0)
216  width = r.width() - x;
217  if (height < 0)
218  height = r.height() - y;
219  QImage *desktopImage = mPrimaryScreen->image();
220  int bytesPerPixel = desktopImage->depth()/8; //We don't support 1, 2, or 4 bpp
221 
222  QImage img(desktopImage->scanLine(r.top() + y) + bytesPerPixel*(r.left()+x), width, height, desktopImage->bytesPerLine(), desktopImage->format());
223  return QPixmap::fromImage(img);
224  }
225  return QPixmap();
226 }
227 
228 
230 {
231  if (virtualDesktop) { // all windows exist on all screens in virtual desktop mode
232  return;
233  }
234  if (screen < 0 || screen > mScreens.size())
235  return;
236  QVNCScreen * newScreen = qobject_cast<QVNCScreen *>(mScreens.at(screen));
237  for(int i = 0; i < mScreens.size(); i++) {
238  QVNCScreen *oldScreen = qobject_cast<QVNCScreen *>(mScreens.at(i));
239  if (oldScreen->windowStack.contains(static_cast<QFbWindow *>(window->platformWindow()))) {
240  oldScreen->removeWindow(static_cast<QFbWindow *>(window->platformWindow()));
241  break;
242  }
243  }
244  window->platformWindow()->setGeometry(window->geometry()); // this should be unified elsewhere
245  newScreen->addWindow(static_cast<QFbWindow *>(window->platformWindow()));
246 }
247 
249 {
250  return fontDb;
251 }
T qobject_cast(QObject *object)
Definition: qobject.h:375
QString section(QChar sep, int start, int end=-1, SectionFlags flags=SectionDefault) const
This function returns a section of the string.
Definition: qstring.h:781
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition: qpixmap.cpp:2197
unsigned long WId
Definition: qwindowdefs.h:119
int type
Definition: qmetatype.cpp:239
static int defaultWidth()
virtual QRegion doRedraw()
Definition: fb_base.cpp:229
QPointer< QWidget > widget
static void usage()
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
void setCursor(QVNCCursor *c)
Definition: qvncserver.h:476
void setDirty(const QRect &rect, bool force=false)
QPixmap grabWindow(WId window, int x, int y, int width, int height) const
This function is called when Qt needs to be able to grab the content of a window. ...
virtual bool hasCapability(Capability cap) const
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
static int bytesPerPixel(QImage::Format format)
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
The QList::const_iterator class provides an STL-style const iterator for QList and QQueue...
Definition: qlist.h:228
virtual void setFormat(QImage::Format format)
Definition: fb_base.cpp:172
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
QPlatformWindow * createPlatformWindow(QWidget *widget, WId winId) const
Factory function for QPlatformWindow.
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
The QString class provides a Unicode character string.
Definition: qstring.h:83
int size
the number of bytes in the visible region of the frame buffer
Definition: qscreen_qws.h:334
The QPlatformWindow class provides an abstraction for top-level windows.
Format format() const
Returns the format of the image.
Definition: qimage.cpp:2305
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
friend class QVNCScreenPrivate
QPlatformFontDatabase * fontDatabase() const
Accessor for the platform integrations fontdatabase.
NSWindow * window
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
virtual void addWindow(QFbWindow *surface)
Definition: fb_base.cpp:305
The QVNCScreen class implements a screen driver for VNC servers.
QPixmapData * createPixmapData(QPixmapData::PixelType type) const
Factory function for QPixmapData.
QPlatformFontDatabase * fontDb
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QWidget * find(WId)
Returns a pointer to the widget with window identifer/handle id.
Definition: qwidget.cpp:2517
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static int defaultHeight()
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
friend class QVNCCursor
QRegion doRedraw()
QVNCDirtyMap * dirtyMap()
The QWindowSurface class provides the drawing area for top-level windows.
QPlatformSoftwareCursor * cursor
Definition: fb_base.h:186
QImage * image() const
Definition: fb_base.h:180
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
static int defaultDisplay()
virtual void removeWindow(QFbWindow *surface)
Definition: fb_base.cpp:313
QList< QPlatformScreen * > mScreens
virtual void setPhysicalSize(QSize size)
Definition: fb_base.cpp:167
QVector< QRect > rects() const
Returns an array of non-overlapping rectangles that make up the region.
Definition: qregion.cpp:4412
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
void setDirty(const QRect &)
Reimplemented Function
virtual void setDepth(int depth)
Definition: fb_base.cpp:162
The QPlatformFontDatabase class makes it possible to customize how fonts are discovered and how they ...
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QVNCIntegration(const QStringList &paramList)
bool hasCapability(QPlatformIntegration::Capability cap) const
QVNCScreen * mPrimaryScreen
virtual QRect geometry() const
Reimplement in subclass to return the pixel geometry of the screen.
Definition: fb_base.h:162
QList< QFbWindow * > windowStack
Definition: fb_base.h:184
void moveToScreen(QWidget *window, int screen)
This function is called when a QWidget is displayed on screen, or the QWidget is to be displayed on a...
static const KeyPair *const end
QVNCDirtyMap * dirty
Definition: qscreenvnc_p.h:253
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition: qwidget.h:158
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
QVNCScreenPrivate * d_ptr
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual void setGeometry(QRect rect)
Definition: fb_base.cpp:152
QWindowSurface * createWindowSurface(QWidget *widget, WId winId) const
Factory function for QWindowSurface.
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1886
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272
QVNCServer * vncServer
Definition: qscreenvnc_p.h:255
QVNCScreen(int display_id)
Constructs a QVNCScreen object.