hyprshell_exec_lib/
util.rs1use anyhow::{Context, anyhow};
2use core_lib::{Active, ClientId, notify_warn};
3use hyprland::ctl::reload;
4use hyprland::data::{Client, Monitor, Workspace};
5use hyprland::keyword::Keyword;
6use hyprland::prelude::*;
7use semver::Version;
8use std::sync::{Mutex, OnceLock};
9use std::thread;
10use std::time::Duration;
11use tracing::{debug, info, trace, warn};
12
13#[must_use]
18pub fn to_client_id(id: &hyprland::shared::Address) -> ClientId {
19 u64::from_str_radix(id.to_string().trim_start_matches("0x"), 16)
20 .expect("Failed to parse client id, this should never happen")
21}
22
23#[must_use]
25pub fn to_client_address(id: ClientId) -> hyprland::shared::Address {
26 hyprland::shared::Address::new(format!("{id:x}"))
27}
28
29pub fn reload_hyprland_config() -> anyhow::Result<()> {
30 debug!("Reloading hyprland config");
31 reload::call().context("Failed to reload hyprland config")
32}
33
34fn get_prev_follow_mouse() -> &'static Mutex<Option<u8>> {
35 static PREV_FOLLOW_MOUSE: OnceLock<Mutex<Option<u8>>> = OnceLock::new();
36 PREV_FOLLOW_MOUSE.get_or_init(|| Mutex::new(None))
37}
38
39pub fn set_no_follow_mouse(value: Option<u8>) -> anyhow::Result<()> {
40 let value = value.unwrap_or(3);
41 let raw = hyprland::EvalRaw::new(format!(
42 "hl.config({{ input = {{ follow_mouse = {value} }} }})"
43 ));
44 raw.eval()
45 .with_context(|| format!("Failed to set follow_mouse to {value}"))?;
46 trace!("Set follow_mouse to {}", value);
47 Ok(())
48}
49
50pub fn reset_no_follow_mouse() -> anyhow::Result<()> {
51 let follow = get_prev_follow_mouse()
52 .lock()
53 .map_err(|e| anyhow::anyhow!("unable to lock get_prev_follow_mouse mutex: {e:?}"))?;
54 if let Some(follow) = follow.as_ref() {
55 set_no_follow_mouse(Some(*follow)).context("Failed to set follow_mouse to default")?;
56 trace!("Restored previous follow_mouse value: {follow}");
57 } else {
58 trace!("No previous follow_mouse value stored, skipping reset");
59 }
60 drop(follow);
61 Ok(())
62}
63
64pub fn set_follow_mouse_default() -> anyhow::Result<()> {
65 let mut lock = get_prev_follow_mouse()
66 .lock()
67 .map_err(|e| anyhow::anyhow!("unable to lock get_prev_follow_mouse mutex: {e:?}"))?;
68 let follow = Keyword::get("input:follow_mouse").context("keyword failed")?;
69 trace!("Storing previous follow_mouse value: {}", follow.value);
70 *lock = Some(follow.value.to_string().parse::<u8>().unwrap_or_default());
71 drop(lock);
72 Ok(())
73}
74
75pub fn get_initial_active() -> anyhow::Result<Active> {
80 let mut tries = 0;
81 loop {
82 match internal_get_initial_active() {
83 Ok(a) => break Ok(a),
84 Err(e) => {
85 if tries > 40 {
86 break Err(e);
87 }
88 warn!("waiting for correct initial active state from hyprland: {e:?}");
89 thread::sleep(Duration::from_millis(500));
90 }
91 }
92 tries += 1;
93 }
94}
95
96fn internal_get_initial_active() -> anyhow::Result<Active> {
97 let active_client = Client::get_active()
98 .ok()
99 .flatten()
100 .map(|c| to_client_id(&c.address));
101 let active_ws = Workspace::get_active()
102 .context("unable to get initial workspace")?
103 .address;
104 let active_monitor = Monitor::get_active()
105 .context("unable to get initial monitor")?
106 .id;
107
108 Ok(Active {
109 client: active_client,
110 workspace: active_ws,
111 monitor: active_monitor,
112 })
113}
114
115pub fn check_version() -> anyhow::Result<()> {
116 pub const MIN_VERSION: Version = Version::new(0, 57, 0);
117
118 let version = get_version()
119 .context("Failed to get version! (hyprland is probably outdated or too new??)")?;
120 trace!("hyprland {version:?}");
121
122 let version = version
123 .version
124 .unwrap_or_else(|| version.tag.trim_start_matches('v').to_string());
125 info!(
126 "Starting hyprshell {} in {} mode on hyprland {version}",
127 env!("CARGO_PKG_VERSION"),
128 if cfg!(debug_assertions) {
129 "debug"
130 } else {
131 "release"
132 },
133 );
134 let parsed_version = Version::parse(&version).context("Unable to parse hyprland Version")?;
135 if parsed_version.lt(&MIN_VERSION) {
136 notify_warn(&format!(
137 "hyprland version {parsed_version} is too old or unknown, please update to at least {MIN_VERSION}",
138 ));
139 }
140 Ok(())
141}
142
143pub fn get_version() -> anyhow::Result<hyprland::data::Version> {
144 let mut tries = 0;
145 loop {
146 match hyprland::data::Version::get() {
147 Ok(a) => break Ok(a),
148 Err(e) => {
149 if tries > 40 {
150 break Err(anyhow!(e));
151 }
152 warn!("waiting for correct version from hyprland: {e:?}");
153 thread::sleep(Duration::from_millis(500));
154 }
155 }
156 tries += 1;
157 }
158}