Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
This changes Linux to control Wiimotes on the interrupt channel. Whic…
…h in turn allows -TR wiimotes to work in Linux.
  • Loading branch information
Sonicadvance1 committed Jan 14, 2013
1 parent 30d4259 commit 5005d52
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
55 changes: 28 additions & 27 deletions Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp
Expand Up @@ -34,11 +34,6 @@
#include "WiimoteReal.h"
#include "Host.h"

// Identify the wiimote device by its class
#define WM_DEV_CLASS_0 0x04
#define WM_DEV_CLASS_1 0x25
#define WM_DEV_CLASS_2 0x00

namespace WiimoteReal
{

Expand Down Expand Up @@ -95,9 +90,15 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes)
// Display discovered devices
for (i = 0; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i)
{
if ((scan_info[i].dev_class[0] == WM_DEV_CLASS_0) &&
(scan_info[i].dev_class[1] == WM_DEV_CLASS_1) &&
(scan_info[i].dev_class[2] == WM_DEV_CLASS_2))
char name[1000];
memset(name, 0, sizeof(name));
ERROR_LOG(WIIMOTE, "found a device...");
if (hci_read_remote_name(device_sock, &scan_info[i].bdaddr, sizeof(name), name, 0) < 0) {
ERROR_LOG(WIIMOTE, "name request failed");
continue;
}
ERROR_LOG(WIIMOTE, "device name %s", name);
if (IsValidBluetoothName(name))
{
bool new_wiimote = true;
// Determine if this wiimote has already been found.
Expand Down Expand Up @@ -146,24 +147,24 @@ bool Wiimote::Connect()

// Output channel
addr.l2_psm = htobs(WM_OUTPUT_CHANNEL);
if ((out_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(out_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
if ((cmd_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(cmd_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
DEBUG_LOG(WIIMOTE, "Unable to open output socket to wiimote.");
close(out_sock);
out_sock = -1;
close(cmd_sock);
cmd_sock = -1;
return false;
}

// Input channel
addr.l2_psm = htobs(WM_INPUT_CHANNEL);
if ((in_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(in_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
if ((int_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(int_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
DEBUG_LOG(WIIMOTE, "Unable to open input socket from wiimote.");
close(in_sock);
close(out_sock);
in_sock = out_sock = -1;
close(int_sock);
close(cmd_sock);
int_sock = cmd_sock = -1;
return false;
}

Expand Down Expand Up @@ -204,17 +205,17 @@ void Wiimote::Close()
{
Host_ConnectWiimote(index, false);

close(out_sock);
close(in_sock);
close(cmd_sock);
close(int_sock);

out_sock = -1;
in_sock = -1;
cmd_sock = -1;
int_sock = -1;
}
}

bool Wiimote::IsOpen() const
{
return out_sock != -1 && in_sock != -1;
return cmd_sock != -1 && int_sock != -1;
}

int Wiimote::IORead(unsigned char *buf)
Expand All @@ -226,19 +227,19 @@ int Wiimote::IORead(unsigned char *buf)

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

if (select(in_sock + 1, &fds, NULL, NULL, &tv) == -1)
if (select(int_sock + 1, &fds, NULL, NULL, &tv) == -1)
{
ERROR_LOG(WIIMOTE, "Unable to select wiimote %i input socket.", index + 1);
return 0;
}

if (!FD_ISSET(in_sock, &fds))
if (!FD_ISSET(int_sock, &fds))
return 0;

// Read the pending message into the buffer
int r = read(in_sock, buf, MAX_PAYLOAD);
int r = read(int_sock, buf, MAX_PAYLOAD);
if (r == -1)
{
// Error reading data
Expand All @@ -265,7 +266,7 @@ int Wiimote::IORead(unsigned char *buf)

int Wiimote::IOWrite(unsigned char* buf, int len)
{
return write(out_sock, buf, len);
return write(int_sock, buf, len);
}

}; // WiimoteReal
2 changes: 1 addition & 1 deletion Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp
Expand Up @@ -44,7 +44,7 @@ Wiimote::Wiimote(const unsigned int _index)
#ifdef __APPLE__
, inputlen(0)
#elif defined(__linux__) && HAVE_BLUEZ
, out_sock(-1), in_sock(-1)
, cmd_sock(-1), int_sock(-1)
#elif defined(_WIN32)
, dev_handle(0), stack(MSBT_STACK_UNKNOWN)
#endif
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h
Expand Up @@ -71,8 +71,8 @@ friend class WiimoteEmu::Wiimote;
int inputlen;
#elif defined(__linux__) && HAVE_BLUEZ
bdaddr_t bdaddr; // Bluetooth address
int out_sock; // Output socket
int in_sock; // Input socket
int cmd_sock; // Command socket
int int_sock; // Interrupt socket

void Close();

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Src/HW/WiimoteReal/WiimoteRealBase.h
Expand Up @@ -34,8 +34,8 @@

// The 4 most significant bits of the first byte of an outgoing command must be
// 0x50 if sending on the command channel and 0xA0 if sending on the interrupt
// channel. On Mac we use interrupt channel; on Windows/Linux, command.
#ifndef __APPLE__
// channel. On Mac and Linux we use interrupt channel; on Windows, command.
#ifdef _WIN32
#define WM_SET_REPORT 0x50
#else
#define WM_SET_REPORT 0xA0
Expand Down

0 comments on commit 5005d52

Please sign in to comment.