Advertisement
EliteAnax17

new wiimoteemu

Jul 9th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.31 KB | None | 0 0
  1. // Copyright 2010 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4.  
  5. #include <cmath>
  6.  
  7. #include <winsock2.h>
  8. #include <WS2tcpip.h>
  9. #include <Windows.h>
  10.  
  11. #include "Common/ChunkFile.h"
  12. #include "Common/CommonTypes.h"
  13. #include "Common/Timer.h"
  14. #include "Core/ConfigManager.h"
  15. #include "Core/Host.h"
  16. #include "Core/Movie.h"
  17. #include "Core/NetPlayClient.h"
  18. #include "Core/HW/WiimoteEmu/MatrixMath.h"
  19. #include "Core/HW/WiimoteEmu/WiimoteEmu.h"
  20. #include "Core/HW/WiimoteEmu/WiimoteHid.h"
  21. #include "Core/HW/WiimoteEmu/Attachment/Classic.h"
  22. #include "Core/HW/WiimoteEmu/Attachment/Drums.h"
  23. #include "Core/HW/WiimoteEmu/Attachment/Guitar.h"
  24. #include "Core/HW/WiimoteEmu/Attachment/Nunchuk.h"
  25. #include "Core/HW/WiimoteEmu/Attachment/Turntable.h"
  26. #include "Core/HW/WiimoteReal/WiimoteReal.h"
  27.  
  28. namespace
  29. {
  30. // :)
  31. auto const TAU = 6.28318530717958647692;
  32. auto const PI = TAU / 2.0;
  33. }
  34.  
  35. namespace WiimoteEmu
  36. {
  37.  
  38. void test(int argc, const char*argv)
  39. {
  40. int sockfd, n;
  41. struct sockaddr_in servaddr, cliaddr;
  42. char sendline[1000];
  43. char recvline[1000];
  44.  
  45. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  46.  
  47. memset(&servaddr, 0, sizeof(servaddr));
  48. servaddr.sin_family = AF_INET;
  49. servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  50. servaddr.sin_port = htons(7000);
  51.  
  52. memset(sendline, 0, 1000);
  53. memcpy(sendline, argv, strlen(argv));
  54.  
  55. int senddata = sendto(sockfd, sendline, strlen(sendline), 0,
  56. (struct sockaddr *)&servaddr, sizeof(servaddr));
  57. }
  58.  
  59. static const u8 eeprom_data_0[] = {
  60. // IR, maybe more
  61. // assuming last 2 bytes are checksum
  62. 0xA1, 0xAA, 0x8B, 0x99, 0xAE, 0x9E, 0x78, 0x30, 0xA7, /*0x74, 0xD3,*/ 0x00, 0x00, // messing up the checksum on purpose
  63. 0xA1, 0xAA, 0x8B, 0x99, 0xAE, 0x9E, 0x78, 0x30, 0xA7, /*0x74, 0xD3,*/ 0x00, 0x00,
  64. // Accelerometer
  65. // Important: checksum is required for tilt games
  66. ACCEL_ZERO_G, ACCEL_ZERO_G, ACCEL_ZERO_G, 0, ACCEL_ONE_G, ACCEL_ONE_G, ACCEL_ONE_G, 0, 0, 0xA3,
  67. ACCEL_ZERO_G, ACCEL_ZERO_G, ACCEL_ZERO_G, 0, ACCEL_ONE_G, ACCEL_ONE_G, ACCEL_ONE_G, 0, 0, 0xA3,
  68. };
  69.  
  70. static const u8 motion_plus_id[] = { 0x00, 0x00, 0xA6, 0x20, 0x00, 0x05 };
  71.  
  72. static const u8 eeprom_data_16D0[] = {
  73. 0x00, 0x00, 0x00, 0xFF, 0x11, 0xEE, 0x00, 0x00,
  74. 0x33, 0xCC, 0x44, 0xBB, 0x00, 0x00, 0x66, 0x99,
  75. 0x77, 0x88, 0x00, 0x00, 0x2B, 0x01, 0xE8, 0x13
  76. };
  77.  
  78. static const ReportFeatures reporting_mode_features[] =
  79. {
  80. //0x30: Core Buttons
  81. { 2, 0, 0, 0, 4 },
  82. //0x31: Core Buttons and Accelerometer
  83. { 2, 4, 0, 0, 7 },
  84. //0x32: Core Buttons with 8 Extension bytes
  85. { 2, 0, 0, 4, 12 },
  86. //0x33: Core Buttons and Accelerometer with 12 IR bytes
  87. { 2, 4, 7, 0, 19 },
  88. //0x34: Core Buttons with 19 Extension bytes
  89. { 2, 0, 0, 4, 23 },
  90. //0x35: Core Buttons and Accelerometer with 16 Extension Bytes
  91. { 2, 4, 0, 7, 23 },
  92. //0x36: Core Buttons with 10 IR bytes and 9 Extension Bytes
  93. { 2, 0, 4, 14, 23 },
  94. //0x37: Core Buttons and Accelerometer with 10 IR bytes and 6 Extension Bytes
  95. { 2, 4, 7, 17, 23 },
  96.  
  97. // UNSUPPORTED:
  98. //0x3d: 21 Extension Bytes
  99. { 0, 0, 0, 2, 23 },
  100. //0x3e / 0x3f: Interleaved Core Buttons and Accelerometer with 36 IR bytes
  101. { 0, 0, 0, 0, 23 },
  102. };
  103.  
  104. void EmulateShake(AccelData* const accel
  105. , ControllerEmu::Buttons* const buttons_group
  106. , u8* const shake_step )
  107. {
  108. // frame count of one up/down shake
  109. // < 9 no shake detection in "Wario Land: Shake It"
  110. auto const shake_step_max = 15;
  111.  
  112. // peak G-force
  113. auto const shake_intensity = 3.0;
  114.  
  115. // shake is a bitfield of X,Y,Z shake button states
  116. static const unsigned int btns[] = { 0x01, 0x02, 0x04 };
  117. unsigned int shake = 0;
  118. buttons_group->GetState( &shake, btns );
  119.  
  120. for (int i = 0; i != 3; ++i)
  121. {
  122. if (shake & (1 << i))
  123. {
  124. (&(accel->x))[i] = std::sin(TAU * shake_step[i] / shake_step_max) * shake_intensity;
  125. shake_step[i] = (shake_step[i] + 1) % shake_step_max;
  126. }
  127. else
  128. shake_step[i] = 0;
  129. }
  130. }
  131.  
  132. void EmulateTilt(AccelData* const accel
  133. , ControllerEmu::Tilt* const tilt_group
  134. , const bool sideways, const bool upright)
  135. {
  136. ControlState roll, pitch;
  137. // 180 degrees
  138. tilt_group->GetState(&roll, &pitch);
  139.  
  140. roll *= PI;
  141. pitch *= PI;
  142.  
  143. unsigned int ud = 0, lr = 0, fb = 0;
  144.  
  145. // some notes that no one will understand but me :p
  146. // left, forward, up
  147. // lr/ left == negative for all orientations
  148. // ud/ up == negative for upright longways
  149. // fb/ forward == positive for (sideways flat)
  150.  
  151. // determine which axis is which direction
  152. ud = upright ? (sideways ? 0 : 1) : 2;
  153. lr = sideways;
  154. fb = upright ? 2 : (sideways ? 0 : 1);
  155.  
  156. int sgn[3]={-1,1,1}; //sign fix
  157.  
  158. if (sideways && !upright)
  159. sgn[fb] *= -1;
  160. if (!sideways && upright)
  161. sgn[ud] *= -1;
  162.  
  163. (&accel->x)[ud] = (sin((PI / 2) - std::max(fabs(roll), fabs(pitch))))*sgn[ud];
  164. (&accel->x)[lr] = -sin(roll)*sgn[lr];
  165. (&accel->x)[fb] = sin(pitch)*sgn[fb];
  166. }
  167.  
  168. #define SWING_INTENSITY 2.5//-uncalibrated(aprox) 0x40-calibrated
  169.  
  170. void EmulateSwing(AccelData* const accel
  171. , ControllerEmu::Force* const swing_group
  172. , const bool sideways, const bool upright)
  173. {
  174. ControlState swing[3];
  175. swing_group->GetState(swing);
  176.  
  177. s8 g_dir[3] = {-1, -1, -1};
  178. u8 axis_map[3];
  179.  
  180. // determine which axis is which direction
  181. axis_map[0] = upright ? (sideways ? 0 : 1) : 2; // up/down
  182. axis_map[1] = sideways; // left|right
  183. axis_map[2] = upright ? 2 : (sideways ? 0 : 1); // forward/backward
  184.  
  185. // some orientations have up as positive, some as negative
  186. // same with forward
  187. if (sideways && !upright)
  188. g_dir[axis_map[2]] *= -1;
  189. if (!sideways && upright)
  190. g_dir[axis_map[0]] *= -1;
  191.  
  192. for (unsigned int i = 0; i < 3; ++i)
  193. (&accel->x)[axis_map[i]] += swing[i] * g_dir[i] * SWING_INTENSITY;
  194. }
  195.  
  196. static const u16 button_bitmasks[] =
  197. {
  198. Wiimote::BUTTON_A,
  199. Wiimote::BUTTON_B,
  200. Wiimote::BUTTON_ONE,
  201. Wiimote::BUTTON_TWO,
  202. Wiimote::BUTTON_MINUS,
  203. Wiimote::BUTTON_PLUS,
  204. Wiimote::BUTTON_HOME
  205. };
  206.  
  207. static const u16 dpad_bitmasks[] =
  208. {
  209. Wiimote::PAD_UP, Wiimote::PAD_DOWN, Wiimote::PAD_LEFT, Wiimote::PAD_RIGHT
  210. };
  211. static const u16 dpad_sideways_bitmasks[] =
  212. {
  213. Wiimote::PAD_RIGHT, Wiimote::PAD_LEFT, Wiimote::PAD_UP, Wiimote::PAD_DOWN
  214. };
  215.  
  216. static const char* const named_buttons[] =
  217. {
  218. "A", "B", "1", "2", "-", "+", "Home",
  219. };
  220.  
  221. void Wiimote::Reset()
  222. {
  223. m_reporting_mode = WM_REPORT_CORE;
  224. // i think these two are good
  225. m_reporting_channel = 0;
  226. m_reporting_auto = false;
  227.  
  228. m_rumble_on = false;
  229. m_speaker_mute = false;
  230. m_motion_plus_present = false;
  231. m_motion_plus_active = false;
  232.  
  233. // will make the first Update() call send a status request
  234. // the first call to RequestStatus() will then set up the status struct extension bit
  235. m_extension->active_extension = -1;
  236.  
  237. // eeprom
  238. memset(m_eeprom, 0, sizeof(m_eeprom));
  239. // calibration data
  240. memcpy(m_eeprom, eeprom_data_0, sizeof(eeprom_data_0));
  241. // dunno what this is for, copied from old plugin
  242. memcpy(m_eeprom + 0x16D0, eeprom_data_16D0, sizeof(eeprom_data_16D0));
  243.  
  244. // set up the register
  245. memset(&m_reg_speaker, 0, sizeof(m_reg_speaker));
  246. memset(&m_reg_ir, 0, sizeof(m_reg_ir));
  247. memset(&m_reg_ext, 0, sizeof(m_reg_ext));
  248. memset(&m_reg_motion_plus, 0, sizeof(m_reg_motion_plus));
  249.  
  250. memcpy(&m_reg_motion_plus.ext_identifier, motion_plus_id, sizeof(motion_plus_id));
  251.  
  252. // status
  253. memset(&m_status, 0, sizeof(m_status));
  254. // Battery levels in voltage
  255. // 0x00 - 0x32: level 1
  256. // 0x33 - 0x43: level 2
  257. // 0x33 - 0x54: level 3
  258. // 0x55 - 0xff: level 4
  259. m_status.battery = (u8)(m_options->settings[5]->GetValue() * 100);
  260.  
  261. memset(m_shake_step, 0, sizeof(m_shake_step));
  262.  
  263. // clear read request queue
  264. while (!m_read_requests.empty())
  265. {
  266. delete[] m_read_requests.front().data;
  267. m_read_requests.pop();
  268. }
  269.  
  270. // Yamaha ADPCM state initialize
  271. m_adpcm_state.predictor = 0;
  272. m_adpcm_state.step = 127;
  273. }
  274.  
  275. Wiimote::Wiimote( const unsigned int index )
  276. : m_index(index)
  277. , ir_sin(0)
  278. , ir_cos(1)
  279. // , m_sound_stream( nullptr )
  280. {
  281. // ---- set up all the controls ----
  282.  
  283. // buttons
  284. groups.emplace_back(m_buttons = new Buttons("Buttons"));
  285. for (auto& named_button : named_buttons)
  286. m_buttons->controls.emplace_back(new ControlGroup::Input( named_button));
  287.  
  288. // ir
  289. groups.emplace_back(m_ir = new Cursor(_trans("IR")));
  290.  
  291. // swing
  292. groups.emplace_back(m_swing = new Force(_trans("Swing")));
  293.  
  294. // tilt
  295. groups.emplace_back(m_tilt = new Tilt(_trans("Tilt")));
  296.  
  297. // shake
  298. groups.emplace_back(m_shake = new Buttons(_trans("Shake")));
  299. m_shake->controls.emplace_back(new ControlGroup::Input("X"));
  300. m_shake->controls.emplace_back(new ControlGroup::Input("Y"));
  301. m_shake->controls.emplace_back(new ControlGroup::Input("Z"));
  302.  
  303. // extension
  304. groups.emplace_back(m_extension = new Extension(_trans("Extension")));
  305. m_extension->attachments.emplace_back(new WiimoteEmu::None(m_reg_ext));
  306. m_extension->attachments.emplace_back(new WiimoteEmu::Nunchuk(m_reg_ext));
  307. m_extension->attachments.emplace_back(new WiimoteEmu::Classic(m_reg_ext));
  308. m_extension->attachments.emplace_back(new WiimoteEmu::Guitar(m_reg_ext));
  309. m_extension->attachments.emplace_back(new WiimoteEmu::Drums(m_reg_ext));
  310. m_extension->attachments.emplace_back(new WiimoteEmu::Turntable(m_reg_ext));
  311.  
  312. m_extension->settings.emplace_back(new ControlGroup::Setting(_trans("Motion Plus"), 0, 0, 1));
  313.  
  314. // rumble
  315. groups.emplace_back(m_rumble = new ControlGroup(_trans("Rumble")));
  316. m_rumble->controls.emplace_back(new ControlGroup::Output(_trans("Motor")));
  317.  
  318. // dpad
  319. groups.emplace_back(m_dpad = new Buttons("D-Pad"));
  320. for (auto& named_direction : named_directions)
  321. m_dpad->controls.emplace_back(new ControlGroup::Input(named_direction));
  322.  
  323. // options
  324. groups.emplace_back( m_options = new ControlGroup(_trans("Options")));
  325. m_options->settings.emplace_back(new ControlGroup::BackgroundInputSetting(_trans("Background Input")));
  326. m_options->settings.emplace_back(new ControlGroup::Setting(_trans("Sideways Wiimote"), false));
  327. m_options->settings.emplace_back(new ControlGroup::Setting(_trans("Upright Wiimote"), false));
  328. m_options->settings.emplace_back(new ControlGroup::IterateUI(_trans("Iterative Input")));
  329. m_options->settings.emplace_back(new ControlGroup::Setting(_trans("Speaker Pan"), 0, -127, 127));
  330. m_options->settings.emplace_back(new ControlGroup::Setting(_trans("Battery"), 95.0 / 100, 0, 255));
  331.  
  332. // TODO: This value should probably be re-read if SYSCONF gets changed
  333. m_sensor_bar_on_top = SConfig::GetInstance().m_SYSCONF->GetData<u8>("BT.BAR") != 0;
  334.  
  335. // --- reset eeprom/register/values to default ---
  336. Reset();
  337. }
  338.  
  339. std::string Wiimote::GetName() const
  340. {
  341. return std::string("Wiimote") + char('1'+m_index);
  342. }
  343.  
  344. bool Wiimote::Step()
  345. {
  346. // TODO: change this a bit
  347. m_motion_plus_present = m_extension->settings[0]->value != 0;
  348.  
  349. m_rumble->controls[0]->control_ref->State(m_rumble_on);
  350.  
  351. // when a movie is active, this button status update is disabled (moved), because movies only record data reports.
  352. if (!Core::g_want_determinism)
  353. {
  354. UpdateButtonsStatus();
  355. }
  356.  
  357. // check if there is a read data request
  358. if (!m_read_requests.empty())
  359. {
  360. ReadRequest& rr = m_read_requests.front();
  361. // send up to 16 bytes to the Wii
  362. SendReadDataReply(rr);
  363. //SendReadDataReply(rr.channel, rr);
  364.  
  365. // if there is no more data, remove from queue
  366. if (0 == rr.size)
  367. {
  368. delete[] rr.data;
  369. m_read_requests.pop();
  370. }
  371.  
  372. // don't send any other reports
  373. return true;
  374. }
  375.  
  376. // check if a status report needs to be sent
  377. // this happens on Wiimote sync and when extensions are switched
  378. if (m_extension->active_extension != m_extension->switch_extension)
  379. {
  380. RequestStatus();
  381.  
  382. // WiiBrew: Following a connection or disconnection event on the Extension Port,
  383. // data reporting is disabled and the Data Reporting Mode must be reset before new data can arrive.
  384. // after a game receives an unrequested status report,
  385. // it expects data reports to stop until it sets the reporting mode again
  386. m_reporting_auto = false;
  387.  
  388. return true;
  389. }
  390.  
  391. return false;
  392. }
  393.  
  394. void Wiimote::UpdateButtonsStatus()
  395. {
  396. // update buttons in status struct
  397. m_status.buttons.hex = 0;
  398. const bool is_sideways = m_options->settings[1]->value != 0;
  399. m_buttons->GetState(&m_status.buttons.hex, button_bitmasks);
  400. m_dpad->GetState(&m_status.buttons.hex, is_sideways ? dpad_sideways_bitmasks : dpad_bitmasks);
  401. test(2, *m_dpad);
  402. }
  403.  
  404. void Wiimote::GetButtonData(u8* const data)
  405. {
  406. // when a movie is active, the button update happens here instead of Wiimote::Step, to avoid potential desync issues.
  407. if (Core::g_want_determinism)
  408. {
  409. UpdateButtonsStatus();
  410. }
  411.  
  412. ((wm_buttons*)data)->hex |= m_status.buttons.hex;
  413. }
  414.  
  415. void Wiimote::GetAccelData(u8* const data, const ReportFeatures& rptf)
  416. {
  417. const bool is_sideways = m_options->settings[1]->value != 0;
  418. const bool is_upright = m_options->settings[2]->value != 0;
  419.  
  420. EmulateTilt(&m_accel, m_tilt, is_sideways, is_upright);
  421. EmulateSwing(&m_accel, m_swing, is_sideways, is_upright);
  422. EmulateShake(&m_accel, m_shake, m_shake_step);
  423.  
  424. wm_accel& accel = *(wm_accel*)(data + rptf.accel);
  425. wm_buttons& core = *(wm_buttons*)(data + rptf.core);
  426.  
  427. // We now use 2 bits more precision, so multiply by 4 before converting to int
  428. s16 x = (s16)(4 * (m_accel.x * ACCEL_RANGE + ACCEL_ZERO_G));
  429. s16 y = (s16)(4 * (m_accel.y * ACCEL_RANGE + ACCEL_ZERO_G));
  430. s16 z = (s16)(4 * (m_accel.z * ACCEL_RANGE + ACCEL_ZERO_G));
  431.  
  432. if (x > 1024)
  433. x = 1024;
  434. else if (x < 0)
  435. x = 0;
  436. if (y > 1024)
  437. y = 1024;
  438. else if (y < 0)
  439. y = 0;
  440. if (z > 1024)
  441. z = 1024;
  442. else if (z < 0)
  443. z = 0;
  444.  
  445. accel.x = (x >> 2) & 0xFF;
  446. accel.y = (y >> 2) & 0xFF;
  447. accel.z = (z >> 2) & 0xFF;
  448.  
  449. core.acc_x_lsb = x & 0x3;
  450. core.acc_y_lsb = (y >> 1) & 0x1;
  451. core.acc_z_lsb = (z >> 1) & 0x1;
  452. }
  453.  
  454. inline void LowPassFilter(double& var, double newval, double period)
  455. {
  456. static const double CUTOFF_FREQUENCY = 5.0;
  457.  
  458. double RC = 1.0 / CUTOFF_FREQUENCY;
  459. double alpha = period / (period + RC);
  460. var = newval * alpha + var * (1.0 - alpha);
  461. }
  462.  
  463. void Wiimote::GetIRData(u8* const data, bool use_accel)
  464. {
  465. u16 x[4], y[4];
  466. memset(x, 0xFF, sizeof(x));
  467.  
  468. ControlState xx = 10000, yy = 0, zz = 0;
  469. double nsin,ncos;
  470.  
  471. if (use_accel)
  472. {
  473. double ax,az,len;
  474. ax = m_accel.x;
  475. az = m_accel.z;
  476. len = sqrt(ax*ax+az*az);
  477. if (len)
  478. {
  479. ax/=len;
  480. az/=len; //normalizing the vector
  481. nsin=ax;
  482. ncos=az;
  483. }
  484. else
  485. {
  486. nsin=0;
  487. ncos=1;
  488. }
  489. // PanicAlert("%d %d %d\nx:%f\nz:%f\nsin:%f\ncos:%f",accel->x,accel->y,accel->z,ax,az,sin,cos);
  490. // PanicAlert("%d %d %d\n%d %d %d\n%d %d %d",accel->x,accel->y,accel->z,calib->zero_g.x,calib->zero_g.y,calib->zero_g.z, calib->one_g.x,calib->one_g.y,calib->one_g.z);
  491. }
  492. else
  493. {
  494. nsin=0; //m_tilt stuff here (can't figure it out yet....)
  495. ncos=1;
  496. }
  497.  
  498. LowPassFilter(ir_sin,nsin,1.0/60);
  499. LowPassFilter(ir_cos,ncos,1.0/60);
  500.  
  501. m_ir->GetState(&xx, &yy, &zz, true);
  502.  
  503. Vertex v[4];
  504.  
  505. static const int camWidth=1024;
  506. static const int camHeight=768;
  507. static const double bndup=-0.315447;
  508. static const double bnddown=0.85;
  509. static const double bndleft=0.443364;
  510. static const double bndright=-0.443364;
  511. static const double dist1=100.0/camWidth; //this seems the optimal distance for zelda
  512. static const double dist2=1.2*dist1;
  513.  
  514. for (auto& vtx : v)
  515. {
  516. vtx.x = xx * (bndright - bndleft) / 2 + (bndleft + bndright) / 2;
  517. if (m_sensor_bar_on_top)
  518. vtx.y = yy * (bndup - bnddown) / 2 + (bndup + bnddown) / 2;
  519. else
  520. vtx.y = yy * (bndup - bnddown) / 2 - (bndup + bnddown) / 2;
  521. vtx.z=0;
  522. }
  523.  
  524. v[0].x-=(zz*0.5+1)*dist1;
  525. v[1].x+=(zz*0.5+1)*dist1;
  526. v[2].x-=(zz*0.5+1)*dist2;
  527. v[3].x+=(zz*0.5+1)*dist2;
  528.  
  529. #define printmatrix(m) PanicAlert("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",m[0][0],m[0][1],m[0][2],m[0][3],m[1][0],m[1][1],m[1][2],m[1][3],m[2][0],m[2][1],m[2][2],m[2][3],m[3][0],m[3][1],m[3][2],m[3][3])
  530. Matrix rot,tot;
  531. static Matrix scale;
  532. MatrixScale(scale,1,camWidth/camHeight,1);
  533. //MatrixIdentity(scale);
  534. MatrixRotationByZ(rot,ir_sin,ir_cos);
  535. //MatrixIdentity(rot);
  536. MatrixMultiply(tot,scale,rot);
  537.  
  538. for (int i = 0; i < 4; i++)
  539. {
  540. MatrixTransformVertex(tot,v[i]);
  541. if ((v[i].x<-1)||(v[i].x>1)||(v[i].y<-1)||(v[i].y>1))
  542. continue;
  543. x[i] = (u16)lround((v[i].x+1)/2*(camWidth-1));
  544. y[i] = (u16)lround((v[i].y+1)/2*(camHeight-1));
  545. }
  546. // PanicAlert("%f %f\n%f %f\n%f %f\n%f %f\n%d %d\n%d %d\n%d %d\n%d %d",
  547. // v[0].x,v[0].y,v[1].x,v[1].y,v[2].x,v[2].y,v[3].x,v[3].y,
  548. // x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[38]);
  549.  
  550. // Fill report with valid data when full handshake was done
  551. if (m_reg_ir.data[0x30])
  552. // ir mode
  553. switch (m_reg_ir.mode)
  554. {
  555. // basic
  556. case 1 :
  557. {
  558. memset(data, 0xFF, 10);
  559. wm_ir_basic* const irdata = (wm_ir_basic*)data;
  560. for (unsigned int i=0; i<2; ++i)
  561. {
  562. if (x[i*2] < 1024 && y[i*2] < 768)
  563. {
  564. irdata[i].x1 = static_cast<u8>(x[i*2]);
  565. irdata[i].x1hi = x[i*2] >> 8;
  566.  
  567. irdata[i].y1 = static_cast<u8>(y[i*2]);
  568. irdata[i].y1hi = y[i*2] >> 8;
  569. }
  570. if (x[i*2+1] < 1024 && y[i*2+1] < 768)
  571. {
  572. irdata[i].x2 = static_cast<u8>(x[i*2+1]);
  573. irdata[i].x2hi = x[i*2+1] >> 8;
  574.  
  575. irdata[i].y2 = static_cast<u8>(y[i*2+1]);
  576. irdata[i].y2hi = y[i*2+1] >> 8;
  577. }
  578. }
  579. }
  580. break;
  581. // extended
  582. case 3 :
  583. {
  584. memset(data, 0xFF, 12);
  585. wm_ir_extended* const irdata = (wm_ir_extended*)data;
  586. for (unsigned int i = 0; i < 4; ++i)
  587. if (x[i] < 1024 && y[i] < 768)
  588. {
  589. irdata[i].x = static_cast<u8>(x[i]);
  590. irdata[i].xhi = x[i] >> 8;
  591.  
  592. irdata[i].y = static_cast<u8>(y[i]);
  593. irdata[i].yhi = y[i] >> 8;
  594.  
  595. irdata[i].size = 10;
  596. }
  597. }
  598. break;
  599. // full
  600. case 5 :
  601. PanicAlert("Full IR report");
  602. // UNSUPPORTED
  603. break;
  604. }
  605. }
  606.  
  607. void Wiimote::GetExtData(u8* const data)
  608. {
  609. m_extension->GetState(data);
  610.  
  611. // i dont think anything accesses the extension data like this, but ill support it. Indeed, commercial games don't do this.
  612. // i think it should be unencrpyted in the register, encrypted when read.
  613. memcpy(m_reg_ext.controller_data, data, sizeof(wm_nc)); // TODO: Should it be nc specific?
  614.  
  615. // motionplus pass-through modes
  616. if (m_motion_plus_active)
  617. {
  618. switch (m_reg_motion_plus.ext_identifier[0x4])
  619. {
  620. // nunchuk pass-through mode
  621. // Bit 7 of byte 5 is moved to bit 6 of byte 5, overwriting it
  622. // Bit 0 of byte 4 is moved to bit 7 of byte 5
  623. // Bit 3 of byte 5 is moved to bit 4 of byte 5, overwriting it
  624. // Bit 1 of byte 5 is moved to bit 3 of byte 5
  625. // Bit 0 of byte 5 is moved to bit 2 of byte 5, overwriting it
  626. case 0x5:
  627. //data[5] & (1 << 7)
  628. //data[4] & (1 << 0)
  629. //data[5] & (1 << 3)
  630. //data[5] & (1 << 1)
  631. //data[5] & (1 << 0)
  632. break;
  633.  
  634. // classic controller/musical instrument pass-through mode
  635. // Bit 0 of Byte 4 is overwritten
  636. // Bits 0 and 1 of Byte 5 are moved to bit 0 of Bytes 0 and 1, overwriting
  637. case 0x7:
  638. //data[4] & (1 << 0)
  639. //data[5] & (1 << 0)
  640. //data[5] & (1 << 1)
  641. break;
  642.  
  643. // unknown pass-through mode
  644. default:
  645. break;
  646. }
  647.  
  648. ((wm_motionplus_data*)data)->is_mp_data = 0;
  649. ((wm_motionplus_data*)data)->extension_connected = m_extension->active_extension;
  650. }
  651.  
  652. if (0xAA == m_reg_ext.encryption)
  653. WiimoteEncrypt(&m_ext_key, data, 0x00, sizeof(wm_nc));
  654. }
  655.  
  656. void Wiimote::Update()
  657. {
  658. // no channel == not connected i guess
  659. if (0 == m_reporting_channel)
  660. return;
  661.  
  662. // returns true if a report was sent
  663. if (Step())
  664. return;
  665.  
  666. u8 data[MAX_PAYLOAD];
  667. memset(data, 0, sizeof(data));
  668.  
  669. Movie::SetPolledDevice();
  670.  
  671. m_status.battery = (u8)(m_options->settings[5]->GetValue() * 100);
  672.  
  673. const ReportFeatures& rptf = reporting_mode_features[m_reporting_mode - WM_REPORT_CORE];
  674. s8 rptf_size = rptf.size;
  675. if (Movie::IsPlayingInput() && Movie::PlayWiimote(m_index, data, rptf, m_extension->active_extension, m_ext_key))
  676. {
  677. if (rptf.core)
  678. m_status.buttons = *(wm_buttons*)(data + rptf.core);
  679. }
  680. else
  681. {
  682. data[0] = 0xA1;
  683. data[1] = m_reporting_mode;
  684.  
  685. // core buttons
  686. if (rptf.core)
  687. GetButtonData(data + rptf.core);
  688.  
  689. // acceleration
  690. if (rptf.accel)
  691. GetAccelData(data, rptf);
  692.  
  693. // IR
  694. if (rptf.ir)
  695. GetIRData(data + rptf.ir, (rptf.accel != 0));
  696.  
  697. // extension
  698. if (rptf.ext)
  699. GetExtData(data + rptf.ext);
  700.  
  701. // hybrid Wiimote stuff (for now, it's not supported while recording)
  702. if (WIIMOTE_SRC_HYBRID == g_wiimote_sources[m_index] && !Movie::IsRecordingInput())
  703. {
  704. using namespace WiimoteReal;
  705.  
  706. std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
  707. if (g_wiimotes[m_index])
  708. {
  709. const Report& rpt = g_wiimotes[m_index]->ProcessReadQueue();
  710. if (!rpt.empty())
  711. {
  712. const u8 *real_data = rpt.data();
  713. switch (real_data[1])
  714. {
  715. // use data reports
  716. default:
  717. if (real_data[1] >= WM_REPORT_CORE)
  718. {
  719. const ReportFeatures& real_rptf = reporting_mode_features[real_data[1] - WM_REPORT_CORE];
  720.  
  721. // force same report type from real-Wiimote
  722. if (&real_rptf != &rptf)
  723. rptf_size = 0;
  724.  
  725. // core
  726. // mix real-buttons with emu-buttons in the status struct, and in the report
  727. if (real_rptf.core && rptf.core)
  728. {
  729. m_status.buttons.hex |= ((wm_buttons*)(real_data + real_rptf.core))->hex;
  730. *(wm_buttons*)(data + rptf.core) = m_status.buttons;
  731. }
  732.  
  733. // accel
  734. // use real-accel data always i guess
  735. if (real_rptf.accel && rptf.accel)
  736. memcpy(data + rptf.accel, real_data + real_rptf.accel, sizeof(wm_accel));
  737.  
  738. // ir
  739. // TODO
  740.  
  741. // ext
  742. // use real-ext data if an emu-extention isn't chosen
  743. if (real_rptf.ext && rptf.ext && (0 == m_extension->switch_extension))
  744. memcpy(data + rptf.ext, real_data + real_rptf.ext, sizeof(wm_nc)); // TODO: Why NC specific?
  745. }
  746. else if (WM_ACK_DATA != real_data[1] || m_extension->active_extension > 0)
  747. rptf_size = 0;
  748. else
  749. // use real-acks if an emu-extension isn't chosen
  750. rptf_size = -1;
  751. break;
  752.  
  753. // use all status reports, after modification of the extension bit
  754. case WM_STATUS_REPORT :
  755. //if (m_extension->switch_extension)
  756. //((wm_status_report*)(real_data + 2))->extension = (m_extension->active_extension > 0);
  757. if (m_extension->active_extension)
  758. ((wm_status_report*)(real_data + 2))->extension = 1;
  759. rptf_size = -1;
  760. break;
  761.  
  762. // use all read-data replies
  763. case WM_READ_DATA_REPLY:
  764. rptf_size = -1;
  765. break;
  766.  
  767. }
  768.  
  769. // copy over report from real-Wiimote
  770. if (-1 == rptf_size)
  771. {
  772. std::copy(rpt.begin(), rpt.end(), data);
  773. rptf_size = (s8)(rpt.size());
  774. }
  775. }
  776. }
  777. }
  778.  
  779. Movie::CallWiiInputManip(data, rptf, m_index, m_extension->active_extension, m_ext_key);
  780. }
  781. if (NetPlay::IsNetPlayRunning())
  782. {
  783. NetPlay_GetWiimoteData(m_index, data, rptf.size);
  784. if (rptf.core)
  785. m_status.buttons = *(wm_buttons*)(data + rptf.core);
  786. }
  787.  
  788. Movie::CheckWiimoteStatus(m_index, data, rptf, m_extension->active_extension, m_ext_key);
  789.  
  790. // don't send a data report if auto reporting is off
  791. if (false == m_reporting_auto && data[2] >= WM_REPORT_CORE)
  792. return;
  793.  
  794. // send data report
  795. if (rptf_size)
  796. {
  797. Core::Callback_WiimoteInterruptChannel(m_index, m_reporting_channel, data, rptf_size);
  798. }
  799. }
  800.  
  801. void Wiimote::ControlChannel(const u16 _channelID, const void* _pData, u32 _Size)
  802. {
  803. // Check for custom communication
  804. if (99 == _channelID)
  805. {
  806. // Wiimote disconnected
  807. //PanicAlert( "Wiimote Disconnected" );
  808.  
  809. // reset eeprom/register/reporting mode
  810. Reset();
  811. return;
  812. }
  813.  
  814. // this all good?
  815. m_reporting_channel = _channelID;
  816.  
  817. const hid_packet* const hidp = (hid_packet*)_pData;
  818.  
  819. INFO_LOG(WIIMOTE, "Emu ControlChannel (page: %i, type: 0x%02x, param: 0x%02x)", m_index, hidp->type, hidp->param);
  820.  
  821. switch (hidp->type)
  822. {
  823. case HID_TYPE_HANDSHAKE :
  824. PanicAlert("HID_TYPE_HANDSHAKE - %s", (hidp->param == HID_PARAM_INPUT) ? "INPUT" : "OUPUT");
  825. break;
  826.  
  827. case HID_TYPE_SET_REPORT :
  828. if (HID_PARAM_INPUT == hidp->param)
  829. {
  830. PanicAlert("HID_TYPE_SET_REPORT - INPUT");
  831. }
  832. else
  833. {
  834. // AyuanX: My experiment shows Control Channel is never used
  835. // shuffle2: but lwbt uses this, so we'll do what we must :)
  836. HidOutputReport((wm_report*)hidp->data);
  837.  
  838. u8 handshake = HID_HANDSHAKE_SUCCESS;
  839. Core::Callback_WiimoteInterruptChannel(m_index, _channelID, &handshake, 1);
  840. }
  841. break;
  842.  
  843. case HID_TYPE_DATA :
  844. PanicAlert("HID_TYPE_DATA - %s", (hidp->param == HID_PARAM_INPUT) ? "INPUT" : "OUTPUT");
  845. break;
  846.  
  847. default :
  848. PanicAlert("HidControlChannel: Unknown type %x and param %x", hidp->type, hidp->param);
  849. break;
  850. }
  851.  
  852. }
  853.  
  854. void Wiimote::InterruptChannel(const u16 _channelID, const void* _pData, u32 _Size)
  855. {
  856. // this all good?
  857. m_reporting_channel = _channelID;
  858.  
  859. const hid_packet* const hidp = (hid_packet*)_pData;
  860.  
  861. switch (hidp->type)
  862. {
  863. case HID_TYPE_DATA:
  864. switch (hidp->param)
  865. {
  866. case HID_PARAM_OUTPUT :
  867. {
  868. const wm_report* const sr = (wm_report*)hidp->data;
  869.  
  870. if (WIIMOTE_SRC_REAL & g_wiimote_sources[m_index])
  871. {
  872. switch (sr->wm)
  873. {
  874. // these two types are handled in RequestStatus() & ReadData()
  875. case WM_REQUEST_STATUS :
  876. case WM_READ_DATA :
  877. if (WIIMOTE_SRC_REAL == g_wiimote_sources[m_index])
  878. WiimoteReal::InterruptChannel(m_index, _channelID, _pData, _Size);
  879. break;
  880.  
  881. default :
  882. WiimoteReal::InterruptChannel(m_index, _channelID, _pData, _Size);
  883. break;
  884. }
  885.  
  886. HidOutputReport(sr, m_extension->switch_extension > 0);
  887. }
  888. else
  889. HidOutputReport(sr);
  890. }
  891. break;
  892.  
  893. default :
  894. PanicAlert("HidInput: HID_TYPE_DATA - param 0x%02x", hidp->param);
  895. break;
  896. }
  897. break;
  898.  
  899. default:
  900. PanicAlert("HidInput: Unknown type 0x%02x and param 0x%02x", hidp->type, hidp->param);
  901. break;
  902. }
  903. }
  904.  
  905. void Wiimote::LoadDefaults(const ControllerInterface& ciface)
  906. {
  907. ControllerEmu::LoadDefaults(ciface);
  908.  
  909. #define set_control(group, num, str) (group)->controls[num]->control_ref->expression = (str)
  910.  
  911. // Buttons
  912. #if defined HAVE_X11 && HAVE_X11
  913. set_control(m_buttons, 0, "Click 1"); // A
  914. set_control(m_buttons, 1, "Click 3"); // B
  915. #else
  916. set_control(m_buttons, 0, "Click 0"); // A
  917. set_control(m_buttons, 1, "Click 1"); // B
  918. #endif
  919. set_control(m_buttons, 2, "1"); // 1
  920. set_control(m_buttons, 3, "2"); // 2
  921. set_control(m_buttons, 4, "Q"); // -
  922. set_control(m_buttons, 5, "E"); // +
  923.  
  924. #ifdef _WIN32
  925. set_control(m_buttons, 6, "!LMENU & RETURN"); // Home
  926. #else
  927. set_control(m_buttons, 6, "!`Alt_L` & Return"); // Home
  928. #endif
  929.  
  930. // Shake
  931. for (size_t i = 0; i != 3; ++i)
  932. set_control(m_shake, i, "Click 2");
  933.  
  934. // IR
  935. set_control(m_ir, 0, "Cursor Y-");
  936. set_control(m_ir, 1, "Cursor Y+");
  937. set_control(m_ir, 2, "Cursor X-");
  938. set_control(m_ir, 3, "Cursor X+");
  939.  
  940. // DPad
  941. #ifdef _WIN32
  942. set_control(m_dpad, 0, "UP"); // Up
  943. set_control(m_dpad, 1, "DOWN"); // Down
  944. set_control(m_dpad, 2, "LEFT"); // Left
  945. set_control(m_dpad, 3, "RIGHT"); // Right
  946. #elif __APPLE__
  947. set_control(m_dpad, 0, "Up Arrow"); // Up
  948. set_control(m_dpad, 1, "Down Arrow"); // Down
  949. set_control(m_dpad, 2, "Left Arrow"); // Left
  950. set_control(m_dpad, 3, "Right Arrow"); // Right
  951. #else
  952. set_control(m_dpad, 0, "Up"); // Up
  953. set_control(m_dpad, 1, "Down"); // Down
  954. set_control(m_dpad, 2, "Left"); // Left
  955. set_control(m_dpad, 3, "Right"); // Right
  956. #endif
  957.  
  958. // ugly stuff
  959. // enable nunchuk
  960. m_extension->switch_extension = 1;
  961.  
  962. // set nunchuk defaults
  963. m_extension->attachments[1]->LoadDefaults(ciface);
  964. }
  965.  
  966. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement