/src/ois/src/win32/extras/WiiMote/OISWiiMoteFactoryCreator.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 206 lines · 131 code · 30 blank · 45 comment · 35 complexity · df5d040bfa60f15cdb5d82026888a553 MD5 · raw file

  1. #include "OISConfig.h"
  2. #ifdef OIS_WIN32_WIIMOTE_SUPPORT
  3. /*
  4. The zlib/libpng License
  5. Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
  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. Permission is granted to anyone to use this software for any purpose, including commercial
  9. applications, and to alter it and redistribute it freely, subject to the following
  10. restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not claim that
  12. you wrote the original software. If you use this software in a product,
  13. an acknowledgment in the product documentation would be appreciated but is
  14. not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include "OISWiiMoteFactoryCreator.h"
  20. #include "OISException.h"
  21. #include "OISWiiMote.h"
  22. #include <assert.h>
  23. #include <boost/thread.hpp> //include here, keep compilation times down
  24. #include <boost/function.hpp>
  25. #include <boost/bind.hpp>
  26. using namespace OIS;
  27. //---------------------------------------------------------------------------------//
  28. WiiMoteFactoryCreator::WiiMoteFactoryCreator() :
  29. mVendorName("cWiiMote"),
  30. mCount(0),
  31. mtThreadHandler(0),
  32. mtWiiMoteListMutex(0),
  33. mtThreadRunning(0)
  34. {
  35. //Discover how many Wii's there are
  36. for( ; mCount < OIS_cWiiMote_MAX_WIIS; ++mCount )
  37. {
  38. cWiiMote wii;
  39. if( wii.ConnectToDevice(mCount) == false )
  40. break;
  41. }
  42. //Store how many WiiMotes there were in the form of integer handles
  43. for(int i = 0; i < mCount; ++i)
  44. mFreeWiis.push_back(i);
  45. //The mutex lasts the whole life of this class. The thread does not.
  46. mtWiiMoteListMutex = new boost::mutex();
  47. }
  48. //---------------------------------------------------------------------------------//
  49. WiiMoteFactoryCreator::~WiiMoteFactoryCreator()
  50. {
  51. //Thread (once all objects destroyed) should be killed off already
  52. assert( (mtThreadRunning == false && mtThreadHandler == 0) &&
  53. "~WiiMoteFactoryCreator(): invalid state.. Some objects left dangling!");
  54. delete mtWiiMoteListMutex;
  55. }
  56. //---------------------------------------------------------------------------------//
  57. DeviceList WiiMoteFactoryCreator::freeDeviceList()
  58. {
  59. DeviceList list;
  60. for( std::deque<int>::iterator i = mFreeWiis.begin(); i != mFreeWiis.end(); ++i )
  61. {
  62. list.insert(std::make_pair(OISJoyStick, mVendorName));
  63. }
  64. return list;
  65. }
  66. //---------------------------------------------------------------------------------//
  67. int WiiMoteFactoryCreator::totalDevices(Type iType)
  68. {
  69. if( iType == OISJoyStick )
  70. return mCount;
  71. else
  72. return 0;
  73. }
  74. //---------------------------------------------------------------------------------//
  75. int WiiMoteFactoryCreator::freeDevices(Type iType)
  76. {
  77. if( iType == OISJoyStick )
  78. return (int)mFreeWiis.size();
  79. else
  80. return 0;
  81. }
  82. //---------------------------------------------------------------------------------//
  83. bool WiiMoteFactoryCreator::vendorExist(Type iType, const std::string & vendor)
  84. {
  85. if( iType == OISJoyStick && mVendorName == vendor )
  86. return true;
  87. else
  88. return false;
  89. }
  90. //---------------------------------------------------------------------------------//
  91. Object* WiiMoteFactoryCreator::createObject(InputManager* creator, Type iType, bool bufferMode, const std::string & vendor)
  92. {
  93. if( mFreeWiis.size() > 0 && (vendor == "" || vendor == mVendorName ) )
  94. {
  95. int id = mFreeWiis.front();
  96. mFreeWiis.pop_front();
  97. WiiMote *wii = new WiiMote(creator, id, bufferMode, this);
  98. if( mtThreadRunning == false )
  99. { //Create common thread manager (this is the first wiimote created)
  100. mtThreadRunning = true;
  101. mtThreadHandler = new boost::thread(boost::bind(&WiiMoteFactoryCreator::_updateWiiMotesThread, this));
  102. }
  103. //Now, add new WiiMote to thread manager for polling
  104. { //Get an auto lock on the list of active wiimotes
  105. boost::mutex::scoped_lock arrayLock(*mtWiiMoteListMutex);
  106. mtInUseWiiMotes.push_back(wii);
  107. }
  108. return wii;
  109. }
  110. else
  111. OIS_EXCEPT(E_InputDeviceNonExistant, "No Device found which matches description!");
  112. }
  113. //---------------------------------------------------------------------------------//
  114. void WiiMoteFactoryCreator::destroyObject(Object* obj)
  115. {
  116. if( obj == 0 )
  117. return;
  118. int wiis_alive = 0;
  119. { //Get an auto lock on the list of active wiimotes
  120. boost::mutex::scoped_lock arrayLock(*mtWiiMoteListMutex);
  121. //Find object
  122. std::vector<WiiMote*>::iterator i = std::find(mtInUseWiiMotes.begin(), mtInUseWiiMotes.end(), obj);
  123. if( i == mtInUseWiiMotes.end() )
  124. OIS_EXCEPT(E_General, "Device not found in wimote collection!");
  125. //Erase opject
  126. mtInUseWiiMotes.erase(i);
  127. //Delete object
  128. delete obj;
  129. wiis_alive = (int)mtInUseWiiMotes.size();
  130. }
  131. //Destroy thread if no longer in use (we do this after unlocking mutex!)
  132. if( wiis_alive == 0 && mtThreadRunning )
  133. {
  134. mtThreadRunning = false;
  135. mtThreadHandler->join();
  136. delete mtThreadHandler;
  137. mtThreadHandler = 0;
  138. }
  139. }
  140. //---------------------------------------------------------------------------------//
  141. void WiiMoteFactoryCreator::_returnWiiMote(int id)
  142. { //Restore ID to controller pool
  143. mFreeWiis.push_front(id);
  144. }
  145. //---------------------------------------------------------------------------------//
  146. bool WiiMoteFactoryCreator::_updateWiiMotesThread()
  147. {
  148. boost::xtime timer;
  149. while(mtThreadRunning)
  150. {
  151. int numMotes = 0;
  152. { //Get an auto lock on the list of active wiimotes
  153. boost::mutex::scoped_lock arrayLock(*mtWiiMoteListMutex);
  154. numMotes = (int)mtInUseWiiMotes.size();
  155. for( std::vector<WiiMote*>::iterator i = mtInUseWiiMotes.begin(), e = mtInUseWiiMotes.end(); i != e; ++i )
  156. { //Update it
  157. (*i)->_threadUpdate();
  158. }
  159. }
  160. //ok, we have updated all wiimotes, let us rest a bit
  161. //sleep time = 30 / 1000
  162. //boost::thread::sleep(xtime) todo xxx wip use sleep instead??
  163. //boost::thread::yield();
  164. boost::xtime_get(&timer, boost::TIME_UTC);
  165. timer.nsec += 20000000; //20 000 000 ~= 1/50 sec
  166. boost::thread::sleep(timer);
  167. }
  168. return true;
  169. }
  170. #endif