00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <opie2/odebug.h>
00030 #include <opie2/oinputsystem.h>
00031
00032 using namespace Opie::Core;
00033
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <unistd.h>
00037 #include <sys/ioctl.h>
00038 #include <sys/fcntl.h>
00039
00040 #if 1
00041
00042 int main( int argc, char** argv )
00043 {
00044 OInputSystem* sys = OInputSystem::instance();
00045 OInputSystem::DeviceIterator it = sys->iterator();
00046
00047 OInputDevice* dev = 0;
00048
00049 while ( it.current() )
00050 {
00051 odebug << "DEMO: OInputSystem contains OInputDevice '" << it.current()->name() << "'" << oendl;
00052
00053 dev = it.current();
00054
00055 QString features;
00056 features += QString( "\nSynchronous: " );
00057 if ( dev->hasFeature( OInputDevice::Synchronous ) ) features += "[ x ]"; else features += "[ ]";
00058 features += QString( "\nKeys or Buttons: " );
00059 if ( dev->hasFeature( OInputDevice::Keys ) ) features += "[ x ]"; else features += "[ ]";
00060 features += QString( "\nRelative Axes: " );
00061 if ( dev->hasFeature( OInputDevice::Relative ) ) features += "[ x ]"; else features += "[ ]";
00062 features += QString( "\nAbsolute Axes: " );
00063 if ( dev->hasFeature( OInputDevice::Absolute ) ) features += "[ x ]"; else features += "[ ]";
00064 features += QString( "\nMiscellaneous: " );
00065 if ( dev->hasFeature( OInputDevice::Miscellaneous ) ) features += "[ x ]"; else features += "[ ]";
00066 features += QString( "\nSwitches: " );
00067 if ( dev->hasFeature( OInputDevice::Switches ) ) features += "[ x ]"; else features += "[ ]";
00068 features += QString( "\nLeds: " );
00069 if ( dev->hasFeature( OInputDevice::Leds ) ) features += "[ x ]"; else features += "[ ]";
00070 features += QString( "\nSound: " );
00071 if ( dev->hasFeature( OInputDevice::Sound ) ) features += "[ x ]"; else features += "[ ]";
00072 features += QString( "\nAutoRepeat " );
00073 if ( dev->hasFeature( OInputDevice::AutoRepeat ) ) features += "[ x ]"; else features += "[ ]";
00074 features += QString( "\nForceFeedback: " );
00075 if ( dev->hasFeature( OInputDevice::ForceFeedback ) ) features += "[ x ]"; else features += "[ ]";
00076 features += QString( "\nPowerManagement: " );
00077 if ( dev->hasFeature( OInputDevice::PowerManagement ) ) features += "[ x ]"; else features += "[ ]";
00078 features += QString( "\nFeedbackStatus: " );
00079 if ( dev->hasFeature( OInputDevice::ForceFeedbackStatus ) ) features += "[ x ]"; else features += "[ ]";
00080
00081 odebug << "========================================"
00082 << "\nDevice: " << dev->name()
00083 << "\nName: " << dev->identity()
00084 << "\nPath: " << dev->path()
00085 << "\nUniq: " << dev->uniq()
00086 << features << "\n"
00087 << oendl;
00088
00089 if ( dev->isHeld( OInputDevice::Key_LEFTSHIFT ) )
00090 odebug << "Left Shift is being held." << oendl;
00091 else odebug << "Left Shift is _not_ being held." << oendl;
00092
00093 if ( dev->isHeld( OInputDevice::Button_LEFT ) )
00094 odebug << "Left Mouse Button is being held." << oendl;
00095 else odebug << "Left Mouse Button is _not_ being held." << oendl;
00096
00097 odebug << "Global key mask: " << dev->globalKeyMask() << oendl;
00098 odebug << "Global switch mask: " << dev->globalSwitchMask() << oendl;
00099
00100
00101 ++it;
00102 }
00103 }
00104
00105 #else
00106
00107 #include <fcntl.h>
00108 #include <unistd.h>
00109 #include <cstdlib>
00110 #include <cstdio>
00111 #include <string>
00112
00113 #include <sys/types.h>
00114 #include <linux/input.h>
00115
00116 using std::string;
00117
00118 const unsigned char BUT1 = 0x01;
00119 const unsigned char BUT2 = 0x04;
00120 const unsigned char BUT3 = 0x02;
00121 const unsigned char BUT4 = 0x40;
00122 const unsigned char BUT5 = 0x80;
00123
00124 #define BITMASK( name, numbits ) \
00125 unsigned short name[ ((numbits) - 1) / (sizeof( short ) * 8) + 1 ]; \
00126 memset( name, 0, sizeof( name ) )
00127
00128 #define TEST_BIT( bitmask, bit ) \
00129 ( bitmask[ (bit) / sizeof(short) / 8 ] & (1u << ( (bit) % (sizeof(short) * 8))) )
00130
00131 int Open_cPad()
00132 {
00133 size_t MAX_DEVICES = 1;
00134 int fd = -1;
00135 input_id info;
00136
00137 for( size_t i = 0; i < MAX_DEVICES; ++i )
00138 {
00139 string devpath( "/dev/input/event0" );
00140
00141
00142
00143 fd = open( devpath.c_str(), O_RDONLY, &info );
00144
00145 if( fd >= 0 )
00146 {
00147 int version = -1;
00148
00149 if (ioctl(fd, EVIOCGVERSION, &version)) {
00150 perror("evdev ioctl");
00151 }
00152
00153
00154
00155 printf("evdev driver version is %d.%d.%d\n",
00156 version >> 16, (version >> 8) & 0xff,
00157 version & 0xff);
00158
00159
00160
00161 if( ioctl( fd, EVIOCGID, &info) )
00162 {
00163 perror( "event device ioctl" );
00164 return -1;
00165 }
00166 printf( "Bus: %#x, Vendor: %#x, Product: %#x, Version: %#x\n",
00167 info.bustype, info.vendor, info.product, info.version );
00168
00169 switch ( info.bustype)
00170 {
00171 case BUS_PCI :
00172 printf(" is on a PCI bus\n");
00173 break;
00174 case BUS_USB :
00175 printf(" is on a Universal Serial Bus\n");
00176 break;
00177
00178 }
00179
00180
00181
00182 BITMASK( features, EV_MAX );
00183
00184 if( ioctl( fd, EVIOCGBIT( 0, EV_MAX ), features) < 0 )
00185 {
00186 perror( "event device ioctl" );
00187 return -1;
00188 }
00189
00190 printf( "Supported features:\n" );
00191 for( size_t bit = 0; bit < EV_MAX; ++bit )
00192 {
00193 if( TEST_BIT( features, bit ) )
00194 {
00195 switch( bit )
00196 {
00197 case EV_SYN :
00198 printf(" Sync. Events\n");
00199 break;
00200
00201
00202
00203 case EV_KEY:
00204 printf( " Keys or buttons\n" );
00205 break;
00206 case EV_REL:
00207 printf( " Relative axes\n" );
00208 break;
00209 case EV_ABS:
00210 printf( " Absolute axes\n" );
00211 break;
00212 case EV_MSC:
00213 printf( " Misc\n" );
00214 break;
00215 case EV_LED:
00216 printf( " Led's\n" );
00217 break;
00218 case EV_SND:
00219 printf( " Sounds\n" );
00220 break;
00221 case EV_REP:
00222 printf( " Repeat\n" );
00223 break;
00224 case EV_FF :
00225 case EV_FF_STATUS:
00226 printf(" Force Feedback\n");
00227 break;
00228 case EV_PWR:
00229 printf(" Power Management\n");
00230 break;
00231 default:
00232 printf( " Unknown (bit %d)\n", bit );
00233 }
00234 }
00235 }
00236
00237
00238
00239 BITMASK( keys, KEY_MAX );
00240
00241 if( ioctl( fd, EVIOCGKEY( sizeof(keys) ), keys ) < 0 )
00242 {
00243 perror( "event device ioctl" );
00244 return -1;
00245 }
00246
00247 printf( "Key global status:\n" );
00248 for( size_t bit = 0; bit < KEY_MAX; ++bit )
00249 {
00250 if( TEST_BIT( keys, bit ) )
00251 {
00252 printf( "Key (bit %d) active\n", bit );
00253 }
00254 }
00255 }
00256 else
00257 printf( "Failed to open device\n" );
00258
00259 close( fd );
00260 }
00261 return fd;
00262 }
00263
00264
00265 int main( int argc, char **argv )
00266 {
00267
00268
00269
00270 Open_cPad();
00271
00272 #if 1
00273 int fd = open( "/dev/input/event0", O_RDONLY );
00274
00275 if( fd == -1 )
00276 {
00277 printf( "Failed to open device\n" );
00278 goto hell;
00279 }
00280
00281 struct input_event evbuf;
00282
00283 for(;;)
00284 {
00285 if( read( fd, &evbuf, sizeof evbuf ) == -1 )
00286 {
00287 printf( "Read error\n" );
00288 goto hell;
00289 }
00290
00291 printf( "Type: %d, Code: %d, Value: %d\n", evbuf.type, evbuf.code, evbuf.value );
00292 }
00293
00294 #endif
00295 #if 0
00296 unsigned char buf[ 4 ];
00297
00298 for(;;)
00299 {
00300 if( read( fd, buf, sizeof buf ) == -1 )
00301 {
00302 printf( "Read error\n" );
00303 goto hell;
00304 }
00305
00306 printf( "Raw:\t%#x\t%#x\t%#x\t%#x\n", buf[ 0 ], buf[ 1 ], buf[ 2 ], buf[ 3 ] );
00307
00308 int dx = buf[ 1 ];
00309
00310 if( ( buf[ 0 ] & 0x10 ) != 0 )
00311 dx -= 256;
00312
00313 int dy = - buf[ 2 ];
00314
00315 if( ( buf[ 0 ] & 0x20 ) != 0 )
00316 dy += 256;
00317
00318 printf( "( %d, %d )\t", dx, dy );
00319
00320 if( buf[ 0 ] & BUT1 )
00321 printf( "Left\t" );
00322 if( buf[ 0 ] & BUT2 )
00323 printf( "Middle\t" );
00324 if( buf[ 0 ] & BUT3 )
00325 printf( "Right\t" );
00326
00327 printf( "\n" );
00328 }
00329 #endif
00330
00331
00332
00333 exit( EXIT_SUCCESS );
00334 hell:
00335 exit( EXIT_FAILURE );
00336 }
00337
00338 #endif