1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins 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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qmouselinuxinput_qws.h"
43
44 #include <QScreen>
45 #include <QSocketNotifier>
46 #include <QStringList>
47
48 #include <qplatformdefs.h>
49 #include <private/qcore_unix_p.h> // overrides QT_OPEN
50
51 #include <errno.h>
52
53 #include <linux/input.h>
54
55 QT_BEGIN_NAMESPACE
56
57
58 class QWSLinuxInputMousePrivate : public QObject
59 {
60 Q_OBJECT
61 public:
62 QWSLinuxInputMousePrivate(QWSLinuxInputMouseHandler *, const QString &);
63 ~QWSLinuxInputMousePrivate();
64
65 void enable(bool on);
66
67 private Q_SLOTS:
68 void readMouseData();
69
70 private:
71 QWSLinuxInputMouseHandler *m_handler;
72 QSocketNotifier * m_notify;
73 int m_fd;
74 int m_x, m_y;
75 int m_buttons;
76 };
77
QWSLinuxInputMouseHandler(const QString & device)78 QWSLinuxInputMouseHandler::QWSLinuxInputMouseHandler(const QString &device)
79 : QWSCalibratedMouseHandler(device)
80 {
81 d = new QWSLinuxInputMousePrivate(this, device);
82 }
83
~QWSLinuxInputMouseHandler()84 QWSLinuxInputMouseHandler::~QWSLinuxInputMouseHandler()
85 {
86 delete d;
87 }
88
suspend()89 void QWSLinuxInputMouseHandler::suspend()
90 {
91 d->enable(false);
92 }
93
resume()94 void QWSLinuxInputMouseHandler::resume()
95 {
96 d->enable(true);
97 }
98
QWSLinuxInputMousePrivate(QWSLinuxInputMouseHandler * h,const QString & device)99 QWSLinuxInputMousePrivate::QWSLinuxInputMousePrivate(QWSLinuxInputMouseHandler *h, const QString &device)
100 : m_handler(h), m_notify(0), m_x(0), m_y(0), m_buttons(0)
101 {
102 setObjectName(QLatin1String("LinuxInputSubsystem Mouse Handler"));
103
104 QString dev = QLatin1String("/dev/input/event0");
105 int grab = 0;
106
107 QStringList args = device.split(QLatin1Char(':'));
108 foreach (const QString &arg, args) {
109 if (arg.startsWith(QLatin1String("grab=")))
110 grab = arg.mid(5).toInt();
111 else if (arg.startsWith(QLatin1String("/dev/")))
112 dev = arg;
113 }
114
115 m_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
116 if (m_fd >= 0) {
117 ::ioctl(m_fd, EVIOCGRAB, grab);
118 m_notify = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
119 connect(m_notify, SIGNAL(activated(int)), this, SLOT(readMouseData()));
120 } else {
121 qWarning("Cannot open mouse input device '%s': %s", qPrintable(dev), strerror(errno));
122 return;
123 }
124 }
125
~QWSLinuxInputMousePrivate()126 QWSLinuxInputMousePrivate::~QWSLinuxInputMousePrivate()
127 {
128 if (m_fd >= 0)
129 QT_CLOSE(m_fd);
130 }
131
enable(bool on)132 void QWSLinuxInputMousePrivate::enable(bool on)
133 {
134 if (m_notify)
135 m_notify->setEnabled(on);
136 }
137
readMouseData()138 void QWSLinuxInputMousePrivate::readMouseData()
139 {
140 if (!qt_screen)
141 return;
142
143 struct ::input_event buffer[32];
144 int n = 0;
145
146 forever {
147 int bytesRead = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
148 if (bytesRead == 0) {
149 qWarning("Got EOF from the input device.");
150 return;
151 }
152 if (bytesRead == -1) {
153 if (errno != EAGAIN)
154 qWarning("Could not read from input device: %s", strerror(errno));
155 break;
156 }
157
158 n += bytesRead;
159 if (n % sizeof(buffer[0]) == 0)
160 break;
161 }
162 n /= sizeof(buffer[0]);
163
164 for (int i = 0; i < n; ++i) {
165 struct ::input_event *data = &buffer[i];
166
167 bool unknown = false;
168 if (data->type == EV_ABS) {
169 if (data->code == ABS_X) {
170 m_x = data->value;
171 } else if (data->code == ABS_Y) {
172 m_y = data->value;
173 } else {
174 unknown = true;
175 }
176 } else if (data->type == EV_REL) {
177 if (data->code == REL_X) {
178 m_x += data->value;
179 } else if (data->code == REL_Y) {
180 m_y += data->value;
181 } else {
182 unknown = true;
183 }
184 } else if (data->type == EV_KEY && data->code == BTN_TOUCH) {
185 m_buttons = data->value ? Qt::LeftButton : 0;
186 } else if (data->type == EV_KEY) {
187 int button = 0;
188 switch (data->code) {
189 case BTN_LEFT: button = Qt::LeftButton; break;
190 case BTN_MIDDLE: button = Qt::MidButton; break;
191 case BTN_RIGHT: button = Qt::RightButton; break;
192 }
193 if (data->value)
194 m_buttons |= button;
195 else
196 m_buttons &= ~button;
197 } else if (data->type == EV_SYN && data->code == SYN_REPORT) {
198 QPoint pos(m_x, m_y);
199 pos = m_handler->transform(pos);
200 m_handler->limitToScreen(pos);
201 m_handler->mouseChanged(pos, m_buttons);
202 } else if (data->type == EV_MSC && data->code == MSC_SCAN) {
203 // kernel encountered an unmapped key - just ignore it
204 continue;
205 } else {
206 unknown = true;
207 }
208 if (unknown) {
209 qWarning("unknown mouse event type=%x, code=%x, value=%x", data->type, data->code, data->value);
210 }
211 }
212 }
213
214 QT_END_NAMESPACE
215
216 #include "qmouselinuxinput_qws.moc"
217