LinuxInputManager.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. The zlib/libpng License
  3. Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
  4. This software is provided 'as-is', without any express or implied warranty. In no event will
  5. the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose, including commercial
  7. applications, and to alter it and redistribute it freely, subject to the following
  8. restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that
  10. you wrote the original software. If you use this software in a product,
  11. an acknowledgment in the product documentation would be appreciated but is
  12. not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "linux/LinuxInputManager.h"
  18. #include "linux/LinuxKeyboard.h"
  19. #include "linux/LinuxJoyStickEvents.h"
  20. #include "linux/LinuxMouse.h"
  21. #include "OISException.h"
  22. #include <cstdlib>
  23. using namespace OIS;
  24. //--------------------------------------------------------------------------------//
  25. LinuxInputManager::LinuxInputManager() : InputManager("X11InputManager")
  26. {
  27. window = 0;
  28. //Default settings
  29. grabMouse = true;
  30. grabKeyboard = true;
  31. hideMouse = true;
  32. mGrabs = true;
  33. keyboardUsed = mouseUsed = false;
  34. //Setup our internal factories
  35. mFactories.push_back(this);
  36. }
  37. //--------------------------------------------------------------------------------//
  38. LinuxInputManager::~LinuxInputManager()
  39. {
  40. //Close all joysticks
  41. LinuxJoyStick::_clearJoys(unusedJoyStickList);
  42. }
  43. //--------------------------------------------------------------------------------//
  44. void LinuxInputManager::_initialize( ParamList &paramList )
  45. {
  46. _parseConfigSettings( paramList );
  47. //Enumerate all devices attached
  48. _enumerateDevices();
  49. }
  50. //--------------------------------------------------------------------------------//
  51. void LinuxInputManager::_parseConfigSettings( ParamList &paramList )
  52. {
  53. ParamList::iterator i = paramList.find("WINDOW");
  54. if( i == paramList.end() )
  55. OIS_EXCEPT( E_InvalidParam, "LinuxInputManager >> No WINDOW!" );
  56. //TODO 64 bit proof this little conversion xxx wip
  57. window = strtoul(i->second.c_str(), 0, 10);
  58. //--------- Keyboard Settings ------------//
  59. i = paramList.find("x11_keyboard_grab");
  60. if( i != paramList.end() )
  61. if( i->second == "false" )
  62. grabKeyboard = false;
  63. //--------- Mouse Settings ------------//
  64. i = paramList.find("x11_mouse_grab");
  65. if( i != paramList.end() )
  66. if( i->second == "false" )
  67. grabMouse = false;
  68. i = paramList.find("x11_mouse_hide");
  69. if( i != paramList.end() )
  70. if( i->second == "false" )
  71. hideMouse = false;
  72. }
  73. //--------------------------------------------------------------------------------//
  74. void LinuxInputManager::_enumerateDevices()
  75. {
  76. //Enumerate all attached devices
  77. unusedJoyStickList = LinuxJoyStick::_scanJoys();
  78. joySticks = unusedJoyStickList.size();
  79. }
  80. //----------------------------------------------------------------------------//
  81. DeviceList LinuxInputManager::freeDeviceList()
  82. {
  83. DeviceList ret;
  84. if( keyboardUsed == false )
  85. ret.insert(std::make_pair(OISKeyboard, mInputSystemName));
  86. if( mouseUsed == false )
  87. ret.insert(std::make_pair(OISMouse, mInputSystemName));
  88. for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
  89. ret.insert(std::make_pair(OISJoyStick, i->vendor));
  90. return ret;
  91. }
  92. //----------------------------------------------------------------------------//
  93. int LinuxInputManager::totalDevices(Type iType)
  94. {
  95. switch(iType)
  96. {
  97. case OISKeyboard: return 1;
  98. case OISMouse: return 1;
  99. case OISJoyStick: return joySticks;
  100. default: return 0;
  101. }
  102. }
  103. //----------------------------------------------------------------------------//
  104. int LinuxInputManager::freeDevices(Type iType)
  105. {
  106. switch(iType)
  107. {
  108. case OISKeyboard: return keyboardUsed ? 0 : 1;
  109. case OISMouse: return mouseUsed ? 0 : 1;
  110. case OISJoyStick: return (int)unusedJoyStickList.size();
  111. default: return 0;
  112. }
  113. }
  114. //----------------------------------------------------------------------------//
  115. bool LinuxInputManager::vendorExist(Type iType, const std::string & vendor)
  116. {
  117. if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName )
  118. {
  119. return true;
  120. }
  121. else if( iType == OISJoyStick )
  122. {
  123. for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
  124. if(i->vendor == vendor)
  125. return true;
  126. }
  127. return false;
  128. }
  129. //----------------------------------------------------------------------------//
  130. Object* LinuxInputManager::createObject(InputManager *creator, Type iType, bool bufferMode, const std::string & vendor)
  131. {
  132. Object *obj = 0;
  133. switch(iType)
  134. {
  135. case OISKeyboard:
  136. {
  137. if( keyboardUsed == false )
  138. obj = new LinuxKeyboard(this, bufferMode, grabKeyboard);
  139. break;
  140. }
  141. case OISMouse:
  142. {
  143. if( mouseUsed == false )
  144. obj = new LinuxMouse(this, bufferMode, grabMouse, hideMouse);
  145. break;
  146. }
  147. case OISJoyStick:
  148. {
  149. for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
  150. {
  151. if(vendor == "" || i->vendor == vendor)
  152. {
  153. obj = new LinuxJoyStick(this, bufferMode, *i);
  154. unusedJoyStickList.erase(i);
  155. break;
  156. }
  157. }
  158. break;
  159. }
  160. default:
  161. break;
  162. }
  163. if( obj == 0 )
  164. OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type.");
  165. return obj;
  166. }
  167. //----------------------------------------------------------------------------//
  168. void LinuxInputManager::destroyObject( Object* obj )
  169. {
  170. if( obj )
  171. {
  172. if( obj->type() == OISJoyStick )
  173. {
  174. unusedJoyStickList.push_back( ((LinuxJoyStick*)obj)->_getJoyInfo() );
  175. }
  176. delete obj;
  177. }
  178. }