Add dbus server

This commit is contained in:
Guillaume Meunier
2024-09-27 19:25:57 +02:00
parent b93a937ae1
commit d0850e819c
17 changed files with 338 additions and 94 deletions
+2 -12
View File
@@ -26,8 +26,7 @@ include("cmake/AutoOption.cmake")
option(WIVRN_USE_SYSTEM_FREETYPE "Use system FreeType" ON)
auto_option(WIVRN_USE_SYSTEM_BOOST "Use system Boost headers" AUTO)
auto_option(WIVRN_USE_SYSTEM_OPENXR "Use system OpenXR headers" AUTO)
auto_option(WIVRN_USE_SYSTEMD "Use libsystemd, either libsystemd or glib is required to show pretty hostname" AUTO)
auto_option(WIVRN_USE_GLIB "Use glib, required to show pretty hostname inside a flatpak container" OFF)
auto_option(WIVRN_USE_SYSTEMD "Use libsystemd" AUTO)
option(WIVRN_USE_ANDROID_VALIDATION_LAYER "Download the android version of the Vulkan validation layer" OFF)
option(WIVRN_BUILD_CLIENT "Build WiVRn client" OFF)
option(WIVRN_BUILD_SERVER "Build WiVRn server" ON)
@@ -144,16 +143,7 @@ if (WIVRN_BUILD_SERVER)
find_package(nlohmann_json REQUIRED)
find_package(CLI11 REQUIRED)
if (WIVRN_USE_GLIB STREQUAL "AUTO")
pkg_check_modules(glib2 IMPORTED_TARGET glib-2.0 gio-2.0)
if(glib2_FOUND)
set(WIVRN_USE_GLIB ON)
else()
set(WIVRN_USE_GLIB OFF)
endif()
elseif (WIVRN_USE_GLIB)
pkg_check_modules(glib2 REQUIRED IMPORTED_TARGET glib-2.0 gio-2.0)
endif()
pkg_check_modules(glib2 REQUIRED IMPORTED_TARGET glib-2.0 gio-2.0 gio-unix-2.0)
endif()
if (WIVRN_BUILD_CLIENT AND NOT ANDROID)
-1
View File
@@ -24,7 +24,6 @@
#cmakedefine WIVRN_USE_X264
#cmakedefine WIVRN_USE_SYSTEMD
#cmakedefine WIVRN_USE_GLIB
#cmakedefine WIVRN_USE_PIPEWIRE
#cmakedefine WIVRN_USE_PULSEAUDIO
+2
View File
@@ -142,6 +142,8 @@ public:
void send_raw(const std::vector<std::span<uint8_t>> & data);
};
using UnixDatagram = UDP;
class TCPListener : public fd_base
{
public:
+48
View File
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/io/github/wivrn/Server">
<interface name="org.freedesktop.DBus.Properties">
<method name="Get">
<arg name="interface_name" direction="in" type="s"/>
<arg name="property_name" direction="in" type="s"/>
<arg name="value" direction="out" type="v"/>
</method>
<method name="GetAll">
<arg name="interface_name" direction="in" type="s"/>
<arg name="props" direction="out" type="a{sv}"/>
</method>
<method name="Set">
<arg name="interface_name" direction="in" type="s"/>
<arg name="property_name" direction="in" type="s"/>
<arg name="value" direction="in" type="v"/>
</method>
<signal name="PropertiesChanged">
<arg type="s" name="interface_name"/>
<arg type="a{sv}" name="changed_properties"/>
<arg type="as" name="invalidated_properties"/>
</signal>
</interface>
<interface name="io.github.wivrn.Server">
<method name="Disconnect"/>
<method name="Quit"/>
<property name="HeadsetConnected" type="b" access="read"/>
<property name="SteamCommand" type="s" access="read"/>
<property name="JsonConfiguration" type="s" access="readwrite"/>
<!-- Data from the headset info packet -->
<property name="RecommendedEyeSize" type="(uu)" access="read"/>
<property name="AvailableRefreshRates" type="ad" access="read"/>
<property name="PreferredRefreshRate" type="d" access="read"/>
<property name="SpeakerChannels" type="u" access="read"/>
<property name="SpeakerSampleRate" type="u" access="read"/>
<property name="MicChannels" type="u" access="read"/>
<property name="MicSampleRate" type="u" access="read"/>
<property name="FieldOfView" type="a(dddd)" access="read"/>
<property name="HandTracking" type="b" access="read"/>
<property name="EyeGaze" type="b" access="read"/>
<property name="FaceTracking" type="b" access="read"/>
<property name="SupportedCodecs" type="as" access="read"/>
</interface>
</node>
+1 -1
View File
@@ -25,6 +25,7 @@ finish-args:
- --filesystem=xdg-config/openvr:create
# Let outside application access the WiVRn socket
- --filesystem=xdg-run/wivrn:create
- --own-name=io.github.wivrn.Server
cleanup:
- /lib/pkgconfig
@@ -147,7 +148,6 @@ modules:
- -DWIVRN_USE_X264=ON
- -DWIVRN_USE_NVENC=ON
- -DWIVRN_USE_PULSEAUDIO=OFF
- -DWIVRN_USE_GLIB=ON
- -DFETCHCONTENT_FULLY_DISCONNECTED=ON
- -DFETCHCONTENT_BASE_DIR=deps
- -DXRT_IPC_MSG_SOCK_FILENAME=wivrn/comp_ipc
+13 -3
View File
@@ -142,6 +142,18 @@ if(WIVRN_USE_PULSEAUDIO)
target_link_libraries(wivrn-server PRIVATE PkgConfig::libpulse)
endif()
# Generate dbus interface code
add_custom_command(OUTPUT wivrn_server_dbus.c wivrn_server_dbus.h
COMMAND gdbus-codegen ${CMAKE_CURRENT_SOURCE_DIR}/../dbus/io.github.wivrn.Server.xml --interface-prefix io.github.wivrn --generate-c-code wivrn_server_dbus --c-namespace Wivrn
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../dbus/io.github.wivrn.Server.xml)
add_custom_target(wivrn_server_dbus ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/wivrn_server_dbus.c ${CMAKE_CURRENT_BINARY_DIR}/wivrn_server_dbus.h)
add_dependencies(wivrn-server wivrn_server_dbus)
target_sources(wivrn-server PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/wivrn_server_dbus.c
${CMAKE_CURRENT_BINARY_DIR}/wivrn_server_dbus.h
)
target_include_directories(wivrn-server PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
include(CompileGLSL)
file(GLOB VULKAN_SHADERS CONFIGURE_DEPENDS "shaders/*.glsl")
@@ -166,9 +178,7 @@ target_link_libraries(
nlohmann_json::nlohmann_json
)
if(WIVRN_USE_GLIB)
target_link_libraries( wivrn-server PRIVATE PkgConfig::glib2)
endif()
target_link_libraries(wivrn-server PRIVATE PkgConfig::glib2)
configure_file(dist/wivrn.service.in wivrn.service)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/wivrn.service
+14 -8
View File
@@ -19,28 +19,27 @@
#include "accept_connection.h"
#include "wivrn_ipc.h"
#include "wivrn_packets.h"
#include "wivrn_sockets.h"
#include <sys/poll.h>
extern int control_pipe_fd;
static void report_status(int status)
static void handle_event_from_main_loop(to_monado::disconnect)
{
uint8_t status_byte = status;
write(control_pipe_fd, &status_byte, sizeof(status_byte));
// Ignore disconnect request when no headset is connected
}
std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd, std::function<bool()> quit)
{
report_status(0);
wivrn_ipc_socket_monado->send(from_monado::headsdet_disconnected{});
xrt::drivers::wivrn::TCPListener listener(xrt::drivers::wivrn::default_port);
pollfd fds[2]{
pollfd fds[3]{
{.fd = watch_fd, .events = POLLIN},
{.fd = listener.get_fd(), .events = POLLIN},
{.fd = wivrn_ipc_socket_monado->get_fd(), .events = POLLIN},
};
while (not(quit and quit()))
@@ -56,9 +55,16 @@ std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd, std::f
if (fds[1].revents & POLLIN)
{
report_status(1);
wivrn_ipc_socket_monado->send(from_monado::headsdet_connected{});
return std::make_unique<xrt::drivers::wivrn::TCP>(listener.accept().first);
}
if (fds[2].revents & POLLIN)
{
auto packet = receive_from_main();
if (packet)
std::visit([](auto && x) { handle_event_from_main_loop(x); }, *packet);
}
}
return {};
+5
View File
@@ -56,6 +56,11 @@ void configuration::set_config_file(const std::filesystem::path & path)
config_file = path;
}
const std::filesystem::path & configuration::get_config_file()
{
return config_file;
}
configuration configuration::read_user_configuration()
{
configuration result;
+1
View File
@@ -49,6 +49,7 @@ struct configuration
bool tcp_only = false;
static void set_config_file(const std::filesystem::path &);
static const std::filesystem::path & get_config_file();
static configuration read_user_configuration();
};
+19 -1
View File
@@ -20,11 +20,17 @@
#include "wivrn_connection.h"
#include "configuration.h"
#include "util/u_logging.h"
#include "wivrn_ipc.h"
#include <arpa/inet.h>
#include <poll.h>
using namespace std::chrono_literals;
static void handle_event_from_main_loop(to_monado::disconnect)
{
// Ignore disconnect request when no headset is connected
}
wivrn_connection::wivrn_connection(TCP && tcp) :
control(std::move(tcp)), stream(-1)
{
@@ -68,11 +74,13 @@ void wivrn_connection::init()
while (true)
{
pollfd fds[2] = {};
pollfd fds[3] = {};
fds[0].events = POLLIN;
fds[0].fd = stream.get_fd();
fds[1].events = POLLIN;
fds[1].fd = control.get_fd();
fds[2].fd = wivrn_ipc_socket_monado->get_fd();
fds[2].events = POLLIN;
int r = ::poll(fds, std::size(fds), 1000);
if (r < 0)
@@ -84,6 +92,9 @@ void wivrn_connection::init()
if (fds[1].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on control socket");
if (fds[2].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on IPC socket");
if (fds[0].revents & POLLIN)
{
auto [packet, peer_addr] = stream.receive_from_raw();
@@ -116,6 +127,13 @@ void wivrn_connection::init()
}
}
if (fds[2].revents & POLLIN)
{
auto packet = receive_from_main();
if (packet)
std::visit([](auto && x) { handle_event_from_main_loop(x); }, *packet);
}
if (std::chrono::steady_clock::now() > timeout)
{
throw std::runtime_error("No handshake received from client");
+14 -1
View File
@@ -19,6 +19,7 @@
#pragma once
#include "wivrn_ipc.h"
#include "wivrn_packets.h"
#include "wivrn_sockets.h"
#include <optional>
@@ -81,11 +82,13 @@ public:
template <typename T>
int poll(T && visitor, int timeout)
{
pollfd fds[2] = {};
pollfd fds[3] = {};
fds[0].events = POLLIN;
fds[0].fd = stream.get_fd();
fds[1].events = POLLIN;
fds[1].fd = control.get_fd();
fds[2].fd = wivrn_ipc_socket_monado->get_fd();
fds[2].events = POLLIN;
while (auto packet = stream.receive_pending())
std::visit(std::forward<T>(visitor), std::move(*packet));
@@ -102,6 +105,9 @@ public:
if (fds[1].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on control socket");
if (fds[2].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on IPC socket");
if (fds[0].revents & POLLIN)
{
auto packet = stream.receive();
@@ -115,6 +121,13 @@ public:
if (packet)
std::visit(std::forward<T>(visitor), std::move(*packet));
}
if (fds[2].revents & POLLIN)
{
auto packet = receive_from_main();
if (packet)
std::visit(std::forward<T>(visitor), std::move(*packet));
}
return r;
}
};
+8 -1
View File
@@ -33,16 +33,17 @@
#include "audio/audio_setup.h"
#include "wivrn_comp_target.h"
#include "wivrn_config.h"
#include "wivrn_controller.h"
#include "wivrn_eye_tracker.h"
#include "wivrn_fb_face2_tracker.h"
#include "wivrn_foveation.h"
#include "wivrn_hmd.h"
#include "wivrn_ipc.h"
#include "xrt/xrt_session.h"
#include <cmath>
#include <magic_enum.hpp>
#include <stdexcept>
#include <vulkan/vulkan.h>
#ifdef WIVRN_FEATURE_STEAMVR_LIGHTHOUSE
@@ -134,6 +135,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
}
self->info = std::get<from_headset::headset_info_packet>(*control);
send_to_main(self->info);
try
{
@@ -407,6 +409,11 @@ void wivrn_session::operator()(audio_data && data)
audio_handle->process_mic_data(std::move(data));
}
void wivrn_session::operator()(to_monado::disconnect &&)
{
throw std::runtime_error("Disconnecting as requested by main loop");
}
void wivrn_session::run(std::weak_ptr<wivrn_session> weak_self)
{
while (true)
+3
View File
@@ -21,6 +21,7 @@
#include "clock_offset.h"
#include "wivrn_connection.h"
#include "wivrn_ipc.h"
#include "wivrn_packets.h"
#include "xrt/xrt_defines.h"
#include "xrt/xrt_results.h"
@@ -145,6 +146,8 @@ public:
void operator()(from_headset::battery &&);
void operator()(audio_data &&);
void operator()(to_monado::disconnect &&);
template <typename T>
void send_stream(T && packet)
{
-38
View File
@@ -3,17 +3,11 @@
#include <unistd.h>
#include "util/u_logging.h"
#include "wivrn_config.h"
#ifdef WIVRN_USE_GLIB
#include <gio/gio.h>
#elif defined(WIVRN_USE_SYSTEMD)
#include <systemd/sd-bus.h>
#endif
static std::string _hostname()
{
#ifdef WIVRN_USE_GLIB
GError * error = NULL;
GDBusConnection * con = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
@@ -59,38 +53,6 @@ static std::string _hostname()
g_object_unref(con);
}
#elif defined(WIVRN_USE_SYSTEMD)
sd_bus * bus;
if (sd_bus_default_system(&bus) < 0)
{
U_LOG_W("Failed to connect to system bus");
}
else
{
for (auto property: {"PrettyHostname", "StaticHostname", "Hostname"})
{
char * hostname = nullptr;
sd_bus_error error = SD_BUS_ERROR_NULL;
if (sd_bus_get_property_string(bus, "org.freedesktop.hostname1", "/org/freedesktop/hostname1", "org.freedesktop.hostname1", property, &error, &hostname) < 0)
{
sd_bus_error_free(&error);
continue;
}
sd_bus_error_free(&error);
if (hostname && strcmp(hostname, ""))
{
std::string s = hostname;
free(hostname);
sd_bus_unref(bus);
return s;
}
}
sd_bus_unref(bus);
}
#endif
char buf[HOST_NAME_MAX];
int code = gethostname(buf, sizeof(buf));
+173 -27
View File
@@ -5,6 +5,7 @@
* @brief Main file for WiVRn Monado service.
*/
#include "openxr/openxr.h"
#include "util/u_trace_marker.h"
#include "active_runtime.h"
@@ -15,12 +16,16 @@
#include "version.h"
#include "wivrn_config.h"
#include "wivrn_ipc.h"
#include "wivrn_packets.h"
#include "wivrn_sockets.h"
#include "wivrn_server_dbus.h"
#include <CLI/CLI.hpp>
#include <avahi-glib/glib-watch.h>
#include <filesystem>
#include <iostream>
#include <iterator>
#include <memory>
#include <poll.h>
#include <sys/signalfd.h>
@@ -46,9 +51,9 @@ extern "C"
using namespace xrt::drivers::wivrn;
std::unique_ptr<TCP> tcp;
std::unique_ptr<TCPListener> listener;
int control_pipe_fd;
static std::unique_ptr<TCPListener> listener;
std::optional<xrt::drivers::wivrn::typed_socket<xrt::drivers::wivrn::UnixDatagram, from_monado::packets, to_monado::packets>> wivrn_ipc_socket_main_loop;
std::optional<xrt::drivers::wivrn::typed_socket<xrt::drivers::wivrn::UnixDatagram, to_monado::packets, from_monado::packets>> wivrn_ipc_socket_monado;
#ifdef WIVRN_USE_SYSTEMD
std::string socket_path()
@@ -163,8 +168,8 @@ static std::string steam_command()
std::string command = "PRESSURE_VESSEL_FILESYSTEMS_RW=" + pressure_vessel_filesystems_rw + " %command%";
if (auto p = active_runtime::manifest_path().string(); p.starts_with("/usr"))
command = "XR_RUNTIME_JSON=/run/host" + p + " " + command;
if (auto p = active_runtime::manifest_path().string(); p.starts_with("/usr"))
command = "XR_RUNTIME_JSON=/run/host" + p + " " + command;
return command;
}
@@ -190,6 +195,8 @@ bool quitting_main_loop;
guint listener_watch;
WivrnServer * dbus_server;
/* TODO: Document FSM
*/
std::optional<active_runtime> runtime_setter;
@@ -197,6 +204,7 @@ std::optional<avahi_publisher> publisher;
gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data);
void stop_listening();
void on_headset_info_packet(const xrt::drivers::wivrn::from_headset::headset_info_packet & info);
void start_publishing();
void stop_publishing();
@@ -395,6 +403,7 @@ void update_fsm()
start_listening();
start_publishing();
wivrn_server_set_headset_connected(dbus_server, false);
}
}
}
@@ -430,36 +439,162 @@ gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data)
gboolean control_received(gint fd, GIOCondition condition, gpointer user_data)
{
uint8_t status;
if (read(fd, &status, sizeof(status)) == sizeof(status))
auto packet = wivrn_ipc_socket_main_loop->receive();
if (packet)
{
switch (status)
if (std::holds_alternative<xrt::drivers::wivrn::from_headset::headset_info_packet>(*packet))
{
case 0:
start_publishing();
break;
case 1:
stop_publishing();
break;
default:
std::cerr << "Unexpected status received: " << (int)status << std::endl;
break;
on_headset_info_packet(std::get<xrt::drivers::wivrn::from_headset::headset_info_packet>(*packet));
wivrn_server_set_headset_connected(dbus_server, true);
}
else if (std::holds_alternative<from_monado::headsdet_connected>(*packet))
{
stop_publishing();
wivrn_server_set_headset_connected(dbus_server, true);
}
else if (std::holds_alternative<from_monado::headsdet_disconnected>(*packet))
{
start_publishing();
wivrn_server_set_headset_connected(dbus_server, false);
}
}
else
{
perror("read control pipe");
}
return true;
}
gboolean
on_handle_disconnect(WivrnServer * skeleton,
GDBusMethodInvocation * invocation,
gpointer user_data)
{
wivrn_ipc_socket_main_loop->send(to_monado::disconnect{});
return true;
}
gboolean
on_handle_quit(WivrnServer * skeleton,
GDBusMethodInvocation * invocation,
gpointer user_data)
{
quitting_main_loop = true;
update_fsm();
return true;
}
void on_json_configuration(WivrnServer * server, const GParamSpec * pspec, gpointer data)
{
const char * json = wivrn_server_get_json_configuration(server);
std::filesystem::path config = configuration::get_config_file();
std::filesystem::path config_new = config;
config_new += ".new";
std::ofstream file(config_new);
file.write(json, strlen(json));
std::error_code ec;
std::filesystem::rename(config_new, config, ec);
if (ec)
std::cerr << "Failed to save configuration: " << ec.message() << std::endl;
}
void on_headset_info_packet(const xrt::drivers::wivrn::from_headset::headset_info_packet & info)
{
GVariantBuilder * builder;
GVariant * value_eye_size = g_variant_new("(uu)", info.recommended_eye_width, info.recommended_eye_height);
wivrn_server_set_recommended_eye_size(dbus_server, value_eye_size);
builder = g_variant_builder_new(G_VARIANT_TYPE("ad"));
for (double rate: info.available_refresh_rates)
{
g_variant_builder_add(builder, "d", rate);
}
GVariant * value_refresh_rates = g_variant_new("ad", builder);
g_variant_builder_unref(builder);
wivrn_server_set_available_refresh_rates(dbus_server, value_refresh_rates);
wivrn_server_set_preferred_refresh_rate(dbus_server, info.preferred_refresh_rate);
auto speaker = info.speaker.value_or(xrt::drivers::wivrn::from_headset::headset_info_packet::audio_description{});
wivrn_server_set_speaker_channels(dbus_server, speaker.num_channels);
wivrn_server_set_speaker_sample_rate(dbus_server, speaker.sample_rate);
auto mic = info.microphone.value_or(xrt::drivers::wivrn::from_headset::headset_info_packet::audio_description{});
wivrn_server_set_mic_channels(dbus_server, mic.num_channels);
wivrn_server_set_mic_sample_rate(dbus_server, mic.sample_rate);
builder = g_variant_builder_new(G_VARIANT_TYPE("a(dddd)"));
for (XrFovf fov: info.fov)
{
g_variant_builder_add(builder, "(dddd)", fov.angleLeft, fov.angleRight, fov.angleUp, fov.angleDown);
}
GVariant * value_field_of_view = g_variant_new("a(dddd)", builder);
g_variant_builder_unref(builder);
wivrn_server_set_field_of_view(dbus_server, value_field_of_view);
wivrn_server_set_hand_tracking(dbus_server, info.hand_tracking);
wivrn_server_set_eye_gaze(dbus_server, info.eye_gaze);
wivrn_server_set_face_tracking(dbus_server, info.face_tracking2_fb);
std::vector<const char *> codecs;
static const std::map<video_codec, const char *> codec_names = {
{h264, "h264"},
{h265, "h265"},
{av1, "av1"}};
for (video_codec codec: info.supported_codecs)
{
auto it = codec_names.find(codec);
if (it != codec_names.end())
codecs.push_back(it->second);
}
codecs.push_back(nullptr);
wivrn_server_set_supported_codecs(dbus_server, codecs.data());
}
void on_name_acquired(GDBusConnection * connection, const gchar * name, gpointer user_data)
{
dbus_server = wivrn_server_skeleton_new();
g_signal_connect(dbus_server,
"handle-disconnect",
G_CALLBACK(on_handle_disconnect),
NULL);
g_signal_connect(dbus_server,
"handle-quit",
G_CALLBACK(on_handle_quit),
NULL);
wivrn_server_set_steam_command(dbus_server, steam_command().c_str());
on_headset_info_packet({});
std::ifstream file(configuration::get_config_file());
std::string config{std::istream_iterator<char>(file), std::istream_iterator<char>()};
wivrn_server_set_json_configuration(dbus_server, config.c_str());
g_signal_connect(dbus_server, "notify::json-configuration", G_CALLBACK(on_json_configuration), NULL);
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(dbus_server),
connection,
"/io/github/wivrn/Server",
NULL);
}
} // namespace
int inner_main(int argc, char * argv[], bool show_instructions)
{
std::cerr << "WiVRn " << xrt::drivers::wivrn::git_version << " starting" << std::endl;
std::cerr << "For Steam games, set command to " << steam_command() << std::endl;
if (show_instructions)
{
std::cerr << "WiVRn " << xrt::drivers::wivrn::git_version << " starting" << std::endl;
std::cerr << "For Steam games, set command to " << steam_command() << std::endl;
}
#ifdef WIVRN_USE_SYSTEMD
create_listen_socket();
@@ -486,15 +621,16 @@ int inner_main(int argc, char * argv[], bool show_instructions)
fcntl(stdin_pipe_fds[0], F_SETFD, FD_CLOEXEC);
fcntl(stdin_pipe_fds[1], F_SETFD, FD_CLOEXEC);
// Create a pipe to report monado status to the main loop
if (pipe(control_pipe_fds) < 0)
// Create a socket to report monado status to the main loop
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, control_pipe_fds) < 0)
{
perror("pipe");
perror("socketpair");
return EXIT_FAILURE;
}
fcntl(control_pipe_fds[0], F_SETFD, FD_CLOEXEC);
fcntl(control_pipe_fds[1], F_SETFD, FD_CLOEXEC);
control_pipe_fd = control_pipe_fds[1];
wivrn_ipc_socket_main_loop.emplace(control_pipe_fds[0]);
wivrn_ipc_socket_monado.emplace(control_pipe_fds[1]);
auto control_listener = g_unix_fd_source_new(control_pipe_fds[0], GIOCondition::G_IO_IN);
g_source_set_callback(control_listener, G_SOURCE_FUNC(&control_received), nullptr, nullptr);
@@ -511,6 +647,16 @@ int inner_main(int argc, char * argv[], bool show_instructions)
g_unix_signal_add(SIGINT, &sigint, (void *)SIGINT);
g_unix_signal_add(SIGTERM, &sigint, (void *)SIGTERM);
// Add dbus server
g_bus_own_name(G_BUS_TYPE_SESSION,
"io.github.wivrn.Server",
G_BUS_NAME_OWNER_FLAGS_NONE,
nullptr,
on_name_acquired,
nullptr,
nullptr,
nullptr);
// Main loop
g_main_loop_run(main_loop);
+7
View File
@@ -23,6 +23,8 @@
#include <sys/mman.h>
#include <sys/wait.h>
std::unique_ptr<xrt::drivers::wivrn::TCP> tcp;
struct cleanup_function
{
void (*callback)(uintptr_t);
@@ -31,6 +33,11 @@ struct cleanup_function
std::array<cleanup_function, 1024> * cleanup_functions; // In shared memory
std::optional<to_monado::packets> receive_from_main()
{
return wivrn_ipc_socket_monado->receive();
}
void init_cleanup_functions()
{
if (cleanup_functions)
+28 -1
View File
@@ -19,12 +19,39 @@
#pragma once
#include <array>
#include "wivrn_packets.h"
#include <memory>
#include <wivrn_sockets.h>
extern std::unique_ptr<xrt::drivers::wivrn::TCP> tcp;
namespace from_monado
{
struct headsdet_connected
{};
struct headsdet_disconnected
{};
using packets = std::variant<xrt::drivers::wivrn::from_headset::headset_info_packet, headsdet_connected, headsdet_disconnected>;
} // namespace from_monado
namespace to_monado
{
struct disconnect
{};
using packets = std::variant<disconnect>;
} // namespace to_monado
extern std::optional<xrt::drivers::wivrn::typed_socket<xrt::drivers::wivrn::UnixDatagram, to_monado::packets, from_monado::packets>> wivrn_ipc_socket_monado;
std::optional<to_monado::packets> receive_from_main();
template <typename T>
void send_to_main(T && packet)
{
wivrn_ipc_socket_monado->send(std::forward<T>(packet));
}
void init_cleanup_functions();
void add_cleanup_function(void (*callback)(uintptr_t), uintptr_t userdata);
void remove_cleanup_function(void (*callback)(uintptr_t), uintptr_t userdata);