diff --git a/CMakeLists.txt b/CMakeLists.txt index 25d7ff50..aa582d40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,6 +219,7 @@ FetchContent_Declare(imgui EXCLUDE_FROM_ALL SYSTEM URL https://github.co ) # Use the docking branch of imgui to use multi-viewport # FetchContent_Declare(imgui EXCLUDE_FROM_ALL URL https://github.com/ocornut/imgui/archive/ce0d0ac8298ce164b5d862577e8b087d92f6e90e.zip) +FetchContent_Declare(uni-algo EXCLUDE_FROM_ALL SYSTEM URL https://github.com/uni-algo/uni-algo/archive/v1.2.0.tar.gz) # Server dependencies FetchContent_Declare(monado diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 82c8d233..114a6b1c 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -7,7 +7,7 @@ set(KTX_FEATURE_LOADTEST_APPS OFF) set(FASTGLTF_COMPILE_AS_CPP20 ON) -FetchContent_MakeAvailable(simdjson spdlog glm fastgltf imgui stb libktx implot) +FetchContent_MakeAvailable(simdjson spdlog glm fastgltf imgui stb libktx implot uni-algo) if (NOT BOOST_FOUND) FetchContent_MakeAvailable(boost) @@ -91,6 +91,8 @@ target_link_libraries(wivrn-imgui FreetypeHarfbuzz) target_link_libraries(wivrn wivrn-imgui) +target_link_libraries(wivrn uni-algo) + target_compile_features(wivrn PRIVATE cxx_std_23) target_compile_definitions(wivrn PRIVATE "XR_USE_TIMESPEC=1") diff --git a/client/android/battery.cpp b/client/android/battery.cpp new file mode 100644 index 00000000..a3d399c3 --- /dev/null +++ b/client/android/battery.cpp @@ -0,0 +1,88 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 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 "battery.h" +#include "android/jnipp.h" +#include "application.h" + +#include + +using Application = jni::object<"android/app/Application">; +using BroadcastReceiver = jni::object<"android/content/BroadcastReceiver">; +using Context = jni::object<"android/content/Context">; +using Intent = jni::object<"android/content/Intent">; +using IntentFilter = jni::object<"android/content/IntentFilter">; +using Object = jni::object<"">; + +struct battery::pimpl +{ + Object act{application::native_app()->activity->clazz}; + Application app = act.call("getApplication"); + Context ctx = app.call("getApplicationContext"); + + jni::string filter_jstr{"android.intent.action.BATTERY_CHANGED"}; + jni::string level_jstr{"level"}; + jni::string scale_jstr{"scale"}; + jni::string plugged_jstr{"plugged"}; + jni::Int default_jint{-1}; + + BroadcastReceiver receiver = BroadcastReceiver{nullptr}; + IntentFilter filter = jni::new_object<"android/content/IntentFilter">(filter_jstr); + + jmethodID register_receiver = jni::klass("android/content/Context").method("registerReceiver", receiver, filter); + + jmethodID get_int_extra = jni::klass("android/content/Intent").method("getIntExtra", level_jstr, default_jint); +}; + +battery::battery() +{ + application::instance().setup_jni(); + + p = std::make_unique(); +} + +battery::~battery() {} + +battery::status battery::get() +{ + if (auto now = std::chrono::steady_clock::now(); now > next_battery_check) + { + next_battery_check = now + battery_check_interval; + + auto intent = p->ctx.call(p->register_receiver, p->receiver, p->filter); + if (intent) + { + auto level_jint = intent.call(p->get_int_extra, p->level_jstr, p->default_jint); + auto scale_jint = intent.call(p->get_int_extra, p->scale_jstr, p->default_jint); + + if (level_jint && level_jint.value >= 0 && scale_jint && scale_jint.value >= 0) + last_status.charge = (float)(level_jint.value) / (float)(scale_jint.value); + else + last_status.charge = std::nullopt; + + auto plugged_jint = intent.call(p->get_int_extra, p->plugged_jstr, p->default_jint); + last_status.charging = plugged_jint && plugged_jint.value > 0; + } + + auto duration = std::chrono::steady_clock::now() - now; + spdlog::info("Battery check took: {} µs", std::chrono::duration_cast(duration).count()); + } + + return last_status; +} diff --git a/client/android/battery.h b/client/android/battery.h new file mode 100644 index 00000000..1259ff31 --- /dev/null +++ b/client/android/battery.h @@ -0,0 +1,49 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 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 . + */ + +#pragma once + +#include +#include +#include + +class battery +{ +public: + struct status + { + std::optional charge = std::nullopt; + bool charging = false; + }; + +private: + struct pimpl; + std::unique_ptr p; + + const std::chrono::seconds battery_check_interval = std::chrono::seconds{2}; + + std::chrono::steady_clock::time_point next_battery_check = std::chrono::steady_clock::now(); + status last_status; + +public: + battery(); + ~battery(); + + status get(); +}; diff --git a/client/android/jnipp.h b/client/android/jnipp.h index 81e59c67..3d313b03 100644 --- a/client/android/jnipp.h +++ b/client/android/jnipp.h @@ -63,6 +63,12 @@ public: inst.vm->AttachCurrentThread(&inst.jni_env, nullptr); } + static void setup_thread(JNIEnv * env) + { + auto & inst = instance(); + inst.jni_env = env; + } + static void detach() { instance() = jni_thread(); diff --git a/client/application.cpp b/client/application.cpp index e6f2f6b4..8c5a01a8 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -949,6 +949,54 @@ std::pair application::get_action(const std::string & re return {}; } +#ifdef __ANDROID__ +extern "C" __attribute__((visibility("default"))) void Java_org_meumeu_wivrn_MainActivity_onNewIntent(JNIEnv * env, jobject instance, jobject intent_obj) +{ + jni::jni_thread::setup_thread(env); + jni::object<"android/content/Intent"> intent{intent_obj}; + + if (auto data_string = intent.call("getDataString")) + { + spdlog::info("Received intent {}", (std::string)data_string); + application::instance().set_server_uri(data_string); + } +} +#endif + +void application::set_server_uri(std::string uri) +{ + std::string server_address; + bool server_tcp_only = false; + + if (uri.starts_with("wivrn://")) + { + server_address = uri.substr(strlen("wivrn://")); + } + else if (uri.starts_with("wivrn+tcp://")) + { + server_address = uri.substr(strlen("wivrn+tcp://")); + server_tcp_only = true; + } + + auto colon = server_address.rfind(":"); + int port = wivrn::default_port; + if (colon != std::string::npos) + { + port = std::stoi(server_address.substr(colon + 1)); + server_address = server_address.substr(0, colon); + } + + { + std::unique_lock _{server_intent_mutex}; + server_intent = wivrn_discover::service{ + .name = "", + .hostname = server_address, + .port = port, + .tcp_only = server_tcp_only, + }; + } +} + application::application(application_info info) : app_info(std::move(info)) @@ -964,21 +1012,10 @@ application::application(application_info info) : // Get the intent, to handle wivrn://uri auto intent = act.call>("getIntent"); - std::string data_string; - if (auto data_string_jni = intent.call("getDataString")) + if (auto data_string = intent.call("getDataString")) { - data_string = data_string_jni; - } - - if (data_string.starts_with("wivrn://")) - { - server_address = data_string.substr(strlen("wivrn://")); - } - - if (data_string.starts_with("wivrn+tcp://")) - { - server_address = data_string.substr(strlen("wivrn+tcp://")); - server_tcp_only = true; + spdlog::info("Started with intent {}", (std::string)data_string); + set_server_uri(data_string); } auto files_dir = ctx.call>("getFilesDir"); diff --git a/client/application.h b/client/application.h index 31f4b0f8..1c52211c 100644 --- a/client/application.h +++ b/client/application.h @@ -54,6 +54,10 @@ class scene; +#ifdef __ANDROID__ +extern "C" __attribute__((visibility("default"))) void Java_org_meumeu_wivrn_MainActivity_onNewIntent(JNIEnv * env, jobject instance, jobject intent_obj); +#endif + struct application_info { std::string name = "Unnamed application"; @@ -71,6 +75,10 @@ class application : public singleton { friend class scene; +#ifdef __ANDROID__ + friend __attribute__((visibility("default"))) void Java_org_meumeu_wivrn_MainActivity_onNewIntent(JNIEnv * env, jobject instance, jobject intent_obj); +#endif + application_info app_info; #ifdef __ANDROID__ ANativeWindow * native_window = nullptr; @@ -140,8 +148,8 @@ class application : public singleton std::shared_ptr wifi; - std::string server_address; - bool server_tcp_only = false; + std::mutex server_intent_mutex; + std::optional server_intent; std::mutex scene_stack_lock; std::vector> scene_stack; @@ -171,6 +179,8 @@ class application : public singleton vk::raii::DebugReportCallbackEXT debug_report_callback = nullptr; #endif + void set_server_uri(std::string uri); + public: using singleton::instance; application(application_info info); @@ -348,14 +358,14 @@ public: return instance().vk_queue; } - const std::string & get_server_address() const + static std::optional get_intent() { - return server_address; - } + std::unique_lock _{instance().server_intent_mutex}; - const bool & get_server_tcp_only() const - { - return server_tcp_only; + std::optional intent = instance().server_intent; + instance().server_intent.reset(); + + return intent; } static vk::raii::PhysicalDevice & get_physical_device() diff --git a/client/configuration.cpp b/client/configuration.cpp index 191d3d13..ffb55492 100644 --- a/client/configuration.cpp +++ b/client/configuration.cpp @@ -177,6 +177,9 @@ configuration::configuration(xr::system & system) if (auto val = root["passthrough_enabled"]; val.is_bool()) passthrough_enabled = val.get_bool(); + if (auto val = root["virtual_keyboard_layout"]; val.is_string()) + virtual_keyboard_layout = val.get_string().value(); + for (const auto & [i, name]: magic_enum::enum_entries()) { if (auto val = root[name]; val.is_bool()) @@ -212,7 +215,7 @@ void configuration::save() for (auto & [cookie, server_data]: servers) { - if (server_data.autoconnect || server_data.manual) + if (server_data.autoconnect or server_data.manual) { ss << "{"; ss << "\"autoconnect\":" << std::boolalpha << server_data.autoconnect << ","; @@ -240,5 +243,6 @@ void configuration::save() json << ",\"passthrough_enabled\":" << std::boolalpha << passthrough_enabled; for (auto & [key, value]: features) json << "," << key << ":" << std::boolalpha << value; + json << ",\"virtual_keyboard_layout\":" << json_string(virtual_keyboard_layout); json << "}"; } diff --git a/client/configuration.h b/client/configuration.h index 7dbed9a8..714eb10c 100644 --- a/client/configuration.h +++ b/client/configuration.h @@ -50,6 +50,8 @@ public: bool show_performance_metrics = false; bool passthrough_enabled = false; + std::string virtual_keyboard_layout = "QWERTY"; + bool check_feature(feature f) const; void set_feature(feature f, bool state); diff --git a/client/java/MainActivity.java b/client/java/MainActivity.java index da777b35..e67ee0e9 100644 --- a/client/java/MainActivity.java +++ b/client/java/MainActivity.java @@ -10,37 +10,8 @@ public class MainActivity extends android.app.NativeActivity } @Override - public native void onRequestPermissionsResult(int requestCode, String [] permissions, int[] grantResults); + public native void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults); - // @Override - // public void onRequestPermissionsResult(int requestCode, String [] permissions, int[] grantResults) - // { - // super.onRequestPermissionsResult(requestCode, permissions, grantResults); - // // switch (requestCode) { - // // case REQUEST_CODE_STORAGE_PERMISSION: - // // if (permissionsGranted(permissions, grantResults)) { - // // NativeApp.sendMessageFromJava("permission_granted", "storage"); - // // } else { - // // NativeApp.sendMessageFromJava("permission_denied", "storage"); - // // } - // // break; - // // case REQUEST_CODE_LOCATION_PERMISSION: - // // if (permissionsGranted(permissions, grantResults)) { - // // mLocationHelper.startLocationUpdates(); - // // } - // // break; - // // case REQUEST_CODE_CAMERA_PERMISSION: - // // if (mCameraHelper != null && permissionsGranted(permissions, grantResults)) { - // // mCameraHelper.startCamera(); - // // } - // // break; - // // case REQUEST_CODE_MICROPHONE_PERMISSION: - // // if (permissionsGranted(permissions, grantResults)) { - // // NativeApp.audioRecording_Start(); - // // } - // // break; - // // default: - // // } - // - // } + @Override + public native void onNewIntent(android.content.Intent intent); } diff --git a/client/main.cpp b/client/main.cpp index 90169726..9e8eef29 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -19,7 +19,6 @@ #include "application.h" #include "scenes/lobby.h" -#include "scenes/stream.h" #include "spdlog/spdlog.h" #include @@ -51,30 +50,6 @@ void real_main() info.version = VK_MAKE_VERSION(1, 0, 0); application app(info); - std::string server_address = app.get_server_address(); - if (not server_address.empty()) - { - auto colon = server_address.rfind(":"); - int port = wivrn::default_port; - if (colon != std::string::npos) - { - port = std::stoi(server_address.substr(colon + 1)); - server_address = server_address.substr(0, colon); - } - auto & config = application::get_config(); - config.servers["wivrn://"] = configuration::server_data{ - .autoconnect = true, - .manual = true, - .visible = true, - .compatible = true, - .service = { - .name = app.get_server_address(), - .hostname = server_address, - .port = port, - .tcp_only = app.get_server_tcp_only(), - }, - }; - } app.push_scene(); app.run(); diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 7b938b1b..f290a377 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -27,6 +27,7 @@ #include "vulkan/vulkan_enums.hpp" #include "vulkan/vulkan_handles.hpp" #include "vulkan/vulkan_to_string.hpp" +#include "xr/space.h" #include #include #include @@ -185,28 +186,31 @@ std::vector> imgui_context::ray_plane_intersection(cons for (const auto & i: layers_) { + if (i.space != xr::spaces::world) + continue; + auto M = glm::transpose(glm::mat3_cast(i.orientation)); // world-to-plane transform glm::quat q = (in.source == ImGuiMouseSource_VRHandTracking) ? i.orientation : in.aim_orientation; - glm::vec3 controller_direction = glm::column(glm::mat3_cast(q), 2); + glm::vec3 controller_direction = -glm::column(glm::mat3_cast(q), 2); // The aim direction is -Z // Compute all vectors in the reference frame of the GUI plane glm::vec3 ray_start = M * (in.aim_position - i.position); glm::vec3 ray_dir = M * controller_direction; - if (ray_dir.z > 0.0001f) + if (ray_dir.z < -0.0001f) { glm::vec2 coord; - // ray_start - distance × ray_dir ∈ imgui plane - // => ray_start.z - distance × ray_dir.z = 0 - float distance = ray_start.z / ray_dir.z; + // ray_start + distance × ray_dir ∈ imgui plane + // => ray_start.z + distance × ray_dir.z = 0 + float distance = -ray_start.z / ray_dir.z; if (distance < 0) continue; - coord.x = ray_start.x - distance * ray_dir.x; - coord.y = ray_start.y - distance * ray_dir.y; + coord.x = ray_start.x + distance * ray_dir.x; + coord.y = ray_start.y + distance * ray_dir.y; // Convert from mesh coordinates to imgui coordinates coord = coord / i.size; @@ -226,6 +230,22 @@ std::vector> imgui_context::ray_plane_intersection(cons return intersections; } +glm::vec3 imgui_context::rw_from_vp(const ImVec2 & position) +{ + for (auto & i: layers_) + { + if (not in_viewport(i, position)) + continue; + + return i.position + glm::mat3_cast(i.orientation) * glm::vec3{ + ((position.x - i.vp_origin.x) / i.vp_size.x - 0.5) * i.size.x, + (-(position.y - i.vp_origin.y) / i.vp_size.y + 0.5) * i.size.y, + 0}; + } + + return {}; +} + imgui_context::imgui_frame & imgui_context::get_frame(vk::Image destination) { for (auto & i: frames) @@ -691,10 +711,13 @@ void imgui_context::compute_pointer_position(imgui_context::controller_state & s if (ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal()) { - // If there is a popup window, use the viewport of that window + // If there is a popup window, use the viewport of that window or the virtual keyboard for (auto & i: layers_) { - if (window_intersects_viewport(modal_popup, i)) + if (i.space != xr::spaces::world) + continue; + + if (window_intersects_viewport(modal_popup, i) or i.always_show_cursor) { for (auto [position, distance]: intersections) { @@ -773,36 +796,23 @@ void imgui_context::new_frame(XrTime display_time) if (focused_controller != (size_t)-1) io.AddMouseSourceEvent(new_states[focused_controller].source); - if (focused_change && controllers[focused_controller].second.trigger_clicked) - { - // Focused controller changed: end the current click - io.AddMouseButtonEvent(0, false); - button_pressed = false; - fingertip_touching = false; - } - if (new_focused_controller != (size_t)-1) { auto scroll = new_states[new_focused_controller].scroll_value; - bool last_trigger = controllers[new_focused_controller].second.trigger_clicked; button_pressed = new_states[new_focused_controller].trigger_clicked; - - bool last_touching = controllers[new_focused_controller].second.fingertip_touching; fingertip_touching = new_states[new_focused_controller].fingertip_touching; + // Focused controller changed: end the current click for this frame + io.AddMouseButtonEvent(0, (button_pressed or fingertip_touching) and not focused_change); + if (auto position = new_states[new_focused_controller].pointer_position) { io.AddMousePosEvent(position->x, position->y); - io.AddMouseButtonEvent(0, button_pressed || fingertip_touching); if (glm::length(scroll) > 0.01f) io.AddMouseWheelEvent(scroll.x, scroll.y); } - else - { - io.AddMouseButtonEvent(0, button_pressed || fingertip_touching); - } } focused_controller = new_focused_controller; @@ -848,82 +858,95 @@ void imgui_context::new_frame(XrTime display_time) swapchain.wait(); } -std::vector imgui_context::end_frame() +std::vector> imgui_context::end_frame() { vk::Image destination = swapchain.images()[image_index].image; ImGui::SetCurrentContext(context); ImPlot::SetCurrentContext(plot_context); - if (auto position = controllers[focused_controller].second.pointer_position) + for (auto & controller: controllers) { - // Clip in the right plane - ImVec2 clip_rect_min(0, 0); - ImVec2 clip_rect_max(size.width, size.height); - - // If there is a modal popup, only display the cursor in the viewport of the popup - ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal(); - if (modal_popup) + if (auto position = controller.second.pointer_position) { - for (auto & i: layers_) + // Clip in the right plane + ImVec2 clip_rect_min(0, 0); + ImVec2 clip_rect_max(0, 0); + + // If there is a modal popup, only display the cursor in the viewport of the popup or on the virtual keyboard + if (ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal()) { - if (window_intersects_viewport(modal_popup, i)) + for (auto & i: layers_) + { + if (i.space != xr::spaces::world) + continue; + + // The cursor is over the virtual keyboard + if (i.always_show_cursor and + position->x >= i.vp_origin.x and + position->y >= i.vp_origin.y and + position->x <= i.vp_origin.x + i.vp_size.x and + position->y <= i.vp_origin.y + i.vp_size.y) + { + clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); + clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); + break; + } + + // The cursor is in the same viewport as the popup + if (window_intersects_viewport(modal_popup, i) and + position->x >= i.vp_origin.x and + position->y >= i.vp_origin.y and + position->x <= i.vp_origin.x + i.vp_size.x and + position->y <= i.vp_origin.y + i.vp_size.y) + { + clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); + clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); + break; + } + } + } + else + { + for (auto & i: layers_) { if (position->x < i.vp_origin.x or position->y < i.vp_origin.y or position->x > i.vp_origin.x + i.vp_size.x or position->y > i.vp_origin.y + i.vp_size.y) - { - // Cursor is not in the same viewport as the popup - clip_rect_min = ImVec2(0, 0); - clip_rect_max = ImVec2(0, 0); - } - else - { - clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); - clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); - } + continue; + + clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); + clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); + break; } } - } - else - { - for (auto & i: layers_) + + // Compute the distance to the closest window + float distance = std::numeric_limits::infinity(); + for (ImGuiWindow * window: context->Windows) { - if (position->x < i.vp_origin.x or - position->y < i.vp_origin.y or - position->x > i.vp_origin.x + i.vp_size.x or - position->y > i.vp_origin.y + i.vp_size.y) - continue; - - clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); - clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); - - break; + distance = std::min(distance, distance_to_window(window, *position)); } + + float radius = 10; + float alpha = std::clamp((40 - distance) / 50, 0, 0.8); + + if (&controller != &controllers[focused_controller]) + alpha /= 3; + + ImU32 color_pressed = ImGui::GetColorU32(ImVec4(0, 0.2, 1, alpha)); + ImU32 color_unpressed = ImGui::GetColorU32(ImVec4(1, 1, 1, alpha)); + + bool pressed = controller.second.trigger_clicked || controller.second.fingertip_touching; + + ImDrawList * draw_list = ImGui::GetForegroundDrawList(); + draw_list->PushClipRect(clip_rect_min, clip_rect_max); + draw_list->AddCircleFilled(*position, radius, pressed ? color_pressed : color_unpressed); + draw_list->AddCircle(*position, radius * 1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4); + draw_list->PopClipRect(); } - - // Compute the distance to the closest window - float distance = std::numeric_limits::infinity(); - for (ImGuiWindow * window: context->Windows) - { - distance = std::min(distance, distance_to_window(window, *position)); - } - - float radius = 10; - float alpha = std::clamp((40 - distance) / 50, 0, 0.8); - - ImU32 color_pressed = ImGui::GetColorU32(ImVec4(0, 0.2, 1, alpha)); - ImU32 color_unpressed = ImGui::GetColorU32(ImVec4(1, 1, 1, alpha)); - - bool pressed = button_pressed || fingertip_touching; - - ImDrawList * draw_list = ImGui::GetForegroundDrawList(); - draw_list->PushClipRect(clip_rect_min, clip_rect_max); - draw_list->AddCircleFilled(*position, radius, pressed ? color_pressed : color_unpressed); - draw_list->AddCircle(*position, radius * 1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4); - draw_list->PopClipRect(); } ImGui::Render(); @@ -966,7 +989,7 @@ std::vector imgui_context::end_frame() swapchain.release(); - std::vector quads; + std::vector> quads; quads.reserve(layers_.size()); for (auto & i: layers_) @@ -984,41 +1007,43 @@ std::vector imgui_context::end_frame() if (not visible) continue; - quads.push_back(XrCompositionLayerQuad{ - .type = XR_TYPE_COMPOSITION_LAYER_QUAD, - .layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, - .space = world, - .eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH, - .subImage = { - .swapchain = swapchain, - .imageRect = { - .offset = { - .x = i.vp_origin.x, - .y = i.vp_origin.y, + quads.emplace_back( + i.z_index, + XrCompositionLayerQuad{ + .type = XR_TYPE_COMPOSITION_LAYER_QUAD, + .layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, + .space = application::space(i.space), + .eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH, + .subImage = { + .swapchain = swapchain, + .imageRect = { + .offset = { + .x = i.vp_origin.x, + .y = i.vp_origin.y, + }, + .extent = { + .width = i.vp_size.x, + .height = i.vp_size.y, + }}, + }, + .pose = { + .orientation = { + .x = i.orientation.x, + .y = i.orientation.y, + .z = i.orientation.z, + .w = i.orientation.w, + }, + .position = { + .x = i.position.x, + .y = i.position.y, + .z = i.position.z, }, - .extent = { - .width = i.vp_size.x, - .height = i.vp_size.y, - }}, - }, - .pose = { - .orientation = { - .x = i.orientation.x, - .y = i.orientation.y, - .z = i.orientation.z, - .w = i.orientation.w, }, - .position = { - .x = i.position.x, - .y = i.position.y, - .z = i.position.z, + .size = { + .width = i.size.x, + .height = i.size.y, }, - }, - .size = { - .width = i.size.x, - .height = i.size.y, - }, - }); + }); } return quads; @@ -1117,3 +1142,14 @@ bool imgui_context::is_modal_popup_shown() const { return ImGui::GetTopMostAndVisiblePopupModal() != nullptr; } + +imgui_context::viewport & imgui_context::layer(ImVec2 position) +{ + for (auto & layer: layers_) + { + if (in_viewport(layer, position)) + return layer; + } + + return layers_.front(); +} diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index 37dfe1c2..c6c0d410 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -20,6 +20,7 @@ #include "wivrn_config.h" #include "xr/hand_tracker.h" +#include "xr/space.h" #include "xr/swapchain.h" #include #include @@ -90,6 +91,7 @@ public: struct viewport { // Position of this viewport in the world + xr::spaces space; // Must be world to allow the cursor to work glm::vec3 position; glm::quat orientation; glm::vec2 size; @@ -98,6 +100,10 @@ public: glm::ivec2 vp_origin; glm::ivec2 vp_size; + bool always_show_cursor = false; // Show the cursor in this viewport even if there is a modal popup elsewhere (eg. this is a virtual keyboard) + + int z_index = 0; + ImVec2 vp_center() const { return ImVec2(vp_origin.x + int(vp_size.x / 2), vp_origin.y + int(vp_size.y / 2)); @@ -160,7 +166,6 @@ private: void initialize_fonts(); std::vector read_controllers_state(XrTime display_time); - void compute_pointer_position(controller_state & state); public: imgui_context( @@ -179,8 +184,10 @@ public: return layers_; } + viewport & layer(ImVec2 position); + void new_frame(XrTime display_time); - std::vector end_frame(); + std::vector> end_frame(); ImFont * large_font; size_t get_focused_controller() const @@ -188,7 +195,11 @@ public: return focused_controller; } - std::vector> ray_plane_intersection(const imgui_context::controller_state & in) const; + std::vector> ray_plane_intersection(const controller_state & in) const; + void compute_pointer_position(controller_state & state); + + // Convert position from viewport coordinates to real-world + glm::vec3 rw_from_vp(const ImVec2 & position); ImTextureID load_texture(const std::string & filename, vk::raii::Sampler && sampler); ImTextureID load_texture(const std::string & filename); diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 0efc62ce..21fe1a61 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -32,11 +32,14 @@ #include "wivrn_client.h" #include "wivrn_packets.h" #include "xr/passthrough.h" +#include "xr/space.h" +#include #include #include "wivrn_discover.h" #include #include +#include #include #include #include @@ -101,15 +104,21 @@ static const std::array supported_depth_formats{ vk::Format::eX8D24UnormPack32, }; -void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position) +static glm::quat compute_gui_orientation(glm::vec3 head_position, glm::vec3 new_gui_position) { const float gui_pitch = -0.2; - const float keyboard_pitch = -0.6; glm::vec3 gui_direction = new_gui_position - head_position; float gui_yaw = atan2(gui_direction.x, gui_direction.z) + M_PI; - auto q = glm::quat(cos(gui_yaw / 2), 0, sin(gui_yaw / 2), 0) * glm::quat(cos(gui_pitch / 2), sin(gui_pitch / 2), 0, 0); + return glm::quat(cos(gui_yaw / 2), 0, sin(gui_yaw / 2), 0) * glm::quat(cos(gui_pitch / 2), sin(gui_pitch / 2), 0, 0); +} + +void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position) +{ + const float keyboard_pitch = -0.6; + + auto q = compute_gui_orientation(head_position, new_gui_position); auto M = glm::mat3_cast(q); // plane-to-world transform // Main window @@ -198,6 +207,8 @@ scenes::lobby::lobby() strcpy(add_server_window_hostname, ""); add_server_window_port = wivrn::default_port; add_server_tcp_only = false; + + keyboard.set_layout(application::get_config().virtual_keyboard_layout); } static std::string ip_address_to_string(const in_addr & addr) @@ -424,7 +435,7 @@ std::optional scenes::lobby::check_recenter_gesture(const std::array< return std::nullopt; } -std::optional scenes::lobby::check_recenter_action(XrTime predicted_display_time) +std::optional scenes::lobby::check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position) { std::optional> aim; @@ -440,31 +451,69 @@ std::optional scenes::lobby::check_recenter_action(XrTime predicted_d if (aim) { - if (not gui_recenter_distance) + if (not gui_recenter_position or not gui_recenter_distance) { - auto intersection = imgui_ctx->ray_plane_intersection({ + // First frame where the recenter action is active + imgui_context::controller_state state{ .active = true, .aim_position = aim->first, .aim_orientation = aim->second, - }); + }; - if (not intersection.empty()) + imgui_ctx->compute_pointer_position(state); + + if (state.pointer_position) { - gui_recenter_distance = std::clamp(intersection.front().second, 0.05f, 1.f); - spdlog::info("recentering at {}m", intersection.front().second); + 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 + 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 = 0.3; } } - const auto & v = aim->first; - const auto & q = aim->second; - return v + q * glm::vec3(0, 0, -*gui_recenter_distance); + 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; + } } else { - gui_recenter_distance.reset(); + gui_recenter_position.reset(); } return std::nullopt; @@ -610,14 +659,24 @@ void scenes::lobby::render(const XrFrameState & frame_state) imgui_ctx->set_current(); if (!async_session.valid() && !next_scene && !ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopup)) { - const auto & servers = application::get_config().servers; - for (auto && [cookie, data]: servers) + if (auto intent = application::get_intent()) { - if (data.visible && (data.autoconnect || force_autoconnect) && data.compatible && autoconnect_enabled) + connect(configuration::server_data{ + .manual = true, + .service = *intent, + }); + } + else + { + const auto & servers = application::get_config().servers; + for (auto && [cookie, data]: servers) { - autoconnect_enabled = false; - connect(data); - break; + if (data.visible && (data.autoconnect || force_autoconnect) && data.compatible && autoconnect_enabled) + { + autoconnect_enabled = false; + connect(data); + break; + } } } } @@ -644,8 +703,8 @@ void scenes::lobby::render(const XrFrameState & frame_state) if (head_position) new_gui_position = check_recenter_gui(head_position->first, head_position->second); - if (!new_gui_position) - new_gui_position = check_recenter_action(frame_state.predictedDisplayTime); + if (!new_gui_position and head_position) + new_gui_position = check_recenter_action(frame_state.predictedDisplayTime, head_position->first); if (application::get_hand_tracking_supported()) { @@ -685,33 +744,46 @@ void scenes::lobby::render(const XrFrameState & frame_state) move_gui(head_position->first, *new_gui_position); } - std::vector imgui_layers = draw_gui(frame_state.predictedDisplayTime); + std::vector> imgui_layers = draw_gui(frame_state.predictedDisplayTime); assert(renderer); renderer->start_frame(); std::vector lobby_layer_views; - std::vector depthinfo_layer_views; - std::vector depthtest_layer_views; + std::vector lobby_depth_layer_views; std::vector controllers_layer_views; + std::vector controllers_depth_layer_views; + + std::array clear_color; + + lobby_handle->visible = not application::get_config().passthrough_enabled; + + if (application::get_config().passthrough_enabled) + clear_color = {0, 0, 0, 0}; + else + clear_color = {0, 0.25, 0.5, 1}; if (composition_layer_depth_test_supported) { - std::array clear_color; + std::tie(lobby_layer_views, lobby_depth_layer_views) = render_layer( + views, + swapchains_lobby, + swapchains_lobby_depth, + *renderer, + *lobby_scene, + clear_color); + for (auto [color, depth]: std::views::zip(lobby_layer_views, lobby_depth_layer_views)) + color.next = &depth; - lobby_handle->visible = not application::get_config().passthrough_enabled; - - if (application::get_config().passthrough_enabled) - clear_color = {0, 0, 0, 0}; - else - clear_color = {0, 0.25, 0.5, 1}; - - std::tie(lobby_layer_views, depthinfo_layer_views) = render_layer(views, swapchains_lobby, swapchains_depth, *renderer, *lobby_scene, clear_color); - - for (auto [color, depth]: std::views::zip(lobby_layer_views, depthinfo_layer_views)) - { + std::tie(controllers_layer_views, controllers_depth_layer_views) = render_layer( + views, + swapchains_controllers, + swapchains_controllers_depth, + *renderer, + *controllers_scene, + {0, 0, 0, 0}); + for (auto [color, depth]: std::views::zip(controllers_layer_views, controllers_depth_layer_views)) color.next = &depth; - } renderer->end_frame(); @@ -719,23 +791,36 @@ void scenes::lobby::render(const XrFrameState & frame_state) for (auto & swapchain: swapchains_lobby) swapchain.release(); - for (auto & swapchain: swapchains_depth) + for (auto & swapchain: swapchains_lobby_depth) + swapchain.release(); + + for (auto & swapchain: swapchains_controllers) + swapchain.release(); + + for (auto & swapchain: swapchains_controllers_depth) swapchain.release(); } else { - if (not application::get_config().passthrough_enabled) - lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1}); + lobby_layer_views = render_layer( + views, + swapchains_lobby, + *renderer, + *lobby_scene, + clear_color); + + controllers_layer_views = render_layer( + views, + swapchains_controllers, + *renderer, + *controllers_scene, + {0, 0, 0, 0}); - controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0}); renderer->end_frame(); // After end_frame because the command buffers are submitted in end_frame - if (not application::get_config().passthrough_enabled) - { - for (auto & swapchain: swapchains_lobby) - swapchain.release(); - } + for (auto & swapchain: swapchains_lobby) + swapchain.release(); for (auto & swapchain: swapchains_controllers) swapchain.release(); @@ -743,7 +828,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) XrCompositionLayerProjection lobby_layer{ .type = XR_TYPE_COMPOSITION_LAYER_PROJECTION, - .layerFlags = 0, + .layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, .space = world_space, .viewCount = (uint32_t)lobby_layer_views.size(), .views = lobby_layer_views.data(), @@ -758,7 +843,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) }; XrEnvironmentBlendMode blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; - std::vector layers_base; + std::vector> layers_with_z_index; if (application::get_config().passthrough_enabled) { @@ -771,7 +856,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND; }, [&](auto & p) { - layers_base.push_back(p.layer()); + layers_with_z_index.emplace_back(-2, p.layer()); }}, passthrough); } @@ -793,54 +878,51 @@ void scenes::lobby::render(const XrFrameState & frame_state) }, }; + // Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer + // The sky in the lobby layer and the passthrough layer have the same depth, so the operation must be: + // - LESS when passthrough is enabled (to avoid overwriting the passthrough) + // - LESS_OR_EQUAL when passthrough is disabled (so that the sky is visible) + XrCompositionLayerDepthTestFB layer_depth_test{ + .type = XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB, + .next = nullptr, + .depthMask = true, + .compareOp = application::get_config().passthrough_enabled ? XR_COMPARE_OP_LESS_FB : XR_COMPARE_OP_LESS_OR_EQUAL_FB, + }; + + // if (composition_layer_depth_test_supported or not application::get_config().passthrough_enabled) + layers_with_z_index.emplace_back(-1, reinterpret_cast(&lobby_layer)); + + for (auto & [z_index, layer]: imgui_layers) + layers_with_z_index.emplace_back(z_index, reinterpret_cast(&layer)); + + layers_with_z_index.emplace_back(1000, reinterpret_cast(&controllers_layer)); + if (composition_layer_depth_test_supported) { - layers_base.push_back(reinterpret_cast(&lobby_layer)); - - for (auto & layer: imgui_layers) - layers_base.push_back(reinterpret_cast(&layer)); - - // Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer - // The sky in the lobby layer and the passthrough layer have the same depth, so the operation must be: - // - LESS when passthrough is enabled (to avoid overwriting the passthrough) - // - LESS_OR_EQUAL when passthrough is disabled (so that the sky is visible) - XrCompositionLayerDepthTestFB layer_depth_test{ - .type = XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB, - .next = nullptr, - .depthMask = true, - .compareOp = application::get_config().passthrough_enabled ? XR_COMPARE_OP_LESS_FB : XR_COMPARE_OP_LESS_OR_EQUAL_FB}; - lobby_layer.next = &layer_depth_test; - for (auto & layer: imgui_layers) + + for (auto & [z_index, layer]: imgui_layers) + { layer.next = &layer_depth_test; - - if (imgui_ctx->is_modal_popup_shown() and composition_layer_color_scale_bias_supported) - { - color_scale_bias.next = imgui_layers.front().next; - imgui_layers.front().next = &color_scale_bias; } - session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); + controllers_layer.next = &layer_depth_test; } - else + + if (imgui_ctx->is_modal_popup_shown() and composition_layer_color_scale_bias_supported) { - if (not application::get_config().passthrough_enabled) - layers_base.push_back(reinterpret_cast(&lobby_layer)); - - for (auto & layer: imgui_layers) - layers_base.push_back(reinterpret_cast(&layer)); - - if (not composition_layer_depth_test_supported) - layers_base.push_back(reinterpret_cast(&controllers_layer)); - - if (imgui_ctx->is_modal_popup_shown() and composition_layer_color_scale_bias_supported) - { - color_scale_bias.next = imgui_layers.front().next; - imgui_layers.front().next = &color_scale_bias; - } - - session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); + color_scale_bias.next = imgui_layers.front().second.next; + imgui_layers.front().second.next = &color_scale_bias; } + + std::ranges::stable_sort(layers_with_z_index); + + std::vector layers; + layers.reserve(layers_with_z_index.size()); + for (auto & [z_index, layer]: layers_with_z_index) + layers.push_back(layer); + + session.end_frame(frame_state.predictedDisplayTime, layers, blend_mode); } void scenes::lobby::on_focused() @@ -853,11 +935,10 @@ void scenes::lobby::on_focused() uint32_t height = views[0].recommendedImageRectHeight; swapchains_lobby.reserve(views.size()); + swapchains_controllers.reserve(views.size()); if (composition_layer_depth_test_supported) - swapchains_depth.reserve(views.size()); - else - swapchains_controllers.reserve(views.size()); + swapchains_lobby_depth.reserve(views.size()); for ([[maybe_unused]] auto view: views) { @@ -865,10 +946,13 @@ void scenes::lobby::on_focused() assert(view.recommendedImageRectHeight == height); swapchains_lobby.emplace_back(session, device, swapchain_format, width, height); + swapchains_controllers.emplace_back(session, device, swapchain_format, width, height); + if (composition_layer_depth_test_supported) - swapchains_depth.emplace_back(session, device, depth_format, width, height); - else - swapchains_controllers.emplace_back(session, device, swapchain_format, width, height); + { + swapchains_lobby_depth.emplace_back(session, device, depth_format, width, height); + swapchains_controllers_depth.emplace_back(session, device, depth_format, width, height); + } } spdlog::info("Created lobby swapchains: {}x{}", width, height); @@ -883,8 +967,7 @@ void scenes::lobby::on_focused() lobby_handle = lobby_scene->new_node(); lobby_scene->import(loader("ground.gltf"), lobby_handle); - if (not composition_layer_depth_test_supported) - controllers_scene.emplace(); + controllers_scene.emplace(); scene_data & controllers_scene_data = composition_layer_depth_test_supported ? *lobby_scene : *controllers_scene; input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, controllers_scene_data); @@ -928,28 +1011,42 @@ void scenes::lobby::on_focused() }); } + // 0.4mm / pixel std::vector vps{ { // Main window + .space = xr::spaces::world, .size = {0.6, 0.4}, .vp_origin = {0, 0}, .vp_size = {1500, 1000}, }, { // Pop up window - .size = {0.6, 0.2}, + .space = xr::spaces::world, + .size = {0.6, 0.24}, .vp_origin = {1500, 0}, - .vp_size = {1500, 500}, + .vp_size = {1500, 600}, }, { // Virtual keyboard + .space = xr::spaces::world, .size = {0.6, 0.2}, - .vp_origin = {1500, 500}, + .vp_origin = {1500, 600}, .vp_size = {1500, 500}, + .always_show_cursor = true, }, - }; + { + // Recenter tip + .space = xr::spaces::view, + .position = {0, -0.4, -1.0}, + .orientation = {1, 0, 0, 0}, + .size = {0.6, 0.12}, + .vp_origin = {0, 1000}, + .vp_size = {1500, 300}, + .z_index = 2000, + }}; - swapchain_imgui = xr::swapchain(session, device, swapchain_format, 3000, 1000); + swapchain_imgui = xr::swapchain(session, device, swapchain_format, 3000, 1300); imgui_ctx.emplace(physical_device, device, queue_family_index, queue, imgui_inputs, swapchain_imgui, vps); @@ -1008,7 +1105,8 @@ void scenes::lobby::on_unfocused() renderer.reset(); swapchains_lobby.clear(); swapchains_controllers.clear(); - swapchains_depth.clear(); + swapchains_lobby_depth.clear(); + swapchains_controllers_depth.clear(); swapchain_imgui = xr::swapchain(); passthrough.emplace(); multicast.reset(); diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 3e13b8bd..2d90ff37 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -39,6 +39,10 @@ #include "render/scene_renderer.h" #include "utils/async.h" +#ifdef __ANDROID__ +#include "android/battery.h" +#endif + class wivrn_session; namespace scenes @@ -79,8 +83,9 @@ class lobby : public scene_impl ImGuiID hovered_item; std::vector swapchains_lobby; + std::vector swapchains_lobby_depth; std::vector swapchains_controllers; - std::vector swapchains_depth; + std::vector swapchains_controllers_depth; xr::swapchain swapchain_imgui; vk::Format swapchain_format; vk::Format depth_format; @@ -90,18 +95,23 @@ class lobby : public scene_impl void update_server_list(); - std::vector draw_gui(XrTime predicted_display_time); + std::vector> draw_gui(XrTime predicted_display_time); XrAction recenter_left_action = XR_NULL_HANDLE; XrAction recenter_right_action = XR_NULL_HANDLE; + std::optional gui_recenter_position; std::optional gui_recenter_distance; bool recenter_gui = true; void move_gui(glm::vec3 head_position, glm::vec3 new_gui_position); + void tooltip(std::string_view text); + +#ifdef __ANDROID__ + battery battery_tracker; +#endif enum class tab { server_list, - new_server, settings, about, licenses, @@ -130,7 +140,7 @@ class lobby : public scene_impl void connect(const configuration::server_data & data); std::optional check_recenter_gesture(const std::array & joints); - std::optional check_recenter_action(XrTime predicted_display_time); + 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); public: diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index e5f0eb96..cf2baf64 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -21,12 +21,15 @@ #include "application.h" #include "asset.h" +#include "configuration.h" #include "imgui.h" #include "imgui_internal.h" #include "implot.h" #include "lobby.h" #include "stream.h" #include "version.h" +#include +#include #include #include @@ -63,6 +66,74 @@ static void CenterTextHV(const std::string & text) ImGui::PopStyleVar(); } +static void display_recentering_tip(imgui_context & ctx, const std::string & tip) +{ + ImGui::PushFont(ctx.large_font); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 20}); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 2); + ImGui::SetNextWindowPos(ctx.layers()[3].vp_center(), ImGuiCond_Always, {0.5, 0.5}); + ImGui::Begin("Recentering tip", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize); + + std::vector lines = utils::split(tip); + std::vector widths; + float max_width = 0; + + for (auto & line: lines) + { + widths.push_back(ImGui::CalcTextSize(line.c_str()).x); + max_width = std::max(max_width, widths.back()); + } + + for (auto [width, line]: std::views::zip(widths, lines)) + { + ImGui::Dummy({(max_width - width) / 2, 0}); + ImGui::SameLine(); + ImGui::TextUnformatted(line.data(), line.data() + line.size()); + } + + ImGui::End(); + ImGui::PopStyleVar(2); + ImGui::PopFont(); +} + +void scenes::lobby::tooltip(std::string_view text) +{ + // FIXME: this is incorrect if we use the docking branch of imgui + ImGuiViewport * viewport = ImGui::GetMainViewport(); + auto & layer = imgui_ctx->layer(ImGui::GetMousePos()); + auto pos_backup = viewport->Pos; + auto size_backup = viewport->Size; + viewport->Pos = ImVec2(layer.vp_origin.x, layer.vp_origin.y); + viewport->Size = ImVec2(layer.vp_size.x, layer.vp_size.y); + + ImVec2 pos{ + (ImGui::GetItemRectMin().x + ImGui::GetItemRectMax().x) / 2, + ImGui::GetItemRectMin().y - 10, + }; + + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {5, 5}); + + // Clamp position to avoid overflowing on the right + auto & style = ImGui::GetStyle(); + const ImVec2 text_size = ImGui::CalcTextSize(text.data(), text.data() + text.size(), true); + const ImVec2 size = {text_size.x + style.WindowPadding.x * 2.0f, text_size.y + style.WindowPadding.y * 2.0f}; + pos.x = std::min(pos.x, viewport->Pos.x + viewport->Size.x - size.x / 2); + + ImGui::SetNextWindowPos(pos, ImGuiCond_Always, {0.5, 1}); + if (ImGui::BeginTooltip()) + { + ImGui::PushStyleColor(ImGuiCol_Text, 0xffffffff); + ImGui::TextUnformatted(text.data(), text.data() + text.size()); + ImGui::PopStyleColor(); + ImGui::EndTooltip(); + } + + ImGui::PopStyleVar(); + + viewport->Pos = pos_backup; + viewport->Size = size_backup; +} + void scenes::lobby::vibrate_on_hover() { if (ImGui::IsItemHovered()) @@ -78,6 +149,8 @@ void scenes::lobby::gui_connecting() { if (next_scene->current_state() == scenes::stream::state::stalled) status = _("Video stream interrupted"); + else if (server_name == "") + status = fmt::format(_F("Connection ready\nStart a VR application on your computer")); else status = fmt::format(_F("Connection ready\nStart a VR application on {}"), server_name); } @@ -101,7 +174,10 @@ void scenes::lobby::gui_connecting() ImGui::Dummy({1000, 1}); ImGui::PushFont(imgui_ctx->large_font); - CenterTextH(fmt::format(_F("Connection to {}"), server_name)); + if (server_name == "") + CenterTextH(fmt::format(_F("Connection"))); + else + CenterTextH(fmt::format(_F("Connection to {}"), server_name)); ImGui::PopFont(); // ImGui::TextWrapped("%s", status.first.c_str()); @@ -122,13 +198,14 @@ void scenes::lobby::gui_connecting() void scenes::lobby::gui_new_server() { - const ImVec2 button_size(220, 80); - + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20}); ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, {10, 10}); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {10, 10}); ImGui::Dummy({1000, 1}); + const ImVec2 button_size(220, 80); + ImGui::BeginTable("table", 2); ImGui::TableSetupColumn("Field name", ImGuiTableColumnFlags_WidthFixed); @@ -166,26 +243,27 @@ void scenes::lobby::gui_new_server() ImGui::EndTable(); - ImGui::Checkbox("TCP only", &add_server_tcp_only); + ImGui::Checkbox(_S("TCP only"), &add_server_tcp_only); vibrate_on_hover(); auto top_left = ImGui::GetWindowContentRegionMin(); auto bottom_right = ImGui::GetWindowContentRegionMax(); ImGui::SetCursorPosX(top_left.x); - if (ImGui::Button("Cancel", button_size)) + if (ImGui::Button(_S("Cancel"), button_size)) { current_tab = tab::server_list; strcpy(add_server_window_prettyname, ""); strcpy(add_server_window_hostname, ""); add_server_window_port = wivrn::default_port; add_server_tcp_only = false; + ImGui::CloseCurrentPopup(); } vibrate_on_hover(); ImGui::SameLine(bottom_right.x - button_size.x); - if (ImGui::Button("Save", button_size)) + if (ImGui::Button(_S("Save"), button_size)) { current_tab = tab::server_list; configuration::server_data data{ @@ -206,10 +284,11 @@ void scenes::lobby::gui_new_server() strcpy(add_server_window_hostname, ""); add_server_window_port = wivrn::default_port; add_server_tcp_only = false; + ImGui::CloseCurrentPopup(); } vibrate_on_hover(); - ImGui::PopStyleVar(3); + ImGui::PopStyleVar(4); // ImGuiStyleVar_FrameRounding, ImGuiStyleVar_ItemSpacing, ImGuiStyleVar_CellPadding, ImGuiStyleVar_FramePadding } void scenes::lobby::gui_server_list() @@ -244,17 +323,20 @@ void scenes::lobby::gui_server_list() for (const auto & [name, cookie]: sorted_cookies) { configuration::server_data & data = config.servers.at(cookie); - bool is_selected = (cookie == selected_item); + // bool is_selected = (cookie == selected_item); ImGui::SetCursorPos(pos); - ImGui::SetNextItemAllowOverlap(); + // ImGui::SetNextItemAllowOverlap(); // TODO custom widget - if (ImGui::Selectable(("##" + cookie).c_str(), is_selected, ImGuiSelectableFlags_None, ImVec2(0, list_item_height))) - selected_item = cookie; + // if (ImGui::Selectable(("##" + cookie).c_str(), is_selected, ImGuiSelectableFlags_None, ImVec2(0, list_item_height))) + // selected_item = cookie; - ImGui::SetCursorPos(ImVec2(pos.x, pos.y)); + if (data.manual) + ImGui::SetCursorPos(ImVec2(pos.x, pos.y + 25)); // FIXME compute the position correctly + else + ImGui::SetCursorPos(ImVec2(pos.x, pos.y)); ImGui::Text("%s", name.c_str()); if (!data.manual) @@ -266,13 +348,7 @@ void scenes::lobby::gui_server_list() vibrate_on_hover(); } - // TODO - // if (ImGui::IsItemHovered()) - // { - // ImGui::SetTooltip("Tooltip"); - // } - - ImVec2 button_position(ImGui::GetWindowContentRegionMax().x - style.WindowPadding.x, pos.y + (list_item_height - button_size.y) / 2); + ImVec2 button_position(ImGui::GetWindowContentRegionMax().x, pos.y + (list_item_height - button_size.y) / 2); button_position.x -= button_size.x + style.WindowPadding.x; ImGui::SetCursorPos(button_position); @@ -302,9 +378,9 @@ void scenes::lobby::gui_server_list() if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) { if (!data.compatible && !data.manual) - ImGui::SetTooltip("%s", _S("Incompatible server version")); + tooltip(_("Incompatible server version")); else if (!data.visible && !data.manual) - ImGui::SetTooltip("%s", _S("Server not available")); + tooltip(_("Server not available")); } ImGui::PopStyleColor(3); @@ -350,6 +426,14 @@ void scenes::lobby::gui_server_list() gui_connecting(); ImGui::EndPopup(); } + + ImGui::SetNextWindowSize({800, 0}); + ImGui::SetNextWindowPos({popup_layer_center.x, popup_layer_center.y}, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f)); + if (ImGui::BeginPopupModal("add server", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize)) + { + gui_new_server(); + ImGui::EndPopup(); + } ImGui::PopStyleVar(3); } @@ -459,7 +543,7 @@ void scenes::lobby::gui_settings() config.save(); vibrate_on_hover(); if (ImGui::IsItemHovered()) - ImGui::SetTooltip("%s", _S("Overlay can be toggled by pressing both thumbsticks")); + tooltip(_("Overlay can be toggled by pressing both thumbsticks")); ImGui::PopStyleVar(); @@ -626,21 +710,29 @@ static bool RadioButtonWithoutCheckBox(const std::string & label, T * v, T v_but return pressed; } -static void ScrollWhenDraggingOnVoid(ImVec2 delta) +static void ScrollWhenDraggingOnVoid() { + ImVec2 delta{0.0f, -ImGui::GetIO().MouseDelta.y}; + // https://github.com/ocornut/imgui/issues/3379#issuecomment-1678718752 ImGuiContext & g = *ImGui::GetCurrentContext(); ImGuiWindow * window = g.CurrentWindow; bool hovered = false; bool held = false; + static bool held_prev = false; + + // Don't drag for the first frame because the current controller might have just changed and have a large delta + ImGuiID id = window->GetID("##scrolldraggingoverlay"); ImGui::KeepAliveID(id); if (g.HoveredId == 0) // If nothing hovered so far in the frame (not same as IsAnyItemHovered()!) ImGui::ButtonBehavior(window->Rect(), id, &hovered, &held, ImGuiButtonFlags_MouseButtonLeft); - if (held && delta.x != 0.0f) + if (held and held_prev and delta.x != 0.0f) ImGui::SetScrollX(window, window->Scroll.x + delta.x); - if (held && delta.y != 0.0f) + if (held and held_prev and delta.y != 0.0f) ImGui::SetScrollY(window, window->Scroll.y + delta.y); + + held_prev = held; } static auto face_weights() @@ -848,13 +940,15 @@ static const char * get_face_icon(XrTime predicted_display_time) void scenes::lobby::draw_features_status(XrTime predicted_display_time) { - const float win_width = ImGui::GetWindowSize().x; + const float win_width = ImGui::GetContentRegionAvail().x; float text_width = 0; auto & config = application::get_config(); struct item { feature f; + std::string tooltip_enabled; + std::string tooltip_disabled; const char * icon_enabled; const char * icon_disabled = ICON_FA_SLASH; bool enabled; @@ -864,6 +958,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) items.push_back({ .f = feature::microphone, + .tooltip_enabled = _("Microphone is enabled"), + .tooltip_disabled = _("Microphone is disabled"), .icon_enabled = ICON_FA_MICROPHONE, .icon_disabled = ICON_FA_MICROPHONE_SLASH, }); @@ -872,6 +968,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) { items.push_back({ .f = feature::hand_tracking, + .tooltip_enabled = _("Hand tracking is enabled"), + .tooltip_disabled = _("Hand tracking is disabled"), .icon_enabled = ICON_FA_HAND, }); } @@ -880,6 +978,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) { items.push_back({ .f = feature::eye_gaze, + .tooltip_enabled = _("Eye tracking is enabled"), + .tooltip_disabled = _("Eye tracking is disabled"), .icon_enabled = ICON_FA_EYE, .icon_disabled = ICON_FA_EYE_SLASH, }); @@ -889,6 +989,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) { items.push_back({ .f = feature::face_tracking, + .tooltip_enabled = _("Face tracking is enabled"), + .tooltip_disabled = _("Face tracking is disabled"), .icon_enabled = get_face_icon(predicted_display_time), .icon_disabled = ICON_FA_FACE_MEH_BLANK, }); @@ -904,6 +1006,13 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) const ImGuiStyle & style = ImGui::GetStyle(); text_width += items.size() * style.FramePadding.x * 2; + // New server button + if (ImGui::Button(_S("Add server")) && !ImGui::IsPopupOpen("add server")) + ImGui::OpenPopup("add server"); + vibrate_on_hover(); + ImGui::SameLine(); + + // Enabled features ImGui::SetCursorPosX((win_width - text_width) / 2); for (auto & i: items) { @@ -919,25 +1028,121 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) config.set_feature(i.f, not i.enabled); config.save(); } + + vibrate_on_hover(); + if (ImGui::IsItemHovered()) + tooltip(i.enabled ? i.tooltip_enabled : i.tooltip_disabled); + if (i.icon_disabled == std::string_view(ICON_FA_SLASH) and not i.enabled) { - auto save = ImGui::GetCursorPos(); + auto save = ImGui::GetCurrentWindow()->DC; ImGui::SetCursorPos(pos + ImGui::GetStyle().FramePadding); ImGui::Text("%s", i.icon_enabled); - ImGui::SetCursorPos(save); + ImGui::GetCurrentWindow()->DC = save; } - vibrate_on_hover(); ImGui::PopStyleColor(2); ImGui::PopStyleVar(); } + + float PrevLineSize = ImGui::GetCurrentWindow()->DC.PrevLineSize.y; + float PrevLineTextBaseOffset = ImGui::GetCurrentWindow()->DC.PrevLineTextBaseOffset; + +#ifdef __ANDROID__ + // Battery status + auto status = battery_tracker.get(); + + const char * battery_icon = nullptr; + if (status.charge) + { + int icon_nr; + + if (status.charging) + { + if (*status.charge > 0.995) + icon_nr = 5; + else + icon_nr = application::now() / 500'000'000 % 5; + } + else + icon_nr = std::round((*status.charge) * 4); + + switch (icon_nr) + { + case 0: + battery_icon = ICON_FA_BATTERY_EMPTY; + break; + case 1: + battery_icon = ICON_FA_BATTERY_QUARTER; + break; + case 2: + battery_icon = ICON_FA_BATTERY_HALF; + break; + case 3: + battery_icon = ICON_FA_BATTERY_THREE_QUARTERS; + break; + case 4: + battery_icon = ICON_FA_BATTERY_FULL; + break; + case 5: + battery_icon = ICON_FA_PLUG; + } + + ImGui::SameLine(); + + // Always use the longest width for layout + float max_battery_width = ImGui::CalcTextSize(ICON_FA_BATTERY_FULL "100%").x; + ImVec4 battery_color = ImGui::GetStyleColorVec4(ImGuiCol_Text); + + if (*status.charge < 0.2) + battery_color = ImVec4(1, 0, 0, 1); + + ImGui::SetCursorPosX(win_width - max_battery_width - style.WindowPadding.x); + ImGui::TextColored(battery_color, "%s %d%%", battery_icon, (int)std::round(*status.charge * 100)); + } +#endif + + ImGui::Dummy({0, 15}); } void scenes::lobby::gui_keyboard() { keyboard.display(hovered_item); + + auto & config = application::get_config(); + + if (keyboard.get_layout() != config.virtual_keyboard_layout) + { + config.virtual_keyboard_layout = keyboard.get_layout(); + config.save(); + } } -std::vector scenes::lobby::draw_gui(XrTime predicted_display_time) +static bool is_gui_visible(imgui_context & ctx, XrTime predicted_display_time) +{ + // Get the GUI position in the view reference frame + if (auto pos = application::locate_controller(application::space(xr::spaces::world), application::space(xr::spaces::view), predicted_display_time)) + { + glm::vec3 view_gui_position = pos->first + pos->second * ctx.layers()[0].position; + + float gui_distance = glm::length(view_gui_position); + glm::vec3 direction = view_gui_position / gui_distance; + + if (view_gui_position.z > 0 or view_gui_position.z < -1.5) + return false; + + if (std::abs(direction.x) > 0.8) + return false; + + if (std::abs(direction.y) > 0.8) + return false; + + return true; + } + + return true; +} + +std::vector> scenes::lobby::draw_gui(XrTime predicted_display_time) { for (const auto & [key, server]: application::get_config().servers) { @@ -955,7 +1160,6 @@ std::vector scenes::lobby::draw_gui(XrTime predicted_dis if (ImGui::GetIO().WantTextInput) { ImGui::SetNextWindowPos(imgui_ctx->layers()[2].vp_center(), ImGuiCond_Always, {0.5, 0.5}); - gui_keyboard(); } @@ -972,18 +1176,18 @@ std::vector scenes::lobby::draw_gui(XrTime predicted_dis { ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10); - ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX() - 20, 0)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 20}); + ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); ImGui::SetCursorPosY(20); switch (current_tab) { case tab::server_list: + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {10, 10}); draw_features_status(predicted_display_time); - gui_server_list(); - break; + ImGui::PopStyleVar(); - case tab::new_server: - gui_new_server(); + gui_server_list(); break; case tab::settings: @@ -1011,15 +1215,13 @@ std::vector scenes::lobby::draw_gui(XrTime predicted_dis ImGui::Dummy(ImVec2(0, 20)); - ImVec2 mouse_delta = ImGui::GetIO().MouseDelta; - ScrollWhenDraggingOnVoid(ImVec2(0.0f, -mouse_delta.y)); + ScrollWhenDraggingOnVoid(); ImGui::EndChild(); - ImGui::PopStyleVar(); // ImGuiStyleVar_FrameRounding + ImGui::PopStyleVar(2); // ImGuiStyleVar_FrameRounding, ImGuiStyleVar_WindowPadding } ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(0, 0, 0, 255)); ImGui::SetCursorPos(style.WindowPadding); - { ImGui::BeginChild("Tabs", {TabWidth, ImGui::GetContentRegionMax().y - ImGui::GetWindowContentRegionMin().y}); @@ -1027,9 +1229,6 @@ std::vector scenes::lobby::draw_gui(XrTime predicted_dis RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Server list"), ¤t_tab, tab::server_list, {TabWidth, 0}); vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("New server"), ¤t_tab, tab::new_server, {TabWidth, 0}); - vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), ¤t_tab, tab::settings, {TabWidth, 0}); vibrate_on_hover(); @@ -1051,6 +1250,14 @@ std::vector scenes::lobby::draw_gui(XrTime predicted_dis ImGui::PopStyleColor(); // ImGuiCol_WindowBg ImGui::PopStyleVar(2); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_ScrollbarSize + if (not is_gui_visible(*imgui_ctx, predicted_display_time)) + { + if (application::get_hand_tracking_supported()) + display_recentering_tip(*imgui_ctx, _("Press A or X or put your palm up\nto move the main window")); + else + display_recentering_tip(*imgui_ctx, _("Press A or X to move the main window")); + } + if (hovered_item != last_hovered && hovered_item != 0) { size_t controller = imgui_ctx->get_focused_controller(); diff --git a/client/scenes/lobby_keyboard.cpp b/client/scenes/lobby_keyboard.cpp index 8b9a8c5d..ab116883 100644 --- a/client/scenes/lobby_keyboard.cpp +++ b/client/scenes/lobby_keyboard.cpp @@ -19,148 +19,148 @@ #define IMGUI_DEFINE_MATH_OPERATORS +#include +#include #include #include #include #include -#include +#include +#include +#include #include "IconsFontAwesome6.h" #include "imgui.h" #include "imgui_internal.h" #include "lobby_keyboard.h" -// #include -// #include -// #include -// #include // u_strToUpper - static const ImGuiKey key_layout = (ImGuiKey)(ImGuiKey_NamedKey_END + 1); static const ImGuiKey key_symbols_letter = (ImGuiKey)(ImGuiKey_NamedKey_END + 2); // See https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fallback/main.qml virtual_keyboard::layout qwerty = { { - {0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"q"}, - {1, u"w"}, - {1, u"eéèêë"}, - {1, u"rŕrř"}, - {1, u"tţtŧť"}, - {1, u"yÿyýŷ"}, - {1, u"uűūũûüuùú"}, - {1, u"iîïīĩiìí"}, - {1, u"oœøõôöòó"}, - {1, u"p"}, - {1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat}, + {0.5}, + {1, "q"}, + {1, "w"}, + {1, "eéèêë"}, + {1, "rŕrř"}, + {1, "tţtŧť"}, + {1, "yÿyýŷ"}, + {1, "uűūũûüuùú"}, + {1, "iîïīĩiìí"}, + {1, "oœøõôöòó"}, + {1, "p"}, + {1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat}, }, // 12 { - {1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"aaäåãâàá"}, - {1, u"sšsşś"}, - {1, u"ddđď"}, - {1, u"f"}, - {1, u"gġgģĝğ"}, - {1, u"h"}, - {1, u"j"}, - {1, u"k"}, - {1, u"lĺŀłļľl"}, - {2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, + {1}, + {1, "aaäåãâàá"}, + {1, "sšsşś"}, + {1, "ddđď"}, + {1, "f"}, + {1, "gġgģĝğ"}, + {1, "h"}, + {1, "j"}, + {1, "k"}, + {1, "lĺŀłļľl"}, + {2}, }, // 12 { - {1.5, u"", ImGuiKey_LeftShift}, - {1, u"zzžż"}, - {1, u"x"}, - {1, u"cçcċčć"}, - {1, u"v"}, - {1, u"b"}, - {1, u"nņńnň"}, - {1, u"m"}, - {1, u","}, - {1, u"."}, + {1.5, "", ImGuiKey_LeftShift}, + {1, "zzžż"}, + {1, "x"}, + {1, "cçcċčć"}, + {1, "v"}, + {1, "b"}, + {1, "nņńnň"}, + {1, "m"}, + {1, ","}, + {1, "."}, // {1, u"-"}, - {1.5, u"", ImGuiKey_RightShift}, + {1.5, "", ImGuiKey_RightShift}, }, // 12 { - {2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"", key_symbols_letter, "?123"}, - {1, u"", key_layout, ICON_FA_GLOBE}, - {5, u" "}, - {3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, + {2}, + {1, "?123", key_symbols_letter}, + {1, ICON_FA_GLOBE, key_layout}, + {5, " "}, + {3}, } // 12 }; // See https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fr_FR/main.qml virtual_keyboard::layout azerty = { { - {0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"aàâæ"}, - {1, u"z"}, - {1, u"eéèêë"}, - {1, u"r"}, - {1, u"t"}, - {1, u"yÿ"}, - {1, u"uùûü"}, - {1, u"iîï"}, - {1, u"oôœ"}, - {1, u"p"}, - {1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat}, + {0.5}, + {1, "aàâæ"}, + {1, "z"}, + {1, "eéèêë"}, + {1, "r"}, + {1, "t"}, + {1, "yÿ"}, + {1, "uùûü"}, + {1, "iîï"}, + {1, "oôœ"}, + {1, "p"}, + {1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat}, }, // 12 { - {1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"q"}, - {1, u"s"}, - {1, u"d"}, - {1, u"f"}, - {1, u"g"}, - {1, u"h"}, - {1, u"j"}, - {1, u"k"}, - {1, u"l"}, - {1, u"m"}, - {1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, + {1}, + {1, "q"}, + {1, "s"}, + {1, "d"}, + {1, "f"}, + {1, "g"}, + {1, "h"}, + {1, "j"}, + {1, "k"}, + {1, "l"}, + {1, "m"}, + // {1, "é"}, + {1}, }, // 12 { - {1.5, u"", ImGuiKey_LeftShift}, - {1, u"w"}, - {1, u"x"}, - {1, u"cç"}, - {1, u"v"}, - {1, u"b"}, - {1, u"n"}, - {1, u","}, - {1, u"."}, - {1, u"-"}, - {1.5, u"", ImGuiKey_RightShift}, + {1.5, "", ImGuiKey_LeftShift}, + {1, "w"}, + {1, "x"}, + {1, "cç"}, + {1, "v"}, + {1, "b"}, + {1, "n"}, + {1, ","}, + {1, "."}, + {1, "-"}, + {1.5, "", ImGuiKey_RightShift}, }, // 12 { - {2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"", key_symbols_letter, "?123"}, - {1, u"", key_layout, ICON_FA_GLOBE}, - {5, u" "}, - {3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, + {2}, + {1, "?123", key_symbols_letter}, + {1, ICON_FA_GLOBE, key_layout}, + {5, " "}, + {3}, } // 12 }; virtual_keyboard::layout digits = { { - {1, u"1"}, - {1, u"2"}, - {1, u"3"}, + {1, "1"}, + {1, "2"}, + {1, "3"}, }, // 3 { - {1, u"4"}, - {1, u"5"}, - {1, u"6"}, + {1, "4"}, + {1, "5"}, + {1, "6"}, }, // 3 { - {1, u"7"}, - {1, u"8"}, - {1, u"9"}, + {1, "7"}, + {1, "8"}, + {1, "9"}, }, // 3 { - {2, u"0"}, - {1, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat}, + {2, "0"}, + {1, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat}, } // 3 }; @@ -168,120 +168,126 @@ virtual_keyboard::layout digits = { // virtual_keyboard::layout qwertz = { // }; -std::array layouts = {&qwerty, &azerty}; - virtual_keyboard::layout symbols = { { - {0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"1"}, - {1, u"2"}, - {1, u"3"}, - {1, u"4"}, - {1, u"5"}, - {1, u"6"}, - {1, u"7"}, - {1, u"8"}, - {1, u"9"}, - {1, u"0"}, - {1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat}, + {0.5}, + {1, "1"}, + {1, "2"}, + {1, "3"}, + {1, "4"}, + {1, "5"}, + {1, "6"}, + {1, "7"}, + {1, "8"}, + {1, "9"}, + {1, "0"}, + {1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat}, }, // 12 { - {1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"@"}, - {1, u"#"}, - {1, u"%"}, - {1, u"&"}, - {1, u"*"}, - {1, u"_"}, - {1, u"-"}, - {1, u"+"}, - {1, u"("}, - {1, u")"}, - {1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, + {1}, + {1, "@"}, + {1, "#"}, + {1, "%"}, + {1, "&"}, + {1, "*"}, + {1, "_"}, + {1, "-"}, + {1, "+"}, + {1, "("}, + {1, ")"}, + {1}, }, // 12 { - {1.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, - {1, u"", ImGuiKey_None, "1/2", virtual_keyboard::key_flag_hidden}, - {1, u"\""}, - {1, u"<"}, - {1, u">"}, - {1, u"'"}, - {1, u":"}, - {1, u"/"}, - {1, u"!"}, - {1, u"?"}, - {1.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, + {1.5}, + {1}, // {1, "", ImGuiKey_None, "1/2", virtual_keyboard::key_flag_hidden}, + {1, "\""}, + {1, "<"}, + {1, ">"}, + {1, "'"}, + {1, ":"}, + {1, "/"}, + {1, "!"}, + {1, "?"}, + {1.5}, }, // 12 { - {1, u"", key_symbols_letter, "ABC"}, - {1, u"", key_layout, ICON_FA_GLOBE}, - {5, u" "}, - {3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden}, + {1, "ABC", key_symbols_letter}, + {1, ICON_FA_GLOBE, key_layout}, + {5, " "}, + {3}, }, }; -static std::string to_utf8(char32_t c) -{ - std::string s; +std::array layouts = { + std::make_pair("QWERTY", &qwerty), + std::make_pair("AZERTY", &azerty), +}; - if (c < 0x80) +virtual_keyboard::key::key(float width, std::string_view characters, ImGuiKey keycode, key_flags flag) : + width(width), + keycode(keycode), + flag(flag) +{ + if (keycode == ImGuiKey_None) { - s += (char)c; - } - else if (c < 0x800) - { - s += (char)(0xc0 | ((c >> 6) & 0x1f)); - s += (char)(0x80 | (c & 0x3f)); - } - else if (c < 0x10000) - { - s += (char)(0xe0 | ((c >> 12) & 0x0f)); - s += (char)(0x80 | ((c >> 6) & 0x3f)); - s += (char)(0x80 | (c & 0x3f)); + // Normal key: the first character is displayed on the key + characters_lower = characters; + characters_upper = una::cases::to_uppercase_utf8(characters); + + glyph_lower = *una::ranges::views::grapheme::utf8(characters_lower).begin(); + glyph_upper = *una::ranges::views::grapheme::utf8(characters_upper).begin(); } else { - s += (char)(0xf0 | ((c >> 18) & 0x07)); - s += (char)(0x80 | ((c >> 12) & 0x3f)); - s += (char)(0x80 | ((c >> 6) & 0x3f)); - s += (char)(0x80 | (c & 0x3f)); + // Special key: characters contains the key label, except shift which depends + // on caps lock + assert(not characters.empty() or keycode == ImGuiKey_LeftShift or keycode == ImGuiKey_RightShift); + glyph_lower = (std::string)characters; + glyph_upper = (std::string)characters; } - - return s; } -// static std::string to_utf8(std::u16string_view str) -// { -// std::string s; -// for(char16_t c: str) -// s += to_utf8(c); -// -// return s; -// } -// -// static std::u16string str_to_upper(std::u16string_view text) -// { -// UErrorCode err = U_ZERO_ERROR; -// -// size_t size = u_strToUpper(nullptr, 0, text.data(), text.size(), "" /* locale*/, &err); -// -// if (U_FAILURE(err)) -// throw std::runtime_error(std::string("u_strToUpper: ") + u_errorName(err)); -// -// std::u16string upper; -// upper.resize(size); -// -// err = U_ZERO_ERROR; -// u_strToUpper(nullptr, 0, text.data(), text.size(), "" /* locale*/, &err); -// -// if (U_FAILURE(err)) -// throw std::runtime_error(std::string("u_strToUpper: ") + u_errorName(err)); -// -// return upper; -// } +virtual_keyboard::key::key(float width) : + width(width), + keycode(ImGuiKey_None), + flag(virtual_keyboard::key_flag_hidden) +{ +} + +// Reimplemented because ImGui::IsWindowHovered() returns false if a popup is open +bool virtual_keyboard::is_window_hovered() +{ + int i = 0; + ImGuiContext & g = *GImGui; + + if (not ImGui::IsMouseHoveringRect(g.CurrentWindow->Pos, g.CurrentWindow->Pos + g.CurrentWindow->Size)) + return false; + + // Ignore any window below the keyboard + while (g.Windows[i] != g.CurrentWindow and i < g.Windows.Size) + i++; + + // Cannot find the keyboard window + if (i >= g.Windows.Size) + return false; + + // Skip the keyboard window + i++; + + // Check if another window is over the keyboard + while (i < g.Windows.Size) + { + if (ImGui::IsMouseHoveringRect(g.Windows[i]->Pos, g.Windows[i]->Pos + g.Windows[i]->Size)) + return false; + + i++; + } + + return true; +} // Mostly copied from ImGui::ButtonBehavior, massively simplified for the keyboard use case, don't take focus when clicked -bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out_hovered, bool * out_held, ImGuiButtonFlags flags) +virtual_keyboard::key_status virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool window_hovered, ImGuiButtonFlags flags) { ImGuiContext & g = *GImGui; @@ -294,37 +300,31 @@ bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out } bool pressed = false; - bool hovered = g.HoveredWindow == g.CurrentWindow and ImGui::IsMouseHoveringRect(bb.Min, bb.Max); - // ImGui::ItemHoverable; + bool hovered = window_hovered and ImGui::IsMouseHoveringRect(bb.Min, bb.Max); + + bool mouse_down = ImGui::IsMouseDown(0, id); // Mouse handling - if (hovered) + if (hovered and mouse_down and active_id != id) { - // Changed wrt original imgui: only 1 button is present - bool mouse_button_clicked = ImGui::IsMouseClicked(0, ImGuiInputFlags_None, id); + active_id = id; + held_duration = 0; - // Process initial action - if (mouse_button_clicked && active_id != id) - { - active_id = id; - held_duration = 0; - - if (flags & ImGuiButtonFlags_PressedOnClick) - pressed = true; - } + if (flags & ImGuiButtonFlags_PressedOnClick) + pressed = true; } // Process while held bool held = false; if (active_id == id) { - if (ImGui::IsMouseDown(0, id)) + if (mouse_down) { held = true; float prev_held_duration = held_duration; held_duration += g.IO.DeltaTime; - if (flags & ImGuiButtonFlags_Repeat and held_duration > g.IO.KeyRepeatDelay) + if ((flags & ImGuiButtonFlags_Repeat) and held_duration > g.IO.KeyRepeatDelay) { int n1 = (prev_held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate; int n2 = (held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate; @@ -335,54 +335,41 @@ bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out } else { - if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease)) + if (hovered and (flags & ImGuiButtonFlags_PressedOnClickRelease)) pressed = true; active_id = 0; } } - if (out_hovered) - *out_hovered = hovered; - - if (out_held) - *out_held = held; - - return pressed; + return key_status{ + .hovered = hovered, + .held = held, + .pressed = pressed, + .hold_duration = held ? held_duration : 0}; } -bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool & hovered) +virtual_keyboard::key_status virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool window_hovered) { - std::string key_label; + bool is_shift = k.keycode == ImGuiKey_LeftShift or k.keycode == ImGuiKey_RightShift; - bool is_shift = k.key == ImGuiKey_LeftShift or k.key == ImGuiKey_RightShift; - - if (k.characters.empty()) - { - if (is_shift) - key_label = (current_case_mode == case_mode::lower) ? ICON_FA_CHEVRON_UP : ICON_FA_CIRCLE_CHEVRON_UP; - else - key_label = k.glyph; - } - else if (current_case_mode == case_mode::lower) - { - key_label = to_utf8(k.characters[0]); - } - else - { - // FIXME: use ICU - key_label = to_utf8(/*u_*/ towupper(k.characters[0])); - } + const std::string & key_label = is_shift + ? (current_case_mode == case_mode::lower) + ? ICON_FA_CHEVRON_UP + : ICON_FA_CIRCLE_CHEVRON_UP + : ((current_case_mode == case_mode::lower) + ? k.glyph_lower + : k.glyph_upper); std::string key_label_id; - if (k.characters.empty()) - key_label_id = "virtual_keyboard_key_" + std::to_string(k.key); + if (k.keycode != ImGuiKey_None) + key_label_id = "virtual_keyboard_key_" + std::to_string(k.keycode); else - key_label_id = "virtual_keyboard_" + to_utf8(k.characters[0]); + key_label_id = "virtual_keyboard_" + std::string(k.glyph_lower); ImGuiWindow * window = ImGui::GetCurrentWindow(); if (window->SkipItems) - return false; + return {}; ImGuiContext & g = *GImGui; const ImGuiStyle & style = g.Style; @@ -395,25 +382,23 @@ bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_ar const ImRect bb(pos, pos + size); ImGui::ItemSize(size, style.FramePadding.y); if (!ImGui::ItemAdd(bb, id)) - return false; + return {}; ImGuiButtonFlags flags = ImGuiButtonFlags_None; if (k.flag & key_flag_repeat) flags |= ImGuiButtonFlags_Repeat; - bool held; - bool pressed = button_behavior(bb, id, &hovered, &held, flags); + auto status = button_behavior(bb, id, window_hovered, flags); // Render - bool active = (held and hovered) or (is_shift and current_case_mode == case_mode::caps_lock); + bool active = (status.held and status.hovered) or (is_shift and current_case_mode == case_mode::caps_lock); - const ImU32 col = ImGui::GetColorU32(active ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered - : ImGuiCol_Button); + const ImU32 col = ImGui::GetColorU32(active ? ImGuiCol_ButtonActive : status.hovered ? ImGuiCol_ButtonHovered + : ImGuiCol_Button); ImGui::RenderNavHighlight(bb, id); ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); - // ImGui::RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, key_label.c_str(), nullptr, &label_size, style.ButtonTextAlign, &bb); ImGui::RenderTextClippedEx( g.CurrentWindow->DrawList, bb.Min + style.FramePadding, @@ -424,52 +409,61 @@ bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_ar style.ButtonTextAlign, &bb); - if (pressed) - { + if (status.pressed) press_single_key(k); - } - return pressed; + return status; } void virtual_keyboard::press_single_key(const key & k) { - if (k.key == ImGuiKey_LeftShift or k.key == ImGuiKey_RightShift) + switch ((int)k.keycode) // cast to int to avoid -Werror=switch { - switch (current_case_mode) - { - case case_mode::lower: - current_case_mode = case_mode::upper; - break; - case case_mode::upper: - current_case_mode = case_mode::caps_lock; - break; - case case_mode::caps_lock: - current_case_mode = case_mode::lower; - break; - } - } - else if (k.key != ImGuiKey_None) - { - ImGui::GetIO().AddKeyEvent(k.key, true); - ImGui::GetIO().AddKeyEvent(k.key, false); - } - else if (not k.characters.empty()) - { - switch (current_case_mode) - { - case case_mode::lower: - ImGui::GetIO().AddInputCharactersUTF8(to_utf8(k.characters[0]).c_str()); - break; + case ImGuiKey_LeftShift: + case ImGuiKey_RightShift: + switch (current_case_mode) + { + case case_mode::lower: + current_case_mode = case_mode::upper; + break; + case case_mode::upper: + current_case_mode = case_mode::caps_lock; + break; + case case_mode::caps_lock: + current_case_mode = case_mode::lower; + break; + } + break; - case case_mode::upper: - current_case_mode = case_mode::lower; - [[fallthrough]]; + case key_layout: + current_layout = (current_layout + 1) % layouts.size(); + break; - case case_mode::caps_lock: - ImGui::GetIO().AddInputCharactersUTF8(to_utf8(towupper(k.characters[0])).c_str()); - break; - } + case key_symbols_letter: + symbols_shown = not symbols_shown; + break; + + case ImGuiKey_None: + switch (current_case_mode) + { + case case_mode::lower: + ImGui::GetIO().AddInputCharactersUTF8(std::string(*una::ranges::views::grapheme::utf8(k.characters_lower).begin()).c_str()); + break; + + case case_mode::upper: + current_case_mode = case_mode::lower; + [[fallthrough]]; + + case case_mode::caps_lock: + ImGui::GetIO().AddInputCharactersUTF8(std::string(*una::ranges::views::grapheme::utf8(k.characters_upper).begin()).c_str()); + break; + } + break; + + default: + ImGui::GetIO().AddKeyEvent(k.keycode, true); + ImGui::GetIO().AddKeyEvent(k.keycode, false); + break; } } @@ -481,7 +475,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id) bool want_digits = input_state and (input_state->Flags & ImGuiInputTextFlags_CharsDecimal) != 0; auto & layout = want_digits ? digits : symbols_shown ? symbols - : *layouts[current_layout]; + : *layouts[current_layout].second; ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {8, 8}); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 16); @@ -499,7 +493,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id) ImVec2 padding = ImGui::GetStyle().WindowPadding; size = {size.x - 2 * padding.x, size.y - 2 * padding.y}; - // Compute keys size; + // Compute keys size: float key_height = [&]() { int N = layout.size(); @@ -526,6 +520,8 @@ void virtual_keyboard::display(ImGuiID & hovered_id) ImVec2 row_position = ImGui::GetCursorPos(); int id = 0; + bool window_hovered = is_window_hovered(); + for (const auto & row: layout) { ImVec2 key_position = row_position; @@ -541,25 +537,8 @@ void virtual_keyboard::display(ImGuiID & hovered_id) { ImGui::SetCursorPos(key_position); - bool is_hovered; - if (draw_single_key(key, id++, ImVec2{base_key_width * key.width - style.ItemSpacing.x, key_height}, is_hovered)) - { - switch ((int)key.key) // cast to int to avoid -Werror=switch - { - case key_layout: - current_layout = (current_layout + 1) % layouts.size(); - break; - - case key_symbols_letter: - symbols_shown = not symbols_shown; - break; - - default: - break; - } - } - - if (is_hovered) + auto status = draw_single_key(key, id++, ImVec2{base_key_width * key.width - style.ItemSpacing.x, key_height}, window_hovered); + if (status.hovered) hovered_id = ImGui::GetItemID(); } @@ -573,7 +552,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id) ImGui::SetCursorPos(row_position); - if (ImGui::IsWindowHovered()) + if (is_window_hovered()) { ImGui::GetIO().MouseDown[0] = false; ImGui::GetIO().MouseClicked[0] = false; @@ -583,3 +562,20 @@ void virtual_keyboard::display(ImGuiID & hovered_id) ImGui::PopStyleColor(); // ImGuiCol_WindowBg ImGui::PopStyleVar(4); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_WindowRounding, ImGuiStyleVar_ItemSpacing, ImGuiStyleVar_FrameRounding } + +const std::string_view virtual_keyboard::get_layout() const +{ + return layouts[current_layout].first; +} + +void virtual_keyboard::set_layout(std::string_view layout_name) +{ + for (int i = 0, n = layouts.size(); i < n; ++i) + { + if (layouts[i].first == layout_name) + { + current_layout = i; + return; + } + } +} diff --git a/client/scenes/lobby_keyboard.h b/client/scenes/lobby_keyboard.h index 6c9ea25d..0a91c9cd 100644 --- a/client/scenes/lobby_keyboard.h +++ b/client/scenes/lobby_keyboard.h @@ -20,7 +20,7 @@ #pragma once #include "imgui.h" -#include +#include #include struct ImRect; @@ -38,14 +38,20 @@ public: struct key { float width; - const std::u16string_view characters; - ImGuiKey key = ImGuiKey_None; - const char * glyph = nullptr; + std::string characters_lower; + std::string characters_upper; + ImGuiKey keycode = ImGuiKey_None; + std::string glyph_lower; + std::string glyph_upper; key_flags flag = key_flag_none; + + key(float width, std::string_view characters, ImGuiKey keycode = ImGuiKey_None, key_flags flag = key_flag_none); + key(float width); }; using layout = std::vector>; +private: enum class case_mode { lower, @@ -53,10 +59,18 @@ public: caps_lock }; -private: - bool button_behavior(const ImRect & bb, ImGuiID id, bool * out_hovered, bool * out_held, ImGuiButtonFlags flags = 0); - bool draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool & hovered); + struct key_status + { + bool hovered = false; + bool held = false; + bool pressed = false; + float hold_duration = 0; + }; + + key_status button_behavior(const ImRect & bb, ImGuiID id, bool window_hovered, ImGuiButtonFlags flags = 0); + key_status draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool window_hovered); void press_single_key(const key & k); + bool is_window_hovered(); ImGuiID active_id = 0; float held_duration = 0; @@ -67,4 +81,6 @@ private: public: void display(ImGuiID & hovered); + const std::string_view get_layout() const; + void set_layout(std::string_view layout_name); }; diff --git a/client/scenes/stream_stats.cpp b/client/scenes/stream_stats.cpp index 8140e83e..c39ef94d 100644 --- a/client/scenes/stream_stats.cpp +++ b/client/scenes/stream_stats.cpp @@ -320,5 +320,9 @@ std::vector scenes::stream::plot_performance_metrics(XrT } ImGui::End(); - return imgui_ctx->end_frame(); + std::vector layers; + for (auto & layer: imgui_ctx->end_frame()) + layers.push_back(layer.second); + + return layers; } diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 63cd2fd5..44f00bb6 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -24,6 +24,7 @@ #include #ifdef __ANDROID__ +#include "android/battery.h" #include "android/jnipp.h" #endif @@ -159,22 +160,8 @@ void scenes::stream::tracking() #ifdef __ANDROID__ // Runtime may use JNI and needs the thread to be attached application::instance().setup_jni(); - jni::object<""> act(application::native_app()->activity->clazz); - auto app = act.call>("getApplication"); - auto ctx = app.call>("getApplicationContext"); - jni::string filter_jstr("android.intent.action.BATTERY_CHANGED"); - jni::string level_jstr("level"); - jni::string scale_jstr("scale"); - jni::string plugged_jstr("plugged"); - jni::Int default_jint(-1); - - auto receiver_jobj = jni::object<"android/content/BroadcastReceiver">(NULL); - auto filter_jobj = jni::new_object<"android/content/IntentFilter">(filter_jstr); - - auto register_receiver = jni::klass("android/content/Context") - .method>("registerReceiver", receiver_jobj, filter_jobj); - auto get_int_extra = jni::klass("android/content/Intent").method("getIntExtra", level_jstr, default_jint); + battery battery_tracker; XrTime next_battery_check = 0; const XrDuration battery_check_interval = 30'000'000'000; // 30s @@ -313,24 +300,13 @@ void scenes::stream::tracking() if (next_battery_check < now and control.enabled[size_t(tid::battery)]) { timer t2(instance); - from_headset::battery battery{}; - auto intent = ctx.call>(register_receiver, receiver_jobj, filter_jobj); - if (intent) - { - auto level_jint = intent.call(get_int_extra, level_jstr, default_jint); - auto scale_jint = intent.call(get_int_extra, scale_jstr, default_jint); - if (level_jint && level_jint.value >= 0 && scale_jint && scale_jint.value >= 0) - { - battery.present = true; - battery.charge = (float)(level_jint.value) / (float)(scale_jint.value); - } - - auto plugged_jint = intent.call(get_int_extra, plugged_jstr, default_jint); - battery.charging = plugged_jint && plugged_jint.value > 0; - } - - network_session->send_stream(battery); + auto status = battery_tracker.get(); + network_session->send_stream(from_headset::battery{ + .charge = status.charge.value_or(-1), + .present = status.charge.has_value(), + .charging = status.charging, + }); next_battery_check = now + battery_check_interval; XrDuration battery_dur = t2.count();