author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Mon, 19 Apr 2010 10:15:19 +0300 | |
branch | RCL_3 |
changeset 9 | b5b118452c7d |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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. |
|
15 |
** |
|
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. |
|
23 |
** |
|
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. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qlock_p.h" |
|
43 |
||
44 |
#include "qvfbshmem.h" |
|
45 |
#include "qvfbhdr.h" |
|
46 |
||
47 |
#include <QFile> |
|
48 |
#include <QTimer> |
|
49 |
||
50 |
#include <stdlib.h> |
|
51 |
#include <unistd.h> |
|
52 |
#include <sys/ipc.h> |
|
53 |
#include <sys/types.h> |
|
54 |
#include <sys/shm.h> |
|
55 |
#include <sys/stat.h> |
|
56 |
#include <sys/sem.h> |
|
57 |
#include <sys/mman.h> |
|
58 |
#include <fcntl.h> |
|
59 |
#include <errno.h> |
|
60 |
#include <math.h> |
|
61 |
||
62 |
QT_BEGIN_NAMESPACE |
|
63 |
||
64 |
#ifdef Q_WS_QWS |
|
65 |
#error qvfb must be compiled with the Qt for X11 package |
|
66 |
#endif |
|
67 |
||
68 |
// Get the name of the directory where Qt for Embedded Linux temporary data should |
|
69 |
// live. |
|
70 |
static QString qws_dataDir(int qws_display_id) |
|
71 |
{ |
|
72 |
QByteArray dataDir = QT_VFB_DATADIR(qws_display_id).toLocal8Bit(); |
|
73 |
if (mkdir(dataDir, 0700)) { |
|
74 |
if (errno != EEXIST) { |
|
75 |
qFatal("Cannot create Qt for Embedded Linux data directory: %s", dataDir.constData()); |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
struct stat buf; |
|
80 |
if (lstat(dataDir, &buf)) |
|
81 |
qFatal("stat failed for Qt for Embedded Linux data directory: %s", dataDir.constData()); |
|
82 |
||
83 |
if (!S_ISDIR(buf.st_mode)) |
|
84 |
qFatal("%s is not a directory", dataDir.constData()); |
|
85 |
if (buf.st_uid != getuid()) |
|
86 |
qFatal("Qt for Embedded Linux data directory is not owned by user %uh", getuid()); |
|
87 |
||
88 |
if ((buf.st_mode & 0677) != 0600) |
|
89 |
qFatal("Qt for Embedded Linux data directory has incorrect permissions: %s", dataDir.constData()); |
|
90 |
dataDir += "/"; |
|
91 |
||
92 |
return QString(dataDir); |
|
93 |
} |
|
94 |
||
95 |
||
96 |
static QString displayPipe; |
|
97 |
static QString displayPiped; |
|
98 |
class DisplayLock |
|
99 |
{ |
|
100 |
public: |
|
101 |
DisplayLock() : qlock(0) { |
|
102 |
if (QFile::exists(displayPiped)) { |
|
103 |
qlock = new QLock(displayPipe, 'd', false); |
|
104 |
qlock->lock(QLock::Read); |
|
105 |
} |
|
106 |
} |
|
107 |
~DisplayLock() { |
|
108 |
if (qlock) { |
|
109 |
qlock->unlock(); |
|
110 |
delete qlock; |
|
111 |
qlock = 0; |
|
112 |
} |
|
113 |
} |
|
114 |
private: |
|
115 |
QLock *qlock; |
|
116 |
}; |
|
117 |
||
118 |
QShMemViewProtocol::QShMemViewProtocol(int displayid, const QSize &s, |
|
119 |
int d, QObject *parent) |
|
120 |
: QVFbViewProtocol(displayid, parent), hdr(0), dataCache(0), lockId(-1), |
|
121 |
windowId(0) |
|
122 |
{ |
|
123 |
int w = s.width(); |
|
124 |
int h = s.height(); |
|
125 |
||
126 |
QString username = "unknown"; |
|
127 |
const char *logname = getenv("LOGNAME"); |
|
128 |
if ( logname ) |
|
129 |
username = logname; |
|
130 |
||
131 |
qws_dataDir(displayid); |
|
132 |
||
133 |
QString oldPipe = "/tmp/qtembedded-" + username + "/" + QString("QtEmbedded-%1").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 |
displayPipe = QTE_PIPE_QVFB(displayid); |
|
155 |
||
156 |
kh = new QVFbKeyPipeProtocol(displayid); |
|
157 |
/* should really depend on receiving qt version, but how can |
|
158 |
one tell? */ |
|
159 |
mh = new QVFbMousePipe(displayid); |
|
160 |
||
161 |
QString mousePipe = mh->pipeName(); |
|
162 |
||
163 |
key_t key = ftok(mousePipe.toLatin1().constData(), 'b'); |
|
164 |
||
165 |
int bpl; |
|
166 |
if (d < 8) |
|
167 |
bpl = (w * d + 7) / 8; |
|
168 |
else |
|
169 |
bpl = w * ((d + 7) / 8); |
|
170 |
||
171 |
displaySize = bpl * h; |
|
172 |
||
173 |
unsigned char *data; |
|
174 |
uint data_offset_value = sizeof(QVFbHeader); |
|
175 |
||
176 |
int dataSize = bpl * h + data_offset_value; |
|
177 |
shmId = shmget(key, dataSize, IPC_CREAT | 0666); |
|
178 |
if (shmId != -1) |
|
179 |
data = (unsigned char *)shmat(shmId, 0, 0); |
|
180 |
else { |
|
181 |
struct shmid_ds shm; |
|
182 |
shmctl(shmId, IPC_RMID, &shm); |
|
183 |
shmId = shmget(key, dataSize, IPC_CREAT | 0666); |
|
184 |
if (shmId == -1) { |
|
185 |
perror("QShMemViewProtocol::QShMemViewProtocol"); |
|
186 |
qFatal("Cannot get shared memory 0x%08x", key); |
|
187 |
} |
|
188 |
data = (unsigned char *)shmat(shmId, 0, 0); |
|
189 |
} |
|
190 |
||
191 |
if ((long)data == -1) { |
|
192 |
delete kh; |
|
193 |
delete mh; |
|
194 |
perror("QShMemViewProtocol::QShMemViewProtocol"); |
|
195 |
qFatal("Cannot attach to shared memory %d",shmId); |
|
196 |
} |
|
197 |
dataCache = (unsigned char *)malloc(displaySize); |
|
198 |
memset(dataCache, 0, displaySize); |
|
199 |
memset(data+sizeof(QVFbHeader), 0, displaySize); |
|
200 |
||
201 |
hdr = (QVFbHeader *)data; |
|
202 |
hdr->width = w; |
|
203 |
hdr->height = h; |
|
204 |
hdr->depth = d; |
|
205 |
hdr->linestep = bpl; |
|
206 |
hdr->dataoffset = data_offset_value; |
|
207 |
hdr->update = QRect(); |
|
208 |
hdr->dirty = 0; |
|
209 |
hdr->numcols = 0; |
|
210 |
hdr->viewerVersion = QT_VERSION; |
|
211 |
hdr->brightness = 255; |
|
212 |
hdr->windowId = 0; |
|
213 |
||
214 |
displayPiped = displayPipe + 'd'; |
|
215 |
||
216 |
||
217 |
mRefreshTimer = new QTimer(this); |
|
218 |
connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(flushChanges())); |
|
219 |
} |
|
220 |
||
221 |
QShMemViewProtocol::~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 |
||
231 |
int QShMemViewProtocol::width() const |
|
232 |
{ |
|
233 |
return hdr->width; |
|
234 |
} |
|
235 |
||
236 |
int QShMemViewProtocol::height() const |
|
237 |
{ |
|
238 |
return hdr->height; |
|
239 |
} |
|
240 |
||
241 |
int QShMemViewProtocol::depth() const |
|
242 |
{ |
|
243 |
return hdr->depth; |
|
244 |
} |
|
245 |
||
246 |
int QShMemViewProtocol::linestep() const |
|
247 |
{ |
|
248 |
return hdr->linestep; |
|
249 |
} |
|
250 |
||
251 |
int QShMemViewProtocol::numcols() const |
|
252 |
{ |
|
253 |
return hdr->numcols; |
|
254 |
} |
|
255 |
||
256 |
QVector<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 |
||
265 |
unsigned char *QShMemViewProtocol::data() const |
|
266 |
{ |
|
267 |
return dataCache; |
|
268 |
//return ((unsigned char *)hdr)+hdr->dataoffset; |
|
269 |
} |
|
270 |
||
271 |
int QShMemViewProtocol::brightness() const |
|
272 |
{ |
|
273 |
return hdr->brightness; |
|
274 |
} |
|
275 |
||
276 |
void 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 |
||
297 |
void QShMemViewProtocol::setRate(int interval) |
|
298 |
{ |
|
299 |
if (interval > 0) |
|
300 |
return mRefreshTimer->start(1000/interval); |
|
301 |
else |
|
302 |
mRefreshTimer->stop(); |
|
303 |
} |
|
304 |
||
305 |
int QShMemViewProtocol::rate() const |
|
306 |
{ |
|
307 |
int i = mRefreshTimer->interval(); |
|
308 |
if (i > 0) |
|
309 |
return 1000/i; |
|
310 |
else |
|
311 |
return 0; |
|
312 |
} |
|
313 |
||
314 |
QT_END_NAMESPACE |