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