paneru 0.3.4

A sliding, tiling window manager for MacOS.
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
use arc_swap::{ArcSwap, Guard};
use bevy::ecs::resource::Resource;
use objc2_core_foundation::{CFData, CFString};
use regex::Regex;
use serde::{Deserialize, Deserializer, de};
use std::{
    collections::HashMap,
    env,
    ffi::c_void,
    fs::read_to_string,
    path::{Path, PathBuf},
    ptr::NonNull,
    sync::{Arc, LazyLock},
};
use stdext::function_name;
use tracing::{error, info, warn};

use crate::{
    commands::{Command, Direction, MouseMove, Operation},
    platform::{Modifiers, OSStatus},
};
use crate::{
    errors::{Error, Result},
    util::MacResult,
};
use crate::{platform::CFStringRef, util::AXUIWrapper};

/// A `LazyLock` that determines the path to the application's configuration file.
/// It checks the `PANERU_CONFIG` environment variable first, then standard XDG locations and user home directory.
/// If no configuration file is found, the application will panic.
pub static CONFIGURATION_FILE: LazyLock<PathBuf> = LazyLock::new(|| {
    if let Ok(path_str) = env::var("PANERU_CONFIG") {
        let path = PathBuf::from(path_str);
        if path.exists() {
            return path;
        }
        warn!(
            "{}: $PANERU_CONFIG is set to {}, but the file does not exist. Falling back to default locations.",
            function_name!(),
            path.display()
        );
    }

    let standard_paths = [
        env::var("HOME")
            .ok()
            .map(|h| PathBuf::from(h).join(".paneru")),
        env::var("HOME")
            .ok()
            .map(|h| PathBuf::from(h).join(".paneru.toml")),
        env::var("XDG_CONFIG_HOME")
            .ok()
            .map(|x| PathBuf::from(x).join("paneru/paneru.toml")),
    ];

    standard_paths
        .into_iter()
        .flatten()
        .find(|path| path.exists())
        .unwrap_or_else(|| {
            panic!(
                "{}: Configuration file not found. Tried: $PANERU_CONFIG, $HOME/.paneru, $HOME/.paneru.toml, $XDG_CONFIG_HOME/paneru/paneru.toml",
                function_name!()
            )
        })
});

/// Parses a string into a `Direction` enum.
///
/// # Arguments
///
/// * `dir` - The string representation of the direction (e.g., "north", "west").
///
/// # Returns
///
/// `Ok(Direction)` if the string is a valid direction, otherwise `Err(Error::InvalidConfig)`.
fn parse_direction(dir: &str) -> Result<Direction> {
    Ok(match dir {
        "north" => Direction::North,
        "south" => Direction::South,
        "west" => Direction::West,
        "east" => Direction::East,
        "first" => Direction::First,
        "last" => Direction::Last,
        _ => {
            return Err(Error::InvalidConfig(format!(
                "{}: Unhandled direction {dir}",
                function_name!()
            )));
        }
    })
}

/// Parses a command argument vector into an `Operation` enum.
///
/// # Arguments
///
/// * `argv` - A slice of strings representing the command arguments (e.g., `["focus", "east"]`).
///
/// # Returns
///
/// `Ok(Operation)` if the arguments represent a valid operation, otherwise `Err(Error::InvalidConfig)`.
fn parse_operation(argv: &[&str]) -> Result<Operation> {
    let empty = "";
    let cmd = *argv.first().unwrap_or(&empty);
    let err = Error::InvalidConfig(format!("{}: Invalid command '{argv:?}'", function_name!()));

    let out = match cmd {
        "focus" => Operation::Focus(parse_direction(argv.get(1).ok_or(err)?)?),
        "swap" => Operation::Swap(parse_direction(argv.get(1).ok_or(err)?)?),
        "center" => Operation::Center,
        "resize" => Operation::Resize,
        "fullwidth" => Operation::FullWidth,
        "manage" => Operation::Manage,
        "equalize" => Operation::Equalize,
        "stack" => Operation::Stack(true),
        "unstack" => Operation::Stack(false),
        "nextdisplay" => Operation::ToNextDisplay,
        _ => {
            return Err(err);
        }
    };
    Ok(out)
}

