00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "inputthread.h"
00022 #include "absval.h"
00023 #include "inputthreadprivate.h"
00024
00025 #include <core/debughelper.h>
00026
00027 #include <QtCore/QCoreApplication>
00028 #include <QtCore/QDebug>
00029 #include <QtCore/QFile>
00030 #include <QtCore/QEvent>
00031 #include <QtCore/QStringList>
00032
00033 #include <fcntl.h>
00034 #include <sys/stat.h>
00035 #include <sys/types.h>
00036 #include <linux/input.h>
00037
00038 #define BITS_PER_LONG (sizeof(long) * 8)
00039 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
00040 #define OFF(x) ((x)%BITS_PER_LONG)
00041 #define BIT(x) (1UL<<OFF(x))
00042 #define LONG(x) ((x)/BITS_PER_LONG)
00043 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
00044
00045 using namespace GluonInput;
00046
00047 InputThread::InputThread( const QString& devicePath, QObject* parent )
00048 : QThread( parent )
00049 , d( new InputThreadPrivate )
00050 {
00051 d->m_devicePath = devicePath;
00052 if( openDevice( devicePath ) )
00053 readInformation();
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 InputThread::~InputThread()
00065 {
00066 closeDevice();
00067 }
00068
00069 void InputThread::run()
00070 {
00071 while( 1 )
00072 {
00073 struct input_event ev;
00074 int rd = read( d->m_fd, &ev, sizeof( struct input_event ) );
00075 if( rd >= ( int ) sizeof( struct input_event ) )
00076 {
00077 QEvent::Type eventType = QEvent::Type( QEvent::User + ev.type );
00078
00079 switch( eventType )
00080 {
00081 case GluonInput::Button:
00082 emit this->buttonStateChanged( ev.code, ev.value );
00083 break;
00084 case GluonInput::RelativeAxis:
00085 emit this->relAxisMoved( ev.code, ev.value );
00086 break;
00087 case GluonInput::AbsoluteAxis:
00088 emit this->absAxisMoved( ev.code, ev.value );
00089 break;
00090 default:
00091 break;
00092 }
00093 }
00094 }
00095 }
00096
00097 bool InputThread::openDevice( const QString& devicePath )
00098 {
00099 if(( d->m_fd = open( devicePath.toUtf8(), O_RDONLY ) ) == -1 )
00100 {
00101 qDebug() << "Could not open device: " << devicePath;
00102 d->m_error = true;
00103 d->m_msgError += "Could not open the device\n";
00104 return false;
00105 }
00106
00107 return true;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 void InputThread::readInformation()
00233 {
00234 if( d->m_fd == -1 )
00235 {
00236 qDebug() << "Device is not open";
00237 d->m_error = true;
00238 d->m_msgError = "Device is not open\n";
00239 return;
00240 }
00241
00242
00243 if( ioctl( d->m_fd, EVIOCGID, &d->m_device_info ) )
00244 {
00245 qDebug() << "Could not retrieve information of device" << d->m_devicePath;
00246 d->m_msgError = "Could not retrieve information of device\n";
00247 d->m_error = true;
00248 return;
00249 }
00250
00251 char devname[256] = "Unknown";
00252
00253 if( ioctl( d->m_fd, EVIOCGNAME( sizeof( devname ) ), devname ) == -1 )
00254 {
00255 qDebug() << "Could not retrieve name of device" << d->m_devicePath;
00256 d->m_msgError = "Could not retrieve name of device\n";
00257 d->m_error = true;
00258 return;
00259 }
00260 d->m_deviceName = QString( devname );
00261
00263 unsigned long bit[EV_MAX][NBITS( KEY_MAX )];
00264 int abs[5];
00265 memset( bit, 0, sizeof( bit ) );
00266
00267 ioctl( d->m_fd, EVIOCGBIT( 0, EV_MAX ), bit[0] );
00268
00269 d->m_buttonCapabilities.clear();
00270 d->m_absAxisInfos.clear();
00271
00272 for( int i = 0; i < EV_MAX; ++i )
00273 {
00274 if( test_bit( i, bit[0] ) )
00275 {
00276 if( !i )
00277 {
00278 continue;
00279 }
00280
00281
00282 ioctl( d->m_fd, EVIOCGBIT( i, KEY_MAX ), bit[i] );
00283 switch( i )
00284 {
00285 case EV_KEY:
00286 {
00287 for( int j = 0; j < KEY_MAX; ++j )
00288 {
00289 if( test_bit( j, bit[i] ) )
00290 d->m_buttonCapabilities.append( j );
00291 }
00292 }
00293 break;
00294 case EV_REL:
00295 {
00296 for( int j = 0; j < KEY_MAX; ++j )
00297 {
00298 if( test_bit( j, bit[i] ) )
00299 d->m_relAxisCapabilities.append( j );
00300 }
00301 }
00302 break;
00303 case EV_ABS:
00304 {
00305 for( int j = 0; j < ABS_MAX; ++j )
00306 {
00307 if( test_bit( j, bit[i] ) )
00308 {
00309
00310 ioctl( d->m_fd, EVIOCGABS( j ), abs );
00311 AbsVal cabs( 0, 0, 0, 0 );
00312 for( int k = 0; k < 5; ++k )
00313 {
00314 if(( k < 3 ) || abs[k] )
00315 {
00316 switch( k )
00317 {
00318 case 0:
00319 cabs.value = abs[k];
00320 break;
00321 case 1:
00322 cabs.min = abs[k];
00323 break;
00324 case 2:
00325 cabs.max = abs[k];
00326 break;
00327 case 3:
00328 cabs.fuzz = abs[k];
00329 break;
00330 case 4:
00331 cabs.flat = abs[k];
00332 break;
00333 }
00334 }
00335 }
00336 d->m_absAxisCapabilities.append( j );
00337 d->m_absAxisInfos.append( cabs );
00338 }
00339 }
00340 }
00341 break;
00342 default:
00343 break;
00344 }
00345 }
00346 }
00347
00348
00349
00350 d->m_deviceType = GluonInput::UnknownDevice;
00351
00352 if( d->m_buttonCapabilities.contains( BTN_TOUCH ) )
00353 {
00354 d->m_deviceType = GluonInput::TouchDevice;
00355 }
00356
00357 if( d->m_buttonCapabilities.contains( BTN_STYLUS )
00358 || d->m_buttonCapabilities.contains( ABS_PRESSURE ) )
00359 {
00360 d->m_deviceType = GluonInput::MouseDevice;
00361 }
00362
00363 if( d->m_buttonCapabilities.contains( BTN_TRIGGER ) )
00364 {
00365 d->m_deviceType = GluonInput::JoystickDevice;
00366 }
00367
00368 if( d->m_buttonCapabilities.contains( BTN_MOUSE ) )
00369 {
00370 d->m_deviceType = GluonInput::MouseDevice;
00371 }
00372
00373 if( d->m_buttonCapabilities.contains( KEY_ENTER ) )
00374 {
00375 d->m_deviceType = GluonInput::KeyboardDevice;
00376 }
00377 }
00378
00379 int InputThread::joystickXAxis()
00380 {
00381 return ABS_X;
00382 }
00383
00384 int InputThread::joystickYAxis()
00385 {
00386 return ABS_Y;
00387 }
00388
00389 int InputThread::joystickZAxis()
00390 {
00391 return ABS_Z;
00392 }
00393
00394 void InputThread::stop()
00395 {
00396 quit();
00397 }
00398
00399 struct input_id InputThread::device_info() const
00400 {
00401 return d->m_device_info;
00402 }
00403
00404 int InputThread::vendor() const
00405 {
00406 return d->m_device_info.vendor;
00407 }
00408
00409 int InputThread::product() const
00410 {
00411 return d->m_device_info.product;
00412 }
00413
00414 int InputThread::version() const
00415 {
00416 return d->m_device_info.version;
00417 }
00418
00419 int InputThread::bustype()const
00420 {
00421 return d->m_device_info.bustype;
00422 }
00423
00424 QList<int> InputThread::buttonCapabilities() const
00425 {
00426 return d->m_buttonCapabilities;
00427 }
00428
00429 QList<int> InputThread::absAxisCapabilities() const
00430 {
00431 return d->m_absAxisCapabilities;
00432 }
00433
00434 QList<int> InputThread::relAxisCapabilities() const
00435 {
00436 return d->m_relAxisCapabilities;
00437 }
00438
00439 AbsVal InputThread::axisInfo( int axisCode ) const
00440 {
00441 return d->m_absAxisInfos[axisCode];
00442 }
00443
00444 const QString InputThread::deviceName() const
00445 {
00446 return d->m_deviceName;
00447 }
00448
00449 GluonInput::DeviceFlag InputThread::deviceType() const
00450 {
00451 return d->m_deviceType;
00452 }
00453
00454 bool InputThread::isEnabled() const
00455 {
00456 return isRunning();
00457 }
00458
00459 bool InputThread::error()
00460 {
00461 return d->m_error;
00462 }
00463
00464 QString InputThread::msgError()
00465 {
00466 return d->m_msgError;
00467 }
00468
00469 void InputThread::closeDevice()
00470 {
00471 if( close( d->m_fd ) )
00472 qDebug() << "Could not close the device: " << d->m_devicePath << endl;
00473 }
00474
00475 #include "inputthread.moc"