From 868fc55f3790f4a81e4b2706cfd3bb4a7ff542c9 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Fri, 1 Nov 2024 16:39:51 +0100 Subject: [PATCH] Inhibit sleeping while a session is active --- flatpak/io.github.wivrn.wivrn.yml.in | 2 + server/CMakeLists.txt | 1 + server/main.cpp | 5 ++ server/sleep_inhibitor.cpp | 97 ++++++++++++++++++++++++++++ server/sleep_inhibitor.h | 28 ++++++++ 5 files changed, 133 insertions(+) create mode 100644 server/sleep_inhibitor.cpp create mode 100644 server/sleep_inhibitor.h diff --git a/flatpak/io.github.wivrn.wivrn.yml.in b/flatpak/io.github.wivrn.wivrn.yml.in index 13f2a83b..1db2ffbc 100644 --- a/flatpak/io.github.wivrn.wivrn.yml.in +++ b/flatpak/io.github.wivrn.wivrn.yml.in @@ -31,6 +31,8 @@ finish-args: - --talk-name=org.kde.StatusNotifierWatcher # Run processes outside of sandbox - --talk-name=org.freedesktop.Flatpak + # Inhibit sleeping while a session is active + - --system-talk-name=org.freedesktop.login1 cleanup: - /lib/pkgconfig diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 78a60557..c80d3bb4 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -65,6 +65,7 @@ add_executable(wivrn-server avahi_publisher.cpp hostname.cpp main.cpp + sleep_inhibitor.cpp start_application.cpp target_instance_wivrn.cpp wivrn_ipc.cpp diff --git a/server/main.cpp b/server/main.cpp index 13415ad1..0890fa54 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -6,6 +6,7 @@ */ #include "openxr/openxr.h" +#include "sleep_inhibitor.h" #include "util/u_trace_marker.h" #include "active_runtime.h" @@ -198,6 +199,7 @@ WivrnServer * dbus_server; */ std::optional runtime_setter; std::optional publisher; +std::optional inhibitor; gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data); void stop_listening(); @@ -446,16 +448,19 @@ gboolean control_received(gint fd, GIOCondition condition, gpointer user_data) if (std::holds_alternative(*packet)) { on_headset_info_packet(std::get(*packet)); + inhibitor.emplace(); wivrn_server_set_headset_connected(dbus_server, true); } else if (std::holds_alternative(*packet)) { stop_publishing(); + inhibitor.emplace(); wivrn_server_set_headset_connected(dbus_server, true); } else if (std::holds_alternative(*packet)) { start_publishing(); + inhibitor.reset(); wivrn_server_set_headset_connected(dbus_server, false); } } diff --git a/server/sleep_inhibitor.cpp b/server/sleep_inhibitor.cpp new file mode 100644 index 00000000..159378d7 --- /dev/null +++ b/server/sleep_inhibitor.cpp @@ -0,0 +1,97 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "sleep_inhibitor.h" +#include +#include +#include +#include + +sleep_inhibitor::sleep_inhibitor() +{ + std::cerr << "Inhibiting sleep" << std::endl; + + GError * error = nullptr; + GDBusProxy * proxy = g_dbus_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + nullptr, + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + nullptr, + &error); + + if (error) + { + std::cerr << "Cannot create DBus proxy for org.freedesktop.login1: " << error->message << std::endl; + g_error_free(error); + return; + } + + GUnixFDList * fd_list; + + GVariant * output = g_dbus_proxy_call_with_unix_fd_list_sync( + proxy, + "Inhibit", + g_variant_new( + "(ssss)", + "sleep:idle", // What + "WiVRn", // Who + "A WiVRn session is active", // Why + "block"), // Mode + G_DBUS_CALL_FLAGS_NONE, + -1, // timeout_msec + nullptr, // fd_list + &fd_list, // out_fd_list + nullptr, // cancellable + &error); + + if (error) + { + std::cerr << "Cannot inhibit sleep: " << error->message << std::endl; + g_error_free(error); + return; + } + + std::cerr << "Received variant type " << g_variant_get_type_string(output) << std::endl; + + int fd_index; + g_variant_get(output, "(h)", &fd_index); + + int fd_count; + int * fds = g_unix_fd_list_steal_fds(fd_list, &fd_count); + + if (fd_index < fd_count) + fd = fds[fd_index]; + + g_free(fds); + + std::cerr << "File descriptor " << fd << std::endl; + + g_variant_unref(output); + g_object_unref(fd_list); +} + +sleep_inhibitor::~sleep_inhibitor() +{ + std::cerr << "Uninhibiting sleep" << std::endl; + + if (fd >= 0) + close(fd); +} diff --git a/server/sleep_inhibitor.h b/server/sleep_inhibitor.h new file mode 100644 index 00000000..37c8c683 --- /dev/null +++ b/server/sleep_inhibitor.h @@ -0,0 +1,28 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +class sleep_inhibitor +{ + int fd = -1; + +public: + sleep_inhibitor(); + ~sleep_inhibitor(); +};