/* * WiVRn VR streaming * Copyright (C) 2023 Guillaume Meunier * Copyright (C) 2023 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "hardware.h" #include "utils/overloaded.h" #include "xr/to_string.h" #include #include #include #include #include #include #include #include #ifdef __ANDROID__ #include "utils/wrap_lambda.h" #include static std::optional get_property(const char * property) { auto info = __system_property_find(property); if (not info) return std::nullopt; std::string result; wrap_lambda cb = [&result](const char * name, const char * value, uint32_t serial) { result = value; }; __system_property_read_callback(info, cb.userdata_first(), cb); return result; } #endif static std::optional env_string(std::string_view name) { #ifdef __ANDROID__ auto android_sysprop_name = std::format("wivrn.debug.{}", name); return get_property(android_sysprop_name.c_str()); #else std::string env_name = "WIVRN_"; env_name.reserve(env_name.size() + name.size()); for (const auto & c: name) env_name += std::toupper(c); const char * value = std::getenv(env_name.c_str()); if (value) return value; return std::nullopt; #endif } static std::optional env_u32(std::string_view name) { const auto value = env_string(name); if (not value) return std::nullopt; try { const unsigned long parsed = std::stoul(*value); if (parsed > std::numeric_limits::max()) return std::nullopt; return static_cast(parsed); } catch (std::runtime_error & e) { spdlog::warn("failed to parse {} (value: {}: {}", name, *value, e.what()); return std::nullopt; } } static std::optional env_bool(std::string_view name) { const auto value = env_string(name); if (not value) return std::nullopt; if (strcmp(value->c_str(), "1") == 0 || strcasecmp(value->c_str(), "true") == 0 || strcasecmp(value->c_str(), "yes") == 0 || strcasecmp(value->c_str(), "on") == 0) return true; if (strcmp(value->c_str(), "0") == 0 || strcasecmp(value->c_str(), "false") == 0 || strcasecmp(value->c_str(), "no") == 0 || strcasecmp(value->c_str(), "off") == 0) return false; return std::nullopt; } static hmd_traits_t traits; #ifndef NDEBUG static bool initialized = false; #endif const hmd_traits_t & hmd_traits() { #ifndef NDEBUG assert(initialized); #endif return traits; } void hmd_traits_init() { #ifndef NDEBUG assert(not initialized); initialized = true; #endif #ifdef __ANDROID__ const auto device = get_property("ro.product.device"); const auto manufacturer = get_property("ro.product.manufacturer"); const auto model = get_property("ro.product.model"); spdlog::info("Guessing HMD model from:"); spdlog::info(" ro.product.device = \"{}\":", device.value_or("")); spdlog::info(" ro.product.manufacturer = \"{}\":", manufacturer.value_or("")); spdlog::info(" ro.product.model = \"{}\":", model.value_or("")); traits.permissions[feature::microphone] = "android.permission.RECORD_AUDIO"; if (manufacturer == "Oculus") { traits.permissions[feature::eye_gaze] = "com.oculus.permission.EYE_TRACKING"; traits.permissions[feature::face_tracking] = "com.oculus.permission.FACE_TRACKING"; // Quest hand tracking creates a fake khr/simple_controller when hand tracking // is enabled, this messes with native hand tracking traits.bind_simple_controller = false; // Quest breaks spec and does not support grip_surface for ext/hand_interaction_ext traits.hand_interaction_grip_surface = false; if (device == "monterey") // Quest 1 { traits.panel_width_override = 1440; traits.controller_profile = "oculus-touch-v2"; traits.vk_debug_ext_allowed = false; // Quest 1 lies, the extension won't load } else if (device == "hollywood") // Quest 2 { traits.panel_width_override = 1832; traits.controller_profile = "oculus-touch-v3"; } else if (device == "seacliff") { traits.panel_width_override = 1800; // Quest pro traits.controller_profile = "meta-quest-touch-pro"; } else if (device == "eureka") { traits.panel_width_override = 2064; // Quest 3 traits.controller_profile = "meta-quest-touch-plus"; } else if (device == "panther") // Quest 3S { traits.panel_width_override = 1832; traits.controller_profile = "meta-quest-touch-plus"; } } else if (model == "Lynx-R1") { traits.needs_srgb_conversion = false; } else if (manufacturer == "Pico") { traits.permissions[feature::eye_gaze] = "com.picovr.permission.EYE_TRACKING"; traits.permissions[feature::face_tracking] = "com.picovr.permission.FACE_TRACKING"; traits.view_locate = false; traits.discard_frame = false; const auto pico_model = get_property("pxr.vendorhw.product.model"); spdlog::info(" pxr.vendorhw.product.model = \"{}\":", pico_model.value_or("")); if (pico_model == "Pico Neo 3") { traits.panel_width_override = 1832; traits.controller_profile = "pico-neo3"; } else if (pico_model and pico_model->starts_with("PICO 4")) { traits.panel_width_override = 2160; if (pico_model == "PICO 4 Ultra") traits.controller_profile = "pico-4u"; else traits.controller_profile = "pico-4"; if (pico_model == "PICO 4 Pro" or pico_model == "PICO 4 Enterprise") traits.pico_face_tracker = true; } } if (manufacturer == "HTC") { // Accepts OpenXR 1.1 but doesn't actually implement it traits.max_openxr_api_version = XR_API_VERSION_1_0; // Doesn't handle additive blend, so needs specific ray model traits.controller_ray_model = "assets://ray-htc.glb"; traits.controller_profile = "htc-vive-focus-3"; if (model == "VIVE Focus 3") traits.panel_width_override = 2448; if (model == "VIVE Focus Vision") traits.panel_width_override = 2448; if (model == "VIVE XR Series") traits.panel_width_override = 1920; } if (manufacturer == "samsung") { if (model == "SM-I610") // Galaxy XR { traits.panel_width_override = 3552; traits.controller_profile = "samsung-galaxyxr"; traits.permissions[feature::hand_tracking] = "android.permission.HAND_TRACKING"; traits.permissions[feature::eye_gaze] = "android.permission.EYE_TRACKING_FINE"; traits.permissions[feature::face_tracking] = "android.permission.FACE_TRACKING"; } } #endif spdlog::info("HMD traits initialized"); boost::pfr::for_each_field_with_name( traits, utils::overloaded{ [](std::string_view name, std::string & field) { if (auto val = env_string(name)) { spdlog::info("\t{} override: {} -> {}", name, field, *val); field = *val; } else { spdlog::info("\t{}: {}", name, field); } }, [](std::string_view name, uint32_t & field) { if (auto val = env_u32(name)) { spdlog::info("\t{} override: {} -> {}", name, field, *val); field = *val; } else { spdlog::info("\t{}: {}", name, field); } }, [](std::string_view name, bool & field) { if (auto val = env_bool(name)) { spdlog::info("\t{} override: {} -> {}", name, field, *val); field = *val; } else { spdlog::info("\t{}: {}", name, field); } }, [](std::string_view name, XrVersion & field) { if (auto val = env_string(name)) { XrVersion v; if (val == "1.1") v = XR_API_VERSION_1_1; else if (val == "1.0") v = XR_API_VERSION_1_0; else { spdlog::warn("XrVersion {} not recognized", *val); return; } spdlog::info("\t{} override: {} -> {}", name, xr::to_string(field), xr::to_string(v)); field = v; } else { spdlog::info("\t{}: {}", name, xr::to_string(field)); } }, [](std::string_view name, hmd_permissions &) { }}); }; std::string model_name() { #ifdef __ANDROID__ const auto manufacturer = get_property("ro.product.manufacturer").value_or(""); const auto model = get_property("ro.product.model").value_or(""); return manufacturer + " " + model; #else return "Unknown headset"; #endif } static XrViewConfigurationView scale_view(XrViewConfigurationView view, uint32_t width) { double ratio = double(width) / view.recommendedImageRectWidth; view.recommendedImageRectWidth = width; view.recommendedImageRectHeight *= ratio; spdlog::info("Using panel size: {}x{}", view.recommendedImageRectWidth, view.recommendedImageRectHeight); return view; } XrViewConfigurationView hmd_traits_t::override_view(XrViewConfigurationView view) const { // Standalone headsets tend to report a lower resolution // as the GPU can't handle full res. // Return the panel resolution instead. spdlog::debug("Recommended image size: {}x{}", view.recommendedImageRectWidth, view.recommendedImageRectHeight); if (panel_width_override > 0) return scale_view(view, panel_width_override); return view; } std::pair controller_offset(std::string_view profile, xr::spaces space) { #ifndef __ANDROID__ if (const char * offset = [space]() -> const char * { switch (space) { case xr::spaces::grip_left: return std::getenv("WIVRN_GRIP_LEFT_OFFSET"); case xr::spaces::grip_right: return std::getenv("WIVRN_GRIP_RIGHT_OFFSET"); case xr::spaces::aim_left: return std::getenv("WIVRN_AIM_LEFT_OFFSET"); case xr::spaces::aim_right: return std::getenv("WIVRN_AIM_RIGHT_OFFSET"); default: return nullptr; } }()) { float x, y, z, yaw, pitch, roll; if (sscanf(offset, "%f %f %f %f %f %f", &x, &y, &z, &yaw, &pitch, &roll) == 6) { return {{x, y, z}, glm::angleAxis(glm::radians(yaw), glm::vec3{0, 1, 0}) * glm::angleAxis(glm::radians(pitch), glm::vec3{1, 0, 0}) * glm::angleAxis(glm::radians(roll), glm::vec3{0, 0, 1})}; } } #endif if (profile == "oculus-touch-v2") switch (space) { case xr::spaces::grip_left: case xr::spaces::grip_right: return {{0, -0.006, -0.025}, glm::angleAxis(glm::radians(-15.f), glm::vec3{1, 0, 0})}; case xr::spaces::aim_left: return {{-0.010, 0, 0.02}, {1, 0, 0, 0}}; case xr::spaces::aim_right: return {{0.010, 0, 0.02}, {1, 0, 0, 0}}; default: break; } else if (profile == "meta-quest-touch-plus") { switch (space) { case xr::spaces::grip_left: case xr::spaces::grip_right: return {{0, -0.010, -0.025}, glm::angleAxis(glm::radians(-12.f), glm::vec3{1, 0, 0})}; case xr::spaces::aim_left: case xr::spaces::aim_right: return {{0, 0, -0.03}, {1, 0, 0, 0}}; default: break; } } else if (profile == "htc-vive-focus-3") switch (space) { case xr::spaces::grip_left: case xr::spaces::grip_right: return {{0, 0.007, -0.030}, {1, 0, 0, 0}}; case xr::spaces::aim_left: case xr::spaces::aim_right: return {{0, -0.025, 0.005}, {1, 0, 0, 0}}; default: break; } else if (profile == "pico-4" || profile == "pico-4s") switch (space) { case xr::spaces::grip_left: case xr::spaces::grip_right: return {{0, -0.014, -0.048}, glm::angleAxis(glm::radians(-35.060f), glm::vec3{1, 0, 0})}; default: break; } else if (profile == "pico-neo3") switch (space) { case xr::spaces::grip_left: case xr::spaces::grip_right: return {{0, 0.005, -0.051}, glm::angleAxis(glm::radians(-29.400f), glm::vec3{1, 0, 0})}; default: break; } else if (profile == "samsung-galaxyxr") switch (space) { case xr::spaces::grip_left: return {{-0.005, 0.000, 0.035}, {1, 0, 0, 0}}; case xr::spaces::grip_right: return {{0.005, 0.000, 0.035}, {1, 0, 0, 0}}; default: break; } return {{0, 0, 0}, {1, 0, 0, 0}}; }