knoll 0.4.0

A command-line tool for configuring macOS displays
Documentation
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
#![allow(dead_code)]
///! This module provides "safer" access to all of the low-level macOS APIs.  
///
/// I suppose I could have considered using the core_graphics crate
/// (https://docs.rs/core-graphics/latest/core_graphics), but it doesn't
/// currently provide "safe" versions of some of these functions, doesn't
/// provide some of the public APIs and none of the private APIs at all.
/// So it would have only saved me some of the extern declarations.
use std::os::raw::c_int;
use std::os::raw::c_void;
use std::ptr::{null, null_mut};

use static_assertions::const_assert;

////////////////////////////////////////////////////////////////////////////////
// Types common to the unsafe and safe APIs

type CGDirectDisplayID = u32;

/// Wrapper so that we do not need to expose the actual implementation.
#[derive(Debug, Default, Hash, Eq, PartialEq, Clone, Copy)]
#[repr(C)]
pub struct DisplayID {
    id: CGDirectDisplayID,
}

/// https://github.com/NUIKit/CGSInternal/blob/master/CGSDisplays.h
#[derive(Debug, Default, Clone)]
#[repr(C)]
pub struct CGSDisplayModeDescription {
    pub mode: i32,
    flags: u32,
    pub width: u32,
    pub height: u32,
    pub depth: u32,
    // This is split into two separate fields as compared CGSDisplays.h
    // because derive(Default) cannot handle arrays 42 elements long.
    // Switching to u64 will change the struct layout.
    dc2: [u32; 21],
    dc3: [u32; 21],
    dc4: u16,
    pub freq: u16,
    dc5: [u8; 16], // dc5[4] seems to also contain the same value as mode.
    // If 2.0, the mode is scaled.
    pub scale: f32,
}

// Sanity check that the struct has the expected size.
const_assert!(std::mem::size_of::<CGSDisplayModeDescription>() == 0xD4);

/// https://developer.apple.com/documentation/corefoundation/cgpoint
#[derive(Debug)]
#[repr(C)]
pub struct CGPoint {
    pub x: f64,
    pub y: f64,
}

/// https://developer.apple.com/documentation/corefoundation/cgrect
#[derive(Debug)]
#[repr(C)]
pub struct CGRect {
    pub origin: CGPoint,
    pub size: CGPoint,
}

/// https://developer.apple.com/documentation/coregraphics/cgerror/
#[derive(Debug, Eq, PartialEq)]
#[repr(i32)]
#[allow(non_camel_case_types)]
pub enum CGError {
    cannotComplete = 1004,
    failure = 1000,
    illegalArgument = 1001,
    invalidConnection = 1002,
    invalidContext = 1003,
    invalidOperation = 1010,
    noneAvailable = 1011,
    notImplemented = 1006,
    rangeCheck = 1007,
    success = 0,
    typeCheck = 1008,
}

// https://developer.apple.com/documentation/coregraphics/cgconfigureoption
#[derive(Debug)]
#[repr(i32)]
#[allow(non_camel_case_types)]
pub enum CGConfigureOption {
    // Currently not used but included for possible future use.
    #[allow(unused)]
    kCGConfigureForAppOnly = 0,
    // Currently not used but included for possible future use.
    #[allow(unused)]
    kCGConfigureForSession = 1,
    kCGConfigurePermanently = 2,
}

#[allow(non_upper_case_globals)]
pub const kCFAllocatorDefault: *const c_void = null();

// TODO I could consider adding wrapper struct to hide implementations
//   of some of these types, but as nearly all of these are opaque pointers,
//   the likelihood of forging meaningful values that do not immediately
//   cause a segfault seems unlikely.

pub type CGDisplayConfigRef = *mut c_void;
pub type CGDisplayModeRef = *const c_void;
pub type CGDisplayFadeInterval = f32;

/// Wrapper over CFArray.
pub type CGDisplayModeArray = *const c_void;

pub type CFArray = *const c_void;
pub type CFTypeRef = *const c_void;
pub type CFAllocator = *const c_void;
pub type CFUUID = *const c_void;
pub type CFIndex = c_int;
pub type CFString = *const c_void;
pub type CFDictionary = *const c_void;
pub type CFStringEncoding = u32;

