source: trunk/src/gui/embedded/qmouselinuxinput_qws.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the plugins 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.
15**
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.
23**
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.
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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qmouselinuxinput_qws.h"
43
44#include <QScreen>
45#include <QSocketNotifier>
46
47#include <qplatformdefs.h>
48#include <private/qcore_unix_p.h> // overrides QT_OPEN
49
50#include <errno.h>
51
52#include <linux/input.h>
53
54QT_BEGIN_NAMESPACE
55
56
57class QWSLinuxInputMousePrivate : public QObject
58{
59 Q_OBJECT
60public:
61 QWSLinuxInputMousePrivate(QWSLinuxInputMouseHandler *, const QString &);
62 ~QWSLinuxInputMousePrivate();
63
64 void enable(bool on);
65
66private Q_SLOTS:
67 void readMouseData();
68
69private:
70 QWSLinuxInputMouseHandler *m_handler;
71 QSocketNotifier * m_notify;
72 int m_fd;
73 int m_x, m_y;
74 int m_buttons;
75};
76
77QWSLinuxInputMouseHandler::QWSLinuxInputMouseHandler(const QString &device)
78 : QWSCalibratedMouseHandler(device)
79{
80 d = new QWSLinuxInputMousePrivate(this, device);
81}
82
83QWSLinuxInputMouseHandler::~QWSLinuxInputMouseHandler()
84{
85 delete d;
86}
87
88void QWSLinuxInputMouseHandler::suspend()
89{
90 d->enable(false);
91}
92
93void QWSLinuxInputMouseHandler::resume()
94{
95 d->enable(true);
96}
97
98QWSLinuxInputMousePrivate::QWSLinuxInputMousePrivate(QWSLinuxInputMouseHandler *h, const QString &device)
99 : m_handler(h), m_notify(0), m_x(0), m_y(0), m_buttons(0)
100{
101 setObjectName(QLatin1String("LinuxInputSubsystem Mouse Handler"));
102
103 QString dev = QLatin1String("/dev/input/event0");
104 if (device.startsWith(QLatin1String("/dev/")))
105 dev = device;
106
107 m_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);
108 if (m_fd >= 0) {
109 m_notify = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
110 connect(m_notify, SIGNAL(activated(int)), this, SLOT(readMouseData()));
111 } else {
112 qWarning("Cannot open mouse input device '%s': %s", qPrintable(dev), strerror(errno));
113 return;
114 }
115}
116
117QWSLinuxInputMousePrivate::~QWSLinuxInputMousePrivate()
118{
119 if (m_fd >= 0)
120 QT_CLOSE(m_fd);
121}
122
123void QWSLinuxInputMousePrivate::enable(bool on)
124{
125 if (m_notify)
126 m_notify->setEnabled(on);
127}
128
129void QWSLinuxInputMousePrivate::readMouseData()
130{
131 if (!qt_screen)
132 return;
133
134 struct ::input_event buffer[32];
135 int n = 0;
136
137 forever {
138 n = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
139
140 if (n == 0) {
141 qWarning("Got EOF from the input device.");
142 return;
143 } else if (n < 0 && (errno != EINTR && errno != EAGAIN)) {
144 qWarning("Could not read from input device: %s", strerror(errno));
145 return;
146 } else if (n % sizeof(buffer[0]) == 0) {
147 break;
148 }
149 }
150
151 n /= sizeof(buffer[0]);
152
153 for (int i = 0; i < n; ++i) {
154 struct ::input_event *data = &buffer[i];
155
156 bool unknown = false;
157 if (data->type == EV_ABS) {
158 if (data->code == ABS_X) {
159 m_x = data->value;
160 } else if (data->code == ABS_Y) {
161 m_y = data->value;
162 } else {
163 unknown = true;
164 }
165 } else if (data->type == EV_REL) {
166 if (data->code == REL_X) {
167 m_x += data->value;
168 } else if (data->code == REL_Y) {
169 m_y += data->value;
170 } else {
171 unknown = true;
172 }
173 } else if (data->type == EV_KEY && data->code == BTN_TOUCH) {
174 m_buttons = data->value ? Qt::LeftButton : 0;
175 } else if (data->type == EV_KEY) {
176 int button = 0;
177 switch (data->code) {
178 case BTN_LEFT: button = Qt::LeftButton; break;
179 case BTN_MIDDLE: button = Qt::MidButton; break;
180 case BTN_RIGHT: button = Qt::RightButton; break;
181 }
182 if (data->value)
183 m_buttons |= button;
184 else
185 m_buttons &= ~button;
186 } else if (data->type == EV_SYN && data->code == SYN_REPORT) {
187 QPoint pos(m_x, m_y);
188 pos = m_handler->transform(pos);
189 m_handler->limitToScreen(pos);
190 m_handler->mouseChanged(pos, m_buttons);
191 } else if (data->type == EV_MSC && data->code == MSC_SCAN) {
192 // kernel encountered an unmapped key - just ignore it
193 continue;
194 } else {
195 unknown = true;
196 }
197 if (unknown) {
198 qWarning("unknown mouse event type=%x, code=%x, value=%x", data->type, data->code, data->value);
199 }
200 }
201}
202
203QT_END_NAMESPACE
204
205#include "qmouselinuxinput_qws.moc"
Note: See TracBrowser for help on using the repository browser.