hyprshell_exec_lib/
plugin.rs

1use anyhow::{Context, bail};
2use config_lib::Modifier;
3use hyprland::ctl::plugin;
4use hyprland_plugin::PluginConfig;
5use std::path::Path;
6use std::sync::OnceLock;
7use tracing::{debug, debug_span, trace};
8
9// info: trying to load a plugin causes hyprland to issue a reload
10// this will cause hyprshell to restart.
11// this second restart wont reload the plugin as the plugin config didnt change
12// if the plugin fails to load it however tries again which the triggers the next reload
13static PLUGIN_COULD_BE_BUILD: OnceLock<bool> = OnceLock::new();
14
15pub fn load_plugin(
16    switch: Option<Modifier>,
17    overview: Option<(Modifier, Box<str>)>,
18) -> anyhow::Result<()> {
19    let _span = debug_span!("load_plugin").entered();
20
21    if PLUGIN_COULD_BE_BUILD.get() == Some(&false) {
22        bail!("plugin could not be built last, skipping to prevent reload loop");
23    }
24
25    let config = PluginConfig {
26        xkb_key_switch_mod: switch.map(|s| Box::from(mod_to_xkb_key(s))),
27        xkb_key_overview_mod: overview
28            .as_ref()
29            .map(|(r#mod, _)| Box::from(r#mod.to_string())),
30        xkb_key_overview_key: overview.map(|(_, key)| key),
31    };
32
33    if check_new_plugin_needed(&config) {
34        unload().context("unable to unload old plugin")?;
35        hyprland_plugin::generate(&config).context("unable to generate plugin")?;
36        trace!(
37            "generated plugin at {:?}",
38            hyprland_plugin::PLUGIN_OUTPUT_PATH
39        );
40        if let Err(err) = plugin::load(Path::new(hyprland_plugin::PLUGIN_OUTPUT_PATH)) {
41            PLUGIN_COULD_BE_BUILD.get_or_init(|| false);
42            trace!("plugin failed to load, disabling plugin");
43            bail!("unable to load plugin: {err:?}")
44        }
45        trace!("loaded plugin");
46    } else {
47        debug!("plugin already loaded, skipping");
48    }
49
50    Ok(())
51}
52
53pub fn check_new_plugin_needed(config: &PluginConfig) -> bool {
54    let plugins = plugin::list().unwrap_or_default();
55    trace!("plugins: {plugins:?}");
56    for plugin in plugins {
57        if plugin.name == hyprland_plugin::PLUGIN_NAME {
58            let Some(desc) = plugin.description.split(" - ").last() else {
59                continue;
60            };
61            if desc == config.to_string() {
62                // config didn't change, no need to reload
63                return false;
64            }
65        }
66    }
67    true
68}
69
70pub fn unload() -> anyhow::Result<()> {
71    let plugins = plugin::list().unwrap_or_default();
72    for plugin in plugins {
73        if plugin.name == hyprland_plugin::PLUGIN_NAME {
74            debug!("plugin loaded, unloading it");
75            plugin::unload(Path::new(hyprland_plugin::PLUGIN_OUTPUT_PATH)).with_context(|| {
76                format!(
77                    "unable to unload old plugin at: {}",
78                    hyprland_plugin::PLUGIN_OUTPUT_PATH
79                )
80            })?;
81            debug!("plugin unloaded");
82        }
83    }
84    Ok(())
85}
86
87#[allow(clippy::must_use_candidate)]
88pub const fn mod_to_xkb_key(r#mod: Modifier) -> &'static str {
89    match r#mod {
90        Modifier::Alt => "XKB_KEY_Alt",
91        Modifier::Ctrl => "XKB_KEY_Control",
92        Modifier::Super => "XKB_KEY_Super",
93    }
94}