// https://developer.apple.com/documentation/corefoundation/cfstringbuiltinencodings
#[derive(Debug)]
#[repr(i32)]
#[allow(non_camel_case_types)]
pub enum CFStringBuiltInEncodings {
    ASCII = 1536,
    // Currently not used but included for possible future use.
    UTF8 = 134217984,
}

/// Implement From to expose a safer conversion.
impl From<CFStringBuiltInEncodings> for CFStringEncoding {
    fn from(value: CFStringBuiltInEncodings) -> Self {
        value as Self
    }
}

// TODO Add values or remove these constants ?
// kDisplayProductIDGeneric
// kDisplayVendorIDUnknown

#[link(name = "AppKit", kind = "framework")]
#[link(name = "IOKit", kind = "framework")]
#[link(name = "ApplicationServices", kind = "framework")]
#[link(name = "DisplayServices", kind = "framework")]
#[link(name = "CoreDisplay", kind = "framework")]
#[link(name = "OSD", kind = "framework")]
#[link(name = "MonitorPanel", kind = "framework")]
#[link(name = "SkyLight", kind = "framework")]
unsafe extern "C" {
    /// https://developer.apple.com/documentation/corefoundation/1521153-cfrelease
    fn CFRelease(cf: CFTypeRef);

    /// https://developer.apple.com/documentation/corefoundation/1542721-cfstringgetcstring/
    fn CFStringGetCString(
        string: CFString,
        buffer: *mut u8,
        buffer_size: CFIndex,
        encoding: CFStringEncoding,
    ) -> bool;

    /// https://developer.apple.com/documentation/corefoundation/1543625-cfuuidcreatestring/
    fn CFUUIDCreateString(allocator: CFAllocator, uuid: CFUUID) -> CFString;

    /// https://developer.apple.com/documentation/corefoundation/1388772-cfarraygetcount/
    fn CFArrayGetCount(array: CFArray) -> usize;

    /// https://developer.apple.com/documentation/corefoundation/1388767-cfarraygetvalueatindex/
    fn CFArrayGetValueAtIndex(array: CFArray, idx: CFIndex) -> *const c_void;

    /// https://developer.apple.com/documentation/coregraphics/1454964-cggetonlinedisplaylist/
    fn CGGetOnlineDisplayList(
        max_displays: u32,
        online_displays: *mut CGDirectDisplayID,
        display_count: *mut u32,
    ) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1454603-cggetactivedisplaylist/
    fn CGGetActiveDisplayList(
        max_displays: u32,
        active_displays: *mut CGDirectDisplayID,
        display_count: *mut u32,
    ) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1455409-cgdisplayserialnumber
    /// Returns 0x0 if there is no encoded serial number or it cannot be identified
    fn CGDisplaySerialNumber(display_id: CGDirectDisplayID) -> u32;

    /// https://developer.apple.com/documentation/coregraphics/1454149-cgdisplaymodelnumber
    /// Returns kDisplayProductIDGeneric if it cannot be identified.
    fn CGDisplayModelNumber(display_id: CGDirectDisplayID) -> u32;

    /// https://developer.apple.com/documentation/coregraphics/1455233-cgdisplayvendornumber
    /// Returns kDisplayVendorIDUnknown if the vendor cannot be identified.
    fn CGDisplayVendorNumber(display_id: CGDirectDisplayID) -> u32;

    /// https://developer.apple.com/documentation/coregraphics/1455299-cgdisplayrotation
    fn CGDisplayRotation(display_id: CGDirectDisplayID) -> f64;

    /// https://developer.apple.com/documentation/coregraphics/1456395-cgdisplaybounds/
    fn CGDisplayBounds(display_id: CGDirectDisplayID) -> CGRect;

    /// https://developer.apple.com/documentation/coregraphics/1455222-cgdisplayisactive
    fn CGDisplayIsActive(display_id: CGDirectDisplayID) -> bool;

    /// https://developer.apple.com/documentation/coregraphics/1455558-cgdisplayisinmirrorset
    fn CGDisplayIsInMirrorSet(display_id: CGDirectDisplayID) -> bool;

    /// https://developer.apple.com/documentation/colorsync/1458801-cgdisplaycreateuuidfromdisplayid/
    fn CGDisplayCreateUUIDFromDisplayID(display_id: CGDirectDisplayID) -> CFUUID;

    /// https://developer.apple.com/documentation/coregraphics/1562069-cgdisplaymoderelease
    fn CGDisplayModeRelease(mode: CGDisplayModeRef);

    /// https://developer.apple.com/documentation/coregraphics/1455537-cgdisplaycopyalldisplaymodes
    fn CGDisplayCopyAllDisplayModes(
        display_id: CGDirectDisplayID,
        options: CFDictionary,
    ) -> CFArray;

    /// https://developer.apple.com/documentation/coregraphics/1454099-cgdisplaycopydisplaymode
    /// Returns null pointer for invalid input display.  Caller is response for releasing.
    fn CGDisplayCopyDisplayMode(display_id: CGDirectDisplayID) -> CGDisplayModeRef;

    /// https://developer.apple.com/documentation/coregraphics/1454442-cgdisplaymodegetwidth/
    fn CGDisplayModeGetWidth(mode: CGDisplayModeRef) -> usize;

    /// https://developer.apple.com/documentation/coregraphics/1455380-cgdisplaymodegetheight
    fn CGDisplayModeGetHeight(mode: CGDisplayModeRef) -> usize;

    /// https://developer.apple.com/documentation/coregraphics/1454661-cgdisplaymodegetrefreshrate
    fn CGDisplayModeGetRefreshRate(mode: CGDisplayModeRef) -> f64;

    /// https://developer.apple.com/documentation/coregraphics/1454092-cgdisplaymodegetioflags
    fn CGDisplayModeGetIOFlags(mode: CGDisplayModeRef) -> u32;

    /// https://developer.apple.com/documentation/coregraphics/1454728-cgdisplaymodegetiodisplaymodeid
    /// These mode numbers do appear to correspond to those reported by
    /// `CGSGetCurrentDisplayMode`, etc.
    fn CGDisplayModeGetIODisplayModeID(mode: CGDisplayModeRef) -> i32;

    /// https://developer.apple.com/documentation/coregraphics/1454756-cgdisplaymodegetpixelwidth/
    fn CGDisplayModeGetPixelWidth(mode: CGDisplayModeRef) -> usize;

    /// https://developer.apple.com/documentation/coregraphics/1456406-cgdisplaymodegetpixelheight/
    fn CGDisplayModeGetPixelHeight(mode: CGDisplayModeRef) -> usize;

    /// https://developer.apple.com/documentation/coregraphics/cgdisplaymode/1454928-isusablefordesktopgui
    fn CGDisplayModeIsUsableForDesktopGUI(mode: CGDisplayModeRef) -> bool;

    /// https://developer.apple.com/documentation/coregraphics/1455235-cgbegindisplayconfiguration/
    /// Pointer allowed to contain garbage on input?
    fn CGBeginDisplayConfiguration(config: *mut CGDisplayConfigRef) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1454488-cgcompletedisplayconfiguration/
    fn CGCompleteDisplayConfiguration(
        config: CGDisplayConfigRef,
        option: CGConfigureOption,
    ) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1455522-cgcanceldisplayconfiguration
    fn CGCancelDisplayConfiguration(config: CGDisplayConfigRef) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1454103-cgconfiguredisplayfadeeffect
    fn CGConfigureDisplayFadeEffect(
        config: CGDisplayConfigRef,
        fade_out_seconds: CGDisplayFadeInterval,
        fade_in_seconds: CGDisplayFadeInterval,
        fade_red: f32,
        fade_green: f32,
        fade_blue: f32,
    ) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1454090-cgconfiguredisplayorigin/
    /// Setting the origin to 0,0 will make the display the main.
    /// Doesn't consume ref, so take reference
    fn CGConfigureDisplayOrigin(
        config: CGDisplayConfigRef,
        display_id: CGDirectDisplayID,
        x: i32,
        y: i32,
    ) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1454273-cgconfiguredisplaywithdisplaymod
    /// options is reserved for future use and should always be null at present.
    fn CGConfigureDisplayWithDisplayMode(
        config: CGDisplayConfigRef,
        display_id: CGDirectDisplayID,
        mode: CGDisplayModeRef,
        options: CFDictionary,
    ) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1454531-cgconfiguredisplaymirrorofdispla
    /// Doesn't consume ref, so take reference
    fn CGConfigureDisplayMirrorOfDisplay(
        config: CGDisplayConfigRef,
        display_id: CGDirectDisplayID,
        master_id: CGDirectDisplayID,
    ) -> CGError;

    /// https://developer.apple.com/documentation/coregraphics/1455336-cgdisplayregisterreconfiguration
    fn CGDisplayRegisterReconfigurationCallback(
        callback: extern "C" fn(),
        user_info: *mut c_void,
    ) -> CGError;

    // TODO Callbacks do not appear to leak if process exits, but might good
    //   to clean up?  Need to determine appropriate location to call.
    // CGDisplayRemoveReconfigurationCallback

    /// https://developer.apple.com/documentation/coregraphics/cgdisplaymirrorsdisplay(_:)
    fn CGDisplayMirrorsDisplay(display_id: CGDirectDisplayID) -> CGDirectDisplayID;

    /// https://developer.apple.com/documentation/appkit/1428475-nsapplicationload
    pub fn NSApplicationLoad() -> bool;
    /// https://developer.apple.com/documentation/corefoundation/1542011-cfrunlooprun/
    pub fn CFRunLoopRun();

    // Private Core Graphics APIs ////////////////////////////////////////////
    // https://github.com/NUIKit/CGSInternal/blob/master/CGSDisplays.h

    fn CGSGetCurrentDisplayMode(display: CGDirectDisplayID, mode_num: *mut c_int) -> CGError;

    fn CGSGetNumberOfDisplayModes(display: CGDirectDisplayID, num_modes: *mut c_int) -> CGError;

    fn CGSGetDisplayModeDescriptionOfLength(
        display_id: CGDirectDisplayID,
        index: c_int,
        mode: *mut CGSDisplayModeDescription,
        length: c_int,
    ) -> CGError;

    // Suspect these do not consume their arguments.

    fn CGSConfigureDisplayMode(
        config: CGDisplayConfigRef,
        display_id: CGDirectDisplayID,
        mode_num: c_int,
    ) -> CGError;

    fn CGSConfigureDisplayEnabled(
        config: CGDisplayConfigRef,
        display_id: CGDirectDisplayID,
        enabled: bool,
    ) -> CGError;

    /// Discovered through disassembly of the SkyLight framework.
    fn SLSSetDisplayRotation(display_id: CGDirectDisplayID, rotation: i32) -> CGError;
    fn SLSSetDisplayBrightness(display_id: CGDirectDisplayID, brightness: i32) -> CGError;

    /// Discovered in https://github.com/MonitorControl/MonitorControl/blob/81170cd7fc582470f3bb75feeb18d0405c2a9a6e/MonitorControl/Support/Bridging-Header.h#L17
    fn DisplayServicesGetBrightness(display_id: CGDirectDisplayID, brightness: *mut f32)
    -> CGError;
    fn DisplayServicesSetBrightness(display_id: CGDirectDisplayID, brightness: f32) -> CGError;
}

