mirror of https://github.com/AxioDL/boo.git
DeviceFinder: Move includes into cpp file where applicable
Avoids over-exposing inclusions that don't need to be propagated across headers.
This commit is contained in:
parent
2b63413369
commit
9853a97dd2
|
@ -1,13 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <unordered_set>
|
#include <memory>
|
||||||
#include <typeindex>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "DeviceToken.hpp"
|
#include <string>
|
||||||
#include "IHIDListener.hpp"
|
#include <unordered_set>
|
||||||
#include "DeviceSignature.hpp"
|
#include "boo/inputdev/DeviceSignature.hpp"
|
||||||
#include <cstring>
|
#include "boo/inputdev/DeviceToken.hpp"
|
||||||
#include <cstdio>
|
#include "boo/inputdev/IHIDListener.hpp"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifndef WIN32_LEAN_AND_MEAN
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
@ -44,28 +43,8 @@ private:
|
||||||
bool _hasToken(const std::string& path) const {
|
bool _hasToken(const std::string& path) const {
|
||||||
return m_tokens.find(path) != m_tokens.end();
|
return m_tokens.find(path) != m_tokens.end();
|
||||||
}
|
}
|
||||||
bool _insertToken(std::unique_ptr<DeviceToken>&& token) {
|
bool _insertToken(std::unique_ptr<DeviceToken>&& token);
|
||||||
if (DeviceSignature::DeviceMatchToken(*token, m_types)) {
|
void _removeToken(const std::string& path);
|
||||||
m_tokensLock.lock();
|
|
||||||
TInsertedDeviceToken inseredTok = m_tokens.insert(std::make_pair(token->getDevicePath(), std::move(token)));
|
|
||||||
m_tokensLock.unlock();
|
|
||||||
deviceConnected(*inseredTok.first->second);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
void _removeToken(const std::string& path) {
|
|
||||||
auto preCheck = m_tokens.find(path);
|
|
||||||
if (preCheck != m_tokens.end()) {
|
|
||||||
DeviceToken& tok = *preCheck->second;
|
|
||||||
std::shared_ptr<DeviceBase> dev = tok.m_connectedDev;
|
|
||||||
tok._deviceClose();
|
|
||||||
deviceDisconnected(tok, dev.get());
|
|
||||||
m_tokensLock.lock();
|
|
||||||
m_tokens.erase(preCheck);
|
|
||||||
m_tokensLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
class CDeviceTokensHandle {
|
class CDeviceTokensHandle {
|
||||||
|
@ -83,26 +62,8 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Application must specify its interested device-types */
|
/* Application must specify its interested device-types */
|
||||||
DeviceFinder(std::unordered_set<uint64_t> types) {
|
DeviceFinder(std::unordered_set<uint64_t> types);
|
||||||
if (skDevFinder) {
|
virtual ~DeviceFinder();
|
||||||
fmt::print(stderr, fmt("only one instance of CDeviceFinder may be constructed"));
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
skDevFinder = this;
|
|
||||||
for (const uint64_t& typeHash : types) {
|
|
||||||
const DeviceSignature* sigIter = BOO_DEVICE_SIGS;
|
|
||||||
while (sigIter->m_name) {
|
|
||||||
if (sigIter->m_typeHash == typeHash)
|
|
||||||
m_types.push_back(sigIter);
|
|
||||||
++sigIter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
virtual ~DeviceFinder() {
|
|
||||||
if (m_listener)
|
|
||||||
m_listener->stopScanning();
|
|
||||||
skDevFinder = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get interested device-type mask */
|
/* Get interested device-type mask */
|
||||||
const DeviceSignature::TDeviceSignatureSet& getTypes() const { return m_types; }
|
const DeviceSignature::TDeviceSignatureSet& getTypes() const { return m_types; }
|
||||||
|
@ -111,29 +72,11 @@ public:
|
||||||
CDeviceTokensHandle getTokens() { return CDeviceTokensHandle(*this); }
|
CDeviceTokensHandle getTokens() { return CDeviceTokensHandle(*this); }
|
||||||
|
|
||||||
/* Automatic device scanning */
|
/* Automatic device scanning */
|
||||||
bool startScanning() {
|
bool startScanning();
|
||||||
if (!m_listener)
|
bool stopScanning();
|
||||||
m_listener = IHIDListenerNew(*this);
|
|
||||||
if (m_listener)
|
|
||||||
return m_listener->startScanning();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
bool stopScanning() {
|
|
||||||
if (!m_listener)
|
|
||||||
m_listener = IHIDListenerNew(*this);
|
|
||||||
if (m_listener)
|
|
||||||
return m_listener->stopScanning();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Manual device scanning */
|
/* Manual device scanning */
|
||||||
bool scanNow() {
|
bool scanNow();
|
||||||
if (!m_listener)
|
|
||||||
m_listener = IHIDListenerNew(*this);
|
|
||||||
if (m_listener)
|
|
||||||
return m_listener->scanNow();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void deviceConnected(DeviceToken&) {}
|
virtual void deviceConnected(DeviceToken&) {}
|
||||||
virtual void deviceDisconnected(DeviceToken&, DeviceBase*) {}
|
virtual void deviceDisconnected(DeviceToken&, DeviceBase*) {}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
#include "boo/inputdev/DeviceFinder.hpp"
|
#include "boo/inputdev/DeviceFinder.hpp"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
#include <Dbt.h>
|
#include <Dbt.h>
|
||||||
#include <hidclass.h>
|
#include <hidclass.h>
|
||||||
|
@ -10,6 +13,76 @@ namespace boo {
|
||||||
|
|
||||||
DeviceFinder* DeviceFinder::skDevFinder = nullptr;
|
DeviceFinder* DeviceFinder::skDevFinder = nullptr;
|
||||||
|
|
||||||
|
DeviceFinder::DeviceFinder(std::unordered_set<uint64_t> types) {
|
||||||
|
if (skDevFinder) {
|
||||||
|
fmt::print(stderr, fmt("only one instance of CDeviceFinder may be constructed"));
|
||||||
|
std::abort();
|
||||||
|
}
|
||||||
|
skDevFinder = this;
|
||||||
|
for (const uint64_t& typeHash : types) {
|
||||||
|
const DeviceSignature* sigIter = BOO_DEVICE_SIGS;
|
||||||
|
while (sigIter->m_name) {
|
||||||
|
if (sigIter->m_typeHash == typeHash)
|
||||||
|
m_types.push_back(sigIter);
|
||||||
|
++sigIter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DeviceFinder::~DeviceFinder() {
|
||||||
|
if (m_listener)
|
||||||
|
m_listener->stopScanning();
|
||||||
|
skDevFinder = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeviceFinder::_insertToken(std::unique_ptr<DeviceToken>&& token) {
|
||||||
|
if (DeviceSignature::DeviceMatchToken(*token, m_types)) {
|
||||||
|
m_tokensLock.lock();
|
||||||
|
TInsertedDeviceToken insertedTok = m_tokens.insert(std::make_pair(token->getDevicePath(), std::move(token)));
|
||||||
|
m_tokensLock.unlock();
|
||||||
|
deviceConnected(*insertedTok.first->second);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceFinder::_removeToken(const std::string& path) {
|
||||||
|
auto preCheck = m_tokens.find(path);
|
||||||
|
if (preCheck != m_tokens.end()) {
|
||||||
|
DeviceToken& tok = *preCheck->second;
|
||||||
|
std::shared_ptr<DeviceBase> dev = tok.m_connectedDev;
|
||||||
|
tok._deviceClose();
|
||||||
|
deviceDisconnected(tok, dev.get());
|
||||||
|
m_tokensLock.lock();
|
||||||
|
m_tokens.erase(preCheck);
|
||||||
|
m_tokensLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeviceFinder::startScanning() {
|
||||||
|
if (!m_listener)
|
||||||
|
m_listener = IHIDListenerNew(*this);
|
||||||
|
if (m_listener)
|
||||||
|
return m_listener->startScanning();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeviceFinder::stopScanning() {
|
||||||
|
if (!m_listener)
|
||||||
|
m_listener = IHIDListenerNew(*this);
|
||||||
|
if (m_listener)
|
||||||
|
return m_listener->stopScanning();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeviceFinder::scanNow() {
|
||||||
|
if (!m_listener)
|
||||||
|
m_listener = IHIDListenerNew(*this);
|
||||||
|
if (m_listener)
|
||||||
|
return m_listener->scanNow();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
#if _WIN32 && !WINDOWS_STORE
|
#if _WIN32 && !WINDOWS_STORE
|
||||||
/* Windows-specific WM_DEVICECHANGED handler */
|
/* Windows-specific WM_DEVICECHANGED handler */
|
||||||
LRESULT DeviceFinder::winDevChangedHandler(WPARAM wParam, LPARAM lParam) {
|
LRESULT DeviceFinder::winDevChangedHandler(WPARAM wParam, LPARAM lParam) {
|
||||||
|
|
Loading…
Reference in New Issue