Don't try to mmap past EOF
[qt-netbsd.git] / tools / qvfb / qvfbprotocol.cpp
blobdf6c110a9a5861851544a3578a1c6c821db97124
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 ** 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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "qvfbprotocol.h"
43 #include "qvfbhdr.h"
45 #include <QDebug>
46 #include <QTimer>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <math.h>
51 #include <fcntl.h>
52 #include <stdlib.h>
53 #include <stdio.h>
55 #ifdef Q_OS_UNIX
56 #include <unistd.h>
57 #endif
59 #include "qvfbshmem.h"
61 QT_BEGIN_NAMESPACE
63 QVFbViewProtocol::QVFbViewProtocol(int display_id, QObject *parent) :
64 QObject(parent), mDisplayId(display_id) { }
66 QVFbViewProtocol::~QVFbViewProtocol() {}
68 void QVFbViewProtocol::flushChanges() {}
70 void QVFbViewProtocol::sendKeyboardData(QString unicode, int keycode,
71 int modifiers, bool press, bool repeat)
73 if (keyHandler())
74 keyHandler()->sendKeyboardData(unicode, keycode, modifiers, press, repeat);
77 void QVFbViewProtocol::sendMouseData(const QPoint &pos, int buttons, int wheel)
79 if (mouseHandler())
80 mouseHandler()->sendMouseData(pos, buttons, wheel);
83 static int openPipe(const char *fileName)
85 unlink(fileName);
87 mkfifo(fileName, 0666);
88 int fd = ::open(fileName, O_RDWR | O_NDELAY);
89 return fd;
92 QVFbKeyPipeProtocol::QVFbKeyPipeProtocol(int display_id)
93 : QVFbKeyProtocol(display_id)
95 fileName = QT_VFB_KEYBOARD_PIPE(display_id);
96 fd = openPipe(fileName.toLocal8Bit().constData());
98 if (fd == -1)
99 qFatal("Cannot open keyboard pipe %s", fileName.toLocal8Bit().data());
102 QVFbKeyPipeProtocol::~QVFbKeyPipeProtocol()
104 sendKeyboardData(0, 0, 0, true, false); // magic die key
105 ::close(fd);
106 unlink(fileName.toLocal8Bit().constData());
109 void QVFbKeyPipeProtocol::sendKeyboardData(QString unicode, int keycode,
110 int modifiers, bool press, bool repeat)
112 QVFbKeyData kd;
113 kd.unicode = unicode[0].unicode();
114 kd.keycode = keycode;
115 kd.modifiers = static_cast<Qt::KeyboardModifier>(modifiers);
116 kd.press = press;
117 kd.repeat = repeat;
118 write(fd, &kd, sizeof(QVFbKeyData));
121 QVFbMousePipe::QVFbMousePipe(int display_id)
122 : QVFbMouseProtocol(display_id)
124 fileName = QT_VFB_MOUSE_PIPE(display_id);
125 fd = openPipe(fileName.toLocal8Bit().constData());
127 if (fd == -1)
128 qFatal("Cannot open mouse pipe %s", fileName.toLocal8Bit().data());
131 QVFbMousePipe::~QVFbMousePipe()
133 ::close(fd);
134 unlink(fileName.toLocal8Bit().constData());
138 void QVFbMousePipe::sendMouseData(const QPoint &pos, int buttons, int wheel)
140 write(fd, &pos, sizeof(QPoint));
141 write(fd, &buttons, sizeof(int));
142 write(fd, &wheel, sizeof(int));
145 QVFbMouseLinuxTP::QVFbMouseLinuxTP(int display_id)
146 : QObject(), QVFbMousePipe(display_id), lastPos(-1,-1)
148 /* the timer is needed because a real touch screen send data as long as
149 there is pressure. And the linux tp driver will filter, requiring
150 a minimum of 5 samples before it even registers a press.
152 repeater = new QTimer(this);
153 connect(repeater, SIGNAL(timeout()), this, SLOT(repeatLastPress()));
156 QVFbMouseLinuxTP::~QVFbMouseLinuxTP()
161 void QVFbMouseLinuxTP::sendMouseData(const QPoint &pos, int buttons, int)
163 if (buttons & Qt::LeftButton) {
164 // press
165 repeater->start(5);
166 writeToPipe(pos, 1);
167 lastPos = pos;
168 } else {
169 // release
170 if (lastPos == QPoint(-1,-1))
171 return; /* only send one release */
172 repeater->stop();
173 writeToPipe(pos, 0);
174 lastPos = QPoint(-1,-1);
178 void QVFbMouseLinuxTP::writeToPipe(const QPoint &pos, ushort pressure)
180 ushort v;
181 write(fd, &pressure, sizeof(ushort));
182 v = pos.x();
183 write(fd, &v, sizeof(ushort));
184 v = pos.y();
185 write(fd, &v, sizeof(ushort));
186 v = 1; // pad
187 write(fd, &v, sizeof(ushort));
190 void QVFbMouseLinuxTP::repeatLastPress()
192 writeToPipe(lastPos, 1);
195 QT_END_NAMESPACE