Changeset 5668 for code/branches/resource2/src/ois/linux/EventHelpers.cpp
- Timestamp:
- Aug 22, 2009, 11:16:34 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/resource2/src/ois/linux/EventHelpers.cpp
r1505 r5668 34 34 #ifdef OIS_LINUX_JOY_DEBUG 35 35 # include <iostream> 36 using namespace std; 37 #endif 38 36 #endif 37 38 using namespace std; 39 39 using namespace OIS; 40 40 … … 42 42 { 43 43 public: 44 std::vector<int> buttons, relAxes, absAxes, hats;44 vector<int> buttons, relAxes, absAxes, hats; 45 45 }; 46 46 47 bool inline isBitSet(unsigned long bits[], unsigned int bit) 48 { 49 return (bits[bit/(sizeof(long)*8)] >> ((bit)%(sizeof(long)*8))) & 1; 50 } 47 bool 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 51 52 //-----------------------------------------------------------------------------// 52 53 DeviceComponentInfo getComponentInfo( int deviceID ) 53 54 { 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"); 56 65 57 66 DeviceComponentInfo components; 58 59 //Read "all" (hence 0) components of the device - read into first entry60 ioctl(deviceID, EVIOCGBIT(0, EV_MAX), info[0]);61 67 62 68 for (int i = 0; i < EV_MAX; i++) 63 69 { 64 if( isBitSet( info[0], i) )70 if( isBitSet(ev_bits, i) ) 65 71 { 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) 69 74 { 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++) 71 87 { 72 if(i == EV_ABS)88 if( isBitSet(abs_bits, j) ) 73 89 { 74 90 //input_absinfo abInfo; … … 89 105 } 90 106 } 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) ) 92 125 { 93 126 components.relAxes.push_back(j); 94 127 } 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) ) 96 146 { 97 147 components.buttons.push_back(j); 98 148 } 99 149 } … … 108 158 bool EventUtils::isJoyStick( int deviceID, JoyStickInfo &js ) 109 159 { 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" ); 111 162 112 163 DeviceComponentInfo info = getComponentInfo( deviceID ); … … 117 168 118 169 #ifdef OIS_LINUX_JOY_DEBUG 119 cout << "\n\nDisplaying ButtonMapping Status:";170 cout << endl << "Displaying ButtonMapping Status:" << endl; 120 171 #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 ) 122 173 { 123 174 //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) ) 125 178 joyButtonFound = true; 126 179 … … 128 181 129 182 #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; 131 185 #endif 132 186 } 187 #ifdef OIS_LINUX_JOY_DEBUG 188 cout << endl; 189 #endif 133 190 134 191 //Joy Buttons found, so it must be a joystick or pad … … 140 197 js.axes = info.relAxes.size() + info.absAxes.size(); 141 198 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 142 204 143 205 //Map the Axes 144 206 #ifdef OIS_LINUX_JOY_DEBUG 145 cout << "\n\nDisplaying AxisMapping Status:";207 cout << endl << "Displaying AxisMapping Status:" << endl; 146 208 #endif 147 209 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 ) 149 211 { 150 212 js.axis_map[*i] = axes; 151 213 214 #ifdef OIS_LINUX_JOY_DEBUG 215 cout << "EventUtils::isJoyStick(" << deviceID 216 << ") : Reading device absolute axis #" << *i << " features" << endl; 217 #endif 218 152 219 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"); 154 222 js.axis_range[axes] = Range(absinfo.minimum, absinfo.maximum); 155 223 156 224 #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; 158 227 #endif 159 228 … … 166 235 167 236 //-----------------------------------------------------------------------------// 168 std::string EventUtils::getName( int deviceID ) 169 { 237 string EventUtils::getName( int deviceID ) 238 { 239 #ifdef OIS_LINUX_JOY_DEBUG 240 cout << "EventUtils::getName(" << deviceID 241 << ") : Reading device name" << endl; 242 #endif 243 170 244 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 //-----------------------------------------------------------------------------// 251 string 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 //-----------------------------------------------------------------------------// 266 string 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); 173 278 } 174 279 … … 177 282 { 178 283 //Linux Event to OIS Event Mappings 179 std::map<int, Effect::EType> typeMap;284 map<int, Effect::EType> typeMap; 180 285 typeMap[FF_CONSTANT] = Effect::Constant; 181 286 typeMap[FF_RAMP] = Effect::Ramp; … … 191 296 typeMap[FF_CUSTOM] = Effect::Custom; 192 297 193 std::map<int, Effect::EForce> forceMap;298 map<int, Effect::EForce> forceMap; 194 299 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; 198 311 199 312 //Remove any previously existing memory and create fresh 200 313 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 208 335 209 336 //FF Axes 210 //if( isBitSet( info, ABS_X) ) //X Axis211 //if( isBitSet( info, ABS_Y) ) //Y Axis212 //if( isBitSet( info, ABS_WHEEL) ) //Wheel337 //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 213 340 214 341 //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++ ) 216 343 { 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)) 218 350 { 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] ); 228 356 } 229 357 } 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); 230 365 231 366 //Check to see if any effects were added, else destroy the pointer
Note: See TracChangeset
for help on using the changeset viewer.