Fixes a crash in QDoubleSpinBox
[qt-netbsd.git] / tools / qvfb / qvfbprotocol.cpp
blobf68a452b51f5f115746bcd4266a13309b6be452b
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.
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 ** 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.
36 ** If you have questions regarding the use of this file, please contact
37 ** 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 QT_BEGIN_NAMESPACE
61 QVFbViewProtocol::QVFbViewProtocol(int display_id, QObject *parent) :
62 QObject(parent), mDisplayId(display_id) { }
64 QVFbViewProtocol::~QVFbViewProtocol() {}
66 void QVFbViewProtocol::flushChanges() {}
68 void QVFbViewProtocol::sendKeyboardData(QString unicode, int keycode,
69 int modifiers, bool press, bool repeat)
71 if (keyHandler())
72 keyHandler()->sendKeyboardData(unicode, keycode, modifiers, press, repeat);
75 void QVFbViewProtocol::sendMouseData(const QPoint &pos, int buttons, int wheel)
77 if (mouseHandler())
78 mouseHandler()->sendMouseData(pos, buttons, wheel);
81 static int openPipe(const char *fileName)
83 unlink(fileName);
85 mkfifo(fileName, 0666);
86 int fd = ::open(fileName, O_RDWR | O_NDELAY);
87 return fd;
90 QVFbKeyPipeProtocol::QVFbKeyPipeProtocol(int display_id)
91 : QVFbKeyProtocol(display_id)
93 fileName = QString(QT_VFB_KEYBOARD_PIPE).arg(display_id);
94 fd = openPipe(fileName.toLocal8Bit().constData());
96 if (fd == -1)
97 qFatal("Cannot open keyboard pipe %s", fileName.toLocal8Bit().data());
100 QVFbKeyPipeProtocol::~QVFbKeyPipeProtocol()
102 sendKeyboardData(0, 0, 0, true, false); // magic die key
103 ::close(fd);
104 unlink(fileName.toLocal8Bit().constData());
107 void QVFbKeyPipeProtocol::sendKeyboardData(QString unicode, int keycode,
108 int modifiers, bool press, bool repeat)
110 QVFbKeyData kd;
111 kd.unicode = unicode[0].unicode();
112 kd.keycode = keycode;
113 kd.modifiers = static_cast<Qt::KeyboardModifier>(modifiers);
114 kd.press = press;
115 kd.repeat = repeat;
116 write(fd, &kd, sizeof(QVFbKeyData));
119 QVFbMousePipe::QVFbMousePipe(int display_id)
120 : QVFbMouseProtocol(display_id)
122 fileName = QString(QT_VFB_MOUSE_PIPE).arg(display_id);
123 fd = openPipe(fileName.toLocal8Bit().constData());
125 if (fd == -1)
126 qFatal("Cannot open mouse pipe %s", fileName.toLocal8Bit().data());
129 QVFbMousePipe::~QVFbMousePipe()
131 ::close(fd);
132 unlink(fileName.toLocal8Bit().constData());
136 void QVFbMousePipe::sendMouseData(const QPoint &pos, int buttons, int wheel)
138 write(fd, &pos, sizeof(QPoint));
139 write(fd, &buttons, sizeof(int));
140 write(fd, &wheel, sizeof(int));
143 QVFbMouseLinuxTP::QVFbMouseLinuxTP(int display_id)
144 : QObject(), QVFbMousePipe(display_id), lastPos(-1,-1)
146 /* the timer is needed because a real touch screen send data as long as
147 there is pressure. And the linux tp driver will filter, requiring
148 a minimum of 5 samples before it even registers a press.
150 repeater = new QTimer(this);
151 connect(repeater, SIGNAL(timeout()), this, SLOT(repeatLastPress()));
154 QVFbMouseLinuxTP::~QVFbMouseLinuxTP()
159 void QVFbMouseLinuxTP::sendMouseData(const QPoint &pos, int buttons, int)
161 if (buttons & Qt::LeftButton) {
162 // press
163 repeater->start(5);
164 writeToPipe(pos, 1);
165 lastPos = pos;
166 } else {
167 // release
168 if (lastPos == QPoint(-1,-1))
169 return; /* only send one release */
170 repeater->stop();
171 writeToPipe(pos, 0);
172 lastPos = QPoint(-1,-1);
176 void QVFbMouseLinuxTP::writeToPipe(const QPoint &pos, ushort pressure)
178 ushort v;
179 write(fd, &pressure, sizeof(ushort));
180 v = pos.x();
181 write(fd, &v, sizeof(ushort));
182 v = pos.y();
183 write(fd, &v, sizeof(ushort));
184 v = 1; // pad
185 write(fd, &v, sizeof(ushort));
188 void QVFbMouseLinuxTP::repeatLastPress()
190 writeToPipe(lastPos, 1);
193 QT_END_NAMESPACE