PageRenderTime 49ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/EventClients/Clients/WiiRemote/CWIID_WiiRemote.cpp

https://gitlab.com/sloshedpuppie/LetsGoRetro
C++ | 776 lines | 606 code | 103 blank | 67 comment | 172 complexity | 1876944a6c93e15be3a869cdbce74c62 MD5 | raw file
  1. /*
  2. * Copyright (C) 2007 by Tobias Arrskog
  3. * topfs@tobias
  4. *
  5. * Copyright (C) 2007-2013 Team XBMC
  6. * http://xbmc.org
  7. *
  8. * This Program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This Program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with XBMC; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. // Compiles with g++ WiiRemote.cpp -lcwiid -o WiiRemote
  24. // Preferably with libcwiid >= 6.0
  25. #include "CWIID_WiiRemote.h"
  26. #include <unistd.h>
  27. bool g_AllowReconnect = true;
  28. bool g_AllowMouse = true;
  29. bool g_AllowNunchuck = true;
  30. CPacketHELO *g_Ping = NULL;
  31. #ifndef ICON_PATH
  32. #define ICON_PATH "../../"
  33. #endif
  34. std::string g_BluetoothIconPath = std::string(ICON_PATH) + std::string("/bluetooth.png");
  35. int32_t getTicks()
  36. {
  37. int32_t ticks;
  38. struct timeval now;
  39. gettimeofday(&now, NULL);
  40. ticks = now.tv_sec * 1000l;
  41. ticks += now.tv_usec / 1000l;
  42. return ticks;
  43. }
  44. CWiiRemote g_WiiRemote;
  45. #ifdef CWIID_OLD
  46. void CWiiRemote::MessageCallback(cwiid_wiimote_t *wiiremote, int mesg_count, union cwiid_mesg mesg[])
  47. {
  48. MessageCallback(wiiremote, mesg_count, mesg, NULL);
  49. }
  50. #endif
  51. /* The MessageCallback for the Wiiremote.
  52. This callback is used for error reports, mainly to see if the connection has been broken
  53. This callback is also used for getting the IR sources, if this is done in update as with buttons we usually only get 1 IR source at a time wich is much harder to calculate */
  54. void CWiiRemote::MessageCallback(cwiid_wiimote_t *wiiremote, int mesg_count, union cwiid_mesg mesg[], struct timespec *timestamp)
  55. {
  56. for (int i=0; i < mesg_count; i++)
  57. {
  58. int valid_source;
  59. switch (mesg[i].type)
  60. {
  61. case CWIID_MESG_IR:
  62. valid_source = 0;
  63. for (int j = 0; j < CWIID_IR_SRC_COUNT; j++)
  64. {
  65. if (mesg[i].ir_mesg.src[j].valid)
  66. valid_source++;
  67. }
  68. if (valid_source == 2)
  69. {
  70. g_WiiRemote.CalculateMousePointer(mesg[i].ir_mesg.src[0].pos[CWIID_X],
  71. mesg[i].ir_mesg.src[0].pos[CWIID_Y],
  72. mesg[i].ir_mesg.src[1].pos[CWIID_X],
  73. mesg[i].ir_mesg.src[1].pos[CWIID_Y]);
  74. }
  75. else if (valid_source > 2)
  76. { //TODO Make this care with the strength of the sources
  77. g_WiiRemote.CalculateMousePointer(mesg[i].ir_mesg.src[0].pos[CWIID_X],
  78. mesg[i].ir_mesg.src[0].pos[CWIID_Y],
  79. mesg[i].ir_mesg.src[1].pos[CWIID_X],
  80. mesg[i].ir_mesg.src[1].pos[CWIID_Y]);
  81. }
  82. break;
  83. case CWIID_MESG_ERROR:
  84. g_WiiRemote.DisconnectNow(true);
  85. break;
  86. case CWIID_MESG_BTN:
  87. g_WiiRemote.ProcessKey(mesg[i].btn_mesg.buttons);
  88. break;
  89. case CWIID_MESG_STATUS:
  90. //Here we can figure out Extension types and such
  91. break;
  92. case CWIID_MESG_NUNCHUK:
  93. g_WiiRemote.ProcessNunchuck(mesg[i].nunchuk_mesg);
  94. break;
  95. case CWIID_MESG_ACC:
  96. case CWIID_MESG_BALANCE:
  97. case CWIID_MESG_CLASSIC:
  98. case CWIID_MESG_MOTIONPLUS:
  99. //Not implemented
  100. break;
  101. case CWIID_MESG_UNKNOWN:
  102. //...
  103. break;
  104. }
  105. }
  106. #ifdef CWIID_OLD
  107. g_WiiRemote.CheckIn();
  108. #endif
  109. }
  110. #ifndef _DEBUG
  111. /* This takes the errors generated at pre-connect and silence them as they are mostly not needed */
  112. void CWiiRemote::ErrorCallback(struct wiimote *wiiremote, const char *str, va_list ap)
  113. {
  114. //Error checking
  115. }
  116. #endif
  117. //Constructor
  118. /*This constructor is never used but it shows how one would connect to just a specific Wiiremote by Mac-Adress*/
  119. CWiiRemote::CWiiRemote(char *wii_btaddr)
  120. {
  121. SetBluetoothAddress(wii_btaddr);
  122. m_SamplesX = NULL;
  123. m_SamplesY = NULL;
  124. m_JoyMap = NULL;
  125. }
  126. //Destructor
  127. CWiiRemote::~CWiiRemote()
  128. {
  129. if (m_connected == true)
  130. this->DisconnectNow(false);
  131. if (m_SamplesY != NULL)
  132. free(m_SamplesY);
  133. if (m_SamplesX != NULL)
  134. free(m_SamplesX);
  135. if (m_JoyMap)
  136. free(m_JoyMap);
  137. }
  138. //---------------------Public-------------------------------------------------------------------
  139. /* Basicly this just sets up standard control bits */
  140. void CWiiRemote::SetBluetoothAddress(const char *btaddr)
  141. {
  142. static const bdaddr_t b = {{0, 0, 0, 0, 0, 0}}; /* BDADDR_ANY */
  143. if (btaddr != NULL)
  144. str2ba(btaddr, &m_btaddr);
  145. else
  146. bacpy(&m_btaddr, &b);
  147. }
  148. void CWiiRemote::SetSensativity(float DeadX, float DeadY, int NumSamples)
  149. {
  150. m_NumSamples = NumSamples;
  151. m_MinX = MOUSE_MAX * DeadX;
  152. m_MinY = MOUSE_MAX * DeadY;
  153. m_MaxX = MOUSE_MAX * (1.0f + DeadX + DeadX);
  154. m_MaxY = MOUSE_MAX * (1.0f + DeadY + DeadY);
  155. if (m_SamplesY != NULL)
  156. delete [] m_SamplesY;
  157. if (m_SamplesX != NULL)
  158. delete [] m_SamplesX;
  159. m_SamplesY = new int[m_NumSamples];
  160. m_SamplesX = new int[m_NumSamples];
  161. }
  162. void CWiiRemote::SetJoystickMap(const char *JoyMap)
  163. {
  164. if (m_JoyMap)
  165. free(m_JoyMap);
  166. if (JoyMap != NULL)
  167. {
  168. m_JoyMap = (char*)malloc(strlen(JoyMap) + 5);
  169. sprintf(m_JoyMap, "JS0:%s", JoyMap);
  170. }
  171. else
  172. m_JoyMap = strdup("JS0:WiiRemote");
  173. }
  174. void CWiiRemote::Initialize(CAddress Addr, int Socket)
  175. {
  176. m_connected = false;
  177. m_lastKeyPressed = 0;
  178. m_LastKey = 0;
  179. m_buttonRepeat = false;
  180. m_lastKeyPressedNunchuck = 0;
  181. m_LastKeyNunchuck = 0;
  182. m_buttonRepeatNunchuck = false;
  183. m_useIRMouse = true;
  184. m_rptMode = 0;
  185. m_Socket = Socket;
  186. m_MyAddr = Addr;
  187. m_NumSamples = WIIREMOTE_SAMPLES;
  188. m_MaxX = WIIREMOTE_X_MAX;
  189. m_MaxY = WIIREMOTE_Y_MAX;
  190. m_MinX = WIIREMOTE_X_MIN;
  191. m_MinY = WIIREMOTE_Y_MIN;
  192. #ifdef CWIID_OLD
  193. m_LastMsgTime = getTicks();
  194. #endif
  195. //All control bits are set to false when cwiid is started
  196. //Report Button presses
  197. ToggleBit(m_rptMode, CWIID_RPT_BTN);
  198. if (g_AllowNunchuck)
  199. ToggleBit(m_rptMode, CWIID_RPT_NUNCHUK);
  200. //If wiiremote is used as a mouse, then report the IR sources
  201. #ifndef CWIID_OLD
  202. if (m_useIRMouse)
  203. #endif
  204. ToggleBit(m_rptMode, CWIID_RPT_IR);
  205. //Have the first and fourth LED on the Wiiremote shine when connected
  206. ToggleBit(m_ledState, CWIID_LED1_ON);
  207. ToggleBit(m_ledState, CWIID_LED4_ON);
  208. }
  209. /* Update is run regularly and we gather the state of the Wiiremote and see if the user have pressed on a button or moved the wiiremote
  210. This could have been done with callbacks instead but it doesn't look nice in C++*/
  211. void CWiiRemote::Update()
  212. {
  213. if (m_DisconnectWhenPossible)
  214. {//If the user has chosen to disconnect or lost communication
  215. DisconnectNow(true);
  216. m_DisconnectWhenPossible = false;
  217. }
  218. #ifdef CWIID_OLD
  219. if(m_connected)
  220. {//Here we check if the connection is suddenly broken
  221. if (!CheckConnection())
  222. {
  223. DisconnectNow(true);
  224. return;
  225. }
  226. }
  227. #endif
  228. }
  229. /* Enable mouse emulation */
  230. void CWiiRemote::EnableMouseEmulation()
  231. {
  232. if (m_useIRMouse)
  233. return;
  234. m_useIRMouse = true;
  235. #ifndef CWIID_OLD
  236. //We toggle IR Reporting (Save resources?)
  237. if (!(m_rptMode & CWIID_RPT_IR))
  238. ToggleBit(m_rptMode, CWIID_RPT_IR);
  239. if (m_connected)
  240. SetRptMode();
  241. #endif
  242. CPacketLOG log(LOGDEBUG, "Enabled WiiRemote mouse emulation");
  243. log.Send(m_Socket, m_MyAddr);
  244. }
  245. /* Disable mouse emulation */
  246. void CWiiRemote::DisableMouseEmulation()
  247. {
  248. if (!m_useIRMouse)
  249. return;
  250. m_useIRMouse = false;
  251. #ifndef CWIID_OLD
  252. //We toggle IR Reporting (Save resources?)
  253. if (m_rptMode & CWIID_RPT_IR)
  254. ToggleBit(m_rptMode, CWIID_RPT_IR);
  255. if (m_connected)
  256. SetRptMode();
  257. #endif
  258. CPacketLOG log(LOGDEBUG, "Disabled WiiRemote mouse emulation");
  259. log.Send(m_Socket, m_MyAddr);
  260. }
  261. /* Is a wiiremote connected*/
  262. bool CWiiRemote::GetConnected()
  263. {
  264. return m_connected;
  265. }
  266. /* Disconnect ASAP*/
  267. void CWiiRemote::Disconnect()
  268. { //This is always called from a criticalsection
  269. if (m_connected)
  270. m_DisconnectWhenPossible = true;
  271. }
  272. #ifdef CWIID_OLD
  273. /* This function is mostly a hack as CWIID < 6.0 doesn't report on disconnects, this function is called everytime
  274. a message is sent to the callback (Will be once every 10 ms or so) this is to see if the connection is interrupted. */
  275. void CWiiRemote::CheckIn()
  276. { //This is always called from a criticalsection
  277. m_LastMsgTime = getTicks();
  278. }
  279. #endif
  280. //---------------------Private-------------------------------------------------------------------
  281. /* Connect is designed to be run in a different thread as it only
  282. exits if wiiremote is either disabled or a connection is made*/
  283. bool CWiiRemote::Connect()
  284. {
  285. #ifndef _DEBUG
  286. cwiid_set_err(ErrorCallback);
  287. #endif
  288. while (!m_connected)
  289. {
  290. g_Ping->Send(m_Socket, m_MyAddr);
  291. int flags = 0;
  292. ToggleBit(flags, CWIID_FLAG_MESG_IFC);
  293. ToggleBit(flags, CWIID_FLAG_REPEAT_BTN);
  294. m_wiiremoteHandle = cwiid_connect(&m_btaddr, flags);
  295. if (m_wiiremoteHandle != NULL)
  296. {
  297. SetupWiiRemote();
  298. // get battery state etc.
  299. cwiid_state wiiremote_state;
  300. int err = cwiid_get_state(m_wiiremoteHandle, &wiiremote_state);
  301. if (!err)
  302. {
  303. char Mesg[1024];
  304. sprintf(Mesg, "%i%% battery remaining", static_cast<int>(((float)(wiiremote_state.battery)/CWIID_BATTERY_MAX)*100.0));
  305. CPacketNOTIFICATION notification("Wii Remote connected", Mesg, ICON_PNG, g_BluetoothIconPath.c_str());
  306. notification.Send(m_Socket, m_MyAddr);
  307. }
  308. else
  309. {
  310. printf("Problem probing for status of WiiRemote; cwiid_get_state returned non-zero\n");
  311. CPacketLOG log(LOGNOTICE, "Problem probing for status of WiiRemote; cwiid_get_state returned non-zero");
  312. log.Send(m_Socket, m_MyAddr);
  313. CPacketNOTIFICATION notification("Wii Remote connected", "", ICON_PNG, g_BluetoothIconPath.c_str());
  314. notification.Send(m_Socket, m_MyAddr);
  315. }
  316. #ifdef CWIID_OLD
  317. /* CheckIn to say that this is the last msg, If this isn't called it could give issues if we Connects -> Disconnect and then try to connect again
  318. the CWIID_OLD hack would automatically disconnect the wiiremote as the lastmsg is too old. */
  319. CheckIn();
  320. #endif
  321. m_connected = true;
  322. CPacketLOG log(LOGNOTICE, "Successfully connected a WiiRemote");
  323. log.Send(m_Socket, m_MyAddr);
  324. return true;
  325. }
  326. //Here's a good place to have a quit flag check...
  327. }
  328. return false;
  329. }
  330. /* Disconnect */
  331. void CWiiRemote::DisconnectNow(bool startConnectThread)
  332. {
  333. if (m_connected) //It shouldn't be enabled at the same time as it is connected
  334. {
  335. cwiid_disconnect(m_wiiremoteHandle);
  336. if (g_AllowReconnect)
  337. {
  338. CPacketNOTIFICATION notification("Wii Remote disconnected", "Press 1 and 2 to reconnect", ICON_PNG, g_BluetoothIconPath.c_str());
  339. notification.Send(m_Socket, m_MyAddr);
  340. }
  341. else
  342. {
  343. CPacketNOTIFICATION notification("Wii Remote disconnected", "", ICON_PNG, g_BluetoothIconPath.c_str());
  344. notification.Send(m_Socket, m_MyAddr);
  345. }
  346. CPacketLOG log(LOGNOTICE, "Successfully disconnected a WiiRemote");
  347. log.Send(m_Socket, m_MyAddr);
  348. }
  349. m_connected = false;
  350. }
  351. #ifdef CWIID_OLD
  352. /* This is a harsh check if there really is a connection, It will mainly be used in CWIID < 6.0
  353. as it doesn't report connect error, wich is needed to see if the Wiiremote suddenly disconnected.
  354. This could possible be done with bluetooth specific queries but I cannot find how to do it. */
  355. bool CWiiRemote::CheckConnection()
  356. {
  357. if ((getTicks() - m_LastMsgTime) > 1000)
  358. {
  359. CPacketLOG log(LOGNOTICE, "Lost connection to the WiiRemote");
  360. log.Send(m_Socket, m_MyAddr);
  361. return false;
  362. }
  363. else
  364. return true;
  365. }
  366. #endif
  367. /* Sets rpt mode when a new wiiremote is connected */
  368. void CWiiRemote::SetupWiiRemote()
  369. { //Lights up the appropriate led and setups the rapport mode, so buttons and IR work
  370. SetRptMode();
  371. SetLedState();
  372. for (int i = 0; i < WIIREMOTE_SAMPLES; i++)
  373. {
  374. m_SamplesX[i] = 0;
  375. m_SamplesY[i] = 0;
  376. }
  377. if (cwiid_set_mesg_callback(m_wiiremoteHandle, MessageCallback))
  378. {
  379. CPacketLOG log(LOGERROR, "Unable to set message callback to the WiiRemote");
  380. log.Send(m_Socket, m_MyAddr);
  381. }
  382. }
  383. void CWiiRemote::ProcessKey(int Key)
  384. {
  385. if (Key != m_LastKey)
  386. {
  387. m_LastKey = Key;
  388. m_lastKeyPressed = getTicks();
  389. m_buttonRepeat = false;
  390. }
  391. else
  392. {
  393. if (m_buttonRepeat)
  394. {
  395. if (getTicks() - m_lastKeyPressed > WIIREMOTE_BUTTON_REPEAT_TIME)
  396. m_lastKeyPressed = getTicks();
  397. else
  398. return;
  399. }
  400. else
  401. {
  402. if (getTicks() - m_lastKeyPressed > WIIREMOTE_BUTTON_DELAY_TIME)
  403. {
  404. m_buttonRepeat = true;
  405. m_lastKeyPressed = getTicks();
  406. }
  407. else
  408. return;
  409. }
  410. }
  411. int RtnKey = -1;
  412. if (Key == CWIID_BTN_UP)
  413. RtnKey = 1;
  414. else if (Key == CWIID_BTN_RIGHT)
  415. RtnKey = 4;
  416. else if (Key == CWIID_BTN_LEFT)
  417. RtnKey = 3;
  418. else if (Key == CWIID_BTN_DOWN)
  419. RtnKey = 2;
  420. else if (Key == CWIID_BTN_A)
  421. RtnKey = 5;
  422. else if (Key == CWIID_BTN_B)
  423. RtnKey = 6;
  424. else if (Key == CWIID_BTN_MINUS)
  425. RtnKey = 7;
  426. else if (Key == CWIID_BTN_PLUS)
  427. RtnKey = 9;
  428. else if (Key == CWIID_BTN_HOME)
  429. RtnKey = 8;
  430. else if (Key == CWIID_BTN_1)
  431. RtnKey = 10;
  432. else if (Key == CWIID_BTN_2)
  433. RtnKey = 11;
  434. if (RtnKey != -1)
  435. {
  436. CPacketBUTTON btn(RtnKey, m_JoyMap, BTN_QUEUE | BTN_NO_REPEAT);
  437. btn.Send(m_Socket, m_MyAddr);
  438. }
  439. }
  440. void CWiiRemote::ProcessNunchuck(struct cwiid_nunchuk_mesg &Nunchuck)
  441. {
  442. if (Nunchuck.stick[0] > 135)
  443. { //R
  444. int x = (int)((((float)Nunchuck.stick[0] - 135.0f) / 95.0f) * 65535.0f);
  445. printf("Right: %i\n", x);
  446. CPacketBUTTON btn(24, m_JoyMap, (BTN_QUEUE | BTN_DOWN), x);
  447. btn.Send(m_Socket, m_MyAddr);
  448. }
  449. else if (Nunchuck.stick[0] < 125)
  450. { //L
  451. int x = (int)((((float)Nunchuck.stick[0] - 125.0f) / 90.0f) * -65535.0f);
  452. printf("Left: %i\n", x);
  453. CPacketBUTTON btn(23, m_JoyMap, (BTN_QUEUE | BTN_DOWN), x);
  454. btn.Send(m_Socket, m_MyAddr);
  455. }
  456. if (Nunchuck.stick[1] > 130)
  457. { //U
  458. int x = (int)((((float)Nunchuck.stick[1] - 130.0f) / 92.0f) * 65535.0f);
  459. printf("Up: %i\n", x);
  460. CPacketBUTTON btn(21, m_JoyMap, (BTN_QUEUE | BTN_DOWN), x);
  461. btn.Send(m_Socket, m_MyAddr);
  462. }
  463. else if (Nunchuck.stick[1] < 120)
  464. { //D
  465. int x = (int)((((float)Nunchuck.stick[1] - 120.0f) / 90.0f) * -65535.0f);
  466. printf("Down: %i\n", x);
  467. CPacketBUTTON btn(22, m_JoyMap, (BTN_QUEUE | BTN_DOWN), x);
  468. btn.Send(m_Socket, m_MyAddr);
  469. }
  470. if (Nunchuck.buttons != m_LastKeyNunchuck)
  471. {
  472. m_LastKeyNunchuck = Nunchuck.buttons;
  473. m_lastKeyPressedNunchuck = getTicks();
  474. m_buttonRepeatNunchuck = false;
  475. }
  476. else
  477. {
  478. if (m_buttonRepeatNunchuck)
  479. {
  480. if (getTicks() - m_lastKeyPressedNunchuck > WIIREMOTE_BUTTON_REPEAT_TIME)
  481. m_lastKeyPressedNunchuck = getTicks();
  482. else
  483. return;
  484. }
  485. else
  486. {
  487. if (getTicks() - m_lastKeyPressedNunchuck > WIIREMOTE_BUTTON_DELAY_TIME)
  488. {
  489. m_buttonRepeatNunchuck = true;
  490. m_lastKeyPressedNunchuck = getTicks();
  491. }
  492. else
  493. return;
  494. }
  495. }
  496. int RtnKey = -1;
  497. if (Nunchuck.buttons == CWIID_NUNCHUK_BTN_C)
  498. RtnKey = 25;
  499. else if (Nunchuck.buttons == CWIID_NUNCHUK_BTN_Z)
  500. RtnKey = 26;
  501. if (RtnKey != -1)
  502. {
  503. CPacketBUTTON btn(RtnKey, m_JoyMap, BTN_QUEUE | BTN_NO_REPEAT);
  504. btn.Send(m_Socket, m_MyAddr);
  505. }
  506. }
  507. /* Tell cwiid wich data will be reported */
  508. void CWiiRemote::SetRptMode()
  509. { //Sets our wiiremote to report something, for example IR, Buttons
  510. #ifdef CWIID_OLD
  511. if (cwiid_command(m_wiiremoteHandle, CWIID_CMD_RPT_MODE, m_rptMode))
  512. #else
  513. if (cwiid_set_rpt_mode(m_wiiremoteHandle, m_rptMode))
  514. #endif
  515. {
  516. CPacketLOG log(LOGERROR, "Error setting WiiRemote report mode");
  517. log.Send(m_Socket, m_MyAddr);
  518. }
  519. }
  520. /* Tell cwiid the LED states */
  521. void CWiiRemote::SetLedState()
  522. { //Sets our leds on the wiiremote
  523. #ifdef CWIID_OLD
  524. if (cwiid_command(m_wiiremoteHandle, CWIID_CMD_LED, m_ledState))
  525. #else
  526. if (cwiid_set_led(m_wiiremoteHandle, m_ledState))
  527. #endif
  528. {
  529. CPacketLOG log(LOGERROR, "Error setting WiiRemote LED state");
  530. log.Send(m_Socket, m_MyAddr);
  531. }
  532. }
  533. /* Calculate the mousepointer from 2 IR sources (Default) */
  534. void CWiiRemote::CalculateMousePointer(int x1, int y1, int x2, int y2)
  535. {
  536. int x3, y3;
  537. x3 = ( (x1 + x2) / 2 );
  538. y3 = ( (y1 + y2) / 2 );
  539. x3 = (int)( ((float)x3 / (float)CWIID_IR_X_MAX) * m_MaxX);
  540. y3 = (int)( ((float)y3 / (float)CWIID_IR_Y_MAX) * m_MaxY);
  541. x3 = (int)(x3 - m_MinX);
  542. y3 = (int)(y3 - m_MinY);
  543. if (x3 < MOUSE_MIN) x3 = MOUSE_MIN;
  544. else if (x3 > MOUSE_MAX) x3 = MOUSE_MAX;
  545. if (y3 < MOUSE_MIN) y3 = MOUSE_MIN;
  546. else if (y3 > MOUSE_MAX) y3 = MOUSE_MAX;
  547. x3 = MOUSE_MAX - x3;
  548. if (m_NumSamples == 1)
  549. {
  550. CPacketMOUSE mouse(x3, y3);
  551. mouse.Send(m_Socket, m_MyAddr);
  552. return;
  553. }
  554. else
  555. {
  556. for (int i = m_NumSamples; i > 0; i--)
  557. {
  558. m_SamplesX[i] = m_SamplesX[i-1];
  559. m_SamplesY[i] = m_SamplesY[i-1];
  560. }
  561. m_SamplesX[0] = x3;
  562. m_SamplesY[0] = y3;
  563. long x4 = 0, y4 = 0;
  564. for (int i = 0; i < m_NumSamples; i++)
  565. {
  566. x4 += m_SamplesX[i];
  567. y4 += m_SamplesY[i];
  568. }
  569. CPacketMOUSE mouse((x4 / m_NumSamples), (y4 / m_NumSamples));
  570. mouse.Send(m_Socket, m_MyAddr);
  571. }
  572. }
  573. void PrintHelp(const char *Prog)
  574. {
  575. printf("Commands:\n");
  576. printf("\t--disable-mouseemulation\n\t--disable-reconnect\n\t--disable-nunchuck\n");
  577. printf("\t--address ADDRESS\n\t--port PORT\n");
  578. printf("\t--btaddr MACADDRESS\n");
  579. printf("\t--deadzone-x DEADX | Number between 0 - 100 (Default: %i)\n", (int)(DEADZONE_X * 100));
  580. printf("\t--deadzone-y DEADY | Number between 0 - 100 (Default: %i)\n", (int)(DEADZONE_Y * 100));
  581. printf("\t--deadzone DEAD | Sets both X and Y too the number\n");
  582. printf("\t--smoothing-samples SAMPLE | Number 1 counts as Off (Default: %i)\n", WIIREMOTE_SAMPLES);
  583. printf("\t--joystick-map JOYMAP | The string ID for the joymap (Default: WiiRemote)\n");
  584. }
  585. int main(int argc, char **argv)
  586. {
  587. char *Address = NULL;
  588. char *btaddr = NULL;
  589. int Port = 9777;
  590. int NumSamples = WIIREMOTE_SAMPLES;
  591. float DeadX = DEADZONE_X;
  592. float DeadY = DEADZONE_Y;
  593. char *JoyMap = NULL;
  594. for (int i = 0; i < argc; i++)
  595. {
  596. if (strcmp(argv[i], "--help") == 0)
  597. {
  598. PrintHelp(argv[0]);
  599. return 0;
  600. }
  601. else if (strcmp(argv[i], "--disable-mouseemulation") == 0)
  602. g_AllowMouse = false;
  603. else if (strcmp(argv[i], "--disable-reconnect") == 0)
  604. g_AllowReconnect = false;
  605. else if (strcmp(argv[i], "--disable-nunchuck") == 0)
  606. g_AllowNunchuck = false;
  607. else if (strcmp(argv[i], "--address") == 0 && ((i + 1) <= argc))
  608. Address = argv[i + 1];
  609. else if (strcmp(argv[i], "--port") == 0 && ((i + 1) <= argc))
  610. Port = atoi(argv[i + 1]);
  611. else if (strcmp(argv[i], "--btaddr") == 0 && ((i + 1) <= argc))
  612. btaddr = argv[i + 1];
  613. else if (strcmp(argv[i], "--deadzone-x") == 0 && ((i + 1) <= argc))
  614. DeadX = ((float)atoi(argv[i + 1]) / 100.0f);
  615. else if (strcmp(argv[i], "--deadzone-y") == 0 && ((i + 1) <= argc))
  616. DeadY = ((float)atoi(argv[i + 1]) / 100.0f);
  617. else if (strcmp(argv[i], "--deadzone") == 0 && ((i + 1) <= argc))
  618. DeadX = DeadY = ((float)atoi(argv[i + 1]) / 100.0f);
  619. else if (strcmp(argv[i], "--smoothing-samples") == 0 && ((i + 1) <= argc))
  620. NumSamples = atoi(argv[i + 1]);
  621. else if (strcmp(argv[i], "--joystick-map") == 0 && ((i + 1) <= argc))
  622. JoyMap = argv[i + 1];
  623. }
  624. if (NumSamples < 1 || DeadX < 0 || DeadY < 0 || DeadX > 1 || DeadY > 1)
  625. {
  626. PrintHelp(argv[0]);
  627. return -1;
  628. }
  629. CAddress my_addr(Address, Port); // Address => localhost on 9777
  630. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  631. if (sockfd < 0)
  632. {
  633. printf("Error creating socket\n");
  634. return -1;
  635. }
  636. if (hci_get_route(NULL) < 0)
  637. {
  638. CPacketLOG log(LOGERROR, "Error No bluetooth device");
  639. log.Send(sockfd, my_addr);
  640. return -1;
  641. }
  642. g_Ping = new CPacketHELO("WiiRemote", ICON_PNG, g_BluetoothIconPath.c_str());
  643. g_WiiRemote.Initialize(my_addr, sockfd);
  644. g_WiiRemote.SetBluetoothAddress(btaddr);
  645. g_WiiRemote.SetSensativity(DeadX, DeadY, NumSamples);
  646. g_WiiRemote.SetSensativity(DeadX, DeadY, NumSamples);
  647. g_WiiRemote.SetJoystickMap(JoyMap);
  648. if (g_AllowMouse)
  649. g_WiiRemote.EnableMouseEmulation();
  650. else
  651. g_WiiRemote.DisableMouseEmulation();
  652. g_Ping->Send(sockfd, my_addr);
  653. bool HaveConnected = false;
  654. while (true)
  655. {
  656. bool Connected = g_WiiRemote.GetConnected();
  657. while (!Connected)
  658. {
  659. if (HaveConnected && !g_AllowReconnect)
  660. exit(0);
  661. Connected = g_WiiRemote.Connect();
  662. HaveConnected = true;
  663. }
  664. #ifdef CWIID_OLD
  665. // Update the state of the WiiRemote more often when we have the old lib due too it not telling when disconnected..
  666. sleep (5);
  667. #else
  668. sleep (15);
  669. #endif
  670. g_Ping->Send(sockfd, my_addr);
  671. g_WiiRemote.Update();
  672. }
  673. }