Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 22, 2009, 11:16:34 PM (15 years ago)
Author:
rgrieder
Message:

Updated OIS library (still 1.2, but CVS version).
There have been some little fixes and support for force feedback on Linux (but this doesn't concern us for now…)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/resource2/src/ois/linux/EventHelpers.cpp

    r1505 r5668  
    3434#ifdef OIS_LINUX_JOY_DEBUG
    3535# include <iostream>
    36   using namespace std;
    37 #endif
    38 
     36#endif
     37
     38using namespace std;
    3939using namespace OIS;
    4040
     
    4242{
    4343public:
    44         std::vector<int> buttons, relAxes, absAxes, hats;
     44        vector<int> buttons, relAxes, absAxes, hats;
    4545};
    4646
    47 bool inline isBitSet(unsigned long bits[], unsigned int bit)
    48 {
    49         return (bits[bit/(sizeof(long)*8)] >> ((bit)%(sizeof(long)*8))) & 1;
    50 }
     47bool inline isBitSet(unsigned char bits[], unsigned int bit)
     48{
     49  return (bits[(bit)/(sizeof(unsigned char)*8)] >> ((bit)%(sizeof(unsigned char)*8))) & 1;
     50}
     51
    5152//-----------------------------------------------------------------------------//
    5253DeviceComponentInfo getComponentInfo( int deviceID )
    5354{
    54         unsigned long info[2][((KEY_MAX-1)/(sizeof(long)*8)) +1];
    55         memset( info, 0, sizeof(info) );
     55        unsigned char ev_bits[1 + EV_MAX/8/sizeof(unsigned char)];
     56        memset( ev_bits, 0, sizeof(ev_bits) );
     57
     58        //Read "all" (hence 0) components of the device
     59#ifdef OIS_LINUX_JOY_DEBUG
     60        cout << "EventUtils::getComponentInfo(" << deviceID
     61                 << ") : Reading device events features" << endl;
     62#endif
     63        if (ioctl(deviceID, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1)
     64                OIS_EXCEPT( E_General, "Could not read device events features");
    5665
    5766        DeviceComponentInfo components;
    58 
    59         //Read "all" (hence 0) components of the device - read into first entry
    60         ioctl(deviceID, EVIOCGBIT(0, EV_MAX), info[0]);
    6167
    6268        for (int i = 0; i < EV_MAX; i++)
    6369        {
    64                 if( isBitSet(info[0], i) )
     70                if( isBitSet(ev_bits, i) )
    6571                {
    66                         memset( info[1], 0, sizeof(info) / 2 );
    67                         ioctl(deviceID, EVIOCGBIT(i, KEY_MAX), info[1]);
    68                         for (int j = 0; j < KEY_MAX; j++)
     72                    // Absolute axis.
     73                    if(i == EV_ABS)
    6974                        {
    70                                 if( isBitSet(info[1], j) )
     75                            unsigned char abs_bits[1 + ABS_MAX/8/sizeof(unsigned char)];
     76                            memset( abs_bits, 0, sizeof(abs_bits) );
     77
     78#ifdef OIS_LINUX_JOY_DEBUG
     79                                cout << "EventUtils::getComponentInfo(" << deviceID
     80                                         << ") : Reading device absolute axis features" << endl;
     81#endif
     82
     83                                if (ioctl(deviceID, EVIOCGBIT(i, sizeof(abs_bits)), abs_bits) == -1)
     84                                    OIS_EXCEPT( E_General, "Could not read device absolute axis features");
     85
     86                                for (int j = 0; j < ABS_MAX; j++)
    7187                                {
    72                                         if(i == EV_ABS)
     88                                    if( isBitSet(abs_bits, j) )
    7389                                        {
    7490                                                //input_absinfo abInfo;
     
    89105                                                }
    90106                                        }
    91                                         else if(i == EV_REL)
     107                                }
     108                        }
     109                        else if(i == EV_REL)
     110                        {
     111                            unsigned char rel_bits[1 + REL_MAX/8/sizeof(unsigned char)];
     112                                memset( rel_bits, 0, sizeof(rel_bits) );
     113                               
     114#ifdef OIS_LINUX_JOY_DEBUG
     115                                cout << "EventUtils::getComponentInfo(" << deviceID
     116                                         << ") : Reading device relative axis features" << endl;
     117#endif
     118
     119                                if (ioctl(deviceID, EVIOCGBIT(i, sizeof(rel_bits)), rel_bits) == -1)
     120                                    OIS_EXCEPT( E_General, "Could not read device relative axis features");
     121                               
     122                                for (int j = 0; j < REL_MAX; j++)
     123                                {
     124                                    if( isBitSet(rel_bits, j) )
    92125                                        {
    93                                                 components.relAxes.push_back(j);
     126                                            components.relAxes.push_back(j);
    94127                                        }
    95                                         else if(i == EV_KEY)
     128                                }
     129                        }
     130                        else if(i == EV_KEY)
     131                        {
     132                            unsigned char key_bits[1 + KEY_MAX/8/sizeof(unsigned char)];
     133                                memset( key_bits, 0, sizeof(key_bits) );
     134                               
     135#ifdef OIS_LINUX_JOY_DEBUG
     136                                cout << "EventUtils::getComponentInfo(" << deviceID
     137                                         << ") : Reading device buttons features" << endl;
     138#endif
     139
     140                                if (ioctl(deviceID, EVIOCGBIT(i, sizeof(key_bits)), key_bits) == -1)
     141                                    OIS_EXCEPT( E_General, "Could not read device buttons features");
     142                               
     143                                for (int j = 0; j < KEY_MAX; j++)
     144                                {
     145                                    if( isBitSet(key_bits, j) )
    96146                                        {
    97                                                 components.buttons.push_back(j);
     147                                            components.buttons.push_back(j);
    98148                                        }
    99149                                }
     
    108158bool EventUtils::isJoyStick( int deviceID, JoyStickInfo &js )
    109159{
    110         if( deviceID == -1 ) OIS_EXCEPT( E_General, "Error with File Descriptor" );
     160        if( deviceID == -1 )
     161                OIS_EXCEPT( E_General, "Error with File Descriptor" );
    111162
    112163        DeviceComponentInfo info = getComponentInfo( deviceID );
     
    117168
    118169        #ifdef OIS_LINUX_JOY_DEBUG
    119           cout << "\n\nDisplaying ButtonMapping Status:";
     170        cout << endl << "Displaying ButtonMapping Status:" << endl;
    120171        #endif
    121         for(std::vector<int>::iterator i = info.buttons.begin(), e = info.buttons.end(); i != e; ++i )
     172        for(vector<int>::iterator i = info.buttons.begin(), e = info.buttons.end(); i != e; ++i )
    122173        {
    123174                //Check to ensure we find at least one joy only button
    124                 if( (*i >= BTN_JOYSTICK && *i <= BTN_THUMBR) || (*i >= BTN_WHEEL && *i <= BTN_GEAR_UP ) )
     175                if( (*i >= BTN_JOYSTICK && *i < BTN_GAMEPAD) 
     176                        || (*i >= BTN_GAMEPAD && *i < BTN_DIGI)
     177                        || (*i >= BTN_WHEEL && *i < KEY_OK) )
    125178                        joyButtonFound = true;
    126179
     
    128181
    129182                #ifdef OIS_LINUX_JOY_DEBUG
    130                   cout << "\nButton Mapping ID (hex): " << hex << *i << " OIS Button Num: " << dec << (buttons-1);
     183                  cout << "Button Mapping ID (hex): " << hex << *i
     184                           << " OIS Button Num: " << dec << buttons-1 << endl;
    131185                #endif
    132186        }
     187        #ifdef OIS_LINUX_JOY_DEBUG
     188        cout << endl;
     189        #endif
    133190
    134191        //Joy Buttons found, so it must be a joystick or pad
     
    140197                js.axes = info.relAxes.size() + info.absAxes.size();
    141198                js.hats = info.hats.size();
     199                #ifdef OIS_LINUX_JOY_DEBUG
     200                  cout << endl << "Device name:" << js.vendor << endl;
     201                  cout << "Device unique Id:" << getUniqueId(deviceID) << endl;
     202                  cout << "Device physical location:" << getPhysicalLocation(deviceID) << endl;
     203                #endif
    142204
    143205                //Map the Axes
    144206                #ifdef OIS_LINUX_JOY_DEBUG
    145                   cout << "\n\nDisplaying AxisMapping Status:";
     207                  cout << endl << "Displaying AxisMapping Status:" << endl;
    146208                #endif
    147209                int axes = 0;
    148                 for(std::vector<int>::iterator i = info.absAxes.begin(), e = info.absAxes.end(); i != e; ++i )
     210                for(vector<int>::iterator i = info.absAxes.begin(), e = info.absAxes.end(); i != e; ++i )
    149211                {
    150212                        js.axis_map[*i] = axes;
    151213
     214#ifdef OIS_LINUX_JOY_DEBUG
     215                        cout << "EventUtils::isJoyStick(" << deviceID
     216                                          << ") : Reading device absolute axis #" << *i << " features" << endl;
     217#endif
     218
    152219                        input_absinfo absinfo;
    153                         ioctl(deviceID, EVIOCGABS(*i), &absinfo);
     220                        if (ioctl(deviceID, EVIOCGABS(*i), &absinfo) == -1)
     221                                OIS_EXCEPT( E_General, "Could not read device absolute axis features");
    154222                        js.axis_range[axes] = Range(absinfo.minimum, absinfo.maximum);
    155223
    156224                        #ifdef OIS_LINUX_JOY_DEBUG
    157                           cout << "\nAxis Mapping ID (hex): " << hex << *i << " OIS Axis Num: " << dec << axes;
     225                          cout << "Axis Mapping ID (hex): " << hex << *i
     226                                   << " OIS Axis Num: " << dec << axes << endl;
    158227                        #endif
    159228
     
    166235
    167236//-----------------------------------------------------------------------------//
    168 std::string EventUtils::getName( int deviceID )
    169 {
     237string EventUtils::getName( int deviceID )
     238{
     239#ifdef OIS_LINUX_JOY_DEBUG
     240        cout << "EventUtils::getName(" << deviceID
     241                 << ") : Reading device name" << endl;
     242#endif
     243
    170244        char name[OIS_DEVICE_NAME];
    171         ioctl(deviceID, EVIOCGNAME(OIS_DEVICE_NAME), name);
    172         return std::string(name);
     245        if (ioctl(deviceID, EVIOCGNAME(OIS_DEVICE_NAME), name) == -1)
     246                OIS_EXCEPT( E_General, "Could not read device name");
     247        return string(name);
     248}
     249
     250//-----------------------------------------------------------------------------//
     251string EventUtils::getUniqueId( int deviceID )
     252{
     253#ifdef OIS_LINUX_JOY_DEBUG
     254        cout << "EventUtils::getUniqueId(" << deviceID
     255                 << ") : Reading device unique Id" << endl;
     256#endif
     257
     258#define OIS_DEVICE_UNIQUE_ID 128
     259        char uId[OIS_DEVICE_UNIQUE_ID];
     260        if (ioctl(deviceID, EVIOCGUNIQ(OIS_DEVICE_UNIQUE_ID), uId) == -1)
     261                OIS_EXCEPT( E_General, "Could not read device unique Id");
     262        return string(uId);
     263}
     264
     265//-----------------------------------------------------------------------------//
     266string EventUtils::getPhysicalLocation( int deviceID )
     267{
     268#ifdef OIS_LINUX_JOY_DEBUG
     269        cout << "EventUtils::getPhysicalLocation(" << deviceID
     270                 << ") : Reading device physical location" << endl;
     271#endif
     272
     273#define OIS_DEVICE_PHYSICAL_LOCATION 128
     274        char physLoc[OIS_DEVICE_PHYSICAL_LOCATION];
     275        if (ioctl(deviceID, EVIOCGPHYS(OIS_DEVICE_PHYSICAL_LOCATION), physLoc) == -1)
     276                OIS_EXCEPT( E_General, "Could not read device physical location");
     277        return string(physLoc);
    173278}
    174279
     
    177282{
    178283        //Linux Event to OIS Event Mappings
    179         std::map<int, Effect::EType> typeMap;
     284        map<int, Effect::EType> typeMap;
    180285        typeMap[FF_CONSTANT] = Effect::Constant;
    181286        typeMap[FF_RAMP]     = Effect::Ramp;
     
    191296        typeMap[FF_CUSTOM]   = Effect::Custom;
    192297
    193         std::map<int, Effect::EForce> forceMap;
     298        map<int, Effect::EForce> forceMap;
    194299        forceMap[FF_CONSTANT] = Effect::ConstantForce;
    195         forceMap[FF_RAMP] = Effect::RampForce;
    196         forceMap[FF_PERIODIC] = Effect::PeriodicForce;
    197         forceMap[FF_CUSTOM] = Effect::CustomForce;
     300        forceMap[FF_RAMP]     = Effect::RampForce;
     301        forceMap[FF_SPRING]   = Effect::ConditionalForce;
     302        forceMap[FF_FRICTION] = Effect::ConditionalForce;
     303        forceMap[FF_SQUARE]   = Effect::PeriodicForce;
     304        forceMap[FF_TRIANGLE] = Effect::PeriodicForce;
     305        forceMap[FF_SINE]     = Effect::PeriodicForce;
     306        forceMap[FF_SAW_UP]   = Effect::PeriodicForce;
     307        forceMap[FF_SAW_DOWN] = Effect::PeriodicForce;
     308        forceMap[FF_DAMPER]   = Effect::ConditionalForce;
     309        forceMap[FF_INERTIA]  = Effect::ConditionalForce;
     310        forceMap[FF_CUSTOM]   = Effect::CustomForce;
    198311
    199312        //Remove any previously existing memory and create fresh
    200313        removeForceFeedback( ff );
    201         *ff = new LinuxForceFeedback();
    202 
    203         unsigned long info[4] = {0,0,0,0};
    204         unsigned long subinfo[4]= {0,0,0,0};
    205 
    206         //Read overall force feedback components of the device
    207         ioctl(deviceID, EVIOCGBIT(EV_FF, sizeof(long)*4), info);
     314        *ff = new LinuxForceFeedback(deviceID);
     315
     316        //Read overall force feedback features
     317        unsigned char ff_bits[1 + FF_MAX/8/sizeof(unsigned char)];
     318        memset(ff_bits, 0, sizeof(ff_bits));
     319
     320#ifdef OIS_LINUX_JOY_DEBUG
     321        cout << "EventUtils::enumerateForceFeedback(" << deviceID
     322                 << ") : Reading device force feedback features" << endl;
     323#endif
     324
     325        if (ioctl(deviceID, EVIOCGBIT(EV_FF, sizeof(ff_bits)), ff_bits) == -1)
     326                OIS_EXCEPT( E_General, "Could not read device force feedback features");
     327
     328
     329    #ifdef OIS_LINUX_JOY_DEBUG
     330        cout << "FF bits: " << hex;
     331        for (int i = 0; i < sizeof(ff_bits); i++)
     332                cout << (int)ff_bits[i];
     333        cout << endl << dec;
     334    #endif
    208335
    209336        //FF Axes
    210         //if( isBitSet(info, ABS_X) ) //X Axis
    211         //if( isBitSet(info, ABS_Y) ) //Y Axis
    212         //if( isBitSet(info, ABS_WHEEL) ) //Wheel
     337        //if( isBitSet(ff_bits, ABS_X) ) //X Axis
     338        //if( isBitSet(ff_bits, ABS_Y) ) //Y Axis
     339        //if( isBitSet(ff_bits, ABS_WHEEL) ) //Wheel
    213340
    214341        //FF Effects
    215         for( int effect = ABS_WHEEL+1; effect < FF_MAX; effect++ )
     342        for( int effect = FF_EFFECT_MIN; effect <= FF_WAVEFORM_MAX; effect++ )
    216343        {
    217                 if(isBitSet(info, effect))
     344                // The RUMBLE force type is ignored, as periodic force one is more powerfull.
     345                // The PERIODIC force type is processed later, for each associated periodic effect type.
     346                if (effect == FF_RUMBLE || effect == FF_PERIODIC)
     347                        continue;
     348
     349                if(isBitSet(ff_bits, effect))
    218350                {
    219                         //std::cout << "\tEffect Type: " << effect << std::endl;
    220                         memset(subinfo, 0, sizeof(subinfo));
    221                         //Read any info about this supported effect
    222                         ioctl(deviceID, EVIOCGBIT(effect, sizeof(long)*4), subinfo);
    223                         for( int force = 0; force < FF_MAX; force++ )
    224                         {
    225                                 if(isBitSet(subinfo, force))
    226                                         (*ff)->_addEffectTypes( forceMap[force], typeMap[effect] );
    227                         }
     351                        #ifdef OIS_LINUX_JOY_DEBUG
     352                    cout << "  Effect Type: " << Effect::getEffectTypeName(typeMap[effect]) << endl;
     353                        #endif
     354
     355                        (*ff)->_addEffectTypes( forceMap[effect], typeMap[effect] );
    228356                }
    229357        }
     358
     359        //FF device properties
     360        if (isBitSet(ff_bits, FF_GAIN))
     361                (*ff)->_setGainSupport(true);
     362               
     363        if (isBitSet(ff_bits, FF_AUTOCENTER))
     364                (*ff)->_setAutoCenterSupport(true);
    230365
    231366        //Check to see if any effects were added, else destroy the pointer
Note: See TracChangeset for help on using the changeset viewer.