Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'wiimote-netplay'
Conflicts:
	Source/Core/Core/Src/NetPlayClient.cpp
	Source/Core/Core/Src/NetPlayClient.h
	Source/Core/Core/Src/NetPlayProto.h
	Source/Core/Core/Src/NetPlayServer.cpp
	Source/Core/Core/Src/NetPlayServer.h
	Source/Core/DolphinWX/Src/NetWindow.cpp
	Source/Core/DolphinWX/Src/NetWindow.h
  • Loading branch information
RachelBryk committed Sep 22, 2013
2 parents bdae5d1 + d3894a0 commit f3469c1
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 59 deletions.
11 changes: 9 additions & 2 deletions Source/Core/Core/Src/HW/WiimoteEmu/WiimoteEmu.cpp
Expand Up @@ -27,6 +27,7 @@ inline double round(double x) { return (x-floor(x))>0.5 ? ceil(x) : floor(x); }
#include "MatrixMath.h"

#include "../../Movie.h"
#include "NetPlayClient.h"

namespace
{
Expand Down Expand Up @@ -339,7 +340,7 @@ bool Wiimote::Step()
m_rumble->controls[0]->control_ref->State(m_rumble_on);

// when a movie is active, this button status update is disabled (moved), because movies only record data reports.
if(!(Movie::IsPlayingInput() || Movie::IsRecordingInput()))
if(!(Movie::IsPlayingInput() || Movie::IsRecordingInput()) || NetPlay::IsNetPlayRunning())
{
UpdateButtonsStatus(has_focus);
}
Expand Down Expand Up @@ -397,7 +398,7 @@ void Wiimote::UpdateButtonsStatus(bool has_focus)
void Wiimote::GetCoreData(u8* const data)
{
// when a movie is active, the button update happens here instead of Wiimote::Step, to avoid potential desync issues.
if(Movie::IsPlayingInput() || Movie::IsRecordingInput())
if(Movie::IsPlayingInput() || Movie::IsRecordingInput() || NetPlay::IsNetPlayRunning())
{
UpdateButtonsStatus(HAS_FOCUS);
}
Expand Down Expand Up @@ -770,6 +771,12 @@ void Wiimote::Update()
}
}
}
if (NetPlay::IsNetPlayRunning())
{
NetPlay_GetWiimoteData(m_index, data, rptf.size);
if (rptf.core)
m_status.buttons = *(wm_core*)(data + rptf.core);
}
if (!Movie::IsPlayingInput())
{
Movie::CheckWiimoteStatus(m_index, data, rptf, m_reg_ir.mode);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core/Src/HW/WiimoteEmu/WiimoteEmu.h
Expand Up @@ -158,6 +158,7 @@ friend void Spy(Wiimote* wm_, const void* data_, int size_);
void WriteData(const wm_write_data* const wd);
void SendReadDataReply(ReadRequest& _request);
void SpeakerData(wm_speaker_data* sd);
bool NetPlay_GetWiimoteData(int wiimote, u8* data, u8 size);

// control groups
Buttons *m_buttons, *m_dpad, *m_shake;
Expand Down
1 change: 0 additions & 1 deletion Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_usb.cpp
Expand Up @@ -465,7 +465,6 @@ u32 CWII_IPC_HLE_Device_usb_oh1_57e_305::Update()
for (unsigned int i = 0; i < m_WiiMotes.size(); i++)
if (m_WiiMotes[i].IsConnected())
{
NetPlay_WiimoteUpdate(i);
Wiimote::Update(i);
}
m_last_ticks = now;
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.cpp
Expand Up @@ -265,7 +265,7 @@ void CWII_IPC_HLE_WiiMote::ExecuteL2capCmd(u8* _pData, u32 _Size)
_dbg_assert_msg_(WII_IPC_WIIMOTE, DoesChannelExist(pHeader->dcid), "L2CAP: SendACLPacket to unknown channel %i", pHeader->dcid);
CChannelMap::iterator itr= m_Channel.find(pHeader->dcid);

const int number = NetPlay_GetWiimoteNum(m_ConnectionHandle & 0xFF);
const int number = m_ConnectionHandle & 0xFF;

if (itr != m_Channel.end())
{
Expand Down Expand Up @@ -862,8 +862,6 @@ void CWII_IPC_HLE_WiiMote::SendCommandToACL(u8 _Ident, u8 _Code, u8 _CommandLeng

void CWII_IPC_HLE_WiiMote::ReceiveL2capData(u16 scid, const void* _pData, u32 _Size)
{
if (NetPlay_WiimoteInput(m_ConnectionHandle & 0xFF, scid, _pData, _Size))
return;

// Allocate DataFrame
u8 DataFrame[1024];
Expand Down
1 change: 0 additions & 1 deletion Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_WiiMote.h
Expand Up @@ -56,7 +56,6 @@ class CWII_IPC_HLE_WiiMote
void ReceiveL2capData(u16 scid, const void* _pData, u32 _Size); // From wiimote

int NetPlay_GetWiimoteNum(int _number);
bool NetPlay_WiimoteInput(int _number, u16 _channelID, const void* _pData, u32& _Size);

void EventConnectionAccepted();
void EventDisconnect();
Expand Down
224 changes: 184 additions & 40 deletions Source/Core/Core/Src/NetPlayClient.cpp
Expand Up @@ -18,6 +18,7 @@
#include "Core.h"
#include "ConfigManager.h"
#include "Movie.h"
#include "HW/WiimoteEmu/WiimoteEmu.h"

std::mutex crit_netplay_client;
static NetPlayClient * netplay_client = NULL;
Expand Down Expand Up @@ -195,6 +196,15 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
}
break;

case NP_MSG_WIIMOTE_MAPPING :
{
for (PadMapping i = 0; i < 4; i++)
packet >> m_wiimote_map[i];

m_dialog->Update();
}
break;

case NP_MSG_PAD_DATA :
{
PadMapping map = 0;
Expand All @@ -207,6 +217,27 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
}
break;

case NP_MSG_WIIMOTE_DATA :
{
PadMapping map = 0;
NetWiimote nw;
u8 size;
packet >> map >> size;

nw.resize(size);
u8* data = new u8[size];
for (unsigned int i = 0; i < size; ++i)
packet >> data[i];
nw.assign(data,data+size);
delete[] data;

// trusting server for good map value (>=0 && <4)
// add to wiimote buffer
m_wiimote_buffer[(unsigned)map].Push(nw);
}
break;


case NP_MSG_PAD_BUFFER :
{
u32 size = 0;
Expand Down Expand Up @@ -353,6 +384,13 @@ void NetPlayClient::GetPlayerList(std::string& list, std::vector<int>& pid_list)
else
ss << '-';
}
for (unsigned int j = 0; j < 4; j++)
{
if (m_wiimote_map[j] == player->pid)
ss << j + 1;
else
ss << '-';
}
ss << " | " << player->ping << "ms\n";
pid_list.push_back(player->pid);
}
Expand Down Expand Up @@ -399,6 +437,22 @@ void NetPlayClient::SendPadState(const PadMapping in_game_pad, const NetPad& np)
m_socket.Send(spac);
}

// called from ---CPU--- thread
void NetPlayClient::SendWiimoteState(const PadMapping in_game_pad, const NetWiimote& nw)
{
// send to server
sf::Packet spac;
spac << (MessageId)NP_MSG_WIIMOTE_DATA;
spac << in_game_pad;
u8 size = nw.size();
spac << size;
for (unsigned int i = 0; i < size; ++i)
spac << nw.data()[i];

std::lock_guard<std::recursive_mutex> lks(m_crit.send);
m_socket.Send(spac);
}

// called from ---GUI--- thread
bool NetPlayClient::StartGame(const std::string &path)
{
Expand Down Expand Up @@ -446,11 +500,17 @@ bool NetPlayClient::StartGame(const std::string &path)

UpdateDevices();

// temporary
// Needed to prevent locking up at boot if (when) the wiimotes connect out of order.
NetWiimote nw;
for (unsigned int i = 0; i<4; ++i)
for (unsigned int f = 0; f<2; ++f)
m_wiimote_buffer[i].Push(nw);
nw.resize(4, 0);

for (unsigned int w = 0; w < 4; ++w)
{
if (m_wiimote_map[w] != -1)
// probably overkill, but whatever
for (unsigned int i = 0; i < 7; ++i)
m_wiimote_buffer[w].Push(nw);
}

return true;
}
Expand Down Expand Up @@ -482,8 +542,6 @@ void NetPlayClient::ClearBuffers()

while (m_wiimote_buffer[i].Size())
m_wiimote_buffer[i].Pop();

m_wiimote_input[i].clear();
}
}

