diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index cbc1e2af..8e1cf148 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -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: diff --git a/CMakeLists.txt b/CMakeLists.txt index e1f9b1aa..303cc491 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index cd328c19..2e9a8218 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -383,9 +383,18 @@ void scenes::lobby::connect(const configuration::server_data & data) data.manual); } -std::optional scenes::lobby::check_recenter_gesture(const std::array & joints) +std::optional scenes::lobby::check_recenter_gesture(xr::spaces space, const std::optional> & 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 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 scenes::lobby::check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position) { - std::optional> 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 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) diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index ca4755b1..54d092d3 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -95,8 +95,10 @@ class lobby : public scene_impl XrAction recenter_left_action = XR_NULL_HANDLE; XrAction recenter_right_action = XR_NULL_HANDLE; - std::optional gui_recenter_position; - std::optional gui_recenter_distance; + // std::optional gui_recenter_position; + // std::optional 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> 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 void connect(const configuration::server_data & data); std::unique_ptr connect_to_session(wivrn_discover::service service, bool manual_connection); - std::optional check_recenter_gesture(const std::array & joints); + std::optional check_recenter_gesture(xr::spaces space, const std::optional> & joints); std::optional check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position); std::optional check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 1c1460a9..8585fed5 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -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); diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index c580a4fd..be306e28 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -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 diff --git a/common/application.cpp b/common/application.cpp index 0e8f6e8c..4992fced 100644 --- a/common/application.cpp +++ b/common/application.cpp @@ -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 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(app_info.at("common.LinuxClientIcon"))}; + std::string icon{std::get(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 & res) diff --git a/common/utils/load_icon.cpp b/common/utils/load_icon.cpp index f4bde8d5..5a3aaffa 100644 --- a/common/utils/load_icon.cpp +++ b/common/utils/load_icon.cpp @@ -18,6 +18,8 @@ #include "load_icon.h" +#include +#include #include #include #include @@ -126,10 +128,7 @@ std::vector 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 png; @@ -143,11 +142,8 @@ std::vector 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 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(header); + auto resource_type = read(header); + auto icon_count = read(header); + + if (reserved != 0 or resource_type != 1) + throw std::runtime_error{"Cannot read icon: " + filename.native()}; + + std::vector 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(header); + entry.height = read(header); + auto num_colors = read(header); + auto reserved = read(header); // reserved + auto planes = read(header); + entry.bpp = read(header); + auto size = read(header); + auto offset = read(header); - std::span header{ico}; + if (offset + size > ico.size()) + throw std::runtime_error{"Cannot read icon: " + filename.native()}; - auto reserved = read(header); - auto resource_type = read(header); - auto icon_count = read(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 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::max(); + + for (const auto & entry: entries) { - ico_file_entry entry; - entry.width = read(header); - entry.height = read(header); - auto num_colors = read(header); - auto reserved = read(header); // reserved - auto planes = read(header); - entry.bpp = read(header); - auto size = read(header); - auto offset = read(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::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(data); + + if (magic == ICO_PNG_MAGIC) + { + // PNG file + std::vector 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 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(data); // Width of image in pixels + auto height = read(data); // Height of image in pixels + auto planes = read(data); // Must be 1 + auto bpp = read(data); // 1, 4, 8, 16, 24, 32 + auto compression = read(data); // Must be 0 for icons + auto image_size = read(data); // Size of image (without this header) + auto x_res = read(data); + auto y_res = read(data); + auto used_colors = read(data); + auto important_colors = read(data); + + if (planes != 1 or compression != 0) return {}; - auto data = best_entry->data; - auto magic = read(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 palette; // BGRX + + if (bpp <= 16) { - // PNG file - std::vector 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 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(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 xor_map = read(data, xor_stride * h); + std::span and_map = read(data, and_stride * h); + + std::vector dest_buffer; + dest_buffer.resize(h * dst_stride); + + switch (bpp) { - auto width = read(data); // Width of image in pixels - auto height = read(data); // Height of image in pixels - auto planes = read(data); // Must be 1 - auto bpp = read(data); // 1, 4, 8, 16, 24, 32 - auto compression = read(data); // Must be 0 for icons - auto image_size = read(data); // Size of image (without this header) - auto x_res = read(data); - auto y_res = read(data); - auto used_colors = read(data); - auto important_colors = read(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 palette; // BGRX - - if (bpp <= 16) - { - // Load the palette - if (used_colors == 0) - used_colors = 1 << bpp; - - palette = read(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 xor_map = read(data, xor_stride * h); - std::span and_map = read(data, and_stride * h); - - std::vector 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 png_data{65536}; - png_alloc_size_t png_size = png_data.size(); + std::vector 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 load_zip(const std::filesystem::path & filename, int size) +{ + std::unique_ptr 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 png; + + while (archive_read_next_header(zip.get(), &entry) == ARCHIVE_OK) + { + printf("%s\n", archive_entry_pathname(entry)); + + std::vector 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 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> icon_cache; @@ -556,7 +607,7 @@ std::unordered_map> icon_cache; const std::vector & 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 icon; @@ -573,7 +624,7 @@ const std::vector & wivrn::load_icon(const std::filesystem::path & fi else if (filename.extension() == ".zip") { // Steam icon (Linux) - // TODO + icon = load_zip(filename, size); } else { diff --git a/common/utils/steam_app_info.cpp b/common/utils/steam_app_info.cpp index 9e8e69dd..168240a1 100644 --- a/common/utils/steam_app_info.cpp +++ b/common/utils/steam_app_info.cpp @@ -18,6 +18,7 @@ #include "steam_app_info.h" #include +#include #include #include #include @@ -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(bindata))}; + for (char & c: name) + c = std::tolower(c); // https://github.com/ValveResourceFormat/ValveKeyValue/blob/master/ValveKeyValue/ValveKeyValue/KeyValues1/KV1BinaryNodeType.cs switch (type) diff --git a/flake.nix b/flake.nix index ed7ac917..6ab65aa9 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,7 @@ pkgs.librsvg pkgs.libpng + pkgs.libarchive ]; package = pkgs.enableDebugging (pkgs.wivrn.overrideAttrs (finalAttrs: oldAttrs: { diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index cefb88ab..60a28b89 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -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()); + } } } }