SenseApiMgr: Refcount device spec objects

This commit is contained in:
2025-08-29 09:50:26 -04:00
parent cd63593ae5
commit f3f2384f9b
5 changed files with 42 additions and 25 deletions
+5 -2
View File
@@ -6,14 +6,17 @@
#include <user/senseDeviceSpec.h> #include <user/senseDeviceSpec.h>
#include <optional> #include <optional>
#include <string> #include <string>
#include <memory>
namespace smo { namespace smo {
namespace sense_api { namespace sense_api {
typedef int (sal_mlo_initializeIndFn)(void); typedef int (sal_mlo_initializeIndFn)(void);
typedef int (sal_mlo_finalizeIndFn)(void); typedef int (sal_mlo_finalizeIndFn)(void);
typedef int (sal_mlo_attachDeviceReqFn)(const device::SenseDeviceSpec &desc); typedef int (sal_mlo_attachDeviceReqFn)(
typedef int (sal_mlo_detachDeviceReqFn)(const device::SenseDeviceSpec &desc); 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. * @brief Hooks provided by Salmanoff to senseApi libraries.
+14 -8
View File
@@ -54,7 +54,9 @@ std::string WindowSelector::stringify() const
return os.str(); return os.str();
} }
AttachedWindow::AttachedWindow(const smo::device::SenseDeviceSpec& spec) AttachedWindow::AttachedWindow(
const std::shared_ptr<smo::device::SenseDeviceSpec>& spec
)
: deviceSpec(spec) : deviceSpec(spec)
{ {
// Validate required function pointers are available // Validate required function pointers are available
@@ -66,9 +68,9 @@ AttachedWindow::AttachedWindow(const smo::device::SenseDeviceSpec& spec)
": Required xcbXorg function pointers not available"); ": Required xcbXorg function pointers not available");
} }
windowSelector.display = getRequiredParamAsInt(spec, "display"); windowSelector.display = getRequiredParamAsInt(*spec, "display");
windowSelector.screen = getRequiredParamAsInt(spec, "screen"); windowSelector.screen = getRequiredParamAsInt(*spec, "screen");
parseWindowSelector(spec); parseWindowSelector(*spec);
// Get connection from libxcbXorg // Get connection from libxcbXorg
std::shared_ptr<xcb_xorg::XcbConnection> conn = std::shared_ptr<xcb_xorg::XcbConnection> conn =
@@ -293,7 +295,9 @@ static int xcbWindow_finalizeInd(void)
return 0; 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( g_attachedWindows.emplace_back(
std::make_unique<xcb_window::AttachedWindow>(desc)); std::make_unique<xcb_window::AttachedWindow>(desc));
@@ -304,7 +308,9 @@ static int xcbWindow_attachDeviceReq(const smo::device::SenseDeviceSpec& desc)
return 0; 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(), auto it = std::find_if(g_attachedWindows.begin(), g_attachedWindows.end(),
[&spec](const std::unique_ptr<xcb_window::AttachedWindow>& window) { [&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()) if (it == g_attachedWindows.end())
{ {
std::cerr << __func__ << ": Device not found for detachment:\n" std::cerr << __func__ << ": Device not found for detachment:\n"
<< spec.stringify() << "\n"; << spec->stringify() << "\n";
return -1; return -1;
} }
g_attachedWindows.erase(it); g_attachedWindows.erase(it);
std::cout << __func__ << ": Detached X11 window device:\n" std::cout << __func__ << ": Detached X11 window device:\n"
<< spec.stringify() << "\n"; << spec->stringify() << "\n";
return 0; return 0;
} }
+6 -4
View File
@@ -30,10 +30,11 @@ struct WindowSelector
class AttachedWindow class AttachedWindow
{ {
public: public:
AttachedWindow(const smo::device::SenseDeviceSpec& spec); AttachedWindow(const std::shared_ptr<smo::device::SenseDeviceSpec>& spec);
~AttachedWindow(); ~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 WindowSelector& getWindowSelector() const { return windowSelector; }
const std::string& getActualWindowName() const { return actualWindowName; } const std::string& getActualWindowName() const { return actualWindowName; }
void* getXcbConnection() const { return xcbConnectionShared.get(); } void* getXcbConnection() const { return xcbConnectionShared.get(); }
@@ -41,10 +42,11 @@ public:
private: private:
void parseWindowSelector(const smo::device::SenseDeviceSpec& spec); void parseWindowSelector(const smo::device::SenseDeviceSpec& spec);
int getRequiredParamAsInt(const smo::device::SenseDeviceSpec& spec, int getRequiredParamAsInt(
const smo::device::SenseDeviceSpec& spec,
const std::string& paramName); const std::string& paramName);
smo::device::SenseDeviceSpec deviceSpec; std::shared_ptr<smo::device::SenseDeviceSpec> deviceSpec;
WindowSelector windowSelector; WindowSelector windowSelector;
std::string actualWindowName; std::string actualWindowName;
std::shared_ptr<xcb_xorg::XcbConnection> xcbConnectionShared; std::shared_ptr<xcb_xorg::XcbConnection> xcbConnectionShared;
+4 -2
View File
@@ -38,8 +38,10 @@ public:
void finalizeAllSenseApiLibs(void); void finalizeAllSenseApiLibs(void);
void attachAllSenseDevicesFromSpecs(void); void attachAllSenseDevicesFromSpecs(void);
void attachSenseDevice(const device::SenseDeviceSpec& spec); void attachSenseDevice(
void detachSenseDevice(const device::SenseDeviceSpec& spec); const std::shared_ptr<device::SenseDeviceSpec>& spec);
void detachSenseDevice(
const std::shared_ptr<device::SenseDeviceSpec>& spec);
void detachAllSenseDevices(void); void detachAllSenseDevices(void);
std::string stringifyLibs() const; std::string stringifyLibs() const;
+12 -8
View File
@@ -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) if (!libOpt)
{ {
throw std::runtime_error( throw std::runtime_error(
std::string(__func__) + ": No library found for API '" std::string(__func__) + ": No library found for API '"
+ spec.api + "'"); + spec->api + "'");
} }
auto& lib = libOpt.value().get(); auto& lib = libOpt.value().get();
if (!lib.senseApiDesc.sal_mgmt_libOps.attachDeviceReq) 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); 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) if (!libOpt)
{ {
throw std::runtime_error( throw std::runtime_error(
std::string(__func__) + ": No library found for API '" std::string(__func__) + ": No library found for API '"
+ spec.api + "'"); + spec->api + "'");
} }
auto& lib = libOpt.value().get(); auto& lib = libOpt.value().get();
if (!lib.senseApiDesc.sal_mgmt_libOps.detachDeviceReq) if (!lib.senseApiDesc.sal_mgmt_libOps.detachDeviceReq)
@@ -272,14 +276,14 @@ void SenseApiManager::detachSenseDevice(const device::SenseDeviceSpec& spec)
void SenseApiManager::attachAllSenseDevicesFromSpecs(void) void SenseApiManager::attachAllSenseDevicesFromSpecs(void)
{ {
for (const auto& spec : device::DeviceManager::senseDeviceSpecs) { for (const auto& spec : device::DeviceManager::senseDeviceSpecs) {
attachSenseDevice(*spec); attachSenseDevice(spec);
} }
} }
void SenseApiManager::detachAllSenseDevices(void) void SenseApiManager::detachAllSenseDevices(void)
{ {
for (const auto& spec : device::DeviceManager::senseDeviceSpecs) { for (const auto& spec : device::DeviceManager::senseDeviceSpecs) {
detachSenseDevice(*spec); detachSenseDevice(spec);
} }
} }