Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
Call roundtrip.
Browse files Browse the repository at this point in the history
Otherwise it doesn't get sent it looks like.
  • Loading branch information
kelvie committed Jan 5, 2024
1 parent 894a0a8 commit 4854a20
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
59 changes: 44 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Bridbe between the org.freedesktop.ScreenSaver interface and the Wayland idle
// inhibitor protocol.

mod xdg_screensaver;
mod wayland;
mod xdg_screensaver;

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -51,26 +51,55 @@ impl OrgFreedesktopScreenSaver for Arc<Mutex<OrgFreedesktopScreenSaverServer>> {
application_name: String,
reason_for_inhibit: String,
) -> Result<(u32,), dbus::MethodErr> {
log::info!("Inhibiting screensaver for {:?} because {:?}", application_name, reason_for_inhibit);
let inhibitor = self.lock().unwrap().inhibit_manager.create_inhibitor();
let cookie = self.lock().unwrap().insert_inhibitor(StoredInhibitor {
inhibitor,
name: application_name,
reason: reason_for_inhibit,
});

return Ok((cookie,));
log::info!(
"Inhibiting screensaver for {:?} because {:?}",
application_name,
reason_for_inhibit
);
match self.lock().unwrap().inhibit_manager.create_inhibitor() {
Ok(inhibitor) => Ok((self.lock().unwrap().insert_inhibitor(StoredInhibitor {
inhibitor,
name: application_name,
reason: reason_for_inhibit,
}),)),
Err(e) => {
log::error!("Failed to create inhibitor: {:?}", e);
Err(dbus::MethodErr::failed(&format!(
"Failed to create inhibitor: {:?}",
e
)))
}
}
}

fn un_inhibit(&mut self, cookie: u32) -> Result<(), dbus::MethodErr> {
log::info!("Uninhibiting {:?}", cookie);
let inhibitor = self.lock().unwrap().inhibitors_by_cookie.remove(&cookie);
log::info!("Inhibitor found? {:?}", inhibitor);
if let Some(inhibitor) = inhibitor {
self.lock().unwrap().inhibit_manager.destroy_inhibitor(inhibitor.inhibitor);
match inhibitor {
None => {
Err(dbus::MethodErr::failed(&format!(
"No inhibitor with cookie {}",
cookie
)))
},
Some(inhibitor) => {
match self
.lock()
.unwrap()
.inhibit_manager
.destroy_inhibitor(inhibitor.inhibitor)
{
Ok(_) => Ok(()),
Err(e) => {
log::error!("Failed to destroy inhibitor: {:?}", e);
Err(dbus::MethodErr::failed(&format!(
"Failed to destroy inhibitor: {:?}",
e
)))
}
}
}
}

return Ok(());
}
}

Expand Down
22 changes: 18 additions & 4 deletions src/wayland.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// get a wayland client

use wayland_client::{
backend::WaylandError,
protocol::{
__interfaces::WL_COMPOSITOR_INTERFACE,
wl_compositor::WlCompositor,
Expand All @@ -9,6 +10,7 @@ use wayland_client::{
},
Dispatch,
};

use wayland_protocols::wp::idle_inhibit::zv1::client::{
__interfaces::ZWP_IDLE_INHIBIT_MANAGER_V1_INTERFACE,
zwp_idle_inhibit_manager_v1::ZwpIdleInhibitManagerV1,
Expand Down Expand Up @@ -69,6 +71,7 @@ impl Dispatch<ZwpIdleInhibitManagerV1, ()> for DispatcherListener {
) {
}
}

impl Dispatch<WlRegistry, ()> for DispatcherListener {
fn event(
state: &mut Self,
Expand Down Expand Up @@ -109,16 +112,26 @@ pub(crate) struct InhibitorManager {
manager: ZwpIdleInhibitManagerV1,
dummy_surface: WlSurface,
queue_handle: wayland_client::QueueHandle<DispatcherListener>,
conn: wayland_client::Connection,
}

impl InhibitorManager {
pub fn create_inhibitor(&self) -> ZwpIdleInhibitorV1 {
self.manager
.create_inhibitor(&self.dummy_surface, &self.queue_handle, ())
pub fn create_inhibitor(&self) -> Result<ZwpIdleInhibitorV1, WaylandError> {
let inhibitor = self
.manager
.create_inhibitor(&self.dummy_surface, &self.queue_handle, ());
match self.conn.roundtrip() {
Ok(_) => Ok(inhibitor),
Err(e) => Err(e),
}
}

pub fn destroy_inhibitor(&self, inhibitor: ZwpIdleInhibitorV1) {
pub fn destroy_inhibitor(&self, inhibitor: ZwpIdleInhibitorV1) -> Result<(), WaylandError> {
inhibitor.destroy();
match self.conn.roundtrip() {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}
}

Expand All @@ -141,6 +154,7 @@ pub async fn get_inhibit_manager() -> Result<InhibitorManager, Box<dyn std::erro
manager: dl.manager.take().unwrap(),
dummy_surface: dl.dummy_surface.take().unwrap(),
queue_handle: qh,
conn,
});
}
}
Expand Down

0 comments on commit 4854a20

Please sign in to comment.