WayVRData → WayVRState in the RefCell

This commit is contained in:
galister
2025-12-26 17:40:22 +09:00
parent dc6d03605f
commit 400952177e
10 changed files with 70 additions and 149 deletions

View File

@@ -230,8 +230,6 @@ pub fn openvr_run(show_by_default: bool, headless: bool) -> Result<(), BackendEr
let _ = adjust_gain(&mut settings_mgr, channel, value);
}
},
#[cfg(feature = "wayvr")]
TaskType::WayVR(_action) => { /* TODO */ }
}
}

View File

@@ -492,8 +492,6 @@ pub fn openxr_run(show_by_default: bool, headless: bool) -> Result<(), BackendEr
}
#[cfg(feature = "openvr")]
TaskType::OpenVR(_) => {}
#[cfg(feature = "wayvr")]
TaskType::WayVR(_action) => { /* TODO */ }
}
}

View File

@@ -13,9 +13,6 @@ use crate::{
windowing::{OverlaySelector, window::OverlayWindowConfig},
};
#[cfg(feature = "wayvr")]
use crate::backend::wayvr::WayVRAction;
static TASK_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0);
struct AppTask {
@@ -100,8 +97,6 @@ pub enum TaskType {
Playspace(PlayspaceTask),
#[cfg(feature = "openvr")]
OpenVR(OpenVrTask),
#[cfg(feature = "wayvr")]
WayVR(WayVRAction),
}
#[derive(Deserialize, Clone, Copy)]

View File

@@ -8,7 +8,6 @@ pub mod window;
use anyhow::Context;
use comp::Application;
use process::ProcessVec;
use serde::Deserialize;
use slotmap::SecondaryMap;
use smallvec::SmallVec;
use smithay::{
@@ -122,10 +121,6 @@ pub struct WayVRState {
overlay_to_window: SecondaryMap<OverlayID, window::WindowHandle>,
}
pub struct WayVR {
pub state: WayVRState,
}
pub enum MouseIndex {
Left,
Center,
@@ -136,7 +131,7 @@ pub enum TickTask {
NewExternalProcess(ExternalProcessRequest), // Call WayVRCompositor::add_client after receiving this message
}
impl WayVR {
impl WayVRState {
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
pub fn new(
gfx: Arc<WGfx>,
@@ -144,7 +139,7 @@ impl WayVR {
config: Config,
signals: SyncEventQueue<WayVRSignal>,
) -> anyhow::Result<Self> {
log::info!("Initializing WayVR");
log::info!("Initializing WayVR server");
let display: wayland_server::Display<Application> = wayland_server::Display::new()?;
let dh = display.handle();
let compositor = compositor::CompositorState::new::<Application>(&dh);
@@ -235,7 +230,7 @@ impl WayVR {
let time_start = get_millis();
let state = WayVRState {
Ok(WayVRState {
time_start,
manager: client::WayVRCompositor::new(state, display, seat_keyboard, seat_pointer)?,
processes: ProcessVec::new(),
@@ -248,9 +243,7 @@ impl WayVR {
mouse_freeze: Instant::now(),
window_to_overlay: HashMap::new(),
overlay_to_window: SecondaryMap::new(),
};
Ok(Self { state })
})
}
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
@@ -258,7 +251,7 @@ impl WayVR {
let mut tasks: Vec<TickTask> = Vec::new();
app.ipc_server.tick(&mut ipc_server::TickParams {
wayland_state: &mut self.state,
wayland_state: self,
input_state: &app.input_state,
tasks: &mut tasks,
signals: &app.wayvr_signals,
@@ -267,14 +260,14 @@ impl WayVR {
// Tick all child processes
let mut to_remove: SmallVec<[process::ProcessHandle; 2]> = SmallVec::new();
for (handle, process) in self.state.processes.iter_mut() {
for (handle, process) in self.processes.iter_mut() {
if !process.is_running() {
to_remove.push(handle);
}
}
for p_handle in &to_remove {
self.state.processes.remove(p_handle);
self.processes.remove(p_handle);
}
if !to_remove.is_empty() {
@@ -283,20 +276,20 @@ impl WayVR {
));
}
while let Some(task) = self.state.tasks.read() {
while let Some(task) = self.tasks.read() {
match task {
WayVRTask::NewExternalProcess(req) => {
tasks.push(TickTask::NewExternalProcess(req));
}
WayVRTask::NewToplevel(client_id, toplevel) => {
// Attach newly created toplevel surfaces to displays
for client in &self.state.manager.clients {
for client in &self.manager.clients {
if client.client.id() != client_id {
continue;
}
let Some(process_handle) =
process::find_by_pid(&self.state.processes, client.pid)
process::find_by_pid(&self.processes, client.pid)
else {
log::error!(
"WayVR window creation failed: Unexpected process ID {}. It wasn't registered before.",
@@ -305,7 +298,7 @@ impl WayVR {
continue;
};
let window_handle = self.state.wm.create_window(&toplevel, process_handle);
let window_handle = self.wm.create_window(&toplevel, process_handle);
let title: Arc<str> = with_states(toplevel.wl_surface(), |states| {
states
@@ -323,7 +316,7 @@ impl WayVR {
create_wl_window_overlay(
title,
app.xr_backend,
app.wayland_server.as_ref().unwrap().clone(),
app.wayvr_server.as_ref().unwrap().clone(),
window_handle,
)
.inspect_err(|e| {
@@ -334,37 +327,34 @@ impl WayVR {
}),
)));
//TODO: populate window_to_overlay
app.wayvr_signals.send(WayVRSignal::BroadcastStateChanged(
packet_server::WvrStateChanged::WindowCreated,
));
}
}
WayVRTask::DropToplevel(client_id, toplevel) => {
for client in &self.state.manager.clients {
for client in &self.manager.clients {
if client.client.id() != client_id {
continue;
}
let Some(window_handle) = self.state.wm.find_window_handle(&toplevel)
else {
let Some(window_handle) = self.wm.find_window_handle(&toplevel) else {
log::warn!("DropToplevel: Couldn't find matching window handle");
continue;
};
if let Some(oid) = self.state.window_to_overlay.remove(&window_handle) {
if let Some(oid) = self.window_to_overlay.remove(&window_handle) {
app.tasks.enqueue(TaskType::Overlay(OverlayTask::Drop(
OverlaySelector::Id(oid),
)));
self.state.overlay_to_window.remove(oid);
self.overlay_to_window.remove(oid);
}
self.state.wm.remove_window(window_handle);
self.wm.remove_window(window_handle);
}
}
WayVRTask::ProcessTerminationRequest(process_handle) => {
if let Some(process) = self.state.processes.get_mut(&process_handle) {
if let Some(process) = self.processes.get_mut(&process_handle) {
process.terminate();
}
@@ -373,31 +363,28 @@ impl WayVR {
}
}
self.state.manager.tick_wayland(&mut self.state.processes)?;
self.manager.tick_wayland(&mut self.processes)?;
if self.state.ticks.is_multiple_of(200) {
self.state.manager.cleanup_clients();
self.state.manager.cleanup_handles();
if self.ticks.is_multiple_of(200) {
self.manager.cleanup_clients();
self.manager.cleanup_handles();
}
self.state.ticks += 1;
self.ticks += 1;
Ok(tasks)
}
pub fn terminate_process(&mut self, process_handle: process::ProcessHandle) {
self.state
.tasks
self.tasks
.send(WayVRTask::ProcessTerminationRequest(process_handle));
}
pub fn overlay_added(&mut self, oid: OverlayID, window: window::WindowHandle) {
self.state.overlay_to_window.insert(oid, window);
self.state.window_to_overlay.insert(window, oid);
self.overlay_to_window.insert(oid, window);
self.window_to_overlay.insert(window, oid);
}
}
impl WayVRState {
pub fn send_mouse_move(&mut self, handle: window::WindowHandle, x: u32, y: u32) {
if self.mouse_freeze > Instant::now() {
return;
@@ -544,25 +531,6 @@ pub struct SpawnProcessResult {
pub child: std::process::Child,
}
#[derive(Deserialize, Clone)]
pub enum WayVRDisplayClickAction {
ToggleVisibility,
Reset,
}
#[derive(Deserialize, Clone)]
pub enum WayVRAction {
AppClick {
catalog_name: Arc<str>,
app_name: Arc<str>,
},
DisplayClick {
display_name: Arc<str>,
action: WayVRDisplayClickAction,
},
ToggleDashboard,
}
struct SurfaceBufWithImageContainer {
inner: RefCell<SurfaceBufWithImage>,
}

View File

@@ -15,14 +15,13 @@ use wlx_common::{common::LeftRight, config::GeneralConfig, windowing::Positionin
use crate::{
backend::{
task::{TaskContainer, TaskType},
wayvr::{self, WayVRAction},
task::TaskContainer,
wayvr::{self, WayVRState},
},
config::load_config_with_conf_d,
config_io,
graphics::WGfxExtras,
ipc::{event_queue::SyncEventQueue, signal::WayVRSignal},
overlays::wayvr::WayVRData,
};
// Flat version of RelativeTo
@@ -206,7 +205,7 @@ impl WayVRConfig {
config: &GeneralConfig,
tasks: &mut TaskContainer,
signals: SyncEventQueue<WayVRSignal>,
) -> anyhow::Result<Rc<RefCell<WayVRData>>> {
) -> anyhow::Result<Rc<RefCell<WayVRState>>> {
let primary_count = self
.displays
.iter()
@@ -222,15 +221,12 @@ impl WayVRConfig {
if let Some(b) = app.shown_at_start
&& b
{
tasks.enqueue(TaskType::WayVR(WayVRAction::AppClick {
catalog_name: Arc::from(catalog_name.as_str()),
app_name: Arc::from(app.name.as_str()),
}));
//CLEANUP: is this needed?
}
}
}
Ok(Rc::new(RefCell::new(WayVRData::new(
Ok(Rc::new(RefCell::new(WayVRState::new(
gfx,
gfx_extras,
Self::get_wayvr_config(config, self)?,

View File

@@ -26,9 +26,6 @@ use crate::{
windowing::OverlaySelector,
};
#[cfg(feature = "wayvr")]
use crate::backend::wayvr::WayVRAction;
pub const BUTTON_EVENTS: [(
&str,
EventListenerKind,
@@ -200,8 +197,7 @@ pub(super) fn setup_custom_button<S: 'static>(
return Ok(EventResult::Pass);
}
app.tasks
.enqueue(TaskType::WayVR(WayVRAction::ToggleDashboard));
//FIXME
Ok(EventResult::Consumed)
}),
"::SetToggle" => {

View File

@@ -3,7 +3,7 @@ use std::{cell::RefCell, rc::Rc};
use wayvr_ipc::packet_server;
#[cfg(feature = "wayvr")]
use crate::{backend::wayvr, overlays::wayvr::WayVRData};
use crate::backend::wayvr::{self, WayVRState};
use crate::{
backend::{
@@ -18,7 +18,7 @@ use crate::{
#[cfg(feature = "wayvr")]
fn process_tick_tasks(
tick_tasks: Vec<backend::wayvr::TickTask>,
r_wayvr: &Rc<RefCell<WayVRData>>,
r_wayvr: &Rc<RefCell<WayVRState>>,
) -> anyhow::Result<()> {
for tick_task in tick_tasks {
match tick_task {
@@ -27,16 +27,12 @@ fn process_tick_tasks(
log::info!("Registering external process with PID {}", request.pid);
wayvr.data.state.add_external_process(request.pid);
wayvr.add_external_process(request.pid);
wayvr
.data
.state
.manager
.add_client(wayvr::client::WayVRClient {
client: request.client,
pid: request.pid,
});
wayvr.manager.add_client(wayvr::client::WayVRClient {
client: request.client,
pid: request.pid,
});
}
}
}
@@ -52,7 +48,7 @@ where
O: Default,
{
#[cfg(feature = "wayvr")]
let wayland_server = app.wayland_server.clone();
let wayvr_server = app.wayvr_server.clone();
while let Some(signal) = app.wayvr_signals.read() {
match signal {
@@ -80,9 +76,9 @@ where
#[cfg(feature = "wayvr")]
{
if let Some(wayland_server) = wayland_server {
let tick_tasks = wayland_server.borrow_mut().data.tick_events(app)?;
process_tick_tasks(tick_tasks, &wayland_server)?;
if let Some(wayvr_server) = wayvr_server {
let tick_tasks = wayvr_server.borrow_mut().tick_events(app)?;
process_tick_tasks(tick_tasks, &wayvr_server)?;
}
}

View File

@@ -1,8 +1,7 @@
use glam::{Affine2, Affine3A, Quat, Vec3, vec3};
use smithay::wayland::compositor::with_states;
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc};
use std::{cell::RefCell, rc::Rc, sync::Arc};
use vulkano::image::view::ImageView;
use wgui::gfx::WGfx;
use wlx_capture::frame::MouseMeta;
use wlx_common::{
overlays::{BackendAttrib, BackendAttribValue, StereoMode},
@@ -13,15 +12,13 @@ use crate::{
backend::{
XrBackend,
input::{self, HoverResult},
wayvr::{self, SurfaceBufWithImage, WayVR},
wayvr::{self, SurfaceBufWithImage, WayVRState},
},
graphics::{ExtentExt, WGfxExtras},
ipc::{event_queue::SyncEventQueue, signal::WayVRSignal},
graphics::ExtentExt,
overlays::screen::capture::ScreenPipeline,
state::{self, AppState},
subsystem::{hid::WheelDelta, input::KeyboardFocus},
windowing::{
OverlayID,
backend::{
FrameMeta, OverlayBackend, OverlayEventData, RenderResources, ShouldRender,
ui_transform,
@@ -30,29 +27,10 @@ use crate::{
},
};
pub struct WayVRData {
pub window_handle_map: HashMap<wayvr::window::WindowHandle, OverlayID>,
pub data: WayVR,
}
impl WayVRData {
pub fn new(
gfx: Arc<WGfx>,
gfx_extras: &WGfxExtras,
config: wayvr::Config,
signals: SyncEventQueue<WayVRSignal>,
) -> anyhow::Result<Self> {
Ok(Self {
window_handle_map: HashMap::default(),
data: WayVR::new(gfx, &gfx_extras, config, signals)?,
})
}
}
pub fn create_wl_window_overlay(
name: Arc<str>,
xr_backend: XrBackend,
wayvr: Rc<RefCell<WayVRData>>,
wayvr: Rc<RefCell<WayVRState>>,
window: wayvr::window::WindowHandle,
) -> anyhow::Result<OverlayWindowConfig> {
Ok(OverlayWindowConfig {
@@ -83,7 +61,7 @@ pub struct WayVRBackend {
pipeline: Option<ScreenPipeline>,
interaction_transform: Option<Affine2>,
window: wayvr::window::WindowHandle,
wayvr: Rc<RefCell<WayVRData>>,
wayvr: Rc<RefCell<WayVRState>>,
just_resumed: bool,
meta: Option<FrameMeta>,
stereo: Option<StereoMode>,
@@ -94,7 +72,7 @@ impl WayVRBackend {
fn new(
name: Arc<str>,
xr_backend: XrBackend,
wayvr: Rc<RefCell<WayVRData>>,
wayvr: Rc<RefCell<WayVRState>>,
window: wayvr::window::WindowHandle,
) -> anyhow::Result<Self> {
Ok(Self {
@@ -130,8 +108,8 @@ impl OverlayBackend for WayVRBackend {
}
fn should_render(&mut self, app: &mut AppState) -> anyhow::Result<ShouldRender> {
let wayvr = &self.wayvr.borrow().data;
let Some(window) = wayvr.state.wm.windows.get(&self.window) else {
let wayvr = &self.wayvr.borrow();
let Some(window) = wayvr.wm.windows.get(&self.window) else {
log::debug!(
"{:?}: WayVR overlay without matching window entry",
self.name
@@ -196,9 +174,8 @@ impl OverlayBackend for WayVRBackend {
) -> anyhow::Result<()> {
let image = self.cur_image.as_ref().unwrap().clone();
let wayvr = &self.wayvr.borrow().data;
let wayvr = &self.wayvr.borrow();
let mouse = wayvr
.state
.wm
.mouse
.as_ref()
@@ -229,19 +206,19 @@ impl OverlayBackend for WayVRBackend {
event_data: OverlayEventData,
) -> anyhow::Result<()> {
if let OverlayEventData::IdAssigned(oid) = event_data {
let wayvr = &mut self.wayvr.borrow_mut().data;
let wayvr = &mut self.wayvr.borrow_mut();
wayvr.overlay_added(oid, self.window.clone());
}
Ok(())
}
fn on_hover(&mut self, _app: &mut state::AppState, hit: &input::PointerHit) -> HoverResult {
let wayvr = &mut self.wayvr.borrow_mut().data;
let wayvr = &mut self.wayvr.borrow_mut();
if let Some(meta) = self.meta.as_ref() {
let x = ((hit.uv.x * (meta.extent[0] as f32)) as u32).max(0);
let y = ((hit.uv.y * (meta.extent[1] as f32)) as u32).max(0);
wayvr.state.send_mouse_move(self.window, x, y);
wayvr.send_mouse_move(self.window, x, y);
}
HoverResult {
@@ -264,11 +241,11 @@ impl OverlayBackend for WayVRBackend {
None
}
} {
let wayvr = &mut self.wayvr.borrow_mut().data;
let wayvr = &mut self.wayvr.borrow_mut();
if pressed {
wayvr.state.send_mouse_down(self.window, index);
wayvr.send_mouse_down(self.window, index);
} else {
wayvr.state.send_mouse_up(index);
wayvr.send_mouse_up(index);
}
}
}
@@ -279,7 +256,7 @@ impl OverlayBackend for WayVRBackend {
_hit: &input::PointerHit,
delta: WheelDelta,
) {
self.wayvr.borrow_mut().data.state.send_mouse_scroll(delta);
self.wayvr.borrow_mut().send_mouse_scroll(delta);
}
fn get_interaction_transform(&mut self) -> Option<Affine2> {

View File

@@ -14,10 +14,11 @@ use wlx_common::{
#[cfg(feature = "wayvr")]
use {
crate::config_wayvr::{self, WayVRConfig},
crate::overlays::wayvr::WayVRData,
std::{cell::RefCell, rc::Rc},
};
#[cfg(feature = "wayvr")]
use crate::backend::wayvr::WayVRState;
#[cfg(feature = "osc")]
use crate::subsystem::osc::OscSender;
@@ -61,7 +62,7 @@ pub struct AppState {
pub osc_sender: Option<OscSender>,
#[cfg(feature = "wayvr")]
pub wayland_server: Option<Rc<RefCell<WayVRData>>>,
pub wayvr_server: Option<Rc<RefCell<WayVRState>>>,
}
#[allow(unused_mut)]
@@ -78,7 +79,7 @@ impl AppState {
let wayvr_signals = SyncEventQueue::new();
#[cfg(feature = "wayvr")]
let wayland_server = session
let wayvr_server = session
.wayvr_config
.post_load(
gfx.clone(),
@@ -93,7 +94,7 @@ impl AppState {
let mut hid_provider = HidWrapper::new();
#[cfg(feature = "wayvr")]
if let Some(wayland_server) = wayland_server.as_ref() {
if let Some(wayland_server) = wayvr_server.as_ref() {
hid_provider.set_wayvr(wayland_server.clone());
}
@@ -157,7 +158,7 @@ impl AppState {
osc_sender,
#[cfg(feature = "wayvr")]
wayland_server,
wayvr_server,
})
}

View File

@@ -1,7 +1,7 @@
use super::hid::{self, HidProvider, VirtualKey};
#[cfg(feature = "wayvr")]
use crate::overlays::wayvr::WayVRData;
use crate::backend::wayvr::WayVRState;
use crate::subsystem::hid::XkbKeymap;
#[cfg(feature = "wayvr")]
use std::{cell::RefCell, rc::Rc};
@@ -19,7 +19,7 @@ pub struct HidWrapper {
pub inner: Box<dyn HidProvider>,
pub keymap: Option<XkbKeymap>,
#[cfg(feature = "wayvr")]
pub wayvr: Option<Rc<RefCell<WayVRData>>>, // Dynamically created if requested
pub wayvr: Option<Rc<RefCell<WayVRState>>>, // Dynamically created if requested
}
impl HidWrapper {
@@ -34,12 +34,10 @@ impl HidWrapper {
}
#[cfg(feature = "wayvr")]
pub fn set_wayvr(&mut self, wayvr: Rc<RefCell<WayVRData>>) {
pub fn set_wayvr(&mut self, wayvr: Rc<RefCell<WayVRState>>) {
if let Some(keymap) = self.keymap.take() {
let _ = wayvr
.borrow_mut()
.data
.state
.set_keymap(&keymap.inner)
.inspect_err(|e| log::error!("Could not set WayVR keymap: {e:?}"));
}
@@ -53,7 +51,7 @@ impl HidWrapper {
{
#[cfg(feature = "wayvr")]
if let Some(wayvr) = &self.wayvr {
wayvr.borrow_mut().data.state.send_key(key as u32, down);
wayvr.borrow_mut().send_key(key as u32, down);
}
}
}
@@ -64,8 +62,6 @@ impl HidWrapper {
if let Some(wayvr) = &self.wayvr {
let _ = wayvr
.borrow_mut()
.data
.state
.set_keymap(&keymap.inner)
.inspect_err(|e| log::error!("Could not set WayVR keymap: {e:?}"));
} else {
@@ -85,7 +81,7 @@ impl HidWrapper {
{
#[cfg(feature = "wayvr")]
if let Some(wayvr) = &self.wayvr {
wayvr.borrow_mut().data.state.set_modifiers(mods);
wayvr.borrow_mut().set_modifiers(mods);
}
}
}