Moving some QVFB stuff out of the configure script and into .pro files
[qt-netbsd.git] / tools / qvfb / qvfbmmap.cpp
blobea35238a713902bd3b03f5b95a9dfd18f517965a
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the tools applications of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "qvfbmmap.h"
43 #include "qvfbhdr.h"
45 #include <QTimer>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <sys/ipc.h>
50 #include <sys/types.h>
51 #include <sys/shm.h>
52 #include <sys/stat.h>
53 #include <sys/sem.h>
54 #include <sys/mman.h>
55 #include <fcntl.h>
56 #include <errno.h>
57 #include <math.h>
59 QT_BEGIN_NAMESPACE
61 QMMapViewProtocol::QMMapViewProtocol(int displayid, const QSize &s,
62 int d, QObject *parent)
63 : QVFbViewProtocol(displayid, parent), hdr(0), dataCache(0), windowId(0)
65 switch (d) {
66 case 1:
67 case 4:
68 case 8:
69 case 12:
70 case 15:
71 case 16:
72 case 18:
73 case 24:
74 case 32:
75 break;
76 default:
77 qFatal("Unsupported bit depth %d\n", d);
80 fileName = QString("/tmp/.qtvfb_map-%1").arg(displayid);
82 int w = s.width();
83 int h = s.height();
86 kh = new QVFbKeyPipeProtocol(displayid);
87 mh = new QVFbMouseLinuxTP(displayid);
89 int bpl;
90 if (d < 8)
91 bpl = (w * d + 7) / 8;
92 else
93 bpl = w * ((d + 7) / 8);
95 displaySize = bpl * h;
97 unsigned char *data;
98 uint data_offset_value = sizeof(QVFbHeader);
99 const int page_size = getpagesize();
100 if (data_offset_value % page_size)
101 data_offset_value += page_size - (data_offset_value % page_size);
103 dataSize = bpl * h + data_offset_value;
105 unlink(fileName.toLocal8Bit().data());
106 fd = ::open( fileName.toLocal8Bit().data(), O_CREAT|O_RDWR, 0666 );
107 ::lseek(fd, dataSize, SEEK_SET);
108 ::write(fd, "\0", 1);
109 if (fd < 0) {
110 data = (unsigned char *)-1;
111 } else {
112 // might need to do something about size?
113 data = (unsigned char *)mmap(NULL, dataSize, PROT_WRITE | PROT_READ,
114 MAP_SHARED, fd, 0);
115 if (data == MAP_FAILED)
116 data = (unsigned char *)-1;
119 if ( (long)data == -1 ){
120 delete kh;
121 delete mh;
122 qFatal( "Cannot attach to mapped file %s", fileName.toLocal8Bit().data());
124 dataCache = (unsigned char *)malloc(displaySize);
125 memset(dataCache, 0, displaySize);
126 memset(data+sizeof(QVFbHeader), 0, displaySize);
128 hdr = (QVFbHeader *)data;
129 hdr->width = w;
130 hdr->height = h;
131 hdr->depth = d;
132 hdr->linestep = bpl;
133 hdr->numcols = 0;
134 hdr->dataoffset = data_offset_value;
135 hdr->update = QRect();
136 hdr->brightness = 255;
137 hdr->windowId = 0;
139 mRefreshTimer = new QTimer(this);
140 connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(flushChanges()));
143 QMMapViewProtocol::~QMMapViewProtocol()
145 munmap((char *)hdr, dataSize);
146 ::close(fd);
147 unlink(fileName.toLocal8Bit().constData());
148 free(dataCache);
149 delete kh;
150 delete mh;
153 int QMMapViewProtocol::brightness() const
155 return hdr->brightness;
158 int QMMapViewProtocol::width() const
160 return hdr->width;
163 int QMMapViewProtocol::height() const
165 return hdr->height;
168 int QMMapViewProtocol::depth() const
170 return hdr->depth;
173 int QMMapViewProtocol::linestep() const
175 return hdr->linestep;
178 int QMMapViewProtocol::numcols() const
180 return hdr->numcols;
183 QVector<QRgb> QMMapViewProtocol::clut() const
185 QVector<QRgb> vector(hdr->numcols);
186 for (int i=0; i < hdr->numcols; ++i)
187 vector[i] = hdr->clut[i];
189 return vector;
192 unsigned char *QMMapViewProtocol::data() const
194 return dataCache;
195 //return ((unsigned char *)hdr)+hdr->dataoffset;
198 void QMMapViewProtocol::flushChanges()
200 // based of dirty rect, copy changes from hdr to hdrcopy
201 memcpy(dataCache, ((char *)hdr) + hdr->dataoffset, displaySize);
202 emit displayDataChanged(QRect(0, 0, width(), height()));
205 void QMMapViewProtocol::setRate(int interval)
207 if (interval > 0)
208 return mRefreshTimer->start(1000/interval);
209 else
210 mRefreshTimer->stop();
213 int QMMapViewProtocol::rate() const
215 int i = mRefreshTimer->interval();
216 if (i > 0)
217 return 1000/i;
218 else
219 return 0;
222 QT_END_NAMESPACE