paneru 0.3.2

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
996
997
998
999
1000
1001
1002
use accessibility_sys::{AXIsProcessTrustedWithOptions, kAXTrustedCheckOptionPrompt};
use bevy::ecs::entity::Entity;
use bevy::ecs::query::Has;
use bevy::ecs::resource::Resource;
use bevy::ecs::system::Query;
use core::ptr::NonNull;
use log::{debug, error, trace, warn};
use notify::{RecursiveMode, Watcher};
use objc2_core_foundation::{
    CFArray, CFDictionary, CFEqual, CFMutableData, CFNumber, CFNumberType, CFRetained, CFString,
    CGPoint, CGRect, kCFBooleanTrue,
};
use objc2_core_graphics::{
    CGDirectDisplayID, CGDisplayBounds, CGError, CGGetActiveDisplayList, CGRectContainsPoint,
    CGWarpMouseCursorPosition,
};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::ptr::null_mut;
use std::slice::from_raw_parts_mut;
use std::time::Duration;
use stdext::function_name;

use crate::ecs::Unmanaged;
use crate::errors::{Error, Result};
use crate::events::{Event, EventSender};
use crate::manager::skylight::SLSGetSpaceManagementMode;
use crate::platform::{ConnID, ProcessSerialNumber, WinID};
use crate::util::{AXUIWrapper, create_array, get_array_values, get_cfdict_value, symlink_target};
use app::ApplicationOS;
pub use app::{Application, ApplicationApi};
pub use display::{Display, Panel, WindowPane};
pub use process::{Process, ProcessApi};
pub use skylight::AXUIElementCopyAttributeValue;
use skylight::{
    _AXUIElementCreateWithRemoteToken, SLSCopyActiveMenuBarDisplayIdentifier,
    SLSCopyAssociatedWindows, SLSCopyBestManagedDisplayForRect, SLSCopyManagedDisplayForWindow,
    SLSCopyManagedDisplaySpaces, SLSCopyWindowsWithOptionsAndTags, SLSFindWindowAndOwner,
    SLSGetConnectionIDForPSN, SLSGetCurrentCursorLocation, SLSGetDisplayMenubarHeight,
    SLSGetWindowBounds, SLSMainConnectionID, SLSManagedDisplayGetCurrentSpace,
    SLSWindowIteratorAdvance, SLSWindowIteratorGetAttributes, SLSWindowIteratorGetParentID,
    SLSWindowIteratorGetTags, SLSWindowIteratorGetWindowID, SLSWindowQueryResultCopyWindows,
    SLSWindowQueryWindows,
};
pub use windows::{Window, WindowApi, WindowOS, WindowPadding, ax_window_id};

mod app;
mod display;
mod process;
mod skylight;
mod windows;