////////////////////////////////////////////////////////////////////////////////

pub fn cf_release(cf: CFTypeRef) {
    unsafe { CFRelease(cf) }
}

pub fn cf_string_get_cstring(
    string: CFString,
    buffer: &mut [u8],
    encoding: CFStringEncoding,
) -> bool {
    unsafe { CFStringGetCString(string, buffer.as_mut_ptr(), buffer.len() as c_int, encoding) }
}

pub fn cf_uuid_create_string(allocator: CFAllocator, uuid: CFUUID) -> CFString {
    unsafe { CFUUIDCreateString(allocator, uuid) }
}

pub fn cg_get_online_display_list(
    online_displays: &mut [DisplayID],
    display_count: &mut u32,
) -> CGError {
    unsafe {
        CGGetOnlineDisplayList(
            online_displays.len() as u32,
            online_displays.as_mut_ptr() as *mut CGDirectDisplayID,
            display_count,
        )
    }
}

pub fn cg_get_active_display_list(
    active_displays: &mut [DisplayID],
    display_count: &mut u32,
) -> CGError {
    unsafe {
        CGGetActiveDisplayList(
            active_displays.len() as u32,
            active_displays.as_mut_ptr() as *mut CGDirectDisplayID,
            display_count,
        )
    }
}

pub fn cg_display_serial_number(display_id: DisplayID) -> u32 {
    unsafe { CGDisplaySerialNumber(display_id.id) }
}

