/src/ois/src/linux/LinuxJoyStickEvents.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 305 lines · 212 code · 41 blank · 52 comment · 54 complexity · 74562448066e3d605674f891abca41d5 MD5 · raw file

  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 "OISConfig.h"
  18. #include "linux/LinuxJoyStickEvents.h"
  19. #include "linux/LinuxInputManager.h"
  20. #include "linux/LinuxForceFeedback.h"
  21. #include "linux/EventHelpers.h"
  22. #include "OISEvents.h"
  23. #include "OISException.h"
  24. #include <fcntl.h> //Needed to Open a file descriptor
  25. #include <cassert>
  26. #include <linux/input.h>
  27. #include <sstream>
  28. # include <iostream>
  29. using namespace std;
  30. using namespace OIS;
  31. //#define OIS_LINUX_JOY_DEBUG
  32. //-------------------------------------------------------------------//
  33. LinuxJoyStick::LinuxJoyStick(InputManager* creator, bool buffered, const JoyStickInfo& js)
  34. : JoyStick(js.vendor, buffered, js.devId, creator)
  35. {
  36. mJoyStick = js.joyFileD;
  37. mState.mAxes.clear();
  38. mState.mAxes.resize(js.axes);
  39. mState.mButtons.clear();
  40. mState.mButtons.resize(js.buttons);
  41. mPOVs = js.hats;
  42. mButtonMap = js.button_map;
  43. mAxisMap = js.axis_map;
  44. mRanges = js.axis_range;
  45. ff_effect = 0;
  46. }
  47. //-------------------------------------------------------------------//
  48. LinuxJoyStick::~LinuxJoyStick()
  49. {
  50. EventUtils::removeForceFeedback( &ff_effect );
  51. }
  52. //-------------------------------------------------------------------//
  53. void LinuxJoyStick::_initialize()
  54. {
  55. //Clear old joy state
  56. mState.mAxes.resize(mAxisMap.size());
  57. mState.clear();
  58. //This will create and new us a force feedback structure if it exists
  59. EventUtils::enumerateForceFeedback( mJoyStick, &ff_effect );
  60. if( mJoyStick == -1 )
  61. OIS_EXCEPT(E_InputDeviceNonExistant, "LinuxJoyStick::_initialize() >> JoyStick Not Found!");
  62. }
  63. //-------------------------------------------------------------------//
  64. void LinuxJoyStick::capture()
  65. {
  66. static const short POV_MASK[8] = {0,0,1,1,2,2,3,3};
  67. //Used to determine if an axis has been changed and needs an event
  68. bool axisMoved[32] = {false, false, false, false, false, false, false, false, false, false, false, false, false,
  69. false, false, false, false, false, false, false, false, false, false, false, false, false,
  70. false, false, false, false, false, false};
  71. //We are in non blocking mode - we just read once, and try to fill up buffer
  72. input_event js[JOY_BUFFERSIZE];
  73. while(true)
  74. {
  75. int ret = read(mJoyStick, &js, sizeof(struct input_event) * JOY_BUFFERSIZE);
  76. if( ret < 0 )
  77. break;
  78. //Determine how many whole events re read up
  79. ret /= sizeof(struct input_event);
  80. for(int i = 0; i < ret; ++i)
  81. {
  82. switch(js[i].type)
  83. {
  84. case EV_KEY: //Button
  85. {
  86. int button = mButtonMap[js[i].code];
  87. #ifdef OIS_LINUX_JOY_DEBUG
  88. cout << "\nButton Code: " << js[i].code << ", OIS Value: " << button << endl;
  89. #endif
  90. //Check to see whether push or released event...
  91. if(js[i].value)
  92. {
  93. mState.mButtons[button] = true;
  94. if( mBuffered && mListener )
  95. if(!mListener->buttonPressed(JoyStickEvent(this,mState), button)) return;
  96. }
  97. else
  98. {
  99. mState.mButtons[button] = false;
  100. if( mBuffered && mListener )
  101. if(!mListener->buttonReleased(JoyStickEvent(this,mState), button)) return;
  102. }
  103. break;
  104. }
  105. case EV_ABS: //Absolute Axis
  106. {
  107. //A Stick (BrakeDefine is the highest possible Axis)
  108. if( js[i].code <= ABS_BRAKE )
  109. {
  110. int axis = mAxisMap[js[i].code];
  111. assert( axis < 32 && "Too many axes (Max supported is 32). Report this to OIS forums!" );
  112. axisMoved[axis] = true;
  113. //check for rescaling:
  114. if( mRanges[axis].min == JoyStick::MIN_AXIS && mRanges[axis].max != JoyStick::MAX_AXIS )
  115. { //Scale is perfect
  116. mState.mAxes[axis].abs = js[i].value;
  117. }
  118. else
  119. { //Rescale
  120. float proportion = (float)(js[i].value-mRanges[axis].max)/(float)(mRanges[axis].min-mRanges[axis].max);
  121. mState.mAxes[axis].abs = (int)(32767.0f - (65535.0f * proportion));
  122. }
  123. }
  124. else if( js[i].code <= ABS_HAT3Y ) //A POV - Max four POVs allowed
  125. {
  126. //Normalise the POV to between 0-7
  127. //Even is X Axis, Odd is Y Axis
  128. unsigned char LinuxPovNumber = js[i].code - 16;
  129. short OIS_POVIndex = POV_MASK[LinuxPovNumber];
  130. //Handle X Axis first (Even) (left right)
  131. if((LinuxPovNumber & 0x0001) == 0)
  132. {
  133. //Why do this? Because, we use a bit field, and when this axis is east,
  134. //it can't possibly be west too. So clear out the two X axes, then refil
  135. //it in with the new direction bit.
  136. //Clear the East/West Bit Flags first
  137. mState.mPOV[OIS_POVIndex].direction &= 0x11110011;
  138. if( js[i].value == -1 ) //Left
  139. mState.mPOV[OIS_POVIndex].direction |= Pov::West;
  140. else if( js[i].value == 1 ) //Right
  141. mState.mPOV[OIS_POVIndex].direction |= Pov::East;
  142. }
  143. //Handle Y Axis (Odd) (up down)
  144. else
  145. {
  146. //Clear the North/South Bit Flags first
  147. mState.mPOV[OIS_POVIndex].direction &= 0x11111100;
  148. if( js[i].value == -1 ) //Up
  149. mState.mPOV[OIS_POVIndex].direction |= Pov::North;
  150. else if( js[i].value == 1 ) //Down
  151. mState.mPOV[OIS_POVIndex].direction |= Pov::South;
  152. }
  153. if( mBuffered && mListener )
  154. if( mListener->povMoved( JoyStickEvent(this,mState), OIS_POVIndex) == false )
  155. return;
  156. }
  157. break;
  158. }
  159. case EV_REL: //Relative Axes (Do any joystick actually have a relative axis?)
  160. #ifdef OIS_LINUX_JOY_DEBUG
  161. cout << "\nWarning: Relatives axes not supported yet" << endl;
  162. #endif
  163. break;
  164. default: break;
  165. }
  166. }
  167. }
  168. //All axes and POVs are combined into one movement per pair per captured frame
  169. if( mBuffered && mListener )
  170. {
  171. for( int i = 0; i < 32; ++i )
  172. if( axisMoved[i] )
  173. if( mListener->axisMoved( JoyStickEvent(this,mState), i) == false )
  174. return;
  175. }
  176. }
  177. //-------------------------------------------------------------------//
  178. void LinuxJoyStick::setBuffered(bool buffered)
  179. {
  180. if( buffered != mBuffered )
  181. {
  182. mBuffered = buffered;
  183. _initialize();
  184. }
  185. }
  186. //-------------------------------------------------------------------//
  187. JoyStickInfo LinuxJoyStick::_getJoyInfo()
  188. {
  189. JoyStickInfo js;
  190. js.devId = mDevID;
  191. js.joyFileD = mJoyStick;
  192. js.vendor = mVendor;
  193. js.axes = (int)mState.mAxes.size();
  194. js.buttons = (int)mState.mButtons.size();
  195. js.hats = mPOVs;
  196. js.button_map = mButtonMap;
  197. js.axis_map = mAxisMap;
  198. js.axis_range = mRanges;
  199. return js;
  200. }
  201. //-------------------------------------------------------------------//
  202. JoyStickInfoList LinuxJoyStick::_scanJoys()
  203. {
  204. JoyStickInfoList joys;
  205. //Search through all of the event devices.. and identify which ones are joysticks
  206. //xxx move this to InputManager, as it can also scan all other events
  207. for(int i = 0; i < 64; ++i )
  208. {
  209. stringstream s;
  210. s << "/dev/input/event" << i;
  211. int fd = open( s.str().c_str(), O_RDWR |O_NONBLOCK );
  212. if(fd == -1)
  213. continue;
  214. #ifdef OIS_LINUX_JOY_DEBUG
  215. cout << "Opening " << s.str() << "..." << endl;
  216. #endif
  217. try
  218. {
  219. JoyStickInfo js;
  220. if( EventUtils::isJoyStick(fd, js) )
  221. {
  222. joys.push_back(js);
  223. #ifdef OIS_LINUX_JOY_DEBUG
  224. cout << "=> Joystick added to list." << endl;
  225. #endif
  226. }
  227. else
  228. {
  229. #ifdef OIS_LINUX_JOY_DEBUG
  230. cout << "=> Not a joystick." << endl;
  231. #endif
  232. close(fd);
  233. }
  234. }
  235. catch(...)
  236. {
  237. #ifdef OIS_LINUX_JOY_DEBUG
  238. cout << "Exception caught!!" << endl;
  239. #endif
  240. close(fd);
  241. }
  242. }
  243. return joys;
  244. }
  245. //-------------------------------------------------------------------//
  246. void LinuxJoyStick::_clearJoys(JoyStickInfoList &joys)
  247. {
  248. for(JoyStickInfoList::iterator i = joys.begin(); i != joys.end(); ++i)
  249. close(i->joyFileD);
  250. joys.clear();
  251. }
  252. //-------------------------------------------------------------------//
  253. Interface* LinuxJoyStick::queryInterface(Interface::IType type)
  254. {
  255. if( ff_effect && type == Interface::ForceFeedback )
  256. return ff_effect;
  257. return 0;
  258. }