source: trunk/tools/qvfb/qvfbmmap.cpp@ 372

Last change on this file since 372 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qvfbmmap.h"
43#include "qvfbhdr.h"
44
45#include <QTimer>
46
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>
58
59QT_BEGIN_NAMESPACE
60
61QMMapViewProtocol::QMMapViewProtocol(int displayid, const QSize &s,
62 int d, QObject *parent)
63 : QVFbViewProtocol(displayid, parent), hdr(0), dataCache(0), windowId(0)
64{
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);
78 }
79
80 fileName = QString("/tmp/.qtvfb_map-%1").arg(displayid);
81
82 int w = s.width();
83 int h = s.height();
84
85
86 kh = new QVFbKeyPipeProtocol(displayid);
87 mh = new QVFbMouseLinuxTP(displayid);
88
89 int bpl;
90 if (d < 8)
91 bpl = (w * d + 7) / 8;
92 else
93 bpl = w * ((d + 7) / 8);
94
95 displaySize = bpl * h;
96
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);
102
103 dataSize = bpl * h + data_offset_value;
104
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;
117 }
118
119 if ( (long)data == -1 ){
120 delete kh;
121 delete mh;
122 qFatal( "Cannot attach to mapped file %s", fileName.toLocal8Bit().data());
123 }
124 dataCache = (unsigned char *)malloc(displaySize);
125 memset(dataCache, 0, displaySize);
126 memset(data+sizeof(QVFbHeader), 0, displaySize);
127
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;
138
139 mRefreshTimer = new QTimer(this);
140 connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(flushChanges()));
141}
142
143QMMapViewProtocol::~QMMapViewProtocol()
144{
145 munmap((char *)hdr, dataSize);
146 ::close(fd);
147 unlink(fileName.toLocal8Bit().constData());
148 free(dataCache);
149 delete kh;
150 delete mh;
151}
152
153int QMMapViewProtocol::brightness() const
154{
155 return hdr->brightness;
156}
157
158int QMMapViewProtocol::width() const
159{
160 return hdr->width;
161}
162
163int QMMapViewProtocol::height() const
164{
165 return hdr->height;
166}
167
168int QMMapViewProtocol::depth() const
169{
170 return hdr->depth;
171}
172
173int QMMapViewProtocol::linestep() const
174{
175 return hdr->linestep;
176}
177
178int QMMapViewProtocol::numcols() const
179{
180 return hdr->numcols;
181}
182
183QVector<QRgb> QMMapViewProtocol::clut() const
184{
185 QVector<QRgb> vector(hdr->numcols);
186 for (int i=0; i < hdr->numcols; ++i)
187 vector[i] = hdr->clut[i];
188
189 return vector;
190}
191
192unsigned char *QMMapViewProtocol::data() const
193{
194 return dataCache;
195 //return ((unsigned char *)hdr)+hdr->dataoffset;
196}
197
198void QMMapViewProtocol::flushChanges()
199{
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()));
203}
204
205void QMMapViewProtocol::setRate(int interval)
206{
207 if (interval > 0)
208 return mRefreshTimer->start(1000/interval);
209 else
210 mRefreshTimer->stop();
211}
212
213int QMMapViewProtocol::rate() const
214{
215 int i = mRefreshTimer->interval();
216 if (i > 0)
217 return 1000/i;
218 else
219 return 0;
220}
221
222QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.