1 /* 2 The zlib/libpng License 3 4 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com) 5 6 This software is provided 'as-is', without any express or implied warranty. In no event will 7 the authors be held liable for any damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial 10 applications, and to alter it and redistribute it freely, subject to the following 11 restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not claim that 14 you wrote the original software. If you use this software in a product, 15 an acknowledgment in the product documentation would be appreciated but is 16 not required. 17 18 2. Altered source versions must be plainly marked as such, and must not be 19 misrepresented as being the original software. 20 21 3. This notice may not be removed or altered from any source distribution. 22 */ 23 #include "OISInputManager.h" 24 #include "OISException.h" 25 #include "OISFactoryCreator.h" 26 #include "OISObject.h" 27 #include <sstream> 28 #include <algorithm> 29 30 //Bring in correct Header / InputManager for current build platform 31 #if defined OIS_SDL_PLATFORM 32 # include "SDL/SDLInputManager.h" 33 #elif defined OIS_WIN32_PLATFORM 34 # include "win32/Win32InputManager.h" 35 #elif defined OIS_LINUX_PLATFORM 36 # include "linux/LinuxInputManager.h" 37 #elif defined OIS_APPLE_PLATFORM 38 # include "mac/CocoaInputManager.h" 39 # include "mac/MacInputManager.h" 40 #elif defined OIS_IPHONE_PLATFORM 41 # include "iphone/iPhoneInputManager.h" 42 #elif defined OIS_XBOX_PLATFORM 43 # include "xbox/XBoxInputManager.h" 44 #endif 45 46 //Bring in extra controls 47 #if defined OIS_LIRC_SUPPORT 48 # include "extras/LIRC/OISLIRCFactoryCreator.h" 49 #endif 50 #if defined OIS_WIN32_WIIMOTE_SUPPORT 51 # include "win32/extras/WiiMote/OISWiiMoteFactoryCreator.h" 52 #endif 53 54 55 using namespace OIS; 56 57 //----------------------------------------------------------------------------// 58 InputManager::InputManager(const std::string& name) : 59 m_VersionName(OIS_VERSION_NAME), 60 mInputSystemName(name), 61 m_lircSupport(0), 62 m_wiiMoteSupport(0) 63 { 64 mFactories.clear(); 65 mFactoryObjects.clear(); 66 } 67 68 //----------------------------------------------------------------------------// 69 InputManager::~InputManager() 70 { 71 #if defined OIS_LIRC_SUPPORT 72 delete m_lircSupport; 73 #endif 74 75 #if defined OIS_WIN32_WIIMOTE_SUPPORT 76 delete m_wiiMoteSupport; 77 #endif 78 } 79 80 //----------------------------------------------------------------------------// 81 unsigned int InputManager::getVersionNumber() 82 { 83 return OIS_VERSION; 84 } 85 86 //----------------------------------------------------------------------------// 87 const std::string &InputManager::getVersionName() 88 { 89 return m_VersionName; 90 } 91 92 //----------------------------------------------------------------------------// 93 InputManager* InputManager::createInputSystem( std::size_t windowhandle ) 94 { 95 ParamList pl; 96 std::ostringstream wnd; 97 wnd << windowhandle; 98 pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() )); 99 100 return createInputSystem( pl ); 101 } 102 103 //----------------------------------------------------------------------------// 104 InputManager* InputManager::createInputSystem( ParamList ¶mList ) 105 { 106 InputManager* im = 0; 107 108 #if defined OIS_SDL_PLATFORM 109 im = new SDLInputManager(); 110 #elif defined OIS_WIN32_PLATFORM 111 im = new Win32InputManager(); 112 #elif defined OIS_XBOX_PLATFORM 113 im = new XBoxInputManager(); 114 #elif defined OIS_LINUX_PLATFORM 115 im = new LinuxInputManager(); 116 #elif defined OIS_APPLE_PLATFORM 117 im = new CocoaInputManager(); 118 // ParamList::iterator i = paramList.find("WINDOW"); 119 // if(i != paramList.end()) 120 // { 121 // id obj = (id)strtoul(i->second.c_str(), 0, 10); 122 // if(obj && [obj isKindOfClass:[NSWindow class]]) 123 // im = new CocoaInputManager(); 124 // #ifndef __LP64__ 125 // else 126 // im = new MacInputManager(); 127 // #endif 128 // } 129 #elif defined OIS_IPHONE_PLATFORM 130 im = new iPhoneInputManager(); 131 #else 132 OIS_EXCEPT(E_General, "No platform library.. check build platform defines!"); 133 #endif 134 135 try 136 { 137 im->_initialize(paramList); 138 } 139 catch(...) 140 { 141 delete im; 142 throw; //rethrow 143 } 144 145 return im; 146 } 147 148 //----------------------------------------------------------------------------// 149 void InputManager::destroyInputSystem(InputManager* manager) 150 { 151 if( manager == 0 ) 152 return; 153 154 //Cleanup before deleting... 155 for( FactoryCreatedObject::iterator i = manager->mFactoryObjects.begin(); 156 i != manager->mFactoryObjects.end(); ++i ) 157 { 158 i->second->destroyObject( i->first ); 159 } 160 161 manager->mFactoryObjects.clear(); 162 delete manager; 163 } 164 165 //--------------------------------------------------------------------------------// 166 const std::string& InputManager::inputSystemName() 167 { 168 return mInputSystemName; 169 } 170 171 //--------------------------------------------------------------------------------// 172 int InputManager::getNumberOfDevices( Type iType ) 173 { 174 //Count up all the factories devices 175 int factoyObjects = 0; 176 FactoryList::iterator i = mFactories.begin(), e = mFactories.end(); 177 for( ; i != e; ++i ) 178 factoyObjects += (*i)->totalDevices(iType); 179 180 return factoyObjects; 181 } 182 183 //----------------------------------------------------------------------------// 184 DeviceList InputManager::listFreeDevices() 185 { 186 DeviceList list; 187 FactoryList::iterator i = mFactories.begin(), e = mFactories.end(); 188 for( ; i != e; ++i ) 189 { 190 DeviceList temp = (*i)->freeDeviceList(); 191 list.insert(temp.begin(), temp.end()); 192 } 193 194 return list; 195 } 196 197 //----------------------------------------------------------------------------// 198 Object* InputManager::createInputObject( Type iType, bool bufferMode, const std::string &vendor ) 199 { 200 Object* obj = 0; 201 FactoryList::iterator i = mFactories.begin(), e = mFactories.end(); 202 for( ; i != e; ++i) 203 { 204 if( (*i)->freeDevices(iType) > 0 ) 205 { 206 if( vendor == "" || (*i)->vendorExist(iType, vendor) ) 207 { 208 obj = (*i)->createObject(this, iType, bufferMode, vendor); 209 mFactoryObjects[obj] = (*i); 210 break; 211 } 212 } 213 } 214 215 if(!obj) 216 OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type."); 217 218 try 219 { //Intialize device 220 obj->_initialize(); 221 } 222 catch(...) 223 { //Somekind of error, cleanup and rethrow 224 destroyInputObject(obj); 225 throw; 226 } 227 228 return obj; 229 } 230 231 //----------------------------------------------------------------------------// 232 void InputManager::destroyInputObject( Object* obj ) 233 { 234 if( obj == 0 ) 235 return; 236 237 FactoryCreatedObject::iterator i = mFactoryObjects.find(obj); 238 if( i != mFactoryObjects.end() ) 239 { 240 i->second->destroyObject(obj); 241 mFactoryObjects.erase(i); 242 } 243 else 244 { 245 OIS_EXCEPT(E_General, "Object creator not known."); 246 } 247 } 248 249 //----------------------------------------------------------------------------// 250 void InputManager::addFactoryCreator( FactoryCreator* factory ) 251 { 252 if(factory != 0) 253 mFactories.push_back(factory); 254 } 255 256 //----------------------------------------------------------------------------// 257 void InputManager::removeFactoryCreator( FactoryCreator* factory ) 258 { 259 if(factory != 0) 260 { 261 //First, destroy all devices created with the factory 262 for( FactoryCreatedObject::iterator i = mFactoryObjects.begin(); i != mFactoryObjects.end(); ++i ) 263 { 264 if( i->second == factory ) 265 { 266 i->second->destroyObject(i->first); 267 mFactoryObjects.erase(i++); 268 } 269 } 270 271 //Now, remove the factory itself 272 FactoryList::iterator fact = std::find(mFactories.begin(), mFactories.end(), factory); 273 if( fact != mFactories.end() ) 274 mFactories.erase(fact); 275 } 276 } 277 278 //----------------------------------------------------------------------------// 279 void InputManager::enableAddOnFactory(AddOnFactories factory) 280 { 281 #if defined OIS_LIRC_SUPPORT 282 if( factory == AddOn_LIRC || factory == AddOn_All ) 283 { 284 if( m_lircSupport == 0 ) 285 { 286 m_lircSupport = new LIRCFactoryCreator(); 287 addFactoryCreator(m_lircSupport); 288 } 289 } 290 #endif 291 292 #if defined OIS_WIN32_WIIMOTE_SUPPORT 293 if( factory == AddOn_WiiMote || factory == AddOn_All ) 294 { 295 if( m_wiiMoteSupport == 0 ) 296 { 297 m_wiiMoteSupport = new WiiMoteFactoryCreator(); 298 addFactoryCreator(m_wiiMoteSupport); 299 } 300 } 301 #endif 302 } 303