From 3644bc909984842f35adb1057ef6e0a277c1aa6a Mon Sep 17 00:00:00 2001 From: Ashley Wulber <48420062+wash2@users.noreply.github.com> Date: Fri, 29 Dec 2023 17:46:43 -0500 Subject: [PATCH] feat: cosmic settings daemon (#8) * feat: cosmic settings daemon * refactor: simplify by unifying trait for state and config * chore: add ping and pong methods * refactor: remove ping & pong methods * cleanup --- Cargo.toml | 1 + cosmic-settings-daemon/Cargo.toml | 15 +++++ cosmic-settings-daemon/examples/simple.rs | 33 ++++++++++ cosmic-settings-daemon/src/lib.rs | 75 +++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 cosmic-settings-daemon/Cargo.toml create mode 100644 cosmic-settings-daemon/examples/simple.rs create mode 100644 cosmic-settings-daemon/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 22ca53a..8d90b10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ resolver = "2" members = [ + "cosmic-settings-daemon", "mpris2", "networkmanager", "timedate", diff --git a/cosmic-settings-daemon/Cargo.toml b/cosmic-settings-daemon/Cargo.toml new file mode 100644 index 0000000..e56af69 --- /dev/null +++ b/cosmic-settings-daemon/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "cosmic-settings-daemon" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +zbus.workspace = true + +[dev-dependencies] +futures = "0.3" +tokio = { version = "1", features = ["full"] } +zbus.workspace = true +zbus.features = ["tokio"] \ No newline at end of file diff --git a/cosmic-settings-daemon/examples/simple.rs b/cosmic-settings-daemon/examples/simple.rs new file mode 100644 index 0000000..f6d8600 --- /dev/null +++ b/cosmic-settings-daemon/examples/simple.rs @@ -0,0 +1,33 @@ +use futures::StreamExt; + +#[tokio::main] +async fn main() -> zbus::Result<()> { + let connection = zbus::Connection::session().await?; + + let proxy = cosmic_settings_daemon::CosmicSettingsDaemonProxy::new(&connection).await?; + + println!("Display brightness: {}", proxy.display_brightness().await?); + println!( + "Keyboard brightness: {}", + proxy.keyboard_brightness().await? + ); + + let (config_path, name) = proxy + .watch_config("com.system76.CosmicTheme.Light", 1) + .await?; + dbg!(&config_path, &name); + let config_proxy = cosmic_settings_daemon::ConfigProxy::builder(&connection) + .path(config_path)? + .destination(name)? + .build() + .await?; + let mut stream = config_proxy.receive_changed().await?; + + println!("Watching config for the libcosmic light theme..."); + println!("Change the light theme in Settings > Appearance to trigger a signal."); + while let Some(c) = stream.next().await { + let c = c.args()?; + println!("Config changed: {} {}", c.id, c.key); + } + Ok(()) +} diff --git a/cosmic-settings-daemon/src/lib.rs b/cosmic-settings-daemon/src/lib.rs new file mode 100644 index 0000000..e4a59eb --- /dev/null +++ b/cosmic-settings-daemon/src/lib.rs @@ -0,0 +1,75 @@ +//! # DBus interface proxy for: `com.system76.CosmicSettingsDaemon` +//! +//! This code was generated by `zbus-xmlgen` `3.1.1` from DBus introspection data. +//! Source: `Interface '/com/system76/CosmicSettingsDaemon' from service 'com.system76.CosmicSettingsDaemon' on session bus`. +//! +//! You may prefer to adapt it, instead of using it verbatim. +//! +//! More information can be found in the +//! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html) +//! section of the zbus documentation. +//! +//! This DBus object implements +//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: +//! +//! * [`zbus::fdo::PeerProxy`] +//! * [`zbus::fdo::PropertiesProxy`] +//! * [`zbus::fdo::IntrospectableProxy`] +//! +//! …consequently `zbus-xmlgen` did not generate code for the above interfaces. + +use zbus::{dbus_proxy, names::OwnedWellKnownName}; + +#[dbus_proxy( + interface = "com.system76.CosmicSettingsDaemon", + default_service = "com.system76.CosmicSettingsDaemon", + default_path = "/com/system76/CosmicSettingsDaemon" +)] +trait CosmicSettingsDaemon { + /// DecreaseDisplayBrightness method + fn decrease_display_brightness(&self) -> zbus::Result<()>; + + /// DecreaseKeyboardBrightness method + fn decrease_keyboard_brightness(&self) -> zbus::Result<()>; + + /// IncreaseDisplayBrightness method + fn increase_display_brightness(&self) -> zbus::Result<()>; + + /// IncreaseKeyboardBrightness method + fn increase_keyboard_brightness(&self) -> zbus::Result<()>; + + /// WatchConfig method + fn watch_config( + &self, + id: &str, + version: u64, + ) -> zbus::Result<(zbus::zvariant::OwnedObjectPath, OwnedWellKnownName)>; + + /// WatchState method + fn watch_state( + &self, + id: &str, + version: u64, + ) -> zbus::Result<(zbus::zvariant::OwnedObjectPath, OwnedWellKnownName)>; + + /// DisplayBrightness property + #[dbus_proxy(property)] + fn display_brightness(&self) -> zbus::Result; + fn set_display_brightness(&self, value: i32) -> zbus::Result<()>; + + /// KeyboardBrightness property + #[dbus_proxy(property)] + fn keyboard_brightness(&self) -> zbus::Result; + fn set_keyboard_brightness(&self, value: i32) -> zbus::Result<()>; +} + +#[dbus_proxy( + interface = "com.system76.CosmicSettingsDaemon.Config", + default_service = "com.system76.CosmicSettingsDaemon.Config" +)] +trait Config { + /// Changed signal + #[dbus_proxy(signal)] + async fn changed(&self, id: String, key: String) -> zbus::Result<()>; +}