Warning: That file was not part of the compilation database. It may have many parsing errors.

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 tools applications 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#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/stat.h>
52#include <sys/mman.h>
53#include <fcntl.h>
54#include <errno.h>
55#include <math.h>
56
57QT_BEGIN_NAMESPACE
58
59QMMapViewProtocol::QMMapViewProtocol(int displayid, const QSize &s,
60 int d, QObject *parent)
61 : QVFbViewProtocol(displayid, parent), hdr(0), dataCache(0), windowId(0)
62{
63 switch (d) {
64 case 1:
65 case 4:
66 case 8:
67 case 12:
68 case 15:
69 case 16:
70 case 18:
71 case 24:
72 case 32:
73 break;
74 default:
75 qFatal("Unsupported bit depth %d\n", d);
76 }
77
78 fileName = QString("/tmp/.qtvfb_map-%1").arg(displayid);
79
80 int w = s.width();
81 int h = s.height();
82
83
84 kh = new QVFbKeyPipeProtocol(displayid);
85 mh = new QVFbMouseLinuxTP(displayid);
86
87 int bpl;
88 if (d < 8)
89 bpl = (w * d + 7) / 8;
90 else
91 bpl = w * ((d + 7) / 8);
92
93 displaySize = bpl * h;
94
95 unsigned char *data;
96 uint data_offset_value = sizeof(QVFbHeader);
97 const int page_size = getpagesize();
98 if (data_offset_value % page_size)
99 data_offset_value += page_size - (data_offset_value % page_size);
100
101 dataSize = bpl * h + data_offset_value;
102
103 unlink(fileName.toLocal8Bit().data());
104 fd = ::open( fileName.toLocal8Bit().data(), O_CREAT|O_RDWR, 0666 );
105 ::lseek(fd, dataSize, SEEK_SET);
106 ::write(fd, "\0", 1);
107 if (fd < 0) {
108 data = (unsigned char *)-1;
109 } else {
110 // might need to do something about size?
111 data = (unsigned char *)mmap(NULL, dataSize, PROT_WRITE | PROT_READ,
112 MAP_SHARED, fd, 0);
113 if (data == MAP_FAILED)
114 data = (unsigned char *)-1;
115 }
116
117 if ( (long)data == -1 ){
118 delete kh;
119 delete mh;
120 qFatal( "Cannot attach to mapped file %s", fileName.toLocal8Bit().data());
121 }
122 dataCache = (unsigned char *)malloc(displaySize);
123 memset(dataCache, 0, displaySize);
124 memset(data+sizeof(QVFbHeader), 0, displaySize);
125
126 hdr = (QVFbHeader *)data;
127 hdr->width = w;
128 hdr->height = h;
129 hdr->depth = d;
130 hdr->linestep = bpl;
131 hdr->numcols = 0;
132 hdr->dataoffset = data_offset_value;
133 hdr->update = QRect();
134 hdr->brightness = 255;
135 hdr->windowId = 0;
136
137 mRefreshTimer = new QTimer(this);
138 connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(flushChanges()));
139}
140
141QMMapViewProtocol::~QMMapViewProtocol()
142{
143 munmap((char *)hdr, dataSize);
144 ::close(fd);
145 unlink(fileName.toLocal8Bit().constData());
146 free(dataCache);
147 delete kh;
148 delete mh;
149}
150
151int QMMapViewProtocol::brightness() const
152{
153 return hdr->brightness;
154}
155
156int QMMapViewProtocol::width() const
157{
158 return hdr->width;
159}
160
161int QMMapViewProtocol::height() const
162{
163 return hdr->height;
164}
165
166int QMMapViewProtocol::depth() const
167{
168 return hdr->depth;
169}
170
171int QMMapViewProtocol::linestep() const
172{
173 return hdr->linestep;
174}
175
176int QMMapViewProtocol::numcols() const
177{
178 return hdr->numcols;
179}
180
181QVector<QRgb> QMMapViewProtocol::clut() const
182{
183 QVector<QRgb> vector(hdr->numcols);
184 for (int i=0; i < hdr->numcols; ++i)
185 vector[i] = hdr->clut[i];
186
187 return vector;
188}
189
190unsigned char *QMMapViewProtocol::data() const
191{
192 return dataCache;
193 //return ((unsigned char *)hdr)+hdr->dataoffset;
194}
195
196void QMMapViewProtocol::flushChanges()
197{
198 // based of dirty rect, copy changes from hdr to hdrcopy
199 memcpy(dataCache, ((char *)hdr) + hdr->dataoffset, displaySize);
200 emit displayDataChanged(QRect(0, 0, width(), height()));
201}
202
203void QMMapViewProtocol::setRate(int interval)
204{
205 if (interval > 0)
206 return mRefreshTimer->start(1000/interval);
207 else
208 mRefreshTimer->stop();
209}
210
211int QMMapViewProtocol::rate() const
212{
213 int i = mRefreshTimer->interval();
214 if (i > 0)
215 return 1000/i;
216 else
217 return 0;
218}
219
220QT_END_NAMESPACE
221

Warning: That file was not part of the compilation database. It may have many parsing errors.