respect current client selection on session resume

this fixes a bug where non-current non-overlay
clients could get up to the focused session state
when a reconnect occured.
This commit is contained in:
caszuu
2026-02-27 16:03:18 +01:00
committed by xytovl
parent adf4b1fc84
commit 7d11674b2a
5 changed files with 63 additions and 66 deletions
+9 -2
View File
@@ -79,7 +79,8 @@ struct pipewire_device : public audio_device
static void mic_state_changed(void * self_v, pw_stream_state old, pw_stream_state state, const char * error);
void process_mic_data(wivrn::audio_data &&) override;
void on_connect() override;
void pause() override;
void resume() override;
~pipewire_device()
{
@@ -415,8 +416,14 @@ void pipewire_device::process_mic_data(wivrn::audio_data && sample)
if (mic_samples.write(std::move(sample)))
mic_buffer_size_bytes += size;
}
void pipewire_device::on_connect()
void pipewire_device::pause()
{
}
void pipewire_device::resume()
{
session.send_control(description());
session.send_control(to_headset::feature_control{to_headset::feature_control::microphone, mic_state == PW_STREAM_STATE_STREAMING});
}
+7 -2
View File
@@ -404,9 +404,14 @@ struct pulse_device : public audio_device
mic_buffer.push(std::move(mic_data));
}
void on_connect() override
void pause() override
{
session.send_control(to_headset::feature_control{to_headset::feature_control::microphone, true});
}
void resume() override
{
session.send_control(description());
session.send_control(to_headset::feature_control{to_headset::feature_control::microphone, (bool)microphone});
}
pulse_device(
+3 -2
View File
@@ -34,9 +34,10 @@ struct audio_device
virtual ~audio_device() = default;
virtual wivrn::to_headset::audio_stream_description description() const = 0;
virtual void process_mic_data(wivrn::audio_data &&) = 0;
virtual void on_connect() = 0;
virtual void pause() = 0;
virtual void resume() = 0;
static std::unique_ptr<audio_device> create(
const std::string & source_name,
+43 -60
View File
@@ -404,24 +404,17 @@ void wivrn_session::pause_session()
{
assert(mnd_ipc_server);
// notify clients about disconnected status
xrt_session_event event{
.state = {
.type = XRT_SESSION_EVENT_STATE_CHANGE,
.visible = false,
.focused = false,
},
};
auto result = push_event(event);
if (result != XRT_SUCCESS)
{
U_LOG_W("Failed to notify session state change: %s", xrt_result_to_string(result).c_str());
}
// notify clients about session pause
update_client_states(false, false);
// pause session components
worker_thread = std::jthread();
worker_thread = std::jthread();
offset_est.reset();
if (audio_handle)
audio_handle->pause();
{
std::shared_lock lock(comp_target_mutex);
if (comp_target)
@@ -431,12 +424,20 @@ void wivrn_session::pause_session()
void wivrn_session::resume_session()
{
// forward session config to headset
assert(mnd_ipc_server);
// reset session components and send descriptor packets to headset
if (audio_handle)
audio_handle->resume();
if (uinput_handler)
send_control(to_headset::feature_control{to_headset::feature_control::hid_input, true});
{
send_control(audio_handle->description());
audio_handle->on_connect();
std::shared_lock lock(comp_target_mutex);
if (comp_target)
comp_target->resume();
}
(*this)(from_headset::get_application_list{
@@ -445,34 +446,10 @@ void wivrn_session::resume_session()
.variant = get_info().variant,
});
if (uinput_handler)
{
send_control(to_headset::feature_control{to_headset::feature_control::hid_input, true});
}
// resume session and notify clients
// reset / resume session components
{
std::shared_lock lock(comp_target_mutex);
if (comp_target)
comp_target->resume();
}
worker_thread = std::jthread([this](std::stop_token stop) { return run_worker(stop); });
// notify clients about session resumed status
xrt_session_event event{
.state = {
.type = XRT_SESSION_EVENT_STATE_CHANGE,
.visible = true,
.focused = true,
},
};
auto result = push_event(event);
if (result != XRT_SUCCESS)
{
U_LOG_W("Failed to notify session state change: %s", xrt_result_to_string(result).c_str());
}
update_client_states(true, true);
}
clock_offset wivrn_session::get_offset()
@@ -780,7 +757,6 @@ void wivrn_session::operator()(from_headset::visibility_mask_changed && mask)
void wivrn_session::operator()(from_headset::session_state_changed && event)
{
assert(mnd_ipc_server);
U_LOG_I("Session state changed: %s", xr::to_string(event.state));
bool visible, focused;
switch (event.state)
@@ -798,22 +774,8 @@ void wivrn_session::operator()(from_headset::session_state_changed && event)
focused = false;
break;
}
scoped_lock lock(mnd_ipc_server->global_state.lock);
auto locked = session_loss.lock();
for (auto & t: mnd_ipc_server->threads)
{
auto id = t.ics.client_state.id;
if (t.ics.server_thread_index < 0 or t.ics.xc == nullptr or locked->contains(id))
continue;
bool current = t.ics.client_state.session_overlay or
mnd_ipc_server->global_state.active_client_index == t.ics.server_thread_index;
U_LOG_D("Setting session state for app %s: visible=%s focused=%s current=%s",
t.ics.client_state.info.application_name,
visible ? "true" : "false",
focused ? "true" : "false",
current ? "true" : "false");
xrt_syscomp_set_state(system_compositor, t.ics.xc, visible and current, focused and current, os_monotonic_get_ns());
}
update_client_states(visible, focused);
}
void wivrn_session::operator()(from_headset::user_presence_changed && event)
{
@@ -1252,6 +1214,27 @@ void wivrn_session::reconnect(std::stop_token stop)
}
}
void wivrn_session::update_client_states(bool visible, bool focused)
{
assert(mnd_ipc_server);
scoped_lock lock(mnd_ipc_server->global_state.lock);
auto locked = session_loss.lock();
for (auto & t: mnd_ipc_server->threads)
{
auto id = t.ics.client_state.id;
if (t.ics.server_thread_index < 0 or t.ics.xc == nullptr or locked->contains(id))
continue;
bool current = t.ics.client_state.session_overlay or
mnd_ipc_server->global_state.active_client_index == t.ics.server_thread_index;
U_LOG_D("Setting session state for app %s: visible=%s focused=%s current=%s",
t.ics.client_state.info.application_name,
visible ? "true" : "false",
focused ? "true" : "false",
current ? "true" : "false");
xrt_syscomp_set_state(system_compositor, t.ics.xc, visible and current, focused and current, os_monotonic_get_ns());
}
}
void wivrn_session::poll_session_loss()
{
assert(mnd_ipc_server);
+1
View File
@@ -220,6 +220,7 @@ private:
void pause_session();
void resume_session();
void update_client_states(bool visible, bool focused);
void poll_session_loss();
// checks if a headset is usable with this session