diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 23fe5725..eee98c86 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -174,6 +174,7 @@ private: // Last application list received from server thread_safe> applications; + XrTime running_application_req = 0; thread_safe running_applications; stream(); diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index 278eea94..cffc52e3 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -614,33 +614,73 @@ void scenes::stream::gui_foveation_settings(float predicted_display_period) void scenes::stream::gui_applications() { + auto now = instance.now(); + if (now - running_application_req > 1'000'000'000) + { + running_application_req = now; + network_session->send_control(from_headset::get_running_applications{}); + } + ImGui::PushFont(nullptr, constants::gui::font_size_large); CenterTextH(_("Running XR applications:")); ImGui::PopFont(); auto apps = running_applications.lock(); - ImVec2 button_size(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0); - ImGui::PushStyleColor(ImGuiCol_Button, IM_COL32_BLACK_TRANS); - ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0); - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10, 10)); + ImVec2 button_size(ImGui::GetWindowSize().x - ImGui::GetCursorPosX() - 20, 0); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, 20)); + ImGui::Spacing(); + std::ranges::sort(apps->applications, [](auto & l, auto & r) { + if (l.overlay == r.overlay) + return false; + return r.overlay; + }); + bool overlay = false; for (const auto & app: apps->applications) { + if (app.overlay and not overlay) + { + ImGui::Separator(); + CenterTextH(_S("Overlays")); + overlay = true; + } + int colors = 1; + ImGui::PushStyleColor(ImGuiCol_Button, IM_COL32_BLACK_TRANS); + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10, 20)); if (app.active or app.overlay) { - ImGui::Text("%s", app.name.c_str()); - if (app.active) - { - ImGui::SameLine(); - ImGui::Text("%s", _S("(focused)")); - } + ImGui::PushStyleColor(ImGuiCol_ButtonActive, IM_COL32_BLACK_TRANS); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, IM_COL32_BLACK_TRANS); + colors += 2; } - else + ImGui::SetNextItemAllowOverlap(); + const bool clicked = RadioButtonWithoutCheckBox( + std::format("{}{}", app.active ? ICON_FA_CHEVRON_RIGHT " " : " ", app.name).c_str(), + app.active, + button_size); + if (clicked and not(app.active or app.overlay)) { - if (RadioButtonWithoutCheckBox(app.name, false, button_size)) - network_session->send_control(from_headset::set_active_application{.id = app.id}); + network_session->send_control(from_headset::set_active_application{.id = app.id}); + imgui_ctx->vibrate_on_hover(); } + ImGui::PopStyleColor(colors); + ImGui::PopStyleVar(2); + + ImGui::SameLine(); + auto right = ImGui::GetWindowSize().x; + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 20); + ImGui::SetCursorPosX(right - ImGui::CalcTextSize(ICON_FA_XMARK).x - ImGui::GetStyle().FramePadding.x - 40); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.8f, 0.2f, 0.2f, 0.40f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.8f, 0.2f, 0.2f, 1.00f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0.1f, 0.1f, 1.00f)); + if (ImGui::Button(std::format(ICON_FA_XMARK "##{}", app.id).c_str())) + network_session->send_control(from_headset::stop_application{.id = app.id}); + imgui_ctx->vibrate_on_hover(); + ImGui::PopStyleColor(3); + + if (ImGui::IsItemHovered()) + imgui_ctx->tooltip(_S("Request to quit, may be ignored by the application")); } - ImGui::PopStyleVar(2); - ImGui::PopStyleColor(); + ImGui::PopStyleVar(); } // Return the vector v such that dot(v, x) > 0 iff x is on the side where the composition layer is visible @@ -872,8 +912,7 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), gui_status, gui_status::settings, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - if (RadioButtonWithoutCheckBox(ICON_FA_LIST " " + _("Applications"), gui_status, gui_status::applications, {tab_width, 0})) - network_session->send_control(from_headset::get_running_applications{}); + RadioButtonWithoutCheckBox(ICON_FA_LIST " " + _("Applications"), gui_status, gui_status::applications, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); int n_items_at_end = 4; diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 3591f9bc..3a1338ae 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -470,6 +470,11 @@ struct set_active_application uint32_t id; }; +struct stop_application +{ + uint32_t id; +}; + using packets = std::variant< crypto_handshake, pin_check_1, @@ -494,7 +499,8 @@ using packets = std::variant< get_application_list, start_app, get_running_applications, - set_active_application>; + set_active_application, + stop_application>; } // namespace from_headset namespace to_headset diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 2772db9a..c13559c0 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -815,6 +815,22 @@ void wivrn_session::operator()(const from_headset::set_active_application & req) (*this)(from_headset::get_running_applications{}); } +void wivrn_session::operator()(const from_headset::stop_application & req) +{ + scoped_lock lock(inst.server->global_state.lock); + for (auto & t: inst.server->threads) + { + if (t.ics.client_state.id == req.id) + { + U_LOG_I("Notify session loss pending for %s", t.ics.client_state.info.application_name); + auto when = os_monotonic_get_ns() + 200 * U_TIME_1MS_IN_NS; + xrt_syscomp_notify_loss_pending(system_compositor, t.ics.xc, when); + session_loss.lock()->emplace(when, req.id); + break; + } + } +} + void wivrn_session::operator()(audio_data && data) { if (audio_handle) @@ -899,6 +915,7 @@ void wivrn_session::run(std::stop_token stop) refresh.adjust(*connection); } } + poll_session_loss(); connection->poll(*this, 20); } catch (const std::exception & e) @@ -1002,6 +1019,29 @@ void wivrn_session::reconnect() } } +void wivrn_session::poll_session_loss() +{ + auto locked = session_loss.lock(); + auto now = os_monotonic_get_ns(); + if (locked->empty()) + return; + auto it = locked->begin(); + scoped_lock lock(inst.server->global_state.lock); + while (it != locked->end() and it->first <= now) + { + for (auto & t: inst.server->threads) + { + if (t.ics.client_state.id == it->second) + { + U_LOG_I("Terminating %s", t.ics.client_state.info.application_name); + xrt_syscomp_notify_lost(system_compositor, t.ics.xc); + break; + } + } + it = locked->erase(it); + } +} + xrt_result_t wivrn_session::get_roles(xrt_system_roles * out_roles) { std::lock_guard lock(roles_mutex); diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 6011738f..9df588d9 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -21,6 +21,7 @@ #include "clock_offset.h" #include "driver/app_pacer.h" +#include "utils/thread_safe.h" #include "wivrn_connection.h" #include "wivrn_controller.h" #include "wivrn_hand_interaction.h" @@ -31,6 +32,7 @@ #include "xrt/xrt_system.h" #include #include +#include #include #include #include @@ -134,6 +136,9 @@ class wivrn_session : public xrt_system_devices std::shared_ptr audio_handle; + // when sessions shall be destroyed, key is timestap, value is client id + thread_safe> session_loss; + std::jthread thread; wivrn_session(std::unique_ptr connection, instance &, u_system &); @@ -194,6 +199,7 @@ public: void operator()(const from_headset::start_app &); void operator()(const from_headset::get_running_applications &); void operator()(const from_headset::set_active_application &); + void operator()(const from_headset::stop_application &); void operator()(audio_data &&); void operator()(to_monado::disconnect &&); @@ -225,6 +231,8 @@ private: void run(std::stop_token stop); void reconnect(); + void poll_session_loss(); + // xrt_system implementation xrt_result_t get_roles(xrt_system_roles * out_roles); xrt_result_t feature_inc(xrt_device_feature_type type);