/// Parses a command argument vector into a `MouseMove` enum.
fn parse_mouse_move(argv: &[&str]) -> Result<MouseMove> {
    let empty = "";
    let cmd = *argv.first().unwrap_or(&empty);
    let err = Error::InvalidConfig(format!(
        "{}: Invalid mouse command '{argv:?}'",
        function_name!()
    ));

    let out = match cmd {
        "nextdisplay" => MouseMove::ToNextDisplay,
        _ => {
            return Err(err);
        }
    };
    Ok(out)
}

/// Parses a command argument vector into a `Command` enum.
///
/// # Arguments
///
/// * `argv` - A slice of strings representing the command arguments (e.g., `["window", "focus", "east"]`).
///
/// # Returns
///
/// `Ok(Command)` if the arguments represent a valid command, otherwise `Err(Error::InvalidConfig)`.
pub fn parse_command(argv: &[&str]) -> Result<Command> {
    let empty = "";
    let cmd = *argv.first().unwrap_or(&empty);

    let out = match cmd {
        "printstate" => Command::PrintState,
        "window" => Command::Window(parse_operation(&argv[1..])?),
        "mouse" => Command::Mouse(parse_mouse_move(&argv[1..])?),
        "quit" => Command::Quit,
        _ => {
            return Err(Error::InvalidConfig(format!(
                "{}: Unhandled command '{argv:?}'",
                function_name!()
            )));
        }
    };
    Ok(out)
}

/// `Config` manages the application's configuration, including options, keybindings, and window-specific parameters.
/// It provides methods for loading, reloading, and querying configuration settings.
#[derive(Clone, Debug, Resource)]
pub struct Config {
    inner: Arc<ArcSwap<InnerConfig>>,
}

impl Config {
    /// Creates a new `Config` instance by loading the configuration from the specified path.
    ///
    /// # Arguments
    ///
    /// * `path` - A reference to the path of the configuration file.
    ///
    /// # Returns
    ///
    /// `Ok(Self)` if the configuration is loaded successfully, otherwise `Err(Error)` with an error message.
    pub fn new(path: &Path) -> Result<Self> {
        let input = read_to_string(path)?;
        Ok(Config {
            inner: Arc::new(ArcSwap::from_pointee(InnerConfig::new(&input)?)),
        })
    }

    /// Reloads the configuration from the specified path, updating the internal options and keybindings.
    ///
    /// # Arguments
    ///
    /// * `path` - A reference to the path of the new configuration file.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the configuration is reloaded successfully, otherwise `Err(Error)` with an error message.
    pub fn reload_config(&mut self, path: &Path) -> Result<()> {
        let input = read_to_string(path)?;
        let new = InnerConfig::new(&input)?;
        self.inner.store(Arc::new(new));
        Ok(())
    }

    /// Returns a read guard to the inner `InnerConfig` for read-only access.
    ///
    /// # Returns
    ///
    /// A `Guard<Arc<InnerConfig>>` allowing read access to `InnerConfig`.
    fn inner(&self) -> Guard<Arc<InnerConfig>> {
        self.inner.load()
    }

    /// Returns a clone of the `MainOptions` from the current configuration.
    ///
    /// # Returns
    ///
    /// A `MainOptions` struct containing the main configuration options.
    pub fn options(&self) -> MainOptions {
        self.inner().options.clone()
    }

    // Returns the animation speed in 1/10th of screen size per second.
    // E.g. a value of 20 means: move at a speed of two screen sizes per second.
    // If a screen is 1920px, the calculated speed would be 3840 pixels/second.
    pub fn animation_speed(&self) -> f64 {
        self.options()
            .animation_speed
            // If unset, set it to something high, so the move happens immediately,
            // effectively disabling animation.
            .unwrap_or(1_000_000.0)
            .max(5.0)
            / 10.0
    }

    /// Finds a keybinding matching the given `keycode` and `modifier` mask.
    ///
    /// # Arguments
    ///
    /// * `keycode` - The raw key code of the keybinding to find.
    /// * `mask` - The modifier mask (e.g., `Alt`, `Shift`, `Cmd`, `Ctrl`) of the keybinding.
    ///
    /// # Returns
    ///
    /// `Some(Command)` if a matching keybinding is found, otherwise `None`.
    pub fn find_keybind(&self, keycode: u8, mask: &Modifiers) -> Option<Command> {
        let config = self.inner();
        config
            .bindings
            .values()
            .flat_map(|binds| binds.all())
            .find_map(|bind| {
                (bind.code == keycode && bind.modifiers == *mask).then_some(bind.command.clone())
            })
    }

