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
use std::ptr::null;

use core_foundation::{array::CFArray, base::TCFType, string::CFString};
use libc::c_double;

#[cfg(feature = "window")]
use crate::window::CGWindowID;
use crate::{
    color_space::CGColorSpace,
    context::CGContext,
    error::CGError,
    geometry::{CGPoint, CGRect, CGSize},
    image::CGImage,
};
pub use crate::{direct_display::*, display_configuration::*, display_fade::*, remote_operation::*};

// IOKit/graphics/IOGraphicsTypes.h
pub const IO1BitIndexedPixels: &str = "P";
pub const IO2BitIndexedPixels: &str = "PP";
pub const IO4BitIndexedPixels: &str = "PPPP";
pub const IO8BitIndexedPixels: &str = "PPPPPPPP";
pub const IO16BitDirectPixels: &str = "-RRRRRGGGGGBBBBB";
pub const IO32BitDirectPixels: &str = "--------RRRRRRRRGGGGGGGGBBBBBBBB";
pub const kIO30BitDirectPixels: &str = "--RRRRRRRRRRGGGGGGGGGGBBBBBBBBBB";
pub const kIO64BitDirectPixels: &str = "-16R16G16B16";
pub const kIO16BitFloatPixels: &str = "-16FR16FG16FB16";
pub const kIO32BitFloatPixels: &str = "-32FR32FG32FB32";
pub const IOYUV422Pixels: &str = "Y4U2V2";
pub const IO8BitOverlayPixels: &str = "O8";

pub struct CGDisplayMode(CGDisplayModeRef);

impl Drop for CGDisplayMode {
    fn drop(&mut self) {
        unsafe { CGDisplayModeRelease(self.0) }
    }
}

impl_TCFType!(CGDisplayMode, CGDisplayModeRef, CGDisplayModeGetTypeID);
impl_CFTypeDescription!(CGDisplayMode);

impl CGDisplayMode {
    pub fn width(&self) -> usize {
        unsafe { CGDisplayModeGetWidth(self.as_concrete_TypeRef()) as usize }
    }

    pub fn height(&self) -> usize {
        unsafe { CGDisplayModeGetHeight(self.as_concrete_TypeRef()) as usize }
    }

    pub fn pixel_width(&self) -> usize {
        unsafe { CGDisplayModeGetPixelWidth(self.as_concrete_TypeRef()) as usize }
    }

    pub fn pixel_height(&self) -> usize {
        unsafe { CGDisplayModeGetPixelHeight(self.as_concrete_TypeRef()) as usize }
    }

    pub fn copy_pixel_encoding(&self) -> Option<CFString> {
        unsafe {
            let encoding = CGDisplayModeCopyPixelEncoding(self.as_concrete_TypeRef());
            if encoding.is_null() {
                None
            } else {
                Some(TCFType::wrap_under_create_rule(encoding))
            }
        }
    }

    pub fn bit_depth(&self) -> usize {
        match self.copy_pixel_encoding() {
            None => 0,
            Some(encoding) => match encoding.to_string().as_str() {
                kIO32BitFloatPixels => 96,
                kIO64BitDirectPixels => 64,
                kIO16BitFloatPixels => 48,
                kIO30BitDirectPixels => 30,
                IO32BitDirectPixels => 32,
                IO16BitDirectPixels => 16,
                IOYUV422Pixels => 16,
                IO8BitOverlayPixels => 8,
                IO8BitIndexedPixels => 8,
                IO4BitIndexedPixels => 4,
                IO2BitIndexedPixels => 2,
                IO1BitIndexedPixels => 1,
                _ => 0,
            },
        }
    }

    pub fn refresh_rate(&self) -> f64 {
        unsafe { CGDisplayModeGetRefreshRate(self.as_concrete_TypeRef()) }
    }

    pub fn io_flags(&self) -> u32 {
        unsafe { CGDisplayModeGetIOFlags(self.as_concrete_TypeRef()) }
    }

    pub fn io_display_mode_id(&self) -> i32 {
        unsafe { CGDisplayModeGetIODisplayModeID(self.as_concrete_TypeRef()) }
    }

