|
| 1 | +use std::{error::Error, future::Future}; |
| 2 | + |
| 3 | +use zbus::{fdo, message::Header, Connection}; |
| 4 | +use zbus_macros::interface; |
| 5 | + |
| 6 | +use crate::dbus::polkit::check_polkit; |
| 7 | + |
| 8 | +use super::Unregisterable; |
| 9 | + |
| 10 | +/// [LedEmitter] is any device that can implement changing LED colors |
| 11 | +pub trait LedEmitter { |
| 12 | + fn name(&self) -> impl Future<Output = Result<String, Box<dyn Error>>> + Send; |
| 13 | + fn get_colors_available( |
| 14 | + &self, |
| 15 | + ) -> impl Future<Output = Result<Vec<String>, Box<dyn Error>>> + Send; |
| 16 | + fn set_enabled( |
| 17 | + &mut self, |
| 18 | + enabled: bool, |
| 19 | + ) -> impl Future<Output = Result<(), Box<dyn Error>>> + Send; |
| 20 | + fn rumble(&mut self, value: f64) -> impl Future<Output = Result<(), Box<dyn Error>>> + Send; |
| 21 | + fn stop(&mut self) -> impl Future<Output = Result<(), Box<dyn Error>>> + Send; |
| 22 | +} |
| 23 | + |
| 24 | +/// DBus interface for a particular LED zone |
| 25 | +pub struct LedInterface<T> |
| 26 | +where |
| 27 | + T: LedEmitter + Send + Sync, |
| 28 | +{ |
| 29 | + device: T, |
| 30 | + name: String, |
| 31 | + colors: Vec<String>, |
| 32 | +} |
| 33 | + |
| 34 | +impl<T> LedInterface<T> |
| 35 | +where |
| 36 | + T: LedEmitter + Send + Sync, |
| 37 | +{ |
| 38 | + /// Create a new dbus interface for the given LED zone |
| 39 | + pub fn new(device: T, name: String, colors: Vec<String>) -> Self { |
| 40 | + Self { |
| 41 | + device, |
| 42 | + name, |
| 43 | + colors, |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[interface( |
| 49 | + name = "org.shadowblip.Output.LED", |
| 50 | + proxy(default_service = "org.shadowblip.InputPlumber") |
| 51 | +)] |
| 52 | +impl<T> LedInterface<T> |
| 53 | +where |
| 54 | + T: LedEmitter + Send + Sync + 'static, |
| 55 | +{ |
| 56 | + /// Name of the LED zone |
| 57 | + #[zbus(property)] |
| 58 | + async fn name(&self) -> fdo::Result<String> { |
| 59 | + Ok(self.name.clone()) |
| 60 | + } |
| 61 | + |
| 62 | + /// Colors available for the LED zone |
| 63 | + #[zbus(property)] |
| 64 | + async fn colors(&self) -> fdo::Result<Vec<String>> { |
| 65 | + Ok(self.colors.clone()) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<T> Unregisterable for LedInterface<T> where T: LedEmitter + Send + Sync {} |
| 70 | + |
| 71 | +/// DBus interface for controlling a single LED color |
| 72 | +pub struct LedColorInterface { |
| 73 | + color: String, |
| 74 | + value: f64, |
| 75 | + brightness: f64, |
| 76 | +} |
| 77 | + |
| 78 | +impl LedColorInterface { |
| 79 | + /// Create a new dbus interface for the given color |
| 80 | + pub fn new(color: &str) -> Self { |
| 81 | + Self { |
| 82 | + color: color.to_string(), |
| 83 | + value: 0.0, |
| 84 | + brightness: 0.0, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Update the color value |
| 89 | + pub fn update_value(conn: &Connection, path: &str, value: f64) { |
| 90 | + let conn = conn.clone(); |
| 91 | + let path = path.to_string(); |
| 92 | + tokio::task::spawn(async move { |
| 93 | + // Get the object instance at the given path so we can send DBus signal |
| 94 | + // updates |
| 95 | + let iface_ref = match conn |
| 96 | + .object_server() |
| 97 | + .interface::<_, Self>(path.clone()) |
| 98 | + .await |
| 99 | + { |
| 100 | + Ok(iface) => iface, |
| 101 | + Err(e) => { |
| 102 | + log::error!("Failed to get DBus interface {path}: {e}"); |
| 103 | + return; |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + let mut iface = iface_ref.get_mut().await; |
| 108 | + iface.value = value; |
| 109 | + let result = iface.value_changed(iface_ref.signal_emitter()).await; |
| 110 | + if let Err(e) = result { |
| 111 | + log::error!("Failed to signal property changed: {e}"); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + /// Update the color brightness |
| 117 | + pub fn update_brightness(conn: &Connection, path: &str, value: f64) { |
| 118 | + let conn = conn.clone(); |
| 119 | + let path = path.to_string(); |
| 120 | + tokio::task::spawn(async move { |
| 121 | + // Get the object instance at the given path so we can send DBus signal |
| 122 | + // updates |
| 123 | + let iface_ref = match conn |
| 124 | + .object_server() |
| 125 | + .interface::<_, Self>(path.clone()) |
| 126 | + .await |
| 127 | + { |
| 128 | + Ok(iface) => iface, |
| 129 | + Err(e) => { |
| 130 | + log::error!("Failed to get DBus interface {path}: {e}"); |
| 131 | + return; |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | + let mut iface = iface_ref.get_mut().await; |
| 136 | + iface.brightness = value; |
| 137 | + let result = iface.brightness_changed(iface_ref.signal_emitter()).await; |
| 138 | + if let Err(e) = result { |
| 139 | + log::error!("Failed to signal property changed: {e}"); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +#[interface( |
| 146 | + name = "org.shadowblip.Output.LED.Color", |
| 147 | + proxy(default_service = "org.shadowblip.InputPlumber") |
| 148 | +)] |
| 149 | +impl LedColorInterface { |
| 150 | + /// Name of the LED color |
| 151 | + #[zbus(property)] |
| 152 | + async fn color(&self) -> fdo::Result<String> { |
| 153 | + Ok(self.color.clone()) |
| 154 | + } |
| 155 | + |
| 156 | + /// Intensity value of the LED color. A value of 1.0 is maximum intensity, and 0.0 is minimum |
| 157 | + /// intensity. |
| 158 | + #[zbus(property)] |
| 159 | + async fn value(&self) -> fdo::Result<f64> { |
| 160 | + Ok(self.value) |
| 161 | + } |
| 162 | + |
| 163 | + #[zbus(property)] |
| 164 | + async fn set_value( |
| 165 | + &mut self, |
| 166 | + value: f64, |
| 167 | + #[zbus(connection)] conn: &Connection, |
| 168 | + #[zbus(header)] hdr: Option<Header<'_>>, |
| 169 | + ) -> fdo::Result<()> { |
| 170 | + check_polkit(conn, hdr, "org.shadowblip.Output.LED.Color.Value").await?; |
| 171 | + self.value = value.clamp(0.0, 1.0); |
| 172 | + Ok(()) |
| 173 | + } |
| 174 | + |
| 175 | + /// Brightness value of the LED color. A value of 1.0 is maximum brightness, and 0.0 is minimum |
| 176 | + /// brightness. |
| 177 | + #[zbus(property)] |
| 178 | + async fn brightness(&self) -> fdo::Result<f64> { |
| 179 | + Ok(0.0) |
| 180 | + } |
| 181 | + |
| 182 | + #[zbus(property)] |
| 183 | + async fn set_brightness( |
| 184 | + &mut self, |
| 185 | + value: f64, |
| 186 | + #[zbus(connection)] conn: &Connection, |
| 187 | + #[zbus(header)] hdr: Option<Header<'_>>, |
| 188 | + ) -> fdo::Result<()> { |
| 189 | + check_polkit(conn, hdr, "org.shadowblip.Output.LED.Color.Brightness").await?; |
| 190 | + self.brightness = value.clamp(0.0, 1.0); |
| 191 | + Ok(()) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +impl Unregisterable for LedColorInterface {} |
0 commit comments