diff --git a/CMakeLists.txt b/CMakeLists.txt index b1b1eb68..4240b9c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ option(WIVRN_BUILD_SERVER_LIBRARY "Build OpenXR library for WiVRn server" ${WIVR option(WIVRN_BUILD_DASHBOARD "Build WiVRn dashboard" OFF) option(WIVRN_BUILD_WIVRNCTL "Build wivrnctl" ON) option(WIVRN_BUILD_DISSECTOR "Build Wireshark dissector" OFF) +option(WIVRN_BUILD_TEST "Build test binaries" OFF) option(WIVRN_WERROR "Treat warnings as errors" OFF) option(WIVRN_USE_NVENC "Enable nvenc (Nvidia) hardware encoder" ON) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 8726e359..a606adda 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -23,19 +23,17 @@ endif() configure_file(wivrn_config.h.in wivrn_config.h) -add_library(wivrn-common-base STATIC - utils/xdg_base_directory.cpp - utils/flatpak.cpp +add_library(wivrn-common-base STATIC EXCLUDE_FROM_ALL utils/strings.cpp + utils/xdg_base_directory.cpp ${CMAKE_CURRENT_BINARY_DIR}/version.cpp ) -add_library(wivrn-common STATIC +add_library(wivrn-common STATIC EXCLUDE_FROM_ALL crypto.cpp smp.cpp secrets.cpp wivrn_sockets.cpp - utils/xdg_base_directory.cpp utils/strings.cpp vk/allocation.cpp vk/error_category.cpp @@ -43,6 +41,22 @@ add_library(wivrn-common STATIC vk/vk_mem_alloc.cpp ) +if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER) + add_library(wivrn-common-server STATIC EXCLUDE_FROM_ALL + utils/flatpak.cpp + application.cpp + ) + target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PUBLIC wivrn-common-base) + + if (WIVRN_BUILD_TEST) + add_executable(list-apps + test_application.cpp + ) + + target_link_libraries(list-apps wivrn-common-server) + endif() +endif() + if(ANDROID) # The Vulkan headers in Android do not include the C++ headers, download them # Minimum version is 1.3.256 @@ -113,5 +127,4 @@ endif() target_include_directories(wivrn-common-base PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(wivrn-common PUBLIC Boost::pfr OpenSSL::Crypto wivrn-external wivrn-common-base) -target_compile_features(wivrn-common PRIVATE cxx_std_20) target_compile_definitions(wivrn-common PUBLIC VULKAN_HPP_NO_STRUCT_CONSTRUCTORS) diff --git a/common/application.cpp b/common/application.cpp new file mode 100644 index 00000000..ccd888e5 --- /dev/null +++ b/common/application.cpp @@ -0,0 +1,313 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * 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 "application.h" + +#include "utils/flatpak.h" +#include "utils/xdg_base_directory.h" + +#include +#include +#include +#include + +namespace wivrn +{ + +namespace +{ + +std::string read_file(const std::filesystem::path & path) +{ + std::ifstream f(path); + std::istreambuf_iterator begin{f}, end; + + return {begin, end}; +} + +std::filesystem::path home() +{ + auto home = std::getenv("HOME"); + return home ? home : ""; +} + +std::pair read_vr_manifest() +{ + // system Steam + auto manifest = read_file(xdg_data_home() / "Steam/config/steamapps.vrmanifest"); + if (not manifest.empty()) + return {"steam", manifest}; + + auto h = home(); + + // system Steam (accessed from flatpak) + manifest = read_file(h / ".local/share/Steam/config/steamapps.vrmanifest"); + if (not manifest.empty()) + return {"steam", manifest}; + + // Flatpak Steam + manifest = read_file(h / ".var/app/com.valvesoftware.Steam/.steam/steam/config/steamapps.vrmanifest"); + if (not manifest.empty()) + return {"flatpak run com.valvesoftware.Steam", manifest}; + return {}; +} + +void read_steam_vr_apps(std::unordered_map & res) +{ + auto [command, manifest] = read_vr_manifest(); + if (manifest.empty()) + return; + nlohmann::json json = nlohmann::json::parse(manifest); + + for (auto & i: json["applications"]) + { + try + { + application app; + + std::string app_key = i["app_key"]; + for (auto [locale, items]: i["strings"].items()) + { + if (auto it = items.find("name"); it != items.end()) + app.name[locale] = *it; + } + + if (not app.name.contains("")) + { + auto it = app.name.find("en_us"); + if (it == app.name.end()) + it = app.name.begin(); + if (it != app.name.end()) + app.name[""] = it->second; + } + + if (i["launch_type"] == "url") + { + app.exec = command + " " + (std::string)i["url"]; + } + else if (i["launch_type"] == "binary") + { + const char prefix[] = "steam.app."; + if (app_key.starts_with(prefix)) + { + // ¯\_(ツ)_/¯ + uint64_t appkey = stoll(app_key.substr(strlen(prefix))); + app.exec = command + " steam://rungameid/" + std::to_string((appkey << 32) + 0x2000000); + } + } + + if (not app.exec.empty()) + res[app_key] = std::move(app); + } + catch (std::exception & e) + { + std::cerr << "Failed to parse Steam VR manifest: " << e.what() << std::endl; + } + } +} + +std::string unescape(std::string_view in) +{ + std::string out; + out.reserve(in.size()); + for (auto c = in.begin(); c != in.end(); ++c) + { + if (*c == '\\') + { + ++c; + if (c == in.end()) + break; + switch (*c) + { + case 's': + out += ' '; + break; + case 'n': + out += '\n'; + break; + case 't': + out += '\t'; + break; + case 'r': + out += '\r'; + break; + default: + out += *c; + } + } + else + { + out += *c; + } + } + return out; +} + +struct key_value +{ + std::string_view key; + std::string_view locale; + std::string_view value; + key_value(std::string_view l) + { + auto pos = l.find_first_of("[ ="); + if (pos == std::string_view::npos) + return; + key = l.substr(0, pos); + l = l.substr(pos); + if (l[0] == '[') + { + pos = l.find(']'); + if (pos == std::string_view::npos) + { + key = std::string_view(); + return; + } + locale = l.substr(1, pos - 1); + l = l.substr(pos + 1); + } + pos = l.find('='); + if (pos == std::string_view::npos) + { + key = std::string_view(); + locale = std::string_view(); + return; + } + l = l.substr(pos + 1); + pos = l.find_first_not_of(" "); + if (pos == std::string_view::npos) + { + key = std::string_view(); + locale = std::string_view(); + return; + } + value = l.substr(pos); + } +}; + +bool contains(std::string_view entries, std::string_view val, char sep = ';') +{ + for (auto bounds: entries | std::views::split(sep)) + { + if (std::string_view(bounds.begin(), bounds.end()) == val) + return true; + } + return false; +} + +std::optional do_desktop_entry(const std::filesystem::path & filename) +{ + auto data = read_file(filename); + std::optional res; + + // Most apps are not VR, don't bother parsing the rest + bool vr = false; + for (auto bounds: data | std::views::split('\n')) + { + std::string_view line(bounds.begin(), bounds.end()); + key_value item(line); + if (item.key == "Categories") + vr = contains(item.value, "X-WiVRn-VR"); + } + if (not vr) + return res; + + res.emplace(); + for (auto bounds: data | std::views::split('\n')) + { + std::string_view line(bounds.begin(), bounds.end()); + if (line.empty()) + continue; + if (line.starts_with("#")) + continue; + + // Only process the main section + if (line.starts_with("[") and line != "[Desktop Entry]") + break; + + key_value item(line); + if (item.key == "Type" and item.value != "Application") + { + res.reset(); + return res; + } + if (item.key == "Name") + res->name.emplace(item.locale, unescape(item.value)); + if (item.key == "Exec") + res->exec = unescape(item.value); + if (item.key == "Path") + res->path = unescape(item.value); + } + + if (res->exec.empty() or not res->name.contains("")) + res.reset(); + + return res; +} + +void do_data_dir(std::filesystem::path dir, std::unordered_map & res) +{ + dir = dir / "applications"; + if (not std::filesystem::is_directory(dir)) + return; + for (const auto & entry: std::filesystem::recursive_directory_iterator(dir)) + { + if (entry.is_directory()) + continue; + + if (entry.path().extension() != ".desktop") + continue; + + // https://specifications.freedesktop.org/desktop-entry-spec/latest/file-naming.html#desktop-file-id + auto file_id = entry.path() + .lexically_relative(dir) + .replace_extension("") + .string(); + std::ranges::replace(file_id, '/', '-'); + + if (res.contains(file_id)) + continue; + + auto app = do_desktop_entry(entry.path()); + if (app) + res.emplace(std::move(file_id), std::move(*app)); + } +} +} // namespace + +std::unordered_map list_applications(bool include_steam) +{ + std::unordered_map res; + + if (include_steam) + read_steam_vr_apps(res); + + do_data_dir(xdg_data_home(), res); + for (auto && dir: xdg_data_dirs()) + do_data_dir(std::move(dir), res); + if (wivrn::is_flatpak()) + { + // Try to guess host data dirs + do_data_dir("/run/host/usr/share", res); + } + + return res; +} + +} // namespace wivrn diff --git a/common/application.h b/common/application.h new file mode 100644 index 00000000..33d97e8f --- /dev/null +++ b/common/application.h @@ -0,0 +1,37 @@ +/* + * 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 +#include + +namespace wivrn +{ + +struct application +{ + // localised names, with empty locale for default + std::unordered_map name; + std::string exec; + std::optional path; +}; + +std::unordered_map list_applications(bool include_steam = true); +} // namespace wivrn diff --git a/common/test_application.cpp b/common/test_application.cpp new file mode 100644 index 00000000..05e49335 --- /dev/null +++ b/common/test_application.cpp @@ -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 . + */ +#include "application.h" + +#include + +int main() +{ + auto apps = wivrn::list_applications(); + + std::cout << "Parsed " << apps.size() << " applications" << std::endl; + for (const auto & [k, app]: apps) + { + std::cout << "======================" << std::endl; + std::cout << k << std::endl; + for (const auto & [locale, name]: app.name) + { + std::cout << "Name"; + if (not locale.empty()) + std::cout << "[" << locale << "]"; + std::cout << "=" << name << std::endl; + } + std::cout << "Exec=" << app.exec << std::endl; + if (app.path) + std::cout << "Path=" << *app.path << std::endl; + } +} diff --git a/common/utils/flatpak.cpp b/common/utils/flatpak.cpp index 7a41fae6..ef8e2a6a 100644 --- a/common/utils/flatpak.cpp +++ b/common/utils/flatpak.cpp @@ -23,10 +23,11 @@ #include static const std::filesystem::path info_path = "/.flatpak-info"; +static const bool is_flatpak_v = std::filesystem::exists(info_path); bool wivrn::is_flatpak() { - return std::filesystem::exists(info_path); + return is_flatpak_v; } std::optional wivrn::flatpak_key(std::string section, std::string key) diff --git a/common/utils/strings.cpp b/common/utils/strings.cpp index 3f42671d..8d1d3646 100644 --- a/common/utils/strings.cpp +++ b/common/utils/strings.cpp @@ -19,13 +19,6 @@ #include "strings.h" -std::string utils::trim(std::string input) -{ - input.resize(input.find_last_not_of("\n ") + 1); - - return input; -} - std::vector utils::split(const std::string & s, const std::string & sep) { std::string::size_type i = 0; diff --git a/common/utils/strings.h b/common/utils/strings.h index f9aba057..e93b9fc4 100644 --- a/common/utils/strings.h +++ b/common/utils/strings.h @@ -19,32 +19,10 @@ #pragma once -#include #include #include namespace utils { -std::string trim(std::string input); - -template -std::string join(const std::vector & vec, const std::string & sep = ", ") -{ - std::stringstream s; - - if (!vec.empty()) - { - s << vec[0]; - - for (size_t i = 1, n = vec.size(); i < n; i++) - { - s << sep << vec[i]; - } - } - - return s.str(); -} - std::vector split(const std::string & s, const std::string & sep = "\n"); - } // namespace utils diff --git a/dashboard/CMakeLists.txt b/dashboard/CMakeLists.txt index bdb0bb9a..b224366e 100644 --- a/dashboard/CMakeLists.txt +++ b/dashboard/CMakeLists.txt @@ -10,7 +10,7 @@ add_executable(wivrn-dashboard main.cpp wivrn_server.cpp wivrn_qdbus_types.cpp - steam_apps.cpp + apps.cpp escape_string.cpp settings.cpp rectangle_partitionner.cpp @@ -68,6 +68,7 @@ target_link_libraries(wivrn-dashboard QCoro::Network QCoro::Qml wivrn-common-base + wivrn-common-server wivrn-external ) diff --git a/dashboard/apps.cpp b/dashboard/apps.cpp new file mode 100644 index 00000000..2ab62820 --- /dev/null +++ b/dashboard/apps.cpp @@ -0,0 +1,43 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * 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 "apps.h" + +#include "application.h" + +#include + +Apps::Apps(QObject * parent) +{ + auto apps = wivrn::list_applications(); + + for (const auto & [id, app]: apps) + { + m_apps.push_back( + vrApp( + QString::fromStdString(app.name.at("")), + QString::fromStdString(app.exec))); + } + + std::ranges::sort(m_apps, [](const vrApp & a, const vrApp & b) { + return a.name() < b.name(); + }); +} + +#include "moc_apps.cpp" diff --git a/dashboard/steam_apps.h b/dashboard/apps.h similarity index 63% rename from dashboard/steam_apps.h rename to dashboard/apps.h index 91e6b9ee..d62bcdaa 100644 --- a/dashboard/steam_apps.h +++ b/dashboard/apps.h @@ -1,6 +1,7 @@ /* * WiVRn VR streaming * Copyright (C) 2024 Guillaume Meunier + * 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 @@ -21,37 +22,30 @@ #include #include -class steamApp +class vrApp { Q_GADGET - QML_VALUE_TYPE(steamApp) + QML_VALUE_TYPE(vrApp) Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath) Q_PROPERTY(QString command READ command WRITE setCommand) QString m_name; - QString m_imagePath; QString m_command; public: - steamApp() = default; - steamApp(const steamApp &) = default; - steamApp(steamApp &&) = default; - steamApp & operator=(const steamApp &) = default; - steamApp & operator=(steamApp &&) = default; - steamApp(QString name, QString imagePath, QString command) : - m_name(name), m_imagePath(imagePath), m_command(command) {} + vrApp() = default; + vrApp(const vrApp &) = default; + vrApp(vrApp &&) = default; + vrApp & operator=(const vrApp &) = default; + vrApp & operator=(vrApp &&) = default; + vrApp(QString name, QString command) : + m_name(name), m_command(command) {} QString name() const { return m_name; } - QString imagePath() const - { - return m_imagePath; - } - QString command() const { return m_command; @@ -62,11 +56,6 @@ public: m_name = value; } - void setImagePath(QString value) - { - m_imagePath = value; - } - void setCommand(QString value) { m_command = value; @@ -77,30 +66,25 @@ public: m_name = QString::fromStdString(value); } - void setImagePath(const std::string & value) - { - m_imagePath = QString::fromStdString(value); - } - void setCommand(const std::string & value) { m_command = QString::fromStdString(value); } }; -class SteamApps : public QObject +class Apps : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON - Q_PROPERTY(QList apps READ apps) - QList m_apps; + Q_PROPERTY(QList apps READ apps) + QList m_apps; public: - SteamApps(QObject * parent = nullptr); + Apps(QObject * parent = nullptr); - const QList & apps() const + const QList & apps() const { return m_apps; } diff --git a/dashboard/qml/SelectGame.qml b/dashboard/qml/SelectGame.qml index 36807b47..f3ad522d 100644 --- a/dashboard/qml/SelectGame.qml +++ b/dashboard/qml/SelectGame.qml @@ -12,24 +12,21 @@ ColumnLayout { Component.onCompleted: { apps.append({ "name": i18nc("don't start an application automatically", "None"), - "image_path": "", "command": "", "is_custom": false }); apps.append({ "name": i18nc("choose a custom application to start", "Custom"), - "image_path": "", "command": "", "is_custom": true }); - var steam_apps = SteamApps.apps; - for (var i = 0; i < steam_apps.length; i++) { + var vr_apps = Apps.apps; + for (var i = 0; i < vr_apps.length; i++) { apps.append({ - "name": steam_apps[i].name, - "image_path": steam_apps[i].imagePath, - "command": steam_apps[i].command, + "name": vr_apps[i].name, + "command": vr_apps[i].command, "is_custom": false }); } diff --git a/dashboard/steam_apps.cpp b/dashboard/steam_apps.cpp deleted file mode 100644 index 0fd18ed4..00000000 --- a/dashboard/steam_apps.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 "steam_apps.h" -#include "escape_string.h" -#include "utils/flatpak.h" -#include -#include -#include -#include -#include - -namespace -{ -std::string read_vr_manifest() -{ - const char * home = getenv("HOME"); - - std::filesystem::path vrmanifest = std::string{home ? home : ""} + "/.steam/steam/config/steamapps.vrmanifest"; - - if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk") - { - QProcess flatpak_spawn; - flatpak_spawn.start("flatpak-spawn", {"--host", "cat", QString::fromStdString(vrmanifest)}); - flatpak_spawn.waitForFinished(); - return flatpak_spawn.readAllStandardOutput().toStdString(); - } - else - { - std::ifstream f(vrmanifest); - std::istreambuf_iterator begin{f}, end; - - return {begin, end}; - } -} -} // namespace - -SteamApps::SteamApps(QObject * parent) -{ - auto manifest = read_vr_manifest(); - if (manifest.empty()) - { - return; - } - - nlohmann::json json; - try - { - json = nlohmann::json::parse(manifest); - } - catch (std::exception & e) - { - qWarning() << "Error parsing VR manifest: " << e.what(); - return; - } - - for (auto & i: json["applications"]) - { - try - { - steamApp app; - - app.setImagePath(i.value("image_path", "")); - app.setName(i["strings"]["en_us"]["name"]); - - if (i["launch_type"] == "url") - { - app.setCommand("steam " + (std::string)i["url"]); - } - else if (i["launch_type"] == "binary") - { - std::string app_key = i["app_key"]; - - const char prefix[] = "steam.app."; - if (app_key.starts_with(prefix)) - { - // ¯\_(ツ)_/¯ - uint64_t appkey = stoll(app_key.substr(strlen(prefix))); - app.setCommand("steam steam://rungameid/" + std::to_string((appkey << 32) + 0x2000000)); - } - } - - if (app.command() != "") - m_apps.push_back(app); - } - catch (std::exception & e) - { - qWarning() << "Error adding app from VR manifest: " << e.what(); - } - } - - std::ranges::sort(m_apps, [](const steamApp & a, const steamApp & b) { - return a.name() < b.name(); - }); -} - -#include "moc_steam_apps.cpp" diff --git a/flatpak/io.github.wivrn.wivrn.yml.in b/flatpak/io.github.wivrn.wivrn.yml.in index b50c2f7e..2d439db1 100644 --- a/flatpak/io.github.wivrn.wivrn.yml.in +++ b/flatpak/io.github.wivrn.wivrn.yml.in @@ -23,6 +23,11 @@ finish-args: - --system-talk-name=org.freedesktop.Avahi # Allow getting the pretty hostname - --system-talk-name=org.freedesktop.hostname1 + # List applications from $XDG_DATA_DIRS + - --filesystem=host-os:ro + - --filesystem=xdg-data:ro + # List flatpak Steam games + - --filesystem=~/.var/app/com.valvesoftware.Steam:ro # Pipewire - --filesystem=xdg-run/pipewire-0 # Set the active OpenXR / OpenVR runtime diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 5308d12a..99d503c1 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -219,6 +219,7 @@ if (WIVRN_BUILD_SERVER) PkgConfig::AVAHI Eigen3::Eigen wivrn-common + wivrn-common-server wivrn-external nlohmann_json::nlohmann_json )