diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 982c43d0..2486e4b7 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -322,6 +322,9 @@ std::shared_ptr scenes::stream::create(std::unique_ptrnetwork_session->send_control(from_headset::session_state_changed{ .state = application::get_session_state(), }); + self->network_session->send_control(from_headset::stream_tab_changed{ + .tab = self->gui_status, + }); if (self->instance.has_extension(XR_KHR_VISIBILITY_MASK_EXTENSION_NAME)) { @@ -759,26 +762,30 @@ void scenes::stream::update_gui_position(xr::spaces controller) } } -bool scenes::stream::is_gui_interactable() const +bool scenes::stream::is_interactable(stream_tab tab) { - switch (gui_status) + switch (tab) { - case gui_status::stats: - case gui_status::settings: - case gui_status::bitrate_settings: - case gui_status::foveation_settings: - case gui_status::applications: - case gui_status::application_launcher: + case stream_tab::stats: + case stream_tab::settings: + case stream_tab::bitrate_settings: + case stream_tab::foveation_settings: + case stream_tab::applications: + case stream_tab::application_launcher: return true; - case gui_status::hidden: - case gui_status::overlay_only: - case gui_status::compact: + case stream_tab::hidden: + case stream_tab::overlay_only: + case stream_tab::compact: return false; } + spdlog::warn("Invalid tab value {}", int(tab)); + return false; +} - assert(false); - __builtin_unreachable(); +bool scenes::stream::is_gui_interactable() const +{ + return is_interactable(gui_status); } void scenes::stream::render(const XrFrameState & frame_state) @@ -808,7 +815,7 @@ void scenes::stream::render(const XrFrameState & frame_state) .variant = application::get_messages_info().variant, }); - gui_status = stream::gui_status::hidden; + next_gui_status = stream_tab::hidden; application::pop_scene(); } @@ -1044,17 +1051,17 @@ void scenes::stream::render(const XrFrameState & frame_state) { switch (gui_status) { - case gui_status::hidden: - case gui_status::bitrate_settings: - case gui_status::foveation_settings: - case gui_status::compact: - case gui_status::overlay_only: + case stream_tab::hidden: + case stream_tab::bitrate_settings: + case stream_tab::foveation_settings: + case stream_tab::compact: + case stream_tab::overlay_only: dimming = dimming - frame_state.predictedDisplayPeriod / (1e9 * constants::stream::fade_duration); break; - case gui_status::stats: - case gui_status::settings: - case gui_status::applications: - case gui_status::application_launcher: + case stream_tab::stats: + case stream_tab::settings: + case stream_tab::applications: + case stream_tab::application_launcher: dimming = dimming + frame_state.predictedDisplayPeriod / (1e9 * constants::stream::fade_duration); break; } @@ -1135,23 +1142,14 @@ void scenes::stream::render(const XrFrameState & frame_state) if (state_1.currentState and state_2.currentState and (state_1.changedSinceLastSync or state_2.changedSinceLastSync)) { - switch (gui_status) - { - case gui_status::hidden: - case gui_status::compact: - case gui_status::overlay_only: - gui_status = next_gui_status; - break; - - case gui_status::stats: - case gui_status::settings: - case gui_status::bitrate_settings: - case gui_status::foveation_settings: - case gui_status::applications: - case gui_status::application_launcher: - gui_status = gui_status::hidden; - break; - } + // Arbitraty transitions can happen from network commands + // Ensure we can't have a set of 2 non interactable states + if (is_gui_interactable()) + next_gui_status = stream_tab::hidden; + else if (is_interactable(stored_gui_status)) + next_gui_status = stored_gui_status; + else + next_gui_status = stream_tab::applications; } } diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 75d9e2ae..601427fb 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -131,30 +131,21 @@ private: uint32_t height; std::optional imgui_ctx; - enum class gui_status - { - hidden, - overlay_only, - compact, - stats, - settings, - bitrate_settings, - foveation_settings, - applications, - application_launcher, - }; - struct gui_toast { std::string content; bool is_urgent = false; }; + static bool is_interactable(stream_tab); bool is_gui_interactable() const; - std::atomic gui_status = gui_status::hidden; - enum gui_status last_gui_status = gui_status::hidden; - enum gui_status next_gui_status = gui_status::applications; + // Tab currently being displayed + stream_tab gui_status = stream_tab::hidden; + // Tab that we will switch to if button is pressed + stream_tab stored_gui_status = stream_tab::applications; + // Tab that will be displayed on next render() + std::atomic next_gui_status = stream_tab::hidden; float dimming = 0; thread_safe> gui_toast; @@ -227,6 +218,7 @@ public: void operator()(to_headset::audio_stream_description &&); void operator()(to_headset::video_stream_description &&); void operator()(to_headset::refresh_rate_change &&); + void operator()(to_headset::stream_tab_change &&); void operator()(to_headset::application_list &&); void operator()(to_headset::application_icon &&); void operator()(to_headset::running_applications &&); diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index 04a6c121..53555fbc 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -462,7 +462,7 @@ void scenes::stream::gui_settings(float predicted_display_period) ImGui::SameLine(0.f, 10.f); ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (size.y / 4.f) - style.FramePadding.y * 3.f); if (ImGui::Button(fmt::format("{}Mbit/s", config.bitrate_bps / 1'000'000).c_str())) - gui_status = gui_status::bitrate_settings; + next_gui_status = stream_tab::bitrate_settings; imgui_ctx->vibrate_on_hover(); if (ImGui::IsItemHovered()) imgui_ctx->tooltip(_("Click to adjust bitrate")); @@ -554,7 +554,7 @@ void scenes::stream::gui_settings(float predicted_display_period) ImGui::SameLine(); if (ImGui::Button(_S("Change"))) - gui_status = gui_status::foveation_settings; + next_gui_status = stream_tab::foveation_settings; imgui_ctx->vibrate_on_hover(); ImGui::EndDisabled(); @@ -600,7 +600,7 @@ void scenes::stream::gui_bitrate_settings(float predicted_display_period) if (ok) { config.save(); - gui_status = gui_status::settings; + next_gui_status = stream_tab::settings; } send_settings_changed_packet(session, network_session.get(), application::get_config(), predicted_display_period); @@ -629,7 +629,7 @@ void scenes::stream::gui_foveation_settings(float predicted_display_period) if (ok) { - gui_status = gui_status::settings; + next_gui_status = stream_tab::settings; // Save settings auto & config = application::get_config(); @@ -640,7 +640,7 @@ void scenes::stream::gui_foveation_settings(float predicted_display_period) } else if (cancel) { - gui_status = gui_status::settings; + next_gui_status = stream_tab::settings; // Restore settings const auto & config = application::get_config(); @@ -744,25 +744,43 @@ void scenes::stream::gui_toasts() void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period) { - if (not(plots_toggle_1 and plots_toggle_2)) - return; + if (auto new_status = next_gui_status.load(); new_status != gui_status) + { + spdlog::info("Switch tab from {} to {}", magic_enum::enum_name(gui_status), magic_enum::enum_name(new_status)); + stored_gui_status = gui_status; + gui_status = new_status; + gui_status_last_change = predicted_display_time; + + // Override session state if the GUI is interactable + if (not is_gui_interactable()) + network_session->send_control(from_headset::session_state_changed{ + .state = application::get_session_state(), + }); + else if (application::get_session_state() == XR_SESSION_STATE_FOCUSED) + network_session->send_control(from_headset::session_state_changed{ + .state = XR_SESSION_STATE_VISIBLE, + }); + + network_session->send_control(from_headset::stream_tab_changed{.tab = new_status}); + } + bool interactable = true; XrSpace world_space = application::space(xr::spaces::world); auto views = session.locate_views(viewconfig, predicted_display_time, world_space).second; switch (gui_status) { - case gui_status::hidden: - case gui_status::bitrate_settings: - case gui_status::foveation_settings: - case gui_status::overlay_only: - case gui_status::compact: + case stream_tab::hidden: + case stream_tab::bitrate_settings: + case stream_tab::foveation_settings: + case stream_tab::overlay_only: + case stream_tab::compact: interactable = false; break; - case gui_status::stats: - case gui_status::settings: - case gui_status::applications: - case gui_status::application_launcher: + case stream_tab::stats: + case stream_tab::settings: + case stream_tab::applications: + case stream_tab::application_launcher: break; } imgui_ctx->set_controllers_enabled(interactable); @@ -782,27 +800,9 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte right_hand.reset(); } - if (gui_status != last_gui_status) - { - last_gui_status = gui_status; - if (is_gui_interactable()) - next_gui_status = gui_status; - gui_status_last_change = predicted_display_time; - - // Override session state if the GUI is interactable - if (not is_gui_interactable()) - network_session->send_control(from_headset::session_state_changed{ - .state = application::get_session_state(), - }); - else if (application::get_session_state() == XR_SESSION_STATE_FOCUSED) - network_session->send_control(from_headset::session_state_changed{ - .state = XR_SESSION_STATE_VISIBLE, - }); - } - float alpha = 1; bool is_urgent = false; - if (gui_status == gui_status::hidden) + if (gui_status == stream_tab::hidden) { auto toast = gui_toast.lock(); if (toast->has_value()) @@ -827,24 +827,24 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte glm::mat3 M = glm::mat3_cast(head_position->second); switch (gui_status) { - case gui_status::bitrate_settings: - case gui_status::foveation_settings: + case stream_tab::bitrate_settings: + case stream_tab::foveation_settings: imgui_ctx->layers()[0].orientation = head_position->second; imgui_ctx->layers()[0].position = head_position->first + M * glm::vec3{0, override_foveation_distance * sin(override_foveation_pitch), -override_foveation_distance}; break; - case gui_status::hidden: + case stream_tab::hidden: // Always use the same position for the GUI shortcut tip imgui_ctx->layers()[0].orientation = head_position->second; imgui_ctx->layers()[0].position = head_position->first + M * glm::vec3{0.0, -0.4, -1.0}; break; - case gui_status::overlay_only: - case gui_status::compact: - case gui_status::stats: - case gui_status::settings: - case gui_status::applications: - case gui_status::application_launcher: + case stream_tab::overlay_only: + case stream_tab::compact: + case stream_tab::stats: + case stream_tab::settings: + case stream_tab::applications: + case stream_tab::application_launcher: imgui_ctx->layers()[0].orientation = head_position->second * head_gui_orientation; imgui_ctx->layers()[0].position = head_position->first + M * head_gui_position; break; @@ -864,36 +864,36 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte bool display_tabs, always_auto_resize; switch (gui_status) { - case gui_status::overlay_only: + case stream_tab::overlay_only: ImGui::SetNextWindowPos(content_center, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); ImGui::SetNextWindowSize(content_size); always_auto_resize = false; display_tabs = false; break; - case gui_status::hidden: - case gui_status::bitrate_settings: - case gui_status::foveation_settings: + case stream_tab::hidden: + case stream_tab::bitrate_settings: + case stream_tab::foveation_settings: ImGui::SetNextWindowPos(viewport_size / 2, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); always_auto_resize = true; display_tabs = false; break; - case gui_status::compact: + case stream_tab::compact: ImGui::SetNextWindowPos(content_center, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); always_auto_resize = true; display_tabs = false; break; - case gui_status::stats: - case gui_status::settings: - case gui_status::applications: + case stream_tab::stats: + case stream_tab::settings: + case stream_tab::applications: ImGui::SetNextWindowPos(margin_around_window); ImGui::SetNextWindowSize(viewport_size - margin_around_window * 2); always_auto_resize = false; display_tabs = true; break; - case gui_status::application_launcher: + case stream_tab::application_launcher: ImGui::SetNextWindowPos(margin_around_window); ImGui::SetNextWindowSize(viewport_size - margin_around_window * 2); always_auto_resize = false; @@ -930,53 +930,53 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte switch (gui_status) { - case gui_status::hidden: + case stream_tab::hidden: gui_toasts(); break; - case gui_status::overlay_only: + case stream_tab::overlay_only: ImGui::SetCursorPos({20, 20}); ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); gui_performance_metrics(); ImGui::EndChild(); break; - case gui_status::compact: + case stream_tab::compact: gui_compact_view(); break; - case gui_status::stats: + case stream_tab::stats: ImGui::SetCursorPos({tab_width + 20, 20}); ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); gui_performance_metrics(); ImGui::EndChild(); break; - case gui_status::settings: + case stream_tab::settings: ImGui::SetCursorPos({tab_width + 20, 20}); ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); gui_settings(predicted_display_period * 1.e-9f); ImGui::EndChild(); break; - case gui_status::bitrate_settings: + case stream_tab::bitrate_settings: gui_bitrate_settings(predicted_display_period * 1.e-9f); break; - case gui_status::foveation_settings: + case stream_tab::foveation_settings: gui_foveation_settings(predicted_display_period * 1.e-9f); break; - case gui_status::applications: + case stream_tab::applications: ImGui::SetCursorPos({tab_width + 20, 20}); ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); gui_applications(); ImGui::EndChild(); break; - case gui_status::application_launcher: + case stream_tab::application_launcher: if (apps.draw_gui(*imgui_ctx, _("Cancel")) != app_launcher::None) - gui_status = gui_status::applications; + next_gui_status = stream_tab::applications; } ImGui::PopStyleVar(2); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_FrameRounding @@ -990,10 +990,11 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10, 10)); - RadioButtonWithoutCheckBox(ICON_FA_LIST " " + _("Applications"), gui_status, gui_status::applications, {tab_width, 0}); + auto target = gui_status; + RadioButtonWithoutCheckBox(ICON_FA_LIST " " + _("Applications"), target, stream_tab::applications, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - if (RadioButtonWithoutCheckBox(ICON_FA_ROCKET " " + _("Start"), gui_status, gui_status::application_launcher, {tab_width, 0})) + if (RadioButtonWithoutCheckBox(ICON_FA_ROCKET " " + _("Start"), target, stream_tab::application_launcher, {tab_width, 0})) { apps.reset(); network_session->send_control(from_headset::get_application_list{ @@ -1004,22 +1005,22 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte } imgui_ctx->vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), gui_status, gui_status::settings, {tab_width, 0}); + RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), target, stream_tab::settings, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Stats"), gui_status, gui_status::stats, {tab_width, 0}); + RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Stats"), target, stream_tab::stats, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); int n_items_at_end = 4; ImGui::SetCursorPosY(ImGui::GetContentRegionMax().y - n_items_at_end * ImGui::GetCurrentContext()->FontSize - (n_items_at_end * 2) * style.FramePadding.y - (n_items_at_end - 1) * style.ItemSpacing.y - style.WindowPadding.y); - RadioButtonWithoutCheckBox(ICON_FA_CHART_LINE " " + _("Statistics overlay"), gui_status, gui_status::overlay_only, {tab_width, 0}); + RadioButtonWithoutCheckBox(ICON_FA_CHART_LINE " " + _("Statistics overlay"), target, stream_tab::overlay_only, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_MINIMIZE " " + _("Compact view"), gui_status, gui_status::compact, {tab_width, 0}); + RadioButtonWithoutCheckBox(ICON_FA_MINIMIZE " " + _("Compact view"), target, stream_tab::compact, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_XMARK " " + _("Close"), gui_status, gui_status::hidden, {tab_width, 0}); + RadioButtonWithoutCheckBox(ICON_FA_XMARK " " + _("Close"), target, stream_tab::hidden, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); bool dummy = false; @@ -1029,6 +1030,7 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte ImGui::PopStyleVar(); // ImGuiStyleVar_FramePadding ImGui::EndChild(); + next_gui_status = target; } ImGui::PopStyleColor(); // ImGuiCol_ChildBg } diff --git a/client/scenes/stream_network.cpp b/client/scenes/stream_network.cpp index 96fe64be..5fa2ee67 100644 --- a/client/scenes/stream_network.cpp +++ b/client/scenes/stream_network.cpp @@ -117,6 +117,11 @@ void scenes::stream::operator()(to_headset::refresh_rate_change && rate) video_stream_description->fps = rate.fps; } +void scenes::stream::operator()(to_headset::stream_tab_change && tab) +{ + next_gui_status = tab.tab; +} + void scenes::stream::operator()(to_headset::timesync_query && query) { network_session->send_stream(from_headset::timesync_response{ diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 14c6c918..965b32ba 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -173,6 +173,19 @@ enum video_codec raw, }; +enum class stream_tab : uint8_t +{ + hidden, + overlay_only, + compact, + stats, + settings, + bitrate_settings, + foveation_settings, + applications, + application_launcher, +}; + struct audio_data { XrTime timestamp; @@ -512,6 +525,11 @@ struct user_presence_changed bool present; }; +struct stream_tab_changed +{ + stream_tab tab; +}; + struct override_foveation_center { bool enabled; @@ -565,6 +583,7 @@ using packets = std::variant< refresh_rate_changed, session_state_changed, user_presence_changed, + stream_tab_changed, override_foveation_center, get_application_list, start_app, @@ -748,6 +767,11 @@ struct refresh_rate_change float fps; }; +struct stream_tab_change +{ + stream_tab tab; +}; + struct application_list { std::string language; @@ -794,6 +818,7 @@ using packets = std::variant< tracking_control, feature_control, refresh_rate_change, + stream_tab_change, application_list, application_icon, running_applications>; diff --git a/dbus/io.github.wivrn.Server.xml b/dbus/io.github.wivrn.Server.xml index f37af7bd..59aa7e00 100644 --- a/dbus/io.github.wivrn.Server.xml +++ b/dbus/io.github.wivrn.Server.xml @@ -73,6 +73,11 @@ + + + + + diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index ea893f8d..64c7ff5b 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -34,20 +34,11 @@ using namespace std::chrono_literals; -static void handle_event_from_main_loop(to_monado::stop) -{ - // Ignore stop request when no headset is connected -} - -static void handle_event_from_main_loop(to_monado::disconnect) -{ - // Ignore disconnect request when no headset is connected -} - -static void handle_event_from_main_loop(to_monado::set_bitrate) -{ - // Ignore bitrate request when no headset is connected -} +// Ignored until connection is established +static void handle_event_from_main_loop(to_monado::stop) {} +static void handle_event_from_main_loop(to_monado::disconnect) {} +static void handle_event_from_main_loop(to_monado::set_bitrate) {} +static void handle_event_from_main_loop(wivrn::to_headset::stream_tab_change) {} static std::string clean_key(std::string key) { diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index 083e7db8..b3b255d5 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -36,6 +36,10 @@ #include "configuration.h" +#ifndef NDEBUG +#include "math/m_api.h" +#endif + namespace wivrn { diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 14d4cfa7..f9258d6f 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -833,6 +833,11 @@ void wivrn_session::operator()(from_headset::refresh_rate_changed && event) }); } +void wivrn_session::operator()(from_headset::stream_tab_changed && event) +{ + send_to_main(std::move(event)); +} + void wivrn_session::operator()(from_headset::get_application_list && request) { to_headset::application_list response{ @@ -975,6 +980,11 @@ void wivrn_session::operator()(to_monado::set_bitrate && data) comp_target->set_bitrate(data.bitrate_bps); } +void wivrn_session::operator()(to_headset::stream_tab_change && data) +{ + send_control(std::move(data)); +} + struct refresh_rate_adjuster { std::chrono::seconds period{10}; diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 3058fd99..d2e6b934 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -178,6 +178,7 @@ public: void operator()(from_headset::session_state_changed &&); void operator()(from_headset::user_presence_changed &&); void operator()(from_headset::refresh_rate_changed &&); + void operator()(from_headset::stream_tab_changed &&); void operator()(from_headset::override_foveation_center &&); void operator()(from_headset::get_application_list &&); void operator()(const from_headset::start_app &); @@ -189,6 +190,7 @@ public: void operator()(to_monado::stop &&); void operator()(to_monado::disconnect &&); void operator()(to_monado::set_bitrate &&); + void operator()(to_headset::stream_tab_change &&); bool has_stream() { diff --git a/server/main.cpp b/server/main.cpp index 8091deed..aa5a36b3 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -537,6 +538,10 @@ gboolean control_received(gint fd, GIOCondition condition, gpointer user_data) start_publishing(); inhibitor.reset(); wivrn_server_set_headset_connected(dbus_server, false); + wivrn_server_set_client_tab(dbus_server, ""); + }, + [&](const wivrn::from_headset::stream_tab_changed & event) { + wivrn_server_set_client_tab(dbus_server, magic_enum::enum_name(event.tab).data()); }, [&](const from_monado::server_error & e) { wivrn_server_emit_server_error(dbus_server, e.where.c_str(), e.message.c_str()); @@ -694,6 +699,29 @@ gboolean on_handle_disable_pairing(WivrnServer * skeleton, GDBusMethodInvocation return G_SOURCE_CONTINUE; } +gboolean on_handle_set_client_tab(WivrnServer * skeleton, GDBusMethodInvocation * invocation, gpointer user_data) +{ + GVariant * args = g_dbus_method_invocation_get_parameters(invocation); + char * c_tab; + g_variant_get_child(args, 0, "s", &c_tab); + std::string tab(c_tab); + g_free(c_tab); + + for (auto [val, name]: magic_enum::enum_entries()) + { + if (tab == name) + { + wivrn_ipc_socket_main_loop->send(to_headset::stream_tab_change{.tab = val}); + g_dbus_method_invocation_return_value(invocation, nullptr); + return G_SOURCE_CONTINUE; + } + } + + std::cerr << "Invalid tab name " << tab << std::endl; + g_dbus_method_invocation_return_dbus_error(invocation, "io.github.wivrn.Server.InvalidTab", "invalid tab"); + return G_SOURCE_CONTINUE; +} + void on_bitrate(WivrnServer * server, const GParamSpec * pspec, gpointer data) { auto bitrate = wivrn_server_get_bitrate(server); @@ -841,6 +869,8 @@ void on_name_acquired(GDBusConnection * connection, const gchar * name, gpointer NULL); } + g_signal_connect(dbus_server, "handle-set-client-tab", G_CALLBACK(on_handle_set_client_tab), NULL); + wivrn_server_set_steam_command(dbus_server, steam_command().c_str()); on_headset_info_packet({}); @@ -870,6 +900,8 @@ void on_name_acquired(GDBusConnection * connection, const gchar * name, gpointer set_encryption_state(wivrn_connection::encryption_state::pairing); else set_encryption_state(enc_state); + + wivrn_server_set_client_tab(dbus_server, ""); } auto create_dbus_connection() diff --git a/server/wivrn_ipc.h b/server/wivrn_ipc.h index 7dfb7e55..b92c2b84 100644 --- a/server/wivrn_ipc.h +++ b/server/wivrn_ipc.h @@ -52,6 +52,7 @@ using packets = std::variant< wivrn::from_headset::headset_info_packet, wivrn::from_headset::settings_changed, wivrn::from_headset::start_app, + wivrn::from_headset::stream_tab_changed, headset_connected, headset_disconnected, server_error>; @@ -70,7 +71,7 @@ struct set_bitrate uint32_t bitrate_bps; }; -using packets = std::variant; +using packets = std::variant; } // namespace to_monado extern std::optional> wivrn_ipc_socket_monado; diff --git a/tools/wivrnctl/main.cpp b/tools/wivrnctl/main.cpp index 01d85066..ca089845 100644 --- a/tools/wivrnctl/main.cpp +++ b/tools/wivrnctl/main.cpp @@ -89,6 +89,22 @@ sd_bus_message_ptr get_property(const sd_bus_ptr & bus, const char * member, con return sd_bus_message_ptr(msg); } +std::string get_property_string(const sd_bus_ptr & bus, const char * member) +{ + sd_bus_error error = SD_BUS_ERROR_NULL; + char * val; + int ret = sd_bus_get_property_string(bus.get(), destination, path, interface, member, &error, &val); + if (ret < 0) + { + std::runtime_error e(std::string("read property ") + member + " failed: " + error.message); + sd_bus_error_free(&error); + throw e; + } + std::string res(val); + free(val); + return res; +} + template void set_property(const sd_bus_ptr & bus, const char * member, const char * signature, Args &&... args) { @@ -218,6 +234,23 @@ void unpair(size_t headset_id) call_method(bus, "RevokeKey", "s", values.at(headset_id - 1).public_key.c_str()); } +void tab(std::string tab_name) +{ + auto bus = get_user_bus(); + if (tab_name.empty()) + { + auto val = get_property_string(bus, "ClientTab"); + if (val.empty()) + std::cout << "" << std::endl; + else + std::cout << val << std::endl; + } + else + { + call_method(bus, "SetClientTab", "s", tab_name.c_str()); + } +} + template auto member(T U::* x) { @@ -340,6 +373,11 @@ int main(int argc, char ** argv) app.add_subcommand("disconnect", "Disconnect headset") ->callback(disconnect); + std::string tab_name; + app.add_subcommand("tab", "Show or set current tab on headset") + ->callback([&]() { return tab(tab_name); }) + ->add_option("[TAB]", tab_name, "Tab to select"); + try { CLI11_PARSE(app, argc, argv);