    /// Finds window properties for a given `title` and `bundle_id`.
    /// It iterates through configured window parameters and returns the first match.
    ///
    /// # Arguments
    ///
    /// * `title` - The title of the window to match.
    /// * `bundle_id` - The bundle identifier of the application owning the window.
    ///
    /// # Returns
    ///
    /// `Some(WindowParams)` if matching window properties are found, otherwise `None`.
    pub fn find_window_properties(&self, title: &str, bundle_id: &str) -> Vec<WindowParams> {
        self.inner()
            .windows
            .as_ref()
            .map(|windows| {
                windows
                    .values()
                    .filter(|params| {
                        let bundle_match =
                            params.bundle_id.as_ref().map(|id| id.as_str() == bundle_id);
                        bundle_match.is_none_or(|m| m) && params.title.is_match(title)
                    })
                    .cloned()
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default()
    }

    pub fn sliver_height(&self) -> f64 {
        self.options().sliver_height.unwrap_or(1.0).clamp(0.1, 1.0)
    }

    pub fn sliver_width(&self) -> i32 {
        i32::from(self.options().sliver_width.unwrap_or(5)).max(1)
    }

    pub fn edge_padding(&self) -> (i32, i32, i32, i32) {
        let o = self.options();
        (
            i32::from(o.padding_top.unwrap_or(0)),
            i32::from(o.padding_right.unwrap_or(0)),
            i32::from(o.padding_bottom.unwrap_or(0)),
            i32::from(o.padding_left.unwrap_or(0)),
        )
    }

    pub fn preset_column_widths(&self) -> Vec<f64> {
        self.options().preset_column_widths
    }

    pub fn continuous_swipe(&self) -> bool {
        self.options().continuous_swipe.unwrap_or(true)
    }
}

impl Default for Config {
    /// Returns a default `Config` instance with an empty `InnerConfig`.
    fn default() -> Self {
        Config {
            inner: Arc::new(ArcSwap::from_pointee(InnerConfig::default())),
        }
    }
}

impl TryFrom<&str> for Config {
    type Error = crate::errors::Error;

    fn try_from(input: &str) -> std::result::Result<Self, Self::Error> {
        Ok(Config {
            inner: Arc::new(ArcSwap::from_pointee(InnerConfig::new(input)?)),
        })
    }
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum OneOrMore {
    Single(Keybinding),
    Multiple(Vec<Keybinding>),
}

impl OneOrMore {
    fn all(&self) -> Vec<&Keybinding> {
        match self {
            OneOrMore::Single(one) => vec![one],
            OneOrMore::Multiple(many) => many.iter().collect::<Vec<_>>(),
        }
    }

    fn all_mut(&mut self) -> Vec<&mut Keybinding> {
        match self {
            OneOrMore::Single(one) => vec![one],
            OneOrMore::Multiple(many) => many.iter_mut().collect::<Vec<_>>(),
        }
    }
}

/// `InnerConfig` holds the actual configuration data parsed from a file, including options, keybindings, and window parameters.
/// It is typically accessed via an `Arc<RwLock<InnerConfig>>` within the `Config` struct.
#[derive(Deserialize, Debug, Default)]
struct InnerConfig {
    options: MainOptions,
    bindings: HashMap<String, OneOrMore>,
    windows: Option<HashMap<String, WindowParams>>,
}

impl InnerConfig {
    /// Creates a new `InnerConfig` by reading and parsing the configuration file from the specified `path`.
    ///
    /// # Arguments
    ///
    /// * `path` - A reference to the path of the configuration file.
    ///
    /// # Returns
    ///
    /// `Ok(InnerConfig)` if the configuration is parsed successfully, otherwise `Err(Error)` with an error message.
    fn new(input: &str) -> Result<InnerConfig> {
        InnerConfig::parse_config(input)
    }

    /// Parses the configuration from a string `input`.
    /// It populates the `code` and `command` fields of `Keybinding` by looking up virtual keys and literal keycodes.
    ///
    /// # Arguments
    ///
    /// * `input` - The string content of the configuration file.
    ///
    /// # Returns
    ///
    /// `Ok(InnerConfig)` if the parsing is successful, otherwise `Err(Error)` with an error message.
    fn parse_config(input: &str) -> Result<InnerConfig> {
        let virtual_keys = generate_virtual_keymap();
        let mut config: InnerConfig = toml::from_str(input)?;

        for (command, bindings) in &mut config.bindings {
            let argv = command.split('_').collect::<Vec<_>>();
            for binding in bindings.all_mut() {
                binding.command = parse_command(&argv)?;

                let code = virtual_keys
                    .iter()
                    .find(|(key, _)| key == &binding.key)
                    .map(|(_, code)| *code)
                    .or_else(|| {
                        literal_keycode()
                            .find(|(key, _)| key == &binding.key)
                            .map(|(_, code)| *code)
                    });
                if let Some(code) = code {
                    binding.code = code;
                } else {
                    error!("{}: invalid key '{}'", function_name!(), &binding.key);
                }
                info!("bind: {binding:?}");
            }
        }
        Ok(config)
    }
}

/// `MainOptions` represents the primary configuration options for the window manager.
/// These options control various behaviors such as mouse focus, gesture recognition, and window animation.
#[derive(Deserialize, Clone, Debug, Default)]
pub struct MainOptions {
    /// Enables or disables focus follows mouse behavior.
    pub focus_follows_mouse: Option<bool>,
    /// Enables or disables mouse follows focus behavior.
    pub mouse_follows_focus: Option<bool>,
    /// The number of fingers required for swipe gestures to move windows.
    pub swipe_gesture_fingers: Option<usize>,
    /// A list of preset column widths (as ratios) used for resizing windows.
    #[serde(default = "default_preset_column_widths")]
    pub preset_column_widths: Vec<f64>,
    /// The animation speed for window movements in pixels per second.
    pub animation_speed: Option<f64>,
    /// Automatically center the window when switching focus with keyboard.
    pub auto_center: Option<bool>,
    /// Height of off-screen window slivers as a ratio (0.0–1.0) of the display height.
    /// Lower values hide the window's corner radius at screen edges.
    /// Default: 1.0 (full height).
    pub sliver_height: Option<f64>,
    /// Width of off-screen window slivers in pixels.
    /// Default: 5 pixels.
    pub sliver_width: Option<u16>,
    /// Padding applied at screen edges (in pixels). Independent from between-window gaps.
    /// Default: 0 on all sides.
    pub padding_top: Option<u16>,
    pub padding_bottom: Option<u16>,
    pub padding_left: Option<u16>,
    pub padding_right: Option<u16>,

    /// Swiping keeps sliding windows until the first or last window.
    /// Set to false to clamp so edge windows stay on-screen. Default: true.
    pub continuous_swipe: Option<bool>,
}

/// Returns a default set of column widths.
pub fn default_preset_column_widths() -> Vec<f64> {
    vec![0.25, 0.33333, 0.50, 0.66667, 0.75]
}

/// `Keybinding` represents a keyboard shortcut and the command it triggers.
/// It includes the key, its raw keycode, modifier keys, and the associated command.
#[derive(Debug)]
pub struct Keybinding {
    pub key: String,
    pub code: u8,
    pub modifiers: Modifiers,
    pub command: Command,
}

impl<'de> Deserialize<'de> for Keybinding {
    /// Deserializes a `Keybinding` from a string input. The input string is expected to be in a format like "`modifier+modifier-key`" or "`key`".
    /// Examples: "`ctrl+alt-q`", "`shift-tab`", "`h`".
    ///
    /// # Arguments
    ///
    /// * `deserializer` - The deserializer used to parse the input.
    ///
    /// # Returns
    ///
    /// `Ok(Self)` if the deserialization is successful, otherwise `Err(D::Error)` with a custom error message.
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let input = String::deserialize(deserializer)?;
        let mut parts = input.split('-').map(str::trim).collect::<Vec<_>>();
        let key = parts.pop();

        if parts.len() > 1 || key.is_none() {
            return Err(de::Error::custom(format!("Too many dashes: {input:?}")));
        }

        let modifiers = match parts.pop() {
            Some(modifiers) => parse_modifiers(modifiers).map_err(de::Error::custom)?,
            None => Modifiers::empty(),
        };

        Ok(Keybinding {
            key: key.unwrap().to_string(),
            code: 0,
            modifiers,
            command: Command::Quit,
        })
    }
}

/// `WindowParams` defines rules and properties for specific windows based on their title or bundle ID.
/// These parameters can override default window management behavior, such as forcing a window to float or setting its initial index.
#[derive(Clone, Debug, Deserialize)]
pub struct WindowParams {
    /// A regular expression to match against the window's title.
    #[serde(deserialize_with = "deserialize_title")]
    title: Regex,
    /// An optional bundle identifier to match against the application's bundle ID.
    bundle_id: Option<String>,
    /// If `true`, the window will be managed as a floating window (not tiled).
    pub floating: Option<bool>,
    /// An optional preferred index for the window's position in the window strip.
    pub index: Option<usize>,
    pub vertical_padding: Option<i32>,
    pub horizontal_padding: Option<i32>,
    pub dont_focus: Option<bool>,
    /// An optional initial width ratio (0.0–1.0) relative to the display width.
    /// Overrides the default column width when the window is first managed.
    pub width: Option<f64>,
    /// Grid placement for floating windows: "cols:rows:x:y:w:h".
    /// Divides the display into a grid and positions the window at the given cell/span.
    pub grid: Option<String>,
}

impl WindowParams {
    /// Parses the grid string into `(x_ratio, y_ratio, w_ratio, h_ratio)`, all 0.0–1.0.
    pub fn grid_ratios(&self) -> Option<(f64, f64, f64, f64)> {
        let grid = self.grid.as_ref()?;
        let parts: Vec<f64> = grid.split(':').filter_map(|s| s.parse().ok()).collect();
        if parts.len() != 6 {
            return None;
        }
        let (cols, rows) = (parts[0], parts[1]);
        if cols <= 0.0 || rows <= 0.0 {
            return None;
        }
        Some((
            parts[2] / cols,
            parts[3] / rows,
            parts[4] / cols,
            parts[5] / rows,
        ))
    }
}

/// Deserializes a regular expression from a string for window titles.
fn deserialize_title<'de, D>(deserializer: D) -> std::result::Result<Regex, D::Error>
where
    D: Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    Regex::new(&s).map_err(de::Error::custom)
}

/// Parses a string containing modifier names (e.g., "alt", "shift", "cmd", "ctrl") separated by "+", and returns their combined bitmask.
///
/// # Arguments
///
/// * `input` - The string containing modifier names (e.g., "ctrl+alt").
///
/// # Returns
///
/// `Ok(Modifiers)` with the combined modifier bitmask if parsing is successful, otherwise `Err(String)` with an error message for an invalid modifier.
fn parse_modifiers(input: &str) -> Result<Modifiers> {
    let mut out = Modifiers::empty();

    let modifiers = input.split('+').map(str::trim).collect::<Vec<_>>();
    for modifier in &modifiers {
        out |= match *modifier {
            "alt" => Modifiers::ALT,
            "shift" => Modifiers::SHIFT,
            "cmd" => Modifiers::CMD,
            "ctrl" => Modifiers::CTRL,
            _ => {
                return Err(Error::InvalidConfig(format!(
                    "{}: Invalid modifier: {modifier}",
                    function_name!()
                )));
            }
        }
    }
    Ok(out)
}

#[link(name = "Carbon", kind = "framework")]
unsafe extern "C" {
    /// Returns a reference to the currently selected keyboard layout input source that is ASCII-capable.
    ///
    /// # Returns
    ///
    /// A raw pointer to the `TISInputSourceRef` (a `c_void` pointer) if successful, otherwise `null_mut()`.
    fn TISCopyCurrentASCIICapableKeyboardLayoutInputSource() -> *mut c_void;

    /// Retrieves a specified property of an input source.
    ///
    /// # Arguments
    ///
    /// * `keyboard` - The raw pointer to the `TISInputSourceRef`.
    /// * `property` - The `CFStringRef` representing the property to retrieve (e.g., `kTISPropertyUnicodeKeyLayoutData`).
    ///
    /// # Returns
    ///
    /// A raw pointer to `CFData` containing the property value.
    fn TISGetInputSourceProperty(keyboard: *const c_void, property: CFStringRef) -> *mut CFData;

    /// Translates a virtual key code to a Unicode string according to the specified keyboard layout.
    ///
    /// # Arguments
    ///
    /// * `keyLayoutPtr` - A pointer to the keyboard layout data.
    /// * `virtualKeyCode` - The virtual key code to translate.
    /// * `keyAction` - The key action (e.g., `UCKeyAction::Down`).
    /// * `modifierKeyState` - The state of the modifier keys (e.g., `kUCKeyModifierAlphaLockBit`).
    /// * `keyboardType` - The type of keyboard, typically obtained from `LMGetKbdType()`.
    /// * `keyTranslateOptions` - Options for the translation process.
    /// * `deadKeyState` - A mutable reference to a `u32` representing the dead key state.
    /// * `maxStringLength` - The maximum length of the output Unicode string buffer.
    /// * `actualStringLength` - A mutable reference to an `isize` to store the actual length of the output Unicode string.
    /// * `unicodeString` - A mutable pointer to the buffer to store the resulting Unicode string.
    ///
    /// # Returns
    ///
    /// An `OSStatus` indicating success or failure.
    fn UCKeyTranslate(
        keyLayoutPtr: *mut u8,
        virtualKeyCode: u16,
        keyAction: u16,
        modifierKeyState: u32,
        keyboardType: u32,
        keyTranslateOptions: u32,
        deadKeyState: &mut u32,
        maxStringLength: usize,
        actualStringLength: &mut isize,
        unicodeString: *mut u16,
    ) -> OSStatus;

    /// Returns the keyboard type for the system.
    ///
    /// # Returns
    ///
    /// A `u8` representing the keyboard type.
    fn LMGetKbdType() -> u8;

    /// A constant `CFStringRef` representing the property key for Unicode keyboard layout data.
    static kTISPropertyUnicodeKeyLayoutData: CFStringRef;

}

/// Returns an iterator over static tuples of virtual key names and their corresponding keycodes.
/// These keycodes identify physical keys on an ANSI-standard US keyboard layout.
///
/// # Returns
///
/// An iterator yielding references to `(&'static str, u8)` tuples.
fn virtual_keycode() -> impl Iterator<Item = &'static (&'static str, u8)> {
    /*
     *  Summary:
     *    Virtual keycodes
     *
     *  Discussion:
     *    These constants are the virtual keycodes defined originally in
     *    Inside Mac Volume V, pg. V-191. They identify physical keys on a
     *    keyboard. Those constants with "ANSI" in the name are labeled
     *    according to the key position on an ANSI-standard US keyboard.
     *    For example, kVK_ANSI_A indicates the virtual keycode for the key
     *    with the letter 'A' in the US keyboard layout. Other keyboard
     *    layouts may have the 'A' key label on a different physical key;
     *    in this case, pressing 'A' will generate a different virtual
     *    keycode.
     */
    static VIRTUAL_KEYCODE: LazyLock<Vec<(&'static str, u8)>> = LazyLock::new(|| {
        vec![
            ("a", 0x00),
            ("s", 0x01),
            ("d", 0x02),
            ("f", 0x03),
            ("h", 0x04),
            ("g", 0x05),
            ("z", 0x06),
            ("x", 0x07),
            ("c", 0x08),
            ("v", 0x09),
            ("section", 0x0a), // iso keyboards only.
            ("b", 0x0b),
            ("q", 0x0c),
            ("w", 0x0d),
            ("e", 0x0e),
            ("r", 0x0f),
            ("y", 0x10),
            ("t", 0x11),
            ("1", 0x12),
            ("2", 0x13),
            ("3", 0x14),
            ("4", 0x15),
            ("6", 0x16),
            ("5", 0x17),
            ("equal", 0x18),
            ("9", 0x19),
            ("7", 0x1a),
            ("minus", 0x1b),
            ("8", 0x1c),
            ("0", 0x1d),
            ("rightbracket", 0x1e),
            ("o", 0x1f),
            ("u", 0x20),
            ("leftbracket", 0x21),
            ("i", 0x22),
            ("p", 0x23),
            ("l", 0x25),
            ("j", 0x26),
            ("quote", 0x27),
            ("k", 0x28),
            ("semicolon", 0x29),
            ("backslash", 0x2a),
            ("comma", 0x2b),
            ("slash", 0x2c),
            ("n", 0x2d),
            ("m", 0x2e),
            ("period", 0x2f),
            ("grave", 0x32),
            ("keypaddecimal", 0x41),
            ("keypadmultiply", 0x43),
            ("keypadplus", 0x45),
            ("keypadclear", 0x47),
            ("keypaddivide", 0x4b),
            ("keypadenter", 0x4c),
            ("keypadminus", 0x4e),
            ("keypadequals", 0x51),
            ("keypad0", 0x52),
            ("keypad1", 0x53),
            ("keypad2", 0x54),
            ("keypad3", 0x55),
            ("keypad4", 0x56),
            ("keypad5", 0x57),
            ("keypad6", 0x58),
            ("keypad7", 0x59),
            ("keypad8", 0x5b),
            ("keypad9", 0x5c),
        ]
    });
    VIRTUAL_KEYCODE.iter()
}

