[1505] | 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/MacInputManager.h" |
---|
[7506] | 39 | #elif defined OIS_IPHONE_PLATFORM |
---|
| 40 | # include "iphone/iPhoneInputManager.h" |
---|
[1505] | 41 | #elif defined OIS_XBOX_PLATFORM |
---|
| 42 | # include "xbox/XBoxInputManager.h" |
---|
| 43 | #endif |
---|
| 44 | |
---|
| 45 | //Bring in extra controls |
---|
| 46 | #if defined OIS_LIRC_SUPPORT |
---|
| 47 | # include "extras/LIRC/OISLIRCFactoryCreator.h" |
---|
| 48 | #endif |
---|
| 49 | #if defined OIS_WIN32_WIIMOTE_SUPPORT |
---|
| 50 | # include "win32/extras/WiiMote/OISWiiMoteFactoryCreator.h" |
---|
| 51 | #endif |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | using namespace OIS; |
---|
| 55 | |
---|
| 56 | //----------------------------------------------------------------------------// |
---|
| 57 | InputManager::InputManager(const std::string& name) : |
---|
| 58 | m_VersionName(OIS_VERSION_NAME), |
---|
| 59 | mInputSystemName(name), |
---|
| 60 | m_lircSupport(0), |
---|
| 61 | m_wiiMoteSupport(0) |
---|
| 62 | { |
---|
[7506] | 63 | mFactories.clear(); |
---|
| 64 | mFactoryObjects.clear(); |
---|
[1505] | 65 | } |
---|
| 66 | |
---|
| 67 | //----------------------------------------------------------------------------// |
---|
| 68 | InputManager::~InputManager() |
---|
| 69 | { |
---|
| 70 | #if defined OIS_LIRC_SUPPORT |
---|
| 71 | delete m_lircSupport; |
---|
| 72 | #endif |
---|
| 73 | |
---|
| 74 | #if defined OIS_WIN32_WIIMOTE_SUPPORT |
---|
| 75 | delete m_wiiMoteSupport; |
---|
| 76 | #endif |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | //----------------------------------------------------------------------------// |
---|
| 80 | unsigned int InputManager::getVersionNumber() |
---|
| 81 | { |
---|
| 82 | return OIS_VERSION; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | //----------------------------------------------------------------------------// |
---|
| 86 | const std::string &InputManager::getVersionName() |
---|
| 87 | { |
---|
| 88 | return m_VersionName; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | //----------------------------------------------------------------------------// |
---|
| 92 | InputManager* InputManager::createInputSystem( std::size_t windowhandle ) |
---|
| 93 | { |
---|
| 94 | ParamList pl; |
---|
| 95 | std::ostringstream wnd; |
---|
| 96 | wnd << windowhandle; |
---|
| 97 | pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() )); |
---|
| 98 | |
---|
| 99 | return createInputSystem( pl ); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | //----------------------------------------------------------------------------// |
---|
| 103 | InputManager* InputManager::createInputSystem( ParamList ¶mList ) |
---|
| 104 | { |
---|
| 105 | InputManager* im = 0; |
---|
| 106 | |
---|
| 107 | #if defined OIS_SDL_PLATFORM |
---|
| 108 | im = new SDLInputManager(); |
---|
| 109 | #elif defined OIS_WIN32_PLATFORM |
---|
| 110 | im = new Win32InputManager(); |
---|
| 111 | #elif defined OIS_XBOX_PLATFORM |
---|
| 112 | im = new XBoxInputManager(); |
---|
| 113 | #elif defined OIS_LINUX_PLATFORM |
---|
| 114 | im = new LinuxInputManager(); |
---|
| 115 | #elif defined OIS_APPLE_PLATFORM |
---|
| 116 | im = new MacInputManager(); |
---|
[7506] | 117 | #elif defined OIS_IPHONE_PLATFORM |
---|
| 118 | im = new iPhoneInputManager(); |
---|
[1505] | 119 | #else |
---|
| 120 | OIS_EXCEPT(E_General, "No platform library.. check build platform defines!"); |
---|
| 121 | #endif |
---|
| 122 | |
---|
| 123 | try |
---|
| 124 | { |
---|
| 125 | im->_initialize(paramList); |
---|
| 126 | } |
---|
| 127 | catch(...) |
---|
| 128 | { |
---|
| 129 | delete im; |
---|
| 130 | throw; //rethrow |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | return im; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | //----------------------------------------------------------------------------// |
---|
| 137 | void InputManager::destroyInputSystem(InputManager* manager) |
---|
| 138 | { |
---|
| 139 | if( manager == 0 ) |
---|
| 140 | return; |
---|
| 141 | |
---|
| 142 | //Cleanup before deleting... |
---|
| 143 | for( FactoryCreatedObject::iterator i = manager->mFactoryObjects.begin(); |
---|
| 144 | i != manager->mFactoryObjects.end(); ++i ) |
---|
| 145 | { |
---|
| 146 | i->second->destroyObject( i->first ); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | manager->mFactoryObjects.clear(); |
---|
| 150 | delete manager; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | //--------------------------------------------------------------------------------// |
---|
| 154 | const std::string& InputManager::inputSystemName() |
---|
| 155 | { |
---|
| 156 | return mInputSystemName; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | //--------------------------------------------------------------------------------// |
---|
| 160 | int InputManager::getNumberOfDevices( Type iType ) |
---|
| 161 | { |
---|
| 162 | //Count up all the factories devices |
---|
| 163 | int factoyObjects = 0; |
---|
| 164 | FactoryList::iterator i = mFactories.begin(), e = mFactories.end(); |
---|
| 165 | for( ; i != e; ++i ) |
---|
| 166 | factoyObjects += (*i)->totalDevices(iType); |
---|
| 167 | |
---|
| 168 | return factoyObjects; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | //----------------------------------------------------------------------------// |
---|
| 172 | DeviceList InputManager::listFreeDevices() |
---|
| 173 | { |
---|
| 174 | DeviceList list; |
---|
| 175 | FactoryList::iterator i = mFactories.begin(), e = mFactories.end(); |
---|
| 176 | for( ; i != e; ++i ) |
---|
| 177 | { |
---|
| 178 | DeviceList temp = (*i)->freeDeviceList(); |
---|
| 179 | list.insert(temp.begin(), temp.end()); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | return list; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | //----------------------------------------------------------------------------// |
---|
| 186 | Object* InputManager::createInputObject( Type iType, bool bufferMode, const std::string &vendor ) |
---|
| 187 | { |
---|
| 188 | Object* obj = 0; |
---|
| 189 | FactoryList::iterator i = mFactories.begin(), e = mFactories.end(); |
---|
| 190 | for( ; i != e; ++i) |
---|
| 191 | { |
---|
| 192 | if( (*i)->freeDevices(iType) > 0 ) |
---|
| 193 | { |
---|
| 194 | if( vendor == "" || (*i)->vendorExist(iType, vendor) ) |
---|
| 195 | { |
---|
| 196 | obj = (*i)->createObject(this, iType, bufferMode, vendor); |
---|
| 197 | mFactoryObjects[obj] = (*i); |
---|
| 198 | break; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | if(!obj) |
---|
| 204 | OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type."); |
---|
| 205 | |
---|
| 206 | try |
---|
| 207 | { //Intialize device |
---|
| 208 | obj->_initialize(); |
---|
| 209 | } |
---|
| 210 | catch(...) |
---|
| 211 | { //Somekind of error, cleanup and rethrow |
---|
| 212 | destroyInputObject(obj); |
---|
| 213 | throw; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | return obj; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | //----------------------------------------------------------------------------// |
---|
| 220 | void InputManager::destroyInputObject( Object* obj ) |
---|
| 221 | { |
---|
| 222 | if( obj == 0 ) |
---|
| 223 | return; |
---|
| 224 | |
---|
| 225 | FactoryCreatedObject::iterator i = mFactoryObjects.find(obj); |
---|
| 226 | if( i != mFactoryObjects.end() ) |
---|
| 227 | { |
---|
| 228 | i->second->destroyObject(obj); |
---|
| 229 | mFactoryObjects.erase(i); |
---|
| 230 | } |
---|
| 231 | else |
---|
| 232 | { |
---|
| 233 | OIS_EXCEPT(E_General, "Object creator not known."); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | //----------------------------------------------------------------------------// |
---|
| 238 | void InputManager::addFactoryCreator( FactoryCreator* factory ) |
---|
| 239 | { |
---|
| 240 | if(factory != 0) |
---|
| 241 | mFactories.push_back(factory); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | //----------------------------------------------------------------------------// |
---|
| 245 | void InputManager::removeFactoryCreator( FactoryCreator* factory ) |
---|
| 246 | { |
---|
| 247 | if(factory != 0) |
---|
| 248 | { |
---|
| 249 | //First, destroy all devices created with the factory |
---|
| 250 | for( FactoryCreatedObject::iterator i = mFactoryObjects.begin(); i != mFactoryObjects.end(); ++i ) |
---|
| 251 | { |
---|
| 252 | if( i->second == factory ) |
---|
| 253 | { |
---|
| 254 | i->second->destroyObject(i->first); |
---|
| 255 | mFactoryObjects.erase(i++); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | //Now, remove the factory itself |
---|
| 260 | FactoryList::iterator fact = std::find(mFactories.begin(), mFactories.end(), factory); |
---|
| 261 | if( fact != mFactories.end() ) |
---|
| 262 | mFactories.erase(fact); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | //----------------------------------------------------------------------------// |
---|
| 267 | void InputManager::enableAddOnFactory(AddOnFactories factory) |
---|
| 268 | { |
---|
| 269 | #if defined OIS_LIRC_SUPPORT |
---|
| 270 | if( factory == AddOn_LIRC || factory == AddOn_All ) |
---|
| 271 | { |
---|
| 272 | if( m_lircSupport == 0 ) |
---|
| 273 | { |
---|
| 274 | m_lircSupport = new LIRCFactoryCreator(); |
---|
| 275 | addFactoryCreator(m_lircSupport); |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | #endif |
---|
| 279 | |
---|
| 280 | #if defined OIS_WIN32_WIIMOTE_SUPPORT |
---|
| 281 | if( factory == AddOn_WiiMote || factory == AddOn_All ) |
---|
| 282 | { |
---|
| 283 | if( m_wiiMoteSupport == 0 ) |
---|
| 284 | { |
---|
| 285 | m_wiiMoteSupport = new WiiMoteFactoryCreator(); |
---|
| 286 | addFactoryCreator(m_wiiMoteSupport); |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | #endif |
---|
| 290 | } |
---|