summaryrefslogtreecommitdiff
path: root/libopie2/opiecore/linux/oinputsystem.cpp
Unidiff
Diffstat (limited to 'libopie2/opiecore/linux/oinputsystem.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiecore/linux/oinputsystem.cpp222
1 files changed, 222 insertions, 0 deletions
diff --git a/libopie2/opiecore/linux/oinputsystem.cpp b/libopie2/opiecore/linux/oinputsystem.cpp
new file mode 100644
index 0000000..bad27ed
--- a/dev/null
+++ b/libopie2/opiecore/linux/oinputsystem.cpp
@@ -0,0 +1,222 @@
1/*
2                 This file is part of the Opie Project
3 =. (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de>
4 .=l.
5           .>+-=
6 _;:,     .>    :=|. This program is free software; you can
7.> <`_,   >  .   <= redistribute it and/or modify it under
8:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
9.="- .-=="i,     .._ License as published by the Free Software
10 - .   .-<_>     .<> Foundation; version 2 of the License.
11     ._= =}       :
12    .%`+i>       _;_.
13    .i_,=:_.      -<s. This program is distributed in the hope that
14     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
15    : ..    .:,     . . . without even the implied warranty of
16    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
17  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
18..}^=.=       =       ; Library General Public License for more
19++=   -.     .`     .: details.
20 :     =  ...= . :.=-
21 -.   .:....=;==+<; You should have received a copy of the GNU
22  -_. . .   )=.  = Library General Public License along with
23    --        :-=` this library; see the file COPYING.LIB.
24 If not, write to the Free Software Foundation,
25 Inc., 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA.
27*/
28
29#include "oinputsystem.h"
30using namespace Opie::Core;
31
32/* QT */
33#include <qdir.h>
34#include <qfile.h>
35
36/* STD */
37#include <errno.h>
38#include <string.h>
39#include <sys/fcntl.h>
40#include <sys/ioctl.h>
41#include <unistd.h>
42
43#define BUFSIZE 256
44#define BIT_MASK( name, numbits ) \
45 unsigned short name[ ((numbits) - 1) / (sizeof( short ) * 8) + 1 ]; \
46 memset( name, 0, sizeof( name ) )
47#define BIT_TEST( bitmask, bit ) \
48 ( bitmask[ (bit) / sizeof(short) / 8 ] & (1u << ( (bit) % (sizeof(short) * 8))) )
49
50/*======================================================================================
51 * OInputSystem
52 *======================================================================================*/
53
54OInputSystem* OInputSystem::_instance = 0;
55
56OInputSystem::OInputSystem() : QObject()
57{
58 qDebug( "OInputSystem::OInputSystem()" );
59 synchronize();
60}
61
62
63void OInputSystem::synchronize()
64{
65 qDebug( "OInputSystem::synchronize()" );
66 QDir devInput( "/dev/input/" );
67 if ( devInput.exists() )
68 {
69 QStringList devInputFiles = devInput.entryList( QDir::System, QDir::Name );
70 for ( QStringList::Iterator it = devInputFiles.begin(); it != devInputFiles.end(); ++it )
71 {
72 QString absPath = devInput.absFilePath( *it );
73 bool isValid = OInputDevice::isValid( absPath );
74 qDebug( "OInputSystem::synchronize() - checking if '%s' is a valid input system node... '%s' [%s]",
75 (const char*) absPath, isValid ? "yes" : "no", isValid ? "(ok)" : strerror( errno ) );
76 if ( isValid ) _devices.insert( *it, new OInputDevice( this, absPath ) );
77 }
78 }
79 qDebug( "OInputSystem::synchronize() done" );
80 if ( !_devices.count() )
81 qWarning( "OInputSystem::no devices found" );
82}
83
84
85OInputSystem::~OInputSystem()
86{
87 qDebug( "OInputSystem::~OInputSystem()" );
88}
89
90
91int OInputSystem::count() const
92{
93 return _devices.count();
94}
95
96
97OInputDevice* OInputSystem::device( const QString& device ) const
98{
99 return _devices[device];
100}
101
102
103OInputSystem* OInputSystem::instance()
104{
105 if ( !_instance ) _instance = new OInputSystem();
106 return _instance;
107}
108
109
110OInputSystem::DeviceIterator OInputSystem::iterator() const
111{
112 return OInputSystem::DeviceIterator( _devices );
113}
114
115/*======================================================================================
116 * OInputDevice
117 *======================================================================================*/
118
119OInputDevice::OInputDevice( QObject* parent, const char* name ) : QObject( parent, name )
120{
121 qDebug( "OInputDevice::OInputDevice( '%s' )", name );
122
123 _fd = ::open( name, O_RDONLY );
124 if ( _fd == -1 )
125 {
126 qDebug( "OInputDevice::OInputDevice() - Warning: couldn't open %s (%s)", name, strerror( errno ) );
127 }
128}
129
130
131OInputDevice::~OInputDevice()
132{
133 qDebug( "OInputDevice::~OInputDevice()" );
134}
135
136
137QString OInputDevice::identity() const
138{
139 char buf[BUFSIZE] = "<unknown>";
140 ::ioctl( _fd, EVIOCGNAME(sizeof buf), buf );
141 return buf;
142}
143
144
145QString OInputDevice::path() const
146{
147 char buf[BUFSIZE] = "<unknown>";
148 ::ioctl( _fd, EVIOCGPHYS(sizeof buf), buf );
149 return buf;
150}
151
152
153QString OInputDevice::uniq() const
154{
155 char buf[BUFSIZE] = "<unknown>";
156 ::ioctl( _fd, EVIOCGUNIQ(sizeof buf), buf );
157 return buf;
158}
159
160
161bool OInputDevice::hasFeature( Feature bit ) const
162{
163 BIT_MASK( features, EV_MAX );
164
165 if( ioctl( _fd, EVIOCGBIT( 0, EV_MAX ), features) < 0 )
166 {
167 perror( "EVIOCGBIT" );
168 return false;
169 }
170 else
171 return BIT_TEST( features, bit );
172}
173
174
175bool OInputDevice::isHeld( Key bit ) const
176{
177 BIT_MASK( keys, KEY_MAX );
178
179 if( ioctl( _fd, EVIOCGKEY( sizeof(keys) ), keys ) < 0 )
180 {
181 perror( "EVIOCGKEY" );
182 return false;
183 }
184 else
185 {
186 return BIT_TEST( keys, bit );
187 }
188}
189
190
191QString OInputDevice::globalKeyMask() const
192{
193 BIT_MASK( keys, KEY_MAX );
194
195 if( ioctl( _fd, EVIOCGKEY( sizeof(keys) ), keys ) < 0 )
196 {
197 perror( "EVIOCGKEY" );
198 return QString::null;
199 }
200 else
201 {
202 QString keymask;
203 for ( int i = 0; i < KEY_MAX; ++i )
204 {
205 if ( BIT_TEST( keys, i ) ) keymask.append( QString().sprintf( "%0d, ", i ) );
206 }
207 return keymask;
208
209 }
210}
211
212
213bool OInputDevice::isValid( const QString& path )
214{
215 char buf[BUFSIZE] = "<unknown>";
216 int fd = ::open( (const char*) path, O_RDONLY );
217 if ( fd < 0 ) return false;
218 int res = ::ioctl( fd, EVIOCGNAME(sizeof buf), buf );
219 ::close( fd );
220 return res >= 0;
221}
222