1
/***************************************************************************
2
* Copyright (C) 2009 by Kai Dombrowe <just89@gmx.de> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18
***************************************************************************/
28
#include <QtCore/QSocketNotifier>
29
#include <QtCore/QMetaType>
30
#include <QtCore/QDir>
34
#include <linux/input.h>
43
Device::Device(QObject *parent, const QString &file)
47
qRegisterMetaType<KeyMon::Event>("KeyMon::Event");
50
int fd = open(file.toLatin1(), O_RDONLY|O_NONBLOCK);
52
kWarning() << "open failed!";
58
m_socketNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
59
connect(m_socketNotifier, SIGNAL(activated(int)), this, SLOT(readEvents()));
67
if (m_socketNotifier) {
68
delete m_socketNotifier;
74
bool Device::error() const
82
QList<DeviceData> Device::getDeviceList()
85
QList<DeviceData> deviceList;
86
QDir input("/dev/input");
87
if (!input.exists()) {
88
kWarning() << "/dev/input: no such directory!";
92
QStringList files = input.entryList(QDir::System);
93
const QRegExp rx("^event[0-9]+$");
95
foreach (const QString &file, files) {
96
if (rx.exactMatch(file)) {
97
const DeviceData device = getDevice(file);
98
if (!device.first.isEmpty() && !device.second.isEmpty()) {
99
deviceList.append(device);
108
DeviceData Device::getDevice(const QString &file)
115
if (!path.startsWith(QLatin1Char('/'))) {
116
path.prepend("/dev/input/");
119
if ((fd = open (path.toLatin1(), O_RDONLY)) == -1) {
120
kWarning() << path << ": open failed!";
123
ioctl(fd, EVIOCGNAME(sizeof(buff)), buff);
125
device.first = QString(buff);
127
device.second = path;
134
void Device::readEvents()
137
int fd = m_socketNotifier->socket();
139
struct input_event ev;
140
int bytesRead = read(fd, &ev, sizeof(ev));
141
if (bytesRead <= 0) {
144
if (bytesRead != sizeof(ev)) {
145
kWarning("Internal error!");
149
const bool pressed = (ev.value == 1);
150
KeyMon::Event::Key key;
154
key = KeyMon::Event::LeftButton;
157
key = KeyMon::Event::RightButton;
160
key = KeyMon::Event::MiddleButton;
163
key = KeyMon::Event::SpecialButton1;
166
key = KeyMon::Event::SpecialButton2;
170
key = KeyMon::Event::WheelUp;
172
key = KeyMon::Event::WheelDown;
176
key = KeyMon::Event::NoButton;
180
if (key != KeyMon::Event::NoButton) {
181
KeyMon::Event sEvent;
183
sEvent.pressed = pressed;
184
emit buttonPressed(sEvent);
191
}; // Namespace KeyMon
194
#include "device.moc"