OSX fix maybe. (based on jchadwick's patch)

This commit is contained in:
Jordan Woyak 2013-02-08 12:14:45 -06:00
parent 8ce58759e3
commit 1998da867b
2 changed files with 38 additions and 50 deletions

View File

@ -10,19 +10,6 @@
} }
@end @end
WiimoteScanner::WiimoteScanner()
{}
std::vector<Wiimote*> WiimoteScanner::FindWiimotes(size_t max_wiimotes)
{
return std::vector<Wiimote*>();
}
bool WiimoteScanner::IsReady() const
{
return false;
}
@implementation SearchBT @implementation SearchBT
- (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *) sender - (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *) sender
error: (IOReturn) error error: (IOReturn) error
@ -112,34 +99,35 @@ bool WiimoteScanner::IsReady() const
namespace WiimoteReal namespace WiimoteReal
{ {
// Find wiimotes. WiimoteScanner::WiimoteScanner()
// wm is an array of max_wiimotes wiimotes {}
// Returns the total number of found wiimotes.
int FindWiimotes(Wiimote **wm, int max_wiimotes) WiimoteScanner::~WiimoteScanner()
{}
std::vector<Wiimote*> WiimoteScanner::FindWiimotes(size_t max_wiimotes)
{ {
// TODO: find the device in the constructor and save it for later
std::vector<Wiimote*> wiimotes;
IOBluetoothHostController *bth; IOBluetoothHostController *bth;
IOBluetoothDeviceInquiry *bti; IOBluetoothDeviceInquiry *bti;
SearchBT *sbt; SearchBT *sbt;
NSEnumerator *en; NSEnumerator *en;
int found_devices = 0, found_wiimotes = 0;
// Count the number of already found wiimotes
for (int i = 0; i < MAX_WIIMOTES; ++i)
if (wm[i])
found_wiimotes++;
bth = [[IOBluetoothHostController alloc] init]; bth = [[IOBluetoothHostController alloc] init];
if ([bth addressAsString] == nil) { if ([bth addressAsString] == nil)
{
WARN_LOG(WIIMOTE, "No bluetooth host controller"); WARN_LOG(WIIMOTE, "No bluetooth host controller");
[bth release]; [bth release];
return found_wiimotes; return wiimotes;
} }
sbt = [[SearchBT alloc] init]; sbt = [[SearchBT alloc] init];
sbt->maxDevices = max_wiimotes - found_wiimotes; sbt->maxDevices = max_wiimotes;
bti = [[IOBluetoothDeviceInquiry alloc] init]; bti = [[IOBluetoothDeviceInquiry alloc] init];
[bti setDelegate: sbt]; [bti setDelegate: sbt];
[bti setInquiryLength: 5]; [bti setInquiryLength: 2];
if ([bti start] == kIOReturnSuccess) if ([bti start] == kIOReturnSuccess)
[bti retain]; [bti retain];
@ -149,10 +137,9 @@ int FindWiimotes(Wiimote **wm, int max_wiimotes)
CFRunLoopRun(); CFRunLoopRun();
[bti stop]; [bti stop];
found_devices = [[bti foundDevices] count]; int found_devices = [[bti foundDevices] count];
NOTICE_LOG(WIIMOTE, "Found %i bluetooth device%c", found_devices, NOTICE_LOG(WIIMOTE, "Found %i bluetooth devices", found_devices);
found_devices == 1 ? '\0' : 's');
en = [[bti foundDevices] objectEnumerator]; en = [[bti foundDevices] objectEnumerator];
for (int i = 0; i < found_devices; i++) for (int i = 0; i < found_devices; i++)
@ -160,24 +147,26 @@ int FindWiimotes(Wiimote **wm, int max_wiimotes)
IOBluetoothDevice *dev = [en nextObject]; IOBluetoothDevice *dev = [en nextObject];
if (!IsValidBluetoothName([[dev name] UTF8String])) if (!IsValidBluetoothName([[dev name] UTF8String]))
continue; continue;
// Find an unused slot
for (int k = 0; k < MAX_WIIMOTES; k++) {
if (wm[k] != NULL ||
!(g_wiimote_sources[k] & WIIMOTE_SRC_REAL))
continue;
wm[k] = new Wiimote(k); Wiimote *wm = new Wiimote();
wm[k]->btd = dev; wm->btd = dev;
found_wiimotes++; wiimotes.push_back(wm);
if(wiimotes.size() >= max_wiimotes)
break; break;
} }
}
[bth release]; [bth release];
[bti release]; [bti release];
[sbt release]; [sbt release];
return found_wiimotes; return wiimotes;
}
bool WiimoteScanner::IsReady() const
{
// TODO: only return true when a BT device is present
return true;
} }
// Connect to a wiimote with a known address. // Connect to a wiimote with a known address.
@ -192,7 +181,8 @@ bool Wiimote::Connect()
withPSM: kBluetoothL2CAPPSMHIDControl delegate: cbt]; withPSM: kBluetoothL2CAPPSMHIDControl delegate: cbt];
[btd openL2CAPChannelSync: &ichan [btd openL2CAPChannelSync: &ichan
withPSM: kBluetoothL2CAPPSMHIDInterrupt delegate: cbt]; withPSM: kBluetoothL2CAPPSMHIDInterrupt delegate: cbt];
if (ichan == NULL || cchan == NULL) { if (ichan == NULL || cchan == NULL)
{
ERROR_LOG(WIIMOTE, "Unable to open L2CAP channels " ERROR_LOG(WIIMOTE, "Unable to open L2CAP channels "
"for wiimote %i", index + 1); "for wiimote %i", index + 1);
Disconnect(); Disconnect();
@ -209,11 +199,6 @@ bool Wiimote::Connect()
m_connected = true; m_connected = true;
Handshake();
SetLEDs(WIIMOTE_LED_1 << index);
m_wiimote_thread = std::thread(std::mem_fun(&Wiimote::ThreadFunc), this);
[cbt release]; [cbt release];
return true; return true;
@ -240,9 +225,9 @@ void Wiimote::Disconnect()
ichan = NULL; ichan = NULL;
} }
bool Wiimote::IsOpen() const bool Wiimote::IsConnected() const
{ {
return IsConnected(); return m_connected;
} }
int Wiimote::IORead(unsigned char *buf) int Wiimote::IORead(unsigned char *buf)
@ -252,6 +237,8 @@ int Wiimote::IORead(unsigned char *buf)
if (!IsConnected()) if (!IsConnected())
return 0; return 0;
// TODO: race conditions here, yo
bytes = inputlen; bytes = inputlen;
memcpy(buf, input, bytes); memcpy(buf, input, bytes);
inputlen = 0; inputlen = 0;
@ -259,14 +246,14 @@ int Wiimote::IORead(unsigned char *buf)
return bytes; return bytes;
} }
int Wiimote::IOWrite(unsigned char *buf, int len) int Wiimote::IOWrite(const unsigned char *buf, int len)
{ {
IOReturn ret; IOReturn ret;
if (!IsConnected()) if (!IsConnected())
return 0; return 0;
ret = [ichan writeAsync: buf length: len refcon: nil]; ret = [ichan writeAsync: const_cast<void*>((void *)buf) length: len refcon: nil];
if (ret == kIOReturnSuccess) if (ret == kIOReturnSuccess)
return len; return len;

View File

@ -84,6 +84,7 @@ public:
IOBluetoothL2CAPChannel *cchan; IOBluetoothL2CAPChannel *cchan;
char input[MAX_PAYLOAD]; char input[MAX_PAYLOAD];
int inputlen; int inputlen;
bool m_connected;
#elif defined(__linux__) && HAVE_BLUEZ #elif defined(__linux__) && HAVE_BLUEZ
bdaddr_t bdaddr; // Bluetooth address bdaddr_t bdaddr; // Bluetooth address
int cmd_sock; // Command socket int cmd_sock; // Command socket