diff --git a/CMakeLists.txt b/CMakeLists.txt index de67ae79..208fad2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,9 +97,9 @@ include(VulkanVersion) if (WIVRN_USE_SYSTEM_BOOST AND NEED_COMMON) if (WIVRN_BUILD_DISSECTOR) - find_package(Boost 1.84.0 REQUIRED COMPONENTS locale url) + find_package(Boost 1.84.0 REQUIRED COMPONENTS locale url iostreams) else() - find_package(Boost 1.75.0 REQUIRED COMPONENTS locale url) + find_package(Boost 1.75.0 REQUIRED COMPONENTS locale url iostreams) endif() endif() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index b66bcb71..e4e85ae5 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,7 +1,7 @@ if (WIVRN_USE_SYSTEM_BOOST) add_library(Boost::pfr ALIAS Boost::headers) else() - set(BOOST_INCLUDE_LIBRARIES locale url pfr) + set(BOOST_INCLUDE_LIBRARIES locale url pfr iostreams) FetchContent_MakeAvailable(boost) endif() @@ -40,14 +40,17 @@ add_library(wivrn-common STATIC EXCLUDE_FROM_ALL if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER) add_library(wivrn-common-server STATIC EXCLUDE_FROM_ALL + application.cpp utils/flatpak.cpp - utils/xdg_icon_lookup.cpp - utils/steam_info.cpp utils/load_icon.cpp utils/ini.cpp - application.cpp + utils/steam_info.cpp + utils/vdf.cpp + utils/xdg_icon_lookup.cpp ) - target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 LibArchive::LibArchive PUBLIC wivrn-common-base) + target_link_libraries(wivrn-common-server + PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 LibArchive::LibArchive Boost::iostreams + PUBLIC wivrn-common-base) if (WIVRN_BUILD_TEST) add_executable(list-apps @@ -55,6 +58,10 @@ if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER) ) target_link_libraries(list-apps wivrn-common-server) + + add_executable(vdf utils/vdf.cpp) + target_compile_definitions(vdf PRIVATE WIVRN_BUILD_TEST) + target_link_libraries(vdf Boost::iostreams) endif() endif() diff --git a/common/application.cpp b/common/application.cpp index 5cc89131..065c7a0f 100644 --- a/common/application.cpp +++ b/common/application.cpp @@ -26,7 +26,6 @@ #include #include -#include #include namespace wivrn @@ -42,175 +41,6 @@ std::string read_file(const std::filesystem::path & path) return {begin, end}; } -std::filesystem::path home() -{ - auto home = std::getenv("HOME"); - return home ? home : ""; -} - -std::pair> find_steam() -{ - auto h = home(); - // Flatpak Steam - if (std::filesystem::exists(h / ".var/app/com.valvesoftware.Steam/.steam/steam")) - return {"flatpak run com.valvesoftware.Steam", h / ".var/app/com.valvesoftware.Steam/.steam/steam"}; - - // Debian Steam (accessed from flatpak) - if (std::filesystem::exists(h / ".steam/debian-installation")) - return {"steam", h / ".steam/debian-installation"}; - - // system Steam - if (std::filesystem::exists(xdg_data_home() / "Steam")) - return {"steam", xdg_data_home() / "Steam"}; - - // system Steam (accessed from flatpak) - if (std::filesystem::exists(h / ".local/share/Steam")) - return {"steam", h / ".local/share/Steam"}; - - return {}; -} - -std::string read_vr_manifest(const std::filesystem::path & root) -{ - return read_file(root / "config/steamapps.vrmanifest"); -} - -std::unordered_map safe_read_steam_icons(const std::filesystem::path & root) -{ - try - { - return read_steam_icons(root / "appcache/appinfo.vdf"); - } - catch (...) - {} - - return {}; -} - -std::optional find_steam_icon(const std::filesystem::path & root, int app_id, const std::unordered_map & info) -{ - auto icon = info.find(app_id); - if (icon == info.end()) - return std::nullopt; - - try - { - auto icon_path = root / "steam/games" / (icon->second.clienticon + ".ico"); - - if (std::filesystem::exists(icon_path)) - return icon_path; - } - catch (...) - { - } - - try - { - auto icon_path = root / "steam/games" / (icon->second.linuxclienticon + ".zip"); - if (std::filesystem::exists(icon_path)) - return icon_path; - } - catch (...) - { - } - - return std::nullopt; -} - -void read_steam_vr_apps(std::unordered_map & res) -{ - auto [command, root] = find_steam(); - if (not root) - return; - - auto manifest = read_vr_manifest(*root); - auto info = safe_read_steam_icons(*root); - - if (manifest.empty()) - return; - nlohmann::json json = nlohmann::json::parse(manifest); - - for (auto & i: json["applications"]) - { - try - { - application app; - - if (i["launch_type"] == "url") - app.exec = command + " " + (std::string)i["url"]; - else - continue; - - 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; - } - - try - { - const char prefix[] = "steam.app."; - if (app_key.starts_with(prefix)) - { - uint32_t app_id = stoll(app_key.substr(strlen(prefix))); - app.icon_path = find_steam_icon(*root, app_id, info); - } - } - catch (...) - { - } - - 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; - } - } - - auto userid = guess_steam_userid(*root); - - auto do_shortcuts = [&](const std::filesystem::path & shortcuts_vdf) { - if (std::filesystem::exists(shortcuts_vdf)) - { - try - { - for (auto && item: read_steam_shortcuts(shortcuts_vdf)) - { - res[std::to_string(item.appid)] = { - .name = {{"", std::move(item.name)}}, - // ¯\_(ツ)_/¯ - .exec = command + " steam://rungameid/" + std::to_string((uint64_t(item.appid) << 32) + 0x2000000), - .icon_path = std::move(item.icon), - }; - } - } - catch (std::exception & e) - { - std::cerr << "Failed to parse Steam shortcuts file " << shortcuts_vdf << ": " << e.what() << std::endl; - } - } - }; - if (userid and std::filesystem::exists(*root / "userdata" / std::to_string(*userid))) - do_shortcuts(*root / "userdata" / std::to_string(*userid) / "config/shortcuts.vdf"); - else - { - for (auto const & entry: std::filesystem::directory_iterator{*root / "userdata"}) - do_shortcuts(entry.path() / "config/shortcuts.vdf"); - } -} - std::string unescape(std::string_view in) { std::string out; @@ -398,8 +228,19 @@ std::unordered_map list_applications() { std::unordered_map res; - read_steam_vr_apps(res); - + for (auto & steam: steam::find_installations()) + { + auto cmd = steam.get_steam_command() + " "; + for (auto && app: steam.list_applications()) + { + res.emplace(std::to_string(app.appid), + application{ + .name = std::move(app.name), + .exec = cmd + app.url, + .icon_path = steam.get_icon(app.appid), + }); + } + } do_data_dir(xdg_data_home(), res); for (auto && dir: xdg_data_dirs()) diff --git a/common/utils/steam_info.cpp b/common/utils/steam_info.cpp index f54c5420..808db459 100644 --- a/common/utils/steam_info.cpp +++ b/common/utils/steam_info.cpp @@ -1,6 +1,7 @@ /* * WiVRn VR streaming * Copyright (C) 2025 Guillaume Meunier + * Copyright (C) 2026 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 @@ -17,10 +18,15 @@ */ #include "steam_info.h" +#include "vdf.h" +#include "xdg_base_directory.h" #include +#include #include +#include #include #include +#include namespace { @@ -128,9 +134,49 @@ void read_dict(binary_reader & in, T & visitor, const std::vector * } } } + +std::filesystem::path home() +{ + auto home = std::getenv("HOME"); + return home ? home : ""; +} + +std::optional guess_steam_userid(const std::filesystem::path & root) +{ + auto localconfig = root / "config/loginusers.vdf"; + if (not std::filesystem::exists(localconfig)) + return {}; + + try + { + wivrn::vdf::root loginusers(localconfig); + if (loginusers.key != "users") + return {}; + + std::optional res; + for (const auto & [userid, entries]: std::get>(loginusers.value)) + { + uint64_t v; + if (std::from_chars(userid.data.begin(), userid.data.end(), v).ec != std::errc{}) + continue; + res = v; // truncate to 32 bits + for (const auto & [key, value]: std::get>(entries)) + { + if (key == "MostRecent" and std::get(value) == "1") + return res; + } + } + return res; + } + catch (std::exception & e) + { + return {}; + } +} + } // namespace -std::unordered_map wivrn::read_steam_icons(std::filesystem::path path) +void read_steam_icons(std::filesystem::path path, std::unordered_map & icons) { binary_reader in{std::ifstream{path}}; @@ -150,11 +196,9 @@ std::unordered_map wivrn::read_steam_icons(std::fil string_table_ptr = &string_table; } - std::unordered_map res; - struct V { - wivrn::steam_icon current{}; + wivrn::steam::icon current{}; void begin_dict(const std::string & key) { @@ -198,7 +242,7 @@ std::unordered_map wivrn::read_steam_icons(std::fil V v; read_dict(in, v, string_table_ptr); if (not(v.current.clienticon.empty() and v.current.linuxclienticon.empty())) - res.emplace(app_id, std::move(v.current)); + icons.emplace(app_id, std::move(v.current)); } catch (...) { @@ -206,12 +250,15 @@ std::unordered_map wivrn::read_steam_icons(std::fil } in.seekg(pos + size_data); } - - return res; } -std::vector wivrn::read_steam_shortcuts(std::filesystem::path path) +void read_steam_shortcuts( + const std::filesystem::path & path, + std::vector & apps, + std::unordered_map & icons) { + if (not std::filesystem::exists(path)) + return; binary_reader in{std::ifstream{path}}; auto type = in.read(); auto name = in.read(); @@ -222,10 +269,12 @@ std::vector wivrn::read_steam_shortcuts(std::filesystem:: std::vector res; struct V { - std::vector & items; + std::vector & apps; + std::unordered_map & icons; int depth = 0; - wivrn::steam_shortcut current{}; + wivrn::steam::application current{}; + std::filesystem::path icon; bool VR = false; void begin_dict(const std::string & key) @@ -234,6 +283,7 @@ std::vector wivrn::read_steam_shortcuts(std::filesystem:: if (depth == 1) { current = decltype(current){}; + icon.clear(); VR = false; } } @@ -243,7 +293,14 @@ std::vector wivrn::read_steam_shortcuts(std::filesystem:: if (depth == 0) { if (VR) - items.push_back(std::move(current)); + { + if (not icon.empty()) + icons[current.appid] = std::move(icon); + // ¯\_(ツ)_/¯ + current.appid = (current.appid << 32) | 0x02000000; + current.url = std::format("steam://rungameid/{}", current.appid); + apps.push_back(std::move(current)); + } } } void on_string(const std::string & key, std::string && val) @@ -251,9 +308,9 @@ std::vector wivrn::read_steam_shortcuts(std::filesystem:: if (depth == 1) { if (strcasecmp(key.c_str(), "AppName") == 0) - current.name = std::move(val); + current.name[""] = std::move(val); else if (strcasecmp(key.c_str(), "icon") == 0 and not val.empty()) - current.icon = std::move(val); + icon = std::move(val); } } void on_uint32_t(const std::string & key, uint32_t val) @@ -266,54 +323,169 @@ std::vector wivrn::read_steam_shortcuts(std::filesystem:: VR = val; } } - } v{res}; + } v{apps, icons}; read_dict(in, v); +} + +wivrn::steam::steam(std::filesystem::path root, bool flatpak) : + root(std::move(root)), + flatpak(flatpak), + default_userid(guess_steam_userid(this->root)) +{ +} + +std::vector wivrn::steam::find_installations() +{ + std::vector res; + auto h = home(); + + auto try_construct = [&res](std::filesystem::path root, bool flatpak = false) { + try + { + if (std::filesystem::exists(root)) + { + res.push_back(wivrn::steam(std::move(root), flatpak)); + return true; + } + } + catch (std::exception &) + { + } + return false; + }; + + // Flatpak Steam + try_construct(h / ".var/app/com.valvesoftware.Steam/.steam/steam", true); + + // Debian Steam (accessed from flatpak) + try_construct(h / ".steam/debian-installation") + // system Steam + or try_construct(xdg_data_home() / "Steam") + // system Steam (accessed from flatpak) + or try_construct(h / ".local/share/Steam"); return res; } -std::optional wivrn::guess_steam_userid(std::filesystem::path steam_root) +std::vector wivrn::steam::list_applications() { - auto localconfig = steam_root / "config/loginusers.vdf"; - if (not std::filesystem::exists(localconfig)) - return {}; - - try + std::vector res; + // Steam games, from VR manifest { - std::optional res; - std::ifstream in{localconfig}; - std::string line; - std::getline(in, line); - if (strcasecmp(line.c_str(), "\"users\"")) - return {}; - - uint64_t current = 0; - - std::vector key; - std::vector value; - while (in) + std::ifstream manifest(root / "config/steamapps.vrmanifest"); + nlohmann::json json = nlohmann::json::parse(manifest); + for (auto & i: json["applications"]) { - std::getline(in, line); - - key.resize(line.size()); - value.resize(line.size()); - - if (sscanf(line.c_str(), "\t\"%ld\"", ¤t)) + try { + // Steam games have an url launch + // shortcuts may be in this file, or may not... + if (i["launch_type"] != "url") + continue; + + std::string app_key = i["app_key"]; + const char prefix[] = "steam.app."; + if (not app_key.starts_with(prefix)) + continue; + + application app{ + .appid = stoul(app_key.substr(strlen(prefix))), + .url = i["url"], + }; + + 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; + else + continue; // skip unnamed app + } + res.push_back(std::move(app)); } - else if (sscanf(line.c_str(), "\t\t%s\t%s", key.data(), value.data())) + catch (std::exception & e) { - if (strncasecmp(key.data(), "\"MostRecent\"", key.size()) == 0 and strncasecmp(value.data(), "\"1\"", value.size()) == 0) - res = current; // truncate to 32 bits + std::cerr << "Failed to parse Steam VR manifest: " << e.what() << std::endl; } } - if (current and not res) - res = current; - return res; } - catch (std::exception & e) + + // Shortcuts + if (default_userid) { + try + { + read_steam_shortcuts(root / "userdata" / std::to_string(*default_userid) / "config/shortcuts.vdf", res, shortcut_icons); + } + catch (std::exception & e) + { + } + } + else + { + // Is it a good idea to just iterate over all users? + for (auto const & entry: std::filesystem::directory_iterator{root / "userdata"}) + try + { + read_steam_shortcuts(entry.path() / "config/shortcuts.vdf", res, shortcut_icons); + } + catch (std::exception & e) + { + } + } + + return res; +} + +std::optional wivrn::steam::get_icon(uint64_t appid) +{ + if (appid & 0x02000000) + { + // Shortcut + auto it = shortcut_icons.find(appid >> 32); + if (it != shortcut_icons.end()) + return it->second; return {}; } + + if (not icons) + { + icons.emplace(); + try + { + read_steam_icons(root / "appcache/appinfo.vdf", *icons); + } + catch (...) + {} + } + auto it = icons->find(appid); + if (it == icons->end()) + return {}; + + auto icon_path = root / "steam/games" / (it->second.clienticon + ".ico"); + + if (std::filesystem::exists(icon_path)) + return icon_path; + + icon_path = root / "steam/games" / (it->second.linuxclienticon + ".zip"); + if (std::filesystem::exists(icon_path)) + return icon_path; + + return {}; +} + +std::string wivrn::steam::get_steam_command() const +{ + if (flatpak) + return "flatpak run com.valvesoftware.Steam"; + return "steam"; } diff --git a/common/utils/steam_info.h b/common/utils/steam_info.h index b05a1d0c..0b20c570 100644 --- a/common/utils/steam_info.h +++ b/common/utils/steam_info.h @@ -26,10 +26,37 @@ namespace wivrn { -struct steam_icon +class steam { - std::string clienticon; - std::string linuxclienticon; +public: + struct application + { + uint64_t appid; + // localised names, with empty locale for default + std::unordered_map name; + std::string url; + }; + struct icon + { + std::string clienticon; + std::string linuxclienticon; + }; + +private: + std::filesystem::path root; + bool flatpak; + std::optional default_userid; + std::optional> icons; + std::unordered_map shortcut_icons; + + steam(std::filesystem::path root, bool flatpak); + +public: + static std::vector find_installations(); + + std::vector list_applications(); + std::optional get_icon(uint64_t app_id); + std::string get_steam_command() const; }; struct steam_shortcut @@ -39,9 +66,4 @@ struct steam_shortcut std::optional icon; }; -std::unordered_map read_steam_icons(std::filesystem::path path); -std::vector read_steam_shortcuts(std::filesystem::path path); - -std::optional guess_steam_userid(std::filesystem::path steam_root); - } // namespace wivrn diff --git a/common/utils/vdf.cpp b/common/utils/vdf.cpp new file mode 100644 index 00000000..b23c60d2 --- /dev/null +++ b/common/utils/vdf.cpp @@ -0,0 +1,191 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2026 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 "vdf.h" + +#include +#include + +namespace +{ + +void consume_whitespace(const char *& pos, const char * end) +{ + for (; pos != end; ++pos) + { + if (not std::isspace(*pos)) + return; + } + throw std::runtime_error("malformed VDF file"); +} + +wivrn::vdf::string read_string(const char *& pos, const char * end) +{ + consume_whitespace(pos, end); + + const bool quoted = *pos == '"'; + if (quoted) + ++pos; + + const char * begin = pos; + + for (; pos != end; ++pos) + { + if (*pos == '\\') + { + ++pos; + if (pos == end) + break; + continue; + } + + if (quoted) + { + if (*pos == '"') + return {std::string_view(begin, pos++)}; + } + else + { + if (std::isspace(*pos) or *pos == '{' or *pos == '}') + return {std::string_view(begin, pos)}; + } + } + throw std::runtime_error("malformed string in VDF file"); +} + +std::variant> read_value(const char *& pos, const char * end); + +wivrn::vdf::keyvalue read_key_value(const char *& pos, const char * end) +{ + return wivrn::vdf::keyvalue{ + .key = read_string(pos, end), + .value = read_value(pos, end), + }; +} + +std::vector read_dict(const char *& pos, const char * end) +{ + consume_whitespace(pos, end); + if (*pos != '{') + throw std::runtime_error("malformed VDF file"); + ++pos; + + std::vector res; + while (true) + { + consume_whitespace(pos, end); + if (*pos == '}') + { + ++pos; + return res; + } + res.push_back(read_key_value(pos, end)); + } + + throw std::runtime_error("malformed dict in VDF file"); +} + +std::variant> read_value(const char *& pos, const char * end) +{ + consume_whitespace(pos, end); + + if (*pos == '{') + return read_dict(pos, end); + else + return read_string(pos, end); +} + +} // namespace + +bool wivrn::vdf::string::operator==(const char * b) const +{ + const char * a = data.begin(); + for (; a != data.end(); ++a, ++b) + { + if (*b == '\0') + return false; + if (*a == '\\') + { + ++a; + // This should be taken care of by the parser + assert(a != data.end()); + } + + if (std::tolower(*b) != std::tolower(*a)) + return false; + } + return *b == '\0'; +} + +wivrn::vdf::root::root(const std::filesystem::path & file) +{ + boost::iostreams::mapped_file in{file}; + opaque = in; + + const char * pos = in.begin(); + key = read_string(pos, in.end()); + value = read_value(pos, in.end()); +} + +void dump(const wivrn::vdf::keyvalue & kv, int depth) +{ + for (int i = 0; i < depth; ++i) + std::cout << '\t'; + std::cout << '"' << kv.key.data << "\": "; + struct V + { + int depth; + void operator()(const wivrn::vdf::string & s) + { + std::cout << '"' << s.data << '"'; + } + void operator()(const std::vector & kv) + { + std::cout << '{' << std::endl; + for (const auto & i: kv) + { + dump(i, depth + 1); + if (&i != &kv.back()) + std::cout << ','; + std::cout << std::endl; + } + for (int i = 0; i < depth; ++i) + std::cout << '\t'; + std::cout << "}"; + } + }; + std::visit(V{depth}, kv.value); +} + +#ifdef WIVRN_BUILD_TEST +void dump(const wivrn::vdf::root & kv) +{ + std::cout << '{'; + dump(kv, 0); + std::cout << '}'; +} + +int main(int argc, char ** argv) +{ + for (int i = 1; i < argc; ++i) + { + wivrn::vdf::root doc{argv[i]}; + dump(doc); + } +} +#endif diff --git a/common/utils/vdf.h b/common/utils/vdf.h new file mode 100644 index 00000000..3be53223 --- /dev/null +++ b/common/utils/vdf.h @@ -0,0 +1,54 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2026 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 +#include +#include + +namespace wivrn +{ + +namespace vdf +{ +struct string +{ + std::string_view data; + + bool operator==(const char *) const; +}; + +struct keyvalue +{ + string key; + std::variant> value; +}; + +struct root : public keyvalue +{ + root(const std::filesystem::path &); + +private: + std::any opaque; +}; +} // namespace vdf + +} // namespace wivrn