Revert "Merge branch 'master' of https://github.com/Vindicus999/WiVRn"
This reverts commit6212dccc2b, reversing changes made to80e31b642a.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
if (WIVRN_USE_SYSTEM_BOOST)
|
||||
if (BOOST_FOUND)
|
||||
add_library(Boost::pfr ALIAS Boost::headers)
|
||||
elseif (WIVRN_BUILD_CLIENT)
|
||||
FetchContent_MakeAvailable(boost)
|
||||
@@ -7,7 +7,6 @@ else()
|
||||
endif()
|
||||
|
||||
if (GIT_DESC)
|
||||
string(REGEX REPLACE "^v([0-9])" "\\1" GIT_DESC ${GIT_DESC})
|
||||
configure_file(version.cpp.in version.cpp)
|
||||
else ()
|
||||
add_custom_target(wivrn-version ALL
|
||||
@@ -51,7 +50,7 @@ if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER)
|
||||
utils/ini.cpp
|
||||
application.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 PUBLIC wivrn-common-base)
|
||||
|
||||
if (WIVRN_BUILD_TEST)
|
||||
add_executable(list-apps
|
||||
|
||||
+40
-34
@@ -20,6 +20,7 @@
|
||||
#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"
|
||||
@@ -50,19 +51,20 @@ std::filesystem::path home()
|
||||
|
||||
std::pair<std::string, std::optional<std::filesystem::path>> 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"};
|
||||
|
||||
// system Steam
|
||||
if (std::filesystem::exists(xdg_data_home() / "Steam"))
|
||||
return {"steam", xdg_data_home() / "Steam"};
|
||||
|
||||
auto h = home();
|
||||
|
||||
// system Steam (accessed from flatpak)
|
||||
if (std::filesystem::exists(h / ".local/share/Steam"))
|
||||
return {"steam", h / ".local/share/Steam"};
|
||||
|
||||
// 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"};
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -83,17 +85,17 @@ steam_app_info read_steam_app_info(const std::filesystem::path & root)
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> find_steam_icon(const std::filesystem::path & root, int app_id, const steam_app_info & info)
|
||||
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 icon_path = root / "steam/games" / (icon + ".ico");
|
||||
|
||||
if (std::filesystem::exists(icon_path))
|
||||
return icon_path;
|
||||
auto image = load_icon(root / "steam/games" / (icon + ".ico"), 128);
|
||||
if (not image.empty())
|
||||
return image;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@@ -101,21 +103,20 @@ std::optional<std::filesystem::path> find_steam_icon(const std::filesystem::path
|
||||
|
||||
try
|
||||
{
|
||||
std::string icon{std::get<std::string_view>(app_info.at("common.linuxclienticon"))};
|
||||
std::string icon{std::get<std::string_view>(app_info.at("common.LinuxClientIcon"))};
|
||||
|
||||
auto icon_path = root / "steam/games" / (icon + ".zip");
|
||||
|
||||
if (std::filesystem::exists(icon_path))
|
||||
return icon_path;
|
||||
auto image = load_icon(root / "steam/games" / (icon + ".zip"), 128);
|
||||
if (not image.empty())
|
||||
return image;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
return {};
|
||||
}
|
||||
|
||||
void read_steam_vr_apps(std::unordered_map<std::string, application> & res)
|
||||
void read_steam_vr_apps(std::unordered_map<std::string, application> & res, bool load_icons)
|
||||
{
|
||||
auto [command, root] = find_steam();
|
||||
if (not root)
|
||||
@@ -165,17 +166,20 @@ void read_steam_vr_apps(std::unordered_map<std::string, application> & res)
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
if (load_icons)
|
||||
{
|
||||
const char prefix[] = "steam.app.";
|
||||
if (app_key.starts_with(prefix))
|
||||
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 (...)
|
||||
{
|
||||
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())
|
||||
@@ -277,7 +281,7 @@ bool contains(std::string_view entries, std::string_view val, char sep = ';')
|
||||
return false;
|
||||
}
|
||||
|
||||
std::optional<application> do_desktop_entry(const std::filesystem::path & filename)
|
||||
std::optional<application> do_desktop_entry(const std::filesystem::path & filename, bool load_icons)
|
||||
{
|
||||
auto data = read_file(filename);
|
||||
std::optional<application> res;
|
||||
@@ -319,11 +323,12 @@ 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 (item.key == "Icon" and load_icons)
|
||||
{
|
||||
try
|
||||
{
|
||||
res->icon_path = xdg_icon_lookup(std::string{item.value}, 256);
|
||||
if (auto filename = xdg_icon_lookup(std::string{item.value}, 256))
|
||||
res->image = load_icon(*filename, 256);
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
@@ -336,7 +341,7 @@ std::optional<application> do_desktop_entry(const std::filesystem::path & filena
|
||||
return res;
|
||||
}
|
||||
|
||||
void do_data_dir(std::filesystem::path dir, std::unordered_map<std::string, application> & res)
|
||||
void do_data_dir(std::filesystem::path dir, std::unordered_map<std::string, application> & res, bool load_icons)
|
||||
{
|
||||
dir = dir / "applications";
|
||||
if (not std::filesystem::is_directory(dir))
|
||||
@@ -359,28 +364,29 @@ void do_data_dir(std::filesystem::path dir, std::unordered_map<std::string, appl
|
||||
if (res.contains(file_id))
|
||||
continue;
|
||||
|
||||
auto app = do_desktop_entry(entry.path());
|
||||
auto app = do_desktop_entry(entry.path(), load_icons);
|
||||
if (app)
|
||||
res.emplace(std::move(file_id), std::move(*app));
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::unordered_map<std::string, application> list_applications()
|
||||
std::unordered_map<std::string, application> list_applications(bool include_steam, bool load_icons)
|
||||
{
|
||||
std::unordered_map<std::string, application> res;
|
||||
|
||||
read_steam_vr_apps(res);
|
||||
if (include_steam)
|
||||
read_steam_vr_apps(res, load_icons);
|
||||
|
||||
do_data_dir(xdg_data_home(), res);
|
||||
do_data_dir(xdg_data_home(), res, load_icons);
|
||||
|
||||
for (auto && dir: xdg_data_dirs())
|
||||
do_data_dir(std::move(dir), res);
|
||||
do_data_dir(std::move(dir), res, load_icons);
|
||||
|
||||
if (wivrn::is_flatpak())
|
||||
{
|
||||
// Try to guess host data dirs
|
||||
do_data_dir("/run/host/usr/share", res);
|
||||
do_data_dir("/run/host/usr/share", res, load_icons);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
@@ -31,9 +31,9 @@ struct application
|
||||
// localised names, with empty locale for default
|
||||
std::unordered_map<std::string, std::string> name;
|
||||
std::string exec;
|
||||
std::optional<std::filesystem::path> icon_path;
|
||||
std::vector<std::byte> image; // In PNG
|
||||
std::optional<std::string> path;
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, application> list_applications();
|
||||
std::unordered_map<std::string, application> list_applications(bool include_steam = true, bool load_icons = true);
|
||||
} // namespace wivrn
|
||||
|
||||
+192
-206
@@ -18,11 +18,10 @@
|
||||
|
||||
#include "load_icon.h"
|
||||
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
#include <cairo.h>
|
||||
#include <fstream>
|
||||
#include <librsvg/rsvg.h>
|
||||
#include <limits>
|
||||
#include <png.h>
|
||||
#include <span>
|
||||
#include <unordered_map>
|
||||
@@ -45,7 +44,7 @@ std::vector<std::byte> load_file(const std::filesystem::path & filename)
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
T read(std::span<const std::byte> & buffer)
|
||||
T read(std::span<std::byte> & buffer)
|
||||
{
|
||||
if (sizeof(T) > buffer.size())
|
||||
throw std::runtime_error{"File truncated"};
|
||||
@@ -64,22 +63,27 @@ T read(std::span<const std::byte> & buffer)
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T>
|
||||
std::span<const T> read(std::span<const std::byte> & buffer, size_t count)
|
||||
std::span<T> read(std::span<std::byte> & buffer, size_t count)
|
||||
{
|
||||
if (count * sizeof(T) > buffer.size())
|
||||
throw std::runtime_error{"File truncated"};
|
||||
|
||||
std::span<const T> value{reinterpret_cast<const T *>(buffer.data()), count};
|
||||
std::span<T> value{reinterpret_cast<T *>(buffer.data()), count};
|
||||
buffer = buffer.subspan(count * sizeof(T));
|
||||
return value;
|
||||
}
|
||||
|
||||
std::vector<wivrn::icon> try_load_svg(const std::vector<std::byte> & data, int size)
|
||||
std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size)
|
||||
{
|
||||
// TODO check if aspect ratio needs to be handled, dpi
|
||||
|
||||
struct deleter
|
||||
{
|
||||
void operator()(GFile * ptr)
|
||||
{
|
||||
g_object_unref(ptr);
|
||||
}
|
||||
|
||||
void operator()(RsvgHandle * ptr)
|
||||
{
|
||||
g_object_unref(ptr);
|
||||
@@ -96,15 +100,15 @@ std::vector<wivrn::icon> try_load_svg(const std::vector<std::byte> & data, int s
|
||||
}
|
||||
};
|
||||
|
||||
using GFile_ptr = std::unique_ptr<GFile, deleter>;
|
||||
using RsvgHandle_ptr = std::unique_ptr<RsvgHandle, deleter>;
|
||||
using cairo_surface_t_ptr = std::unique_ptr<cairo_surface_t, deleter>;
|
||||
using cairo_t_ptr = std::unique_ptr<cairo_t, deleter>;
|
||||
|
||||
GError * error{};
|
||||
GFile_ptr file{g_file_new_for_path(filename.c_str())};
|
||||
|
||||
RsvgHandle_ptr handle{rsvg_handle_new_from_data((guint8 *)data.data(), data.size(), &error)};
|
||||
if (!handle)
|
||||
throw std::runtime_error{std::string{"Could not open: "} + error->message};
|
||||
RsvgHandle_ptr handle{rsvg_handle_new_from_gfile_sync(file.get(), RSVG_HANDLE_FLAGS_NONE, nullptr, &error)};
|
||||
|
||||
// Create a Cairo image surface and a rendering context for it
|
||||
cairo_surface_t_ptr surface{cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size)};
|
||||
@@ -122,7 +126,10 @@ std::vector<wivrn::icon> try_load_svg(const std::vector<std::byte> & data, int s
|
||||
};
|
||||
|
||||
if (!rsvg_handle_render_document(handle.get(), cr.get(), &viewport, &error))
|
||||
throw std::runtime_error{std::string{"Could not render: "} + error->message};
|
||||
{
|
||||
g_printerr("could not render: %s", error->message);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Write a PNG file
|
||||
std::vector<std::byte> png;
|
||||
@@ -136,83 +143,154 @@ std::vector<wivrn::icon> try_load_svg(const std::vector<std::byte> & data, int s
|
||||
return CAIRO_STATUS_SUCCESS;
|
||||
};
|
||||
|
||||
if (auto status = cairo_surface_write_to_png_stream(surface.get(), cb, &png); status != CAIRO_STATUS_SUCCESS)
|
||||
throw std::runtime_error{std::string{"Could not write output: "} + cairo_status_to_string(status)};
|
||||
if (cairo_surface_write_to_png_stream(surface.get(), cb, &png) != CAIRO_STATUS_SUCCESS)
|
||||
{
|
||||
g_printerr("could not write output file");
|
||||
return {};
|
||||
}
|
||||
|
||||
return {{size, size, 32, std::move(png)}};
|
||||
return png;
|
||||
}
|
||||
|
||||
std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
// 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;
|
||||
};
|
||||
|
||||
constexpr int rowstride(int width, int bpp)
|
||||
{
|
||||
int words_per_line = (width * bpp + 31) / 32;
|
||||
return words_per_line * 4;
|
||||
}
|
||||
static_assert(rowstride(1, 1) == 4);
|
||||
static_assert(rowstride(8, 1) == 4);
|
||||
static_assert(rowstride(16, 1) == 4);
|
||||
static_assert(rowstride(32, 1) == 4);
|
||||
static_assert(rowstride(33, 1) == 8);
|
||||
|
||||
static_assert(rowstride(1, 4) == 4);
|
||||
static_assert(rowstride(8, 4) == 4);
|
||||
static_assert(rowstride(16, 4) == 8);
|
||||
static_assert(rowstride(32, 4) == 16);
|
||||
static_assert(rowstride(33, 4) == 20);
|
||||
|
||||
static_assert(rowstride(1, 8) == 4);
|
||||
static_assert(rowstride(8, 8) == 8);
|
||||
static_assert(rowstride(16, 8) == 16);
|
||||
static_assert(rowstride(32, 8) == 32);
|
||||
static_assert(rowstride(33, 8) == 36);
|
||||
|
||||
static_assert(rowstride(1, 24) == 4);
|
||||
static_assert(rowstride(8, 24) == 24);
|
||||
static_assert(rowstride(16, 24) == 48);
|
||||
static_assert(rowstride(32, 24) == 96);
|
||||
static_assert(rowstride(33, 24) == 100);
|
||||
|
||||
static_assert(rowstride(1, 32) == 4);
|
||||
static_assert(rowstride(8, 32) == 32);
|
||||
static_assert(rowstride(16, 32) == 64);
|
||||
static_assert(rowstride(32, 32) == 128);
|
||||
static_assert(rowstride(33, 32) == 132);
|
||||
|
||||
std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size, int index = -1)
|
||||
{
|
||||
const uint32_t ICO_PNG_MAGIC = 0x474e5089;
|
||||
|
||||
// See https://gitlab.gnome.org/GNOME/gimp/-/tree/master/plug-ins/file-ico?ref_type=heads
|
||||
auto rowstride = [](int width, int bpp) {
|
||||
int words_per_line = (width * bpp + 31) / 32;
|
||||
return words_per_line * 4;
|
||||
};
|
||||
|
||||
static_assert(rowstride(1, 1) == 4);
|
||||
static_assert(rowstride(8, 1) == 4);
|
||||
static_assert(rowstride(16, 1) == 4);
|
||||
static_assert(rowstride(32, 1) == 4);
|
||||
static_assert(rowstride(33, 1) == 8);
|
||||
|
||||
static_assert(rowstride(1, 4) == 4);
|
||||
static_assert(rowstride(8, 4) == 4);
|
||||
static_assert(rowstride(16, 4) == 8);
|
||||
static_assert(rowstride(32, 4) == 16);
|
||||
static_assert(rowstride(33, 4) == 20);
|
||||
|
||||
static_assert(rowstride(1, 8) == 4);
|
||||
static_assert(rowstride(8, 8) == 8);
|
||||
static_assert(rowstride(16, 8) == 16);
|
||||
static_assert(rowstride(32, 8) == 32);
|
||||
static_assert(rowstride(33, 8) == 36);
|
||||
|
||||
static_assert(rowstride(1, 24) == 4);
|
||||
static_assert(rowstride(8, 24) == 24);
|
||||
static_assert(rowstride(16, 24) == 48);
|
||||
static_assert(rowstride(32, 24) == 96);
|
||||
static_assert(rowstride(33, 24) == 100);
|
||||
|
||||
static_assert(rowstride(1, 32) == 4);
|
||||
static_assert(rowstride(8, 32) == 32);
|
||||
static_assert(rowstride(16, 32) == 64);
|
||||
static_assert(rowstride(32, 32) == 128);
|
||||
static_assert(rowstride(33, 32) == 132);
|
||||
|
||||
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)
|
||||
throw std::runtime_error{"Cannot read icon"};
|
||||
|
||||
std::vector<wivrn::icon> icons;
|
||||
|
||||
for (int i = 0; i < icon_count; i++)
|
||||
try
|
||||
{
|
||||
auto width = read<uint8_t>(header);
|
||||
auto 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);
|
||||
auto bpp = read<uint16_t>(header);
|
||||
auto size = read<uint32_t>(header);
|
||||
auto offset = read<uint32_t>(header);
|
||||
auto ico = load_file(filename);
|
||||
|
||||
if (offset + size > ico.size())
|
||||
throw std::runtime_error{"Cannot read icon"};
|
||||
std::span header{ico};
|
||||
|
||||
auto data = std::span{ico}.subspan(offset, size);
|
||||
if (reserved != 0)
|
||||
throw std::runtime_error{"Cannot read icon"};
|
||||
if (planes != 0 and planes != 1)
|
||||
throw std::runtime_error{"Cannot read icon"};
|
||||
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);
|
||||
|
||||
if (offset + size > ico.size())
|
||||
return {};
|
||||
|
||||
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 {};
|
||||
|
||||
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 = nullptr;
|
||||
|
||||
// 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;
|
||||
}();
|
||||
|
||||
if (not best_entry)
|
||||
return {};
|
||||
|
||||
auto data = best_entry->data;
|
||||
auto magic = read<uint32_t>(data);
|
||||
|
||||
if (magic == ICO_PNG_MAGIC)
|
||||
@@ -230,21 +308,18 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
.format = PNG_FORMAT_BGRA,
|
||||
};
|
||||
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
|
||||
continue;
|
||||
// throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||
return {};
|
||||
|
||||
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
||||
|
||||
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
||||
{
|
||||
png_image_free(&png_ref);
|
||||
continue;
|
||||
// throw std::runtime_error{"Cannot read icon: " + filename.native()};
|
||||
return {};
|
||||
}
|
||||
|
||||
// TODO: get bit depth from PNG instead of trusting the ico header
|
||||
icons.emplace_back(png_ref.width, png_ref.height, bpp, std::move(png_data));
|
||||
png_image_free(&png_ref);
|
||||
|
||||
return png_data;
|
||||
}
|
||||
else if (magic == 40)
|
||||
{
|
||||
@@ -273,7 +348,7 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
auto w = width;
|
||||
auto h = height / 2;
|
||||
|
||||
std::span<const uint8_t> palette; // BGRX
|
||||
std::span<uint8_t> palette; // BGRX
|
||||
|
||||
if (bpp <= 16)
|
||||
{
|
||||
@@ -288,8 +363,8 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
size_t and_stride = rowstride(w, 1);
|
||||
size_t dst_stride = 4 * w;
|
||||
|
||||
std::span xor_map = read<uint8_t>(data, xor_stride * h);
|
||||
std::span and_map = read<uint8_t>(data, and_stride * h);
|
||||
std::span<uint8_t> xor_map = read<uint8_t>(data, xor_stride * h);
|
||||
std::span<uint8_t> and_map = read<uint8_t>(data, and_stride * h);
|
||||
|
||||
std::vector<uint8_t> dest_buffer;
|
||||
dest_buffer.resize(h * dst_stride);
|
||||
@@ -299,8 +374,8 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
case 1:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
const uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
const uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
uint8_t * src_and_row = 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++)
|
||||
@@ -327,8 +402,8 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
case 4:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
const uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
const uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
uint8_t * src_and_row = 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++)
|
||||
@@ -355,8 +430,8 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
case 8:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
const uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
const uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
uint8_t * src_and_row = 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++)
|
||||
@@ -383,8 +458,8 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
case 16:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
const uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
const uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
uint8_t * src_and_row = 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++)
|
||||
@@ -413,8 +488,8 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
case 24:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
const uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
const uint8_t * src_and_row = and_map.data() + and_stride * y;
|
||||
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
|
||||
uint8_t * src_and_row = 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++)
|
||||
@@ -437,7 +512,7 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
case 32:
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
const uint8_t * src_xor_row = xor_map.data() + xor_stride * 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);
|
||||
@@ -464,136 +539,47 @@ std::vector<wivrn::icon> try_load_ico(const std::vector<std::byte> & ico)
|
||||
else
|
||||
png_data.resize(png_size);
|
||||
|
||||
icons.emplace_back(w, h, bpp, std::move(png_data));
|
||||
return png_data;
|
||||
}
|
||||
}
|
||||
|
||||
return icons;
|
||||
}
|
||||
|
||||
std::vector<wivrn::icon> try_load_zip(const std::vector<std::byte> & data)
|
||||
{
|
||||
std::unique_ptr<archive, decltype(&archive_read_free)> zip{archive_read_new(), &archive_read_free};
|
||||
|
||||
archive_read_support_filter_all(zip.get());
|
||||
archive_read_support_format_all(zip.get());
|
||||
|
||||
if (auto rc = archive_read_open_memory(zip.get(), data.data(), data.size()); rc != ARCHIVE_OK)
|
||||
throw std::runtime_error{std::string("Cannot open ZIP: ") + archive_error_string(zip.get())};
|
||||
|
||||
archive_entry * entry;
|
||||
std::vector<wivrn::icon> icons;
|
||||
|
||||
while (archive_read_next_header(zip.get(), &entry) == ARCHIVE_OK)
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::vector<std::byte> buffer;
|
||||
buffer.resize(archive_entry_size(entry));
|
||||
|
||||
if (auto status = archive_read_data(zip.get(), buffer.data(), buffer.size()); status < 0)
|
||||
continue;
|
||||
|
||||
// Try reading the PNG
|
||||
std::vector<uint8_t> pixels;
|
||||
png_image png_ref{
|
||||
.version = PNG_IMAGE_VERSION,
|
||||
.format = PNG_FORMAT_BGRA,
|
||||
};
|
||||
|
||||
if (not png_image_begin_read_from_memory(&png_ref, buffer.data(), buffer.size()))
|
||||
{
|
||||
png_image_free(&png_ref);
|
||||
continue;
|
||||
}
|
||||
|
||||
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
||||
|
||||
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
||||
{
|
||||
png_image_free(&png_ref);
|
||||
continue;
|
||||
}
|
||||
|
||||
// PNG read successfully
|
||||
icons.emplace_back(png_ref.width, png_ref.height, 32, std::move(buffer)); // TODO: check how to get bit depth
|
||||
|
||||
png_image_free(&png_ref);
|
||||
}
|
||||
|
||||
return icons;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<wivrn::icon> try_load_png(const std::vector<std::byte> & png_data)
|
||||
{
|
||||
std::vector<wivrn::icon> icons;
|
||||
|
||||
// Try reading the PNG
|
||||
std::vector<uint8_t> pixels;
|
||||
png_image png_ref{
|
||||
.version = PNG_IMAGE_VERSION,
|
||||
.format = PNG_FORMAT_BGRA,
|
||||
};
|
||||
|
||||
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
|
||||
{
|
||||
png_image_free(&png_ref);
|
||||
throw std::runtime_error("");
|
||||
}
|
||||
|
||||
pixels.resize(PNG_IMAGE_SIZE(png_ref));
|
||||
|
||||
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
|
||||
{
|
||||
png_image_free(&png_ref);
|
||||
throw std::runtime_error("");
|
||||
}
|
||||
|
||||
// PNG read successfully
|
||||
icons.emplace_back(png_ref.width, png_ref.height, 32, std::move(png_data)); // TODO: check how to get bit depth
|
||||
|
||||
png_image_free(&png_ref);
|
||||
|
||||
return icons;
|
||||
}
|
||||
|
||||
std::unordered_map<std::filesystem::path, std::vector<wivrn::icon>> icon_cache;
|
||||
std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
|
||||
|
||||
} // namespace
|
||||
|
||||
const std::vector<wivrn::icon> & wivrn::load_icon(const std::filesystem::path & filename)
|
||||
const std::vector<std::byte> & wivrn::load_icon(const std::filesystem::path & filename, int size)
|
||||
{
|
||||
auto it = icon_cache.find(filename);
|
||||
if (it != icon_cache.end() and not it->second.empty())
|
||||
if (it != icon_cache.end())
|
||||
return it->second;
|
||||
|
||||
auto data = load_file(filename);
|
||||
std::vector<std::byte> icon;
|
||||
|
||||
try
|
||||
if (filename.extension() == ".svg")
|
||||
{
|
||||
return icon_cache.emplace(filename, try_load_svg(data, 256)).first->second;
|
||||
icon = load_svg(filename, size);
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
|
||||
try
|
||||
else if (filename.extension() == ".ico")
|
||||
{
|
||||
return icon_cache.emplace(filename, try_load_ico(data)).first->second;
|
||||
// Steam icon (Windows)
|
||||
icon = load_ico(filename, size);
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
|
||||
try
|
||||
else if (filename.extension() == ".zip")
|
||||
{
|
||||
return icon_cache.emplace(filename, try_load_zip(data)).first->second;
|
||||
// Steam icon (Linux)
|
||||
// TODO
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
|
||||
try
|
||||
else
|
||||
{
|
||||
return icon_cache.emplace(filename, try_load_png(data)).first->second;
|
||||
// TODO read arbitrary format / rewrite as png
|
||||
icon = load_file(filename);
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
|
||||
throw std::runtime_error{"Cannot load icon: " + filename.native()};
|
||||
return icon_cache.emplace(filename, std::move(icon)).first->second;
|
||||
}
|
||||
|
||||
@@ -23,13 +23,5 @@
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
struct icon
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
int bpp;
|
||||
std::vector<std::byte> png_data;
|
||||
};
|
||||
|
||||
const std::vector<icon> & load_icon(const std::filesystem::path & filename);
|
||||
} // namespace wivrn
|
||||
const std::vector<std::byte> & load_icon(const std::filesystem::path & filename, int size);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
#include "steam_app_info.h"
|
||||
#include <bit>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <span>
|
||||
@@ -90,8 +89,6 @@ void read_vdf(wivrn::steam_app_info::info & info, const std::string & prefix, st
|
||||
while (type != 8)
|
||||
{
|
||||
std::string name{string_table.at(read<uint32_t>(bindata))};
|
||||
for (char & c: name)
|
||||
c = std::tolower(c);
|
||||
|
||||
// https://github.com/ValveResourceFormat/ValveKeyValue/blob/master/ValveKeyValue/ValveKeyValue/KeyValues1/KV1BinaryNodeType.cs
|
||||
switch (type)
|
||||
|
||||
@@ -131,12 +131,6 @@ public:
|
||||
std::tie(resource, allocation) = traits::create(device, create_info, alloc_info);
|
||||
|
||||
vmaSetAllocationName(vk_allocator::instance(), allocation, name.c_str());
|
||||
if (vk_allocator::instance().has_debug_utils)
|
||||
device.setDebugUtilsObjectNameEXT(vk::DebugUtilsObjectNameInfoEXT{
|
||||
.objectType = T::objectType,
|
||||
.objectHandle = uint64_t(CType(*resource)),
|
||||
.pObjectName = name.c_str(),
|
||||
});
|
||||
}
|
||||
|
||||
basic_allocation(const basic_allocation &) = delete;
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.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 <array>
|
||||
#include <cstdint>
|
||||
#include <tuple>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
template <typename... Args>
|
||||
class specialization_constants
|
||||
{
|
||||
std::tuple<Args...> values;
|
||||
std::array<vk::SpecializationMapEntry, sizeof...(Args)> entries;
|
||||
vk::SpecializationInfo info_;
|
||||
|
||||
specialization_constants(specialization_constants &&) = delete;
|
||||
specialization_constants(const specialization_constants &) = delete;
|
||||
specialization_constants & operator=(const specialization_constants &) = delete;
|
||||
specialization_constants & operator=(specialization_constants &&) = delete;
|
||||
|
||||
template <uint32_t i = sizeof...(Args) - 1>
|
||||
void fill_entries()
|
||||
{
|
||||
entries[i] = vk::SpecializationMapEntry{
|
||||
.constantID = i,
|
||||
.offset = uint32_t(uintptr_t(std::addressof(std::get<i>(values))) - uintptr_t(std::addressof(values))),
|
||||
.size = sizeof(std::get<i>(values)),
|
||||
};
|
||||
if constexpr (i > 0)
|
||||
fill_entries<i - 1>();
|
||||
}
|
||||
|
||||
public:
|
||||
specialization_constants(Args &&... args) :
|
||||
values(std::forward<Args>(args)...),
|
||||
info_{
|
||||
.mapEntryCount = uint32_t(entries.size()),
|
||||
.pMapEntries = entries.data(),
|
||||
.dataSize = sizeof(values),
|
||||
.pData = &values,
|
||||
}
|
||||
{
|
||||
fill_entries();
|
||||
}
|
||||
|
||||
operator vk::SpecializationInfo *()
|
||||
{
|
||||
return &info_;
|
||||
}
|
||||
|
||||
vk::SpecializationInfo * info()
|
||||
{
|
||||
return &info_;
|
||||
}
|
||||
|
||||
template <uint32_t i>
|
||||
auto & data()
|
||||
{
|
||||
return std::get<i>(values);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
specialization_constants<Args...> make_specialization_constants(Args &&... args)
|
||||
{
|
||||
return specialization_constants<Args...>(std::forward<Args>(args)...);
|
||||
}
|
||||
@@ -20,8 +20,7 @@
|
||||
#include "vk_allocator.h"
|
||||
#include "vk/check.h"
|
||||
|
||||
vk_allocator::vk_allocator(const VmaAllocatorCreateInfo & info, bool has_debug_utils) :
|
||||
has_debug_utils(has_debug_utils)
|
||||
vk_allocator::vk_allocator(const VmaAllocatorCreateInfo & info)
|
||||
{
|
||||
CHECK_VK(vmaCreateAllocator(&info, &handle));
|
||||
}
|
||||
|
||||
@@ -27,8 +27,7 @@ class vk_allocator : public singleton<vk_allocator>
|
||||
VmaAllocator handle = nullptr;
|
||||
|
||||
public:
|
||||
const bool has_debug_utils;
|
||||
vk_allocator(const VmaAllocatorCreateInfo &, bool has_debug_utils);
|
||||
vk_allocator(const VmaAllocatorCreateInfo &);
|
||||
~vk_allocator();
|
||||
|
||||
operator VmaAllocator()
|
||||
|
||||
+3
-14
@@ -41,7 +41,7 @@ namespace wivrn
|
||||
// Default port for server to listen, both TCP and UDP
|
||||
static const int default_port = 9757;
|
||||
|
||||
static constexpr int protocol_revision = 1;
|
||||
static constexpr int protocol_revision = 2;
|
||||
|
||||
enum class device_id : uint8_t
|
||||
{
|
||||
@@ -239,11 +239,6 @@ struct headset_info_packet
|
||||
uint32_t num_generic_trackers;
|
||||
std::vector<video_codec> supported_codecs; // from preferred to least preferred
|
||||
std::string system_name;
|
||||
|
||||
// Used for the application list
|
||||
std::string language;
|
||||
std::string country;
|
||||
std::string variant;
|
||||
};
|
||||
|
||||
struct handshake
|
||||
@@ -680,16 +675,11 @@ struct application_list
|
||||
{
|
||||
std::string id;
|
||||
std::string name;
|
||||
std::vector<std::byte> image; // In PNG
|
||||
};
|
||||
std::vector<application> applications;
|
||||
};
|
||||
|
||||
struct application_icon
|
||||
{
|
||||
std::string id;
|
||||
std::vector<std::byte> image; // In PNG
|
||||
};
|
||||
|
||||
using packets = std::variant<
|
||||
crypto_handshake,
|
||||
pin_check_2,
|
||||
@@ -703,7 +693,6 @@ using packets = std::variant<
|
||||
timesync_query,
|
||||
tracking_control,
|
||||
refresh_rate_change,
|
||||
application_list,
|
||||
application_icon>;
|
||||
application_list>;
|
||||
} // namespace to_headset
|
||||
} // namespace wivrn
|
||||
|
||||
@@ -69,9 +69,10 @@ wivrn::fd_base::~fd_base()
|
||||
wivrn::UDP::UDP()
|
||||
{
|
||||
fd = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
|
||||
if (fd < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
}
|
||||
|
||||
wivrn::UDP::UDP(int fd)
|
||||
@@ -176,9 +177,10 @@ wivrn::TCP::TCP(int fd)
|
||||
wivrn::TCP::TCP(in6_addr address, int port)
|
||||
{
|
||||
fd = socket(AF_INET6, SOCK_STREAM, 0);
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
|
||||
if (fd < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
|
||||
sockaddr_in6 sa;
|
||||
sa.sin6_family = AF_INET6;
|
||||
@@ -197,9 +199,10 @@ wivrn::TCP::TCP(in6_addr address, int port)
|
||||
wivrn::TCP::TCP(in_addr address, int port)
|
||||
{
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
|
||||
if (fd < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
|
||||
sockaddr_in sa;
|
||||
sa.sin_family = AF_INET;
|
||||
|
||||
Reference in New Issue
Block a user