pub fn cg_display_model_number(display_id: DisplayID) -> u32 {
    unsafe { CGDisplayModelNumber(display_id.id) }
}

pub fn cg_display_vendor_number(display_id: DisplayID) -> u32 {
    unsafe { CGDisplayModelNumber(display_id.id) }
}

pub fn cg_display_rotation(display_id: DisplayID) -> f64 {
    unsafe { CGDisplayRotation(display_id.id) }
}

pub fn cg_display_bounds(display_id: DisplayID) -> CGRect {
    unsafe { CGDisplayBounds(display_id.id) }
}

pub fn cg_display_is_active(display_id: DisplayID) -> bool {
    unsafe { CGDisplayIsActive(display_id.id) }
}

pub fn cg_display_is_in_mirror_set(display_id: DisplayID) -> bool {
    unsafe { CGDisplayIsInMirrorSet(display_id.id) }
}

pub fn cg_display_create_uuid_from_display_id(display_id: DisplayID) -> CFUUID {
    unsafe { CGDisplayCreateUUIDFromDisplayID(display_id.id) }
}

pub fn cg_display_mode_release(mode: CGDisplayModeRef) {
    unsafe { CGDisplayModeRelease(mode) }
}

pub fn cg_display_copy_all_display_modes(display_id: DisplayID) -> CGDisplayModeArray {
    unsafe { CGDisplayCopyAllDisplayModes(display_id.id, null()) }
}

