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
//!

/// A bitmask of [`NcKey`][crate::NcKey] modifiers.
///
/// # Default
/// *[`NcKeyMod::None`]
///
/// # Flags
/// - [`Shift`][NcKeyMod::Shift]
/// - [`Alt`][NcKeyMod::Alt]
/// - [`Ctrl`][NcKeyMod::Ctrl]
/// - [`Super`][NcKeyMod::Super]
/// - [`Hyper`][NcKeyMod::Hyper]
/// - [`Meta`][NcKeyMod::Meta]
/// - [`CapsLock`][NcKeyMod::CapsLock]
/// - [`NumLock`][NcKeyMod::NumLock]
/// - [`None`][NcKeyMod::None]
/// - [`Mask`][NcKeyMod::Mask]
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct NcKeyMod(pub u32);

/// # Flags
impl NcKeyMod {
    ///
    pub const Shift: Self = Self(c_api::NCKEY_MOD_SHIFT);

    ///
    pub const Alt: Self = Self(c_api::NCKEY_MOD_ALT);

    ///
    pub const Ctrl: Self = Self(c_api::NCKEY_MOD_CTRL);

    ///
    pub const Super: Self = Self(c_api::NCKEY_MOD_SUPER);

    ///
    pub const Hyper: Self = Self(c_api::NCKEY_MOD_HYPER);

    ///
    pub const Meta: Self = Self(c_api::NCKEY_MOD_META);

    ///
    pub const CapsLock: Self = Self(c_api::NCKEY_MOD_CAPSLOCK);

    ///
    pub const NumLock: Self = Self(c_api::NCKEY_MOD_NUMLOCK);

    /// None of the modifiers (all bits set to 0).
    pub const None: Self = Self(0);

    /// The modifier mask (all bits set to 1).
    pub const Mask: Self = Self(u32::MAX);
}

/// # Aliases
impl NcKeyMod {
    ///
    pub const Control: Self = Self::Ctrl;
}

/// # Methods
impl NcKeyMod {
    /// Returns true if no modifiers are present.
    pub fn none_p(&self) -> bool {
        *self == NcKeyMod::None
    }

    /// Returns true if the `Shift` modifier is present.
    pub fn shift_p(&self) -> bool {
        *self & NcKeyMod::Shift != NcKeyMod::None
    }

    /// Returns true if the `Alt` modifier is present.
    pub fn alt_p(&self) -> bool {
        *self & NcKeyMod::Alt != NcKeyMod::None
    }

    /// Returns true if the `Ctrl` modifier is present.
    pub fn ctrl_p(&self) -> bool {
        *self & NcKeyMod::Ctrl != NcKeyMod::None
    }

    /// Returns true if the `Super` modifier is present.
    pub fn super_p(&self) -> bool {
        *self & NcKeyMod::Super != NcKeyMod::None
    }

    /// Returns true if the `Hyper` modifier is present.
    pub fn hyper_p(&self) -> bool {
        *self & NcKeyMod::Hyper != NcKeyMod::None
    }

    /// Returns true if the `Meta` modifier is present.
    pub fn meta_p(&self) -> bool {
        *self & NcKeyMod::Meta != NcKeyMod::None
    }

    /// Returns true if the `CapsLock` modifier is present.
    pub fn capslock_p(&self) -> bool {
        *self & NcKeyMod::CapsLock != NcKeyMod::None
    }

    /// Returns true if the `NumLock` modifier is present.
    pub fn numlock_p(&self) -> bool {
        *self & NcKeyMod::NumLock != NcKeyMod::None
    }
}

mod core_impls {
    use core::fmt;

    #[cfg(not(feature = "std"))]
    use alloc::string::String;

    use super::NcKeyMod;

    impl Default for NcKeyMod {
        fn default() -> Self {
            Self::None
        }
    }

    impl fmt::Display for NcKeyMod {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            let mut string = String::new();

            if self.none_p() {
                string += "None ";
            } else {
                if self.capslock_p() {
                    string += "CapsLock+";
                }
                if self.numlock_p() {
                    string += "NumLock+";
                }
                if self.ctrl_p() {
                    string += "Ctrl+";
                }
                if self.shift_p() {
                    string += "Shift+";
                }
                if self.alt_p() {
                    string += "Alt+";
                }
                if self.meta_p() {
                    string += "Meta+";
                }
                if self.super_p() {
                    string += "Super+";
                }
                if self.hyper_p() {
                    string += "Hyper+";
                }
            }
            string.pop();

            write!(f, "{}", string)
        }
    }

    impl fmt::Debug for NcKeyMod {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "NcKeyMod::{}", self)
        }
    }

    crate::from_primitive![NcKeyMod, u32];
    crate::unit_impl_from![NcKeyMod, u32];

    crate::unit_impl_ops![bitwise; NcKeyMod, u32];
}

pub(crate) mod c_api {
    use crate::c_api::ffi;

    ///
    pub const NCKEY_MOD_SHIFT: u32 = ffi::NCKEY_MOD_SHIFT;

    ///
    pub const NCKEY_MOD_ALT: u32 = ffi::NCKEY_MOD_ALT;

    ///
    pub const NCKEY_MOD_CTRL: u32 = ffi::NCKEY_MOD_CTRL;

    ///
    pub const NCKEY_MOD_SUPER: u32 = ffi::NCKEY_MOD_SUPER;

    ///
    pub const NCKEY_MOD_HYPER: u32 = ffi::NCKEY_MOD_HYPER;

    ///
    pub const NCKEY_MOD_META: u32 = ffi::NCKEY_MOD_META;

    ///
    pub const NCKEY_MOD_CAPSLOCK: u32 = ffi::NCKEY_MOD_CAPSLOCK;

    ///
    pub const NCKEY_MOD_NUMLOCK: u32 = ffi::NCKEY_MOD_NUMLOCK;
}