/// Returns an iterator over static tuples of literal key names and their corresponding keycodes.
/// These keycodes are for keys that are independent of the keyboard layout (e.g., Return, Tab, Space).
///
/// # Returns
///
/// An iterator yielding references to `(&'static str, u8)` tuples.
fn literal_keycode() -> impl Iterator<Item = &'static (&'static str, u8)> {
    /* keycodes for keys that are independent of keyboard layout*/
    static LITERAL_KEYCODE: LazyLock<Vec<(&'static str, u8)>> = LazyLock::new(|| {
        vec![
            ("return", 0x24),
            ("tab", 0x30),
            ("space", 0x31),
            ("delete", 0x33),
            ("escape", 0x35),
            ("command", 0x37),
            ("shift", 0x38),
            ("capslock", 0x39),
            ("option", 0x3a),
            ("control", 0x3b),
            ("rightcommand", 0x36),
            ("rightshift", 0x3c),
            ("rightoption", 0x3d),
            ("rightcontrol", 0x3e),
            ("function", 0x3f),
            ("f17", 0x40),
            ("volumeup", 0x48),
            ("volumedown", 0x49),
            ("mute", 0x4a),
            ("f18", 0x4f),
            ("f19", 0x50),
            ("f20", 0x5a),
            ("f5", 0x60),
            ("f6", 0x61),
            ("f7", 0x62),
            ("f3", 0x63),
            ("f8", 0x64),
            ("f9", 0x65),
            ("f11", 0x67),
            ("f13", 0x69),
            ("f16", 0x6a),
            ("f14", 0x6b),
            ("f10", 0x6d),
            ("contextualmenu", 0x6e),
            ("f12", 0x6f),
            ("f15", 0x71),
            ("help", 0x72),
            ("home", 0x73),
            ("pageup", 0x74),
            ("forwarddelete", 0x75),
            ("f4", 0x76),
            ("end", 0x77),
            ("f2", 0x78),
            ("pagedown", 0x79),
            ("f1", 0x7a),
            ("leftarrow", 0x7b),
            ("rightarrow", 0x7c),
            ("downarrow", 0x7d),
            ("uparrow", 0x7e),
        ]
    });
    LITERAL_KEYCODE.iter()
}

