Fix minor issue with real wiimote data report handling.

This commit is contained in:
Jordan Woyak 2013-04-04 19:34:00 -05:00
parent a2ebb2b324
commit 99da297951
3 changed files with 66 additions and 76 deletions

View File

@ -707,10 +707,10 @@ void Wiimote::Update()
std::lock_guard<std::recursive_mutex> lk(g_refresh_lock); std::lock_guard<std::recursive_mutex> lk(g_refresh_lock);
if (g_wiimotes[m_index]) if (g_wiimotes[m_index])
{ {
Report rpt = g_wiimotes[m_index]->ProcessReadQueue(); const Report& rpt = g_wiimotes[m_index]->ProcessReadQueue();
const u8 *real_data = rpt.first; if (!rpt.empty())
if (real_data)
{ {
const u8 *real_data = rpt.data();
switch (real_data[1]) switch (real_data[1])
{ {
// use data reports // use data reports
@ -770,13 +770,9 @@ void Wiimote::Update()
// copy over report from real-wiimote // copy over report from real-wiimote
if (-1 == rptf_size) if (-1 == rptf_size)
{ {
memcpy(data, real_data, rpt.second); std::copy(rpt.begin(), rpt.end(), data);
rptf_size = rpt.second; rptf_size = rpt.size();
} }
if (real_data != g_wiimotes[m_index]->\
m_last_data_report.first)
delete[] real_data;
} }
} }
} }

View File