Expand Down Expand Up @@ -570,16 +628,95 @@ bool NetPlayClient::GetNetPads(const u8 pad_nb, const SPADStatus* const pad_stat
return true;
}

// called from ---CPU--- thread
void NetPlayClient::WiimoteInput(int _number, u16 _channelID, const void* _pData, u32 _Size)
{
// XXX
}

// called from ---CPU--- thread
void NetPlayClient::WiimoteUpdate(int _number)
bool NetPlayClient::WiimoteUpdate(int _number, u8* data, const u8 size)
{
// XXX
NetWiimote nw;
static u8 previousSize[4] = {4,4,4,4};
{
std::lock_guard<std::recursive_mutex> lkp(m_crit.players);

// in game mapping for this local wiimote
unsigned int in_game_num = LocalWiimoteToInGameWiimote(_number);

// does this local wiimote map in game?
if (in_game_num < 4)
{
if (previousSize[in_game_num] == size)
{
nw.assign(data, data + size);
do
{
// add to buffer
m_wiimote_buffer[in_game_num].Push(nw);

SendWiimoteState(in_game_num, nw);
} while (m_wiimote_buffer[in_game_num].Size() <= m_target_buffer_size * 200 / 120); // TODO: add a seperate setting for wiimote buffer?
}
else
{
while (m_wiimote_buffer[in_game_num].Size() > 0)
{
// Reporting mode changed, so previous buffer is no good.
m_wiimote_buffer[in_game_num].Pop();
}
nw.resize(size, 0);

m_wiimote_buffer[in_game_num].Push(nw);
m_wiimote_buffer[in_game_num].Push(nw);
m_wiimote_buffer[in_game_num].Push(nw);
previousSize[in_game_num] = size;
}
}

} // unlock players

while (previousSize[_number] == size && !m_wiimote_buffer[_number].Pop(nw))
{
// wait for receiving thread to push some data
Common::SleepCurrentThread(1);
if (false == m_is_running)
return false;
}

// Use a blank input, since we may not have any valid input.
if (previousSize[_number] != size)
{
nw.resize(size, 0);
m_wiimote_buffer[_number].Push(nw);
m_wiimote_buffer[_number].Push(nw);
}

// We should have used a blank input last time, so now we just need to pop through the old buffer, until we reach a good input
if (nw.size() != size)
{
u8 tries = 0;
// Clear the buffer and wait for new input, since we probably just changed reporting mode.
while (nw.size() != size)
{
while (!m_wiimote_buffer[_number].Pop(nw))
{
Common::SleepCurrentThread(1);
if (false == m_is_running)
return false;
}
++tries;
if (tries > m_target_buffer_size * 200 / 120)
break;
}

// If it still mismatches, it surely desynced
if (size != nw.size())
{
PanicAlert("Netplay has desynced. There is no way to recover from this.");
return false;
}
}

previousSize[_number] = size;
memcpy(data, nw.data(), size);
return true;
}

