Qt 4.8
qtslib.cpp
Go to the documentation of this file.
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 QtGui module 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 
43 #include "qtslib.h"
44 
45 
46 #include <QSocketNotifier>
47 #include <QStringList>
48 #include <QPoint>
49 #include <QWindowSystemInterface>
50 
51 #include <Qt>
52 
53 #include <errno.h>
54 #include <tslib.h>
55 
56 #include <qdebug.h>
57 
59 
61  const QString &specification)
62  : m_notify(0), m_x(0), m_y(0), m_pressed(0), m_rawMode(false)
63 {
64  qDebug() << "QTsLibMouseHandler" << key << specification;
65  setObjectName(QLatin1String("TSLib Mouse Handler"));
66 
67  QByteArray device = "/dev/input/event1";
68  if (specification.startsWith("/dev/"))
69  device = specification.toLocal8Bit();
70 
71  m_dev = ts_open(device.constData(), 1);
72 
73  if (ts_config(m_dev)) {
74  perror("Error configuring\n");
75  }
76 
77 
79 
80  int fd = ts_fd(m_dev);
81  if (fd >= 0) {
83  connect(m_notify, SIGNAL(activated(int)), this, SLOT(readMouseData()));
84  } else {
85  qWarning("Cannot open mouse input device '%s': %s", device.constData(), strerror(errno));
86  return;
87  }
88 }
89 
90 
92 {
93  if (m_dev)
94  ts_close(m_dev);
95 }
96 
97 
98 static bool get_sample(struct tsdev *dev, struct ts_sample *sample, bool rawMode)
99 {
100  if (rawMode) {
101  return (ts_read_raw(dev, sample, 1) == 1);
102  } else {
103  int ret = ts_read(dev, sample, 1);
104  return ( ret == 1);
105  }
106 }
107 
108 
110 {
111  ts_sample sample;
112  while (get_sample(m_dev, &sample, m_rawMode)) {
113 
114  bool pressed = sample.pressure;
115  int x = sample.x;
116  int y = sample.y;
117 
118 
119  if (!m_rawMode) {
120  //filtering: ignore movements of 2 pixels or less
121  int dx = x - m_x;
122  int dy = y - m_y;
123  if (dx*dx <= 4 && dy*dy <= 4 && pressed == m_pressed)
124  continue;
125  } else {
126  // work around missing coordinates on mouse release in raw mode
127  if (sample.pressure == 0 && sample.x == 0 && sample.y == 0) {
128  x = m_x;
129  y = m_y;
130  }
131  }
132  QPoint pos(x, y);
133 
134  //printf("handleMouseEvent %d %d %d %ld\n", m_x, m_y, pressed, sample.tv.tv_usec);
135 
137 
138  m_x = x;
139  m_y = y;
140  m_pressed = pressed;
141  }
142 }
143 
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QTsLibMouseHandler(const QString &key, const QString &specification)
Definition: qtslib.cpp:60
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
Q_CORE_EXPORT void qDebug(const char *,...)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
static void handleMouseEvent(QWidget *w, const QPoint &local, const QPoint &global, Qt::MouseButtons b)
tlw == 0 means that ev is in global coords only
Q_CORE_EXPORT void qWarning(const char *,...)
void readMouseData()
Definition: qtslib.cpp:109
QSocketNotifier * m_notify
Definition: qtslib.h:66
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int compare(const QString &s) const
Definition: qstring.cpp:5037
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
static bool get_sample(struct tsdev *dev, struct ts_sample *sample, bool rawMode)
Definition: qtslib.cpp:98
tsdev * m_dev
Definition: qtslib.h:67
int errno