WiimoteReal: Remove unsafe static_cast in IOLinux
Since we now support different scanner sources, g_wiimotes is not guaranteed to only contain WiimoteLinux anymore. This replaces the previous "already connected" check with one that doesn't use g_wiimotes.
This commit is contained in:
parent
9c02e327b7
commit
d2976086b6
|
@ -14,6 +14,14 @@
|
||||||
|
|
||||||
namespace WiimoteReal
|
namespace WiimoteReal
|
||||||
{
|
{
|
||||||
|
// This is used to store the Bluetooth address of connected Wiimotes,
|
||||||
|
// so we can ignore Wiimotes that are already connected.
|
||||||
|
static std::vector<std::string> s_known_addrs;
|
||||||
|
static bool IsNewWiimote(const std::string& addr)
|
||||||
|
{
|
||||||
|
return std::find(s_known_addrs.begin(), s_known_addrs.end(), addr) == s_known_addrs.end();
|
||||||
|
}
|
||||||
|
|
||||||
WiimoteScannerLinux::WiimoteScannerLinux() : m_device_id(-1), m_device_sock(-1)
|
WiimoteScannerLinux::WiimoteScannerLinux() : m_device_id(-1), m_device_sock(-1)
|
||||||
{
|
{
|
||||||
// Get the id of the first Bluetooth device.
|
// Get the id of the first Bluetooth device.
|
||||||
|
@ -82,39 +90,27 @@ void WiimoteScannerLinux::FindWiimotes(std::vector<Wiimote*>& found_wiimotes, Wi
|
||||||
}
|
}
|
||||||
|
|
||||||
ERROR_LOG(WIIMOTE, "device name %s", name);
|
ERROR_LOG(WIIMOTE, "device name %s", name);
|
||||||
if (IsValidBluetoothName(name))
|
if (!IsValidBluetoothName(name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
char bdaddr_str[18] = {};
|
||||||
|
ba2str(&scan_infos[i].bdaddr, bdaddr_str);
|
||||||
|
|
||||||
|
if (!IsNewWiimote(bdaddr_str))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Found a new device
|
||||||
|
s_known_addrs.push_back(bdaddr_str);
|
||||||
|
Wiimote* wm = new WiimoteLinux(scan_infos[i].bdaddr);
|
||||||
|
if (IsBalanceBoardName(name))
|
||||||
{
|
{
|
||||||
bool new_wiimote = true;
|
found_board = wm;
|
||||||
|
NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str);
|
||||||
// Determine if this Wiimote has already been found.
|
}
|
||||||
for (int j = 0; j < MAX_BBMOTES && new_wiimote; ++j)
|
else
|
||||||
{
|
{
|
||||||
// compare this address with the stored addresses in our global array
|
found_wiimotes.push_back(wm);
|
||||||
// static_cast is OK here, since we're only ever going to have this subclass in g_wiimotes
|
NOTICE_LOG(WIIMOTE, "Found Wiimote (%s).", bdaddr_str);
|
||||||
// on Linux (and likewise, only WiimoteWindows on Windows, etc)
|
|
||||||
auto connected_wiimote = static_cast<WiimoteLinux*>(g_wiimotes[j]);
|
|
||||||
if (connected_wiimote && bacmp(&scan_infos[i].bdaddr, &connected_wiimote->Address()) == 0)
|
|
||||||
new_wiimote = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_wiimote)
|
|
||||||
{
|
|
||||||
// Found a new device
|
|
||||||
char bdaddr_str[18] = {};
|
|
||||||
ba2str(&scan_infos[i].bdaddr, bdaddr_str);
|
|
||||||
|
|
||||||
Wiimote* wm = new WiimoteLinux(scan_infos[i].bdaddr);
|
|
||||||
if (IsBalanceBoardName(name))
|
|
||||||
{
|
|
||||||
found_board = wm;
|
|
||||||
NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
found_wiimotes.push_back(wm);
|
|
||||||
NOTICE_LOG(WIIMOTE, "Found Wiimote (%s).", bdaddr_str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,11 +139,6 @@ WiimoteLinux::~WiimoteLinux()
|
||||||
close(m_wakeup_pipe_r);
|
close(m_wakeup_pipe_r);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bdaddr_t& WiimoteLinux::Address() const
|
|
||||||
{
|
|
||||||
return m_bdaddr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to a Wiimote with a known address.
|
// Connect to a Wiimote with a known address.
|
||||||
bool WiimoteLinux::ConnectInternal()
|
bool WiimoteLinux::ConnectInternal()
|
||||||
{
|
{
|
||||||
|
@ -189,6 +180,10 @@ void WiimoteLinux::DisconnectInternal()
|
||||||
|
|
||||||
m_cmd_sock = -1;
|
m_cmd_sock = -1;
|
||||||
m_int_sock = -1;
|
m_int_sock = -1;
|
||||||
|
char bdaddr_str[18] = {};
|
||||||
|
ba2str(&m_bdaddr, bdaddr_str);
|
||||||
|
s_known_addrs.erase(std::remove(s_known_addrs.begin(), s_known_addrs.end(), bdaddr_str),
|
||||||
|
s_known_addrs.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WiimoteLinux::IsConnected() const
|
bool WiimoteLinux::IsConnected() const
|
||||||
|
|
|
@ -16,7 +16,6 @@ class WiimoteLinux final : public Wiimote
|
||||||
public:
|
public:
|
||||||
WiimoteLinux(bdaddr_t bdaddr);
|
WiimoteLinux(bdaddr_t bdaddr);
|
||||||
~WiimoteLinux() override;
|
~WiimoteLinux() override;
|
||||||
const bdaddr_t& Address() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool ConnectInternal() override;
|
bool ConnectInternal() override;
|
||||||
|
|
Loading…
Reference in New Issue