From d0850e819c87dbe0d829e6023b55a178aba1a9ce Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Fri, 6 Sep 2024 14:25:40 +0200 Subject: [PATCH] Add dbus server --- CMakeLists.txt | 14 +- common/wivrn_config.h.in | 1 - common/wivrn_sockets.h | 2 + dbus/io.github.wivrn.Server.xml | 48 +++++++ flatpak/io.github.wivrn.wivrn.yml | 2 +- server/CMakeLists.txt | 16 ++- server/accept_connection.cpp | 22 ++-- server/driver/configuration.cpp | 5 + server/driver/configuration.h | 1 + server/driver/wivrn_connection.cpp | 20 ++- server/driver/wivrn_connection.h | 15 ++- server/driver/wivrn_session.cpp | 9 +- server/driver/wivrn_session.h | 3 + server/hostname.cpp | 38 ------ server/main.cpp | 200 +++++++++++++++++++++++++---- server/wivrn_ipc.cpp | 7 + server/wivrn_ipc.h | 29 ++++- 17 files changed, 338 insertions(+), 94 deletions(-) create mode 100644 dbus/io.github.wivrn.Server.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ccf89f6..720b3e31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/common/wivrn_config.h.in b/common/wivrn_config.h.in index f6cbd90e..7cd7a22a 100644 --- a/common/wivrn_config.h.in +++ b/common/wivrn_config.h.in @@ -24,7 +24,6 @@ #cmakedefine WIVRN_USE_X264 #cmakedefine WIVRN_USE_SYSTEMD -#cmakedefine WIVRN_USE_GLIB #cmakedefine WIVRN_USE_PIPEWIRE #cmakedefine WIVRN_USE_PULSEAUDIO diff --git a/common/wivrn_sockets.h b/common/wivrn_sockets.h index 9466272e..05b0767c 100644 --- a/common/wivrn_sockets.h +++ b/common/wivrn_sockets.h @@ -142,6 +142,8 @@ public: void send_raw(const std::vector> & data); }; +using UnixDatagram = UDP; + class TCPListener : public fd_base { public: diff --git a/dbus/io.github.wivrn.Server.xml b/dbus/io.github.wivrn.Server.xml new file mode 100644 index 00000000..df9ad3e0 --- /dev/null +++ b/dbus/io.github.wivrn.Server.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/flatpak/io.github.wivrn.wivrn.yml b/flatpak/io.github.wivrn.wivrn.yml index 1a144687..841aa103 100644 --- a/flatpak/io.github.wivrn.wivrn.yml +++ b/flatpak/io.github.wivrn.wivrn.yml @@ -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 diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 5b7c25c2..9c4968a6 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -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 diff --git a/server/accept_connection.cpp b/server/accept_connection.cpp index 8e5e34ce..1aaa22e0 100644 --- a/server/accept_connection.cpp +++ b/server/accept_connection.cpp @@ -19,28 +19,27 @@ #include "accept_connection.h" +#include "wivrn_ipc.h" #include "wivrn_packets.h" #include "wivrn_sockets.h" #include -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 accept_connection(int watch_fd, std::function 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 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(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 {}; diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index dc185a5d..e1fe2199 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -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; diff --git a/server/driver/configuration.h b/server/driver/configuration.h index 4cefcd05..3137a4d3 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -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(); }; diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index 7741c178..95b02247 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -20,11 +20,17 @@ #include "wivrn_connection.h" #include "configuration.h" #include "util/u_logging.h" +#include "wivrn_ipc.h" #include #include 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"); diff --git a/server/driver/wivrn_connection.h b/server/driver/wivrn_connection.h index b7f3f116..2a75a0e7 100644 --- a/server/driver/wivrn_connection.h +++ b/server/driver/wivrn_connection.h @@ -19,6 +19,7 @@ #pragma once +#include "wivrn_ipc.h" #include "wivrn_packets.h" #include "wivrn_sockets.h" #include @@ -81,11 +82,13 @@ public: template 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(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(visitor), std::move(*packet)); } + + if (fds[2].revents & POLLIN) + { + auto packet = receive_from_main(); + if (packet) + std::visit(std::forward(visitor), std::move(*packet)); + } return r; } }; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index e5853193..d17fb20c 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -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 #include +#include #include #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(*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 weak_self) { while (true) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 81b7c298..16d5b803 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -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 void send_stream(T && packet) { diff --git a/server/hostname.cpp b/server/hostname.cpp index 88dc4c2b..951a5059 100644 --- a/server/hostname.cpp +++ b/server/hostname.cpp @@ -3,17 +3,11 @@ #include #include "util/u_logging.h" -#include "wivrn_config.h" -#ifdef WIVRN_USE_GLIB #include -#elif defined(WIVRN_USE_SYSTEMD) -#include -#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)); diff --git a/server/main.cpp b/server/main.cpp index 2ccc4b2d..473de9aa 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -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 #include #include #include +#include #include #include #include @@ -46,9 +51,9 @@ extern "C" using namespace xrt::drivers::wivrn; -std::unique_ptr tcp; -std::unique_ptr listener; -int control_pipe_fd; +static std::unique_ptr listener; +std::optional> wivrn_ipc_socket_main_loop; +std::optional> 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 runtime_setter; @@ -197,6 +204,7 @@ std::optional 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(*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(*packet)); + wivrn_server_set_headset_connected(dbus_server, true); + } + else if (std::holds_alternative(*packet)) + { + stop_publishing(); + wivrn_server_set_headset_connected(dbus_server, true); + } + else if (std::holds_alternative(*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 codecs; + static const std::map 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(file), std::istream_iterator()}; + + 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); diff --git a/server/wivrn_ipc.cpp b/server/wivrn_ipc.cpp index b5e074cd..8e1076bf 100644 --- a/server/wivrn_ipc.cpp +++ b/server/wivrn_ipc.cpp @@ -23,6 +23,8 @@ #include #include +std::unique_ptr tcp; + struct cleanup_function { void (*callback)(uintptr_t); @@ -31,6 +33,11 @@ struct cleanup_function std::array * cleanup_functions; // In shared memory +std::optional receive_from_main() +{ + return wivrn_ipc_socket_monado->receive(); +} + void init_cleanup_functions() { if (cleanup_functions) diff --git a/server/wivrn_ipc.h b/server/wivrn_ipc.h index fd03295a..0b686cf4 100644 --- a/server/wivrn_ipc.h +++ b/server/wivrn_ipc.h @@ -19,12 +19,39 @@ #pragma once -#include +#include "wivrn_packets.h" #include #include extern std::unique_ptr tcp; +namespace from_monado +{ +struct headsdet_connected +{}; +struct headsdet_disconnected +{}; + +using packets = std::variant; +} // namespace from_monado + +namespace to_monado +{ +struct disconnect +{}; + +using packets = std::variant; +} // namespace to_monado + +extern std::optional> wivrn_ipc_socket_monado; + +std::optional receive_from_main(); +template +void send_to_main(T && packet) +{ + wivrn_ipc_socket_monado->send(std::forward(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);