veridian_kernel/desktop/wayland/
idle_inhibit.rs1#![allow(dead_code)]
12
13use alloc::collections::BTreeMap;
14
15use crate::error::KernelError;
16
17pub const ZWP_IDLE_INHIBIT_MANAGER_V1: &str = "zwp_idle_inhibit_manager_v1";
23
24pub const ZWP_IDLE_INHIBIT_MANAGER_V1_VERSION: u32 = 1;
26
27pub const ZWP_IDLE_INHIBIT_MANAGER_V1_DESTROY: u16 = 0;
30pub const ZWP_IDLE_INHIBIT_MANAGER_V1_CREATE_INHIBITOR: u16 = 1;
32
33pub const ZWP_IDLE_INHIBITOR_V1_DESTROY: u16 = 0;
36
37pub struct IdleInhibitor {
48 pub id: u32,
50 pub surface_id: u32,
52 pub active: bool,
54}
55
56impl IdleInhibitor {
57 pub fn new(id: u32, surface_id: u32) -> Self {
59 Self {
60 id,
61 surface_id,
62 active: true,
63 }
64 }
65}
66
67pub struct IdleInhibitManager {
73 inhibitors: BTreeMap<u32, IdleInhibitor>,
75 next_id: u32,
77}
78
79impl IdleInhibitManager {
80 pub fn new() -> Self {
82 Self {
83 inhibitors: BTreeMap::new(),
84 next_id: 1,
85 }
86 }
87
88 pub fn create_inhibitor(&mut self, surface_id: u32) -> Result<u32, KernelError> {
92 let id = self.next_id;
93 self.next_id += 1;
94
95 let inhibitor = IdleInhibitor::new(id, surface_id);
96 self.inhibitors.insert(id, inhibitor);
97
98 Ok(id)
99 }
100
101 pub fn destroy_inhibitor(&mut self, id: u32) -> Result<(), KernelError> {
103 self.inhibitors.remove(&id).ok_or(KernelError::NotFound {
104 resource: "idle_inhibitor",
105 id: id as u64,
106 })?;
107 Ok(())
108 }
109
110 pub fn remove_inhibitors_for_surface(&mut self, surface_id: u32) -> usize {
115 let before = self.inhibitors.len();
116 self.inhibitors
117 .retain(|_, inh| inh.surface_id != surface_id);
118 before - self.inhibitors.len()
119 }
120
121 pub fn is_idle_inhibited(&self) -> bool {
127 self.inhibitors.values().any(|inh| inh.active)
128 }
129
130 pub fn active_count(&self) -> usize {
132 self.inhibitors.values().filter(|inh| inh.active).count()
133 }
134
135 pub fn get_inhibitor(&self, id: u32) -> Option<&IdleInhibitor> {
137 self.inhibitors.get(&id)
138 }
139
140 pub fn inhibitor_count(&self) -> usize {
142 self.inhibitors.len()
143 }
144}
145
146impl Default for IdleInhibitManager {
147 fn default() -> Self {
148 Self::new()
149 }
150}
151
152#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[test]
161 fn test_create_inhibitor() {
162 let mut mgr = IdleInhibitManager::new();
163 let id = mgr.create_inhibitor(10).unwrap();
164 assert!(mgr.is_idle_inhibited());
165 assert_eq!(mgr.active_count(), 1);
166
167 let inh = mgr.get_inhibitor(id).unwrap();
168 assert_eq!(inh.surface_id, 10);
169 assert!(inh.active);
170 }
171
172 #[test]
173 fn test_destroy_inhibitor() {
174 let mut mgr = IdleInhibitManager::new();
175 let id = mgr.create_inhibitor(10).unwrap();
176 assert!(mgr.is_idle_inhibited());
177
178 mgr.destroy_inhibitor(id).unwrap();
179 assert!(!mgr.is_idle_inhibited());
180 assert_eq!(mgr.inhibitor_count(), 0);
181 }
182
183 #[test]
184 fn test_destroy_nonexistent() {
185 let mut mgr = IdleInhibitManager::new();
186 assert!(mgr.destroy_inhibitor(999).is_err());
187 }
188
189 #[test]
190 fn test_remove_for_surface() {
191 let mut mgr = IdleInhibitManager::new();
192 mgr.create_inhibitor(10).unwrap();
193 mgr.create_inhibitor(10).unwrap();
194 mgr.create_inhibitor(20).unwrap();
195
196 let removed = mgr.remove_inhibitors_for_surface(10);
197 assert_eq!(removed, 2);
198 assert_eq!(mgr.inhibitor_count(), 1);
199 assert!(mgr.is_idle_inhibited()); }
201
202 #[test]
203 fn test_not_inhibited_when_empty() {
204 let mgr = IdleInhibitManager::new();
205 assert!(!mgr.is_idle_inhibited());
206 assert_eq!(mgr.active_count(), 0);
207 }
208
209 #[test]
210 fn test_multiple_inhibitors() {
211 let mut mgr = IdleInhibitManager::new();
212 mgr.create_inhibitor(1).unwrap();
213 mgr.create_inhibitor(2).unwrap();
214 mgr.create_inhibitor(3).unwrap();
215 assert_eq!(mgr.active_count(), 3);
216 assert!(mgr.is_idle_inhibited());
217 }
218}