Qt 4.8
qscreenvfb_qws.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 QtGui module 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 QT_NO_QWS_QVFB
43 
44 #define QTOPIA_QVFB_BRIGHTNESS
45 
46 #include <stdlib.h>
47 #include <sys/types.h>
48 #include <sys/ipc.h>
49 #include <sys/shm.h>
50 #include <sys/stat.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include <errno.h>
54 
55 #include <qvfbhdr.h>
56 #include <qscreenvfb_qws.h>
57 #include <qkbdvfb_qws.h>
58 #include <qmousevfb_qws.h>
59 #include <qwindowsystem_qws.h>
60 #include <qsocketnotifier.h>
61 #include <qapplication.h>
62 #include <qscreen_qws.h>
64 #include <qkbddriverfactory_qws.h>
65 #include <qdebug.h>
66 
68 
70 {
71 public:
74 
75  bool success;
76  unsigned char *shmrgn;
78  bool blank;
81 #ifndef QT_NO_QWS_KEYBOARD
83 #endif
84 };
85 
87  : mouse(0)
88 
89 {
90 #ifndef QT_NO_QWS_KEYBOARD
91  keyboard = 0;
92 #endif
93  brightness = 255;
94  blank = false;
95 }
96 
98 {
99  delete mouse;
100 #ifndef QT_NO_QWS_KEYBOARD
101  delete keyboard;
102 #endif
103 }
104 
181 QVFbScreen::QVFbScreen(int display_id)
182  : QScreen(display_id, VFbClass), d_ptr(new QVFbScreenPrivate)
183 {
184  d_ptr->shmrgn = 0;
185  d_ptr->hdr = 0;
186  data = 0;
187 }
188 
193 {
194  delete d_ptr;
195 }
196 
197 static QVFbScreen *connected = 0;
198 
199 bool QVFbScreen::connect(const QString &displaySpec)
200 {
201  QStringList displayArgs = displaySpec.split(QLatin1Char(':'));
202  if (displayArgs.contains(QLatin1String("Gray")))
203  grayscale = true;
204 
205  key_t key = ftok(QT_VFB_MOUSE_PIPE(displayId).toLocal8Bit(), 'b');
206 
207  if (key == -1)
208  return false;
209 
210 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
211 #ifndef QT_QWS_FRAMEBUFFER_LITTLE_ENDIAN
212  if (displayArgs.contains(QLatin1String("littleendian")))
213 #endif
215 #endif
216 
217  int shmId = shmget(key, 0, 0);
218  if (shmId != -1)
219  d_ptr->shmrgn = (unsigned char *)shmat(shmId, 0, 0);
220  else
221  return false;
222 
223  if ((long)d_ptr->shmrgn == -1 || d_ptr->shmrgn == 0) {
224  qDebug("No shmrgn %ld", (long)d_ptr->shmrgn);
225  return false;
226  }
227 
230 
231  dw = w = d_ptr->hdr->width;
232  dh = h = d_ptr->hdr->height;
233  d = d_ptr->hdr->depth;
234 
235  switch (d) {
236  case 1:
238  break;
239  case 8:
241  break;
242  case 12:
244  break;
245  case 15:
247  break;
248  case 16:
250  break;
251  case 18:
253  break;
254  case 24:
256  break;
257  case 32:
259  break;
260  }
261 
262  lstep = d_ptr->hdr->linestep;
263 
264  // Handle display physical size spec.
265  int dimIdxW = -1;
266  int dimIdxH = -1;
267  for (int i = 0; i < displayArgs.size(); ++i) {
268  if (displayArgs.at(i).startsWith(QLatin1String("mmWidth"))) {
269  dimIdxW = i;
270  break;
271  }
272  }
273  for (int i = 0; i < displayArgs.size(); ++i) {
274  if (displayArgs.at(i).startsWith(QLatin1String("mmHeight"))) {
275  dimIdxH = i;
276  break;
277  }
278  }
279  if (dimIdxW >= 0) {
280  bool ok;
281  int pos = 7;
282  if (displayArgs.at(dimIdxW).at(pos) == QLatin1Char('='))
283  ++pos;
284  int pw = displayArgs.at(dimIdxW).mid(pos).toInt(&ok);
285  if (ok) {
286  physWidth = pw;
287  if (dimIdxH < 0)
289  }
290  }
291  if (dimIdxH >= 0) {
292  bool ok;
293  int pos = 8;
294  if (displayArgs.at(dimIdxH).at(pos) == QLatin1Char('='))
295  ++pos;
296  int ph = displayArgs.at(dimIdxH).mid(pos).toInt(&ok);
297  if (ok) {
298  physHeight = ph;
299  if (dimIdxW < 0)
301  }
302  }
303  if (dimIdxW < 0 && dimIdxH < 0) {
304  const int dpi = 72;
305  physWidth = qRound(dw * 25.4 / dpi);
306  physHeight = qRound(dh * 25.4 / dpi);
307  }
308 
309  qDebug("Connected to VFB server %s: %d x %d x %d %dx%dmm (%dx%ddpi)", displaySpec.toLatin1().data(),
310  w, h, d, physWidth, physHeight, qRound(dw*25.4/physWidth), qRound(dh*25.4/physHeight) );
311 
312  size = lstep * h;
313  mapsize = size;
315  memcpy(screenclut, d_ptr->hdr->clut, sizeof(QRgb) * screencols);
316 
317  connected = this;
318 
319  if (qgetenv("QT_QVFB_BGR").toInt())
321 
322  return true;
323 }
324 
326 {
327  connected = 0;
328  if ((long)d_ptr->shmrgn != -1 && d_ptr->shmrgn) {
329  if (qApp->type() == QApplication::GuiServer && d_ptr->hdr->dataoffset >= (int)sizeof(QVFbHeader)) {
330  d_ptr->hdr->serverVersion = 0;
331  }
332  shmdt((char*)d_ptr->shmrgn);
333  }
334 }
335 
337 {
338 #ifndef QT_NO_QWS_MOUSE_QVFB
339  const QString mouseDev = QT_VFB_MOUSE_PIPE(displayId);
340  d_ptr->mouse = new QVFbMouseHandler(QLatin1String("QVFbMouse"), mouseDev);
341  qwsServer->setDefaultMouse("None");
342  if (d_ptr->mouse)
343  d_ptr->mouse->setScreen(this);
344 #endif
345 
346 #if !defined(QT_NO_QWS_KBD_QVFB) && !defined(QT_NO_QWS_KEYBOARD)
347  const QString keyboardDev = QT_VFB_KEYBOARD_PIPE(displayId);
348  d_ptr->keyboard = new QVFbKeyboardHandler(keyboardDev);
349  qwsServer->setDefaultKeyboard("None");
350 #endif
351 
352  if (d_ptr->hdr->dataoffset >= (int)sizeof(QVFbHeader))
354 
355  if(d==8) {
356  screencols=256;
357  if (grayscale) {
358  // Build grayscale palette
359  for(int loopc=0;loopc<256;loopc++) {
360  screenclut[loopc]=qRgb(loopc,loopc,loopc);
361  }
362  } else {
363  // 6x6x6 216 color cube
364  int idx = 0;
365  for(int ir = 0x0; ir <= 0xff; ir+=0x33) {
366  for(int ig = 0x0; ig <= 0xff; ig+=0x33) {
367  for(int ib = 0x0; ib <= 0xff; ib+=0x33) {
368  screenclut[idx]=qRgb(ir, ig, ib);
369  idx++;
370  }
371  }
372  }
373  screencols=idx;
374  }
375  memcpy(d_ptr->hdr->clut, screenclut, sizeof(QRgb) * screencols);
377  } else if (d == 4) {
378  int val = 0;
379  for (int idx = 0; idx < 16; idx++, val += 17) {
380  screenclut[idx] = qRgb(val, val, val);
381  }
382  screencols = 16;
383  memcpy(d_ptr->hdr->clut, screenclut, sizeof(QRgb) * screencols);
385  } else if (d == 1) {
386  screencols = 2;
387  screenclut[1] = qRgb(0xff, 0xff, 0xff);
388  screenclut[0] = qRgb(0, 0, 0);
389  memcpy(d_ptr->hdr->clut, screenclut, sizeof(QRgb) * screencols);
391  }
392 
393 #ifndef QT_NO_QWS_CURSOR
395 #endif
396  return true;
397 }
398 
400 {
401 }
402 
403 void QVFbScreen::setMode(int ,int ,int)
404 {
405 }
406 
407 // save the state of the graphics card
408 // This is needed so that e.g. we can restore the palette when switching
409 // between linux virtual consoles.
411 {
412  // nothing to do.
413 }
414 
415 // restore the state of the graphics card.
417 {
418 }
419 void QVFbScreen::setDirty(const QRect& rect)
420 {
421  const QRect r = rect.translated(-offset());
422  d_ptr->hdr->dirty = true;
423  d_ptr->hdr->update = d_ptr->hdr->update.united(r);
424 }
425 
426 void QVFbScreen::setBrightness(int b)
427 {
428  if (connected) {
429  connected->d_ptr->brightness = b;
430 
431  QVFbHeader *hdr = connected->d_ptr->hdr;
432  if (hdr->viewerVersion < 0x040400) // brightness not supported
433  return;
434 
435  const int br = connected->d_ptr->blank ? 0 : b;
436  if (hdr->brightness != br) {
437  hdr->brightness = br;
438  connected->setDirty(connected->region().boundingRect());
439  }
440  }
441 }
442 
443 void QVFbScreen::blank(bool on)
444 {
445  d_ptr->blank = on;
446  setBrightness(connected->d_ptr->brightness);
447 }
448 
449 #endif // QT_NO_QWS_QVFB
450 
int screencols
the number of entries in the color table
Definition: qscreen_qws.h:309
virtual void restore()
Reimplemented Function
int linestep
Definition: qvfbhdr.h:94
The QWSKeyboardHandler class is a base class for keyboard drivers in Qt for Embedded Linux...
Definition: qkbd_qws.h:57
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
unsigned int QRgb
Definition: qrgb.h:53
virtual void blank(bool)
Prevents the screen driver form displaying any content on the screen.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
int d
the pixel depth
Definition: qscreen_qws.h:327
QWSMouseHandler * mouse
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
static void setDefaultMouse(const char *)
Sets the mouse driver that will be used if the QWS_MOUSE_PROTO environment variable is not defined...
QRect united(const QRect &other) const
Returns the bounding rectangle of this rectangle and the given rectangle.
Definition: qrect.h:491
The QWSMouseHandler class is a base class for mouse drivers in Qt for Embedded Linux.
Definition: qmouse_qws.h:66
The QVFbScreen class implements a screen driver for the virtual framebuffer.
QRgb screenclut[256]
the color table
Definition: qscreen_qws.h:308
QRect translated(int dx, int dy) const
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis...
Definition: qrect.h:328
QVFbScreenPrivate * d_ptr
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
QRect boundingRect() const
Returns the bounding rectangle of this region.
Definition: qregion.cpp:4363
int physHeight
the physical height of the screen in millimeters.
Definition: qscreen_qws.h:340
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
QRect update
Definition: qvfbhdr.h:96
static void initSoftwareCursor()
Initializes the screen cursor.
PixelType pixeltype
set to BGRPixel
Definition: qscreen_qws.h:328
int physWidth
the physical width of the screen in millimeters.
Definition: qscreen_qws.h:339
void setPixelFormat(QImage::Format format)
Sets the screen&#39;s pixel format to format.
Q_CORE_EXPORT void qDebug(const char *,...)
virtual QRegion region() const
Returns the region covered by this screen driver.
Definition: qscreen_qws.h:284
uchar * data
points to the first visible pixel in the frame buffer.
Definition: qscreen_qws.h:311
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QWSKeyboardHandler * keyboard
#define QT_VFB_KEYBOARD_PIPE(DISPLAY)
Definition: qvfbhdr.h:78
#define qApp
int viewerVersion
Definition: qvfbhdr.h:100
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
int w
the logical width of the screen.
Definition: qscreen_qws.h:324
static int toInt(const QByteArray &str)
Definition: generator.cpp:167
unsigned char * shmrgn
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QVFbScreen(int display_id)
Constructs a QVNCScreen object.
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
int displayId
Definition: qscreen_qws.h:337
virtual void disconnect()
Reimplemented Function
static void setDefaultKeyboard(const char *)
Sets the keyboard driver that will be used if the QWS_KEYBOARD environment variable is not defined...
bool dirty
Definition: qvfbhdr.h:97
virtual bool connect(const QString &displaySpec)
Reimplemented Function
int width
Definition: qvfbhdr.h:91
virtual void setMode(int nw, int nh, int nd)
Reimplemented Function
virtual ~QVFbScreen()
Destroys this QVFbScreen object.
#define QT_VFB_MOUSE_PIPE(DISPLAY)
Definition: qvfbhdr.h:76
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
int dw
the device width
Definition: qscreen_qws.h:331
int serverVersion
Definition: qvfbhdr.h:101
int dh
the device height
Definition: qscreen_qws.h:332
int dataoffset
Definition: qvfbhdr.h:95
void setFrameBufferLittleEndian(bool littleEndian)
Q_GUI_EXPORT_INLINE QRgb qRgb(int r, int g, int b)
Definition: qrgb.h:69
int key
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
bool grayscale
the gray scale screen mode flag
Definition: qscreen_qws.h:329
The QScreen class is a base class for screen drivers in Qt for Embedded Linux.
Definition: qscreen_qws.h:191
virtual void shutdownDevice()
Reimplemented Function
virtual bool initDevice()
Reimplemented Function
int height
Definition: qvfbhdr.h:92
int lstep
the number of bytes representing a line in the frame buffer.
Definition: qscreen_qws.h:325
virtual void save()
Reimplemented Function
static QVFbScreen * connected
int brightness
Definition: qvfbhdr.h:102
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
QRgb clut[256]
Definition: qvfbhdr.h:99
int h
the logical height of the screen.
Definition: qscreen_qws.h:326
int mapsize
the total number of bytes in the frame buffer
Definition: qscreen_qws.h:335
int depth
Definition: qvfbhdr.h:93
virtual void setDirty(const QRect &r)
Reimplemented Function
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
QPoint offset() const
Returns the logical offset of the screen, i.
QWSServer Q_GUI_EXPORT * qwsServer
void setScreen(const QScreen *screen)
Sets the screen for this mouse driver to be the given screen.
Definition: qmouse_qws.cpp:264
#define QT_VERSION
This macro expands a numeric value of the form 0xMMNNPP (MM = major, NN = minor, PP = patch) that spe...
Definition: qglobal.h:51
int numcols
Definition: qvfbhdr.h:98