diff --git a/common/utils/strings.cpp b/common/utils/strings.cpp index 8d1d3646..78221efd 100644 --- a/common/utils/strings.cpp +++ b/common/utils/strings.cpp @@ -19,21 +19,21 @@ #include "strings.h" -std::vector utils::split(const std::string & s, const std::string & sep) +std::vector utils::split(std::string_view s, std::string_view sep) { std::string::size_type i = 0; std::vector v; while (true) { - std::string::size_type j = s.find_first_of(sep, i); - if (j == std::string::npos) + auto j = s.find_first_of(sep, i); + if (j == std::string_view::npos) { - v.push_back(s.substr(i)); + v.emplace_back(s.substr(i)); return v; } - v.push_back(s.substr(i, j - i)); + v.emplace_back(s.substr(i, j - i)); i = j + 1; } } diff --git a/common/utils/strings.h b/common/utils/strings.h index 247039cf..9bc6ad6b 100644 --- a/common/utils/strings.h +++ b/common/utils/strings.h @@ -25,7 +25,7 @@ namespace utils { -std::vector split(const std::string & s, const std::string & sep = "\n"); +std::vector split(std::string_view s, std::string_view = "\n"); // https://www.cppstories.com/2021/heterogeneous-access-cpp20/ struct string_hash diff --git a/dashboard/CMakeLists.txt b/dashboard/CMakeLists.txt index 5bd3e8c9..92217d1e 100644 --- a/dashboard/CMakeLists.txt +++ b/dashboard/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(wivrn-dashboard dashboard_utils.cpp escape_string.cpp settings.cpp + steam.cpp adb.cpp apk_installer.cpp firewall.cpp diff --git a/dashboard/firewall.cpp b/dashboard/firewall.cpp index 39a78f86..a06ec49d 100644 --- a/dashboard/firewall.cpp +++ b/dashboard/firewall.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include diff --git a/dashboard/firewall.h b/dashboard/firewall.h index 7609a65c..188b16bf 100644 --- a/dashboard/firewall.h +++ b/dashboard/firewall.h @@ -19,7 +19,6 @@ #pragma once #include -#include #include class firewall : public QObject diff --git a/dashboard/qml/Main.qml b/dashboard/qml/Main.qml index a1f49d8c..324c0597 100644 --- a/dashboard/qml/Main.qml +++ b/dashboard/qml/Main.qml @@ -216,6 +216,28 @@ Kirigami.ApplicationWindow { visible: WivrnServer.openVRCompat.length == 0 && config.openvr == "" } + Kirigami.InlineMessage { + Layout.fillWidth: true + text: i18n("Steam is installed as a snap. Snaps are not compatible with WiVRn.") + type: Kirigami.MessageType.Warning + showCloseButton: true + visible: DashboardSettings.show_system_checks && Steam.snap + } + + Kirigami.InlineMessage { + Layout.fillWidth: true + text: i18n("Steam is installed as a flatpak but does not have sufficient permissions.") + type: Kirigami.MessageType.Warning + showCloseButton: true + visible: DashboardSettings.show_system_checks && Steam.flatpakNeedPerm + actions: [ + Kirigami.Action { + text: i18n("Fix it") + onTriggered: Steam.fixFlatpakPerm() + } + ] + } + Kirigami.InlineMessage { id: message_failed_to_start Layout.fillWidth: true diff --git a/dashboard/steam.cpp b/dashboard/steam.cpp new file mode 100644 index 00000000..e369dfd2 --- /dev/null +++ b/dashboard/steam.cpp @@ -0,0 +1,160 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * 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 "steam.h" + +#include "escape_sandbox.h" +#include "utils/flatpak.h" +#include "utils/ini.h" +#include "utils/strings.h" + +#include +#include +#include + +bool steam::snap() const +{ + if (wivrn::is_flatpak()) + { + auto test = escape_sandbox("test", "-f", "/snap/bin/steam"); + test->setProcessChannelMode(QProcess::MergedChannels); + test->start(); + if (test->waitForFinished()) + return test->exitCode() == 0; + return false; + } + + return std::filesystem::exists("/snap/bin/steam"); +} + +static QString get_flatpak_scope() +{ + for (const QString scope: {"--user", "--system"}) + { + auto proc = escape_sandbox("flatpak", scope, "info", "com.valvesoftware.Steam"); + proc->setProcessChannelMode(QProcess::MergedChannels); + proc->start(); + if (proc->waitForFinished() and proc->exitCode() == 0) + return scope; + } + return ""; +} + +static std::filesystem::path find_dir(const std::filesystem::path & d, const std::filesystem::path & needle) +{ + for (auto copy = d; not copy.empty(); copy = copy.parent_path()) + { + if (copy.filename() == needle) + return copy; + } + return d; +} + +static std::optional wivrn_app_path() +{ + if (auto app_path = wivrn::flatpak_key(wivrn::flatpak::section::instance, "app-path")) + { + if (app_path->starts_with("/var")) + return find_dir(*app_path, "io.github.wivrn.wivrn"); + // assume it's in home + return "xdg-data/flatpak/app/io.github.wivrn.wivrn"; + } + return {}; +} + +bool steam::flatpakNeedPerm() const +{ + auto scope = get_flatpak_scope(); + if (scope.isEmpty()) + return false; + auto proc = escape_sandbox("flatpak", scope, "override", "--show", "com.valvesoftware.Steam"); + proc->setProcessChannelMode(QProcess::SeparateChannels); + proc->start(); + if (not proc->waitForFinished()) + { + qWarning() << "timeout trying to get Steam flatpak overrides"; + return false; + } + if (proc->exitCode()) + { + qWarning() << "failed to get Steam flatpak overrides"; + return false; + } + std::stringstream overrides(proc->readAllStandardOutput().toStdString()); + wivrn::ini ini(overrides); + if (auto filesystems = ini.get_optional("Context", "filesystems")) + { + auto items = utils::split(*filesystems, ";"); + for (auto & item: items) + item = item.substr(0, item.find_last_of(':')); + for (const auto & path: {"xdg-run/wivrn", "xdg-config/openxr", "xdg-config/openvr"}) + { + if (not std::ranges::contains(items, path)) + return true; + } + if (auto path = wivrn_app_path()) + { + if (not std::ranges::contains(items, path)) + return true; + } + return false; + } + return true; +} + +void steam::fixFlatpakPerm() +{ + auto scope = get_flatpak_scope(); + if (scope.isEmpty()) + return; + std::unique_ptr proc; + if (scope == "--system") + proc = escape_sandbox( + "pkexec", + "flatpak", + "--system", + "override", + "--filesystem=xdg-run/wivrn:ro", + "--filesystem=xdg-config/openxr:ro", + "--filesystem=xdg-config/openvr:ro", + "--filesystem=" + wivrn_app_path().value_or("xdg-run/wivrn") + ":ro", + "com.valvesoftware.Steam"); + else + proc = escape_sandbox( + "flatpak", + scope, + "override", + "--filesystem=xdg-run/wivrn:ro", + "--filesystem=xdg-config/openxr:ro", + "--filesystem=xdg-config/openvr:ro", + "--filesystem=" + wivrn_app_path().value_or("xdg-run/wivrn") + ":ro", + "com.valvesoftware.Steam"); + proc->setProcessChannelMode(QProcess::SeparateChannels); + proc->start(); + if (not proc->waitForFinished()) + { + qWarning() << "timeout trying to set Steam flatpak overrides"; + return; + } + if (proc->exitCode()) + { + qWarning() << "failed to set Steam flatpak overrides"; + return; + } + flatpakNeedPermChanged(); +} diff --git a/dashboard/steam.h b/dashboard/steam.h new file mode 100644 index 00000000..aa4719cb --- /dev/null +++ b/dashboard/steam.h @@ -0,0 +1,42 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * 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 + +#include +#include + +class steam : public QObject +{ +public: + Q_OBJECT + QML_NAMED_ELEMENT(Steam) + QML_SINGLETON + + Q_PROPERTY(bool snap READ snap CONSTANT) + Q_PROPERTY(bool flatpakNeedPerm READ flatpakNeedPerm NOTIFY flatpakNeedPermChanged) + +public: + bool snap() const; + bool flatpakNeedPerm() const; + + Q_INVOKABLE void fixFlatpakPerm(); + +Q_SIGNALS: + void flatpakNeedPermChanged(); +};