Files
salmanoff/senseApis/xcbXorg/xcbXorg.cpp
T
hayodea a4f96c8dfa Senselib FFI: Use devDesc object in SenseApiLib, also add per-device metadata to xcbXorg
We also had to write conversion constructors and so on to CSenseDeviceDesc.
The technical debt that's being piled up from this C FFI is excessive.
I think we'll end it here. API Libs will have to be written in C++
from now on. API Lib interfaces will be in C++.

We'll use cross compilers to ensure stability for out-of-tree lib
development.
2025-01-13 11:53:38 -04:00

122 lines
3.2 KiB
C++

#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <vector>
#include <xcb/xcb.h>
#include "xcbXorg.h"
class AttachedDevice {
public:
AttachedDevice(const CSenseDeviceSpec &spec) : deviceSpec(spec) {}
const CSenseDeviceSpec& getSpec() const {
return deviceSpec;
}
private:
CSenseDeviceSpec deviceSpec;
};
static std::vector<AttachedDevice> attachedDevices;
struct XcbConnectionInfo {
std::unique_ptr<xcb_connection_t, decltype(&xcb_disconnect)> connection;
int screenNumber;
XcbConnectionInfo()
: connection(nullptr, &xcb_disconnect), screenNumber(0) {}
};
static XcbConnectionInfo xcbConn;
static CExportedImplexorApiDesc xcbXorgExportedImplexorApis[] =
{
{
.name = "video-implexor"
}
};
static sal_mlo_initializeIndFn xcbXorg_initializeInd;
static sal_mlo_finalizeIndFn xcbXorg_finalizeInd;
static sal_mlo_attachDeviceReqFn xcbXorg_attachDeviceReq;
static sal_mlo_detachDeviceReqFn xcbXorg_detachDeviceReq;
static Csal_mgmt_libOps xcbXorgMgmtLibOps =
{
.initializeInd = xcbXorg_initializeInd,
.finalizeInd = xcbXorg_finalizeInd,
.attachDeviceReq = xcbXorg_attachDeviceReq,
.detachDeviceReq = xcbXorg_detachDeviceReq
};
static CSenseApiDesc xcbXorgApiDesc =
{
.name = "xcb-xorg",
.numExportedImplexorApis = sizeof(xcbXorgExportedImplexorApis) /
sizeof(*xcbXorgExportedImplexorApis),
.exportedImplexorApis = xcbXorgExportedImplexorApis,
.sal_mgmt_libOps = &xcbXorgMgmtLibOps
};
extern HK_UNMANGLED getSenseApiDescFn HK_GET_SENSE_API_DESC_FN_NAME;
const CSenseApiDesc *HK_GET_SENSE_API_DESC_FN_NAME(void)
{
return &xcbXorgApiDesc;
}
static sal_mlo_initializeIndFn xcbXorg_initializeInd;
int xcbXorg_initializeInd(void)
{
xcbConn.connection.reset(
xcb_connect(nullptr, &xcbConn.screenNumber));
if (xcb_connection_has_error(xcbConn.connection.get()))
{
throw std::runtime_error(
std::string(__func__) + ": Failed to connect to X server");
}
std::cout << __func__ << ": Connected to X server, screen number "
<< xcbConn.screenNumber << "\n";
return 0;
}
static sal_mlo_finalizeIndFn xcbXorg_finalizeInd;
int xcbXorg_finalizeInd(void)
{
if (!xcbConn.connection) {
return 0;
}
xcbConn.connection.reset();
std::cout << __func__ << ": Disconnected from X server\n";
return 0;
}
static sal_mlo_attachDeviceReqFn xcbXorg_attachDeviceReq;
int xcbXorg_attachDeviceReq(const CSenseDeviceSpec *const desc)
{
attachedDevices.emplace_back(*desc);
std::ostringstream os;
for (const auto& device : attachedDevices) {
os << device.getSpec();
}
std::cout << __func__ << ": >>>> Attaching device spec: " << *desc << "\n"
<< " >>>> Current attached devices:\n" << os.str();
return 0;
}
static sal_mlo_detachDeviceReqFn xcbXorg_detachDeviceReq;
int xcbXorg_detachDeviceReq(const CSenseDeviceSpec *const spec)
{
auto it = std::remove_if(attachedDevices.begin(), attachedDevices.end(),
[spec](const AttachedDevice &device) {
return device.getSpec() == *spec;
});
attachedDevices.erase(it, attachedDevices.end());
std::cout << __func__ << ": Detaching device spec\n";
return 0;
}