/// Defines the interface for a window manager, abstracting OS-specific operations.
pub trait WindowManagerApi: Send + Sync {
    /// Creates a new `Application` instance from a given `ProcessApi`.
    ///
    /// # Arguments
    ///
    /// * `process` - A reference to the `ProcessApi` trait object representing the application's process.
    ///
    /// # Returns
    ///
    /// `Ok(Application)` if the application is successfully created, otherwise `Err(Error)`.
    fn new_application(&self, process: &dyn ProcessApi) -> Result<Application>;
    /// Refreshes the state of a given display, including its spaces and windows.
    ///
    /// # Arguments
    ///
    /// * `display` - A mutable reference to the `Display` to refresh.
    /// * `windows` - A mutable query for all `Window` components, their `Entity`, and `Has<Unmanaged>` status.
    fn refresh_display(
        &self,
        display: &mut Display,
        windows: &mut Query<(&mut Window, Entity, Has<Unmanaged>)>,
    );
    /// Retrieves a list of window IDs associated with a parent window.
    ///
    /// # Arguments
    ///
    /// * `window_id` - The `WinID` of the parent window.
    ///
    /// # Returns
    ///
    /// A `Vec<WinID>` containing the IDs of associated child windows.
    fn get_associated_windows(&self, window_id: WinID) -> Vec<WinID>;
    /// Retrieves a list of all currently present displays.
    ///
    /// # Returns
    ///
    /// A `Vec<Display>` containing `Display` objects for all present displays.
    fn present_displays(&self) -> Vec<Display>;
    /// Retrieves the `CGDirectDisplayID` of the active menu bar display.
    ///
    /// # Returns
    ///
    /// `Ok(u32)` with the display ID if successful, otherwise `Err(Error)`.
    fn active_display_id(&self) -> Result<u32>;
    /// Retrieves the ID of the current active space on a given display.
    ///
    /// # Arguments
    ///
    /// * `display_id` - The `CGDirectDisplayID` of the display.
    ///
    /// # Returns
    ///
    /// `Ok(u64)` with the space ID if successful, otherwise `Err(Error)`.
    fn active_display_space(&self, display_id: CGDirectDisplayID) -> Result<u64>;
    /// Centers the mouse cursor on a given window within its display bounds if it's not already within the window.
    ///
    /// # Arguments
    ///
    /// * `window` - A reference to the `Window` to center the mouse on.
    /// * `display_bounds` - The `CGRect` representing the bounds of the display the window is on.
    fn center_mouse(&self, window: &Window, display_bounds: &CGRect);
    /// Adds existing windows for a given application, potentially resolving unresolved windows.
    ///
    /// # Arguments
    ///
    /// * `app` - A mutable reference to the `Application` whose windows are to be added.
    /// * `spaces` - A slice of space IDs to query for windows.
    /// * `refresh_index` - An integer indicating the refresh index (used internally for tracking).
    ///
    /// # Returns
    ///
    /// `Ok(Vec<Window>)` containing the found and added windows, otherwise `Err(Error)`.
    fn add_existing_application_windows(
        &self,
        app: &mut Application,
        spaces: &[u64],
        refresh_index: i32,
    ) -> Result<Vec<Window>>;
    /// Finds the `WinID` of a window at a given screen point.
    ///
    /// # Arguments
    ///
    /// * `point` - A reference to the `CGPoint` representing the screen coordinate.
    ///
    /// # Returns
    ///
    /// `Ok(WinID)` with the found window's ID if successful, otherwise `Err(Error)`.
    fn find_window_at_point(&self, point: &CGPoint) -> Result<WinID>;
    /// Returns a list of `WinID`s for all windows in a given workspace (space).
    ///
    /// # Arguments
    ///
    /// * `space_id` - The ID of the space to query.
    ///
    /// # Returns
    ///
    /// `Ok(Vec<WinID>)` containing the list of window IDs, otherwise `Err(Error)`.
    fn windows_in_workspace(&self, space_id: u64) -> Result<Vec<WinID>>;
    /// Sends an `Event::Exit` to the event loop, signaling the application to quit.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the exit event is sent successfully, otherwise `Err(Error)`.
    fn quit(&self) -> Result<()>;

    fn setup_config_watcher(&self, path: &Path) -> Result<Box<dyn Watcher>>;
}

/// `WindowManager` is a Bevy resource that holds a boxed `WindowManagerApi` trait object.
/// It allows for dynamic dispatch to the OS-specific window management implementation.
#[derive(Resource)]
pub struct WindowManager(pub Box<dyn WindowManagerApi>);