/// Represents the action of a key, used in `UCKeyTranslate`.
enum UCKeyAction {
    /// The key is going down.
    Down = 0, // key is going down
              /*
              Up = 1,      // key is going up
              AutoKey = 2, // auto-key down
              Display = 3, // get information for key display (as in Key Caps)
              */
}

/// Generates a vector of (`key_name`, keycode) tuples for virtual keys based on the current ASCII-capable keyboard layout.
/// This involves using macOS Carbon API functions to translate virtual keycodes to Unicode characters.
///
/// # Returns
///
/// A `Vec<(String, u8)>` containing the translated key names and their keycodes. Returns an empty vector if an error occurs during keyboard layout fetching.
fn generate_virtual_keymap() -> Vec<(String, u8)> {
    let keyboard = AXUIWrapper::from_retained(unsafe {
        TISCopyCurrentASCIICapableKeyboardLayoutInputSource()
    })
    .ok();
    let keyboard_layout = keyboard
        .and_then(|keyboard| {
            NonNull::new(unsafe {
                TISGetInputSourceProperty(
                    keyboard.as_ptr::<c_void>(),
                    kTISPropertyUnicodeKeyLayoutData,
                )
            })
        })
        .and_then(|uchr| NonNull::new(unsafe { CFData::byte_ptr(uchr.as_ref()).cast_mut() }));
    let Some(keyboard_layout) = keyboard_layout else {
        error!(
            "{}: problem fetching current virtual keyboard layout.",
            function_name!()
        );
        return vec![];
    };

    let mut state = 0u32;
    let mut chars = vec![0u16; 256];
    let mut got: isize = 0;
    virtual_keycode()
        .filter_map(|(_, keycode)| {
            unsafe {
                UCKeyTranslate(
                    keyboard_layout.as_ptr(),
                    (*keycode).into(),
                    UCKeyAction::Down as u16,
                    0,
                    LMGetKbdType().into(),
                    1,
                    &mut state,
                    chars.len(),
                    &mut got,
                    chars.as_mut_ptr(),
                )
            }
            .to_result(function_name!())
            .ok()
            .map(|()| {
                let name = unsafe { CFString::with_characters(None, chars.as_ptr(), got) }
                    .map(|chars| chars.to_string());
                name.zip(Some(*keycode))
            })
        })
        .flatten()
        .collect()
}