@ -57,7 +57,8 @@ Wiimote::Wiimote()
#elif defined(_WIN32) #elif defined(_WIN32)
, dev_handle(0), stack(MSBT_STACK_UNKNOWN) , dev_handle(0), stack(MSBT_STACK_UNKNOWN)
#endif #endif
, m_last_data_report(Report((u8 *)NULL, 0)) , m_last_data_report()
, m_current_report()
, m_channel(0), m_run_thread(false) , m_channel(0), m_run_thread(false)
{ {
#if defined(__linux__) && HAVE_BLUEZ #if defined(__linux__) && HAVE_BLUEZ
@ -73,11 +74,7 @@ Wiimote::~Wiimote()
Disconnect(); Disconnect();
ClearReadQueue(); ClearReadQueue();
m_write_reports.Clear();
// clear write queue
Report rpt;
while (m_write_reports.Pop(rpt))
delete[] rpt.first;
} }
// to be called from CPU thread // to be called from CPU thread
@ -85,12 +82,10 @@ void Wiimote::QueueReport(u8 rpt_id, const void* _data, unsigned int size)
{ {
auto const data = static_cast<const u8*>(_data); auto const data = static_cast<const u8*>(_data);
Report rpt; Report rpt(size + 2);
rpt.second = size + 2; rpt[0] = WM_SET_REPORT | WM_BT_OUTPUT;
rpt.first = new u8[rpt.second]; rpt[1] = rpt_id;
rpt.first[0] = WM_SET_REPORT | WM_BT_OUTPUT; std::copy_n(data, size, rpt.begin() + 2);
rpt.first[1] = rpt_id;
std::copy(data, data + size, rpt.first + 2);
m_write_reports.Push(rpt); m_write_reports.Push(rpt);
} }
@ -108,14 +103,9 @@ void Wiimote::ClearReadQueue()
{ {
Report rpt; Report rpt;
if (m_last_data_report.first) // The "Clear" function isn't thread-safe :/
{
delete[] m_last_data_report.first;
m_last_data_report.first = NULL;
}
while (m_read_reports.Pop(rpt)) while (m_read_reports.Pop(rpt))
delete[] rpt.first; {}
} }
void Wiimote::ControlChannel(const u16 channel, const void* const data, const u32 size) void Wiimote::ControlChannel(const u16 channel, const void* const data, const u32 size)
@ -148,39 +138,35 @@ void Wiimote::InterruptChannel(const u16 channel, const void* const _data, const
} }
auto const data = static_cast<const u8*>(_data); auto const data = static_cast<const u8*>(_data);
Report rpt(data, data + size);
Report rpt;
rpt.first = new u8[size];
rpt.second = (u8)size;
std::copy(data, data + size, rpt.first);
// Convert output DATA packets to SET_REPORT packets. // Convert output DATA packets to SET_REPORT packets.
// Nintendo Wiimotes work without this translation, but 3rd // Nintendo Wiimotes work without this translation, but 3rd
// party ones don't. // party ones don't.
if (rpt.first[0] == 0xa2) if (rpt[0] == 0xa2)
{ {
rpt.first[0] = WM_SET_REPORT | WM_BT_OUTPUT; rpt[0] = WM_SET_REPORT | WM_BT_OUTPUT;
} }
// Disallow games from turning off all of the LEDs. // Disallow games from turning off all of the LEDs.
// It makes Wiimote connection status confusing. // It makes Wiimote connection status confusing.
if (rpt.first[1] == WM_LEDS) if (rpt[1] == WM_LEDS)
{ {
auto& leds_rpt = *reinterpret_cast<wm_leds*>(&rpt.first[2]); auto& leds_rpt = *reinterpret_cast<wm_leds*>(&rpt[2]);
if (0 == leds_rpt.leds) if (0 == leds_rpt.leds)
{ {
// Turn on ALL of the LEDs. // Turn on ALL of the LEDs.
leds_rpt.leds = 0xf; leds_rpt.leds = 0xf;
} }
} }
else if (rpt.first[1] == WM_WRITE_SPEAKER_DATA else if (rpt[1] == WM_WRITE_SPEAKER_DATA
&& !SConfig::GetInstance().m_WiimoteEnableSpeaker) && !SConfig::GetInstance().m_WiimoteEnableSpeaker)
{ {
// Translate speaker data reports into rumble reports. // Translate speaker data reports into rumble reports.
rpt.first[1] = WM_CMD_RUMBLE; rpt[1] = WM_CMD_RUMBLE;
// Keep only the rumble bit. // Keep only the rumble bit.
rpt.first[2] &= 0x1; rpt[2] &= 0x1;
rpt.second = 3; rpt.resize(3);
} }
m_write_reports.Push(rpt); m_write_reports.Push(rpt);
@ -188,25 +174,22 @@ void Wiimote::InterruptChannel(const u16 channel, const void* const _data, const
bool Wiimote::Read() bool Wiimote::Read()
{ {
Report rpt; Report rpt(MAX_PAYLOAD);
auto const result = IORead(rpt.data());
rpt.first = new unsigned char[MAX_PAYLOAD]; if (result > 0 && m_channel > 0)
rpt.second = IORead(rpt.first); {
// Add it to queue
if (0 == rpt.second) rpt.resize(result);
m_read_reports.Push(std::move(rpt));
return true;
}
else if (0 == result)
{ {
WARN_LOG(WIIMOTE, "Wiimote::IORead failed. Disconnecting Wiimote %d.", index + 1); WARN_LOG(WIIMOTE, "Wiimote::IORead failed. Disconnecting Wiimote %d.", index + 1);
Disconnect(); Disconnect();
} }
if (rpt.second > 0 && m_channel > 0)
{
// Add it to queue
m_read_reports.Push(rpt);
return true;
}
delete[] rpt.first;
return false; return false;
} }
@ -216,16 +199,15 @@ bool Wiimote::Write()
{ {
Report const& rpt = m_write_reports.Front(); Report const& rpt = m_write_reports.Front();
bool const is_speaker_data = rpt.first[1] == WM_WRITE_SPEAKER_DATA; bool const is_speaker_data = rpt[1] == WM_WRITE_SPEAKER_DATA;
if (!is_speaker_data || m_last_audio_report.GetTimeDifference() > 5) if (!is_speaker_data || m_last_audio_report.GetTimeDifference() > 5)
{ {
IOWrite(rpt.first, rpt.second); IOWrite(rpt.data(), rpt.size());
if (is_speaker_data) if (is_speaker_data)
m_last_audio_report.Update(); m_last_audio_report.Update();
delete[] rpt.first;
m_write_reports.Pop(); m_write_reports.Pop();
return true; return true;
} }
@ -235,22 +217,38 @@ bool Wiimote::Write()
} }
// Returns the next report that should be sent // Returns the next report that should be sent
Report Wiimote::ProcessReadQueue() const Report& Wiimote::ProcessReadQueue()
{ {
// Pop through the queued reports // Pop through the queued reports
Report rpt = m_last_data_report; while (m_read_reports.Pop(m_current_report))
while (m_read_reports.Pop(rpt)) {
if (m_current_report[1] >= WM_REPORT_CORE)
{ {
if (rpt.first[1] >= WM_REPORT_CORE)
// A data report // A data report
m_last_data_report = rpt; m_last_data_report.swap(m_current_report);
}
else else
{
// Some other kind of report // Some other kind of report
return rpt;
// If this input report is an "ack" for setting the data reporting mode,
// then drop m_last_data_report as it may be of the wrong type
if (WM_ACK_DATA == m_current_report[1] && WM_REPORT_MODE == m_current_report[4])
{
m_last_data_report.clear();
}
// Copy button data (which is included in every input report except WM_REPORT_EXT21)
// needed to prevent rare spurious presses/releases.
if (m_last_data_report.size() >= 4 && m_last_data_report[1] != WM_REPORT_EXT21)
std::copy_n(m_current_report.begin() + 2, 2, m_last_data_report.begin() + 2);
return m_current_report;
}
} }
// The queue was empty, or there were only data reports // The queue was empty, or there were only data reports
return rpt; return m_last_data_report;
} }
void Wiimote::Update() void Wiimote::Update()
@ -262,16 +260,12 @@ void Wiimote::Update()
} }
// Pop through the queued reports // Pop through the queued reports
Report const rpt = ProcessReadQueue(); const Report& rpt = ProcessReadQueue();
// Send the report // Send the report
if (rpt.first != NULL && m_channel > 0) if (!rpt.empty() && m_channel > 0)
Core::Callback_WiimoteInterruptChannel(index, m_channel, Core::Callback_WiimoteInterruptChannel(index, m_channel,
rpt.first, rpt.second); rpt.data(), rpt.size());
// Delete the data if it isn't also the last data rpt
if (rpt != m_last_data_report)
delete[] rpt.first;
} }
bool Wiimote::Prepare(int _index) bool Wiimote::Prepare(int _index)

View File

@ -33,8 +33,7 @@
#include "../../InputCommon/Src/InputConfig.h" #include "../../InputCommon/Src/InputConfig.h"
// Pointer to data, and size of data typedef std::vector<u8> Report;
typedef std::pair<u8*,u8> Report;
namespace WiimoteReal namespace WiimoteReal
{ {
@ -50,7 +49,7 @@ public:
void InterruptChannel(const u16 channel, const void* const data, const u32 size); void InterruptChannel(const u16 channel, const void* const data, const u32 size);
void Update(); void Update();
Report ProcessReadQueue(); const Report& ProcessReadQueue();
bool Read(); bool Read();
bool Write(); bool Write();
@ -100,6 +99,7 @@ public:
protected: protected:
Report m_last_data_report; Report m_last_data_report;
Report m_current_report;
u16 m_channel; u16 m_channel;
private: private: