Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Vindicus999
2025-08-09 17:12:50 +00:00
11 changed files with 500 additions and 400 deletions
+4 -4
View File
@@ -33,7 +33,7 @@ jobs:
- name: Install dependencies
run: >
sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libavahi-glib-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev libboost-all-dev librsvg2-bin libssl-dev libnotify-dev librsvg2-dev
sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libavahi-glib-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev libboost-all-dev librsvg2-bin libssl-dev libnotify-dev librsvg2-dev libarchive-dev
- name: Prepare Vulkan SDK
uses: humbletim/setup-vulkan-sdk@286ed76c72de45dbf50c515779cf4ca2f6aa3944
with:
@@ -87,7 +87,7 @@ jobs:
- name: Install dependencies
run: >
apt-get update && apt-get install --yes libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev qt6-base-dev qt6-tools-dev libcap-dev pkexec libboost-all-dev librsvg2-bin libssl-dev libnotify-dev qt6-declarative-dev extra-cmake-modules libkirigami-dev libkf6i18n-dev libkf6coreaddons-dev libkf6qqc2desktopstyle-dev libkf6iconthemes-dev jq sudo cmake gcc g++ git qcoro-qt6-dev librsvg2-dev
apt-get update && apt-get install --yes libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev qt6-base-dev qt6-tools-dev libcap-dev pkexec libboost-all-dev librsvg2-bin libssl-dev libnotify-dev qt6-declarative-dev extra-cmake-modules libkirigami-dev libkf6i18n-dev libkf6coreaddons-dev libkf6qqc2desktopstyle-dev libkf6iconthemes-dev jq sudo cmake gcc g++ git qcoro-qt6-dev librsvg2-dev libarchive-dev
# runtime: qml6-module-qtqml-workerscript qml6-module-qtquick-dialogs
- name: Prepare Vulkan SDK
uses: humbletim/setup-vulkan-sdk@286ed76c72de45dbf50c515779cf4ca2f6aa3944
@@ -192,7 +192,7 @@ jobs:
- name: install server dependencies
run: >
sudo apt-get update && sudo apt-get install libavahi-glib-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev qt6-base-dev qt6-tools-dev libcap-dev pkexec libnotify-dev librsvg2-dev
sudo apt-get update && sudo apt-get install libavahi-glib-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev qt6-base-dev qt6-tools-dev libcap-dev pkexec libnotify-dev librsvg2-dev libarchive-dev
- name: Configure CMake
run: >
@@ -467,7 +467,7 @@ jobs:
- name: Install dependencies
run: |
apt update
apt install --yes libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libavahi-glib-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev qt6-base-dev qt6-tools-dev libcap-dev pkexec libboost-all-dev librsvg2-bin dpkg-dev pkg-config debhelper-compat build-essential lintian cmake libnotify-dev qt6-declarative-dev extra-cmake-modules libkirigami-dev libkf6i18n-dev libkf6coreaddons-dev libkf6qqc2desktopstyle-dev libkf6iconthemes-dev jq sudo cmake gcc g++ git wget libssl-dev qcoro-qt6-dev librsvg2-dev
apt install --yes libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libavahi-glib-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev qt6-base-dev qt6-tools-dev libcap-dev pkexec libboost-all-dev librsvg2-bin dpkg-dev pkg-config debhelper-compat build-essential lintian cmake libnotify-dev qt6-declarative-dev extra-cmake-modules libkirigami-dev libkf6i18n-dev libkf6coreaddons-dev libkf6qqc2desktopstyle-dev libkf6iconthemes-dev jq sudo cmake gcc g++ git wget libssl-dev qcoro-qt6-dev librsvg2-dev libarchive-dev
- uses: actions/checkout@v4
with:
+1
View File
@@ -185,6 +185,7 @@ endif()
if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER)
pkg_check_modules(librsvg-2.0 REQUIRED IMPORTED_TARGET librsvg-2.0)
find_package(LibArchive)
find_package(PNG)
endif()
+118 -83
View File
@@ -383,9 +383,18 @@ void scenes::lobby::connect(const configuration::server_data & data)
data.manual);
}
std::optional<glm::vec3> scenes::lobby::check_recenter_gesture(const std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & joints)
std::optional<glm::vec3> scenes::lobby::check_recenter_gesture(xr::spaces space, const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & joints)
{
const auto & palm = joints[XR_HAND_JOINT_PALM_EXT].first;
if (recentering_context and std::get<0>(*recentering_context) != space)
return std::nullopt;
if (not joints)
{
recentering_context.reset();
return std::nullopt;
}
const auto & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first;
const auto & o = palm.pose.orientation;
const auto & p = palm.pose.position;
glm::quat q{o.w, o.x, o.y, o.z};
@@ -393,102 +402,130 @@ std::optional<glm::vec3> scenes::lobby::check_recenter_gesture(const std::array<
if (glm::dot(q * glm::vec3(0, 1, 0), glm::vec3(0, -1, 0)) > constants::lobby::recenter_cosangle_min)
{
recentering_context.emplace(space, glm::vec3{}, 0);
return v + glm::vec3(0, constants::lobby::recenter_distance_up, 0) + q * glm::vec3(0, 0, -constants::lobby::recenter_distance_front);
}
recentering_context.reset();
return std::nullopt;
}
std::optional<glm::vec3> scenes::lobby::check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position)
{
std::optional<std::pair<glm::vec3, glm::quat>> aim;
glm::vec3 offset_position;
glm::quat offset_orientation;
xr::spaces controller;
glm::vec3 recenter_position;
float recenter_distance;
if (application::read_action_bool(recenter_left_action).value_or(std::pair{0, false}).second)
if (recentering_context)
{
aim = application::locate_controller(application::space(xr::spaces::aim_left), application::space(xr::spaces::world), predicted_display_time);
std::tie(offset_position, offset_orientation) = input->offset[xr::spaces::aim_left];
}
if (application::read_action_bool(recenter_right_action).value_or(std::pair{0, false}).second)
{
aim = application::locate_controller(application::space(xr::spaces::aim_right), application::space(xr::spaces::world), predicted_display_time);
std::tie(offset_position, offset_orientation) = input->offset[xr::spaces::aim_right];
}
if (aim)
{
// Handle controller offset
aim->first = aim->first + glm::mat3_cast(aim->second * offset_orientation) * offset_position;
aim->second = aim->second * offset_orientation;
if (not gui_recenter_position or not gui_recenter_distance)
controller = std::get<0>(*recentering_context);
bool state;
switch (controller)
{
// First frame where the recenter action is active
imgui_context::controller_state state{
.active = true,
.aim_position = aim->first,
.aim_orientation = aim->second,
};
case xr::spaces::aim_left:
state = application::read_action_bool(recenter_left_action).value_or(std::pair{0, false}).second;
break;
imgui_ctx->compute_pointer_position(state);
case xr::spaces::aim_right:
state = application::read_action_bool(recenter_right_action).value_or(std::pair{0, false}).second;
break;
if (state.pointer_position) // TODO: check that the pointer is inside an imgui window
{
auto M = glm::mat3_cast(imgui_ctx->layers()[0].orientation);
default:
return std::nullopt;
}
// Pointer position in world
glm::vec3 world_pointer_position = imgui_ctx->rw_from_vp(*state.pointer_position);
if (not state)
{
recentering_context.reset();
return std::nullopt;
}
}
else if (auto state = application::read_action_bool(recenter_left_action); state and state->second)
controller = xr::spaces::aim_left;
else if (auto state = application::read_action_bool(recenter_right_action); state and state->second)
controller = xr::spaces::aim_right;
else
return std::nullopt;
// Pointer position in GUI
gui_recenter_position = glm::transpose(M) * (world_pointer_position - imgui_ctx->layers()[0].position);
gui_recenter_distance = glm::length(state.aim_position - world_pointer_position);
}
else
{
gui_recenter_position = glm::vec3(0, 0, 0);
gui_recenter_distance = constants::lobby::recenter_action_distance;
}
auto aim = application::locate_controller(application::space(controller), application::space(xr::spaces::world), predicted_display_time);
if (not aim)
{
// The controller cannot be located
recentering_context.reset();
return std::nullopt;
}
// Handle controller offset
auto [offset_position, offset_orientation] = input->offset[controller];
aim->first = aim->first + glm::mat3_cast(aim->second * offset_orientation) * offset_position;
aim->second = aim->second * offset_orientation;
if (not recentering_context)
{
// First frame of recentering
imgui_context::controller_state state{
.active = true,
.aim_position = aim->first,
.aim_orientation = aim->second,
};
imgui_ctx->compute_pointer_position(state);
if (state.pointer_position) // TODO: check that the pointer is inside an imgui window
{
auto M = glm::mat3_cast(imgui_ctx->layers()[0].orientation);
// Pointer position in world
glm::vec3 world_pointer_position = imgui_ctx->rw_from_vp(*state.pointer_position);
// Pointer position in GUI
recenter_position = glm::transpose(M) * (world_pointer_position - imgui_ctx->layers()[0].position);
recenter_distance = glm::length(state.aim_position - world_pointer_position);
}
else
{
// Subsequent frames: find the GUI position that gives the correct world pointer position
glm::vec3 controller_direction = -glm::column(glm::mat3_cast(aim->second), 2);
glm::vec3 wanted_world_pointer_position = aim->first + controller_direction * *gui_recenter_distance;
// I'm sure there's an analytical solution but I can't be bothered to write it so
// let's use a gradient descent instead
auto f = [&](glm::vec3 new_gui_position) {
auto q = compute_gui_orientation(head_position, new_gui_position);
auto M = glm::mat3_cast(q); // plane-to-world transform
glm::vec3 world_pointer_position = new_gui_position + M * *gui_recenter_position;
return world_pointer_position - wanted_world_pointer_position;
};
// One step is usually enough, the solution will continuously improve in the next frames
glm::vec3 gui_position = imgui_ctx->layers()[0].position;
float eps = 0.01;
glm::vec3 obj = f(gui_position);
glm::vec3 obj_dx = (f(gui_position + glm::vec3(eps, 0, 0)) - obj) / eps;
glm::vec3 obj_dy = (f(gui_position + glm::vec3(0, eps, 0)) - obj) / eps;
glm::vec3 obj_dz = (f(gui_position + glm::vec3(0, 0, eps)) - obj) / eps;
glm::mat3 jacobian{obj_dx, obj_dy, obj_dz};
gui_position -= glm::inverse(jacobian) * obj;
return gui_position;
// Use the center of the GUI if the controller points outside of the GUI
recenter_position = glm::vec3(0, 0, 0);
recenter_distance = constants::lobby::recenter_action_distance;
}
recentering_context.emplace(controller, recenter_position, recenter_distance);
return std::nullopt;
}
else
{
gui_recenter_position.reset();
}
// Subsequent frames: find the GUI position that gives the correct world pointer position
std::tie(controller, recenter_position, recenter_distance) = *recentering_context;
return std::nullopt;
glm::vec3 controller_direction = -glm::column(glm::mat3_cast(aim->second), 2);
glm::vec3 wanted_world_pointer_position = aim->first + controller_direction * recenter_distance;
// I'm sure there's an analytical solution but I can't be bothered to write it so
// let's use a gradient descent instead
auto f = [&](glm::vec3 new_gui_position) {
auto q = compute_gui_orientation(head_position, new_gui_position);
auto M = glm::mat3_cast(q); // plane-to-world transform
glm::vec3 world_pointer_position = new_gui_position + M * recenter_position;
return world_pointer_position - wanted_world_pointer_position;
};
// One step is usually enough, the solution will continuously improve in the next frames
glm::vec3 gui_position = imgui_ctx->layers()[0].position;
float eps = 0.01;
glm::vec3 obj = f(gui_position);
glm::vec3 obj_dx = (f(gui_position + glm::vec3(eps, 0, 0)) - obj) / eps;
glm::vec3 obj_dy = (f(gui_position + glm::vec3(0, eps, 0)) - obj) / eps;
glm::vec3 obj_dz = (f(gui_position + glm::vec3(0, 0, eps)) - obj) / eps;
glm::mat3 jacobian{obj_dx, obj_dy, obj_dz};
gui_position -= glm::inverse(jacobian) * obj;
return gui_position;
}
}
std::optional<glm::vec3> scenes::lobby::check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation)
@@ -620,18 +657,16 @@ void scenes::lobby::render(const XrFrameState & frame_state)
hand_model::apply(world, left, right);
if (left and xr::hand_tracker::check_flags(*left, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0))
{
hide_left_controller = true;
if (!new_gui_position)
new_gui_position = check_recenter_gesture(*left);
}
if (right and xr::hand_tracker::check_flags(*right, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0))
{
hide_right_controller = true;
if (!new_gui_position)
new_gui_position = check_recenter_gesture(*right);
}
if (not new_gui_position)
new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left);
if (not new_gui_position)
new_gui_position = check_recenter_gesture(xr::spaces::palm_right, right);
}
if (head_position && new_gui_position)
+5 -3
View File
@@ -95,8 +95,10 @@ class lobby : public scene_impl<lobby>
XrAction recenter_left_action = XR_NULL_HANDLE;
XrAction recenter_right_action = XR_NULL_HANDLE;
std::optional<glm::vec3> gui_recenter_position;
std::optional<float> gui_recenter_distance;
// std::optional<glm::vec3> gui_recenter_position;
// std::optional<float> gui_recenter_distance;
// Which controller is used for recentering, position of the pointed point in the GUI, in GUI axes, and distance between the controller and the pointed point during recentering
std::optional<std::tuple<xr::spaces, glm::vec3, float>> recentering_context;
bool recenter_gui = true;
void move_gui(glm::vec3 head_position, glm::vec3 new_gui_position);
@@ -154,7 +156,7 @@ class lobby : public scene_impl<lobby>
void connect(const configuration::server_data & data);
std::unique_ptr<wivrn_session> connect_to_session(wivrn_discover::service service, bool manual_connection);
std::optional<glm::vec3> check_recenter_gesture(const std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & joints);
std::optional<glm::vec3> check_recenter_gesture(xr::spaces space, const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & joints);
std::optional<glm::vec3> check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position);
std::optional<glm::vec3> check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation);
+1 -1
View File
@@ -462,7 +462,7 @@ void scenes::lobby::gui_connected(XrTime predicted_display_time)
}();
ImGui::PushStyleColor(ImGuiCol_Button, 0);
if (icon(app.name + "##" + app.id, texture, {256, 256}, 0, {icon_width, 0}))
if (icon(app.name + "##" + app.id, texture, {256, 256}, ImGuiButtonFlags_PressedOnClickRelease, {icon_width, 0}))
{
timestamp_start_application = predicted_display_time;
next_scene->start_application(app.id);
+1 -1
View File
@@ -50,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 PUBLIC wivrn-common-base)
target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 LibArchive::LibArchive PUBLIC wivrn-common-base)
if (WIVRN_BUILD_TEST)
add_executable(list-apps
+3 -3
View File
@@ -84,7 +84,7 @@ steam_app_info read_steam_app_info(const std::filesystem::path & root)
return {};
}
std::filesystem::path find_steam_icon(const std::filesystem::path & root, int app_id, const steam_app_info & info)
std::optional<std::filesystem::path> find_steam_icon(const std::filesystem::path & root, int app_id, const steam_app_info & info)
{
const auto & app_info = info.get(app_id);
@@ -102,7 +102,7 @@ std::filesystem::path find_steam_icon(const std::filesystem::path & root, int ap
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");
@@ -113,7 +113,7 @@ std::filesystem::path find_steam_icon(const std::filesystem::path & root, int ap
{
}
return {};
return std::nullopt;
}
void read_steam_vr_apps(std::unordered_map<std::string, application> & res)
+351 -300
View File
@@ -18,6 +18,8 @@
#include "load_icon.h"
#include <archive.h>
#include <archive_entry.h>
#include <cairo.h>
#include <fstream>
#include <librsvg/rsvg.h>
@@ -126,10 +128,7 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
};
if (!rsvg_handle_render_document(handle.get(), cr.get(), &viewport, &error))
{
g_printerr("could not render: %s", error->message);
return {};
}
throw std::runtime_error{std::string{"Could not render: "} + error->message};
// Write a PNG file
std::vector<std::byte> png;
@@ -143,11 +142,8 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
return CAIRO_STATUS_SUCCESS;
};
if (cairo_surface_write_to_png_stream(surface.get(), cb, &png) != CAIRO_STATUS_SUCCESS)
{
g_printerr("could not write output file");
return {};
}
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)};
return png;
}
@@ -201,352 +197,407 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
{
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)
throw std::runtime_error{"Cannot read icon: " + filename.native()};
std::vector<ico_file_entry> entries;
int max_bpp = 0;
for (int i = 0; i < icon_count; i++)
{
auto ico = load_file(filename);
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);
std::span header{ico};
if (offset + size > ico.size())
throw std::runtime_error{"Cannot read icon: " + filename.native()};
auto reserved = read<uint16_t>(header);
auto resource_type = read<uint16_t>(header);
auto icon_count = read<uint16_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)
throw std::runtime_error{"Cannot read icon: " + filename.native()};
if (planes != 0 and planes != 1)
throw std::runtime_error{"Cannot read icon: " + filename.native()};
if (reserved != 0 or resource_type != 1)
return {};
entries.push_back(entry);
std::vector<ico_file_entry> entries;
int max_bpp = 0;
max_bpp = std::max(max_bpp, entry.bpp);
}
for (int i = 0; i < icon_count; i++)
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)
{
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 (entry.bpp != max_bpp)
continue;
if (offset + size > ico.size())
return {};
if (entry.width % size != 0 or entry.height % size != 0)
continue;
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);
if (std::min(entry.width, entry.height) / size < criterion)
{
criterion = std::min(entry.width, entry.height);
best_entry = &entry;
}
}
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;
}
}
if (best_entry)
return best_entry;
}();
if (not 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)
{
// PNG file
std::vector<std::byte> png_data;
// The PNG magic has already been consumed, put it back
png_data.insert(png_data.end(), data.begin() - 4, data.end());
// Check if the PNG is valid
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()))
throw std::runtime_error{"Cannot read icon: " + filename.native()};
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{"Cannot read icon: " + filename.native()};
}
png_image_free(&png_ref);
return png_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 {};
auto data = best_entry->data;
auto magic = read<uint32_t>(data);
if (bpp != 1 and
bpp != 4 and
bpp != 8 and
bpp != 16 and
bpp != 24 and
bpp != 32)
return {};
if (magic == ICO_PNG_MAGIC)
auto w = width;
auto h = height / 2;
std::span<uint8_t> palette; // BGRX
if (bpp <= 16)
{
// PNG file
std::vector<std::byte> png_data;
// Load the palette
if (used_colors == 0)
used_colors = 1 << bpp;
// The PNG magic has already been consumed, put it back
png_data.insert(png_data.end(), data.begin() - 4, data.end());
// Check if the PNG is valid
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()))
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);
return {};
}
png_image_free(&png_ref);
return png_data;
palette = read<uint8_t>(data, used_colors * 4);
}
else if (magic == 40)
size_t xor_stride = rowstride(w, bpp);
size_t and_stride = rowstride(w, 1);
size_t dst_stride = 4 * w;
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);
switch (bpp)
{
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);
case 1:
for (int y = 0; y < h; 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);
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;
std::span<uint8_t> palette; // BGRX
if (bpp <= 16)
{
// Load the palette
if (used_colors == 0)
used_colors = 1 << bpp;
palette = read<uint8_t>(data, used_colors * 4);
}
size_t xor_stride = rowstride(w, bpp);
size_t and_stride = rowstride(w, 1);
size_t dst_stride = 4 * w;
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);
switch (bpp)
{
case 1:
for (int y = 0; y < h; y++)
for (int x = 0, i = 0; x < w; x++)
{
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);
int colour = (src_xor_row[x / 8] >> (7 - (x % 8))) & 0x1;
if (colour >= used_colors)
return {};
for (int x = 0, i = 0; x < w; x++)
{
int colour = (src_xor_row[x / 8] >> (7 - (x % 8))) & 0x1;
if (colour >= used_colors)
return {};
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
// 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;
// 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;
}
*dst_row++ = b;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
break;
}
break;
case 4:
for (int y = 0; y < h; y++)
case 4:
for (int y = 0; y < h; 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++)
{
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);
int colour = (src_xor_row[x / 2] >> (4 - 4 * (x % 2))) & 0xf;
if (colour >= used_colors)
return {};
for (int x = 0, i = 0; x < w; x++)
{
int colour = (src_xor_row[x / 2] >> (4 - 4 * (x % 2))) & 0xf;
if (colour >= used_colors)
return {};
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
// 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;
// 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;
}
*dst_row++ = b;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
break;
}
break;
case 8:
for (int y = 0; y < h; y++)
case 8:
for (int y = 0; y < h; 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++)
{
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);
int colour = *src_xor_row++;
if (colour >= used_colors)
return {};
for (int x = 0, i = 0; x < w; x++)
{
int colour = *src_xor_row++;
if (colour >= used_colors)
return {};
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
// 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;
// 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;
}
*dst_row++ = b;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
break;
}
break;
case 16:
for (int y = 0; y < h; y++)
case 16:
for (int y = 0; y < h; 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++)
{
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);
int colour = (src_xor_row[1] << 8) | src_xor_row[0];
src_xor_row += 2;
for (int x = 0, i = 0; x < w; x++)
{
int colour = (src_xor_row[1] << 8) | src_xor_row[0];
src_xor_row += 2;
if (colour >= used_colors)
return {};
if (colour >= used_colors)
return {};
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
uint8_t b = palette[4 * colour + 0]; // Blue
uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
// 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;
// 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;
}
*dst_row++ = b;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
break;
}
break;
case 24:
for (int y = 0; y < h; y++)
case 24:
for (int y = 0; y < h; 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++)
{
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);
uint8_t b = *src_xor_row++; // Blue
uint8_t g = *src_xor_row++; // Green
uint8_t r = *src_xor_row++; // Red
for (int x = 0, i = 0; x < w; x++)
{
uint8_t b = *src_xor_row++; // Blue
uint8_t g = *src_xor_row++; // Green
uint8_t r = *src_xor_row++; // Red
// 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;
// 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;
}
*dst_row++ = b;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
break;
}
break;
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);
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;
}
memcpy(dst_row, src_xor_row, dst_stride);
}
break;
}
png_image png{
.version = PNG_IMAGE_VERSION,
.width = w,
.height = h,
.format = PNG_FORMAT_BGRA,
};
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();
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);
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)
{
else
png_data.resize(png_size);
return png_data;
}
return {};
throw std::runtime_error{"Wrong magic number in " + filename.native()};
}
std::vector<std::byte> load_zip(const std::filesystem::path & filename, int size)
{
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_filename(zip.get(), filename.c_str(), 1'000'000); rc != ARCHIVE_OK)
throw std::runtime_error{"Cannot open " + filename.native() + ": " + archive_error_string(zip.get())};
archive_entry * entry;
int width = 0;
int height = 0;
std::vector<std::byte> png;
while (archive_read_next_header(zip.get(), &entry) == ARCHIVE_OK)
{
printf("%s\n", archive_entry_pathname(entry));
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;
}
// Keep the largest icon only
if (png_ref.width * png_ref.height < width * height)
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: keep it until we find a better one
width = png_ref.width;
height = png_ref.height;
png = std::move(buffer);
png_image_free(&png_ref);
}
return png;
}
std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
@@ -556,7 +607,7 @@ std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
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())
if (it != icon_cache.end() and not it->second.empty())
return it->second;
std::vector<std::byte> icon;
@@ -573,7 +624,7 @@ const std::vector<std::byte> & wivrn::load_icon(const std::filesystem::path & fi
else if (filename.extension() == ".zip")
{
// Steam icon (Linux)
// TODO
icon = load_zip(filename, size);
}
else
{
+3
View File
@@ -18,6 +18,7 @@
#include "steam_app_info.h"
#include <bit>
#include <cctype>
#include <cstring>
#include <fstream>
#include <span>
@@ -89,6 +90,8 @@ 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)
+1
View File
@@ -19,6 +19,7 @@
pkgs.librsvg
pkgs.libpng
pkgs.libarchive
];
package = pkgs.enableDebugging (pkgs.wivrn.overrideAttrs (finalAttrs: oldAttrs: {
+12 -5
View File
@@ -759,12 +759,19 @@ void wivrn_session::operator()(from_headset::get_application_list && request)
{
if (app.icon_path)
{
auto icon = load_icon(*app.icon_path, 256);
try
{
auto icon = load_icon(*app.icon_path, 256);
send_control(to_headset::application_icon{
.id = id,
.image = std::move(icon),
});
send_control(to_headset::application_icon{
.id = id,
.image = std::move(icon),
});
}
catch (std::exception & e)
{
U_LOG_W("Error loading icon %s: %s", app.icon_path->c_str(), e.what());
}
}
}
}