/src/ois/src/extras/LIRC/OISLIRCFactoryCreator.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 405 lines · 285 code · 66 blank · 54 comment · 86 complexity · ddf1f73b81f34e515a24700bb837b0fa MD5 · raw file

  1. #include "OISConfig.h"
  2. #ifdef OIS_LIRC_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 "OISLIRCFactoryCreator.h"
  20. #include "OISException.h"
  21. #include <assert.h>
  22. #include <stdlib.h>
  23. #ifdef OIS_WIN32_PLATFORM
  24. # pragma warning (disable : 4996)
  25. # pragma warning (disable : 4267)
  26. # pragma warning (disable : 4554)
  27. # pragma warning (disable : 4996)
  28. # define _WIN32_WINNT 0x0500
  29. #endif
  30. #include <boost/asio.hpp>
  31. #include <boost/bind.hpp>
  32. #include <boost/thread.hpp>
  33. #include <istream>
  34. #include <sstream>
  35. using namespace OIS;
  36. //---------------------------------------------------------------------------------//
  37. class LIRCFactoryCreator::BoostWrapper
  38. {
  39. public:
  40. LIRCFactoryCreator::BoostWrapper() : mSocket(mIOService), mThreadHandler(0)
  41. {
  42. }
  43. //TCP stuff
  44. boost::asio::io_service mIOService;
  45. boost::asio::ip::tcp::socket mSocket;
  46. //Thread Stuff
  47. //! Boost thread execution object (only alive when at least 1 lirc is alive)
  48. boost::thread *mThreadHandler;
  49. //! Gaurds access to the active lirc list
  50. boost::mutex mLircListMutex;
  51. };
  52. //---------------------------------------------------------------------------------//
  53. LIRCFactoryCreator::LIRCFactoryCreator() :
  54. mConnected(false),
  55. mThreadRunning(false),
  56. mCount(0),
  57. mWrapped(0)
  58. {
  59. mWrapped = new BoostWrapper();
  60. mIP = (getenv("OIS_LIRC_IP") != 0) ? getenv("OIS_LIRC_IP") : "127.0.0.1";
  61. mPort = (getenv("OIS_LIRC_PORT") != 0) ? getenv("OIS_LIRC_PORT") : "8765";
  62. try
  63. {
  64. enableConnection(true);
  65. discoverRemotes();
  66. }
  67. catch(...)
  68. {
  69. mCount = 0;
  70. }
  71. //Regardless of if there is remotes or not, we will close the conenction now.
  72. enableConnection(false);
  73. }
  74. //---------------------------------------------------------------------------------//
  75. LIRCFactoryCreator::~LIRCFactoryCreator()
  76. {
  77. enableConnectionThread(false);
  78. enableConnection(false);
  79. delete mWrapped;
  80. }
  81. //---------------------------------------------------------------------------------//
  82. void LIRCFactoryCreator::discoverRemotes()
  83. {
  84. //http://www.lirc.org/html/technical.html#applications
  85. mCount = 0;
  86. mWrapped->mSocket.write_some(boost::asio::buffer("LIST\n"));
  87. boost::asio::streambuf buffer;
  88. //Read all remotes
  89. bool start = false;
  90. bool data = false;
  91. for(;;)
  92. {
  93. boost::asio::read_until(mWrapped->mSocket, buffer, '\n');
  94. std::istream str(&buffer);
  95. std::string res;
  96. str >> res;
  97. if( res == "" ) //If nothing left, we are done
  98. break;
  99. else if( res == "ERROR" ) //If any errors, we leave immediately
  100. return;
  101. else if( res == "END" ) //We have reached the end block
  102. start = false;
  103. else if( res == "DATA" ) //After Data will be a list of remote names
  104. {
  105. start = true;
  106. data = true;
  107. continue;
  108. }
  109. //Have we gotten the DATA word yet?
  110. if( start == false )
  111. continue;
  112. if( data ) //How many?
  113. mCount = atoi(res.c_str());
  114. else //What follows should now be a list of remote names
  115. mUnusedRemotes.push_back(res);
  116. data = false;
  117. }
  118. //Read information about each remote
  119. boost::asio::streambuf buffer2;
  120. for( int i = 0; i < mCount; ++i )
  121. {
  122. std::ostringstream istr;
  123. istr << "LIST " << mUnusedRemotes[i] << "\n";
  124. mWrapped->mSocket.write_some(boost::asio::buffer(istr.str()));
  125. RemoteInfo information;
  126. int buttonCount = 0;
  127. start = data = false;
  128. for(;;)
  129. {
  130. boost::asio::read_until(mWrapped->mSocket, buffer, '\n');
  131. std::istream str(&buffer);
  132. std::string res;
  133. str >> res;
  134. if( res == "" ) //If nothing left, we are done
  135. break;
  136. else if( res == "ERROR" ) //If error, bail out
  137. return;
  138. else if( res == "END" ) //We have reached the end block
  139. start = false;
  140. else if( res == "DATA" ) //After Data will be button count
  141. {
  142. start = true;
  143. data = true;
  144. continue;
  145. }
  146. //Have we gotten the DATA word yet?
  147. if( start == false )
  148. continue;
  149. if( data ) //After button count, there will be a list of button names
  150. information.buttons = atoi(res.c_str());
  151. else
  152. information.buttonMap[res] = buttonCount++;
  153. data = false;
  154. }
  155. mJoyStickInformation[mUnusedRemotes[i]] = information;
  156. }
  157. }
  158. //---------------------------------------------------------------------------------//
  159. void LIRCFactoryCreator::enableConnection(bool enable, bool blocking)
  160. {
  161. if( enable == true && mConnected == false )
  162. {
  163. boost::asio::ip::tcp::resolver resolver(mWrapped->mIOService);
  164. boost::asio::ip::tcp::resolver::query query(mIP, mPort);
  165. boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  166. boost::asio::ip::tcp::resolver::iterator end;
  167. //Connect (trying all found connections - ip4/ip6)
  168. boost::asio::error result = boost::asio::error::host_not_found;
  169. while (result && endpoint_iterator != end)
  170. {
  171. mWrapped->mSocket.close();
  172. mWrapped->mSocket.connect(*endpoint_iterator++, boost::asio::assign_error(result));
  173. }
  174. if (result != boost::asio::error::success)
  175. throw (result);
  176. if( blocking == false )
  177. {
  178. mWrapped->mSocket.io_control(boost::asio::socket_base::non_blocking_io(true));
  179. }
  180. mConnected = true;
  181. }
  182. else if( enable == false )
  183. {
  184. mWrapped->mSocket.close();
  185. mConnected = false;
  186. }
  187. }
  188. //---------------------------------------------------------------------------------//
  189. void LIRCFactoryCreator::enableConnectionThread(bool enable)
  190. {
  191. if( enable == true && mThreadRunning == false )
  192. {
  193. mThreadRunning = true;
  194. mWrapped->mThreadHandler = new boost::thread(boost::bind(&LIRCFactoryCreator::threadUpdate, this));
  195. }
  196. else if( enable == false && mThreadRunning == true )
  197. {
  198. mThreadRunning = false;
  199. mWrapped->mThreadHandler->join();
  200. delete mWrapped->mThreadHandler;
  201. mWrapped->mThreadHandler = 0;
  202. }
  203. }
  204. //---------------------------------------------------------------------------------//
  205. void LIRCFactoryCreator::threadUpdate()
  206. {
  207. boost::xtime timer;
  208. boost::asio::streambuf buffer;
  209. std::istream stream(&buffer);
  210. std::string code, repeat, button, remote;
  211. while( mThreadRunning )
  212. {
  213. try
  214. {
  215. while( mWrapped->mSocket.in_avail() > 0 )
  216. {
  217. boost::asio::read_until(mWrapped->mSocket, buffer, '\n');
  218. stream >> code; //64 bit value, ignorable
  219. stream >> repeat; //Repeat rate starting at zero (we ignore, for now)
  220. stream >> button; //Button name
  221. stream >> remote; //Remote name
  222. { //Lock object, find out which remote sent event
  223. boost::mutex::scoped_lock arrayLock(mWrapped->mLircListMutex);
  224. std::map<std::string, LIRCControl*>::iterator i = mUpdateRemotes.find(remote);
  225. if( i != mUpdateRemotes.end() )
  226. {
  227. i->second->queueButtonPressed(button);
  228. }
  229. }
  230. }
  231. }
  232. catch(...)
  233. { //Hmm, what should we do if we get a socket error here.. Ignore it I suppose,
  234. } //and wait till the used remote objects get shutdown. We could try to
  235. //reconnect, but how do we know if we will even get the same remotes.
  236. boost::xtime_get(&timer, boost::TIME_UTC);
  237. timer.nsec += 300000000; // 100 000 000 ~= .3 sec
  238. boost::thread::sleep(timer);
  239. }
  240. }
  241. //---------------------------------------------------------------------------------//
  242. DeviceList LIRCFactoryCreator::freeDeviceList()
  243. {
  244. DeviceList list;
  245. for( std::vector<std::string>::iterator i = mUnusedRemotes.begin(); i != mUnusedRemotes.end(); ++i )
  246. list.insert(std::make_pair(OISJoyStick, *i));
  247. return list;
  248. }
  249. //---------------------------------------------------------------------------------//
  250. int LIRCFactoryCreator::totalDevices(Type iType)
  251. {
  252. if( iType == OISJoyStick )
  253. return mCount;
  254. else
  255. return 0;
  256. }
  257. //---------------------------------------------------------------------------------//
  258. int LIRCFactoryCreator::freeDevices(Type iType)
  259. {
  260. if( iType == OISJoyStick )
  261. return (int)mUnusedRemotes.size();
  262. else
  263. return 0;
  264. }
  265. //---------------------------------------------------------------------------------//
  266. bool LIRCFactoryCreator::vendorExist(Type iType, const std::string & vendor)
  267. {
  268. if( iType == OISJoyStick && std::find(mUnusedRemotes.begin(), mUnusedRemotes.end(), vendor) != mUnusedRemotes.end() )
  269. return true;
  270. else
  271. return false;
  272. }
  273. //---------------------------------------------------------------------------------//
  274. Object* LIRCFactoryCreator::createObject(InputManager* creator, Type iType, bool bufferMode, const std::string & vendor)
  275. {
  276. if( mUnusedRemotes.size() > 0 )
  277. {
  278. std::vector<std::string>::iterator remote = mUnusedRemotes.end();
  279. if( vendor == "" )
  280. remote = mUnusedRemotes.begin();
  281. else
  282. remote = std::find(mUnusedRemotes.begin(), mUnusedRemotes.end(), vendor);
  283. if( remote != mUnusedRemotes.end() )
  284. {
  285. //Make sure connection is established
  286. enableConnection(true, false);
  287. //Make sure connection thread is alive
  288. enableConnectionThread(true);
  289. //Create device
  290. LIRCControl *obj = new LIRCControl(creator, 0, bufferMode, this, mJoyStickInformation[*remote]);
  291. //Add to used list, and then remove from unused list
  292. {
  293. boost::mutex::scoped_lock arrayLock(mWrapped->mLircListMutex);
  294. mUpdateRemotes[*remote] = obj;
  295. }
  296. mUnusedRemotes.erase(remote);
  297. return obj;
  298. }
  299. }
  300. OIS_EXCEPT(E_InputDeviceNonExistant, "No Device found which matches description!");
  301. }
  302. //---------------------------------------------------------------------------------//
  303. void LIRCFactoryCreator::destroyObject(Object* obj)
  304. {
  305. if( obj == 0 )
  306. return;
  307. int remotes_alive = 0;
  308. { //Scope lock
  309. boost::mutex::scoped_lock arrayLock(mWrapped->mLircListMutex);
  310. //Find object
  311. std::map<std::string, LIRCControl*>::iterator i = mUpdateRemotes.begin(), e = mUpdateRemotes.end();
  312. bool found = false;
  313. for(; i != e; ++i)
  314. {
  315. if( i->second == obj )
  316. {
  317. found = true;
  318. break;
  319. }
  320. }
  321. if( found == false )
  322. OIS_EXCEPT(E_General, "Device not found in LIRC remote collection!");
  323. //Move from used to unused list
  324. mUnusedRemotes.push_back(i->first);
  325. mUpdateRemotes.erase(i);
  326. delete obj;
  327. remotes_alive = (int)mUpdateRemotes.size();
  328. }
  329. //Destroy thread if no longer in use (we do this after unlocking mutex!)
  330. if( remotes_alive == 0 )
  331. enableConnectionThread(false);
  332. }
  333. #endif