Skip to content

Commit

Permalink
delete objects when outputs are disconnected (to test)
Browse files Browse the repository at this point in the history
  • Loading branch information
Givralix committed Jan 1, 2024
1 parent db80241 commit de6fe07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/dbus_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ impl RootServer {

Ok(())
}

pub async fn remove_output(
&self,
instance: &mut Connection,
output_name: String,
) -> Result<()> {
let mut outputs = self.outputs.lock().await;
let mut output_index = None;
for (index, output) in outputs.iter().enumerate() {
if output.lock().await.output_name == output_name {
output_index = Some(index);
break;
}
}
if let Some(index) = output_index {
outputs.remove(index);
let path = format!("/outputs/{}", output_name.replace('-', "_"));
instance.object_server().remove::<Server, _>(path).await?;
}

Ok(())
}
}

#[dbus_interface(name = "rs.wl.gammarelay")]
Expand Down
14 changes: 10 additions & 4 deletions src/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub async fn run(
outputs,
gamma_manager,
new_output_names: Vec::new(),
output_names_to_delete: Vec::new(),
};

loop {
Expand All @@ -53,10 +54,11 @@ pub async fn run(
recv_events = conn.async_recv_events() => {
recv_events?;
conn.dispatch_events(&mut state);
if !state.new_output_names.is_empty() {
while let Some(output_name) = state.new_output_names.pop() {
root_server.add_output(&mut instance, tx.clone(), output_name).await?;
}
while let Some(output_name) = state.new_output_names.pop() {
root_server.add_output(&mut instance, tx.clone(), output_name).await?;
}
while let Some(output_name) = state.output_names_to_delete.pop() {
root_server.remove_output(&mut instance, output_name).await?;
}
}
Some(request) = rx.recv() => {
Expand All @@ -78,6 +80,7 @@ struct State {
outputs: Vec<Output>,
gamma_manager: ZwlrGammaControlManagerV1,
new_output_names: Vec<String>,
output_names_to_delete: Vec<String>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -144,6 +147,9 @@ fn wl_registry_cb(conn: &mut Connection<State>, state: &mut State, event: &wl_re
wl_registry::Event::GlobalRemove(name) => {
if let Some(output_index) = state.outputs.iter().position(|o| o.reg_name == *name) {
let output = state.outputs.swap_remove(output_index);
state
.output_names_to_delete
.push(output.name.clone().unwrap());
output.destroy(conn);
}
}
Expand Down

0 comments on commit de6fe07

Please sign in to comment.