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__ #ifdef __APPLE__
#include "Core/HW/WiimoteReal/WiimoteReal.h" #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 namespace WiimoteReal
{ {
class WiimoteScannerDarwin final : public WiimoteScannerBackend class WiimoteScannerDarwin final : public WiimoteScannerBackend
{ {
public: public:
WiimoteScannerDarwin() = default; WiimoteScannerDarwin();
~WiimoteScannerDarwin() override; ~WiimoteScannerDarwin() override;
bool IsReady() const override; bool IsReady() const override;
void FindWiimotes(std::vector<Wiimote*>&, Wiimote*&) override; void FindWiimotes(std::vector<Wiimote*>&, Wiimote*&) override;
void Update() override {} // not needed void Update() override {} // not needed
private: private:
IOBluetoothHostController* m_host_controller;
bool m_stop_scanning = false; bool m_stop_scanning = false;
}; };
} // namespace WiimoteReal } // namespace WiimoteReal

View File

@ -21,28 +21,41 @@
namespace WiimoteReal 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() WiimoteScannerDarwin::~WiimoteScannerDarwin()
{ {
[m_host_controller release];
m_host_controller = nil;
m_stop_scanning = true; m_stop_scanning = true;
} }
void WiimoteScannerDarwin::FindWiimotes(std::vector<Wiimote*>& found_wiimotes, void WiimoteScannerDarwin::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
Wiimote*& found_board) Wiimote*& found_board)
{ {
// TODO: find the device in the constructor and save it for later if (!m_host_controller)
IOBluetoothHostController* bth;
IOBluetoothDeviceInquiry* bti;
found_board = nullptr;
bth = [[IOBluetoothHostController alloc] init];
bool btFailed = [bth addressAsString] == nil;
if (btFailed)
{ {
WARN_LOG_FMT(WIIMOTE, "No Bluetooth host controller");
[bth release];
return; return;
} }
IOBluetoothDeviceInquiry* bti;
found_board = nullptr;
SearchBT* sbt = [[SearchBT alloc] init]; SearchBT* sbt = [[SearchBT alloc] init];
sbt->maxDevices = 32; sbt->maxDevices = 32;
bti = [[IOBluetoothDeviceInquiry alloc] init]; bti = [[IOBluetoothDeviceInquiry alloc] init];
@ -52,7 +65,6 @@ void WiimoteScannerDarwin::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
if ([bti start] != kIOReturnSuccess) if ([bti start] != kIOReturnSuccess)
{ {
ERROR_LOG_FMT(WIIMOTE, "Unable to do Bluetooth discovery"); ERROR_LOG_FMT(WIIMOTE, "Unable to do Bluetooth discovery");
[bth release];
[sbt release]; [sbt release];
return; return;
@ -87,15 +99,13 @@ void WiimoteScannerDarwin::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
} }
} }
[bth release];
[bti release]; [bti release];
[sbt release]; [sbt release];
} }
bool WiimoteScannerDarwin::IsReady() const bool WiimoteScannerDarwin::IsReady() const
{ {
// TODO: only return true when a BT device is present return m_host_controller != nil;
return true;
} }
WiimoteDarwin::WiimoteDarwin(IOBluetoothDevice* device) : m_btd(device) WiimoteDarwin::WiimoteDarwin(IOBluetoothDevice* device) : m_btd(device)