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
use std::{cell::Cell, fmt::Debug, hash::Hash, sync::Arc};

use futures::channel::oneshot;
use libc::getpid;
use parking_lot::Mutex;

use crate::{capturable_content::{CapturableContentError, CapturableContentFilter}, prelude::{CapturableContent, CapturableWindow}, util::{Point, Rect, Size}};

use super::objc_wrap::{get_window_description, get_window_levels, CGMainDisplayID, CGWindowID, SCDisplay, SCRunningApplication, SCShareableContent, SCWindow};

pub struct MacosCapturableContent {
    pub windows: Vec<SCWindow>,
    pub displays: Vec<SCDisplay>,
}

impl MacosCapturableContent {
    pub async fn new(filter: CapturableContentFilter) -> Result<Self, CapturableContentError> {
        // Force core graphics initialization
        unsafe { CGMainDisplayID() };
        let (exclude_desktop, onscreen_only) = filter.windows.map_or((false, true), |filter| (!filter.desktop_windows, filter.onscreen_only));
        let (tx, rx) = oneshot::channel();
        let mut tx = Mutex::new(Some(tx));
        SCShareableContent::get_shareable_content_with_completion_handler(exclude_desktop, onscreen_only, move |result| {
            if let Some(tx) = tx.lock().take() {
                let _ = tx.send(result);
            }
        });

        match rx.await {
            Ok(Ok(content)) => {
                let windows = content.windows()
                    .into_iter()
                    .filter(|window| filter.impl_capturable_content_filter.filter_scwindow(window))
                    .collect();
                let displays = content.displays()
                    .into_iter()
                    .filter(|display| filter.impl_capturable_content_filter.filter_scdisplay(display))
                    .collect();
                Ok(Self {
                    windows,
                    displays,
                })
            },
            Ok(Err(error)) => {
                Err(CapturableContentError::Other(format!("SCShareableContent returned error code: {}", error.code())))
            }
            Err(error) => Err(CapturableContentError::Other(format!("Failed to receive SCSharableContent result from completion handler future: {}", error.to_string()))),
        }
    }
}

#[derive(Clone)]
pub struct MacosCapturableWindow {
    pub(crate) window: SCWindow
}

impl MacosCapturableWindow {
    pub fn from_impl(window: SCWindow) -> Self {
        Self {
            window
        }
    }

    pub fn title(&self) -> String {
        self.window.title()
    }

    pub fn rect(&self) -> Rect {
        let frame = self.window.frame();
        Rect {
            origin: Point {
                x: frame.origin.x,
                y: frame.origin.y,
            },
            size: Size {
                width: frame.size.x,
                height: frame.size.y
            }
        }
    }

    pub fn application(&self) -> MacosCapturableApplication {
        MacosCapturableApplication {
            running_application: self.window.owning_application()
        }
    }

    pub fn is_visible(&self) -> bool {
        self.window.on_screen()
    }
}

impl Debug for MacosCapturableWindow {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MacosCapturableWindow").field("window", &self.window.title()).finish()
    }
}

impl PartialEq for MacosCapturableWindow {
    fn eq(&self, other: &Self) -> bool {
        self.window.id().0 == other.window.id().0
    }
}

impl Hash for MacosCapturableWindow {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.window.id().0.hash(state);
    }
}

impl Eq for MacosCapturableWindow {}

#[derive(Clone)]
pub struct MacosCapturableDisplay {
    pub(crate) display: SCDisplay
}

impl MacosCapturableDisplay {
    pub fn from_impl(display: SCDisplay) -> Self {
        Self {
            display
        }
    }

    pub fn rect(&self) -> Rect {
        let frame = self.display.frame();
        Rect {
            origin: Point {
                x: frame.origin.x,
                y: frame.origin.y,
            },
            size: Size {
                width: frame.size.x,
                height: frame.size.y
            }
        }
    }
}

impl PartialEq for MacosCapturableDisplay {
    fn eq(&self, other: &Self) -> bool {
        self.display.raw_id() == other.display.raw_id()
    }
}

impl Hash for MacosCapturableDisplay {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.display.raw_id().hash(state)
    }
}

impl Eq for MacosCapturableDisplay {}

impl Debug for MacosCapturableDisplay {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MacosCapturableDisplay").field("display", &self.display.raw_id()).finish()
    }
}

#[derive()]
pub struct MacosCapturableApplication {
    pub(crate) running_application: SCRunningApplication,
}

impl MacosCapturableApplication {
    pub fn identifier(&self) -> String {
        self.running_application.bundle_identifier()
    }

    pub fn name(&self) -> String {
        self.running_application.application_name()
    }

