From 22c2276cef00c92ecaa814c777ab72de86477e90 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 29 Apr 2010 18:51:04 +0000 Subject: [PATCH] New Wiimote Plugin: Added "Upright Wiimote" option. Fixed a nunchuk problem in ZTP and Wii Sports with some Hacks. Some work on emulated Swing and Speaker (disabled). Fixes/Cleanups. ControllerInterface: Fixed an issue when a DInput device reports the same axis more than once. Fixed some old comments. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5422 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../ControllerInterface/ControllerInterface.h | 5 +- .../DirectInput/DirectInputJoystick.cpp | 50 ++- .../InputPluginCommon/Src/ConfigDiag.cpp | 12 +- .../Src/ConfigDiagBitmaps.cpp | 149 ++++---- .../InputPluginCommon/Src/ControllerEmu.cpp | 23 +- .../InputPluginCommon/Src/ControllerEmu.h | 21 +- .../Plugin_WiimoteNew.vcproj | 20 +- .../Plugins/Plugin_WiimoteNew/Src/SConscript | 1 + .../Src/WiimoteEmu/Attachment/Nunchuk.cpp | 46 +-- .../Src/WiimoteEmu/Attachment/Nunchuk.h | 2 +- .../Src/WiimoteEmu/EmuSubroutines.cpp | 43 ++- .../Src/WiimoteEmu/Encryption.cpp | 41 ++- .../Src/WiimoteEmu/Encryption.h | 6 +- .../Src/WiimoteEmu/Speaker.cpp | 39 +++ .../Src/WiimoteEmu/WiimoteEmu.cpp | 331 +++++++++++++----- .../Src/WiimoteEmu/WiimoteEmu.h | 77 +++- .../Src/WiimoteEmu/WiimoteHid.h | 3 +- 17 files changed, 579 insertions(+), 290 deletions(-) create mode 100644 Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Speaker.cpp diff --git a/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h index cb504c36e3..8ecdf73de8 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/Src/ControllerInterface/ControllerInterface.h @@ -16,7 +16,6 @@ #define CIFACE_USE_DIRECTINPUT #endif #if defined(HAVE_X11) && HAVE_X11 -// Xlib is not tested at all currently, it is like 80% complete at least though #define CIFACE_USE_XLIB #endif #ifndef CIFACE_USE_DIRECTINPUT_JOYSTICK @@ -74,7 +73,7 @@ public: // // Output // - // guess wut it is, yup and output + // an output on a device // class Output : public Control { @@ -175,7 +174,7 @@ public: // ControlReference // // these are what you create to actually use the inputs, InputReference or OutputReference - // they have a vector < struct { device , vector < controls > } > + // they have a DeviceQualifier and ControlQualifier used to match 1 or more inputs // // after being binded to devices and controls with ControllerInterface::UpdateReference, // each one can binded to a devices, and 0+ controls the device diff --git a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp index 44b0e1ee08..2b6bf6bb76 100644 --- a/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp +++ b/Source/Core/InputCommon/Src/ControllerInterface/DirectInput/DirectInputJoystick.cpp @@ -4,6 +4,11 @@ #include "DirectInputJoystick.h" +inline bool operator<(const GUID & lhs, const GUID & rhs) +{ + return memcmp(&lhs, &rhs, sizeof(GUID)) < 0 ? true : false; +} + namespace ciface { namespace DirectInput @@ -231,8 +236,18 @@ Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVI } // get up to 6 axes and 2 sliders std::vector axes; + m_device->EnumObjects( DIEnumDeviceObjectsCallback, (LPVOID)&axes, DIDFT_ABSAXIS ); + unsigned int cur_slider = 0; - m_device->EnumObjects( DIEnumDeviceObjectsCallback, (LPVOID)&axes, DIDFT_AXIS ); + + // map of axis offsets in joystate dataformat based on axis guidType + std::map types; + types[GUID_XAxis] = 0; + types[GUID_YAxis] = 1; + types[GUID_ZAxis] = 2; + types[GUID_RxAxis] = 3; + types[GUID_RyAxis] = 4; + types[GUID_RzAxis] = 5; // going in reverse leaves the list more organized in the end for me :/ std::vector::const_reverse_iterator i = axes.rbegin(), @@ -254,24 +269,25 @@ Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVI if ( DI_OK == m_device->GetProperty( DIPROP_RANGE, &range.diph ) ) { int offset = -1; - const GUID type = i->guidType; - // figure out which axis this is - if ( type == GUID_XAxis ) - offset = 0; - else if ( type == GUID_YAxis ) - offset = 1; - else if ( type == GUID_ZAxis ) - offset = 2; - else if ( type == GUID_RxAxis ) - offset = 3; - else if ( type == GUID_RyAxis ) - offset = 4; - else if ( type == GUID_RzAxis ) - offset = 5; - else if ( type == GUID_Slider ) - if ( cur_slider < 2 ) + if (GUID_Slider ==i->guidType) + { + // max of 2 sliders / limit of used data format + if (cur_slider < 2) offset = 6 + cur_slider++; + } + else + { + // don't add duplicate axes, some buggy drivers report the same axis twice + const std::map::iterator f = types.find(i->guidType); + if (types.end() != f) + { + offset = f->second; + // remove from the map so it isn't added again + types.erase(f); + } + } + if ( offset >= 0 ) { diff --git a/Source/Plugins/InputPluginCommon/Src/ConfigDiag.cpp b/Source/Plugins/InputPluginCommon/Src/ConfigDiag.cpp index afa521d8d8..fa6e490c3b 100644 --- a/Source/Plugins/InputPluginCommon/Src/ConfigDiag.cpp +++ b/Source/Plugins/InputPluginCommon/Src/ConfigDiag.cpp @@ -19,9 +19,9 @@ void GamepadPage::ConfigExtension( wxCommandEvent& event ) const std::size_t orig_size = control_groups.size(); ControlGroupsSizer* const szr = new ControlGroupsSizer( ex->attachments[ex->switch_extension], pnl, this, &control_groups ); - pnl->SetSizerAndFit( szr ); + pnl->SetSizerAndFit( szr ); // needed pnl_szr->Add( pnl, 0, wxLEFT, 5 ); - dlg->SetSizerAndFit( pnl_szr ); + dlg->SetSizerAndFit( pnl_szr ); // needed dlg->Center(); @@ -131,7 +131,7 @@ ControlDialog::ControlDialog( wxWindow* const parent, ControllerInterface::Contr szr->Add( d_szr, 0, wxEXPAND|wxLEFT|wxRIGHT|wxTOP, 5 ); szr->Add( control_chooser, 0, wxEXPAND|wxALL, 5 ); - SetSizerAndFit( szr ); + SetSizerAndFit( szr ); // needed } @@ -661,6 +661,7 @@ ControlGroupBox::ControlGroupBox( ControllerEmu::ControlGroup* const group, wxWi case GROUP_TYPE_STICK : case GROUP_TYPE_TILT : case GROUP_TYPE_CURSOR : + case GROUP_TYPE_FORCE : { wxBitmap bitmap(64, 64); wxMemoryDC dc; @@ -871,7 +872,7 @@ GamepadPage::GamepadPage( wxWindow* parent, const unsigned int pad_num, ConfigDi UpdateGUI(); - SetSizerAndFit( mapping ); + SetSizerAndFit( mapping ); // needed Layout(); }; @@ -911,7 +912,8 @@ ConfigDialog::ConfigDialog( wxWindow* const parent, Plugin& plugin, const std::s szr->Add( m_pad_notebook, 0, wxEXPAND|wxTOP|wxLEFT|wxRIGHT, 5 ); szr->Add( btns, 0, wxEXPAND|wxALL, 5 ); - SetSizerAndFit( szr ); + SetSizerAndFit( szr ); // needed + // not needed here it seems, but it cant hurt //Layout(); diff --git a/Source/Plugins/InputPluginCommon/Src/ConfigDiagBitmaps.cpp b/Source/Plugins/InputPluginCommon/Src/ConfigDiagBitmaps.cpp index 0cb19ef610..0bb5d3294c 100644 --- a/Source/Plugins/InputPluginCommon/Src/ConfigDiagBitmaps.cpp +++ b/Source/Plugins/InputPluginCommon/Src/ConfigDiagBitmaps.cpp @@ -10,7 +10,7 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) ge = current_page->control_groups.end(); for ( ; g != ge; ++g ) { - + // if this control group has a bitmap if ( (*g)->static_bitmap ) { @@ -22,6 +22,20 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) // just always update m_plugin.controller_interface.UpdateInput(); + wxMemoryDC dc; + wxBitmap bitmap((*g)->static_bitmap->GetBitmap()); + dc.SelectObject(bitmap); + dc.Clear(); + + // label for sticks and stuff + if (64 == bitmap.GetHeight()) + { + wxFont small_font(6, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD); + dc.SetFont(small_font); + dc.SetTextForeground(0xC0C0C0); + dc.DrawText(wxString::FromAscii((*g)->control_group->name).Upper(), 4, 2); + } + switch ( (*g)->control_group->type ) { case GROUP_TYPE_TILT : @@ -55,12 +69,6 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) xx *= 32 - 1; xx += 32; yy *= 32 - 1; yy += 32; - // setup - wxBitmap bitmap(64, 64); - wxMemoryDC dc; - dc.SelectObject(bitmap); - dc.Clear(); - // draw the shit // ir cursor forward movement @@ -75,7 +83,10 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) dc.SetPen(*wxLIGHT_GREY_PEN); dc.SetBrush(*wxWHITE_BRUSH); if ( GROUP_TYPE_STICK == (*g)->control_group->type ) + { + dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawCircle( 32, 32, 32); + } else dc.DrawRectangle( 16, 16, 32, 32 ); @@ -106,27 +117,70 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) //dc.DrawRectangle( x-4, 64-y-1, 8, 2 ); } - // box outline - // Windows XP color - dc.SetPen(wxPen(_T("#7f9db9"))); - dc.SetBrush(*wxTRANSPARENT_BRUSH); - dc.DrawRectangle(0, 0, 64, 64); + } + break; + case GROUP_TYPE_FORCE : + { + float raw_dot[3]; + float adj_dot[3]; + const float deadzone = 32 * ((*g)->control_group)->settings[0]->value; - // done drawing - dc.SelectObject(wxNullBitmap); + // adjusted + ((ControllerEmu::Force*)(*g)->control_group)->GetState( adj_dot, 32.0, 32-1.5 ); + + // raw + for ( unsigned int i=0; i<3; ++i ) + { + raw_dot[i] = (*g)->control_group->controls[i*2 + 1]->control_ref->State() + - (*g)->control_group->controls[i*2]->control_ref->State(); + raw_dot[i] *= 32 - 1; raw_dot[i] += 32; + } + + // deadzone rect for forward/backward visual + dc.SetBrush(*wxLIGHT_GREY_BRUSH); + dc.SetPen(*wxLIGHT_GREY_PEN); + dc.DrawRectangle( 0, 32 - deadzone, 64, deadzone * 2 ); + + // raw forward/background line + dc.SetPen(*wxGREY_PEN); + dc.SetBrush(*wxGREY_BRUSH); + dc.DrawRectangle( 0, raw_dot[2] - 1, 64, 2 ); + + // adjusted forward/background line + if ( adj_dot[2]!=32 ) + { + dc.SetPen(*wxRED_PEN); + dc.SetBrush(*wxRED_BRUSH); + dc.DrawRectangle( 0, adj_dot[2] - 1, 64, 2 ); + } + + // a rectangle, for looks i guess + dc.SetBrush(*wxWHITE_BRUSH); + dc.SetPen(*wxLIGHT_GREY_PEN); + dc.DrawRectangle( 16, 16, 32, 32 ); + + // deadzone square + dc.SetBrush(*wxLIGHT_GREY_BRUSH); + dc.DrawRectangle( 32 - deadzone, 32 - deadzone, deadzone * 2, deadzone * 2 ); + + // raw dot + dc.SetPen(*wxGREY_PEN); + dc.SetBrush(*wxGREY_BRUSH); + dc.DrawRectangle( raw_dot[1] - 2, raw_dot[0] - 2, 4, 4 ); + + // adjusted dot + if ( adj_dot[1]!=32 || adj_dot[0]!=32 ) + { + dc.SetPen(*wxRED_PEN); + dc.SetBrush(*wxRED_BRUSH); + dc.DrawRectangle( adj_dot[1]-2, adj_dot[0]-2, 4, 4 ); + } - // set the shit - (*g)->static_bitmap->SetBitmap( bitmap ); } break; case GROUP_TYPE_BUTTONS : { const unsigned int button_count = ((unsigned int)(*g)->control_group->controls.size()); - // setup - wxBitmap bitmap(12*button_count+1, 12); - wxMemoryDC dc; - dc.SelectObject(bitmap); - dc.Clear(); // draw the shit dc.SetPen(*wxGREY_PEN); @@ -140,7 +194,6 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) for ( unsigned int n = 0; nstatic_bitmap->SetBitmap( bitmap ); + } break; case GROUP_TYPE_TRIGGERS : { const unsigned int trigger_count = ((unsigned int)((*g)->control_group->controls.size())); - // setup - wxBitmap bitmap( 64, 12*trigger_count+1); - wxMemoryDC dc; - dc.SelectObject(bitmap); - dc.Clear(); // draw the shit dc.SetPen(*wxGREY_PEN); @@ -200,26 +237,11 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRectangle(0, 0, deadzone*64, trigger_count*14); - // box outline - // Windows XP color - dc.SetPen(wxPen(_T("#7f9db9"))); - dc.DrawRectangle(0, 0, bitmap.GetWidth(), bitmap.GetHeight()); - - // done drawing - dc.SelectObject(wxNullBitmap); - - // set the shit - (*g)->static_bitmap->SetBitmap( bitmap ); } break; case GROUP_TYPE_MIXED_TRIGGERS : { const unsigned int trigger_count = ((unsigned int)((*g)->control_group->controls.size() / 2)); - // setup - wxBitmap bitmap( 64+12+1, 12*trigger_count+1); - wxMemoryDC dc; - dc.SelectObject(bitmap); - dc.Clear(); // draw the shit dc.SetPen(*wxGREY_PEN); @@ -245,22 +267,21 @@ void ConfigDialog::UpdateBitmaps(wxTimerEvent& WXUNUSED(event)) dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRectangle(thresh*64, 0, 128, trigger_count*14); - // box outline - // Windows XP color - dc.SetPen(wxPen(_T("#7f9db9"))); - dc.DrawRectangle(0, 0, bitmap.GetWidth(), bitmap.GetHeight()); - - // done drawing - dc.SelectObject(wxNullBitmap); - - // set the shit - (*g)->static_bitmap->SetBitmap( bitmap ); } break; default : break; } + // box outline + // Windows XP color + dc.SetPen(wxPen(_T("#7f9db9"))); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + dc.DrawRectangle(0, 0, bitmap.GetWidth(), bitmap.GetHeight()); + + dc.SelectObject(wxNullBitmap); + (*g)->static_bitmap->SetBitmap(bitmap); + m_plugin.interface_crit.Leave(); } } diff --git a/Source/Plugins/InputPluginCommon/Src/ControllerEmu.cpp b/Source/Plugins/InputPluginCommon/Src/ControllerEmu.cpp index b15607e5cc..2e6f73b620 100644 --- a/Source/Plugins/InputPluginCommon/Src/ControllerEmu.cpp +++ b/Source/Plugins/InputPluginCommon/Src/ControllerEmu.cpp @@ -251,18 +251,15 @@ ControllerEmu::Triggers::Triggers( const char* const _name ) : ControlGroup( _na ControllerEmu::Force::Force( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_FORCE ) { - controls.push_back( new Input( "X-" ) ); - controls.push_back( new Input( "X+" ) ); - controls.push_back( new Input( "Y-" ) ); - controls.push_back( new Input( "Y+" ) ); - controls.push_back( new Input( "Z-" ) ); - controls.push_back( new Input( "Z+" ) ); -} + controls.push_back( new Input( "Up" ) ); + controls.push_back( new Input( "Down" ) ); + controls.push_back( new Input( "Left" ) ); + controls.push_back( new Input( "Right" ) ); + controls.push_back( new Input( "Forward" ) ); + controls.push_back( new Input( "Backward" ) ); + controls.push_back( new Input( "Modifier" ) ); -void ControllerEmu::Force::GetState( u8* data, const u8 base, const u8 range ) -{ - for ( unsigned int i=0; i<3; ++i,++data ) - *data = u8( ( controls[i*2+1]->control_ref->State() - controls[i*2]->control_ref->State() ) * range + base ); + settings.push_back( new Setting("Dead Zone", 0, 1, 50 ) ); } ControllerEmu::Tilt::Tilt( const char* const _name ) : ControlGroup( _name, GROUP_TYPE_TILT ) @@ -287,10 +284,12 @@ ControllerEmu::Cursor::Cursor( const char* const _name, const SWiimoteInitialize { for ( unsigned int i = 0; i < 4; ++i ) controls.push_back( new Input( named_directions[i] ) ); + controls.push_back( new Input( "Forward" ) ); + controls.push_back( new Input( "Hide" ) ); + settings.push_back( new Setting("Center", 0.5f ) ); settings.push_back( new Setting("Width", 0.5f ) ); settings.push_back( new Setting("Height", 0.5f ) ); - settings.push_back( new Setting("Top", 0.5f ) ); } diff --git a/Source/Plugins/InputPluginCommon/Src/ControllerEmu.h b/Source/Plugins/InputPluginCommon/Src/ControllerEmu.h index eb779f1ae7..d5eeee962d 100644 --- a/Source/Plugins/InputPluginCommon/Src/ControllerEmu.h +++ b/Source/Plugins/InputPluginCommon/Src/ControllerEmu.h @@ -237,7 +237,20 @@ public: public: Force( const char* const _name ); - void GetState( u8* data, const u8 base, const u8 range ); + template + void GetState( C* axis, const u8 base, const R range ) + { + const float deadzone = settings[0]->value; + for ( unsigned int i=0; i<6; i+=2 ) + { + const float state = controls[i+1]->control_ref->State() - controls[i]->control_ref->State(); + if (abs(state) > deadzone) + *axis++ = (C)((state - (deadzone * sign(state))) / (1 - deadzone) * range + base); + //*axis++ = state * range + base; + else + *axis++ = (C)(base); + } + } }; class Tilt : public ControlGroup @@ -333,9 +346,9 @@ public: // adjust cursor according to settings if (adjusted) { - xx *= ( settings[0]->value * 2 ); - yy *= ( settings[1]->value * 2 ); - yy += ( settings[2]->value - 0.5f ); + xx *= ( settings[1]->value * 2 ); + yy *= ( settings[2]->value * 2 ); + yy += ( settings[0]->value - 0.5f ); } *x = xx; diff --git a/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj b/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj index 2236290f55..489e9f9991 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj +++ b/Source/Plugins/Plugin_WiimoteNew/Plugin_WiimoteNew.vcproj @@ -67,7 +67,7 @@ /> + + diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/SConscript b/Source/Plugins/Plugin_WiimoteNew/Src/SConscript index e625f8b199..041c29ec5e 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/SConscript +++ b/Source/Plugins/Plugin_WiimoteNew/Src/SConscript @@ -15,6 +15,7 @@ files = [ 'WiimoteEmu/Attachment/Guitar.cpp', 'WiimoteEmu/EmuSubroutines.cpp', 'WiimoteEmu/Encryption.cpp', + 'WiimoteEmu/Speaker.cpp', 'WiimoteNew.cpp', ] diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.cpp b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.cpp index b89b97fc6b..fa3548dbb0 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.cpp +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.cpp @@ -39,9 +39,10 @@ Nunchuk::Nunchuk() : Attachment( "Nunchuk" ) // stick groups.push_back( m_stick = new AnalogStick( "Stick" ) ); - // force - //groups.push_back( m_tilt = new Tilt( "Tilt" ) ); - groups.push_back( m_tilt = new Tilt( "Pitch and Roll" ) ); + // tilt + groups.push_back( m_tilt = new Tilt( "Tilt" ) ); + + // swing //groups.push_back( m_swing = new Force( "Swing" ) ); // shake @@ -57,7 +58,7 @@ Nunchuk::Nunchuk() : Attachment( "Nunchuk" ) memcpy( ®[0xfa], nunchuck_id, sizeof(nunchuck_id) ); // this should get set to 0 on disconnect, but it isn't, o well - m_shake_step = 0; + memset(m_shake_step, 0, sizeof(m_shake_step)); } void Nunchuk::GetState( u8* const data, const bool focus ) @@ -68,43 +69,18 @@ void Nunchuk::GetState( u8* const data, const bool focus ) m_stick->GetState( &ncdata->jx, &ncdata->jy, 0x80, focus ? 127 : 0 ); // tilt - float x, y; - m_tilt->GetState( &x, &y, 0, focus ? (PI / 2) : 0 ); // 90 degrees + EmulateTilt((wm_accel*)&ncdata->ax, m_tilt, (accel_cal*)®[0x20], focus); - // this isn't doing anything with those low bits in the calib data, o well - - const accel_cal* const cal = (accel_cal*)®[0x20]; - const u8* const zero_g = &cal->zero_g.x; - u8 one_g[3]; - for ( unsigned int i=0; i<3; ++i ) - one_g[i] = (&cal->one_g.x)[i] - zero_g[i]; - - // this math should be good enough :P - ncdata->az = u8(sin( (PI / 2) - std::max( abs(x), abs(y) ) ) * one_g[2] + zero_g[2]); - ncdata->ax = u8(sin(x) * -one_g[0] + zero_g[0]); - ncdata->ay = u8(sin(y) * one_g[1] + zero_g[1]); - - // shake - const unsigned int btns[] = { 0x01, 0x02, 0x04 }; - unsigned int shake = 0; if (focus) - m_shake->GetState( &shake, btns ); - if (shake) { - for ( unsigned int i=0; i<3; ++i ) - if ( shake & (1 << i) ) - (&ncdata->ax)[i] = shake_data[m_shake_step]; - m_shake_step = (m_shake_step + 1) % sizeof(shake_data); - } - else - m_shake_step = 0; - - // buttons - if (focus) + // shake + EmulateShake(&ncdata->ax, m_shake, m_shake_step); + // buttons m_buttons->GetState( &ncdata->bt, nunchuk_button_bitmasks ); + } // flip the button bits :/ - ncdata->bt ^= 0x3; + ncdata->bt ^= 0x03; } diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.h b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.h index 9d42dc55ea..fecc101e10 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.h +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Attachment/Nunchuk.h @@ -19,7 +19,7 @@ private: Buttons* m_buttons; AnalogStick* m_stick; - unsigned int m_shake_step; + unsigned int m_shake_step[3]; }; } diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/EmuSubroutines.cpp b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/EmuSubroutines.cpp index 1e34245cb3..bb143008b4 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/EmuSubroutines.cpp +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/EmuSubroutines.cpp @@ -65,6 +65,11 @@ void Wiimote::ReportMode(const u16 _channelID, wm_report_mode* dr) m_reporting_mode = dr->mode; m_reporting_channel = _channelID; + // some hax to skip a few Update() cycles to fix a nunchuk prob in ztp and wii sports + // skipping 10 seems to work + // probably like 1/6th of a second that the user won't have control :/ + m_skip_update = 10; + if (0 == dr->all_the_time) PanicAlert("Wiimote: Reporting Always is set to OFF! Everything should be fine, but games never do this."); @@ -137,24 +142,21 @@ void Wiimote::HidOutputReport(const u16 _channelID, wm_report* sr) break; case WM_WRITE_SPEAKER_DATA : // 0x18 +#ifdef USE_WIIMOTE_EMU_SPEAKER + SpeakerData((wm_speaker_data*)sr->data); +#endif // TODO: Does this need an ack? - { - // testing - //wm_speaker_data* const sd = (wm_speaker_data*)sr->data; - //unsigned int length = sd->length >> 3; - - //PanicAlert( "WM Speaker Data:\nlength: %d\nformat: 0x%x\nrate: 0x%x\nvolume: 0x%x", - //length, m_reg_speaker->format, m_reg_speaker->sample_rate, m_reg_speaker->volume ); - - //for (unsigned int i=0; idata[0]); //PanicAlert( "WM Speaker Mute: %d", sr->data[0] & 0x04 ); +#ifdef USE_WIIMOTE_EMU_SPEAKER + // testing + if (sr->data[0] ^ 0x04) + m_channel_status.step = 0; +#endif m_speaker_mute = (sr->data[0] & 0x04) ? 1 : 0; break; @@ -240,7 +242,6 @@ void Wiimote::RequestStatus(const u16 _channelID, wm_request_status* rs) void Wiimote::WriteData(const u16 _channelID, wm_write_data* wd) { u32 address = convert24bit(wd->address); - //u16 size = convert16bit(rd->size); // ignore the 0x010000 bit address &= 0xFEFFFF; @@ -302,13 +303,16 @@ void Wiimote::WriteData(const u16 _channelID, wm_write_data* wd) // that we send it parts of a key, only the last full key will have an effect // I might have f'ed this up if ( address >= 0xa40040 && address <= 0xa4004c ) - { - u8 data[WIIMOTE_REG_EXT_SIZE]; - m_register.Read( 0xa40000, data, WIIMOTE_REG_EXT_SIZE ); - wiimote_gen_key( &m_ext_key, data + 0x40 ); - } + wiimote_gen_key(&m_ext_key, m_reg_ext->encryption_key); + //else if ( address >= 0xa40020 && address < 0xa40040 ) + // PanicAlert("Writing to extension calibration data! Extension may misbehave"); } break; + // ir + case 0xB0 : + if (5 == m_reg_ir->mode) + PanicAlert("IR Full Mode is Unsupported!"); + break; } } @@ -386,8 +390,11 @@ void Wiimote::ReadData(const u16 _channelID, wm_read_data* rd) { // Encrypt data read from extension register // Check if encrypted reads is on - if ( m_reg_ext[0xf0] == 0xaa ) + if (0xaa == m_reg_ext->encryption) wiimote_encrypt(&m_ext_key, block, address & 0xffff, (u8)size); + + //if ( address >= 0xa40008 && address < 0xa40020 ) + // PanicAlert("Reading extension data from register"); } break; // motion plus diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.cpp b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.cpp index 8cebbc2fff..44ea0b3f7d 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.cpp +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.cpp @@ -18,11 +18,10 @@ #include "Common.h" -#include "pluginspecs_wiimote.h" #include "Encryption.h" -u8 ans_tbl[7][6] = { +static const u8 ans_tbl[7][6] = { {0xA8,0x77,0xA6,0xE0,0xF7,0x43}, {0x5A,0x35,0x85,0xE2,0x72,0x97}, {0x8F,0xB7,0x1A,0x62,0x87,0x38}, @@ -32,7 +31,7 @@ u8 ans_tbl[7][6] = { {0x30,0x7E,0x90, 0xE,0x85, 0xA}, }; -u8 tsbox[256] = { +static const u8 tsbox[256] = { 0x70,0x51, 3,0x86,0x40, 0xD,0x4F,0xEB,0x3E,0xCC,0xD1,0x87,0x35,0xBD,0xF5, 0xB, 0x5E,0xD0,0xF8,0xF2,0xD5,0xE2,0x6C,0x31, 0xC,0xAD,0xFC,0x21,0xC3,0x78,0xC1, 6, 0xC2,0x4C,0x55,0xE6,0x4A,0x34,0x48,0x11,0x1E,0xDA,0xE7,0x1A,0x84,0xA0,0x96,0xA7, @@ -51,7 +50,7 @@ u8 tsbox[256] = { 0x8B,0x9B, 8, 0xF,0xDC,0x81,0x18,0x20, 4,0xE4,0x71,0xCF,0xE9,0x2B,0x42,0x58, }; -u8 sboxes[8][256] = { +static const u8 sboxes[8][256] = { { 1,0xA0,0xA9,0x62,0xD6,0x3F,0x85,0xA7,0xB6,0xD4,0xFA,0x15,0x66,0x17, 9,0xBD, 0x5D,0x14,0x34,0x26,0x59,0x72,0x91,0x54, 6,0x4F,0xF8,0xB0,0x5B,0x74,0x93,0x99, @@ -199,18 +198,18 @@ u8 sboxes[8][256] = { }; -static inline u8 ror8(u8 a, u8 b) { +static inline u8 ror8(const u8 a, const u8 b) +{ return (a>>b) | ((a<<(8-b))&0xff); } -void genkey(u8 *rand, u8 idx, u8 *key) +void genkey(const u8* const rand, const u8 idx, u8* const key) { - u8 *ans = ans_tbl[idx]; + const u8* const ans = ans_tbl[idx]; u8 t0[10]; - int i; - for(i=0;i<10;i++) + for(int i=0; i<10; ++i) t0[i] = tsbox[rand[i]]; key[0] = ((ror8((ans[0]^t0[5]),(t0[2]%8)) - t0[9]) ^ t0[4]); @@ -222,7 +221,7 @@ void genkey(u8 *rand, u8 idx, u8 *key) } -void gentabs(u8 *rand, u8 *key, u8 idx, u8 *ft, u8 *sb) +void gentabs(const u8* const rand, const u8* const key, const u8 idx, u8* const ft, u8* const sb) { ft[0] = sboxes[idx][key[4]] ^ sboxes[(idx+1)%8][rand[3]]; ft[1] = sboxes[idx][key[2]] ^ sboxes[(idx+1)%8][rand[5]]; @@ -247,25 +246,25 @@ void gentabs(u8 *rand, u8 *key, u8 idx, u8 *ft, u8 *sb) /* Generate key from the 0x40-0x4c data in g_RegExt */ -void wiimote_gen_key(wiimote_key *key, u8 *keydata) +void wiimote_gen_key(wiimote_key* const key, const u8* const keydata) { u8 rand[10]; u8 skey[6]; u8 testkey[6]; int idx; - for(int i=0;i<10;i++) + for (int i=0; i<10; ++i) rand[9-i] = keydata[i]; - for(int i=0;i<6;i++) + for (int i=0; i<6; ++i) skey[5-i] = keydata[i+10]; //DEBUG_LOG(WIIMOTE, "rand: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", rand[0], rand[1], rand[2], rand[3], rand[4], rand[5], rand[6], rand[7], rand[8], rand[9]); //DEBUG_LOG(WIIMOTE, "key: %02x %02x %02x %02x %02x %02x", skey[0], skey[1], skey[2], skey[3], skey[4], skey[5]); - for(idx = 0; idx < 7; idx++) + for(idx = 0; idx < 7; ++idx) { genkey(rand, idx, testkey); - if(!memcmp(testkey, skey, 6)) + if (0 == memcmp(testkey, skey, 6)) break; } // default case is idx = 7 which is valid (homebrew uses it for the 0x17 case) @@ -281,16 +280,16 @@ void wiimote_gen_key(wiimote_key *key, u8 *keydata) // TODO: is there a reason these can only handle a length of 255? /* Encrypt data */ -void wiimote_encrypt(wiimote_key *key, u8 *data, int addr, u8 len) +void wiimote_encrypt(const wiimote_key* const key, u8* const data, int addr, const u8 len) { - for(int i = 0; i < len; i++, addr++) - data[i] = (data[i] - key->ft[addr%8]) ^ key->sb[addr%8]; + for (int i = 0; i < len; ++i, ++addr) + data[i] = (data[i] - key->ft[addr % 8]) ^ key->sb[addr % 8]; } /* Decrypt data */ -void wiimote_decrypt(wiimote_key *key, u8 *data, int addr, u8 len) +void wiimote_decrypt(const wiimote_key* const key, u8* const data, int addr, const u8 len) { - for(int i = 0; i < len; i++, addr++) - data[i] = (data[i] ^ key->sb[addr%8]) + key->ft[addr%8]; + for (int i = 0; i < len; ++i, ++addr) + data[i] = (data[i] ^ key->sb[addr % 8]) + key->ft[addr % 8]; } diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.h b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.h index 3e18f4be00..92a06ad2a4 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.h +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Encryption.h @@ -31,10 +31,10 @@ struct wiimote_key }; -void wiimote_encrypt(wiimote_key *key, u8 *data, int addr, u8 len); -void wiimote_decrypt(wiimote_key *key, u8 *data, int addr, u8 len); +void wiimote_encrypt(const wiimote_key* const key, u8* const data, int addr, const u8 len); +void wiimote_decrypt(const wiimote_key* const key, u8* const data, int addr, const u8 len); -void wiimote_gen_key(wiimote_key *key, u8 *keydata); +void wiimote_gen_key(wiimote_key* const key, const u8* const keydata); #endif diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Speaker.cpp b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Speaker.cpp new file mode 100644 index 0000000000..2296a0482a --- /dev/null +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/Speaker.cpp @@ -0,0 +1,39 @@ + +#include "WiimoteEmu.h" + +#ifdef USE_WIIMOTE_EMU_SPEAKER + +namespace WiimoteEmu +{ + +void Wiimote::SpeakerData(wm_speaker_data* sd) +{ + SoundBuffer sb; + sb.samples = new s16[sd->length * 2]; + + s16* s = sb.samples; + const u8* const e = sd->data + sd->length; + for ( const u8* i = sd->data; i> 4 ); + } + + alGenBuffers(1, &sb.buffer); + // TODO make this not always 3000 + alBufferData(sb.buffer, AL_FORMAT_MONO16, sb.samples, (sd->length * sizeof(short) * 2), 3360); + // testing + //alBufferData(sb.buffer, AL_FORMAT_MONO16, sb.samples, (sd->length * sizeof(short) * 2), 48000/m_reg_speaker->sample_rate); + alSourceQueueBuffers(m_audio_source, 1, &sb.buffer); + + ALint state; + alGetSourcei(m_audio_source, AL_SOURCE_STATE, &state); + if (AL_PLAYING != state) + alSourcePlay(m_audio_source); + + m_audio_buffers.push(sb); +} + +} + +#endif diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp index 9bc61b7e2b..c76764167a 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.cpp @@ -77,8 +77,101 @@ struct ReportFeatures { 0, 0, 0, 0, 23 }, }; -// array of accel data to emulate shaking -const u8 shake_data[8] = { 0x40, 0x01, 0x40, 0x80, 0xC0, 0xFF, 0xC0, 0x80 }; +void EmulateShake( u8* const accel + , ControllerEmu::Buttons* const buttons_group + , unsigned int* const shake_step ) +{ + static const u8 shake_data[] = { 0x40, 0x01, 0x40, 0x80, 0xC0, 0xFF, 0xC0, 0x80 }; + static const unsigned int btns[] = { 0x01, 0x02, 0x04 }; + unsigned int shake = 0; + + buttons_group->GetState( &shake, btns ); + for ( unsigned int i=0; i<3; ++i ) + if (shake & (1 << i)) + { + accel[i] = shake_data[shake_step[i]++]; + shake_step[i] %= sizeof(shake_data); + } + else + shake_step[i] = 0; +} + +void EmulateTilt( wm_accel* const accel + , ControllerEmu::Tilt* const tilt_group + , const accel_cal* const cal + , bool focus, bool sideways, bool upright) +{ + float roll, pitch; + tilt_group->GetState( &roll, &pitch, 0, focus ? (PI / 2) : 0 ); // 90 degrees + + // this isn't doing anything with those low bits in the calib data, o well + + const u8* const zero_g = &cal->zero_g.x; + s8 one_g[3]; + for ( unsigned int i=0; i<3; ++i ) + one_g[i] = (&cal->one_g.x)[i] - zero_g[i]; + + unsigned int ud = 0, lr = 0, fb = 0; + + // some notes that no one will understand but me :p + + // left, forward, up + // lr/ left == negative for all orientations + // ud/ up == negative for upright longways + // fb/ forward == positive for (sideways flat) + + //if (sideways) + //{ + // if (upright) + // { + // ud = 0; + // lr = 1; + // fb = 2; + // } + // else + // { + // ud = 2; + // lr = 1; + // fb = 0; + // one_g[fb] *= -1; + // } + //} + //else + //{ + // if (upright) + // { + // ud = 1; + // lr = 0; + // fb = 2; + // one_g[ud] *= -1; + // } + // else + // { + // ud = 2; + // lr = 0; + // fb = 1; + // } + //} + + // this is the above statements compacted + ud = upright ? (sideways ? 0 : 1) : 2; + lr = sideways; + fb = upright ? 2 : (sideways ? 0 : 1); + + if (sideways && !upright) + one_g[fb] *= -1; + if (!sideways && upright) + one_g[ud] *= -1; + + (&accel->x)[ud] = u8(sin( (PI / 2) - std::max( abs(roll), abs(pitch) ) ) * one_g[ud] + zero_g[ud]); + (&accel->x)[lr] = u8(sin(roll) * -one_g[lr] + zero_g[lr]); + (&accel->x)[fb] = u8(sin(pitch) * one_g[fb] + zero_g[fb]); +} + +//void EmulateSwing() +//{ +// +//} const u16 button_bitmasks[] = { @@ -115,6 +208,9 @@ void Wiimote::Reset() m_rumble_on = false; m_speaker_mute = false; + // used for some hax in Update() + m_skip_update = 0; + // will make the first Update() call send a status request // the first call to RequestStatus() will then set up the status struct extension bit m_extension->active_extension = -1; @@ -133,10 +229,10 @@ void Wiimote::Reset() m_register[0xa60000].resize(WIIMOTE_REG_EXT_SIZE,0); m_register[0xB00000].resize(WIIMOTE_REG_IR_SIZE,0); - m_reg_speaker = (SpeakerConfig*)&m_register[0xa20000][0]; - m_reg_ext = &m_register[0xa40000][0]; + m_reg_speaker = (SpeakerReg*)&m_register[0xa20000][0]; + m_reg_ext = (ExtensionReg*)&m_register[0xa40000][0]; m_reg_motion_plus = &m_register[0xa60000][0]; - m_reg_ir = &m_register[0xB00000][0]; + m_reg_ir = (IrReg*)&m_register[0xB00000][0]; // testing //memcpy( m_reg_motion_plus + 0xfa, motion_plus_id, sizeof(motion_plus_id) ); @@ -150,7 +246,8 @@ void Wiimote::Reset() // 0x55 - 0xff: level 4 m_status.battery = 0x5f; - m_shake_step = 0; + memset(m_shake_step, 0, sizeof(m_shake_step)); + memset(m_swing_step, 0, sizeof(m_swing_step)); // clear read request queue while (m_read_requests.size()) @@ -160,7 +257,9 @@ void Wiimote::Reset() } } -Wiimote::Wiimote( const unsigned int index ) : m_index(index) +Wiimote::Wiimote( const unsigned int index ) + : m_index(index) +// , m_sound_stream( NULL ) { // ---- set up all the controls ---- @@ -171,12 +270,11 @@ Wiimote::Wiimote( const unsigned int index ) : m_index(index) // ir groups.push_back( m_ir = new Cursor( "IR", &g_WiimoteInitialize ) ); - m_ir->controls.push_back( new ControlGroup::Input( "Forward" ) ); - m_ir->controls.push_back( new ControlGroup::Input( "Hide" ) ); - // forces - groups.push_back( m_tilt = new Tilt( "Pitch and Roll" ) ); - //groups.push_back( m_tilt = new Tilt( "Tilt" ) ); + // tilt + groups.push_back( m_tilt = new Tilt( "Tilt" ) ); + + // swing //groups.push_back( m_swing = new Force( "Swing" ) ); // shake @@ -193,20 +291,46 @@ Wiimote::Wiimote( const unsigned int index ) : m_index(index) m_extension->attachments.push_back( new WiimoteEmu::Guitar() ); m_extension->attachments.push_back( new WiimoteEmu::Drums() ); + // rumble + groups.push_back( m_rumble = new ControlGroup( "Rumble" ) ); + m_rumble->controls.push_back( new ControlGroup::Output( "Motor" ) ); + // dpad groups.push_back( m_dpad = new Buttons( "D-Pad" ) ); for ( unsigned int i=0; i < 4; ++i ) m_dpad->controls.push_back( new ControlGroup::Input( named_directions[i] ) ); - // rumble - groups.push_back( m_rumble = new ControlGroup( "Rumble" ) ); - m_rumble->controls.push_back( new ControlGroup::Output( "Motor" ) ); - // options groups.push_back( m_options = new ControlGroup( "Options" ) ); m_options->settings.push_back( new ControlGroup::Setting( "Background Input", false ) ); m_options->settings.push_back( new ControlGroup::Setting( "Sideways Wiimote", false ) ); + m_options->settings.push_back( new ControlGroup::Setting( "Upright Wiimote", false ) ); + +#ifdef USE_WIIMOTE_EMU_SPEAKER + // set up speaker stuff + // this doesnt belong here + // TODO: i never clean up any of this audio stuff + + if (0 == m_index) // very dumb + { + ALCdevice* pDevice; + ALchar DeviceName[] = "DirectSound3D"; + pDevice = alcOpenDevice(DeviceName); + ALCcontext* pContext; + pContext = alcCreateContext(pDevice, NULL); + alcMakeContextCurrent(pContext); + } + + alListener3f(AL_POSITION, 0.0, 0.0, 0.0); + alListener3f(AL_VELOCITY, 0.0, 0.0, 0.0); + alListener3f(AL_DIRECTION, 0.0, 0.0, 0.0); + + alGenSources(1, &m_audio_source); + alSourcef(m_audio_source, AL_PITCH, 1.0); + alSourcef(m_audio_source, AL_GAIN, 1.0); + alSourcei(m_audio_source, AL_LOOPING, false); +#endif // --- reset eeprom/register/values to default --- Reset(); @@ -219,18 +343,37 @@ std::string Wiimote::GetName() const void Wiimote::Update() { - const bool is_sideways = m_options->settings[1]->value > 0; + const bool is_sideways = m_options->settings[1]->value > 0; + const bool is_upright = m_options->settings[2]->value > 0; // if windows is focused or background input is enabled - const bool focus = g_WiimoteInitialize.pRendererHasFocus() || (m_options->settings[0]->value != 0); + const bool is_focus = g_WiimoteInitialize.pRendererHasFocus() || (m_options->settings[0]->value != 0); // no rumble if no focus - if (false == focus) + if (false == is_focus) m_rumble_on = false; m_rumble->controls[0]->control_ref->State(m_rumble_on); - // testing speaker stuff + // ----speaker---- +#ifdef USE_WIIMOTE_EMU_SPEAKER + + ALint processed = 0; + alGetSourcei(m_audio_source, AL_BUFFERS_PROCESSED, &processed); + + while (processed--) + { + //PanicAlert("Buffer Processed"); + alSourceUnqueueBuffers(m_audio_source, 1, &m_audio_buffers.front().buffer); + alDeleteBuffers(1, &m_audio_buffers.front().buffer); + delete[] m_audio_buffers.front().samples; + m_audio_buffers.pop(); + } + + // testing speaker crap //m_rumble->controls[0]->control_ref->State( m_speaker_data.size() > 0 ); + //if ( m_speaker_data.size() ) + //m_speaker_data.pop(); + //while ( m_speaker_data.size() ) //{ // std::ofstream file; @@ -239,12 +382,11 @@ void Wiimote::Update() // file.close(); // m_speaker_data.pop(); //} - //if ( m_speaker_data.size() ) - // m_speaker_data.pop(); +#endif // update buttons in status struct m_status.buttons = 0; - if ( focus ) + if (is_focus) { m_buttons->GetState( &m_status.buttons, button_bitmasks ); m_dpad->GetState( &m_status.buttons, is_sideways ? dpad_sideways_bitmasks : dpad_bitmasks ); @@ -283,12 +425,29 @@ void Wiimote::Update() m_reporting_auto = false; } - if ( false == m_reporting_auto ) + if (false == m_reporting_auto) return; + // Some hax to skip a few update cycles after a change in reporting mode + // It fixes the nunchuk prob in ztp and wii sports. I have no idea why + // maybe its an m_reporting_channel problem? + if (m_skip_update) + { + --m_skip_update; + return; + } + // figure out what data we need const ReportFeatures& rpt = reporting_mode_features[m_reporting_mode - WM_REPORT_CORE]; + // what does the real wiimote do when put in a reporting mode with extension data, + // but with no extension attached? should i just send zeros? sure + //if (rpt.ext && (m_extension->active_extension <= 0)) + //{ + // m_reporting_auto = false; + // return; + //} + // set up output report // made data bigger than needed in case the wii specifies the wrong ir mode for a reporting mode u8 data[46]; @@ -301,79 +460,63 @@ void Wiimote::Update() if (rpt.core) *(wm_core*)(data + rpt.core) = m_status.buttons; - // accelerometer + // ----accelerometer---- if (rpt.accel) { - // tilt - float x, y; - m_tilt->GetState( &x, &y, 0, focus ? (PI / 2) : 0 ); // 90 degrees + // ----TILT---- + EmulateTilt((wm_accel*)&data[rpt.accel], m_tilt, (accel_cal*)&m_eeprom[0x16], is_focus, is_sideways, is_upright ); - // this isn't doing anything with those low bits in the calib data, o well - - const accel_cal* const cal = (accel_cal*)&m_eeprom[0x16]; - const u8* const zero_g = &cal->zero_g.x; - u8 one_g[3]; - for ( unsigned int i=0; i<3; ++i ) - one_g[i] = (&cal->one_g.x)[i] - zero_g[i]; - - // this math should be good enough :P - data[rpt.accel + 2] = u8(sin( (PI / 2) - std::max( abs(x), abs(y) ) ) * one_g[2] + zero_g[2]); - - if (is_sideways) - { - data[rpt.accel + 0] = u8(sin(y) * -one_g[1] + zero_g[1]); - data[rpt.accel + 1] = u8(sin(x) * -one_g[0] + zero_g[0]); - } - else - { - data[rpt.accel + 0] = u8(sin(x) * -one_g[0] + zero_g[0]); - data[rpt.accel + 1] = u8(sin(y) * one_g[1] + zero_g[1]); - } - - // shake - const unsigned int btns[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 }; - unsigned int shake = 0; - if (focus) - m_shake->GetState( &shake, btns ); - if (shake) - { - for ( unsigned int i=0; i<3; ++i ) - if (shake & (1 << i)) - data[rpt.accel + i] = shake_data[m_shake_step]; - m_shake_step = (m_shake_step + 1) % sizeof(shake_data); - } - else - m_shake_step = 0; - - // TODO: swing + // ----SWING---- + //const s8 swing_data[] = { 0x20, 0x40, 0x20, 0x00 }; //u8 swing[3]; - //m_swing->GetState( swing, 0x80, 60 ); - //for ( unsigned int i=0; i<3; ++i ) - // if ( swing[i] != 0x80 ) - // data[rpt.accel + i] = swing[i]; + //m_swing->GetState( swing, 0x80, 0x40 ); + + //// up/down + //if (swing[0] != 0x80) + //{ + // //data[rpt.accel + 0] = swing[0]; + // data[rpt.accel + 2] += swing_data[m_swing_step[0]/4]; + // if (m_swing_step[0] < 12) + // ++m_swing_step[0]; + //} + //else + // m_swing_step[0] = 0; + + //// left/right + //if (swing[1] != 0x80) + // data[rpt.accel + !is_sideways] = swing[1]; + + //// forward/backward + //if (swing[2] != 0x80) + // data[rpt.accel + is_sideways] = swing[2]; + + // ----SHAKE---- + if (is_focus) + EmulateShake(data + rpt.accel, m_shake, m_shake_step); } - // extension + // ----extension---- if (rpt.ext) { - m_extension->GetState(data + rpt.ext, focus); - - // both of these ifs work - //if ( m_reg_ext[0xf0] != 0x55 ) - if ( m_reg_ext[0xf0] == 0xaa ) - wiimote_encrypt(&m_ext_key, data + rpt.ext, 0x00, sizeof(wm_extension)); + m_extension->GetState(data + rpt.ext, is_focus); // i dont think anything accesses the extension data like this, but ill support it - memcpy(m_reg_ext + 8, data + rpt.ext, sizeof(wm_extension)); + // i think it should be unencrpyted in the register, encrypted when read + memcpy(m_reg_ext->controller_data, data + rpt.ext, sizeof(wm_extension)); + + // both of these ifs work + //if (0x55 != m_reg_ext->encryption) + if (0xAA == m_reg_ext->encryption) + wiimote_encrypt(&m_ext_key, data + rpt.ext, 0x00, sizeof(wm_extension)); } - // ir + // ----ir---- if (rpt.ir) { float xx = 10000, yy = 0, zz = 0; - if (focus) + if (is_focus) m_ir->GetState(&xx, &yy, &zz, true); xx *= (-256 * 0.95f); @@ -394,7 +537,7 @@ void Wiimote::Update() x[3] = (unsigned int)(xx + 1.2f * distance); // ir mode - switch (m_reg_ir[0x33]) + switch (m_reg_ir->mode) { // basic case 1 : @@ -432,8 +575,8 @@ void Wiimote::Update() wm_ir_extended* const irdata = (wm_ir_extended*)(data + rpt.ir); if (y < 768) { - for ( unsigned int i=0; i<2; ++i ) - if (irdata[i].x < 1024) + for ( unsigned int i=0; i<4; ++i ) + if (x[i] < 1024) { irdata[i].x = u8(x[i]); irdata[i].xhi = x[i] >> 8; @@ -542,25 +685,26 @@ void Wiimote::InterruptChannel(const u16 _channelID, const void* _pData, u32 _Si // TODO: i need to test this void Wiimote::Register::Read( size_t address, void* dst, size_t length ) { + const_iterator i = begin(); + const const_iterator e = end(); while (length) { const std::vector* block = NULL; size_t addr_start = 0; size_t addr_end = address+length; - // TODO: don't need to start at begin() each time // find block and start of next block - const_iterator - i = begin(), - e = end(); for ( ; i!=e; ++i ) + // if address is inside or after this block if ( address >= i->first ) { block = &i->second; addr_start = i->first; } + // if address is before this block else { + // how far til the start of the next block addr_end = std::min( i->first, addr_end ); break; } @@ -568,7 +712,9 @@ void Wiimote::Register::Read( size_t address, void* dst, size_t length ) // read bytes from a mapped block if (block) { + // offset of wanted data in the vector const size_t offset = std::min( address - addr_start, block->size() ); + // how much data we can read depending on the vector size and how much we want const size_t amt = std::min( block->size()-offset, length ); memcpy( dst, &block->operator[](offset), amt ); @@ -592,25 +738,26 @@ void Wiimote::Register::Read( size_t address, void* dst, size_t length ) // TODO: i need to test this void Wiimote::Register::Write( size_t address, void* src, size_t length ) { + iterator i = begin(); + const const_iterator e = end(); while (length) { std::vector* block = NULL; size_t addr_start = 0; size_t addr_end = address+length; - // TODO: don't need to start at begin() each time // find block and start of next block - iterator - i = begin(), - e = end(); for ( ; i!=e; ++i ) + // if address is inside or after this block if ( address >= i->first ) { block = &i->second; addr_start = i->first; } + // if address is before this block else { + // how far til the start of the next block addr_end = std::min( i->first, addr_end ); break; } @@ -618,7 +765,9 @@ void Wiimote::Register::Write( size_t address, void* src, size_t length ) // write bytes to a mapped block if (block) { + // offset of wanted data in the vector const size_t offset = std::min( address - addr_start, block->size() ); + // how much data we can read depending on the vector size and how much we want const size_t amt = std::min( block->size()-offset, length ); memcpy( &block->operator[](offset), src, amt ); diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h index eb617d66ac..ab6354dfce 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteEmu.h @@ -1,6 +1,11 @@ #ifndef _CONEMU_WIIMOTE_H_ #define _CONEMU_WIIMOTE_H_ +//#define USE_WIIMOTE_EMU_SPEAKER + +// just used to get the OpenAL includes :p +//#include + #include #include "WiimoteHid.h" @@ -23,7 +28,14 @@ extern SWiimoteInitialize g_WiimoteInitialize; namespace WiimoteEmu { -extern const u8 shake_data[8]; +void EmulateShake( u8* const accel_data + , ControllerEmu::Buttons* const buttons_group + , unsigned int* const shake_step ); + +void EmulateTilt( wm_accel* const accel + , ControllerEmu::Tilt* const tilt_group + , const accel_cal* const cal + , bool focus, bool sideways = false, bool upright = false); class Wiimote : public ControllerEmu { @@ -53,6 +65,10 @@ private: void ReadData(const u16 _channelID, wm_read_data* rd); void SendReadDataReply(const u16 _channelID, ReadRequest& _request); +#ifdef USE_WIIMOTE_EMU_SPEAKER + void SpeakerData(wm_speaker_data* sd); +#endif + // control groups Buttons* m_buttons; Buttons* m_dpad; @@ -74,7 +90,11 @@ private: unsigned int m_reporting_mode; unsigned int m_reporting_channel; - unsigned int m_shake_step; + // hax + unsigned int m_skip_update; + + unsigned int m_shake_step[3]; + unsigned int m_swing_step[3]; wm_status_report m_status; @@ -91,18 +111,61 @@ private: // maybe read requests cancel any current requests std::queue< ReadRequest > m_read_requests; - //std::queue< u8 > m_speaker_data; +#ifdef USE_WIIMOTE_EMU_SPEAKER + // speaker stuff + struct SoundBuffer + { + s16* samples; + ALuint buffer; + }; + std::queue m_audio_buffers; + ALuint m_audio_source; + ADPCMChannelStatus m_channel_status; +#endif u8 m_eeprom[WIIMOTE_EEPROM_SIZE]; - u8* m_reg_ext; - u8* m_reg_ir; + u8* m_reg_motion_plus; - struct SpeakerConfig + + struct IrReg { - u16 : 16; + u8 unknown1[0x33]; + u8 mode; + + } *m_reg_ir; + + struct ExtensionReg + { + u8 unknown1[0x08]; + + // address 0x08 + u8 controller_data[0x06]; + u8 unknown2[0x12]; + + // address 0x20 + u8 calibration[0x10]; + u8 unknown3[0x10]; + + // address 0x40 + u8 encryption_key[0x10]; + u8 unknown4[0xA0]; + + // address 0xF0 + u8 encryption; + u8 unknown5[0x9]; + + // address 0xFA + u8 constant_id[6]; + + } *m_reg_ext; + + struct SpeakerReg + { + u16 unknown; u8 format; u16 sample_rate; u8 volume; + u8 unk[4]; } *m_reg_speaker; diff --git a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteHid.h b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteHid.h index cdaa4e89e3..fccb08b552 100644 --- a/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteHid.h +++ b/Source/Plugins/Plugin_WiimoteNew/Src/WiimoteEmu/WiimoteHid.h @@ -285,7 +285,8 @@ struct wm_report_ext21 #define WM_SPEAKER_MUTE 0x19 #define WM_WRITE_SPEAKER_DATA 0x18 struct wm_speaker_data { - u8 length; // shifted left by three bits + u8 unknown : 3; + u8 length : 5; u8 data[20]; };