// Derived helpers
pub fn cg_display_modes_get_count(modes: CGDisplayModeArray) -> usize {
    unsafe { CFArrayGetCount(modes) }
}

pub fn cg_display_modes_at_index(modes: CGDisplayModeArray, idx: CFIndex) -> CGDisplayModeRef {
    unsafe { CFArrayGetValueAtIndex(modes, idx) }
}

pub fn cg_display_copy_display_mode(display_id: DisplayID) -> Option<CGDisplayModeRef> {
    unsafe {
        let mode = CGDisplayCopyDisplayMode(display_id.id);
        if !mode.is_null() { Some(mode) } else { None }
    }
}

pub fn cg_display_mode_get_width(mode: &CGDisplayModeRef) -> usize {
    unsafe { CGDisplayModeGetWidth(*mode) }
}

pub fn cg_display_mode_get_height(mode: &CGDisplayModeRef) -> usize {
    unsafe { CGDisplayModeGetHeight(*mode) }
}

pub fn cg_display_mode_get_pixel_width(mode: &CGDisplayModeRef) -> usize {
    unsafe { CGDisplayModeGetPixelWidth(*mode) }
}

pub fn cg_display_mode_get_pixel_height(mode: &CGDisplayModeRef) -> usize {
    unsafe { CGDisplayModeGetPixelHeight(*mode) }
}

pub fn cg_display_mode_get_refresh_rate(mode: &CGDisplayModeRef) -> f64 {
    unsafe { CGDisplayModeGetRefreshRate(*mode) }
}

pub fn cg_display_mode_get_io_flags(mode: &CGDisplayModeRef) -> u32 {
    unsafe { CGDisplayModeGetIOFlags(*mode) }
}

pub fn cg_display_mode_get_io_display_mode_id(mode: &CGDisplayModeRef) -> i32 {
    unsafe { CGDisplayModeGetIODisplayModeID(*mode) }
}

pub fn cg_display_mode_is_usable_for_desktop_gui(mode: &CGDisplayModeRef) -> bool {
    unsafe { CGDisplayModeIsUsableForDesktopGUI(*mode) }
}

pub fn cg_begin_display_configuration() -> Result<CGDisplayConfigRef, CGError> {
    unsafe {
        let mut config_ref: CGDisplayConfigRef = null_mut();
        let error = CGBeginDisplayConfiguration(&mut config_ref);
        match error {
            CGError::success => Ok(config_ref),
            _ => Err(error),
        }
    }
}

pub fn cg_complete_display_configuration(
    config_ref: CGDisplayConfigRef,
    option: CGConfigureOption,
) -> CGError {
    unsafe { CGCompleteDisplayConfiguration(config_ref, option) }
}

pub fn cg_cancel_display_configuration(config_ref: CGDisplayConfigRef) -> CGError {
    unsafe { CGCancelDisplayConfiguration(config_ref) }
}

