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