some cleanup

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1227 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
nakeee 2008-11-20 14:38:32 +00:00
parent 58f998cb55
commit 12a8174446
2 changed files with 202 additions and 199 deletions

View File

@ -86,6 +86,7 @@ void GetMousePos(float& x, float& y)
x = point.x / (float)width; x = point.x / (float)width;
y = point.y / (float)height; y = point.y / (float)height;
#else #else
// TODO fix on linux
x = 0.5f; x = 0.5f;
y = 0.5f; y = 0.5f;
#endif #endif

View File

@ -36,255 +36,257 @@ namespace WiiMoteReal
{ {
#define MAX_WIIMOTES 1 #define MAX_WIIMOTES 1
//****************************************************************************** //******************************************************************************
// Forwording // Forwarding
//****************************************************************************** //******************************************************************************
class CWiiMote; class CWiiMote;
#ifdef _WIN32 #ifdef _WIN32
DWORD WINAPI ReadWiimote_ThreadFunc(void* arg); DWORD WINAPI ReadWiimote_ThreadFunc(void* arg);
#else #else
void* ReadWiimote_ThreadFunc(void* arg); void* ReadWiimote_ThreadFunc(void* arg);
#endif #endif
//****************************************************************************** //******************************************************************************
// Variable declarations // Variable declarations
//****************************************************************************** //******************************************************************************
wiimote_t** g_WiiMotesFromWiiUse = NULL; wiimote_t** g_WiiMotesFromWiiUse = NULL;
Common::Thread* g_pReadThread = NULL; Common::Thread* g_pReadThread = NULL;
int g_NumberOfWiiMotes; int g_NumberOfWiiMotes;
CWiiMote* g_WiiMotes[MAX_WIIMOTES]; CWiiMote* g_WiiMotes[MAX_WIIMOTES];
bool g_Shutdown = false; bool g_Shutdown = false;
//****************************************************************************** //******************************************************************************
// Prolly this class should be in its own file // Probably this class should be in its own file
//****************************************************************************** //******************************************************************************
class CWiiMote class CWiiMote
{ {
public: public:
CWiiMote(u8 _WiimoteNumber, wiimote_t* _pWiimote)
: m_WiimoteNumber(_WiimoteNumber)
, m_pWiiMote(_pWiimote)
, m_LastReportValid(false)
, m_channelID(0)
, m_pCriticalSection(NULL)
{
m_pCriticalSection = new Common::CriticalSection();
CWiiMote(u8 _WiimoteNumber, wiimote_t* _pWiimote) wiiuse_set_leds(m_pWiiMote, WIIMOTE_LED_4);
: m_WiimoteNumber(_WiimoteNumber)
, m_pWiiMote(_pWiimote)
, m_LastReportValid(false)
, m_channelID(0)
, m_pCriticalSection(NULL)
{
m_pCriticalSection = new Common::CriticalSection();
wiiuse_set_leds(m_pWiiMote, WIIMOTE_LED_4);
#ifdef _WIN32 #ifdef _WIN32
// F|RES: i dunno if we really need this // F|RES: i dunno if we really need this
CancelIo(m_pWiiMote->dev_handle); CancelIo(m_pWiiMote->dev_handle);
#endif #endif
} }
virtual ~CWiiMote() virtual ~CWiiMote()
{ {
delete m_pCriticalSection; delete m_pCriticalSection;
}; };
// send raw HID data from the core to wiimote // send raw HID data from the core to wiimote
void SendData(u16 _channelID, const u8* _pData, u32 _Size) void SendData(u16 _channelID, const u8* _pData, u32 _Size)
{ {
m_channelID = _channelID; m_channelID = _channelID;
m_pCriticalSection->Enter(); m_pCriticalSection->Enter();
{ {
SEvent WriteEvent; SEvent WriteEvent;
memcpy(WriteEvent.m_PayLoad, _pData+1, _Size-1); memcpy(WriteEvent.m_PayLoad, _pData+1, _Size-1);
m_EventWriteQueue.push(WriteEvent); m_EventWriteQueue.push(WriteEvent);
} }
m_pCriticalSection->Leave(); m_pCriticalSection->Leave();
} }
// read data from wiimote (but don't send it to the core, just filter and queue) // read data from wiimote (but don't send it to the core, just filter
void ReadData() // and queue)
{ void ReadData()
m_pCriticalSection->Enter(); {
m_pCriticalSection->Enter();
if (!m_EventWriteQueue.empty()) if (!m_EventWriteQueue.empty())
{ {
SEvent& rEvent = m_EventWriteQueue.front(); SEvent& rEvent = m_EventWriteQueue.front();
wiiuse_io_write(m_pWiiMote, (byte*)rEvent.m_PayLoad, MAX_PAYLOAD); wiiuse_io_write(m_pWiiMote, (byte*)rEvent.m_PayLoad, MAX_PAYLOAD);
m_EventWriteQueue.pop(); m_EventWriteQueue.pop();
} }
m_pCriticalSection->Leave(); m_pCriticalSection->Leave();
if (wiiuse_io_read(m_pWiiMote)) if (wiiuse_io_read(m_pWiiMote))
{ {
const byte* pBuffer = m_pWiiMote->event_buf; const byte* pBuffer = m_pWiiMote->event_buf;
// check if we have a channel (connection) if so save the data... // check if we have a channel (connection) if so save the
if (m_channelID > 0) // data...
{ if (m_channelID > 0)
m_pCriticalSection->Enter(); {
m_pCriticalSection->Enter();
// filter out reports // filter out reports
if (pBuffer[0] >= 0x30) if (pBuffer[0] >= 0x30)
{ {
memcpy(m_LastReport.m_PayLoad, pBuffer, MAX_PAYLOAD); memcpy(m_LastReport.m_PayLoad, pBuffer,
m_LastReportValid = true; MAX_PAYLOAD);
} m_LastReportValid = true;
else }
{ else
SEvent ImportantEvent; {
memcpy(ImportantEvent.m_PayLoad, pBuffer, MAX_PAYLOAD); SEvent ImportantEvent;
m_EventReadQueue.push(ImportantEvent); memcpy(ImportantEvent.m_PayLoad, pBuffer,
} MAX_PAYLOAD);
m_EventReadQueue.push(ImportantEvent);
}
m_pCriticalSection->Leave(); m_pCriticalSection->Leave();
} }
} }
}; };
// send queued data to the core // send queued data to the core
void Update() void Update()
{ {
m_pCriticalSection->Enter(); m_pCriticalSection->Enter();
if (m_EventReadQueue.empty()) if (m_EventReadQueue.empty())
{ {
if (m_LastReportValid) if (m_LastReportValid)
SendEvent(m_LastReport); SendEvent(m_LastReport);
} }
else else
{ {
SendEvent(m_EventReadQueue.front()); SendEvent(m_EventReadQueue.front());
m_EventReadQueue.pop(); m_EventReadQueue.pop();
} }
m_pCriticalSection->Leave(); m_pCriticalSection->Leave();
}; };
private: private:
struct SEvent struct SEvent
{ {
SEvent() SEvent()
{ {
memset(m_PayLoad, 0, MAX_PAYLOAD); memset(m_PayLoad, 0, MAX_PAYLOAD);
} }
byte m_PayLoad[MAX_PAYLOAD]; byte m_PayLoad[MAX_PAYLOAD];
}; };
typedef std::queue<SEvent> CEventQueue; typedef std::queue<SEvent> CEventQueue;
u8 m_WiimoteNumber; // just for debugging u8 m_WiimoteNumber; // just for debugging
u16 m_channelID; u16 m_channelID;
wiimote_t* m_pWiiMote; wiimote_t* m_pWiiMote;
Common::CriticalSection* m_pCriticalSection; Common::CriticalSection* m_pCriticalSection;
CEventQueue m_EventReadQueue; CEventQueue m_EventReadQueue;
CEventQueue m_EventWriteQueue; CEventQueue m_EventWriteQueue;
bool m_LastReportValid; bool m_LastReportValid;
SEvent m_LastReport; SEvent m_LastReport;
void SendEvent(SEvent& _rEvent) void SendEvent(SEvent& _rEvent)
{ {
// we don't have an answer channel // we don't have an answer channel
if (m_channelID == 0) if (m_channelID == 0)
return; return;
// check event buffer; // check event buffer;
u8 Buffer[1024]; u8 Buffer[1024];
u32 Offset = 0; u32 Offset = 0;
hid_packet* pHidHeader = (hid_packet*)(Buffer + Offset); hid_packet* pHidHeader = (hid_packet*)(Buffer + Offset);
Offset += sizeof(hid_packet); Offset += sizeof(hid_packet);
pHidHeader->type = HID_TYPE_DATA; pHidHeader->type = HID_TYPE_DATA;
pHidHeader->param = HID_PARAM_INPUT; pHidHeader->param = HID_PARAM_INPUT;
memcpy(&Buffer[Offset], _rEvent.m_PayLoad, MAX_PAYLOAD); memcpy(&Buffer[Offset], _rEvent.m_PayLoad, MAX_PAYLOAD);
Offset += MAX_PAYLOAD; Offset += MAX_PAYLOAD;
g_WiimoteInitialize.pWiimoteInput(m_channelID, Buffer, Offset); g_WiimoteInitialize.pWiimoteInput(m_channelID, Buffer, Offset);
} }
}; };
//****************************************************************************** //******************************************************************************
// Function Definitions // Function Definitions
//****************************************************************************** //******************************************************************************
int Initialize() int Initialize()
{ {
memset(g_WiiMotes, 0, sizeof(CWiiMote*) * MAX_WIIMOTES); memset(g_WiiMotes, 0, sizeof(CWiiMote*) * MAX_WIIMOTES);
g_WiiMotesFromWiiUse = wiiuse_init(MAX_WIIMOTES); g_WiiMotesFromWiiUse = wiiuse_init(MAX_WIIMOTES);
g_NumberOfWiiMotes = wiiuse_find(g_WiiMotesFromWiiUse, MAX_WIIMOTES, 5); g_NumberOfWiiMotes = wiiuse_find(g_WiiMotesFromWiiUse, MAX_WIIMOTES, 5);
for (int i=0; i<g_NumberOfWiiMotes; i++) for (int i=0; i<g_NumberOfWiiMotes; i++)
{ {
g_WiiMotes[i] = new CWiiMote(i+1, g_WiiMotesFromWiiUse[i]); g_WiiMotes[i] = new CWiiMote(i+1, g_WiiMotesFromWiiUse[i]);
} }
if (g_NumberOfWiiMotes == 0) if (g_NumberOfWiiMotes > 0)
return 0; g_pReadThread = new Common::Thread(ReadWiimote_ThreadFunc, NULL);
g_pReadThread = new Common::Thread(ReadWiimote_ThreadFunc, NULL); return g_NumberOfWiiMotes;
}
return true; void DoState(void* ptr, int mode)
} {}
void DoState(void* ptr, int mode) void Shutdown(void)
{} {
g_Shutdown = true;
void Shutdown(void) // stop the thread
{ if (g_pReadThread != NULL)
g_Shutdown = true; {
g_pReadThread->WaitForDeath();
delete g_pReadThread;
g_pReadThread = NULL;
}
// stop the thread // delete the wiimotes
if (g_pReadThread != NULL) for (int i=0; i<g_NumberOfWiiMotes; i++)
{ {
g_pReadThread->WaitForDeath(); delete g_WiiMotes[i];
delete g_pReadThread; g_WiiMotes[i] = NULL;
g_pReadThread = NULL; }
}
// delete the wiimotes // clean up wiiuse
for (int i=0; i<g_NumberOfWiiMotes; i++) wiiuse_cleanup(g_WiiMotesFromWiiUse, g_NumberOfWiiMotes);
{ }
delete g_WiiMotes[i];
g_WiiMotes[i] = NULL;
}
// clean up wiiuse void InterruptChannel(u16 _channelID, const void* _pData, u32 _Size)
wiiuse_cleanup(g_WiiMotesFromWiiUse, g_NumberOfWiiMotes); {
} g_WiiMotes[0]->SendData(_channelID, (const u8*)_pData, _Size);
}
void InterruptChannel(u16 _channelID, const void* _pData, u32 _Size) void ControlChannel(u16 _channelID, const void* _pData, u32 _Size)
{ {
g_WiiMotes[0]->SendData(_channelID, (const u8*)_pData, _Size); g_WiiMotes[0]->SendData(_channelID, (const u8*)_pData, _Size);
} }
void ControlChannel(u16 _channelID, const void* _pData, u32 _Size)
{
g_WiiMotes[0]->SendData(_channelID, (const u8*)_pData, _Size);
}
void Update() void Update()
{ {
for (int i=0; i<g_NumberOfWiiMotes; i++) for (int i=0; i<g_NumberOfWiiMotes; i++)
{ {
g_WiiMotes[i]->Update(); g_WiiMotes[i]->Update();
} }
} }
#ifdef _WIN32 #ifdef _WIN32
DWORD WINAPI ReadWiimote_ThreadFunc(void* arg) DWORD WINAPI ReadWiimote_ThreadFunc(void* arg)
#else #else
void *ReadWiimote_ThreadFunc(void* arg) void *ReadWiimote_ThreadFunc(void* arg)
#endif #endif
{ {
while (!g_Shutdown) while (!g_Shutdown)
{ {
for (int i=0; i<g_NumberOfWiiMotes; i++) for (int i=0; i<g_NumberOfWiiMotes; i++)
{ {
g_WiiMotes[i]->ReadData(); g_WiiMotes[i]->ReadData();
} }
} }
return 0; return 0;
} }
}; // end of namespace }; // end of namespace