Skip to content

Instantly share code, notes, and snippets.

@main--

main--/dbus.rs Secret

Created December 22, 2016 02:26
Show Gist options
  • Save main--/f72663801f66420f31597277130e0612 to your computer and use it in GitHub Desktop.
Save main--/f72663801f66420f31597277130e0612 to your computer and use it in GitHub Desktop.
extern crate dbus;
use std::io::BufRead;
use dbus::{Connection, ConnectionItem, BusType, Message, OwnedFd};
use dbus::arg::Array;
fn main() {
let bus = Connection::get_private(BusType::System).unwrap();
let mut ih = Message::new_method_call("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "Inhibit").unwrap();
ih.append_items(&["sleep".into(), "test".into(), "test".into(), "delay".into()]);
let resp = bus.send_with_reply_and_block(ih, 2000).unwrap();
let fd: OwnedFd = resp.get1().unwrap();
drop(resp);
bus.add_match("interface='org.freedesktop.login1.Manager',member='PrepareForSleep'").unwrap();
let mut fdc = Some(fd);
loop {
match bus.iter(1000).next().unwrap() {
ConnectionItem::Signal(ref s) if &*s.interface().unwrap() == "org.freedesktop.login1.Manager" && &*s.member().unwrap() == "PrepareForSleep" => {
let starting: bool = s.get1().unwrap();
if starting {
fdc = None
} else {
// call Inhibit
}
}
_ => (),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment