WayVR: Read the WAYVR_DISPLAY_AUTH environment variable from external processes, matching it to our virtual display.

This enables applications to be run via Flatpak or any other wrapper application.
This commit is contained in:
Aleksander
2025-03-09 21:14:50 +01:00
parent 0b7cd61d05
commit 91af7c99b1
4 changed files with 83 additions and 40 deletions

View File

@@ -346,7 +346,10 @@ impl WayVR {
WayVRTask::NewToplevel(client_id, toplevel) => { WayVRTask::NewToplevel(client_id, toplevel) => {
// Attach newly created toplevel surfaces to displays // Attach newly created toplevel surfaces to displays
for client in &self.state.manager.clients { for client in &self.state.manager.clients {
if client.client.id() == client_id { if client.client.id() != client_id {
continue;
}
if let Some(process_handle) = if let Some(process_handle) =
process::find_by_pid(&self.state.processes, client.pid) process::find_by_pid(&self.state.processes, client.pid)
{ {
@@ -373,9 +376,6 @@ impl WayVR {
client.pid client.pid
); );
} }
break;
}
} }
} }
WayVRTask::ProcessTerminationRequest(process_handle) => { WayVRTask::ProcessTerminationRequest(process_handle) => {

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap; use std::{collections::HashMap, io::Read};
use wayvr_ipc::packet_server; use wayvr_ipc::packet_server;
@@ -88,6 +88,23 @@ impl Drop for WayVRProcess {
} }
} }
fn get_process_env_value(pid: i32, key: &str) -> anyhow::Result<Option<String>> {
let path = format!("/proc/{}/environ", pid);
let mut env_data = String::new();
std::fs::File::open(path)?.read_to_string(&mut env_data)?;
let lines: Vec<&str> = env_data.split('\0').filter(|s| !s.is_empty()).collect();
for line in lines {
if let Some(cell) = line.split_once('=') {
if cell.0 == key {
return Ok(Some(String::from(cell.1)));
}
}
}
Ok(None)
}
impl WayVRProcess { impl WayVRProcess {
fn is_running(&mut self) -> bool { fn is_running(&mut self) -> bool {
match self.child.try_wait() { match self.child.try_wait() {
@@ -153,8 +170,12 @@ impl ExternalProcess {
gen_id!(ProcessVec, Process, ProcessCell, ProcessHandle); gen_id!(ProcessVec, Process, ProcessCell, ProcessHandle);
pub fn find_by_pid(processes: &ProcessVec, pid: u32) -> Option<ProcessHandle> { pub fn find_by_pid(processes: &ProcessVec, pid: u32) -> Option<ProcessHandle> {
log::debug!("Finding process with PID {}", pid);
for (idx, cell) in processes.vec.iter().enumerate() { for (idx, cell) in processes.vec.iter().enumerate() {
if let Some(cell) = cell { let Some(cell) = cell else {
continue;
};
match &cell.obj { match &cell.obj {
Process::Managed(wayvr_process) => { Process::Managed(wayvr_process) => {
if wayvr_process.child.id() == pid { if wayvr_process.child.id() == pid {
@@ -168,7 +189,23 @@ pub fn find_by_pid(processes: &ProcessVec, pid: u32) -> Option<ProcessHandle> {
} }
} }
} }
log::debug!("Finding by PID failed, trying WAYVR_DISPLAY_AUTH...");
if let Ok(Some(value)) = get_process_env_value(pid as i32, "WAYVR_DISPLAY_AUTH") {
for (idx, cell) in processes.vec.iter().enumerate() {
let Some(cell) = cell else {
continue;
};
if let Process::Managed(wayvr_process) = &cell.obj {
if wayvr_process.auth_key == value {
return Some(ProcessVec::get_handle(cell, idx));
} }
}
}
}
log::debug!("Process find with PID {} failed", pid);
None None
} }

View File

@@ -326,7 +326,13 @@ fn modular_button_up(button: &mut ModularControl, _: &mut (), app: &mut AppState
} }
} }
fn modular_button_scroll(button: &mut ModularControl, _: &mut (), app: &mut AppState, delta_y: f32, delta_x: f32) { fn modular_button_scroll(
button: &mut ModularControl,
_: &mut (),
app: &mut AppState,
delta_y: f32,
_delta_x: f32,
) {
// want panic // want panic
let ModularData::Button(data) = button.state.as_mut().unwrap() else { let ModularData::Button(data) = button.state.as_mut().unwrap() else {
panic!("modular_button_scroll: button state is not Button"); panic!("modular_button_scroll: button state is not Button");

View File

@@ -3,7 +3,6 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
f32::consts::PI, f32::consts::PI,
ops::Add,
ptr, ptr,
sync::{atomic::AtomicU64, Arc}, sync::{atomic::AtomicU64, Arc},
time::{Duration, Instant}, time::{Duration, Instant},
@@ -151,7 +150,8 @@ impl InteractionHandler for ScreenInteractionHandler {
app.hid_provider.mouse_move(pos); app.hid_provider.mouse_move(pos);
} }
fn on_scroll(&mut self, app: &mut AppState, _hit: &PointerHit, delta_y: f32, delta_x: f32) { fn on_scroll(&mut self, app: &mut AppState, _hit: &PointerHit, delta_y: f32, delta_x: f32) {
app.hid_provider.wheel((delta_y*64.) as i32, (delta_x*64.) as i32) app.hid_provider
.wheel((delta_y * 64.) as i32, (delta_x * 64.) as i32)
} }
fn on_left(&mut self, _app: &mut AppState, _hand: usize) {} fn on_left(&mut self, _app: &mut AppState, _hand: usize) {}
} }