pub fn cg_configure_display_origin(
    config_ref: &CGDisplayConfigRef,
    display_id: DisplayID,
    x: i32,
    y: i32,
) -> CGError {
    unsafe { CGConfigureDisplayOrigin(*config_ref, display_id.id, x, y) }
}

pub fn cg_configure_display_with_display_mode(
    config_ref: &CGDisplayConfigRef,
    display_id: DisplayID,
    mode: &CGDisplayModeRef,
) -> CGError {
    unsafe { CGConfigureDisplayWithDisplayMode(*config_ref, display_id.id, *mode, null()) }
}

pub fn cg_configure_display_fade_effect(
    config_ref: &CGDisplayConfigRef,
    fade_out_seconds: CGDisplayFadeInterval,
    fade_in_seconds: CGDisplayFadeInterval,
    fade_red: f32,
    fade_green: f32,
    fade_blue: f32,
) -> CGError {
    unsafe {
        CGConfigureDisplayFadeEffect(
            *config_ref,
            fade_out_seconds,
            fade_in_seconds,
            fade_red,
            fade_green,
            fade_blue,
        )
    }
}

pub fn cg_configure_display_mirror_of_display(
    config_ref: &CGDisplayConfigRef,
    display_id: DisplayID,
    master_id: Option<DisplayID>,
) -> CGError {
    // In CGDirectDisplay.h kCGNullDirectDisplay is a defined as a
    // preprocess macro to be 0, as such there is no symbol we can
    // link to obtain the value.
    let target_id = master_id.map(|d| d.id).unwrap_or(0);

    unsafe { CGConfigureDisplayMirrorOfDisplay(*config_ref, display_id.id, target_id) }
}

pub fn cg_display_register_reconfiguration_callback(cb: extern "C" fn()) -> CGError {
    unsafe { CGDisplayRegisterReconfigurationCallback(cb, null_mut()) }
}

pub fn ns_application_load() -> bool {
    unsafe { NSApplicationLoad() }
}

pub fn cf_run_loop_run() {
    unsafe { CFRunLoopRun() }
}

pub fn cgs_get_current_display_mode(display: DisplayID, mode: &mut i32) -> CGError {
    unsafe { CGSGetCurrentDisplayMode(display.id, mode) }
}

pub fn cgs_get_number_of_display_modes(display: DisplayID, num: &mut i32) -> CGError {
    unsafe { CGSGetNumberOfDisplayModes(display.id, num) }
}

pub fn cgs_get_display_mode_description(
    display_id: DisplayID,
    index: i32,
    mode_desc: &mut CGSDisplayModeDescription,
) -> CGError {
    unsafe {
        CGSGetDisplayModeDescriptionOfLength(
            display_id.id,
            index,
            mode_desc,
            std::mem::size_of::<CGSDisplayModeDescription>() as c_int,
        )
    }
}

pub fn cgs_configure_display_mode(
    config_ref: &CGDisplayConfigRef,
    display_id: DisplayID,
    mode_num: i32,
) -> CGError {
    unsafe { CGSConfigureDisplayMode(*config_ref, display_id.id, mode_num) }
}

pub fn cgs_configure_display_enabled(
    config_ref: &CGDisplayConfigRef,
    display_id: DisplayID,
    enabled: bool,
) -> CGError {
    unsafe { CGSConfigureDisplayEnabled(*config_ref, display_id.id, enabled) }
}

pub fn cg_display_mirrors_display(display_id: DisplayID) -> Option<DisplayID> {
    let mirrored_id = unsafe { CGDisplayMirrorsDisplay(display_id.id) };
    if mirrored_id == 0 {
        None
    } else {
        Some(DisplayID { id: mirrored_id })
    }
}
pub fn sls_set_display_rotation(display_id: DisplayID, rotation: i32) -> CGError {
    unsafe { SLSSetDisplayRotation(display_id.id, rotation) }
}

pub fn display_services_get_brightness(display_id: DisplayID, brightness: &mut f32) -> CGError {
    unsafe { DisplayServicesGetBrightness(display_id.id, brightness) }
}

pub fn display_services_set_brightness(display_id: DisplayID, brightness: f32) -> CGError {
    unsafe { DisplayServicesSetBrightness(display_id.id, brightness) }
}