    pub fn pid(&self) -> i32 {
        self.running_application.pid()
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// Represents the "window level" of a native Mac OS window. Windows within the same level are ordered above or below levels that are above below or above this level respectively.
pub enum MacosWindowLevel {
    BelowDesktop      =  0,
    Desktop           =  1,
    DesktopIcon       =  2,
    Backstop          =  3,
    Normal            =  4,
    Floating          =  5,
    TornOffMenu       =  6,
    Dock              =  7,
    MainMenu          =  8,
    Status            =  9,
    ModalPanel        = 10,
    PopupMenu         = 11,
    Dragging          = 12,
    ScreenSaver       = 13,
    Overlay           = 14,
    Help              = 15,
    Utility           = 16,
    Cursor            = 17,
    AssistiveTechHigh = 18,
}

/// A capturable window with mac-os specific features
pub trait MacosCapturableWindowExt {
    /// Get the window layer of this window
    fn get_window_layer(&self) -> Result<i32, CapturableContentError>;

    /// Get the window level of this window
    fn get_window_level(&self) -> Result<MacosWindowLevel, CapturableContentError>;

    /// Get the native window id for this capturable window.
    /// This is the `CGWindowID` for this window.
    fn get_window_id(&self) -> u32;

    /// Try and convert the given CGWindowID to a capturable window.
    fn from_window_id(window_id: u32) -> impl std::future::Future<Output = Result<CapturableWindow, CapturableContentError>>;
}

fn get_window_layer(window_id: u32) -> Result<i32, ()> {
    let window_description = get_window_description(CGWindowID(window_id))?;
    Ok(window_description.window_layer)
}

fn get_window_level(window_id: u32) -> Result<MacosWindowLevel, ()> {
    let window_levels = get_window_levels();
    let level = get_window_layer(window_id)?;
    Ok(
        if (level < window_levels.desktop) {
            MacosWindowLevel::BelowDesktop
        } else if (level < window_levels.desktop_icon) {
            MacosWindowLevel::Desktop
        } else if (level < window_levels.backstop) {
            MacosWindowLevel::DesktopIcon
        } else if (level < window_levels.normal) {
            MacosWindowLevel::Backstop
        } else if (level < window_levels.floating) {
            MacosWindowLevel::Normal
        } else if (level < window_levels.torn_off_menu) {
            MacosWindowLevel::Floating
        } else if (level < window_levels.modal_panel) {
            MacosWindowLevel::TornOffMenu
        } else if (level < window_levels.utility) {
            MacosWindowLevel::ModalPanel
        } else if (level < window_levels.dock) {
            MacosWindowLevel::Utility
        } else if (level < window_levels.main_menu) {
            MacosWindowLevel::Dock
        } else if (level < window_levels.status) {
            MacosWindowLevel::MainMenu
        } else if (level < window_levels.pop_up_menu) {
            MacosWindowLevel::Status
        } else if (level < window_levels.overlay) {
            MacosWindowLevel::PopupMenu
        } else if (level < window_levels.help) {
            MacosWindowLevel::Overlay
        } else if (level < window_levels.dragging) {
            MacosWindowLevel::Help
        } else if (level < window_levels.screen_saver) {
            MacosWindowLevel::Dragging
        } else if (level < window_levels.assistive_tech_high) {
            MacosWindowLevel::ScreenSaver
        } else if (level < window_levels.cursor) {
            MacosWindowLevel::AssistiveTechHigh
        } else {
            MacosWindowLevel::Cursor
        }
    )
}

impl MacosCapturableWindowExt for CapturableWindow {
    fn get_window_layer(&self) -> Result<i32, CapturableContentError> {
        get_window_layer(self.impl_capturable_window.window.id().0)
            .map_err(|_| CapturableContentError::Other(("Failed to retreive window layer".to_string())))
    }

    fn get_window_level(&self) -> Result<MacosWindowLevel, CapturableContentError> {
        get_window_level(self.impl_capturable_window.window.id().0)
            .map_err(|_| CapturableContentError::Other(("Failed to retreive window level".to_string())))
    }

    fn get_window_id(&self) -> u32 {
        self.impl_capturable_window.window.id().0
     }
 
     fn from_window_id(window_id: u32) -> impl std::future::Future<Output = Result<CapturableWindow, CapturableContentError>> {
         async move {
             let content = CapturableContent::new(CapturableContentFilter::ALL_WINDOWS).await?;
             for window in content.windows().into_iter() {
                 if window.get_window_id() == window_id {
                     return Ok(window.clone());
                 }
             }
             Err(CapturableContentError::Other(format!("No capturable window with id: {} found", window_id)))
         }
     }
}

#[derive(Clone)]
pub(crate) struct MacosCapturableContentFilter {
    pub window_level_range: (Option<MacosWindowLevel>, Option<MacosWindowLevel>),
    pub excluded_bundle_ids: Option<Arc<[String]>>,
    pub excluded_window_ids: Option<Arc<[u32]>>,
}

impl Default for MacosCapturableContentFilter {
    fn default() -> Self {
        Self {
            window_level_range: (None, None),
            excluded_bundle_ids: None,
            excluded_window_ids: None,
        }
    }
}

impl MacosCapturableContentFilter {
    fn filter_scwindow(&self, window: &SCWindow) -> bool {
        let mut allow = true;
        if self.window_level_range != (None, None) {
            if let Ok(level) = get_window_level(window.id().0) {
                allow &= match &self.window_level_range {
                    (Some(min), Some(max)) => (level >= *min) && (level <= *max),
                    (Some(min), None) => level >= *min,
                    (None, Some(max)) => level <= *max,
                    (None, None) => unreachable!(),
                };
            }
        }
        if let Some(excluded_bundle_ids) = &self.excluded_bundle_ids {
            let bundle_id = window.owning_application().bundle_identifier();
            if excluded_bundle_ids.contains(&bundle_id.to_lowercase()) {
                allow = false;
            }
        }
        if let Some(excluded_window_ids) = &self.excluded_window_ids {
            if excluded_window_ids.contains(&window.id().0) {
                allow = false;
            }
        }
        allow
    }

    fn filter_scdisplay(&self, display: &SCDisplay) -> bool {
        true
    }

    pub const DEFAULT: Self = MacosCapturableContentFilter {
        window_level_range: (None, None),
        excluded_bundle_ids: None,
        excluded_window_ids: None,
    };

    pub const NORMAL_WINDOWS: Self = MacosCapturableContentFilter {
        window_level_range: (Some(MacosWindowLevel::Normal), Some(MacosWindowLevel::TornOffMenu)),
        excluded_bundle_ids: None,
        excluded_window_ids: None,
    };
}

/// A capturable content filter with Mac OS specific options
pub trait MacosCapturableContentFilterExt: Sized {
    /// Set the range of "window levels" to filter to (inclusive)
    fn with_window_level_range(self, min: Option<MacosWindowLevel>, max: Option<MacosWindowLevel>) -> Result<Self, CapturableContentError>;
    /// Exclude windows who's applications have the provided bundle ids
    fn with_exclude_bundle_ids(self, bundle_id: &[&str]) -> Self;
    /// Exclude windows with the given CGWindowIDs
    fn with_exclude_window_ids(self, window_ids: &[u32]) -> Self;
}

impl MacosCapturableContentFilterExt for CapturableContentFilter {
    fn with_window_level_range(self, min: Option<MacosWindowLevel>, max: Option<MacosWindowLevel>) -> Result<Self, CapturableContentError> {
        match (&min, &max) {
            (Some(min_level), Some(max_level)) => {
                if *min_level as i32 > *max_level as i32 {
                    return Err(CapturableContentError::Other(format!("Invalid window level range: minimum level: {:?} is greater than maximum level: {:?}", *min_level, *max_level)));
                }
            },
            _ => {}
        }
        Ok(Self {
            impl_capturable_content_filter: MacosCapturableContentFilter {
                window_level_range: (min, max),
                ..self.impl_capturable_content_filter
            },
            ..self
        })
    }

    fn with_exclude_bundle_ids(self, excluded_bundle_ids: &[&str]) -> Self {
        let mut new_bundle_id_list = vec![];
        if let Some(current_bundle_ids) = &self.impl_capturable_content_filter.excluded_bundle_ids {
            for bundle_id in current_bundle_ids.iter() {
                new_bundle_id_list.push(bundle_id.to_owned());
            }
        }
        for bundle_id in excluded_bundle_ids.iter() {
            new_bundle_id_list.push((*bundle_id).to_lowercase());
        }
        Self {
            impl_capturable_content_filter: MacosCapturableContentFilter {
                excluded_bundle_ids: Some(new_bundle_id_list.into_boxed_slice().into()),
                ..self.impl_capturable_content_filter
            },
            ..self
        }
    }

    fn with_exclude_window_ids(self, excluded_window_ids: &[u32]) -> Self {
        let mut new_excluded_window_id_list = vec![];
        if let Some(current_excluded_window_ids) = &self.impl_capturable_content_filter.excluded_window_ids {
            for window_id in current_excluded_window_ids.iter() {
                new_excluded_window_id_list.push(*window_id);
            }
        }
        for window_id in excluded_window_ids.iter() {
            new_excluded_window_id_list.push(*window_id);
        }
        Self {
            impl_capturable_content_filter: MacosCapturableContentFilter {
                excluded_window_ids: Some(new_excluded_window_id_list.into_boxed_slice().into()),
                ..self.impl_capturable_content_filter
            },
            ..self
        }
    }
}