SenseApiMgr: Refcount device spec objects
This commit is contained in:
@@ -6,14 +6,17 @@
|
||||
#include <user/senseDeviceSpec.h>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace smo {
|
||||
namespace sense_api {
|
||||
|
||||
typedef int (sal_mlo_initializeIndFn)(void);
|
||||
typedef int (sal_mlo_finalizeIndFn)(void);
|
||||
typedef int (sal_mlo_attachDeviceReqFn)(const device::SenseDeviceSpec &desc);
|
||||
typedef int (sal_mlo_detachDeviceReqFn)(const device::SenseDeviceSpec &desc);
|
||||
typedef int (sal_mlo_attachDeviceReqFn)(
|
||||
const std::shared_ptr<device::SenseDeviceSpec>& desc);
|
||||
typedef int (sal_mlo_detachDeviceReqFn)(
|
||||
const std::shared_ptr<device::SenseDeviceSpec>& desc);
|
||||
|
||||
/**
|
||||
* @brief Hooks provided by Salmanoff to senseApi libraries.
|
||||
|
||||
@@ -54,7 +54,9 @@ std::string WindowSelector::stringify() const
|
||||
return os.str();
|
||||
}
|
||||
|
||||
AttachedWindow::AttachedWindow(const smo::device::SenseDeviceSpec& spec)
|
||||
AttachedWindow::AttachedWindow(
|
||||
const std::shared_ptr<smo::device::SenseDeviceSpec>& spec
|
||||
)
|
||||
: deviceSpec(spec)
|
||||
{
|
||||
// Validate required function pointers are available
|
||||
@@ -66,9 +68,9 @@ AttachedWindow::AttachedWindow(const smo::device::SenseDeviceSpec& spec)
|
||||
": Required xcbXorg function pointers not available");
|
||||
}
|
||||
|
||||
windowSelector.display = getRequiredParamAsInt(spec, "display");
|
||||
windowSelector.screen = getRequiredParamAsInt(spec, "screen");
|
||||
parseWindowSelector(spec);
|
||||
windowSelector.display = getRequiredParamAsInt(*spec, "display");
|
||||
windowSelector.screen = getRequiredParamAsInt(*spec, "screen");
|
||||
parseWindowSelector(*spec);
|
||||
|
||||
// Get connection from libxcbXorg
|
||||
std::shared_ptr<xcb_xorg::XcbConnection> conn =
|
||||
@@ -293,7 +295,9 @@ static int xcbWindow_finalizeInd(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int xcbWindow_attachDeviceReq(const smo::device::SenseDeviceSpec& desc)
|
||||
static int xcbWindow_attachDeviceReq(
|
||||
const std::shared_ptr<smo::device::SenseDeviceSpec>& desc
|
||||
)
|
||||
{
|
||||
g_attachedWindows.emplace_back(
|
||||
std::make_unique<xcb_window::AttachedWindow>(desc));
|
||||
@@ -304,7 +308,9 @@ static int xcbWindow_attachDeviceReq(const smo::device::SenseDeviceSpec& desc)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int xcbWindow_detachDeviceReq(const smo::device::SenseDeviceSpec& spec)
|
||||
static int xcbWindow_detachDeviceReq(
|
||||
const std::shared_ptr<smo::device::SenseDeviceSpec>& spec
|
||||
)
|
||||
{
|
||||
auto it = std::find_if(g_attachedWindows.begin(), g_attachedWindows.end(),
|
||||
[&spec](const std::unique_ptr<xcb_window::AttachedWindow>& window) {
|
||||
@@ -315,13 +321,13 @@ static int xcbWindow_detachDeviceReq(const smo::device::SenseDeviceSpec& spec)
|
||||
if (it == g_attachedWindows.end())
|
||||
{
|
||||
std::cerr << __func__ << ": Device not found for detachment:\n"
|
||||
<< spec.stringify() << "\n";
|
||||
<< spec->stringify() << "\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_attachedWindows.erase(it);
|
||||
std::cout << __func__ << ": Detached X11 window device:\n"
|
||||
<< spec.stringify() << "\n";
|
||||
<< spec->stringify() << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,10 +30,11 @@ struct WindowSelector
|
||||
class AttachedWindow
|
||||
{
|
||||
public:
|
||||
AttachedWindow(const smo::device::SenseDeviceSpec& spec);
|
||||
AttachedWindow(const std::shared_ptr<smo::device::SenseDeviceSpec>& spec);
|
||||
~AttachedWindow();
|
||||
|
||||
const smo::device::SenseDeviceSpec& getDeviceSpec() const { return deviceSpec; }
|
||||
const std::shared_ptr<smo::device::SenseDeviceSpec>& getDeviceSpec() const
|
||||
{ return deviceSpec; }
|
||||
const WindowSelector& getWindowSelector() const { return windowSelector; }
|
||||
const std::string& getActualWindowName() const { return actualWindowName; }
|
||||
void* getXcbConnection() const { return xcbConnectionShared.get(); }
|
||||
@@ -41,10 +42,11 @@ public:
|
||||
|
||||
private:
|
||||
void parseWindowSelector(const smo::device::SenseDeviceSpec& spec);
|
||||
int getRequiredParamAsInt(const smo::device::SenseDeviceSpec& spec,
|
||||
const std::string& paramName);
|
||||
int getRequiredParamAsInt(
|
||||
const smo::device::SenseDeviceSpec& spec,
|
||||
const std::string& paramName);
|
||||
|
||||
smo::device::SenseDeviceSpec deviceSpec;
|
||||
std::shared_ptr<smo::device::SenseDeviceSpec> deviceSpec;
|
||||
WindowSelector windowSelector;
|
||||
std::string actualWindowName;
|
||||
std::shared_ptr<xcb_xorg::XcbConnection> xcbConnectionShared;
|
||||
|
||||
@@ -38,8 +38,10 @@ public:
|
||||
void finalizeAllSenseApiLibs(void);
|
||||
|
||||
void attachAllSenseDevicesFromSpecs(void);
|
||||
void attachSenseDevice(const device::SenseDeviceSpec& spec);
|
||||
void detachSenseDevice(const device::SenseDeviceSpec& spec);
|
||||
void attachSenseDevice(
|
||||
const std::shared_ptr<device::SenseDeviceSpec>& spec);
|
||||
void detachSenseDevice(
|
||||
const std::shared_ptr<device::SenseDeviceSpec>& spec);
|
||||
void detachAllSenseDevices(void);
|
||||
|
||||
std::string stringifyLibs() const;
|
||||
|
||||
@@ -231,14 +231,16 @@ void SenseApiManager::finalizeAllSenseApiLibs(void)
|
||||
}
|
||||
}
|
||||
|
||||
void SenseApiManager::attachSenseDevice(const device::SenseDeviceSpec& spec)
|
||||
void SenseApiManager::attachSenseDevice(
|
||||
const std::shared_ptr<device::SenseDeviceSpec>& spec
|
||||
)
|
||||
{
|
||||
auto libOpt = getSenseApiLibByApiName(spec.api);
|
||||
auto libOpt = getSenseApiLibByApiName(spec->api);
|
||||
if (!libOpt)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
std::string(__func__) + ": No library found for API '"
|
||||
+ spec.api + "'");
|
||||
+ spec->api + "'");
|
||||
}
|
||||
auto& lib = libOpt.value().get();
|
||||
if (!lib.senseApiDesc.sal_mgmt_libOps.attachDeviceReq)
|
||||
@@ -250,14 +252,16 @@ void SenseApiManager::attachSenseDevice(const device::SenseDeviceSpec& spec)
|
||||
lib.senseApiDesc.sal_mgmt_libOps.attachDeviceReq(spec);
|
||||
}
|
||||
|
||||
void SenseApiManager::detachSenseDevice(const device::SenseDeviceSpec& spec)
|
||||
void SenseApiManager::detachSenseDevice(
|
||||
const std::shared_ptr<device::SenseDeviceSpec>& spec
|
||||
)
|
||||
{
|
||||
auto libOpt = getSenseApiLibByApiName(spec.api);
|
||||
auto libOpt = getSenseApiLibByApiName(spec->api);
|
||||
if (!libOpt)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
std::string(__func__) + ": No library found for API '"
|
||||
+ spec.api + "'");
|
||||
+ spec->api + "'");
|
||||
}
|
||||
auto& lib = libOpt.value().get();
|
||||
if (!lib.senseApiDesc.sal_mgmt_libOps.detachDeviceReq)
|
||||
@@ -272,14 +276,14 @@ void SenseApiManager::detachSenseDevice(const device::SenseDeviceSpec& spec)
|
||||
void SenseApiManager::attachAllSenseDevicesFromSpecs(void)
|
||||
{
|
||||
for (const auto& spec : device::DeviceManager::senseDeviceSpecs) {
|
||||
attachSenseDevice(*spec);
|
||||
attachSenseDevice(spec);
|
||||
}
|
||||
}
|
||||
|
||||
void SenseApiManager::detachAllSenseDevices(void)
|
||||
{
|
||||
for (const auto& spec : device::DeviceManager::senseDeviceSpecs) {
|
||||
detachSenseDevice(*spec);
|
||||
detachSenseDevice(spec);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user