[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 "linux/LinuxInputManager.h" |
---|
| 24 | #include "linux/LinuxKeyboard.h" |
---|
| 25 | #include "linux/LinuxJoyStickEvents.h" |
---|
| 26 | #include "linux/LinuxMouse.h" |
---|
| 27 | #include "OISException.h" |
---|
| 28 | #include <cstdlib> |
---|
| 29 | |
---|
| 30 | using namespace OIS; |
---|
| 31 | |
---|
| 32 | //--------------------------------------------------------------------------------// |
---|
| 33 | LinuxInputManager::LinuxInputManager() : InputManager("X11InputManager") |
---|
| 34 | { |
---|
| 35 | window = 0; |
---|
| 36 | |
---|
| 37 | //Default settings |
---|
| 38 | grabMouse = true; |
---|
| 39 | grabKeyboard = true; |
---|
| 40 | hideMouse = true; |
---|
| 41 | mGrabs = true; |
---|
| 42 | useXRepeat = false; |
---|
| 43 | keyboardUsed = mouseUsed = false; |
---|
| 44 | |
---|
| 45 | //Setup our internal factories |
---|
| 46 | mFactories.push_back(this); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | //--------------------------------------------------------------------------------// |
---|
| 50 | LinuxInputManager::~LinuxInputManager() |
---|
| 51 | { |
---|
| 52 | //Close all joysticks |
---|
| 53 | LinuxJoyStick::_clearJoys(unusedJoyStickList); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | //--------------------------------------------------------------------------------// |
---|
| 57 | void LinuxInputManager::_initialize( ParamList ¶mList ) |
---|
| 58 | { |
---|
| 59 | _parseConfigSettings( paramList ); |
---|
| 60 | |
---|
| 61 | //Enumerate all devices attached |
---|
| 62 | _enumerateDevices(); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | //--------------------------------------------------------------------------------// |
---|
| 66 | void LinuxInputManager::_parseConfigSettings( ParamList ¶mList ) |
---|
| 67 | { |
---|
| 68 | ParamList::iterator i = paramList.find("WINDOW"); |
---|
| 69 | if( i == paramList.end() ) |
---|
| 70 | OIS_EXCEPT( E_InvalidParam, "LinuxInputManager >> No WINDOW!" ); |
---|
| 71 | |
---|
| 72 | //TODO 64 bit proof this little conversion xxx wip |
---|
| 73 | window = strtoul(i->second.c_str(), 0, 10); |
---|
| 74 | |
---|
| 75 | //--------- Keyboard Settings ------------// |
---|
| 76 | i = paramList.find("XAutoRepeatOn"); |
---|
| 77 | if( i != paramList.end() ) |
---|
| 78 | if( i->second == "true" ) |
---|
| 79 | useXRepeat = true; |
---|
| 80 | |
---|
| 81 | i = paramList.find("x11_keyboard_grab"); |
---|
| 82 | if( i != paramList.end() ) |
---|
| 83 | if( i->second == "false" ) |
---|
| 84 | grabKeyboard = false; |
---|
| 85 | |
---|
| 86 | //--------- Mouse Settings ------------// |
---|
| 87 | i = paramList.find("x11_mouse_grab"); |
---|
| 88 | if( i != paramList.end() ) |
---|
| 89 | if( i->second == "false" ) |
---|
| 90 | grabMouse = false; |
---|
| 91 | |
---|
| 92 | i = paramList.find("x11_mouse_hide"); |
---|
| 93 | if( i != paramList.end() ) |
---|
| 94 | if( i->second == "false" ) |
---|
| 95 | hideMouse = false; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | //--------------------------------------------------------------------------------// |
---|
| 99 | void LinuxInputManager::_enumerateDevices() |
---|
| 100 | { |
---|
| 101 | //Enumerate all attached devices |
---|
| 102 | unusedJoyStickList = LinuxJoyStick::_scanJoys(); |
---|
| 103 | joySticks = unusedJoyStickList.size(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | //----------------------------------------------------------------------------// |
---|
| 107 | DeviceList LinuxInputManager::freeDeviceList() |
---|
| 108 | { |
---|
| 109 | DeviceList ret; |
---|
| 110 | |
---|
| 111 | if( keyboardUsed == false ) |
---|
| 112 | ret.insert(std::make_pair(OISKeyboard, mInputSystemName)); |
---|
| 113 | |
---|
| 114 | if( mouseUsed == false ) |
---|
| 115 | ret.insert(std::make_pair(OISMouse, mInputSystemName)); |
---|
| 116 | |
---|
| 117 | for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i) |
---|
| 118 | ret.insert(std::make_pair(OISJoyStick, i->vendor)); |
---|
| 119 | |
---|
| 120 | return ret; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | //----------------------------------------------------------------------------// |
---|
| 124 | int LinuxInputManager::totalDevices(Type iType) |
---|
| 125 | { |
---|
| 126 | switch(iType) |
---|
| 127 | { |
---|
| 128 | case OISKeyboard: return 1; |
---|
| 129 | case OISMouse: return 1; |
---|
| 130 | case OISJoyStick: return joySticks; |
---|
| 131 | default: return 0; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | //----------------------------------------------------------------------------// |
---|
| 136 | int LinuxInputManager::freeDevices(Type iType) |
---|
| 137 | { |
---|
| 138 | switch(iType) |
---|
| 139 | { |
---|
| 140 | case OISKeyboard: return keyboardUsed ? 0 : 1; |
---|
| 141 | case OISMouse: return mouseUsed ? 0 : 1; |
---|
| 142 | case OISJoyStick: return (int)unusedJoyStickList.size(); |
---|
| 143 | default: return 0; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | //----------------------------------------------------------------------------// |
---|
| 148 | bool LinuxInputManager::vendorExist(Type iType, const std::string & vendor) |
---|
| 149 | { |
---|
| 150 | if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName ) |
---|
| 151 | { |
---|
| 152 | return true; |
---|
| 153 | } |
---|
| 154 | else if( iType == OISJoyStick ) |
---|
| 155 | { |
---|
| 156 | for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i) |
---|
| 157 | if(i->vendor == vendor) |
---|
| 158 | return true; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | return false; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | //----------------------------------------------------------------------------// |
---|
| 165 | Object* LinuxInputManager::createObject(InputManager *creator, Type iType, bool bufferMode, const std::string & vendor) |
---|
| 166 | { |
---|
| 167 | Object *obj = 0; |
---|
| 168 | |
---|
| 169 | switch(iType) |
---|
| 170 | { |
---|
| 171 | case OISKeyboard: |
---|
| 172 | { |
---|
| 173 | if( keyboardUsed == false ) |
---|
| 174 | obj = new LinuxKeyboard(this, bufferMode, grabKeyboard, useXRepeat); |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | case OISMouse: |
---|
| 178 | { |
---|
| 179 | if( mouseUsed == false ) |
---|
| 180 | obj = new LinuxMouse(this, bufferMode, grabMouse, hideMouse); |
---|
| 181 | break; |
---|
| 182 | } |
---|
| 183 | case OISJoyStick: |
---|
| 184 | { |
---|
| 185 | for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i) |
---|
| 186 | { |
---|
| 187 | if(vendor == "" || i->vendor == vendor) |
---|
| 188 | { |
---|
| 189 | obj = new LinuxJoyStick(this, bufferMode, *i); |
---|
| 190 | unusedJoyStickList.erase(i); |
---|
| 191 | break; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | break; |
---|
| 195 | } |
---|
| 196 | default: |
---|
| 197 | break; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if( obj == 0 ) |
---|
| 201 | OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type."); |
---|
| 202 | |
---|
| 203 | return obj; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | //----------------------------------------------------------------------------// |
---|
| 207 | void LinuxInputManager::destroyObject( Object* obj ) |
---|
| 208 | { |
---|
| 209 | if( obj ) |
---|
| 210 | { |
---|
| 211 | if( obj->type() == OISJoyStick ) |
---|
| 212 | { |
---|
| 213 | unusedJoyStickList.push_back( ((LinuxJoyStick*)obj)->_getJoyInfo() ); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | delete obj; |
---|
| 217 | } |
---|
| 218 | } |
---|