mirror of
https://codeberg.org/icewind/shortcutd.git
synced 2026-06-03 09:14:07 +02:00
client: zbus/tokio
This commit is contained in:
parent
cea82db46b
commit
dfebec7c24
5 changed files with 68 additions and 120 deletions
|
|
@ -9,8 +9,10 @@ name = "shortcutd"
|
|||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
dbus = { version = "0.8.4" }
|
||||
futures = "0.3.28"
|
||||
zbus = { version = "3.13.1", features = ["tokio"], default-features = false }
|
||||
evdev-shortcut = { version = "0.1.1", default_features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
test-case = "3.1.0"
|
||||
test-case = "3.1.0"
|
||||
tokio = { version = "1.28.2", features = ["macros", "rt-multi-thread"] }
|
||||
|
|
@ -1,23 +1,24 @@
|
|||
use shortcutd::{Shortcut, ShortcutClient};
|
||||
use shortcutd::{ShortcutClient};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
use futures::pin_mut;
|
||||
use futures::stream::iter;
|
||||
use futures::StreamExt;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut client = ShortcutClient::new()?;
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let client = ShortcutClient::new().await?;
|
||||
|
||||
let shortcut: Shortcut = "<Ctrl><Alt>-KeyP".parse()?;
|
||||
let streams = [
|
||||
Box::pin(client.register("<Ctrl>-KeyM".parse()?).await?),
|
||||
Box::pin(client.register("<Ctrl><Alt>-KeyO".parse()?).await?),
|
||||
];
|
||||
|
||||
client.register(shortcut, |s| {
|
||||
eprintln!("shortcut1 {}", s);
|
||||
})?;
|
||||
let stream = iter(streams).flatten_unordered(None);
|
||||
|
||||
let shortcut: Shortcut = "<Ctrl><Alt>-KeyO".parse()?;
|
||||
pin_mut!(stream);
|
||||
|
||||
client.register(shortcut, |s| {
|
||||
eprintln!("shortcut2 {}", s);
|
||||
})?;
|
||||
|
||||
loop {
|
||||
client.process(Duration::from_millis(1000))?;
|
||||
while let Some(event) = stream.next().await {
|
||||
println!("{} {}", event.shortcut, event.state.as_str());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,97 +1,63 @@
|
|||
use dbus::arg;
|
||||
use dbus::blocking;
|
||||
pub use evdev_shortcut::Shortcut;
|
||||
use evdev_shortcut::{ShortcutEvent, ShortcutState};
|
||||
use futures::Stream;
|
||||
use zbus::{Connection, fdo};
|
||||
use zbus::dbus_proxy;
|
||||
use futures::StreamExt;
|
||||
|
||||
const INTERFACE: &'static str = "nl.icewind.shortcutd";
|
||||
|
||||
pub trait NlIcewindShortcutd {
|
||||
fn register(&self, shortcut: &str) -> Result<String, dbus::Error>;
|
||||
#[dbus_proxy(
|
||||
interface = "nl.icewind.shortcutd",
|
||||
default_service = "nl.icewind.shortcutd",
|
||||
default_path = "/register"
|
||||
)]
|
||||
trait Register {
|
||||
async fn register(&self, shortcut: &str) -> fdo::Result<String>;
|
||||
}
|
||||
|
||||
impl<'a, C: ::std::ops::Deref<Target = blocking::Connection>> NlIcewindShortcutd
|
||||
for blocking::Proxy<'a, C>
|
||||
{
|
||||
fn register(&self, shortcut: &str) -> Result<String, dbus::Error> {
|
||||
self.method_call(INTERFACE, "Register", (shortcut,))
|
||||
.and_then(|r: (String,)| Ok(r.0))
|
||||
}
|
||||
|
||||
#[dbus_proxy(
|
||||
interface = "nl.icewind.shortcutd",
|
||||
default_service = "nl.icewind.shortcutd"
|
||||
)]
|
||||
trait ShortcutSignal {
|
||||
#[dbus_proxy(signal)]
|
||||
async fn triggered(&self, pressed: bool) -> fdo::Result<()>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NlIcewindShortcutdTriggered {}
|
||||
|
||||
impl arg::AppendAll for NlIcewindShortcutdTriggered {
|
||||
fn append(&self, _: &mut arg::IterAppend) {}
|
||||
}
|
||||
|
||||
impl arg::ReadAll for NlIcewindShortcutdTriggered {
|
||||
fn read(_: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
|
||||
Ok(NlIcewindShortcutdTriggered {})
|
||||
}
|
||||
}
|
||||
|
||||
impl dbus::message::SignalArgs for NlIcewindShortcutdTriggered {
|
||||
const NAME: &'static str = "Triggered";
|
||||
const INTERFACE: &'static str = INTERFACE;
|
||||
}
|
||||
|
||||
pub trait OrgFreedesktopDBusIntrospectable {
|
||||
fn introspect(&self) -> Result<String, dbus::Error>;
|
||||
}
|
||||
|
||||
impl<'a, C: ::std::ops::Deref<Target = blocking::Connection>> OrgFreedesktopDBusIntrospectable
|
||||
for blocking::Proxy<'a, C>
|
||||
{
|
||||
fn introspect(&self) -> Result<String, dbus::Error> {
|
||||
self.method_call("org.freedesktop.DBus.Introspectable", "Introspect", ())
|
||||
.and_then(|r: (String,)| Ok(r.0))
|
||||
}
|
||||
}
|
||||
|
||||
// Autogenerated code ends here.
|
||||
|
||||
use dbus::blocking::Connection;
|
||||
use dbus::Message;
|
||||
use std::time::Duration;
|
||||
|
||||
pub struct ShortcutClient {
|
||||
connection: Connection,
|
||||
}
|
||||
|
||||
impl ShortcutClient {
|
||||
pub fn new() -> Result<Self, dbus::Error> {
|
||||
pub async fn new() -> Result<Self, zbus::Error> {
|
||||
Ok(ShortcutClient {
|
||||
connection: Connection::new_system()?,
|
||||
connection: Connection::system().await?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn register(
|
||||
&mut self,
|
||||
shortcut: Shortcut,
|
||||
handler: impl Fn(Shortcut) + Send + 'static,
|
||||
) -> Result<(), dbus::Error> {
|
||||
let path = self
|
||||
.connection
|
||||
.with_proxy(INTERFACE, "/register", Duration::from_millis(5000))
|
||||
.register(&format!("{}", shortcut))?;
|
||||
|
||||
let proxy = self
|
||||
.connection
|
||||
.with_proxy(INTERFACE, &path, Duration::from_millis(5000));
|
||||
|
||||
// Let's start listening to signals.
|
||||
let _id = proxy.match_signal(
|
||||
move |_: NlIcewindShortcutdTriggered, _: &Connection, _: &Message| {
|
||||
handler(shortcut.clone());
|
||||
true
|
||||
},
|
||||
);
|
||||
|
||||
Ok(())
|
||||
pub fn from_zbus(connection: Connection) -> Self {
|
||||
ShortcutClient { connection }
|
||||
}
|
||||
|
||||
pub fn process(&mut self, timeout: Duration) -> Result<(), dbus::Error> {
|
||||
self.connection.process(timeout)?;
|
||||
Ok(())
|
||||
pub async fn register(
|
||||
&self,
|
||||
shortcut: Shortcut,
|
||||
) -> Result<impl Stream<Item=ShortcutEvent> + '_, zbus::Error> {
|
||||
let register = RegisterProxy::new(&self.connection).await?;
|
||||
let path = register.register(&format!("{}", shortcut)).await?;
|
||||
|
||||
let p = ShortcutSignalProxy::builder(&self.connection).path(path.as_str())?.build().await?;
|
||||
let signals = p.receive_triggered().await?;
|
||||
|
||||
Ok(signals.filter_map(move |signal| {
|
||||
let shortcut = shortcut.clone();
|
||||
async move {
|
||||
let pressed = signal.args().ok()?.pressed;
|
||||
Some(ShortcutEvent {
|
||||
shortcut,
|
||||
state: if pressed { ShortcutState::Pressed } else { ShortcutState::Released },
|
||||
})
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue