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
//! Querying display information.

use static_assertions::assert_not_impl_all;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::mem::MaybeUninit;

use crate::geo::Rect;
use crate::{bind, Video};

pub use self::mode::*;

mod mode;

/// Densities per inch of the display.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Dpi {
    /// A diagonal density per inch.
    pub ddpi: f32,
    /// A horizontal density per inch.
    pub hdpi: f32,
    /// A vertical density per inch.
    pub vdpi: f32,
}

/// A display queries bounds, name, dpi and modes.
pub struct Display<'video> {
    index: i32,
    _phantom: PhantomData<&'video Video<'video>>,
}

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

assert_not_impl_all!(Display: Send, Sync);

impl<'video> Display<'video> {
    pub(super) fn new(index: i32, _: &'video Video) -> Self {
        Self {
            index,
            _phantom: PhantomData,
        }
    }

    /// Returns the bounds of the display if available.
    #[must_use]
    pub fn bounds(&self) -> Option<Rect> {
        let mut raw_rect = MaybeUninit::uninit();
        let ret = unsafe { bind::SDL_GetDisplayBounds(self.index, raw_rect.as_mut_ptr()) };
        (ret == 0).then(|| unsafe { raw_rect.assume_init() }.into())
    }

    /// Returns the usable bounds of the display if available.
    #[must_use]
    pub fn usable_bounds(&self) -> Option<Rect> {
        let mut raw_rect = MaybeUninit::uninit();
        let ret = unsafe { bind::SDL_GetDisplayUsableBounds(self.index, raw_rect.as_mut_ptr()) };
        (ret == 0).then(|| unsafe { raw_rect.assume_init() }.into())
    }

    /// Returns the dpi information [`Dpi`] if available.
    #[must_use]
    pub fn dpi(&self) -> Option<Dpi> {
        let mut dpi = Dpi {
            ddpi: 0.0,
            hdpi: 0.0,
            vdpi: 0.0,
        };
        let ret = unsafe {
            bind::SDL_GetDisplayDPI(self.index, &mut dpi.ddpi, &mut dpi.hdpi, &mut dpi.vdpi)
        };

        (ret == 0).then(|| dpi)
    }

    /// Returns the name of the display.
    #[must_use]
    pub fn name(&self) -> &str {
        let raw_str = unsafe { bind::SDL_GetDisplayName(self.index) };
        unsafe { CStr::from_ptr(raw_str) }
            .to_str()
            .expect("the display name was not utf8")
    }

    /// Returns the modes supported by the display.
    #[must_use]
    pub fn modes(&self) -> Vec<Mode> {
        let ret = unsafe { bind::SDL_GetNumDisplayModes(self.index) };
        if ret < 0 {
            return vec![];
        }
        (0..ret)
            .map(|idx| {
                let mut raw = MaybeUninit::uninit();
                let ret = unsafe { bind::SDL_GetDisplayMode(self.index, idx, raw.as_mut_ptr()) };
                debug_assert!(ret != 0);
                let mode = unsafe { raw.assume_init() };
                Mode::new(mode)
            })
            .collect()
    }

    /// Returns the current mode of the display.
    #[must_use]
    pub fn current_mode(&self) -> Mode {
        let mut raw = MaybeUninit::uninit();
        let ret = unsafe { bind::SDL_GetCurrentDisplayMode(self.index, raw.as_mut_ptr()) };
        debug_assert!(ret != 0);
        let mode = unsafe { raw.assume_init() };
        Mode::new(mode)
    }

    /// Returns the original mode of the display.
    #[must_use]
    pub fn original_mode(&self) -> Mode {
        let mut raw = MaybeUninit::uninit();
        let ret = unsafe { bind::SDL_GetDesktopDisplayMode(self.index, raw.as_mut_ptr()) };
        debug_assert!(ret != 0);
        let mode = unsafe { raw.assume_init() };
        Mode::new(mode)
    }
}