waywarp 0.1.3

A high-performance, keyboard-driven mouse control tool for Wayland compositors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#![allow(dead_code)]

use std::fs::{self, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use tracing::{error, info, warn};

/// Default modern waywarp configuration path under ~/.config
const CONFIG_SUBDIR: &str = "waywarp";
const CONFIG_FILE_NAME: &str = "config";

#[derive(Debug, Clone)]
pub struct Config {
    pub hint_bg: [f64; 4],       // RGBA, values from 0.0 to 1.0
    pub hint_fg: [f64; 4],       // RGBA, values from 0.0 to 1.0
    pub hint_font: String,       // Font family name
    pub hint_size: u32,          // Font size
    pub hint_border_radius: f64, // Corner radius
    pub hint_chars: String,      // Key character set
    pub refinement_passes: u32,  // Multi-pass refinement layers
    pub exit_on_select: bool,    // Close automatically after warp
    pub on_select_cmd: String,   // Command callback post-warp
    pub on_exit_cmd: String,     // Command callback on exit

    // Keybindings for Normal Mode
    pub key_left: String,
    pub key_right: String,
    pub key_up: String,
    pub key_down: String,
    pub key_shift: String,
    pub key_ctrl: String,
    pub key_click_left: String,
    pub key_click_right: String,
    pub key_click_middle: String,
    pub key_scroll_up: String,
    pub key_scroll_down: String,
    pub key_exit: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            hint_bg: [1.0, 0.33, 0.33, 0.38], // #ff555560
            hint_fg: [1.0, 1.0, 1.0, 1.0],    // #ffffffff
            hint_font: "monospace".to_string(),
            hint_size: 18,
            hint_border_radius: 25.0,
            hint_chars: "asdfghjklqwertzxv".to_string(),
            refinement_passes: 2,
            exit_on_select: true,
            on_select_cmd: "hyprctl dispatch movecursor {global_x} {global_y}".to_string(),
            on_exit_cmd: "".to_string(),

            key_left: "h,Left".to_string(),
            key_right: "l,Right".to_string(),
            key_up: "k,Up".to_string(),
            key_down: "j,Down".to_string(),
            key_shift: "Shift_L,Shift_R".to_string(),
            key_ctrl: "Control_L,Control_R".to_string(),
            key_click_left: "f,Return".to_string(),
            key_click_right: "d".to_string(),
            key_click_middle: "s".to_string(),
            key_scroll_up: "u".to_string(),
            key_scroll_down: "e".to_string(),
            key_exit: "Escape,q,Q".to_string(),
        }
    }
}

impl Config {
    /// Retrieve default config file destination (XDG_CONFIG_HOME or ~/.config)
    pub fn get_config_path() -> PathBuf {
        let base_dir = std::env::var("XDG_CONFIG_HOME")
            .map(PathBuf::from)
            .unwrap_or_else(|_| {
                let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
                PathBuf::from(home).join(".config")
            });
        base_dir.join(CONFIG_SUBDIR).join(CONFIG_FILE_NAME)
    }

    /// Load config from file. Creates default if file is missing.
    pub fn load() -> Self {
        let path = Self::get_config_path();
        if !path.exists() {
            // Try to create the default config file
            if let Err(write_err) = Self::write_default_config(&path) {
                warn!(
                    "Could not write default config to {:?}: {}",
                    path, write_err
                );
            } else {
                info!(
                    "Successfully created default configuration file at {:?}",
                    path
                );
            }
            return Config::default();
        }

        match Self::load_from_path(&path) {
            Ok(cfg) => cfg,
            Err(e) => {
                warn!(
                    "Failed to load configuration from {:?}: {}. Falling back to defaults.",
                    path, e
                );
                Config::default()
            }
        }
    }

    /// Explicitly load from a specific file path (for custom tests / CLI overrides)
    pub fn load_from_path(path: &Path) -> anyhow::Result<Self> {
        if !path.exists() {
            return Err(anyhow::anyhow!("Config file does not exist"));
        }

        let content = fs::read_to_string(path)?;
        Self::parse_ini(&content)
    }

