Skip to main content

hyprshell_hyprland_plugin/
configure.rs

1use crate::{PLUGIN_AUTHOR, PLUGIN_DESC, PLUGIN_NAME, PLUGIN_VERSION};
2use anyhow::Context;
3use core_lib::binds::generate_transfer;
4use core_lib::transfer::{OpenSwitch, TransferType};
5use core_lib::util::get_daemon_socket_path_buff;
6use std::fmt::Display;
7use std::fs::OpenOptions;
8use std::io::{Read, Write};
9use tempfile::TempDir;
10use tracing::debug_span;
11
12pub struct PluginConfig {
13    pub xkb_key_switch_mod: Option<Box<str>>,
14    pub xkb_key_switch_key: Option<Box<str>>,
15    pub xkb_key_overview_mod: Option<Box<str>>,
16    pub xkb_key_overview_key: Option<Box<str>>,
17}
18impl Display for PluginConfig {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        write!(
21            f,
22            "{}|{}|{}|{}",
23            self.xkb_key_switch_mod.as_deref().unwrap_or(""),
24            self.xkb_key_switch_key.as_deref().unwrap_or(""),
25            self.xkb_key_overview_mod.as_deref().unwrap_or(""),
26            self.xkb_key_overview_key.as_deref().unwrap_or(""),
27        )
28    }
29}
30
31pub fn configure(
32    dir: &TempDir,
33    config: &PluginConfig,
34    version: &semver::Version,
35) -> anyhow::Result<()> {
36    let _span = debug_span!("configure", path =? dir.path()).entered();
37    let defs = dir.path().join("defs.h");
38
39    let mut defs_file = OpenOptions::new()
40        .read(true)
41        .open(&defs)
42        .with_context(|| format!("unable to open defs file: {}", defs.display()))?;
43    let mut buffer = String::new();
44    defs_file
45        .read_to_string(&mut buffer)
46        .context("unable to read defs file")?;
47    let path = get_daemon_socket_path_buff()
48        .to_str()
49        .map(str::to_string)
50        .context("unable to get daemon socket path")?;
51    for replace in [
52        ("#include \"defs-test.h\"", ""),
53        ("$HYPRSHELL_PLUGIN_NAME$", PLUGIN_NAME),
54        ("$HYPRSHELL_PLUGIN_AUTHOR$", PLUGIN_AUTHOR),
55        (
56            "$HYPRSHELL_PLUGIN_DESC$",
57            &format!("{PLUGIN_DESC} - {config}"),
58        ),
59        (
60            "$HYPRSHELL_PLUGIN_VERSION$",
61            &format!("{PLUGIN_VERSION}-{version}"),
62        ),
63        (
64            "$HYPRSHELL_PRINT_DEBUG$",
65            if cfg!(debug_assertions) { "1" } else { "0" },
66        ),
67        ("$HYPRSHELL_SOCKET_PATH$", &path),
68        (
69            "$HYPRSHELL_SWTICH_XKB_MOD_L$",
70            &config
71                .xkb_key_switch_mod
72                .as_deref()
73                .map_or_else(|| "-1".to_string(), |m| format!("{m}_L")),
74        ),
75        (
76            "$HYPRSHELL_SWTICH_XKB_MOD_R$",
77            &config
78                .xkb_key_switch_mod
79                .as_deref()
80                .map_or_else(|| "-1".to_string(), |m| format!("{m}_R")),
81        ),
82        (
83            "$HYPRSHELL_OVERVIEW_MOD$",
84            config.xkb_key_overview_mod.as_deref().unwrap_or(""),
85        ),
86        (
87            "$HYPRSHELL_OVERVIEW_KEY$",
88            config.xkb_key_overview_key.as_deref().unwrap_or(""),
89        ),
90        (
91            "$HYPRSHELL_SWITCH_KEY$",
92            config.xkb_key_switch_key.as_deref().unwrap_or(""),
93        ),
94        (
95            "$HYPRSHELL_OPEN_OVERVIEW$",
96            &generate_transfer(&TransferType::OpenOverview),
97        ),
98        (
99            "$HYPRSHELL_CLOSE$",
100            &generate_transfer(&TransferType::CloseSwitch),
101        ),
102        (
103            "$HYPRSHELL_OPEN_SWITCH$",
104            &generate_transfer(&TransferType::OpenSwitch(OpenSwitch { reverse: false })),
105        ),
106        (
107            "$HYPRSHELL_OPEN_SWITCH_REVERSE$",
108            &generate_transfer(&TransferType::OpenSwitch(OpenSwitch { reverse: true })),
109        ),
110    ] {
111        buffer = buffer.replace(replace.0, replace.1);
112    }
113    buffer.push('\n');
114    drop(defs_file);
115    let mut defs_file = OpenOptions::new()
116        .write(true)
117        .truncate(true)
118        .open(&defs)
119        .with_context(|| format!("unable to open defs file: {}", defs.display()))?;
120    defs_file
121        .write_all(buffer.as_bytes())
122        .context("unable to write defs file")?;
123    // tracing::trace!("Updated defs file: {defs:?}, content:\n{buffer}");
124    Ok(())
125}