author | mickeyl <mickeyl> | 2005-02-04 17:45:50 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2005-02-04 17:45:50 (UTC) |
commit | c0248a2c02381b5a5dce5c4543db6ff46486f2f1 (patch) (unidiff) | |
tree | d0aa40497fe90875a273d2e0dbbfc65608f23b43 | |
parent | 276022cd2a84f531bbf727c34d76420d8195512d (diff) | |
download | opie-c0248a2c02381b5a5dce5c4543db6ff46486f2f1.zip opie-c0248a2c02381b5a5dce5c4543db6ff46486f2f1.tar.gz opie-c0248a2c02381b5a5dce5c4543db6ff46486f2f1.tar.bz2 |
use a sane method to discover valid device nodes
-rw-r--r-- | libopie2/opiecore/oinputsystem.cpp | 38 | ||||
-rw-r--r-- | libopie2/opiecore/oinputsystem.h | 19 |
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 | |||
@@ -1,197 +1,221 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of the Opie Project | 2 | This file is part of the Opie Project |
3 | =. (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de> | 3 | =. (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de> |
4 | .=l. | 4 | .=l. |
5 | .>+-= | 5 | .>+-= |
6 | _;:, .> :=|. This program is free software; you can | 6 | _;:, .> :=|. This program is free software; you can |
7 | .> <`_, > . <= redistribute it and/or modify it under | 7 | .> <`_, > . <= redistribute it and/or modify it under |
8 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | 8 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public |
9 | .="- .-=="i, .._ License as published by the Free Software | 9 | .="- .-=="i, .._ License as published by the Free Software |
10 | - . .-<_> .<> Foundation; either version 2 of the License, | 10 | - . .-<_> .<> Foundation; either version 2 of the License, |
11 | ._= =} : or (at your option) any later version. | 11 | ._= =} : or (at your option) any later version. |
12 | .%`+i> _;_. | 12 | .%`+i> _;_. |
13 | .i_,=:_. -<s. This program is distributed in the hope that | 13 | .i_,=:_. -<s. This program is distributed in the hope that |
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
15 | : .. .:, . . . without even the implied warranty of | 15 | : .. .:, . . . without even the implied warranty of |
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
18 | ..}^=.= = ; Library General Public License for more | 18 | ..}^=.= = ; Library General Public License for more |
19 | ++= -. .` .: details. | 19 | ++= -. .` .: details. |
20 | : = ...= . :.=- | 20 | : = ...= . :.=- |
21 | -. .:....=;==+<; You should have received a copy of the GNU | 21 | -. .:....=;==+<; You should have received a copy of the GNU |
22 | -_. . . )=. = Library General Public License along with | 22 | -_. . . )=. = Library General Public License along with |
23 | -- :-=` this library; see the file COPYING.LIB. | 23 | -- :-=` this library; see the file COPYING.LIB. |
24 | If not, write to the Free Software Foundation, | 24 | If not, write to the Free Software Foundation, |
25 | Inc., 59 Temple Place - Suite 330, | 25 | Inc., 59 Temple Place - Suite 330, |
26 | Boston, MA 02111-1307, USA. | 26 | Boston, MA 02111-1307, USA. |
27 | */ | 27 | */ |
28 | 28 | ||
29 | #include "oinputsystem.h" | 29 | #include "oinputsystem.h" |
30 | using namespace Opie::Core; | 30 | using 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 ) \ |
46 | ( bitmask[ (bit) / sizeof(short) / 8 ] & (1u << ( (bit) % (sizeof(short) * 8))) ) | 48 | ( bitmask[ (bit) / sizeof(short) / 8 ] & (1u << ( (bit) % (sizeof(short) * 8))) ) |
47 | 49 | ||
48 | /*====================================================================================== | 50 | /*====================================================================================== |
49 | * OInputSystem | 51 | * OInputSystem |
50 | *======================================================================================*/ | 52 | *======================================================================================*/ |
51 | 53 | ||
52 | OInputSystem* OInputSystem::_instance = 0; | 54 | OInputSystem* OInputSystem::_instance = 0; |
53 | 55 | ||
54 | OInputSystem::OInputSystem() : QObject() | 56 | OInputSystem::OInputSystem() : QObject() |
55 | { | 57 | { |
56 | qDebug( "OInputSystem::OInputSystem()" ); | 58 | qDebug( "OInputSystem::OInputSystem()" ); |
57 | synchronize(); | 59 | synchronize(); |
58 | } | 60 | } |
59 | 61 | ||
60 | 62 | ||
61 | void OInputSystem::synchronize() | 63 | void 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 | ||
72 | OInputSystem::~OInputSystem() | 85 | OInputSystem::~OInputSystem() |
73 | { | 86 | { |
74 | qDebug( "OInputSystem::~OInputSystem()" ); | 87 | qDebug( "OInputSystem::~OInputSystem()" ); |
75 | } | 88 | } |
76 | 89 | ||
77 | 90 | ||
78 | int OInputSystem::count() const | 91 | int OInputSystem::count() const |
79 | { | 92 | { |
80 | return _devices.count(); | 93 | return _devices.count(); |
81 | } | 94 | } |
82 | 95 | ||
83 | 96 | ||
84 | OInputDevice* OInputSystem::device( const QString& device ) const | 97 | OInputDevice* OInputSystem::device( const QString& device ) const |
85 | { | 98 | { |
86 | return _devices[device]; | 99 | return _devices[device]; |
87 | } | 100 | } |
88 | 101 | ||
89 | 102 | ||
90 | OInputSystem* OInputSystem::instance() | 103 | OInputSystem* OInputSystem::instance() |
91 | { | 104 | { |
92 | if ( !_instance ) _instance = new OInputSystem(); | 105 | if ( !_instance ) _instance = new OInputSystem(); |
93 | return _instance; | 106 | return _instance; |
94 | } | 107 | } |
95 | 108 | ||
96 | 109 | ||
97 | OInputSystem::DeviceIterator OInputSystem::iterator() const | 110 | OInputSystem::DeviceIterator OInputSystem::iterator() const |
98 | { | 111 | { |
99 | return OInputSystem::DeviceIterator( _devices ); | 112 | return OInputSystem::DeviceIterator( _devices ); |
100 | } | 113 | } |
101 | 114 | ||
102 | /*====================================================================================== | 115 | /*====================================================================================== |
103 | * OInputDevice | 116 | * OInputDevice |
104 | *======================================================================================*/ | 117 | *======================================================================================*/ |
105 | 118 | ||
106 | OInputDevice::OInputDevice( QObject* parent, const char* name ) : QObject( parent, name ) | 119 | OInputDevice::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 | } |
116 | 129 | ||
117 | 130 | ||
118 | OInputDevice::~OInputDevice() | 131 | OInputDevice::~OInputDevice() |
119 | { | 132 | { |
120 | qDebug( "OInputDevice::~OInputDevice()" ); | 133 | qDebug( "OInputDevice::~OInputDevice()" ); |
121 | } | 134 | } |
122 | 135 | ||
123 | 136 | ||
124 | QString OInputDevice::identity() const | 137 | QString OInputDevice::identity() const |
125 | { | 138 | { |
126 | char buf[BUFSIZE] = "<unknown>"; | 139 | char buf[BUFSIZE] = "<unknown>"; |
127 | ::ioctl( _fd, EVIOCGNAME(sizeof buf), buf ); | 140 | ::ioctl( _fd, EVIOCGNAME(sizeof buf), buf ); |
128 | return buf; | 141 | return buf; |
129 | } | 142 | } |
130 | 143 | ||
131 | 144 | ||
132 | QString OInputDevice::path() const | 145 | QString OInputDevice::path() const |
133 | { | 146 | { |
134 | char buf[BUFSIZE] = "<unknown>"; | 147 | char buf[BUFSIZE] = "<unknown>"; |
135 | ::ioctl( _fd, EVIOCGPHYS(sizeof buf), buf ); | 148 | ::ioctl( _fd, EVIOCGPHYS(sizeof buf), buf ); |
136 | return buf; | 149 | return buf; |
137 | } | 150 | } |
138 | 151 | ||
139 | 152 | ||
140 | QString OInputDevice::uniq() const | 153 | QString OInputDevice::uniq() const |
141 | { | 154 | { |
142 | char buf[BUFSIZE] = "<unknown>"; | 155 | char buf[BUFSIZE] = "<unknown>"; |
143 | ::ioctl( _fd, EVIOCGUNIQ(sizeof buf), buf ); | 156 | ::ioctl( _fd, EVIOCGUNIQ(sizeof buf), buf ); |
144 | return buf; | 157 | return buf; |
145 | } | 158 | } |
146 | 159 | ||
147 | 160 | ||
148 | bool OInputDevice::hasFeature( Feature bit ) const | 161 | bool 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 |
158 | return BIT_TEST( features, bit ); | 171 | return BIT_TEST( features, bit ); |
159 | } | 172 | } |
160 | 173 | ||
161 | 174 | ||
162 | bool OInputDevice::isHeld( Key bit ) const | 175 | bool OInputDevice::isHeld( Key bit ) const |
163 | { | 176 | { |
164 | BIT_MASK( keys, KEY_MAX ); | 177 | BIT_MASK( keys, KEY_MAX ); |
165 | 178 | ||
166 | if( ioctl( _fd, EVIOCGKEY( sizeof(keys) ), keys ) < 0 ) | 179 | if( ioctl( _fd, EVIOCGKEY( sizeof(keys) ), keys ) < 0 ) |
167 | { | 180 | { |
168 | perror( "EVIOCGKEY" ); | 181 | perror( "EVIOCGKEY" ); |
169 | return false; | 182 | return false; |
170 | } | 183 | } |
171 | else | 184 | else |
172 | { | 185 | { |
173 | return BIT_TEST( keys, bit ); | 186 | return BIT_TEST( keys, bit ); |
174 | } | 187 | } |
175 | } | 188 | } |
176 | 189 | ||
177 | 190 | ||
178 | QString OInputDevice::globalKeyMask() const | 191 | QString OInputDevice::globalKeyMask() const |
179 | { | 192 | { |
180 | BIT_MASK( keys, KEY_MAX ); | 193 | BIT_MASK( keys, KEY_MAX ); |
181 | 194 | ||
182 | if( ioctl( _fd, EVIOCGKEY( sizeof(keys) ), keys ) < 0 ) | 195 | if( ioctl( _fd, EVIOCGKEY( sizeof(keys) ), keys ) < 0 ) |
183 | { | 196 | { |
184 | perror( "EVIOCGKEY" ); | 197 | perror( "EVIOCGKEY" ); |
185 | } | 198 | } |
186 | else | 199 | else |
187 | { | 200 | { |
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 | |||
212 | bool 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 | |||
@@ -32,106 +32,111 @@ | |||
32 | #include "linux_input.h" | 32 | #include "linux_input.h" |
33 | 33 | ||
34 | /* QT */ | 34 | /* QT */ |
35 | #include <qobject.h> | 35 | #include <qobject.h> |
36 | #include <qdict.h> | 36 | #include <qdict.h> |
37 | 37 | ||
38 | namespace Opie { | 38 | namespace Opie { |
39 | namespace Core { | 39 | namespace Core { |
40 | 40 | ||
41 | class OInputDevice; | 41 | class OInputDevice; |
42 | 42 | ||
43 | /** | 43 | /** |
44 | * @brief A container class for all input devices | 44 | * @brief A container class for all input devices |
45 | * | 45 | * |
46 | * This class provides access to all available input system devices of your computer. | 46 | * This class provides access to all available input system devices of your computer. |
47 | * | 47 | * |
48 | * @author Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> | 48 | * @author Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> |
49 | */ | 49 | */ |
50 | class OInputSystem : public QObject | 50 | class OInputSystem : public QObject |
51 | { | 51 | { |
52 | public: | 52 | public: |
53 | typedef QDict<OInputDevice> DeviceMap; | 53 | typedef QDict<OInputDevice> DeviceMap; |
54 | typedef QDictIterator<OInputDevice> DeviceIterator; | 54 | typedef QDictIterator<OInputDevice> DeviceIterator; |
55 | 55 | ||
56 | /** | 56 | /** |
57 | * @returns the number of available input devices | 57 | * @returns the number of available input devices |
58 | */ | 58 | */ |
59 | int count() const; | 59 | int count() const; |
60 | /** | 60 | /** |
61 | * @returns a pointer to the (one and only) @ref OInputSystem instance. | 61 | * @returns a pointer to the (one and only) @ref OInputSystem instance. |
62 | */ | 62 | */ |
63 | static OInputSystem* instance(); | 63 | static OInputSystem* instance(); |
64 | /** | 64 | /** |
65 | * @returns an iterator usable for iterating through all network interfaces. | 65 | * @returns an iterator usable for iterating through all network interfaces. |
66 | */ | 66 | */ |
67 | DeviceIterator iterator() const; | 67 | DeviceIterator iterator() const; |
68 | /** | 68 | /** |
69 | * @returns a pointer to the @ref OAudioInterface object for the specified @a interface or 0, if not found | 69 | * @returns a pointer to the @ref OAudioInterface object for the specified @a interface or 0, if not found |
70 | * @see OAudioInterface | 70 | * @see OAudioInterface |
71 | */ | 71 | */ |
72 | OInputDevice* device( const QString& interface ) const; | 72 | OInputDevice* device( const QString& interface ) const; |
73 | /** | 73 | /** |
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 | ||
91 | class OInputDevice : public QObject | 91 | class 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 | /** |
105 | * @returns the path of this input device | 105 | * @returns the path of this input device |
106 | */ | 106 | */ |
107 | QString path() const; | 107 | QString path() const; |
108 | /** | 108 | /** |
109 | * @returns a unique identifier for this input device | 109 | * @returns a unique identifier for this input device |
110 | * @note Only a few devices support this | 110 | * @note Only a few devices support this |
111 | */ | 111 | */ |
112 | QString uniq() const; | 112 | QString uniq() const; |
113 | /** | 113 | /** |
114 | * @returns whether a certain @a Feature is being supported by this device | 114 | * @returns whether a certain @a Feature is being supported by this device |
115 | */ | 115 | */ |
116 | bool hasFeature( Feature ) const; | 116 | bool hasFeature( Feature ) const; |
117 | /** | 117 | /** |
118 | * @returns whether a given @a Key or Button is being held at the moment | 118 | * @returns whether a given @a Key or Button is being held at the moment |
119 | */ | 119 | */ |
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 | ||
133 | } | 138 | } |
134 | } | 139 | } |
135 | 140 | ||
136 | #endif // OINPUTSYSTEM_H | 141 | #endif // OINPUTSYSTEM_H |
137 | 142 | ||