source: trunk/tools/qvfb/qvfbshmem.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: 8.5 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 "qlock_p.h"
43
44#include "qvfbshmem.h"
45#include "qvfbhdr.h"
46
47#define QTE_PIPE "QtEmbedded-%1"
48
49#include <QFile>
50#include <QTimer>
51
52#include <stdlib.h>
53#include <unistd.h>
54#include <sys/ipc.h>
55#include <sys/types.h>
56#include <sys/shm.h>
57#include <sys/stat.h>
58#include <sys/sem.h>
59#include <sys/mman.h>
60#include <fcntl.h>
61#include <errno.h>
62#include <math.h>
63
64QT_BEGIN_NAMESPACE
65
66#ifdef Q_WS_QWS
67#error qvfb must be compiled with the Qt for X11 package
68#endif
69
70// Get the name of the directory where Qt for Embedded Linux temporary data should
71// live.
72static QString qws_dataDir(int qws_display_id)
73{
74 QByteArray dataDir = QString("/tmp/qtembedded-%1").arg(qws_display_id).toLocal8Bit();
75 if (mkdir(dataDir, 0700)) {
76 if (errno != EEXIST) {
77 qFatal("Cannot create Qt for Embedded Linux data directory: %s", dataDir.constData());
78 }
79 }
80
81 struct stat buf;
82 if (lstat(dataDir, &buf))
83 qFatal("stat failed for Qt for Embedded Linux data directory: %s", dataDir.constData());
84
85 if (!S_ISDIR(buf.st_mode))
86 qFatal("%s is not a directory", dataDir.constData());
87 if (buf.st_uid != getuid())
88 qFatal("Qt for Embedded Linux data directory is not owned by user %uh", getuid());
89
90 if ((buf.st_mode & 0677) != 0600)
91 qFatal("Qt for Embedded Linux data directory has incorrect permissions: %s", dataDir.constData());
92 dataDir += "/";
93
94 return QString(dataDir);
95}
96
97
98static QString displayPipe;
99static QString displayPiped;
100class DisplayLock
101{
102public:
103 DisplayLock() : qlock(0) {
104 if (QFile::exists(displayPiped)) {
105 qlock = new QLock(displayPipe, 'd', false);
106 qlock->lock(QLock::Read);
107 }
108 }
109 ~DisplayLock() {
110 if (qlock) {
111 qlock->unlock();
112 delete qlock;
113 qlock = 0;
114 }
115 }
116private:
117 QLock *qlock;
118};
119
120QShMemViewProtocol::QShMemViewProtocol(int displayid, const QSize &s,
121 int d, QObject *parent)
122 : QVFbViewProtocol(displayid, parent), hdr(0), dataCache(0), lockId(-1),
123 windowId(0)
124{
125 int w = s.width();
126 int h = s.height();
127
128 QString username = "unknown";
129 const char *logname = getenv("LOGNAME");
130 if ( logname )
131 username = logname;
132
133 QString oldPipe = "/tmp/qtembedded-" + username + "/" + QString(QTE_PIPE).arg(displayid);
134 int oldPipeSemkey = ftok(oldPipe.toLatin1().constData(), 'd');
135 if (oldPipeSemkey != -1) {
136 int oldPipeLockId = semget(oldPipeSemkey, 0, 0);
137 if (oldPipeLockId >= 0){
138 sembuf sops;
139 sops.sem_num = 0;
140 sops.sem_op = 1;
141 sops.sem_flg = SEM_UNDO;
142 int rv;
143 do {
144 rv = semop(lockId,&sops,1);
145 } while (rv == -1 && errno == EINTR);
146
147 perror("QShMemViewProtocol::QShMemViewProtocol");
148 qFatal("Cannot create lock file as an old version of QVFb has "
149 "opened %s. Close other QVFb and try again",
150 oldPipe.toLatin1().constData());
151 }
152 }
153
154 kh = new QVFbKeyPipeProtocol(displayid);
155 /* should really depend on receiving qt version, but how can
156 one tell? */
157 mh = new QVFbMousePipe(displayid);
158
159 QString mousePipe = mh->pipeName();
160
161 key_t key = ftok(mousePipe.toLatin1().constData(), 'b');
162
163 int bpl;
164 if (d < 8)
165 bpl = (w * d + 7) / 8;
166 else
167 bpl = w * ((d + 7) / 8);
168
169 displaySize = bpl * h;
170
171 unsigned char *data;
172 uint data_offset_value = sizeof(QVFbHeader);
173
174 int dataSize = bpl * h + data_offset_value;
175 shmId = shmget(key, dataSize, IPC_CREAT | 0666);
176 if (shmId != -1)
177 data = (unsigned char *)shmat(shmId, 0, 0);
178 else {
179 struct shmid_ds shm;
180 shmctl(shmId, IPC_RMID, &shm);
181 shmId = shmget(key, dataSize, IPC_CREAT | 0666);
182 if (shmId == -1) {
183 perror("QShMemViewProtocol::QShMemViewProtocol");
184 qFatal("Cannot get shared memory 0x%08x", key);
185 }
186 data = (unsigned char *)shmat(shmId, 0, 0);
187 }
188
189 if ((long)data == -1) {
190 delete kh;
191 delete mh;
192 perror("QShMemViewProtocol::QShMemViewProtocol");
193 qFatal("Cannot attach to shared memory %d",shmId);
194 }
195 dataCache = (unsigned char *)malloc(displaySize);
196 memset(dataCache, 0, displaySize);
197 memset(data+sizeof(QVFbHeader), 0, displaySize);
198
199 hdr = (QVFbHeader *)data;
200 hdr->width = w;
201 hdr->height = h;
202 hdr->depth = d;
203 hdr->linestep = bpl;
204 hdr->dataoffset = data_offset_value;
205 hdr->update = QRect();
206 hdr->dirty = 0;
207 hdr->numcols = 0;
208 hdr->viewerVersion = QT_VERSION;
209 hdr->brightness = 255;
210 hdr->windowId = 0;
211
212 displayPipe = qws_dataDir(displayid) + QString(QTE_PIPE).arg(displayid);
213
214 displayPiped = displayPipe + 'd';
215
216
217 mRefreshTimer = new QTimer(this);
218 connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(flushChanges()));
219}
220
221QShMemViewProtocol::~QShMemViewProtocol()
222{
223 struct shmid_ds shm;
224 shmdt( (char*)hdr );
225 shmctl( shmId, IPC_RMID, &shm );
226 free(dataCache);
227 delete kh;
228 delete mh;
229}
230
231int QShMemViewProtocol::width() const
232{
233 return hdr->width;
234}
235
236int QShMemViewProtocol::height() const
237{
238 return hdr->height;
239}
240
241int QShMemViewProtocol::depth() const
242{
243 return hdr->depth;
244}
245
246int QShMemViewProtocol::linestep() const
247{
248 return hdr->linestep;
249}
250
251int QShMemViewProtocol::numcols() const
252{
253 return hdr->numcols;
254}
255
256QVector<QRgb> QShMemViewProtocol::clut() const
257{
258 QVector<QRgb> vector(hdr->numcols);
259 for (int i=0; i < hdr->numcols; ++i)
260 vector[i]=hdr->clut[i];
261
262 return vector;
263}
264
265unsigned char *QShMemViewProtocol::data() const
266{
267 return dataCache;
268 //return ((unsigned char *)hdr)+hdr->dataoffset;
269}
270
271int QShMemViewProtocol::brightness() const
272{
273 return hdr->brightness;
274}
275
276void QShMemViewProtocol::flushChanges()
277{
278 QRect r;
279 if (hdr->dirty) {
280 DisplayLock();
281
282 hdr->dirty = false;
283 r = hdr->update;
284 hdr->update = QRect();
285
286 if (hdr->windowId != windowId) {
287 windowId = hdr->windowId;
288 emit displayEmbedRequested(hdr->windowId);
289 } else if (!hdr->windowId) {
290 // copy the memory area, for now, be inefficient.
291 memcpy(dataCache, ((char *)hdr) + hdr->dataoffset, displaySize);
292 }
293 }
294 emit displayDataChanged(r);
295}
296
297void QShMemViewProtocol::setRate(int interval)
298{
299 if (interval > 0)
300 return mRefreshTimer->start(1000/interval);
301 else
302 mRefreshTimer->stop();
303}
304
305int QShMemViewProtocol::rate() const
306{
307 int i = mRefreshTimer->interval();
308 if (i > 0)
309 return 1000/i;
310 else
311 return 0;
312}
313
314QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.