1use crate::util::to_client_id;
2use anyhow::Context;
3use core_lib::{
4 ByFirst, ClientData, ClientId, MonitorData, MonitorId, WorkspaceAddress, WorkspaceData,
5};
6use hyprland::data::{Client, Clients, Monitor, Monitors, Workspace, WorkspaceKind, Workspaces};
7use hyprland::prelude::*;
8use regex::Regex;
9use tracing::{debug_span, instrument, trace, warn};
10
11fn get_hypr_data() -> anyhow::Result<(Vec<Monitor>, Vec<Workspace>, Vec<Client>)> {
12 let _span = debug_span!("get_hypr_data").entered();
13 let monitors = Monitors::get().context("monitors failed")?.to_vec();
14 let workspaces = Workspaces::get()
16 .context("workspaces failed")?
17 .into_iter()
18 .filter(|w| !w.address.is_empty())
19 .collect::<Vec<_>>();
20 let clients = Clients::get()
21 .context("clients failed")?
22 .into_iter()
23 .filter(|c| !c.workspace.address.is_empty())
24 .collect::<Vec<_>>();
25
26 Ok((monitors, workspaces, clients))
27}
28
29#[allow(clippy::type_complexity, clippy::too_many_lines)]
30#[instrument(level = "debug")]
31pub fn collect_hypr_data(
32 exclude_workspaces: Option<&Regex>,
33) -> anyhow::Result<(
34 Vec<(ClientId, ClientData)>,
35 Vec<(WorkspaceAddress, WorkspaceData)>,
36 Vec<(MonitorId, MonitorData)>,
37 Option<(String, ClientId)>,
38 WorkspaceAddress,
39 MonitorId,
40)> {
41 let (monitors, workspaces, clients) =
42 get_hypr_data().context("loading hyprland data failed")?;
43
44 let mut monitor_data = {
47 let mut md: Vec<(MonitorId, MonitorData)> = Vec::with_capacity(monitors.iter().len());
48
49 for monitor in &monitors {
50 #[allow(clippy::cast_sign_loss)]
51 md.push((
52 monitor.id,
53 MonitorData {
54 id: monitor.id,
55 x: monitor.x,
56 y: monitor.y,
57 width: (f32::from(monitor.width) / monitor.scale) as u16,
58 height: (f32::from(monitor.height) / monitor.scale) as u16,
59 connector: monitor.name.clone(),
60 scale: monitor.scale,
61 },
62 ));
63 }
64 md
65 };
66
67 let mut workspace_data = {
69 let mut wd: Vec<(WorkspaceAddress, WorkspaceData)> = Vec::with_capacity(workspaces.len());
70
71 for (monitor_id, monitor_data) in &monitor_data {
72 workspaces
73 .iter()
74 .filter(|ws| ws.monitor_id == Some(*monitor_id))
75 .for_each(|workspace| {
76 wd.push((
77 workspace.address.clone(),
78 WorkspaceData {
79 address: workspace.address.clone(),
80 name: workspace.name.clone(),
81 special: workspace.workspace_type == WorkspaceKind::Special,
82 monitor: *monitor_id,
83 height: monitor_data.height,
84 width: monitor_data.width,
85 any_client_enabled: true, },
87 ));
88 });
89 }
90 wd
91 };
92
93 let mut client_data = {
94 let mut cd: Vec<(ClientId, ClientData)> = Vec::with_capacity(clients.len());
95
96 for client in clients {
97 let Some(monitor) = client.monitor else {
98 continue;
99 };
100 if workspace_data
101 .find_by_first(&client.workspace.address)
102 .is_some()
103 {
104 cd.push((
105 to_client_id(&client.address),
106 ClientData {
107 x: client.at.0,
108 y: client.at.1,
109 width: client.size.0,
110 height: client.size.1,
111 class: client.class.clone(),
112 workspace: client.workspace.address.clone(),
113 monitor,
114 focus_history_id: client.focus_history_id,
115 title: client.title.clone(),
116 floating: client.floating,
117 pid: client.pid,
118 enabled: true, },
120 ));
121 } else {
122 warn!(
123 "workspace {:?} not found for client {client:?}",
124 client.workspace
125 );
126 }
127 }
128 cd
129 };
130
131 trace!(
134 "workspaces bevore filter by regex: {}",
135 workspace_data.len()
136 );
137 workspace_data.retain(|(_, ws)| {
138 exclude_workspaces
139 .as_ref()
140 .is_none_or(|reg| !reg.is_match(&ws.name))
141 });
142 trace!("workspaces after filter by regex: {}", workspace_data.len());
143 client_data.retain(|(_id, cl)| workspace_data.find_by_first(&cl.workspace).is_some());
144
145 workspace_data.sort_by(|(a_address, a), (b_address, b)| {
146 if a.special != b.special {
147 return a.special.cmp(&b.special);
148 }
149 match (a.address.parse::<u32>().ok(), b.address.parse::<u32>().ok()) {
150 (Some(a_id), Some(b_id)) => a_id.cmp(&b_id),
151 (Some(_), None) => std::cmp::Ordering::Less,
152 (None, Some(_)) => std::cmp::Ordering::Greater,
153 (None, None) => a_address.cmp(b_address),
154 }
155 });
156 monitor_data.sort_by_key(|a| a.0);
157
158 let active_ws = Workspace::get_active()
161 .map(|w| w.address)
162 .context("active workspace failed")?;
163 let active_ws = Client::get_active()
164 .context("active client failed")?
165 .map_or(active_ws, |a| a.workspace.address);
166 let active_monitor = Monitor::get_active().context("active monitor failed")?.id;
167 let active_client = Client::get_active()
168 .context("active client failed")?
169 .map(|a| (a.class.clone(), to_client_id(&a.address)));
170
171 Ok((
172 client_data,
173 workspace_data,
174 monitor_data,
175 active_client,
176 active_ws,
177 active_monitor,
178 ))
179}
180
181pub fn get_monitors() -> Vec<MonitorData> {
182 Monitors::get()
183 .map_or(vec![], HyprDataVec::to_vec)
184 .iter()
185 .map(|m| MonitorData {
186 id: m.id,
187 x: m.x,
188 y: m.y,
189 width: m.width,
190 height: m.height,
191 connector: m.name.clone(),
192 scale: m.scale,
193 })
194 .collect()
195}
196
197#[must_use]
198pub fn get_current_monitor() -> Option<MonitorData> {
199 Monitor::get_active().ok().map(|m| MonitorData {
200 id: m.id,
201 x: m.x,
202 y: m.y,
203 width: m.width,
204 height: m.height,
205 connector: m.name.clone(),
206 scale: m.scale,
207 })
208}
209
210pub fn get_client_classes() -> Vec<String> {
211 Clients::get()
212 .map_or(vec![], HyprDataVec::to_vec)
213 .iter()
214 .map(|client| client.class.clone())
215 .collect()
216}