Skip to content

Commit

Permalink
WiimoteScannerDarwin: Initialize IOBluetoothHostController in constru…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
OatmealDome committed Jun 24, 2021
1 parent eb7b70b commit 0de30fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
10 changes: 9 additions & 1 deletion Source/Core/Core/HW/WiimoteReal/IOdarwin.h
Expand Up @@ -7,17 +7,25 @@
#ifdef __APPLE__
#include "Core/HW/WiimoteReal/WiimoteReal.h"

#ifdef __OBJC__
#import <IOBluetooth/IOBluetooth.h>
#else
// IOBluetooth's types won't be defined in pure C++ mode.
typedef void IOBluetoothHostController;
#endif

namespace WiimoteReal
{
class WiimoteScannerDarwin final : public WiimoteScannerBackend
{
public:
WiimoteScannerDarwin() = default;
WiimoteScannerDarwin();
~WiimoteScannerDarwin() override;
bool IsReady() const override;
void FindWiimotes(std::vector<Wiimote*>&, Wiimote*&) override;
void Update() override {} // not needed
private:
IOBluetoothHostController* m_host_controller;
bool m_stop_scanning = false;
};
} // namespace WiimoteReal
Expand Down
40 changes: 25 additions & 15 deletions Source/Core/Core/HW/WiimoteReal/IOdarwin.mm
Expand Up @@ -21,27 +21,40 @@ @interface ConnectBT : NSObject

namespace WiimoteReal
{
WiimoteScannerDarwin::WiimoteScannerDarwin()
{
m_host_controller = [IOBluetoothHostController defaultController];
if (![m_host_controller addressAsString])
{
WARN_LOG_FMT(WIIMOTE, "No Bluetooth host controller");

[m_host_controller release];
m_host_controller = nil;

return;
}

[m_host_controller retain];
}

WiimoteScannerDarwin::~WiimoteScannerDarwin()
{
[m_host_controller release];
m_host_controller = nil;

m_stop_scanning = true;
}

void WiimoteScannerDarwin::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
Wiimote*& found_board)
{
// TODO: find the device in the constructor and save it for later
IOBluetoothHostController* bth;
IOBluetoothDeviceInquiry* bti;
found_board = nullptr;

bth = [[IOBluetoothHostController alloc] init];
bool btFailed = [bth addressAsString] == nil;
if (btFailed)
if (!m_host_controller)
{
WARN_LOG_FMT(WIIMOTE, "No Bluetooth host controller");
[bth release];
return;
}

IOBluetoothDeviceInquiry* bti;
found_board = nullptr;

SearchBT* sbt = [[SearchBT alloc] init];
sbt->maxDevices = 32;
Expand All @@ -52,7 +65,6 @@ @interface ConnectBT : NSObject
if ([bti start] != kIOReturnSuccess)
{
ERROR_LOG_FMT(WIIMOTE, "Unable to do Bluetooth discovery");
[bth release];
[sbt release];

return;
Expand Down Expand Up @@ -86,16 +98,14 @@ @interface ConnectBT : NSObject
found_wiimotes.push_back(wm);
}
}

[bth release];

[bti release];
[sbt release];
}

bool WiimoteScannerDarwin::IsReady() const
{
// TODO: only return true when a BT device is present
return true;
return m_host_controller != nil;
}

WiimoteDarwin::WiimoteDarwin(IOBluetoothDevice* device) : m_btd(device)
Expand Down

0 comments on commit 0de30fa

Please sign in to comment.