|
| 1 | +use std::{ |
| 2 | + collections::HashMap, |
| 3 | + str::FromStr as _, |
| 4 | + sync::Arc, |
| 5 | +}; |
| 6 | + |
| 7 | +use tokio::sync::RwLock; |
| 8 | +use zbus::{ |
| 9 | + fdo, |
| 10 | + interface, |
| 11 | + object_server::SignalEmitter, |
| 12 | + zvariant::Value, |
| 13 | +}; |
| 14 | + |
| 15 | +use crate::{ |
| 16 | + profile::PowerProfile, |
| 17 | + system::DaemonState, |
| 18 | +}; |
| 19 | + |
| 20 | +const DRIVER_NAME: &str = "watt"; |
| 21 | +const CPU_DRIVER_NAME: &str = "watt"; |
| 22 | + |
| 23 | +pub struct PowerProfilesInterface { |
| 24 | + state: Arc<RwLock<DaemonState>>, |
| 25 | +} |
| 26 | + |
| 27 | +impl PowerProfilesInterface { |
| 28 | + pub fn new(state: Arc<RwLock<DaemonState>>) -> Self { |
| 29 | + Self { state } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[interface(name = "net.hadess.PowerProfiles")] |
| 34 | +impl PowerProfilesInterface { |
| 35 | + // Properties |
| 36 | + #[zbus(property)] |
| 37 | + async fn active_profile(&self) -> String { |
| 38 | + let state = self.state.read().await; |
| 39 | + state.active_profile().as_str().to_owned() |
| 40 | + } |
| 41 | + |
| 42 | + #[zbus(property)] |
| 43 | + async fn set_active_profile(&self, profile: &str) -> zbus::Result<()> { |
| 44 | + let profile = match PowerProfile::from_str(profile) { |
| 45 | + Ok(profile) => profile, |
| 46 | + Err(_) => { |
| 47 | + return Err(zbus::Error::from(fdo::Error::InvalidArgs(format!( |
| 48 | + "invalid profile: {profile}, valid: performance, balanced, \ |
| 49 | + power-saver" |
| 50 | + )))); |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + let mut state = self.state.write().await; |
| 55 | + state.set_active_profile(profile); |
| 56 | + |
| 57 | + log::info!( |
| 58 | + "D-Bus: active profile set to {profile}", |
| 59 | + profile = profile.as_str() |
| 60 | + ); |
| 61 | + |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | + |
| 65 | + #[zbus(property)] |
| 66 | + async fn profiles(&self) -> Vec<HashMap<String, Value<'_>>> { |
| 67 | + PowerProfile::all() |
| 68 | + .iter() |
| 69 | + .map(|profile| { |
| 70 | + let mut map = HashMap::new(); |
| 71 | + map.insert("Profile".to_owned(), Value::from(profile.as_str())); |
| 72 | + map.insert("Driver".to_owned(), Value::from(DRIVER_NAME)); |
| 73 | + map.insert("CpuDriver".to_owned(), Value::from(CPU_DRIVER_NAME)); |
| 74 | + map |
| 75 | + }) |
| 76 | + .collect() |
| 77 | + } |
| 78 | + |
| 79 | + #[zbus(property)] |
| 80 | + async fn actions(&self) -> Vec<String> { |
| 81 | + Vec::new() |
| 82 | + } |
| 83 | + |
| 84 | + #[zbus(property)] |
| 85 | + async fn performance_degraded(&self) -> String { |
| 86 | + let state = self.state.read().await; |
| 87 | + state.performance_degraded().unwrap_or_default().to_owned() |
| 88 | + } |
| 89 | + |
| 90 | + #[zbus(property)] |
| 91 | + async fn performance_inhibited(&self) -> String { |
| 92 | + let state = self.state.read().await; |
| 93 | + match state.profile_holds().first() { |
| 94 | + Some(hold) => hold.reason.clone(), |
| 95 | + None => String::new(), |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + #[zbus(property)] |
| 100 | + async fn active_profile_holds(&self) -> Vec<HashMap<String, Value<'_>>> { |
| 101 | + let state = self.state.read().await; |
| 102 | + state |
| 103 | + .profile_holds() |
| 104 | + .into_iter() |
| 105 | + .map(|hold| { |
| 106 | + let mut map = HashMap::new(); |
| 107 | + map.insert("Profile".to_owned(), Value::from(hold.profile.as_str())); |
| 108 | + map.insert("Reason".to_owned(), Value::from(hold.reason)); |
| 109 | + map |
| 110 | + .insert("ApplicationId".to_owned(), Value::from(hold.application_id)); |
| 111 | + map |
| 112 | + }) |
| 113 | + .collect() |
| 114 | + } |
| 115 | + |
| 116 | + async fn hold_profile( |
| 117 | + &self, |
| 118 | + #[zbus(signal_emitter)] signal_emitter: SignalEmitter<'_>, |
| 119 | + profile: String, |
| 120 | + reason: String, |
| 121 | + application_id: String, |
| 122 | + ) -> fdo::Result<u32> { |
| 123 | + let profile = match PowerProfile::from_str(&profile) { |
| 124 | + Ok(profile) => profile, |
| 125 | + Err(_) => { |
| 126 | + return Err(fdo::Error::InvalidArgs(format!( |
| 127 | + "invalid profile: {profile}" |
| 128 | + ))); |
| 129 | + }, |
| 130 | + }; |
| 131 | + |
| 132 | + if profile == PowerProfile::Balanced { |
| 133 | + return Err(fdo::Error::InvalidArgs( |
| 134 | + "profile holds only support performance and power-saver".to_owned(), |
| 135 | + )); |
| 136 | + } |
| 137 | + |
| 138 | + let mut state = self.state.write().await; |
| 139 | + let cookie = state.add_profile_hold(profile, reason, application_id); |
| 140 | + |
| 141 | + log::info!("D-Bus profile hold added, cookie={cookie}"); |
| 142 | + |
| 143 | + // Emit property change signals |
| 144 | + drop(state); // release lock before emitting signals |
| 145 | + |
| 146 | + // Log signal failures but don't fail the operation. |
| 147 | + // State was already mutated. |
| 148 | + if let Err(e) = self.active_profile_holds_changed(&signal_emitter).await { |
| 149 | + log::warn!("failed to emit ActiveProfileHolds change signal: {e}"); |
| 150 | + } |
| 151 | + |
| 152 | + if let Err(e) = self.active_profile_changed(&signal_emitter).await { |
| 153 | + log::warn!("failed to emit ActiveProfile change signal: {e}"); |
| 154 | + } |
| 155 | + |
| 156 | + Ok(cookie) |
| 157 | + } |
| 158 | + |
| 159 | + async fn release_profile( |
| 160 | + &self, |
| 161 | + #[zbus(signal_emitter)] signal_emitter: SignalEmitter<'_>, |
| 162 | + cookie: u32, |
| 163 | + ) -> fdo::Result<()> { |
| 164 | + let mut state = self.state.write().await; |
| 165 | + state |
| 166 | + .release_profile_hold(cookie) |
| 167 | + .map_err(|error| fdo::Error::Failed(error.to_string()))?; |
| 168 | + |
| 169 | + log::info!("D-Bus profile hold released, cookie={cookie}"); |
| 170 | + |
| 171 | + drop(state); |
| 172 | + |
| 173 | + if let Err(e) = self.active_profile_holds_changed(&signal_emitter).await { |
| 174 | + log::warn!("Failed to emit ActiveProfileHolds change signal: {e}"); |
| 175 | + } |
| 176 | + |
| 177 | + if let Err(e) = self.active_profile_changed(&signal_emitter).await { |
| 178 | + log::warn!("Failed to emit ActiveProfile change signal: {e}"); |
| 179 | + } |
| 180 | + |
| 181 | + Ok(()) |
| 182 | + } |
| 183 | +} |
0 commit comments