    /// Write the standard default configuration file to disk
    fn write_default_config(path: &Path) -> io::Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }

        let mut file = File::create(path)?;
        writeln!(file, "# waywarp config")?;
        writeln!(file, "hint_bg=#ff555560")?;
        writeln!(file, "hint_fg=#ffffffff")?;
        writeln!(file, "hint_font=monospace")?;
        writeln!(file, "hint_size=18")?;
        writeln!(file, "hint_border_radius=25.0")?;
        writeln!(file, "hint_chars=asdfghjklqwertzxv")?;
        writeln!(file, "refinement_passes=2")?;
        writeln!(file, "exit_on_select=true")?;
        writeln!(
            file,
            "on_select_cmd=hyprctl dispatch movecursor {{global_x}} {{global_y}}"
        )?;
        writeln!(file, "on_exit_cmd=")?;
        writeln!(file, "key_left=h,Left")?;
        writeln!(file, "key_right=l,Right")?;
        writeln!(file, "key_up=k,Up")?;
        writeln!(file, "key_down=j,Down")?;
        writeln!(file, "key_shift=Shift_L,Shift_R")?;
        writeln!(file, "key_ctrl=Control_L,Control_R")?;
        writeln!(file, "key_click_left=f,Return")?;
        writeln!(file, "key_click_right=d")?;
        writeln!(file, "key_click_middle=s")?;
        writeln!(file, "key_scroll_up=u")?;
        writeln!(file, "key_scroll_down=e")?;
        writeln!(file, "key_exit=Escape,q,Q")?;
        Ok(())
    }

    /// Parse flat INI string line-by-line
    fn parse_ini(content: &str) -> anyhow::Result<Self> {
        let mut config = Config::default();

        for (line_num, raw_line) in content.lines().enumerate() {
            let line = raw_line.trim();
            if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
                continue;
            }

            let mut parts = line.splitn(2, '=');
            let key = parts.next().unwrap_or("").trim();
            let val = parts.next().unwrap_or("").trim();

            if key.is_empty() {
                continue;
            }

            // Support both modern naming and hyprwarp backward compatibility
            match key {
                "hint_bg" | "hint_bgcolor" => {
                    if let Some(rgba) = parse_hex_color(val) {
                        config.hint_bg = rgba;
                    } else {
                        warn!(
                            "Line {}: invalid color format for {}, using default",
                            line_num + 1,
                            key
                        );
                    }
                }
                "hint_fg" | "hint_fgcolor" => {
                    if let Some(rgba) = parse_hex_color(val) {
                        config.hint_fg = rgba;
                    } else {
                        warn!(
                            "Line {}: invalid color format for {}, using default",
                            line_num + 1,
                            key
                        );
                    }
                }
                "hint_font" => {
                    config.hint_font = val.to_string();
                }
                "hint_size" => {
                    if let Ok(size) = val.parse::<u32>() {
                        config.hint_size = size.clamp(8, 64);
                    }
                }
                "hint_border_radius" | "hint_radius" => {
                    if let Ok(radius) = val.parse::<f64>() {
                        config.hint_border_radius = radius.clamp(0.0, 100.0);
                    }
                }
                "hint_chars" => {
                    if !val.is_empty() {
                        config.hint_chars = val.to_string();
                    }
                }
                "refinement_passes" => {
                    if let Ok(passes) = val.parse::<u32>() {
                        config.refinement_passes = passes.clamp(1, 4);
                    }
                }
                "exit_on_select" => {
                    if let Ok(b) = val.parse::<bool>() {
                        config.exit_on_select = b;
                    }
                }
                "on_select_cmd" => {
                    config.on_select_cmd = val.to_string();
                }
                "on_exit_cmd" => {
                    config.on_exit_cmd = val.to_string();
                }
                "key_left" => {
                    config.key_left = val.to_string();
                }
                "key_right" => {
                    config.key_right = val.to_string();
                }
                "key_up" => {
                    config.key_up = val.to_string();
                }
                "key_down" => {
                    config.key_down = val.to_string();
                }
                "key_shift" => {
                    config.key_shift = val.to_string();
                }
                "key_ctrl" => {
                    config.key_ctrl = val.to_string();
                }
                "key_click_left" => {
                    config.key_click_left = val.to_string();
                }
                "key_click_right" => {
                    config.key_click_right = val.to_string();
                }
                "key_click_middle" => {
                    config.key_click_middle = val.to_string();
                }
                "key_scroll_up" => {
                    config.key_scroll_up = val.to_string();
                }
                "key_scroll_down" => {
                    config.key_scroll_down = val.to_string();
                }
                "key_exit" => {
                    config.key_exit = val.to_string();
                }
                _ => {
                    warn!("Line {}: unknown configuration key '{}'", line_num + 1, key);
                }
            }
        }

        Ok(config)
    }

    /// Spawns the specified shell command, replacing coordinate/scale placeholders
    pub fn execute_callback(
        cmd_template: &str,
        x: i32,
        y: i32,
        screen_w: i32,
        screen_h: i32,
    ) -> anyhow::Result<()> {
        if cmd_template.trim().is_empty() {
            return Ok(());
        }

        let scale_x = x as f64 / screen_w as f64;
        let scale_y = y as f64 / screen_h as f64;

        let formatted = cmd_template
            .replace("{x}", &x.to_string())
            .replace("{y}", &y.to_string())
            .replace("{global_x}", &x.to_string())
            .replace("{global_y}", &y.to_string())
            .replace("{screen_w}", &screen_w.to_string())
            .replace("{screen_h}", &screen_h.to_string())
            .replace("{scale_x}", &format!("{:.4}", scale_x))
            .replace("{scale_y}", &format!("{:.4}", scale_y));

        info!("Spawning action callback command: {:?}", formatted);

        std::process::Command::new("sh")
            .arg("-c")
            .arg(&formatted)
            .spawn()
            .map_err(|e| {
                error!("Failed to spawn shell callback command: {:?}", e);
                e
            })?;

        Ok(())
    }
}