    pub fn is_usable_for_desktop_gui(&self) -> bool {
        unsafe { CGDisplayModeIsUsableForDesktopGUI(self.as_concrete_TypeRef()) }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CGDisplay {
    pub id: CGDirectDisplayID,
}

impl CGDisplay {
    pub fn new(id: CGDirectDisplayID) -> Self {
        CGDisplay {
            id,
        }
    }

    pub fn main() -> Self {
        unsafe { CGDisplay::new(CGMainDisplayID()) }
    }

    pub fn copy_display_modes(&self) -> Option<CFArray<CGDisplayMode>> {
        unsafe {
            let modes = CGDisplayCopyAllDisplayModes(self.id, null());
            if modes.is_null() {
                None
            } else {
                Some(TCFType::wrap_under_create_rule(modes))
            }
        }
    }

    pub fn copy_display_mode(&self) -> Option<CGDisplayMode> {
        unsafe {
            let mode = CGDisplayCopyDisplayMode(self.id);
            if mode.is_null() {
                None
            } else {
                Some(TCFType::wrap_under_create_rule(mode))
            }
        }
    }

    pub fn bounds(&self) -> CGRect {
        unsafe { CGDisplayBounds(self.id) }
    }

    pub fn pixels_wide(&self) -> usize {
        unsafe { CGDisplayPixelsWide(self.id) as usize }
    }

    pub fn pixels_high(&self) -> usize {
        unsafe { CGDisplayPixelsHigh(self.id) as usize }
    }

    pub fn set_display_mode(&self, mode: &CGDisplayMode) -> Result<(), CGError> {
        let result = unsafe { CGDisplaySetDisplayMode(self.id, mode.as_concrete_TypeRef(), null()) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    pub fn capture(&self) -> Result<(), CGError> {
        let result = unsafe { CGDisplayCapture(self.id) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    pub fn capture_with_options(&self, options: CGCaptureOptions) -> Result<(), CGError> {
        let result = unsafe { CGDisplayCaptureWithOptions(self.id, options) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    #[cfg(feature = "window")]
    pub fn shielding_window_id(&self) -> CGWindowID {
        unsafe { CGShieldingWindowID(self.id) }
    }

    pub fn new_image(&self) -> Option<CGImage> {
        unsafe {
            let image = CGDisplayCreateImage(self.id);
            if image.is_null() {
                None
            } else {
                Some(TCFType::wrap_under_create_rule(image))
            }
        }
    }

    pub fn new_image_for_rect(&self, rect: CGRect) -> Option<CGImage> {
        unsafe {
            let image = CGDisplayCreateImageForRect(self.id, rect);
            if image.is_null() {
                None
            } else {
                Some(TCFType::wrap_under_create_rule(image))
            }
        }
    }

    pub fn hide_cursor(&self) -> Result<(), CGError> {
        let result = unsafe { CGDisplayHideCursor(self.id) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    pub fn show_cursor(&self) -> Result<(), CGError> {
        let result = unsafe { CGDisplayShowCursor(self.id) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    pub fn move_cursor_to_point(&self, point: CGPoint) -> Result<(), CGError> {
        let result = unsafe { CGDisplayMoveCursorToPoint(self.id, point) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    pub fn warp_mouse_cursor_position(&self, point: CGPoint) -> Result<(), CGError> {
        let result = unsafe { CGWarpMouseCursorPosition(point) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    pub fn get_drawing_context(&self) -> Option<CGContext> {
        unsafe {
            let context = CGDisplayGetDrawingContext(self.id);
            if context.is_null() {
                None
            } else {
                Some(TCFType::wrap_under_create_rule(context))
            }
        }
    }

    // display configuration

    pub fn set_stereo_operation(&self, stereo: bool, force_blue_line: bool, option: CGConfigureOption) -> Result<(), CGError> {
        let result = unsafe { CGDisplaySetStereoOperation(self.id, stereo as _, force_blue_line as _, option) };
        if result == CGError::Success {
            Ok(())
        } else {
            Err(result)
        }
    }

    pub fn is_active(&self) -> bool {
        unsafe { CGDisplayIsActive(self.id) != 0 }
    }

    pub fn is_asleep(&self) -> bool {
        unsafe { CGDisplayIsAsleep(self.id) != 0 }
    }

    pub fn is_online(&self) -> bool {
        unsafe { CGDisplayIsOnline(self.id) != 0 }
    }

    pub fn is_main(&self) -> bool {
        unsafe { CGDisplayIsMain(self.id) != 0 }
    }

    pub fn is_built_in(&self) -> bool {
        unsafe { CGDisplayIsBuiltin(self.id) != 0 }
    }

    pub fn is_in_mirror_set(&self) -> bool {
        unsafe { CGDisplayIsInMirrorSet(self.id) != 0 }
    }

    pub fn is_always_in_mirror_set(&self) -> bool {
        unsafe { CGDisplayIsInHWMirrorSet(self.id) != 0 }
    }

    pub fn is_in_hw_mirror_set(&self) -> bool {
        unsafe { CGDisplayIsInHWMirrorSet(self.id) != 0 }
    }

    pub fn is_stereo(&self) -> bool {
        unsafe { CGDisplayIsStereo(self.id) != 0 }
    }

    pub fn uses_opengl_acceleration(&self) -> bool {
        unsafe { CGDisplayUsesOpenGLAcceleration(self.id) != 0 }
    }

    pub fn mirrors_display(&self) -> Self {
        unsafe { CGDisplay::new(CGDisplayMirrorsDisplay(self.id)) }
    }

    pub fn primary_display(&self) -> Self {
        unsafe { CGDisplay::new(CGDisplayPrimaryDisplay(self.id)) }
    }

    pub fn unit_number(&self) -> u32 {
        unsafe { CGDisplayUnitNumber(self.id) }
    }

    pub fn vendor_number(&self) -> u32 {
        unsafe { CGDisplayVendorNumber(self.id) }
    }

    pub fn model_number(&self) -> u32 {
        unsafe { CGDisplayModelNumber(self.id) }
    }

    pub fn serial_number(&self) -> u32 {
        unsafe { CGDisplaySerialNumber(self.id) }
    }

    pub fn screen_size(&self) -> CGSize {
        unsafe { CGDisplayScreenSize(self.id) }
    }

    pub fn rotation(&self) -> c_double {
        unsafe { CGDisplayRotation(self.id) }
    }

    pub fn copy_color_space(&self) -> Option<CGColorSpace> {
        unsafe {
            let space = CGDisplayCopyColorSpace(self.id);
            if space.is_null() {
                None
            } else {
                Some(TCFType::wrap_under_create_rule(space))
            }
        }
    }
}

fn get_displays_from_ids(ids: &mut Vec<CGDirectDisplayID>, count: usize) -> Option<Vec<CGDisplay>> {
    ids.truncate(count as usize);
    Some(ids.into_iter().map(|id| CGDisplay::new(*id)).collect())
}

pub fn get_displays_with_point(point: CGPoint, max_displays: usize) -> Option<Vec<CGDisplay>> {
    unsafe {
        let mut count = max_displays as u32;
        let mut ids: Vec<CGDirectDisplayID> = vec![0; max_displays];
        let result = CGGetDisplaysWithPoint(point, count, ids.as_mut_ptr(), &mut count);
        if result == CGError::Success {
            get_displays_from_ids(&mut ids, count as usize)
        } else {
            None
        }
    }
}

pub fn get_displays_with_rect(rect: CGRect, max_displays: usize) -> Option<Vec<CGDisplay>> {
    unsafe {
        let mut count = max_displays as u32;
        let mut ids = vec![0; max_displays];
        let result = CGGetDisplaysWithRect(rect, count, ids.as_mut_ptr(), &mut count);
        if result == CGError::Success {
            get_displays_from_ids(&mut ids, count as usize)
        } else {
            None
        }
    }
}

pub fn get_displays_with_opengl_display_mask(mask: CGOpenGLDisplayMask, max_displays: usize) -> Option<Vec<CGDisplay>> {
    unsafe {
        let mut count = max_displays as u32;
        let mut ids = vec![0; max_displays];
        let result = CGGetDisplaysWithOpenGLDisplayMask(mask, count, ids.as_mut_ptr(), &mut count);
        if result == CGError::Success {
            get_displays_from_ids(&mut ids, count as usize)
        } else {
            None
        }
    }
}

pub fn get_active_display_list(max_displays: usize) -> Option<Vec<CGDisplay>> {
    unsafe {
        let mut count = max_displays as u32;
        let mut ids = vec![0; max_displays];
        let result = CGGetActiveDisplayList(count, ids.as_mut_ptr(), &mut count);
        if result == CGError::Success {
            get_displays_from_ids(&mut ids, count as usize)
        } else {
            None
        }
    }
}

pub fn get_online_display_list(max_displays: usize) -> Option<Vec<CGDisplay>> {
    unsafe {
        let mut count = max_displays as u32;
        let mut ids = vec![0; max_displays];
        let result = CGGetOnlineDisplayList(count, ids.as_mut_ptr(), &mut count);
        if result == CGError::Success {
            get_displays_from_ids(&mut ids, count as usize)
        } else {
            None
        }
    }
}

pub fn display_id_to_opengl_display_mask(display_id: CGDirectDisplayID) -> CGOpenGLDisplayMask {
    unsafe { CGDisplayIDToOpenGLDisplayMask(display_id) }
}

pub fn opengl_display_mask_to_display_id(mask: CGOpenGLDisplayMask) -> CGDirectDisplayID {
    unsafe { CGOpenGLDisplayMaskToDisplayID(mask) }
}

pub fn capture_all_displays() -> Result<(), CGError> {
    let result = unsafe { CGCaptureAllDisplays() };
    if result == CGError::Success {
        Ok(())
    } else {
        Err(result)
    }
}

pub fn capture_all_displays_with_options(options: CGCaptureOptions) -> Result<(), CGError> {
    let result = unsafe { CGCaptureAllDisplaysWithOptions(options) };
    if result == CGError::Success {
        Ok(())
    } else {
        Err(result)
    }
}

pub fn release_all_displays() -> Result<(), CGError> {
    let result = unsafe { CGReleaseAllDisplays() };
    if result == CGError::Success {
        Ok(())
    } else {
        Err(result)
    }
}