source: trunk/tools/qvfb/qvfbprotocol.cpp@ 372

Last change on this file since 372 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qvfbprotocol.h"
43#include "qvfbhdr.h"
44
45#include <QDebug>
46#include <QTimer>
47
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>
54
55#ifdef Q_OS_UNIX
56#include <unistd.h>
57#endif
58
59QT_BEGIN_NAMESPACE
60
61QVFbViewProtocol::QVFbViewProtocol(int display_id, QObject *parent) :
62 QObject(parent), mDisplayId(display_id) { }
63
64QVFbViewProtocol::~QVFbViewProtocol() {}
65
66void QVFbViewProtocol::flushChanges() {}
67
68void QVFbViewProtocol::sendKeyboardData(QString unicode, int keycode,
69 int modifiers, bool press, bool repeat)
70{
71 if (keyHandler())
72 keyHandler()->sendKeyboardData(unicode, keycode, modifiers, press, repeat);
73}
74
75void QVFbViewProtocol::sendMouseData(const QPoint &pos, int buttons, int wheel)
76{
77 if (mouseHandler())
78 mouseHandler()->sendMouseData(pos, buttons, wheel);
79}
80
81static int openPipe(const char *fileName)
82{
83 unlink(fileName);
84
85 mkfifo(fileName, 0666);
86 int fd = ::open(fileName, O_RDWR | O_NDELAY);
87 return fd;
88}
89
90QVFbKeyPipeProtocol::QVFbKeyPipeProtocol(int display_id)
91 : QVFbKeyProtocol(display_id)
92{
93 fileName = QString(QT_VFB_KEYBOARD_PIPE).arg(display_id);
94 fd = openPipe(fileName.toLocal8Bit().constData());
95
96 if (fd == -1)
97 qFatal("Cannot open keyboard pipe %s", fileName.toLocal8Bit().data());
98}
99
100QVFbKeyPipeProtocol::~QVFbKeyPipeProtocol()
101{
102 sendKeyboardData(0, 0, 0, true, false); // magic die key
103 ::close(fd);
104 unlink(fileName.toLocal8Bit().constData());
105}
106
107void QVFbKeyPipeProtocol::sendKeyboardData(QString unicode, int keycode,
108 int modifiers, bool press, bool repeat)
109{
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));
117}
118
119QVFbMousePipe::QVFbMousePipe(int display_id)
120 : QVFbMouseProtocol(display_id)
121{
122 fileName = QString(QT_VFB_MOUSE_PIPE).arg(display_id);
123 fd = openPipe(fileName.toLocal8Bit().constData());
124
125 if (fd == -1)
126 qFatal("Cannot open mouse pipe %s", fileName.toLocal8Bit().data());
127}
128
129QVFbMousePipe::~QVFbMousePipe()
130{
131 ::close(fd);
132 unlink(fileName.toLocal8Bit().constData());
133}
134
135
136void QVFbMousePipe::sendMouseData(const QPoint &pos, int buttons, int wheel)
137{
138 write(fd, &pos, sizeof(QPoint));
139 write(fd, &buttons, sizeof(int));
140 write(fd, &wheel, sizeof(int));
141}
142
143QVFbMouseLinuxTP::QVFbMouseLinuxTP(int display_id)
144 : QObject(), QVFbMousePipe(display_id), lastPos(-1,-1)
145{
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.
149 */
150 repeater = new QTimer(this);
151 connect(repeater, SIGNAL(timeout()), this, SLOT(repeatLastPress()));
152}
153
154QVFbMouseLinuxTP::~QVFbMouseLinuxTP()
155{
156}
157
158
159void QVFbMouseLinuxTP::sendMouseData(const QPoint &pos, int buttons, int)
160{
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);
173 }
174}
175
176void QVFbMouseLinuxTP::writeToPipe(const QPoint &pos, ushort pressure)
177{
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));
186}
187
188void QVFbMouseLinuxTP::repeatLastPress()
189{
190 writeToPipe(lastPos, 1);
191}
192
193QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.