Skip to content

Commit

Permalink
wayland: allow binding to removed outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mahkoh committed Apr 25, 2024
1 parent 4651f76 commit c27bf4d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub trait Global: GlobalBase {
pub struct Globals {
next_name: NumCell<u32>,
registry: CopyHashMap<GlobalName, Rc<dyn Global>>,
removed: CopyHashMap<GlobalName, Rc<dyn Global>>,
pub outputs: CopyHashMap<GlobalName, Rc<WlOutputGlobal>>,
pub seats: CopyHashMap<GlobalName, Rc<WlSeatGlobal>>,
}
Expand All @@ -136,6 +137,7 @@ impl Globals {
let slf = Self {
next_name: NumCell::new(1),
registry: CopyHashMap::new(),
removed: CopyHashMap::new(),
outputs: Default::default(),
seats: Default::default(),
};
Expand Down Expand Up @@ -241,9 +243,17 @@ impl Globals {
Ok(global)
}

pub fn remove<T: WaylandGlobal>(&self, state: &State, global: &T) -> Result<(), GlobalsError> {
pub fn remove<T: RemovableWaylandGlobal>(
&self,
state: &State,
global: &T,
) -> Result<(), GlobalsError> {
let _global = self.take(global.name(), true)?;
global.remove(self);
let replacement = global.create_replacement();
assert_eq!(global.name(), replacement.name());
assert_eq!(global.interface().0, replacement.interface().0);
self.removed.set(global.name(), replacement);
self.broadcast(state, global.required_caps(), global.xwayland_only(), |r| {
r.send_global_remove(global.name())
});
Expand Down Expand Up @@ -295,7 +305,10 @@ impl Globals {
let res = if remove {
self.registry.remove(&name)
} else {
self.registry.get(&name)
match self.registry.get(&name) {
Some(res) => Some(res),
_ => self.removed.get(&name),
}
};
match res {
Some(g) => Ok(g),
Expand Down Expand Up @@ -330,3 +343,7 @@ pub trait WaylandGlobal: Global + 'static {
let _ = globals;
}
}

pub trait RemovableWaylandGlobal: WaylandGlobal {
fn create_replacement(&self) -> Rc<dyn Global>;
}
6 changes: 5 additions & 1 deletion src/ifs/wl_output.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod removed_output;

use {
crate::{
backend,
Expand Down Expand Up @@ -229,13 +231,15 @@ impl WlOutputGlobal {

global_base!(WlOutputGlobal, WlOutput, WlOutputError);

const OUTPUT_VERSION: u32 = 4;

impl Global for WlOutputGlobal {
fn singleton(&self) -> bool {
false
}

fn version(&self) -> u32 {
4
OUTPUT_VERSION
}

fn break_loops(&self) {
Expand Down
63 changes: 63 additions & 0 deletions src/ifs/wl_output/removed_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use {
crate::{
client::{Client, ClientError},
globals::{Global, GlobalName, RemovableWaylandGlobal},
ifs::wl_output::{WlOutput, WlOutputGlobal, OUTPUT_VERSION},
object::Version,
wire::WlOutputId,
},
std::rc::Rc,
thiserror::Error,
};

struct RemovedOutputGlobal {
name: GlobalName,
}

impl RemovedOutputGlobal {
fn bind_(
self: Rc<Self>,
id: WlOutputId,
client: &Rc<Client>,
version: Version,
) -> Result<(), RemovedOutputError> {
let obj = Rc::new(WlOutput {
global: Default::default(),
id,
xdg_outputs: Default::default(),
client: client.clone(),
version,
tracker: Default::default(),
});
track!(client, obj);
client.add_client_obj(&obj)?;
Ok(())
}
}

global_base!(RemovedOutputGlobal, WlOutput, RemovedOutputError);

impl Global for RemovedOutputGlobal {
fn singleton(&self) -> bool {
false
}

fn version(&self) -> u32 {
OUTPUT_VERSION
}
}

simple_add_global!(RemovedOutputGlobal);

impl RemovableWaylandGlobal for WlOutputGlobal {
fn create_replacement(&self) -> Rc<dyn Global> {
Rc::new(RemovedOutputGlobal { name: self.name })
}
}

#[derive(Debug, Error)]
enum RemovedOutputError {
#[error(transparent)]
ClientError(Box<ClientError>),
}
efrom!(RemovedOutputError, ClientError);
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use {
SyncFile,
},
gfx_apis::create_gfx_context,
globals::{Globals, GlobalsError, WaylandGlobal},
globals::{Globals, GlobalsError, RemovableWaylandGlobal, WaylandGlobal},
ifs::{
ext_foreign_toplevel_list_v1::ExtForeignToplevelListV1,
ext_session_lock_v1::ExtSessionLockV1,
Expand Down Expand Up @@ -511,7 +511,7 @@ impl State {
self.globals.add_global(self, global)
}

pub fn remove_global<T: WaylandGlobal>(&self, global: &T) -> Result<(), GlobalsError> {
pub fn remove_global<T: RemovableWaylandGlobal>(&self, global: &T) -> Result<(), GlobalsError> {
self.globals.remove(self, global)
}

Expand Down

0 comments on commit c27bf4d

Please sign in to comment.