impl Deref for WindowManager {
    type Target = Box<dyn WindowManagerApi>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for WindowManager {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// `WindowManagerOS` is the macOS-specific implementation of the `WindowManagerApi` trait.
/// It directly interacts with the macOS `SkyLight` and Accessibility APIs to manage windows.
pub struct WindowManagerOS {
    main_cid: ConnID,
    event_sender: EventSender,
}

impl WindowManagerOS {
    /// Creates a new `WindowManagerOS` instance.
    /// It initializes the main connection ID to the macOS `SkyLight` API.
    ///
    /// # Arguments
    ///
    /// * `event_sender` - The `EventSender` to dispatch events from the window manager.
    ///
    /// # Returns
    ///
    /// A new `WindowManagerOS` instance.
    pub fn new(event_sender: EventSender) -> Self {
        let main_cid = unsafe { SLSMainConnectionID() };
        debug!("{}: My connection id: {main_cid}", function_name!());

        Self {
            main_cid,
            event_sender,
        }
    }

    /// Retrieves a list of space IDs for a given display UUID.
    /// It queries the `SkyLight` API for managed display spaces and filters by the provided UUID.
    ///
    /// # Arguments
    ///
    /// * `uuid` - A reference to the `CFString` representing the display's UUID.
    ///
    /// # Returns
    ///
    /// `Ok(Vec<u64>)` with the list of space IDs if successful, otherwise `Err(Error)` if the spaces cannot be retrieved or the display is not found.
    fn display_space_list(&self, uuid: &CFString) -> Result<Vec<u64>> {
        let display_spaces = NonNull::new(unsafe { SLSCopyManagedDisplaySpaces(self.main_cid) })
            .map(|ptr| unsafe { CFRetained::from_raw(ptr) })
            .ok_or(Error::PermissionDenied(format!(
                "{}: can not copy managed display spaces for {}.",
                function_name!(),
                self.main_cid
            )))?;

        for display in get_array_values(display_spaces.as_ref()) {
            let display_ref = unsafe { display.as_ref() };
            trace!("{}: display {display_ref:?}", function_name!());
            let identifier = get_cfdict_value::<CFString>(
                display_ref,
                &CFString::from_static_str("Display Identifier"),
            )?;
            let identifier_ref = unsafe { identifier.as_ref() };
            debug!(
                "{}: identifier {identifier_ref:?} uuid {uuid:?}",
                function_name!()
            );
            // FIXME: For some reason the main display does not have a UUID in the name, but is
            // referenced as simply "Main".
            if identifier_ref.to_string().ne("Main") && !CFEqual(Some(identifier_ref), Some(uuid)) {
                continue;
            }

            let spaces =
                get_cfdict_value::<CFArray>(display_ref, &CFString::from_static_str("Spaces"))?;
            debug!("{}: spaces {spaces:?}", function_name!());

            let space_list = get_array_values(unsafe { spaces.as_ref() })
                .filter_map(|space| {
                    let num = get_cfdict_value::<CFNumber>(
                        unsafe { space.as_ref() },
                        &CFString::from_static_str("id64"),
                    )
                    .ok()?;

                    let mut id = 0u64;
                    unsafe {
                        CFNumber::value(
                            num.as_ref(),
                            CFNumber::r#type(num.as_ref()),
                            NonNull::from(&mut id).as_ptr().cast(),
                        )
                    };
                    (id != 0).then_some(id)
                })
                .collect::<Vec<u64>>();
            return Ok(space_list);
        }
        Err(Error::PermissionDenied(format!(
            "{}: could not get any displays for {}",
            function_name!(),
            self.main_cid
        )))
    }

    /// Retrieves the UUID of the active menu bar display.
    /// This typically corresponds to the display where the primary menu bar is located.
    ///
    /// # Returns
    ///
    /// `Ok(CFRetained<CFString>)` with the UUID if successful, otherwise `Err(Error)` if the active display cannot be determined.
    fn active_display_uuid(&self) -> Result<CFRetained<CFString>> {
        unsafe {
            let ptr = SLSCopyActiveMenuBarDisplayIdentifier(self.main_cid);
            let ptr = NonNull::new(ptr.cast_mut()).ok_or(Error::NotFound(format!(
                "{}: can not find active display for connection {}.",
                function_name!(),
                self.main_cid
            )))?;
            Ok(CFRetained::from_raw(ptr))
        }
    }

    /// Retrieves the UUID of the display a specific window is currently on.
    /// It first tries `SLSCopyManagedDisplayForWindow` and then falls back to `SLSCopyBestManagedDisplayForRect`
    /// using the window's bounds if the first call fails.
    ///
    /// # Arguments
    ///
    /// * `window_id` - The `WinID` of the window.
    ///
    /// # Returns
    ///
    /// `Ok(CFRetained<CFString>)` with the display UUID if successful, otherwise `Err(Error)` if the display cannot be determined.
    fn display_uuid(&self, window_id: WinID) -> Result<CFRetained<CFString>> {
        let uuid = unsafe {
            NonNull::new(SLSCopyManagedDisplayForWindow(self.main_cid, window_id).cast_mut())
                .map(|uuid| CFRetained::from_raw(uuid))
        };
        uuid.or_else(|| {
            let mut frame = CGRect::default();
            unsafe {
                SLSGetWindowBounds(self.main_cid, window_id, &mut frame);
                NonNull::new(SLSCopyBestManagedDisplayForRect(self.main_cid, frame).cast_mut())
                    .map(|uuid| CFRetained::from_raw(uuid))
            }
        })
        .ok_or(Error::InvalidInput(format!(
            "{}: can not get display uuid for window {window_id}.",
            function_name!()
        )))
    }

    /// Retrieves the `CGDirectDisplayID` of the display a specific window is currently on.
    /// It internally calls `display_uuid` and then converts the UUID to a display ID.
    ///
    /// # Arguments
    ///
    /// * `window_id` - The `WinID` of the window.
    ///
    /// # Returns
    ///
    /// `Ok(u32)` with the display ID if successful, otherwise `Err(Error)`.
    fn display_id(&self, window_id: WinID) -> Result<CGDirectDisplayID> {
        let uuid = self.display_uuid(window_id);
        uuid.and_then(|uuid| Display::id_from_uuid(&uuid))
    }

    /// Repopulates the current window panel with eligible windows from a specified space.
    /// This function filters windows based on eligibility and ensures they are present in the panel.
    ///
    /// # Arguments
    ///
    /// * `space_id` - The ID of the space to refresh windows from.
    /// * `windows` - A query for all `Window` components and their `Entity`.
    ///
    /// # Returns
    ///
    /// A `Vec<Entity>` containing the entities of eligible windows in the specified space.
    fn refresh_windows_space(
        &self,
        space_id: u64,
        windows: &Query<(&Window, Entity)>,
    ) -> Vec<Entity> {
        space_window_list_for_connection(self.main_cid, &[space_id], None, false)
            .inspect_err(|err| {
                warn!(
                    "{}: getting windows for space {space_id}: {err}",
                    function_name!()
                );
            })
            .unwrap_or_default()
            .into_iter()
            .filter_map(|window_id| windows.iter().find(|(window, _)| window.id() == window_id))
            .filter_map(|(window, entity)| window.is_eligible().then_some(entity))
            .collect()
    }

    /// Returns the connection ID (`ConnID`) for a given process serial number (`PSN`).
    ///
    /// # Arguments
    ///
    /// * `psn` - The `ProcessSerialNumber` of the process.
    ///
    /// # Returns
    ///
    /// `Some(ConnID)` if the connection ID is found, otherwise `None`.
    fn connection_for_process(&self, psn: ProcessSerialNumber) -> Option<ConnID> {
        let mut connection: ConnID = 0;
        unsafe { SLSGetConnectionIDForPSN(self.main_cid, &psn, &mut connection) };
        (connection != 0).then_some(connection)
    }
}

impl WindowManagerApi for WindowManagerOS {
    fn new_application(&self, process: &dyn ProcessApi) -> Result<Application> {
        let connection = self.connection_for_process(process.psn());
        ApplicationOS::new(connection, process, &self.event_sender)
            .map(|app| Application::new(Box::new(app)))
    }

    /// Refreshes the state of a given display, including its spaces and windows.
    /// It repopulates each `WindowPane` with eligible windows from its corresponding space,
    /// ensuring that existing windows maintain their order and unmanaged windows are ignored.
    ///
    /// # Arguments
    ///
    /// * `display` - A mutable reference to the `Display` to refresh.
    /// * `windows` - A mutable query for all `Window` components, their `Entity`, and `Has<Unmanaged>` status.
    fn refresh_display(
        &self,
        display: &mut Display,
        windows: &mut Query<(&mut Window, Entity, Has<Unmanaged>)>,
    ) {
        debug!(
            "{}: Refreshing windows on display {}",
            function_name!(),
            display.id()
        );

        let display_bounds = display.bounds;
        for (space_id, pane) in &mut display.spaces {
            let mut lens = windows.transmute_lens::<(&Window, Entity)>();
            let new_windows = self.refresh_windows_space(*space_id, &lens.query());

            // Preserve the order - do not flush existing windows.
            for window_entity in pane.all_windows() {
                if !new_windows.contains(&window_entity) {
                    pane.remove(window_entity);
                }
            }
            for window_entity in new_windows {
                if windows
                    .get(window_entity)
                    .is_ok_and(|(_, _, unmanaged)| unmanaged)
                {
                    // Window is not managed, do not insert it into the panel.
                    continue;
                }
                if pane.index_of(window_entity).is_err() {
                    pane.append(window_entity);
                    if let Ok((mut window, _, _)) = windows.get_mut(window_entity) {
                        _ = window.update_frame(&display_bounds);
                    }
                }
            }
            debug!(
                "{}: space {space_id}: after refresh {pane}",
                function_name!()
            );
        }
    }

    /// Returns child windows of the main window.
    fn get_associated_windows(&self, window_id: WinID) -> Vec<WinID> {
        let window_list = unsafe {
            let arr_ref = SLSCopyAssociatedWindows(self.main_cid, window_id);
            CFRetained::retain(arr_ref)
        };

        get_array_values(&window_list)
            .filter_map(|item| {
                let mut child_wid: WinID = 0;
                if unsafe {
                    CFNumber::value(
                        item.as_ref(),
                        CFNumberType::SInt32Type,
                        NonNull::from(&mut child_wid).as_ptr().cast(),
                    )
                } {
                    debug!(
                        "{}: checking {}'s childen: {}",
                        function_name!(),
                        window_id,
                        child_wid
                    );
                    (child_wid != 0).then_some(child_wid)
                } else {
                    warn!(
                        "{}: Unable to find subwindows of window {}: {item:?}.",
                        function_name!(),
                        window_id
                    );
                    None
                }
            })
            .collect()
    }

    /// Retrieves a list of all currently present displays, along with their associated spaces.
    ///
    /// # Returns
    ///
    /// A `Vec<Self>` containing `Display` objects for all present displays.
    fn present_displays(&self) -> Vec<Display> {
        let mut count = 0u32;
        unsafe {
            CGGetActiveDisplayList(0, null_mut(), &raw mut count);
        }
        if count < 1 {
            return vec![];
        }
        let mut displays = Vec::with_capacity(count.try_into().unwrap());
        unsafe {
            CGGetActiveDisplayList(count, displays.as_mut_ptr(), &raw mut count);
            displays.set_len(count.try_into().unwrap());
        }
        displays
            .into_iter()
            .flat_map(|id| {
                let uuid = Display::uuid_from_id(id);
                uuid.and_then(|uuid| {
                    self.display_space_list(uuid.as_ref()).map(|spaces| {
                        let bounds = CGDisplayBounds(id);
                        let mut menubar_height: u32 = 0;
                        unsafe { SLSGetDisplayMenubarHeight(id, &raw mut menubar_height) };
                        debug!("{}: menubar height: {menubar_height}", function_name!());
                        Display::new(id, spaces, bounds, menubar_height)
                    })
                })
            })
            .collect()
    }

    /// Retrieves the `CGDirectDisplayID` of the active menu bar display.
    ///
    /// # Returns
    ///
    /// `Ok(u32)` with the display ID if successful, otherwise `Err(Error)`.
    fn active_display_id(&self) -> Result<u32> {
        let uuid = self.active_display_uuid()?;
        Display::id_from_uuid(&uuid)
    }

    /// Retrieves the ID of the current active space on this display.
    ///
    /// # Returns
    ///
    /// `Ok(u64)` with the space ID if successful, otherwise `Err(Error)`.
    fn active_display_space(&self, display_id: CGDirectDisplayID) -> Result<u64> {
        // let cid = self.main_cid;
        // let uuid = Display::active_display_uuid(cid);
        // uuid.map(|uuid| unsafe { SLSManagedDisplayGetCurrentSpace(cid, uuid.deref()) })
        Display::uuid_from_id(display_id).map(|uuid| unsafe {
            SLSManagedDisplayGetCurrentSpace(self.main_cid, &raw const *uuid)
        })
    }

    /// Centers the mouse cursor on the window if it's not already within the window's bounds.
    fn center_mouse(&self, window: &Window, display_bounds: &CGRect) {
        let mut cursor = CGPoint::default();
        if unsafe { CGError::Success != SLSGetCurrentCursorLocation(self.main_cid, &mut cursor) } {
            warn!(
                "{}: Unable to get current cursor position.",
                function_name!()
            );
            return;
        }
        let frame = window.frame();
        if CGRectContainsPoint(frame, cursor) {
            return;
        }

        let center = CGPoint::new(
            display_bounds.origin.x + frame.origin.x + frame.size.width / 2.0,
            display_bounds.origin.y + frame.origin.y + frame.size.height / 2.0,
        );
        let display_id = self.display_id(window.id());
        #[allow(clippy::redundant_closure)]
        let bounds = display_id.map(|display_id| CGDisplayBounds(display_id));
        if bounds.is_ok_and(|bounds| !CGRectContainsPoint(bounds, center)) {
            return;
        }
        CGWarpMouseCursorPosition(center);
    }

    /// Adds existing windows for a given application, attempting to resolve any that are not yet found.
    /// It compares the application's reported window list with the global window list and uses brute-forcing if necessary.
    ///
    /// # Arguments
    ///
    /// * `app` - A mutable reference to the `Application` whose windows are to be added.
    /// * `spaces` - A slice of space IDs to query.
    /// * `refresh_index` - An integer indicating the refresh index, used to determine if all windows are resolved.
    ///
    /// # Returns
    ///
    /// `Ok(Vec<Window>)` containing the found windows, otherwise `Err(Error)`.
    // bool window_manager_add_existing_application_windows(struct space_manager *sm,
    // struct window_manager *wm, struct application *application, int refresh_index)
    fn add_existing_application_windows(
        &self,
        app: &mut Application,
        spaces: &[u64],
        refresh_index: i32,
    ) -> Result<Vec<Window>> {
        // uint32_t *global_window_list = window_manager_existing_application_window_list(application, &global_window_count);
        // if (!global_window_list) return result;
        let global_window_list = existing_application_window_list(self.main_cid, app, spaces)?;
        if global_window_list.is_empty() {
            return Err(Error::InvalidInput(format!(
                "{}: No windows found for {app}",
                function_name!()
            )));
        }
        debug!(
            "{}: {app} has global windows: {global_window_list:?}",
            function_name!()
        );

        let mut found_windows: Vec<Window> = Vec::new();
        for found in app.window_list()? {
            match found {
                Ok(found_window) => {
                    if !found_windows
                        .iter()
                        .any(|window| window.id() == found_window.id())
                    {
                        found_windows.push(found_window);
                    }
                }
                Err(err) => {
                    debug!("{}: create window: {err}", function_name!());
                }
            }
        }

        // if (global_window_count == window_count-empty_count)
        if global_window_list.len() == found_windows.len() {
            if refresh_index != -1 {
                debug!(
                    "{}: All windows for {:?} are now resolved",
                    function_name!(),
                    app.psn(),
                );
            }
        } else {
            // for (int i = 0; i < global_window_count; ++i) {
            //     struct window *window = window_manager_find_window(wm, global_window_list[i]);
            //     if (!window) {
            //         missing_window = true;
            //         break;
            //     }
            // }
            let find_window =
                |window_id| found_windows.iter().find(|window| window.id() == window_id);
            let mut app_window_list: Vec<WinID> = global_window_list
                .iter()
                .filter(|window_id| find_window(**window_id).is_none())
                .copied()
                .collect();

            // if (missing_window) {
            if !app_window_list.is_empty() {
                debug!(
                    "{}: {:?} has windows that are not yet resolved",
                    function_name!(),
                    app.psn(),
                );
                found_windows.extend(bruteforce_windows(app, &mut app_window_list));

                // } else {
                //     // debug("%s: all windows for %s are now resolved\n", __FUNCTION__, application->name);
                //     info!(
                //         "add_existing_application_windows: All windows for {} are now resolved (second pass)",
                //         app.inner().name
                //     );
                //     // buf_del(wm->applications_to_refresh, refresh_index);
                //     result = true;
            }
        }

        // if (window_list_ref) CFRelease(window_list_ref);
        Ok(found_windows)
    }

    /// Finds a window at a given screen point using `SkyLight` API.
    ///
    /// # Arguments
    ///
    /// * `point` - A reference to the `CGPoint` representing the screen coordinate.
    ///
    /// # Returns
    ///
    /// `Ok(WinID)` with the found window's ID if successful, otherwise `Err(Error)`.
    fn find_window_at_point(&self, point: &CGPoint) -> Result<WinID> {
        let mut window_id: WinID = 0;
        let mut window_conn_id: ConnID = 0;
        let mut window_point = CGPoint { x: 0f64, y: 0f64 };
        unsafe {
            SLSFindWindowAndOwner(
                self.main_cid,
                0, // filter window id
                1,
                0,
                point,
                &mut window_point,
                &mut window_id,
                &mut window_conn_id,
            )
        };
        if self.main_cid == window_conn_id {
            unsafe {
                SLSFindWindowAndOwner(
                    self.main_cid,
                    window_id,
                    -1,
                    0,
                    point,
                    &mut window_point,
                    &mut window_id,
                    &mut window_conn_id,
                )
            };
        }
        if window_id == 0 {
            Err(Error::invalid_window(&format!(
                "{}: could not find a window at {point:?}",
                function_name!()
            )))
        } else {
            Ok(window_id)
        }
    }

    /// Returns a list of windows in a given workspace.
    fn windows_in_workspace(&self, space_id: u64) -> Result<Vec<WinID>> {
        space_window_list_for_connection(self.main_cid, &[space_id], None, true)
    }

    fn quit(&self) -> Result<()> {
        self.event_sender.send(Event::Exit)
    }

    fn setup_config_watcher(&self, path: &Path) -> Result<Box<dyn Watcher>> {
        let setup = notify::Config::default()
            .with_poll_interval(Duration::from_secs(3))
            .with_follow_symlinks(false);
        let config_handler = ConfigHandler(self.event_sender.clone());
        let symlink = symlink_target(path);

        let mut watcher = if let Some(symlink) = symlink {
            setup.with_follow_symlinks(true);
            let mut watcher = notify::PollWatcher::new(config_handler, setup)?;
            debug!(
                "{}: watching symlink target {} for changes.",
                function_name!(),
                symlink.display()
            );
            watcher.watch(&symlink, RecursiveMode::NonRecursive)?;

            Ok::<Box<dyn Watcher>, Error>(Box::new(watcher))
        } else {
            Ok::<Box<dyn Watcher>, Error>(Box::new(notify::RecommendedWatcher::new(
                config_handler,
                setup,
            )?))
        }?;
        debug!(
            "{}: watching config file {} for changes.",
            function_name!(),
            path.display()
        );
        watcher.watch(path, RecursiveMode::NonRecursive)?;
        Ok(watcher)
    }
}

/// Retrieves a list of window IDs for specified spaces and connection, with an option to include minimized windows.
/// This function uses `SkyLight` API calls to query windows based on their space, connection, and visibility tags.
///
/// # Arguments
///
/// * `main_cid` - The main connection ID.
/// * `spaces` - A slice of space IDs to query windows from.
/// * `cid` - An optional connection ID. If `None`, the main connection ID is used.
/// * `also_minimized` - A boolean indicating whether to include minimized windows in the result.
///
/// # Returns
///
/// `Ok(Vec<WinID>)` containing the list of window IDs if successful, otherwise `Err(Error)`.
fn space_window_list_for_connection(
    main_cid: ConnID,
    spaces: &[u64],
    cid: Option<ConnID>,
    also_minimized: bool,
) -> Result<Vec<WinID>> {
    let space_list_ref = create_array(spaces, CFNumberType::SInt64Type)?;

    let mut set_tags = 0i64;
    let mut clear_tags = 0i64;
    let options = if also_minimized { 0x7 } else { 0x2 };
    let ptr = NonNull::new(unsafe {
        SLSCopyWindowsWithOptionsAndTags(
            main_cid,
            cid.unwrap_or(0),
            &raw const *space_list_ref,
            options,
            &mut set_tags,
            &mut clear_tags,
        )
    })
    .ok_or(Error::InvalidInput(format!(
        "{}: nullptr returned from SLSCopyWindowsWithOptionsAndTags.",
        function_name!()
    )))?;
    let window_list_ref = unsafe { CFRetained::from_raw(ptr) };

    let count = window_list_ref.count();
    if count == 0 {
        return Err(Error::NotFound(format!(
            "{}: zero windows returned",
            function_name!()
        )));
    }

    let query = unsafe {
        CFRetained::from_raw(SLSWindowQueryWindows(
            main_cid,
            &raw const *window_list_ref,
            count,
        ))
    };
    let iterator =
        unsafe { CFRetained::from_raw(SLSWindowQueryResultCopyWindows(query.deref().into())) };

    let mut window_list = Vec::with_capacity(count.try_into()?);
    while unsafe { SLSWindowIteratorAdvance(&raw const *iterator) } {
        let tags = unsafe { SLSWindowIteratorGetTags(&raw const *iterator) };
        let attributes = unsafe { SLSWindowIteratorGetAttributes(&raw const *iterator) };
        let parent_wid: WinID = unsafe { SLSWindowIteratorGetParentID(&raw const *iterator) };
        let window_id: WinID = unsafe { SLSWindowIteratorGetWindowID(&raw const *iterator) };

        trace!(
            "{}: id: {window_id} parent: {parent_wid} tags: 0x{tags:x} attributes: 0x{attributes:x}",
            function_name!()
        );
        if found_valid_window(parent_wid, attributes, tags) {
            window_list.push(window_id);
        }
    }
    Ok(window_list)
}

/// Determines if a window is valid based on its parent ID, attributes, and tags.
/// This function implements complex logic to filter out irrelevant or invalid windows.
///
/// # Arguments
///
/// * `parent_wid` - The parent window ID.
/// * `attributes` - The attributes of the window.
/// * `tags` - The tags associated with the window.
///
/// # Returns
///
/// `true` if the window is considered valid, `false` otherwise.
fn found_valid_window(parent_wid: WinID, attributes: i64, tags: i64) -> bool {
    parent_wid == 0
        && ((0 != (attributes & 0x2) || 0 != (tags & 0x0400_0000_0000_0000))
            && (0 != (tags & 0x1) || (0 != (tags & 0x2) && 0 != (tags & 0x8000_0000))))
        || ((attributes == 0x0 || attributes == 0x1)
            && (0 != (tags & 0x1000_0000_0000_0000) || 0 != (tags & 0x0300_0000_0000_0000))
            && (0 != (tags & 0x1) || (0 != (tags & 0x2) && 0 != (tags & 0x8000_0000))))
}

/// Retrieves a list of existing application window IDs for a given application.
/// It queries windows across all active displays and spaces associated with the application's connection.
///
/// # Arguments
///
/// * `cid` - The main connection ID.
/// * `app` - A reference to the `Application` for which to retrieve window IDs.
/// * `spaces` - A slice of space IDs to query.
///
/// # Returns
///
/// `Ok(Vec<WinID>)` containing the list of window IDs if successful, otherwise `Err(Error)`.
fn existing_application_window_list(
    cid: ConnID,
    app: &Application,
    spaces: &[u64],
) -> Result<Vec<WinID>> {
    if spaces.is_empty() {
        return Err(Error::NotFound(format!(
            "{}: no spaces returned",
            function_name!()
        )));
    }
    space_window_list_for_connection(cid, spaces, app.connection(), true)
}

/// Attempts to find and add unresolved windows for a given application by brute-forcing `element_id` values.
/// This is a workaround for macOS API limitations that do not return `AXUIElementRef` for windows on inactive spaces.
///
/// # Arguments
///
/// * `app` - A reference to the `Application` whose windows are to be brute-forced.
/// * `window_list` - A mutable vector of `WinID`s representing the expected global window list; found windows are removed from this list.
fn bruteforce_windows(app: &mut Application, window_list: &mut Vec<WinID>) -> Vec<Window> {
    const MAGIC: u32 = 0x636f_636f;
    let mut found_windows = Vec::new();
    debug!(
        "{}: {app} has unresolved window on other desktops, bruteforcing them.",
        function_name!()
    );

    //
    // NOTE: MacOS API does not return AXUIElementRef of windows on inactive spaces. However,
    // we can just brute-force the element_id and create the AXUIElementRef ourselves.
    //  https://github.com/decodism
    //  https://github.com/lwouis/alt-tab-macos/issues/1324#issuecomment-2631035482
    //

    unsafe {
        const BUFSIZE: isize = 0x14;
        let Some(data_ref) = CFMutableData::new(None, BUFSIZE) else {
            error!("{}: error creating mutable data", function_name!());
            return found_windows;
        };
        CFMutableData::increase_length(data_ref.deref().into(), BUFSIZE);

        let data = from_raw_parts_mut(
            CFMutableData::mutable_byte_ptr(data_ref.deref().into()),
            BUFSIZE as usize,
        );
        let bytes = app.pid().to_ne_bytes();
        data[0x0..bytes.len()].copy_from_slice(&bytes);
        let bytes = MAGIC.to_ne_bytes();
        data[0x8..0x8 + bytes.len()].copy_from_slice(&bytes);

        for element_id in 0..0x7fffu64 {
            //
            // NOTE: Only the element_id changes between iterations.
            //

            let bytes = element_id.to_ne_bytes();
            data[0xc..0xc + bytes.len()].copy_from_slice(&bytes);

            let Ok(element_ref) =
                AXUIWrapper::retain(_AXUIElementCreateWithRemoteToken(data_ref.as_ref()))
            else {
                continue;
            };
            let Ok(window_id) = ax_window_id(element_ref.as_ptr()) else {
                continue;
            };

            if let Some(index) = window_list.iter().position(|&id| id == window_id) {
                window_list.remove(index);
                debug!("{}: Found window {window_id:?}", function_name!());
                if let Ok(window) = WindowOS::new(&element_ref)
                    .inspect_err(|err| warn!("{}: {err}", function_name!()))
                {
                    found_windows.push(Window::new(Box::new(window)));
                }
            }
        }
    }
    found_windows
}

/// Checks if the application has Accessibility privileges.
/// It will prompt the user to grant permission if not already granted.
///
/// # Returns
///
/// `true` if Accessibility privileges are granted, `false` otherwise.
pub fn check_ax_privilege() -> bool {
    unsafe {
        let keys = [kAXTrustedCheckOptionPrompt
            .cast::<CFString>()
            .as_ref()
            .unwrap()];
        let values = [kCFBooleanTrue.unwrap()];
        let opts = CFDictionary::from_slices(&keys, &values);
        AXIsProcessTrustedWithOptions((&raw const *opts).cast())
    }
}

/// Checks if the macOS "Displays have separate Spaces" option is enabled.
/// This is crucial for the window manager's functionality, as Paneru relies on independent spaces per display.
///
/// # Returns
///
/// `true` if separate spaces are enabled, `false` otherwise.
pub fn check_separate_spaces() -> bool {
    unsafe {
        let cid = SLSMainConnectionID();
        SLSGetSpaceManagementMode(cid) == 1
    }
}

/// `ConfigHandler` is an implementation of `notify::EventHandler` that reloads the application configuration
/// when the configuration file changes. It also dispatches a `ConfigRefresh` event.
struct ConfigHandler(EventSender);

impl notify::EventHandler for ConfigHandler {
    /// Handles file system events for the configuration file. When the content changes, it reloads the configuration.
    /// Specifically, it responds to `ModifyKind::Data(DataChange::Content)` events.
    ///
    /// # Arguments
    ///
    /// * `event` - The result of a file system event.
    fn handle_event(&mut self, event: notify::Result<notify::Event>) {
        if let Ok(event) = event {
            _ = self.0.send(Event::ConfigRefresh(event)).inspect_err(|err| {
                warn!("{}: error sending config refresh: {err}", function_name!());
            });
        }
    }
}