Files
salmanoff/senseApis/xcbXorg/xcbXorg.cpp
T
hayodea 09caf314f1 Eliminate the C FFI; Namespace lib API and DeviceManager
We decided to get rid of the C FFI for libs. It was becoming too intricate
and complicated. It was becoming a technical burden and expanding into
too much extra code. It's unfortunate, but we'll have to give up on getting
out-of-tree hot-loadable libraries the easy way.

It's possible to still do it with cross compilation or by keeping track
of the libstdc++ version that the running harikoff binary was compiled
against. Then we can ensure that our loadable lib code is linked against
that same libstdc++ code and this should ensure ABI stability.
2025-01-13 21:57:11 -04:00

110 lines
3.0 KiB
C++

#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <vector>
#include <user/senseDeviceSpec.h>
#include <user/senseApiDesc.h>
#include <xcb/xcb.h>
#include "xcbXorg.h"
class AttachedDevice {
public:
AttachedDevice(const hk::device::SenseDeviceSpec &spec)
: deviceSpec(spec)
{}
const hk::device::SenseDeviceSpec& getSpec() const {
return deviceSpec;
}
private:
hk::device::SenseDeviceSpec deviceSpec;
};
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 std::vector<AttachedDevice> attachedDevices;
static hk::sense_api::sal_mlo_initializeIndFn xcbXorg_initializeInd;
static hk::sense_api::sal_mlo_finalizeIndFn xcbXorg_finalizeInd;
static hk::sense_api::sal_mlo_attachDeviceReqFn xcbXorg_attachDeviceReq;
static hk::sense_api::sal_mlo_detachDeviceReqFn xcbXorg_detachDeviceReq;
static hk::sense_api::SenseApiDesc xcbXorgApiDesc =
{
.name = "xcb-xorg",
.exportedImplexorApis = { { "video-implexor" } },
.sal_mgmt_libOps = {
.initializeInd = xcbXorg_initializeInd,
.finalizeInd = xcbXorg_finalizeInd,
.attachDeviceReq = xcbXorg_attachDeviceReq,
.detachDeviceReq = xcbXorg_detachDeviceReq
}
};
extern HK_UNMANGLED hk::sense_api::getSenseApiDescFn
HK_GET_SENSE_API_DESC_FN_NAME;
const hk::sense_api::SenseApiDesc &HK_GET_SENSE_API_DESC_FN_NAME(void)
{
return xcbXorgApiDesc;
}
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;
}
int xcbXorg_finalizeInd(void)
{
if (!xcbConn.connection) {
return 0;
}
xcbConn.connection.reset();
std::cout << __func__ << ": Disconnected from X server\n";
return 0;
}
int xcbXorg_attachDeviceReq(const hk::device::SenseDeviceSpec *const desc)
{
attachedDevices.emplace_back(*desc);
std::ostringstream os;
for (const auto& device : attachedDevices) {
os << device.getSpec().stringify();
}
std::cout << __func__ << ": >>>> Attaching device spec: " << desc->stringify() << "\n"
<< " >>>> Current attached devices:\n" << os.str();
return 0;
}
int xcbXorg_detachDeviceReq(const hk::device::SenseDeviceSpec *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;
}