Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'linux-wiimote-crash-fix'
  • Loading branch information
jordan-woyak committed Jan 10, 2013
2 parents cf94245 + a693b84 commit 240ea0f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
5 changes: 5 additions & 0 deletions Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp
Expand Up @@ -36,6 +36,11 @@ void Wiimote::RealDisconnect()
return;
}

bool Wiimote::IsOpen() const
{
return IsConnected();
}

int Wiimote::IORead(unsigned char* buf)
{
return 0;
Expand Down
48 changes: 28 additions & 20 deletions Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp
Expand Up @@ -136,10 +136,10 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes)
// Connect to a wiimote with a known address.
bool Wiimote::Connect()
{
struct sockaddr_l2 addr;

if (IsConnected()) return false;
if (IsConnected())
return false;

sockaddr_l2 addr;
addr.l2_family = AF_BLUETOOTH;
addr.l2_bdaddr = bdaddr;
addr.l2_cid = 0;
Expand Down Expand Up @@ -195,28 +195,36 @@ void Wiimote::RealDisconnect()
if (m_wiimote_thread.joinable())
m_wiimote_thread.join();

Host_ConnectWiimote(index, false);
Close();
}

close(out_sock);
close(in_sock);
void Wiimote::Close()
{
if (IsOpen())
{
Host_ConnectWiimote(index, false);

out_sock = -1;
in_sock = -1;
close(out_sock);
close(in_sock);

out_sock = -1;
in_sock = -1;
}
}

int Wiimote::IORead(unsigned char *buf)
bool Wiimote::IsOpen() const
{
struct timeval tv;
fd_set fds;
int r;

if (!IsConnected())
return 0;
return out_sock != -1 && in_sock != -1;
}

int Wiimote::IORead(unsigned char *buf)
{
// Block select for 1/2000th of a second
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = WIIMOTE_DEFAULT_TIMEOUT * 1000;

fd_set fds;
FD_ZERO(&fds);
FD_SET(in_sock, &fds);

Expand All @@ -230,7 +238,7 @@ int Wiimote::IORead(unsigned char *buf)
return 0;

// Read the pending message into the buffer
r = read(in_sock, buf, MAX_PAYLOAD);
int r = read(in_sock, buf, MAX_PAYLOAD);
if (r == -1)
{
// Error reading data
Expand All @@ -241,17 +249,17 @@ int Wiimote::IORead(unsigned char *buf)
// This can happen if the bluetooth dongle is disconnected
ERROR_LOG(WIIMOTE, "Bluetooth appears to be disconnected. "
"Wiimote %i will be disconnected.", index + 1);
RealDisconnect();
Close();
}

return 0;
}
if (!r)
else if (!r)
{
// Disconnect
RealDisconnect();
return 0;
Close();
}

return r;
}

Expand Down
5 changes: 5 additions & 0 deletions Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp
Expand Up @@ -294,6 +294,11 @@ void Wiimote::RealDisconnect()
ResetEvent(&hid_overlap);
}

bool Wiimote::IsOpen() const
{
return IsConnected();
}

int Wiimote::IORead(unsigned char* buf)
{
DWORD b, r;
Expand Down
5 changes: 5 additions & 0 deletions Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm
Expand Up @@ -227,6 +227,11 @@ int FindWiimotes(Wiimote **wm, int max_wiimotes)
ichan = NULL;
}

bool Wiimote::IsOpen() const
{
return IsConnected();
}

int Wiimote::IORead(unsigned char *buf)
{
int bytes;
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp
Expand Up @@ -240,7 +240,7 @@ void Wiimote::Disconnect()
DisableDataReporting();
}

bool Wiimote::IsConnected()
bool Wiimote::IsConnected() const
{
return m_connected;
}
Expand Down Expand Up @@ -312,7 +312,7 @@ void Wiimote::ThreadFunc()
Rumble();

// main loop
while (IsConnected())
while (IsOpen())
{
#ifdef __APPLE__
while (Write()) {}
Expand Down
6 changes: 5 additions & 1 deletion Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h
Expand Up @@ -53,7 +53,8 @@ friend class WiimoteEmu::Wiimote;
bool Read();
bool Write();
bool Connect();
bool IsConnected();
bool IsConnected() const;
bool IsOpen() const;
void Disconnect();
void DisableDataReporting();
void Rumble();
Expand All @@ -72,6 +73,9 @@ friend class WiimoteEmu::Wiimote;
bdaddr_t bdaddr; // Bluetooth address
int out_sock; // Output socket
int in_sock; // Input socket

void Close();

#elif defined(_WIN32)
char devicepath[255]; // Unique wiimote reference
//ULONGLONG btaddr; // Bluetooth address
Expand Down

0 comments on commit 240ea0f

Please sign in to comment.