/// Helper function to parse hex color (#RRGGBBAA, #RRGGBB, RRGGBBAA, RRGGBB) to [f64; 4]
fn parse_hex_color(hex: &str) -> Option<[f64; 4]> {
    let clean = hex.trim().trim_start_matches('#');
    let len = clean.len();
    if len == 6 {
        let r = u8::from_str_radix(&clean[0..2], 16).ok()? as f64 / 255.0;
        let g = u8::from_str_radix(&clean[2..4], 16).ok()? as f64 / 255.0;
        let b = u8::from_str_radix(&clean[4..6], 16).ok()? as f64 / 255.0;
        Some([r, g, b, 1.0])
    } else if len == 8 {
        let r = u8::from_str_radix(&clean[0..2], 16).ok()? as f64 / 255.0;
        let g = u8::from_str_radix(&clean[2..4], 16).ok()? as f64 / 255.0;
        let b = u8::from_str_radix(&clean[4..6], 16).ok()? as f64 / 255.0;
        let a = u8::from_str_radix(&clean[6..8], 16).ok()? as f64 / 255.0;
        Some([r, g, b, a])
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_hex_color() {
        assert_eq!(parse_hex_color("#ffffff"), Some([1.0, 1.0, 1.0, 1.0]));
        assert_eq!(parse_hex_color("ffffff"), Some([1.0, 1.0, 1.0, 1.0]));

        let red_translucent = parse_hex_color("#ff000080").unwrap();
        assert!((red_translucent[0] - 1.0).abs() < 1e-5);
        assert!((red_translucent[1] - 0.0).abs() < 1e-5);
        assert!((red_translucent[2] - 0.0).abs() < 1e-5);
        assert!((red_translucent[3] - 128.0 / 255.0).abs() < 1e-5);

        assert_eq!(parse_hex_color("invalid"), None);
    }

    #[test]
    fn test_parse_ini_basic() {
        let ini = r#"
            # comment
            hint_bg = #11223344
            hint_fg = #556677
            hint_font = JetBrainsMono
            hint_size = 20
            hint_border_radius = 5.0
            hint_chars = abc
            refinement_passes = 3
            exit_on_select = false
            on_select_cmd = echo selected
            on_exit_cmd = echo exited
        "#;

        let cfg = Config::parse_ini(ini).unwrap();
        assert!((cfg.hint_bg[0] - 17.0 / 255.0).abs() < 1e-5);
        assert!((cfg.hint_bg[3] - 68.0 / 255.0).abs() < 1e-5);
        assert_eq!(cfg.hint_font, "JetBrainsMono");
        assert_eq!(cfg.hint_size, 20);
        assert_eq!(cfg.hint_border_radius, 5.0);
        assert_eq!(cfg.hint_chars, "abc");
        assert_eq!(cfg.refinement_passes, 3);
        assert!(!cfg.exit_on_select);
        assert_eq!(cfg.on_select_cmd, "echo selected");
        assert_eq!(cfg.on_exit_cmd, "echo exited");
    }

    #[test]
    fn test_parse_ini_compatibility() {
        let ini = r#"
            hint_bgcolor = #ff0000
            hint_fgcolor = #00ff00
            hint_radius = 12
        "#;

        let cfg = Config::parse_ini(ini).unwrap();
        assert!((cfg.hint_bg[0] - 1.0).abs() < 1e-5);
        assert!((cfg.hint_fg[1] - 1.0).abs() < 1e-5);
        assert_eq!(cfg.hint_border_radius, 12.0);
    }

    #[test]
    fn test_parse_ini_clamps() {
        let ini = r#"
            hint_size = 100
            hint_border_radius = 200
            refinement_passes = 10
        "#;

        let cfg = Config::parse_ini(ini).unwrap();
        assert_eq!(cfg.hint_size, 64); // Clamped to 64
        assert_eq!(cfg.hint_border_radius, 100.0); // Clamped to 100.0
        assert_eq!(cfg.refinement_passes, 4); // Clamped to 4
    }
}