From 6da658bcfb4f67abd6c697f2c222529a20ac10ca Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Fri, 16 Aug 2024 14:45:03 +0200 Subject: [PATCH] Allow using glib instead of sd-bus to get the pretty hostname --- CMakeLists.txt | 14 ++++++++- common/wivrn_config.h.in | 1 + flatpak/io.github.wivrn.wivrn.yml | 7 +++-- server/CMakeLists.txt | 4 +++ server/hostname.cpp | 52 +++++++++++++++++++++++++++++-- 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 729f0e03..17a143f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,8 @@ 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, required to show pretty hostname" 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) 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) @@ -140,6 +141,17 @@ if (WIVRN_BUILD_SERVER) find_package(Eigen3 REQUIRED) 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() endif() if (WIVRN_BUILD_CLIENT AND NOT ANDROID) diff --git a/common/wivrn_config.h.in b/common/wivrn_config.h.in index a152ba2c..d8c8f109 100644 --- a/common/wivrn_config.h.in +++ b/common/wivrn_config.h.in @@ -24,6 +24,7 @@ #cmakedefine WIVRN_USE_X264 #cmakedefine WIVRN_USE_SYSTEMD +#cmakedefine WIVRN_USE_GLIB #cmakedefine WIVRN_USE_PIPEWIRE #cmakedefine WIVRN_USE_PULSEAUDIO diff --git a/flatpak/io.github.wivrn.wivrn.yml b/flatpak/io.github.wivrn.wivrn.yml index ab4e195b..af071bd0 100644 --- a/flatpak/io.github.wivrn.wivrn.yml +++ b/flatpak/io.github.wivrn.wivrn.yml @@ -122,6 +122,7 @@ 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 @@ -194,10 +195,10 @@ finish-args: # Needs to talk to the network - --share=network # Announce WiVRn via avahi - # - --system-talk-name=org.freedesktop.Avahi + - --system-talk-name=org.freedesktop.Avahi # Allow getting the pretty hostname - # - --system-talk-name=org.freedesktop.hostname1 - - --socket=system-bus # workaround bug between sd-bus (https://github.com/systemd/systemd/issues/16610) and xdg-dbus-proxy (https://github.com/flatpak/xdg-dbus-proxy/issues/21) + - --system-talk-name=org.freedesktop.hostname1 + # - --socket=system-bus # workaround bug between sd-bus (https://github.com/systemd/systemd/issues/16610) and xdg-dbus-proxy (https://github.com/flatpak/xdg-dbus-proxy/issues/21) # Pipewire - --filesystem=xdg-run/pipewire-0 # Set the active OpenXR / OpenVR runtime diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 6693b9e5..1c2b7300 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -159,6 +159,10 @@ target_link_libraries( nlohmann_json::nlohmann_json ) +if(WIVRN_USE_GLIB) + target_link_libraries( wivrn-server PRIVATE PkgConfig::glib2) +endif() + configure_file(dist/wivrn.service.in wivrn.service) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/wivrn.service DESTINATION lib/systemd/user/) diff --git a/server/hostname.cpp b/server/hostname.cpp index e39cf06b..af149458 100644 --- a/server/hostname.cpp +++ b/server/hostname.cpp @@ -5,13 +5,61 @@ #include "util/u_logging.h" #include "wivrn_config.h" -#ifdef WIVRN_USE_SYSTEMD +#ifdef WIVRN_USE_GLIB +#include +#elif defined(WIVRN_USE_SYSTEMD) #include #endif std::string hostname() { -#ifdef WIVRN_USE_SYSTEMD +#ifdef WIVRN_USE_GLIB + GError * error = NULL; + GDBusConnection * con = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); + + if (!con) + { + U_LOG_W("Failed to connect to system bus: %s", error->message); + } + else + { + for (auto property: {"PrettyHostname", "StaticHostname", "Hostname"}) + { + GVariant * result = g_dbus_connection_call_sync(con, + "org.freedesktop.hostname1", + "/org/freedesktop/hostname1", + "org.freedesktop.DBus.Properties", + "Get", + g_variant_new("(ss)", "org.freedesktop.hostname1", property), + G_VARIANT_TYPE("(v)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (error) + continue; + + GVariant * property_value; + g_variant_get(result, "(v)", &property_value); + const char * hostname = g_variant_get_string(property_value, NULL); + + if (hostname && strcmp(hostname, "")) + { + std::string s = hostname; + g_variant_unref(property_value); + g_variant_unref(result); + g_object_unref(con); + return s; + } + + g_variant_unref(property_value); + g_variant_unref(result); + } + + g_object_unref(con); + } +#elif defined(WIVRN_USE_SYSTEMD) sd_bus * bus; if (sd_bus_default_system(&bus) < 0) {