#[test]
fn test_config_parsing() {
    let input = r#"
[options]
focus_follows_mouse = true

[bindings]
quit = "ctrl+alt-q"
window_manage = "ctrl+alt-t"
window_stack = ["ctrl-s", "alt-s"]

[windows]

[windows.pip]
title = "picture.*picture"
bundle_id = "com.something.apple"
floating = true
index = 1
"#;
    let config = Config {
        inner: Arc::new(ArcSwap::from_pointee(
            InnerConfig::parse_config(input).expect("Failed to parse config"),
        )),
    };
    let find_key = |k| {
        virtual_keycode()
            .find_map(|(s, v)| (format!("{k}") == *s).then_some(*v))
            .unwrap()
    };

    assert_eq!(config.inner().options.focus_follows_mouse, Some(true));

    // Modifiers: alt = 1<<0, ctrl = 1<<3.
    let keycode = find_key('q');
    assert!(matches!(
        config.find_keybind(keycode, &(Modifiers::ALT | Modifiers::CTRL)),
        Some(Command::Quit)
    ));

    let keycode = find_key('t');
    assert!(matches!(
        config.find_keybind(keycode, &(Modifiers::ALT | Modifiers::CTRL)),
        Some(Command::Window(Operation::Manage))
    ));

    let keycode = find_key('s');
    assert!(matches!(
        config.find_keybind(keycode, &Modifiers::CTRL),
        Some(Command::Window(Operation::Stack(true)))
    ));

    assert!(matches!(
        config.find_keybind(keycode, &Modifiers::ALT),
        Some(Command::Window(Operation::Stack(true)))
    ));

    let props = config.find_window_properties("picture in picture", "com.something.apple");
    assert_eq!(props[0].floating, Some(true));
    assert_eq!(props[0].index, Some(1));
}

