| 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** 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 have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 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 |  | 
|---|
| 59 | QT_BEGIN_NAMESPACE | 
|---|
| 60 |  | 
|---|
| 61 | QMMapViewProtocol::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 |  | 
|---|
| 143 | QMMapViewProtocol::~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 |  | 
|---|
| 153 | int QMMapViewProtocol::brightness() const | 
|---|
| 154 | { | 
|---|
| 155 | return hdr->brightness; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | int QMMapViewProtocol::width() const | 
|---|
| 159 | { | 
|---|
| 160 | return hdr->width; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | int QMMapViewProtocol::height() const | 
|---|
| 164 | { | 
|---|
| 165 | return hdr->height; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | int QMMapViewProtocol::depth() const | 
|---|
| 169 | { | 
|---|
| 170 | return hdr->depth; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | int QMMapViewProtocol::linestep() const | 
|---|
| 174 | { | 
|---|
| 175 | return hdr->linestep; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | int  QMMapViewProtocol::numcols() const | 
|---|
| 179 | { | 
|---|
| 180 | return hdr->numcols; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | QVector<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 |  | 
|---|
| 192 | unsigned char *QMMapViewProtocol::data() const | 
|---|
| 193 | { | 
|---|
| 194 | return dataCache; | 
|---|
| 195 | //return ((unsigned char *)hdr)+hdr->dataoffset; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | void 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 |  | 
|---|
| 205 | void QMMapViewProtocol::setRate(int interval) | 
|---|
| 206 | { | 
|---|
| 207 | if (interval > 0) | 
|---|
| 208 | return mRefreshTimer->start(1000/interval); | 
|---|
| 209 | else | 
|---|
| 210 | mRefreshTimer->stop(); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | int QMMapViewProtocol::rate() const | 
|---|
| 214 | { | 
|---|
| 215 | int i = mRefreshTimer->interval(); | 
|---|
| 216 | if (i > 0) | 
|---|
| 217 | return 1000/i; | 
|---|
| 218 | else | 
|---|
| 219 | return 0; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | QT_END_NAMESPACE | 
|---|