Send application icons to the headset
This commit is contained in:
@@ -183,6 +183,11 @@ if (WIVRN_BUILD_SERVER)
|
||||
pkg_check_modules(libnotify REQUIRED IMPORTED_TARGET libnotify)
|
||||
endif()
|
||||
|
||||
if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER)
|
||||
pkg_check_modules(librsvg-2.0 REQUIRED IMPORTED_TARGET librsvg-2.0)
|
||||
find_package(PNG)
|
||||
endif()
|
||||
|
||||
if (WIVRN_BUILD_DASHBOARD)
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Quick Test Gui QuickControls2 Widgets Network)
|
||||
|
||||
|
||||
@@ -44,9 +44,12 @@ 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
|
||||
utils/flatpak.cpp
|
||||
utils/xdg_icon_lookup.cpp
|
||||
utils/steam_app_info.cpp
|
||||
utils/load_icon.cpp
|
||||
application.cpp
|
||||
)
|
||||
target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PUBLIC wivrn-common-base)
|
||||
target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 PUBLIC wivrn-common-base)
|
||||
|
||||
if (WIVRN_BUILD_TEST)
|
||||
add_executable(list-apps
|
||||
|
||||
+86
-12
@@ -20,7 +20,10 @@
|
||||
#include "application.h"
|
||||
|
||||
#include "utils/flatpak.h"
|
||||
#include "utils/load_icon.h"
|
||||
#include "utils/steam_app_info.h"
|
||||
#include "utils/xdg_base_directory.h"
|
||||
#include "utils/xdg_icon_lookup.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@@ -32,7 +35,6 @@ namespace wivrn
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
std::string read_file(const std::filesystem::path & path)
|
||||
{
|
||||
std::ifstream f(path);
|
||||
@@ -47,30 +49,82 @@ std::filesystem::path home()
|
||||
return home ? home : "";
|
||||
}
|
||||
|
||||
std::pair<std::string, std::string> read_vr_manifest()
|
||||
std::pair<std::string, std::optional<std::filesystem::path>> find_steam()
|
||||
{
|
||||
// system Steam
|
||||
auto manifest = read_file(xdg_data_home() / "Steam/config/steamapps.vrmanifest");
|
||||
if (not manifest.empty())
|
||||
return {"steam", manifest};
|
||||
if (std::filesystem::exists(xdg_data_home() / "Steam"))
|
||||
return {"steam", xdg_data_home() / "Steam"};
|
||||
|
||||
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};
|
||||
if (std::filesystem::exists(h / ".local/share/Steam"))
|
||||
return {"steam", h / ".local/share/Steam"};
|
||||
|
||||
// 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};
|
||||
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"};
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string read_vr_manifest(const std::filesystem::path & root)
|
||||
{
|
||||
return read_file(root / "config/steamapps.vrmanifest");
|
||||
}
|
||||
|
||||
steam_app_info read_steam_app_info(const std::filesystem::path & root)
|
||||
{
|
||||
try
|
||||
{
|
||||
return steam_app_info{root / "appcache/appinfo.vdf"};
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::byte> load_steam_icon(const std::filesystem::path & root, int app_id, const steam_app_info & info)
|
||||
{
|
||||
const auto & app_info = info.get(app_id);
|
||||
|
||||
try
|
||||
{
|
||||
std::string icon{std::get<std::string_view>(app_info.at("common.clienticon"))};
|
||||
|
||||
auto image = load_icon(root / "steam/games" / (icon + ".ico"), 128);
|
||||
if (not image.empty())
|
||||
return image;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::string icon{std::get<std::string_view>(app_info.at("common.LinuxClientIcon"))};
|
||||
|
||||
auto image = load_icon(root / "steam/games" / (icon + ".zip"), 128);
|
||||
if (not image.empty())
|
||||
return image;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void read_steam_vr_apps(std::unordered_map<std::string, application> & res)
|
||||
{
|
||||
auto [command, manifest] = read_vr_manifest();
|
||||
auto [command, root] = find_steam();
|
||||
if (not root)
|
||||
return;
|
||||
|
||||
auto manifest = read_vr_manifest(*root);
|
||||
auto info = read_steam_app_info(*root);
|
||||
|
||||
if (manifest.empty())
|
||||
return;
|
||||
nlohmann::json json = nlohmann::json::parse(manifest);
|
||||
@@ -112,6 +166,19 @@ void read_steam_vr_apps(std::unordered_map<std::string, application> & res)
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
const char prefix[] = "steam.app.";
|
||||
if (app_key.starts_with(prefix))
|
||||
{
|
||||
uint32_t app_id = stoll(app_key.substr(strlen(prefix)));
|
||||
app.image = load_steam_icon(*root, app_id, info);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
if (not app.exec.empty())
|
||||
res[app_key] = std::move(app);
|
||||
}
|
||||
@@ -253,6 +320,11 @@ std::optional<application> do_desktop_entry(const std::filesystem::path & filena
|
||||
res->exec = unescape(item.value);
|
||||
if (item.key == "Path")
|
||||
res->path = unescape(item.value);
|
||||
if (item.key == "Icon")
|
||||
{
|
||||
if (auto filename = xdg_icon_lookup(std::string{item.value}, 256))
|
||||
res->image = load_icon(*filename, 256);
|
||||
}
|
||||
}
|
||||
|
||||
if (res->exec.empty() or not res->name.contains(""))
|
||||
@@ -299,8 +371,10 @@ std::unordered_map<std::string, application> list_applications(bool include_stea
|
||||
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
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
@@ -30,6 +31,7 @@ struct application
|
||||
// localised names, with empty locale for default
|
||||
std::unordered_map<std::string, std::string> name;
|
||||
std::string exec;
|
||||
std::vector<std::byte> image; // In PNG
|
||||
std::optional<std::string> path;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,416 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "load_icon.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <librsvg/rsvg.h>
|
||||
#include <limits>
|
||||
#include <png.h>
|
||||
#include <span>
|
||||
|
||||
namespace
|
||||
{
|
||||
std::vector<std::byte> load_file(const std::filesystem::path & filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::binary | std::ios::ate);
|
||||
file.exceptions(std::ios_base::badbit | std::ios_base::failbit);
|
||||
|
||||
size_t size = file.tellg();
|
||||
file.seekg(0);
|
||||
|
||||
std::vector<std::byte> bytes(size);
|
||||
|
||||
file.read(reinterpret_cast<char *>(bytes.data()), size);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
T read(std::span<std::byte> & buffer)
|
||||
{
|
||||
if (sizeof(T) > buffer.size())
|
||||
throw std::runtime_error{"File truncated"};
|
||||
|
||||
T value;
|
||||
memcpy(&value, buffer.data(), sizeof(T));
|
||||
buffer = buffer.subspan(sizeof(T));
|
||||
|
||||
static_assert(std::endian::native == std::endian::big or std::endian::native == std::endian::little);
|
||||
|
||||
if constexpr (std::endian::native == std::endian::big)
|
||||
return std::byteswap(value);
|
||||
else
|
||||
return value;
|
||||
}
|
||||
|
||||
std::span<std::byte> read(std::span<std::byte> & buffer, size_t size)
|
||||
{
|
||||
if (size > buffer.size())
|
||||
throw std::runtime_error{"File truncated"};
|
||||
|
||||
std::span<std::byte> value{buffer.data(), size};
|
||||
buffer = buffer.subspan(size);
|
||||
return value;
|
||||
}
|
||||
|
||||
std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size)
|
||||
{
|
||||
// TODO check if aspect ratio needs to be handled, dpi
|
||||
|
||||
GError * error{};
|
||||
GFile * file = g_file_new_for_path(filename.c_str());
|
||||
RsvgHandle * handle = rsvg_handle_new_from_gfile_sync(file, RSVG_HANDLE_FLAGS_NONE, nullptr, &error);
|
||||
|
||||
// Create a Cairo image surface and a rendering context for it
|
||||
cairo_surface_t * surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
|
||||
cairo_t * cr = cairo_create(surface);
|
||||
|
||||
// Set the dots-per-inch
|
||||
rsvg_handle_set_dpi(handle, 96.0);
|
||||
|
||||
// Render the handle scaled proportionally into that whole surface
|
||||
RsvgRectangle viewport = {
|
||||
.x = 0.0,
|
||||
.y = 0.0,
|
||||
.width = (double)size,
|
||||
.height = (double)size,
|
||||
};
|
||||
|
||||
if (!rsvg_handle_render_document(handle, cr, &viewport, &error))
|
||||
{
|
||||
g_printerr("could not render: %s", error->message);
|
||||
g_object_unref(handle);
|
||||
g_object_unref(file);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Write a PNG file
|
||||
std::vector<std::byte> png;
|
||||
auto cb = [](void * closure, const unsigned char * data, unsigned int length) -> cairo_status_t {
|
||||
std::vector<std::byte> & png = *reinterpret_cast<std::vector<std::byte> *>(closure);
|
||||
|
||||
size_t old_size = png.size();
|
||||
png.resize(old_size + length);
|
||||
memcpy(&png[old_size], data, length);
|
||||
|
||||
return CAIRO_STATUS_SUCCESS;
|
||||
};
|
||||
|
||||
if (cairo_surface_write_to_png_stream(surface, cb, &png) != CAIRO_STATUS_SUCCESS)
|
||||
{
|
||||
g_printerr("could not write output file");
|
||||
g_object_unref(handle);
|
||||
g_object_unref(file);
|
||||
return {};
|
||||
}
|
||||
|
||||
g_object_unref(handle);
|
||||
g_object_unref(file);
|
||||
return png;
|
||||
}
|
||||
|
||||
// See https://gitlab.gnome.org/GNOME/gimp/-/tree/master/plug-ins/file-ico?ref_type=heads
|
||||
|
||||
struct ico_file_entry
|
||||
{
|
||||
int width; // Width of icon in pixels
|
||||
int height; // Height of icon in pixels
|
||||
int bpp; // 1, 4, 8, 16, 24 or 32 bits per pixel
|
||||
std::span<std::byte> data;
|
||||
};
|
||||
|
||||
int rowstride(int width, int bpp)
|
||||
{
|
||||
switch (bpp)
|
||||
{
|
||||
case 1:
|
||||
if ((width % 32) == 0)
|
||||
return width / 8;
|
||||
else
|
||||
return 4 * (width / 32 + 1);
|
||||
|
||||
// case 4:
|
||||
// if ((width % 8) == 0)
|
||||
// return width / 2;
|
||||
// else
|
||||
// return 4 * (width / 8 + 1);
|
||||
//
|
||||
// case 8:
|
||||
// if ((width % 4) == 0)
|
||||
// return width;
|
||||
// else
|
||||
// return 4 * (width / 4 + 1);
|
||||
|
||||
case 24:
|
||||
if (((width * 3) % 4) == 0)
|
||||
return width * 3;
|
||||
else
|
||||
return 4 * (width * 3 / 4 + 1);
|
||||
|
||||
case 32:
|
||||
return width * 4;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size, int index = -1)
|
||||
{
|
||||
const uint32_t ICO_PNG_MAGIC = 0x474e5089;
|
||||
|
||||
try
|
||||
{
|
||||
auto ico = load_file(filename);
|
||||
|
||||
std::span header{ico};
|
||||
|
||||
auto reserved = read<uint16_t>(header);
|
||||
auto resource_type = read<uint16_t>(header);
|
||||
auto icon_count = read<uint16_t>(header);
|
||||
|
||||
if (reserved != 0 or resource_type != 1)
|
||||
return {};
|
||||
|
||||
std::vector<ico_file_entry> entries;
|
||||
int max_bpp = 0;
|
||||
|
||||
for (int i = 0; i < icon_count; i++)
|
||||
{
|
||||
ico_file_entry entry;
|
||||
entry.width = read<uint8_t>(header);
|
||||
entry.height = read<uint8_t>(header);
|
||||
auto num_colors = read<uint8_t>(header);
|
||||
auto reserved = read<uint8_t>(header); // reserved
|
||||
auto planes = read<uint16_t>(header);
|
||||
entry.bpp = read<uint16_t>(header);
|
||||
auto size = read<uint32_t>(header);
|
||||
auto offset = read<uint32_t>(header);
|
||||
|
||||
entry.data = std::span{ico}.subspan(offset, size);
|
||||
|
||||
if (entry.width == 0)
|
||||
entry.width = 256;
|
||||
if (entry.height == 0)
|
||||
entry.height = 256;
|
||||
if (reserved != 0)
|
||||
return {};
|
||||
if (planes != 0 and planes != 1)
|
||||
return {};
|
||||
|
||||
// Ignore 16bpp and less
|
||||
if (entry.bpp < 24)
|
||||
continue;
|
||||
|
||||
entries.push_back(entry);
|
||||
|
||||
max_bpp = std::max(max_bpp, entry.bpp);
|
||||
}
|
||||
|
||||
const ico_file_entry & best_entry = index >= 0 ? entries.at(index) : [&]() {
|
||||
const ico_file_entry * best_entry;
|
||||
|
||||
// Look for the smallest multiple of the requested size
|
||||
int criterion = std::numeric_limits<int>::max();
|
||||
|
||||
for (const auto & entry: entries)
|
||||
{
|
||||
if (entry.bpp != max_bpp)
|
||||
continue;
|
||||
|
||||
if (entry.width % size != 0 or entry.height % size != 0)
|
||||
continue;
|
||||
|
||||
if (std::min(entry.width, entry.height) / size < criterion)
|
||||
{
|
||||
criterion = std::min(entry.width, entry.height);
|
||||
best_entry = &entry;
|
||||
}
|
||||
}
|
||||
if (best_entry)
|
||||
return *best_entry;
|
||||
|
||||
// No multiple found: pick the largest icon
|
||||
criterion = 0;
|
||||
|
||||
for (const auto & entry: entries)
|
||||
{
|
||||
if (entry.bpp != max_bpp)
|
||||
continue;
|
||||
|
||||
if (entry.width * entry.height > criterion)
|
||||
{
|
||||
criterion = entry.width * entry.height;
|
||||
best_entry = &entry;
|
||||
}
|
||||
}
|
||||
return *best_entry;
|
||||
}();
|
||||
|
||||
auto data = best_entry.data;
|
||||
auto magic = read<uint32_t>(data);
|
||||
|
||||
if (magic == ICO_PNG_MAGIC)
|
||||
{
|
||||
// PNG file
|
||||
return {std::from_range, best_entry.data};
|
||||
}
|
||||
else if (magic == 40)
|
||||
{
|
||||
auto width = read<uint32_t>(data); // Width of image in pixels
|
||||
auto height = read<uint32_t>(data); // Height of image in pixels
|
||||
auto planes = read<uint16_t>(data); // Must be 1
|
||||
auto bpp = read<uint16_t>(data); // 1, 4, 8, 16, 24, 32
|
||||
auto compression = read<uint32_t>(data); // Must be 0 for icons
|
||||
auto image_size = read<uint32_t>(data); // Size of image (without this header)
|
||||
auto x_res = read<uint32_t>(data);
|
||||
auto y_res = read<uint32_t>(data);
|
||||
auto used_colors = read<uint32_t>(data);
|
||||
auto important_colors = read<uint32_t>(data);
|
||||
|
||||
if (planes != 1 or compression != 0)
|
||||
return {};
|
||||
|
||||
if (bpp != 1 and
|
||||
bpp != 4 and
|
||||
bpp != 8 and
|
||||
bpp != 16 and
|
||||
bpp != 24 and
|
||||
bpp != 32)
|
||||
return {};
|
||||
|
||||
auto w = width;
|
||||
auto h = height / 2;
|
||||
|
||||
// Don't bother supporting paletted icons
|
||||
// std::vector<uint32_t> palette;
|
||||
//
|
||||
// if (bpp <= 8)
|
||||
// {
|
||||
// // Load the palette
|
||||
// if (used_colors == 0)
|
||||
// used_colors = 1 << bpp;
|
||||
//
|
||||
// palette.resize(used_colors);
|
||||
// for (int i = 0; i < palette.size(); i++)
|
||||
// palette[i] = read<uint32_t>(data);
|
||||
// }
|
||||
|
||||
size_t xor_stride = rowstride(w, bpp);
|
||||
size_t and_stride = rowstride(w, 1);
|
||||
size_t dst_stride = 4 * w;
|
||||
|
||||
std::span<std::byte> xor_map = read(data, xor_stride * h);
|
||||
std::span<std::byte> and_map = read(data, and_stride * h);
|
||||
|
||||
std::vector<uint8_t> dest_buffer;
|
||||
dest_buffer.resize(h * dst_stride);
|
||||
|
||||
switch (bpp)
|
||||
{
|
||||
case 24:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
uint8_t * src_xor_row = (uint8_t *)xor_map.data() + xor_stride * y;
|
||||
uint8_t * src_and_row = (uint8_t *)and_map.data() + and_stride * y;
|
||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||
|
||||
for (int x = 0, i = 0; x < w; x++)
|
||||
{
|
||||
uint8_t b = *src_xor_row++; // Red
|
||||
uint8_t g = *src_xor_row++; // Green
|
||||
uint8_t r = *src_xor_row++; // Blue
|
||||
|
||||
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
|
||||
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
|
||||
|
||||
*dst_row++ = b;
|
||||
*dst_row++ = g;
|
||||
*dst_row++ = r;
|
||||
*dst_row++ = a;
|
||||
}
|
||||
}
|
||||
|
||||
case 32:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
uint8_t * src_xor_row = (uint8_t *)xor_map.data() + xor_stride * y;
|
||||
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
|
||||
|
||||
memcpy(dst_row, src_xor_row, dst_stride);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
png_image png{
|
||||
.version = PNG_IMAGE_VERSION,
|
||||
.width = w,
|
||||
.height = h,
|
||||
.format = PNG_FORMAT_BGRA,
|
||||
};
|
||||
|
||||
std::vector<std::byte> png_data{65536};
|
||||
png_alloc_size_t png_size = png_data.size();
|
||||
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
|
||||
|
||||
if (png_size > png_data.size())
|
||||
{
|
||||
png_data.resize(png_size);
|
||||
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
|
||||
}
|
||||
else
|
||||
png_data.resize(png_size);
|
||||
|
||||
return png_data;
|
||||
}
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<std::byte> wivrn::load_icon(const std::filesystem::path & filename, int size)
|
||||
{
|
||||
// TODO keep icons in cache
|
||||
|
||||
if (filename.extension() == ".svg")
|
||||
{
|
||||
return load_svg(filename, size);
|
||||
}
|
||||
else if (filename.extension() == ".ico")
|
||||
{
|
||||
// Steam icon (Windows)
|
||||
return load_ico(filename, size);
|
||||
}
|
||||
else if (filename.extension() == ".zip")
|
||||
{
|
||||
// Steam icon (Linux)
|
||||
// TODO
|
||||
return {};
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO read arbitrary format / rewrite as png
|
||||
return load_file(filename);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
std::vector<std::byte> load_icon(const std::filesystem::path & filename, int size);
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "steam_app_info.h"
|
||||
#include <bit>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
T read(std::span<char> & buffer);
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
T read(std::span<char> & buffer)
|
||||
{
|
||||
if (sizeof(T) > buffer.size())
|
||||
throw std::runtime_error{"File truncated"};
|
||||
|
||||
T value;
|
||||
memcpy(&value, buffer.data(), sizeof(T));
|
||||
buffer = buffer.subspan(sizeof(T));
|
||||
|
||||
static_assert(std::endian::native == std::endian::big or std::endian::native == std::endian::little);
|
||||
|
||||
if constexpr (std::endian::native == std::endian::big)
|
||||
return std::byteswap(value);
|
||||
else
|
||||
return value;
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string_view read<std::string_view>(std::span<char> & buffer)
|
||||
{
|
||||
size_t size = strnlen((char *)buffer.data(), buffer.size());
|
||||
if (size == buffer.size())
|
||||
throw std::runtime_error{"File truncated"};
|
||||
|
||||
char * str = buffer.data();
|
||||
buffer = buffer.subspan(size + 1);
|
||||
return {str, size};
|
||||
}
|
||||
|
||||
std::span<char> read(std::span<char> & buffer, size_t size)
|
||||
{
|
||||
if (size > buffer.size())
|
||||
throw std::runtime_error{"File truncated"};
|
||||
|
||||
std::span<char> value{buffer.data(), size};
|
||||
buffer = buffer.subspan(size);
|
||||
return value;
|
||||
}
|
||||
|
||||
std::vector<std::string_view> read_string_table(std::span<char> buffer)
|
||||
{
|
||||
std::vector<std::string_view> string_table;
|
||||
|
||||
uint32_t count = read<uint32_t>(buffer);
|
||||
string_table.reserve(count);
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
string_table.emplace_back(read<std::string_view>(buffer));
|
||||
|
||||
return string_table;
|
||||
}
|
||||
|
||||
void read_vdf(wivrn::steam_app_info::info & info, const std::string & prefix, std::span<char> & bindata, const std::vector<std::string_view> & string_table)
|
||||
{
|
||||
int type = read<uint8_t>(bindata);
|
||||
|
||||
while (type != 8)
|
||||
{
|
||||
std::string name{string_table.at(read<uint32_t>(bindata))};
|
||||
|
||||
// https://github.com/ValveResourceFormat/ValveKeyValue/blob/master/ValveKeyValue/ValveKeyValue/KeyValues1/KV1BinaryNodeType.cs
|
||||
switch (type)
|
||||
{
|
||||
case 0: // Dictionary
|
||||
read_vdf(info, prefix + name + ".", bindata, string_table);
|
||||
break;
|
||||
|
||||
case 1: // UTF-8 string
|
||||
info.emplace(prefix + name, read<std::string_view>(bindata));
|
||||
break;
|
||||
|
||||
case 2: // uint32_t
|
||||
info.emplace(prefix + name, read<uint32_t>(bindata));
|
||||
break;
|
||||
|
||||
// case 3: // float
|
||||
// break;
|
||||
|
||||
default:
|
||||
throw std::runtime_error{"Unknown object type " + std::to_string(type)};
|
||||
}
|
||||
|
||||
type = read<uint8_t>(bindata);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
wivrn::steam_app_info::steam_app_info(std::filesystem::path path)
|
||||
{
|
||||
std::vector<std::string_view> string_table;
|
||||
|
||||
{
|
||||
std::ifstream f{path};
|
||||
data = std::vector<char>{std::istreambuf_iterator{f}, {}};
|
||||
}
|
||||
std::span<char> buffer = data;
|
||||
|
||||
// See https://github.com/SteamDatabase/SteamAppInfo/blob/master/README.md#file-header
|
||||
uint32_t magic = read<uint32_t>(buffer);
|
||||
if ((magic & 0xff'ff'ff'00) != 0x07'56'44'00)
|
||||
throw std::runtime_error{"Wrong magic number"};
|
||||
int version = magic & 0xff;
|
||||
|
||||
if (version < 41)
|
||||
throw std::runtime_error{"Unsupported version"};
|
||||
|
||||
int universe = read<uint32_t>(buffer);
|
||||
if (version >= 41)
|
||||
{
|
||||
size_t string_offset = read<uint64_t>(buffer);
|
||||
string_table = read_string_table(std::span<char>{data}.subspan(string_offset));
|
||||
}
|
||||
else
|
||||
throw std::runtime_error{"Unsupported version"};
|
||||
|
||||
while (true)
|
||||
{
|
||||
// See https://github.com/SteamDatabase/SteamAppInfo/blob/master/README.md#app-entry-repeated
|
||||
|
||||
auto app_id = read<uint32_t>(buffer);
|
||||
if (app_id == 0)
|
||||
break;
|
||||
|
||||
auto size_data = read<uint32_t>(buffer);
|
||||
char * current_offset = buffer.data();
|
||||
|
||||
read<uint32_t>(buffer); // info_state
|
||||
read<uint32_t>(buffer); // last_updated
|
||||
read<uint64_t>(buffer); // pics_token
|
||||
read(buffer, 20); // SHA1 of the app info
|
||||
read<uint32_t>(buffer); // change number
|
||||
// if (version < 38)
|
||||
// read<uint8_t>(buffer); // section type
|
||||
read(buffer, 20); // SHA1 of the bin data
|
||||
auto bindata = read(buffer, size_data - (buffer.data() - current_offset));
|
||||
|
||||
try
|
||||
{
|
||||
bindata = bindata.subspan(4 + 1); // Skip the checksum + type (must be 0)
|
||||
|
||||
info app_info;
|
||||
read_vdf(app_info, "", bindata, string_table);
|
||||
|
||||
app_data.emplace(app_id, app_info);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Ignore errors
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class steam_app_info
|
||||
{
|
||||
public:
|
||||
using info = std::unordered_map<std::string, std::variant<uint32_t, std::string_view>>;
|
||||
|
||||
private:
|
||||
std::vector<char> data;
|
||||
|
||||
std::unordered_map<int, info> app_data;
|
||||
|
||||
public:
|
||||
steam_app_info(std::filesystem::path path);
|
||||
steam_app_info() = default;
|
||||
steam_app_info(const steam_app_info &) = delete;
|
||||
steam_app_info(steam_app_info &&) = default;
|
||||
steam_app_info & operator=(const steam_app_info &) = delete;
|
||||
steam_app_info & operator=(steam_app_info &&) = default;
|
||||
|
||||
const auto & get(int appid) const
|
||||
{
|
||||
return app_data.at(appid);
|
||||
}
|
||||
};
|
||||
} // namespace wivrn
|
||||
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "xdg_icon_lookup.h"
|
||||
|
||||
#include "strings.h"
|
||||
#include "utils/flatpak.h"
|
||||
#include "xdg_base_directory.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/property_tree/ini_parser.hpp>
|
||||
#include <filesystem>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
|
||||
namespace
|
||||
{
|
||||
enum class icon_theme_type
|
||||
{
|
||||
fixed,
|
||||
scalable,
|
||||
threshold
|
||||
};
|
||||
|
||||
struct icon_theme_dir
|
||||
{
|
||||
std::filesystem::path path;
|
||||
|
||||
int size;
|
||||
int scale;
|
||||
icon_theme_type type;
|
||||
int min_size;
|
||||
int max_size;
|
||||
int threshold;
|
||||
|
||||
bool match_size(int required_size, int required_scale) const
|
||||
{
|
||||
if (scale != required_scale)
|
||||
return false;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case icon_theme_type::fixed:
|
||||
return size == required_size;
|
||||
|
||||
case icon_theme_type::scalable:
|
||||
return min_size <= required_size and required_size <= max_size;
|
||||
|
||||
case icon_theme_type::threshold:
|
||||
return size - threshold <= required_size and required_size <= size + threshold;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int size_distance(int required_size, int required_scale) const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case icon_theme_type::fixed:
|
||||
return abs(size * scale - required_size * required_scale);
|
||||
|
||||
case icon_theme_type::scalable:
|
||||
if (required_size * required_scale < min_size * scale)
|
||||
return min_size * scale - required_size * required_scale;
|
||||
|
||||
if (required_size * required_scale > max_size * scale)
|
||||
return required_size * required_scale - max_size * scale;
|
||||
|
||||
return 0;
|
||||
|
||||
case icon_theme_type::threshold:
|
||||
if (required_size * required_scale < (size - threshold) * scale)
|
||||
return min_size * scale - required_size * required_scale;
|
||||
|
||||
if (required_size * required_scale > (size + threshold) * scale)
|
||||
return required_size * required_scale - max_size * scale;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return std::numeric_limits<int>::max();
|
||||
}
|
||||
};
|
||||
|
||||
void find_icon_theme_dirs_helper(const std::vector<std::filesystem::path> & base_dirs, std::vector<icon_theme_dir> & dirs, std::vector<std::string> & themes, const std::string & theme)
|
||||
{
|
||||
for (const auto & base_dir: base_dirs)
|
||||
{
|
||||
const auto theme_dir = base_dir / "icons" / theme;
|
||||
const auto index_theme = theme_dir / "index.theme";
|
||||
|
||||
if (not std::filesystem::exists(index_theme))
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
boost::property_tree::ptree index;
|
||||
boost::property_tree::ini_parser::read_ini(index_theme, index);
|
||||
|
||||
for (const auto & inherited_theme: utils::split(index.get_optional<std::string>("Icon Theme.Inherits").value_or(""), ","))
|
||||
{
|
||||
// if (not std::ranges::contains(themes, inherited_theme))
|
||||
// themes.push_back(inherited_theme);
|
||||
}
|
||||
|
||||
for (auto directory: utils::split(index.get<std::string>("Icon Theme.Directories"), ","))
|
||||
{
|
||||
int size = index.get<int>(directory + ".Size");
|
||||
int scale = index.get_optional<int>(directory + ".Scale").value_or(1);
|
||||
int min_size = index.get_optional<int>(directory + ".MinSize").value_or(size);
|
||||
int max_size = index.get_optional<int>(directory + ".MaxSize").value_or(size);
|
||||
int threshold = index.get_optional<int>(directory + ".Threshold").value_or(2);
|
||||
|
||||
std::string type_str = index.get_optional<std::string>(directory + ".Type").value_or("Threshold");
|
||||
|
||||
icon_theme_type type = icon_theme_type::threshold;
|
||||
if (type_str == "Fixed")
|
||||
type = icon_theme_type::fixed;
|
||||
else if (type_str == "Scalable")
|
||||
type = icon_theme_type::scalable;
|
||||
else if (type_str == "Threshold")
|
||||
type = icon_theme_type::threshold;
|
||||
|
||||
dirs.push_back(icon_theme_dir{
|
||||
.path = theme_dir / directory,
|
||||
.size = size,
|
||||
.scale = scale,
|
||||
.type = type,
|
||||
.min_size = min_size,
|
||||
.max_size = max_size,
|
||||
.threshold = threshold,
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
// std::cerr << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<icon_theme_dir> find_icon_theme_dirs(std::vector<std::string> themes)
|
||||
{
|
||||
std::vector<icon_theme_dir> dirs;
|
||||
std::vector<std::filesystem::path> base_dirs = xdg_data_dirs();
|
||||
|
||||
if (wivrn::is_flatpak())
|
||||
{
|
||||
// Try to guess host data dirs
|
||||
base_dirs.push_back("/run/host/usr/share");
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < themes.size(); ++i)
|
||||
find_icon_theme_dirs_helper(base_dirs, dirs, themes, themes[i]);
|
||||
|
||||
if (not std::ranges::contains(themes, "hicolor"))
|
||||
find_icon_theme_dirs_helper(base_dirs, dirs, themes, "hicolor");
|
||||
|
||||
return dirs;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::optional<std::filesystem::path> wivrn::xdg_icon_lookup(const std::string & icon_name, int size, int scale)
|
||||
{
|
||||
if (std::filesystem::exists(icon_name))
|
||||
return icon_name;
|
||||
|
||||
static const std::vector<icon_theme_dir> dirs = find_icon_theme_dirs({"hicolor"});
|
||||
|
||||
for (const auto & dir: dirs)
|
||||
{
|
||||
if (dir.match_size(size, scale))
|
||||
{
|
||||
for (const auto & ext: {".png", ".svg", ".xpm"})
|
||||
{
|
||||
if (std::filesystem::exists(dir.path / (icon_name + ext)))
|
||||
return dir.path / (icon_name + ext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int best_distance = std::numeric_limits<int>::max();
|
||||
std::optional<std::filesystem::path> best_path;
|
||||
|
||||
for (const auto & dir: dirs)
|
||||
{
|
||||
if (dir.size_distance(size, scale) < best_distance)
|
||||
{
|
||||
for (const auto & ext: {".png", ".svg", ".xpm"})
|
||||
{
|
||||
if (std::filesystem::exists(dir.path / (icon_name + ext)))
|
||||
{
|
||||
best_distance = dir.size_distance(size, scale);
|
||||
best_path = dir.path / (icon_name + ext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best_path;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
std::optional<std::filesystem::path> xdg_icon_lookup(const std::string & icon_name, int size, int scale = 1);
|
||||
}
|
||||
@@ -675,6 +675,7 @@ struct application_list
|
||||
{
|
||||
std::string id;
|
||||
std::string name;
|
||||
std::vector<std::byte> image; // In PNG
|
||||
};
|
||||
std::vector<application> applications;
|
||||
};
|
||||
|
||||
@@ -737,7 +737,8 @@ void wivrn_session::operator()(from_headset::get_application_list && request)
|
||||
response.applications.emplace_back(
|
||||
std::move(id),
|
||||
// FIXME: use locale
|
||||
app.name.at(""));
|
||||
app.name.at(""),
|
||||
app.image);
|
||||
}
|
||||
send_control(std::move(response));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user