#[test]
#[allow(clippy::float_cmp)]
fn test_grid_ratios() {
    use regex::Regex;

    let make = |grid: Option<&str>| WindowParams {
        title: Regex::new(".*").unwrap(),
        bundle_id: None,
        floating: None,
        index: None,
        vertical_padding: None,
        horizontal_padding: None,
        dont_focus: None,
        width: None,
        grid: grid.map(Into::into),
    };

    // Standard 2x2 grid, cell (1,1), span 1x1 → bottom-right quarter.
    assert_eq!(
        make(Some("2:2:1:1:1:1")).grid_ratios(),
        Some((0.5, 0.5, 0.5, 0.5))
    );

    // 3x3 grid, cell (0,0), span 2x1 → top-left, 2/3 width, 1/3 height.
    assert_eq!(
        make(Some("3:3:0:0:2:1")).grid_ratios(),
        Some((0.0, 0.0, 2.0 / 3.0, 1.0 / 3.0))
    );

    // Full screen: 1x1 grid, cell (0,0), span 1x1.
    assert_eq!(
        make(Some("1:1:0:0:1:1")).grid_ratios(),
        Some((0.0, 0.0, 1.0, 1.0))
    );

    // Invalid: too few parts.
    assert_eq!(make(Some("2:2:1:1")).grid_ratios(), None);

    // Invalid: zero columns.
    assert_eq!(make(Some("0:2:0:0:1:1")).grid_ratios(), None);

    // No grid set.
    assert_eq!(make(None).grid_ratios(), None);
}