WiimoteScannerDarwin: Initialize IOBluetoothHostController in constructor

This commit is contained in:
OatmealDome 2021-06-23 22:55:15 -04:00
parent eb7b70b48d
commit 0de30fa0bb
2 changed files with 34 additions and 16 deletions

View File

@ -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

View File

@ -21,27 +21,40 @@
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;
@ -52,7 +65,6 @@ void WiimoteScannerDarwin::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
if ([bti start] != kIOReturnSuccess)
{
ERROR_LOG_FMT(WIIMOTE, "Unable to do Bluetooth discovery");
[bth release];
[sbt release];
return;
@ -86,16 +98,14 @@ void WiimoteScannerDarwin::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
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)