hyprshell_exec_lib/
collect.rs1use crate::to_client_id;
2use core_lib::{
3 ClientData, ClientId, FindByFirst, MonitorData, MonitorId, WorkspaceData, WorkspaceId,
4};
5use hyprland::data::{Client, Clients, Monitor, Monitors, Workspace, Workspaces};
6use hyprland::prelude::*;
7use tracing::{Level, span, warn};
8
9fn get_hypr_data() -> anyhow::Result<(Vec<Monitor>, Vec<Workspace>, Vec<Client>)> {
10 let _span = span!(Level::TRACE, "get_hypr_data").entered();
11 let monitors = Monitors::get()?.to_vec();
12 let workspaces = {
14 let mut workspaces = Workspaces::get()?
15 .into_iter()
16 .filter(|w| w.id != -1) .filter(|w| !w.id < 0) .collect::<Vec<_>>();
19
20 workspaces.sort_by(|a, b| a.id.cmp(&b.id));
21 workspaces
22 };
23 let clients = Clients::get()?
24 .into_iter()
25 .filter(|c| c.workspace.id != -1) .filter(|w| !w.workspace.id < 0) .collect::<Vec<_>>();
28
29 Ok((monitors, workspaces, clients))
30}
31
32#[allow(clippy::type_complexity)]
33pub fn collect_hypr_data() -> anyhow::Result<(
34 Vec<(ClientId, ClientData)>,
35 Vec<(WorkspaceId, WorkspaceData)>,
36 Vec<(MonitorId, MonitorData)>,
37 Option<(String, ClientId)>,
38 WorkspaceId,
39 MonitorId,
40)> {
41 let _span = span!(Level::TRACE, "convert_hypr_data").entered();
42
43 let (monitors, workspaces, clients) = get_hypr_data()?;
44
45 let mut monitor_data = {
48 let mut md: Vec<(MonitorId, MonitorData)> = Vec::with_capacity(monitors.iter().len());
49
50 monitors.iter().for_each(|monitor| {
51 md.push((
52 monitor.id,
53 MonitorData {
54 x: monitor.x,
55 y: monitor.y,
56 width: (monitor.width as f32 / monitor.scale) as u16,
57 height: (monitor.height as f32 / monitor.scale) as u16,
58 connector: monitor.name.clone(),
59 },
60 ));
61 });
62 md
63 };
64
65 let mut workspace_data = {
67 let mut wd: Vec<(WorkspaceId, WorkspaceData)> = Vec::with_capacity(workspaces.len());
68
69 for (monitor_id, monitor_data) in monitor_data.iter() {
70 let mut x_offset: i32 = 0;
71 workspaces
72 .iter()
73 .filter(|ws| ws.monitor_id == *monitor_id)
74 .for_each(|workspace| {
75 wd.push((
76 workspace.id,
77 WorkspaceData {
78 x: x_offset,
79 y: monitor_data.y,
80 name: workspace.name.clone(),
81 monitor: *monitor_id,
82 height: monitor_data.height,
83 width: monitor_data.width,
84 },
85 ));
86 x_offset += monitor_data.width as i32;
87 });
88 }
89 wd
90 };
91
92 let client_data = {
93 let mut cd: Vec<(ClientId, ClientData)> = Vec::with_capacity(clients.len());
94
95 for client in clients {
96 if workspace_data.find_by_first(&client.workspace.id).is_some() {
97 cd.push((
98 to_client_id(&client.address),
99 ClientData {
100 x: client.at.0,
101 y: client.at.1,
102 width: client.size.0,
103 height: client.size.1,
104 class: client.class.clone(),
105 workspace: client.workspace.id,
106 monitor: client.monitor,
107 focus_history_id: client.focus_history_id,
108 title: client.title.clone(),
109 floating: client.floating,
110 pid: client.pid,
111 enabled: false, },
113 ));
114 } else {
115 warn!(
116 "workspace {:?} not found for client {:?}",
117 client.workspace, client
118 );
119 }
120 }
121 cd
122 };
123
124 workspace_data.sort_by(|a, b| a.0.cmp(&b.0));
125 monitor_data.sort_by(|a, b| a.0.cmp(&b.0));
126
127 let active_ws = Workspace::get_active()?.id;
128 let active_monitor = Monitor::get_active()?.id;
129 let active_client = Client::get_active()?.map(|a| (a.class.clone(), to_client_id(&a.address)));
130
131 Ok((
132 client_data,
133 workspace_data,
134 monitor_data,
135 active_client,
136 active_ws,
137 active_monitor,
138 ))
139}