// called from ---GUI--- thread and ---NETPLAY--- thread (client side)
Expand Down Expand Up @@ -614,6 +751,11 @@ void NetPlayClient::Stop()
if (m_pad_map[i] == m_local_player->pid)
isPadMapped = true;
}
for (unsigned int i = 0; i < 4; ++i)
{
if (m_wiimote_map[i] == m_local_player->pid)
isPadMapped = true;
}
// tell the server to stop if we have a pad mapped in game.
if (isPadMapped)
{
Expand Down Expand Up @@ -660,6 +802,25 @@ u8 NetPlayClient::LocalPadToInGamePad(u8 local_pad)
return ingame_pad;
}

u8 NetPlayClient::LocalWiimoteToInGameWiimote(u8 local_pad)
{
// Figure out which in-game pad maps to which local pad.
// The logic we have here is that the local slots always
// go in order.
int local_pad_count = -1;
int ingame_pad = 0;
for (; ingame_pad < 4; ingame_pad++)
{
if (m_wiimote_map[ingame_pad] == m_local_player->pid)
local_pad_count++;

if (local_pad_count == local_pad)
break;
}

return ingame_pad;
}

// stuff hacked into dolphin

// called from ---CPU--- thread
Expand All @@ -674,6 +835,16 @@ bool CSIDevice_GCController::NetPlay_GetInput(u8 numPAD, SPADStatus PadStatus, u
return false;
}

bool WiimoteEmu::Wiimote::NetPlay_GetWiimoteData(int wiimote, u8* data, u8 size)
{
std::lock_guard<std::mutex> lk(crit_netplay_client);

if (netplay_client)
return netplay_client->WiimoteUpdate(wiimote, data, size);
else
return false;
}

bool CSIDevice_GCSteeringWheel::NetPlay_GetInput(u8 numPAD, SPADStatus PadStatus, u32 *PADStatus)
{
return CSIDevice_GCController::NetPlay_GetInput(numPAD, PadStatus, PADStatus);
Expand Down Expand Up @@ -718,33 +889,6 @@ u8 CSIDevice_DanceMat::NetPlay_InGamePadToLocalPad(u8 numPAD)
return CSIDevice_GCController::NetPlay_InGamePadToLocalPad(numPAD);
}

// called from ---CPU--- thread
// wiimote update / used for frame counting
//void CWII_IPC_HLE_Device_usb_oh1_57e_305::NetPlay_WiimoteUpdate(int _number)
void CWII_IPC_HLE_Device_usb_oh1_57e_305::NetPlay_WiimoteUpdate(int)
{
}

// called from ---CPU--- thread
//
int CWII_IPC_HLE_WiiMote::NetPlay_GetWiimoteNum(int _number)
{
return _number;
}

// called from ---CPU--- thread
// intercept wiimote input callback
//bool CWII_IPC_HLE_WiiMote::NetPlay_WiimoteInput(int _number, u16 _channelID, const void* _pData, u32& _Size)
bool CWII_IPC_HLE_WiiMote::NetPlay_WiimoteInput(int, u16, const void*, u32&)
{
std::lock_guard<std::mutex> lk(crit_netplay_client);

if (netplay_client)
return true;
else
return false;
}

bool NetPlay::IsNetPlayRunning()
{
return netplay_client != NULL;
Expand Down

0 comments on commit f3469c1

Please sign in to comment.