summaryrefslogtreecommitdiff
authormickeyl <mickeyl>2005-02-04 17:45:50 (UTC)
committer mickeyl <mickeyl>2005-02-04 17:45:50 (UTC)
commitc0248a2c02381b5a5dce5c4543db6ff46486f2f1 (patch) (unidiff)
treed0aa40497fe90875a273d2e0dbbfc65608f23b43
parent276022cd2a84f531bbf727c34d76420d8195512d (diff)
downloadopie-c0248a2c02381b5a5dce5c4543db6ff46486f2f1.zip
opie-c0248a2c02381b5a5dce5c4543db6ff46486f2f1.tar.gz
opie-c0248a2c02381b5a5dce5c4543db6ff46486f2f1.tar.bz2
use a sane method to discover valid device nodes
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiecore/oinputsystem.cpp38
-rw-r--r--libopie2/opiecore/oinputsystem.h19
2 files changed, 43 insertions, 14 deletions
diff --git a/libopie2/opiecore/oinputsystem.cpp b/libopie2/opiecore/oinputsystem.cpp
index d1a28f5..a2306ca 100644
--- a/libopie2/opiecore/oinputsystem.cpp
+++ b/libopie2/opiecore/oinputsystem.cpp
@@ -27,19 +27,21 @@
27*/ 27*/
28 28
29#include "oinputsystem.h" 29#include "oinputsystem.h"
30using namespace Opie::Core; 30using namespace Opie::Core;
31 31
32/* QT */ 32/* QT */
33#include <qdir.h>
33#include <qfile.h> 34#include <qfile.h>
34 35
35/* STD */ 36/* STD */
36#include <errno.h> 37#include <errno.h>
37#include <string.h> 38#include <string.h>
38#include <sys/fcntl.h> 39#include <sys/fcntl.h>
39#include <sys/ioctl.h> 40#include <sys/ioctl.h>
41#include <unistd.h>
40 42
41#define BUFSIZE 256 43#define BUFSIZE 256
42#define BIT_MASK( name, numbits ) \ 44#define BIT_MASK( name, numbits ) \
43 unsigned short name[ ((numbits) - 1) / (sizeof( short ) * 8) + 1 ]; \ 45 unsigned short name[ ((numbits) - 1) / (sizeof( short ) * 8) + 1 ]; \
44 memset( name, 0, sizeof( name ) ) 46 memset( name, 0, sizeof( name ) )
45#define BIT_TEST( bitmask, bit ) \ 47#define BIT_TEST( bitmask, bit ) \
@@ -58,17 +60,28 @@ OInputSystem::OInputSystem() : QObject()
58} 60}
59 61
60 62
61void OInputSystem::synchronize() 63void OInputSystem::synchronize()
62{ 64{
63 qDebug( "OInputSystem::synchronize()" ); 65 qDebug( "OInputSystem::synchronize()" );
64 if ( QFile::exists( "/dev/input/event0" ) ) _devices.insert( "0", new OInputDevice( this, "/dev/input/event0" ) ); 66 QDir devInput( "/dev/input/" );
65 if ( QFile::exists( "/dev/input/event1" ) ) _devices.insert( "1", new OInputDevice( this, "/dev/input/event1" ) ); 67 if ( devInput.exists() )
66 if ( QFile::exists( "/dev/input/event2" ) ) _devices.insert( "2", new OInputDevice( this, "/dev/input/event2" ) ); 68 {
67 if ( QFile::exists( "/dev/input/event3" ) ) _devices.insert( "3", new OInputDevice( this, "/dev/input/event3" ) ); 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'",
75 (const char*) absPath, isValid ? "yes" : "no" );
76 if ( isValid ) _devices.insert( *it, new OInputDevice( this, absPath ) );
77 }
78 }
68 qDebug( "OInputSystem::synchronize() done" ); 79 qDebug( "OInputSystem::synchronize() done" );
80 if ( !_devices.count() )
81 qWarning( "OInputSystem::no devices found" );
69} 82}
70 83
71 84
72OInputSystem::~OInputSystem() 85OInputSystem::~OInputSystem()
73{ 86{
74 qDebug( "OInputSystem::~OInputSystem()" ); 87 qDebug( "OInputSystem::~OInputSystem()" );
@@ -103,13 +116,13 @@ OInputSystem::DeviceIterator OInputSystem::iterator() const
103 * OInputDevice 116 * OInputDevice
104 *======================================================================================*/ 117 *======================================================================================*/
105 118
106OInputDevice::OInputDevice( QObject* parent, const char* name ) : QObject( parent, name ) 119OInputDevice::OInputDevice( QObject* parent, const char* name ) : QObject( parent, name )
107{ 120{
108 qDebug( "OInputDevice::OInputDevice( '%s' )", name ); 121 qDebug( "OInputDevice::OInputDevice( '%s' )", name );
109 122
110 _fd = ::open( name, O_RDONLY ); 123 _fd = ::open( name, O_RDONLY );
111 if ( _fd == -1 ) 124 if ( _fd == -1 )
112 { 125 {
113 qDebug( "OInputDevice::OInputDevice() - Warning: couldn't open %s (%s)", name, strerror( errno ) ); 126 qDebug( "OInputDevice::OInputDevice() - Warning: couldn't open %s (%s)", name, strerror( errno ) );
114 } 127 }
115} 128}
@@ -145,13 +158,13 @@ QString OInputDevice::uniq() const
145} 158}
146 159
147 160
148bool OInputDevice::hasFeature( Feature bit ) const 161bool OInputDevice::hasFeature( Feature bit ) const
149{ 162{
150 BIT_MASK( features, EV_MAX ); 163 BIT_MASK( features, EV_MAX );
151 164
152 if( ioctl( _fd, EVIOCGBIT( 0, EV_MAX ), features) < 0 ) 165 if( ioctl( _fd, EVIOCGBIT( 0, EV_MAX ), features) < 0 )
153 { 166 {
154 perror( "EVIOCGBIT" ); 167 perror( "EVIOCGBIT" );
155 return false; 168 return false;
156 } 169 }
157 else 170 else
@@ -188,10 +201,21 @@ QString OInputDevice::globalKeyMask() const
188 QString keymask; 201 QString keymask;
189 for ( int i = 0; i < KEY_MAX; ++i ) 202 for ( int i = 0; i < KEY_MAX; ++i )
190 { 203 {
191 if ( BIT_TEST( keys, i ) ) keymask.append( QString().sprintf( "%0d, ", i ) ); 204 if ( BIT_TEST( keys, i ) ) keymask.append( QString().sprintf( "%0d, ", i ) );
192 } 205 }
193 return keymask; 206 return keymask;
194 207
195 } 208 }
196} 209}
197 210
211
212bool OInputDevice::isValid( const QString& path )
213{
214 char buf[BUFSIZE] = "<unknown>";
215 int fd = ::open( (const char*) path, O_RDONLY );
216 if ( fd < 0 ) return false;
217 int res = ::ioctl( fd, EVIOCGNAME(sizeof buf), buf );
218 ::close( fd );
219 return res >= 0;
220}
221
diff --git a/libopie2/opiecore/oinputsystem.h b/libopie2/opiecore/oinputsystem.h
index 7919610..6f822a1 100644
--- a/libopie2/opiecore/oinputsystem.h
+++ b/libopie2/opiecore/oinputsystem.h
@@ -74,31 +74,31 @@ class OInputSystem : public QObject
74 * @internal Rebuild the internal interface database 74 * @internal Rebuild the internal interface database
75 * @note Sometimes it might be useful to call this from client code, 75 * @note Sometimes it might be useful to call this from client code,
76 */ 76 */
77 void synchronize(); 77 void synchronize();
78 /** 78 /**
79 * @internal destructor 79 * @internal destructor
80 */ 80 */
81 ~OInputSystem(); 81 ~OInputSystem();
82 82
83 protected: 83 protected:
84 OInputSystem(); 84 OInputSystem();
85 85
86 static OInputSystem* _instance; 86 static OInputSystem* _instance;
87 DeviceMap _devices; 87 DeviceMap _devices;
88}; 88};
89 89
90 90
91class OInputDevice : public QObject 91class OInputDevice : public QObject
92{ 92{
93 public: 93 public:
94 OInputDevice( QObject* parent, const char* name = 0 ); 94 OInputDevice( QObject* parent, const char* name = 0 );
95 ~OInputDevice(); 95 ~OInputDevice();
96 96
97 #include "oinputsystemenums.h" 97 #include "oinputsystemenums.h"
98 98
99 public: 99 public:
100 /** 100 /**
101 * @returns the identity string of this input device 101 * @returns the identity string of this input device
102 */ 102 */
103 QString identity() const; 103 QString identity() const;
104 /** 104 /**
@@ -120,13 +120,18 @@ class OInputDevice : public QObject
120 bool isHeld( Key ) const; 120 bool isHeld( Key ) const;
121 /** 121 /**
122 * @internal 122 * @internal
123 * @returns a string containing a printable form of the global keymask 123 * @returns a string containing a printable form of the global keymask
124 */ 124 */
125 QString globalKeyMask() const; 125 QString globalKeyMask() const;
126 126 /**
127 * @internal
128 * @returns whether a certain @a path corresponds to an input device
129 */
130 static bool isValid( const QString& path );
131
127 private: 132 private:
128 int _fd; 133 int _fd;
129 input_id _id; 134 input_id _id;
130 135
131}; 136};
132 137