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
//! `lock_keys` provides a cross platform way for lock keys handling.
//!
//! Supported platforms: Linux ([Xlib](https://en.wikipedia.org/wiki/Xlib) static), Windows ([winuser API](https://docs.microsoft.com/en-us/windows/win32/api/winuser)) and macOS ([IOKit](https://developer.apple.com/documentation/iokit)).
//!
//! # Example
//!
//! The example below shows how to toggle the state of the Capital Lock key:
//!
//! ```rust
//! use lock_keys::*;
//!
//! fn main() {
//!     let lock_key = LockKey::new();
//!     lock_key.enable(LockKeys::CapitalLock).unwrap();
//! }
//! ```

#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "windows")]
mod windows;

#[cfg(target_os = "macos")]
mod macos;

use std::fmt;
use std::io;

#[doc(hidden)]
enum LockKeyHandle {}

// Indicates the lock key state, i.e. enabled/disabled.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum LockKeyState {
    Enabled,
    Disabled,
}

impl LockKeyState {
    pub fn toggle(self) -> Self {
        match self {
            Self::Enabled => Self::Disabled,
            Self::Disabled => Self::Enabled,
        }
    }
}

impl From<bool> for LockKeyState {
    fn from(val: bool) -> Self {
        if val {
            LockKeyState::Enabled
        } else {
            LockKeyState::Disabled
        }
    }
}

impl From<LockKeyState> for bool {
    fn from(val: LockKeyState) -> Self {
        val == LockKeyState::Enabled
    }
}

impl fmt::Display for LockKeyState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match *self {
            LockKeyState::Enabled => "enabled",
            LockKeyState::Disabled => "disabled",
        })
    }
}

/// A specialized `Result` type lock key handling.
pub type LockKeyResult = io::Result<LockKeyState>;

/// The available lock keys for handling, i.e. Capital Lock, Number Lock and Scrolling Lock.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum LockKeys {
    CapitalLock,
    NumberLock,
    ScrollingLock,
}

/// The lock ley object to hold the OS specific handle when it is required.
pub struct LockKey {
    handle: *mut LockKeyHandle,
}

impl fmt::Debug for LockKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.handle, f)
    }
}

/// A collection of methods that are required for lock key handling.
pub trait LockKeyWrapper {
    /// Creates a new lock key object.
    fn new() -> Self;
    /// Sets a new state for the lock key.
    fn set(&self, key: LockKeys, state: LockKeyState) -> LockKeyResult;
    /// Enables the lock key.
    fn enable(&self, key: LockKeys) -> LockKeyResult;
    /// Disables the lock key.
    fn disable(&self, key: LockKeys) -> LockKeyResult;
    /// Toggles the lock key state returning its previous one.
    fn toggle(&self, key: LockKeys) -> LockKeyResult;
    /// Retrieves the lock key state.
    fn state(&self, key: LockKeys) -> LockKeyResult;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn set() {
        let lock_key = LockKey::new();
        let old_lock_key_state = lock_key.state(LockKeys::CapitalLock).unwrap();
        assert_eq!(
            lock_key
                .set(LockKeys::CapitalLock, LockKeyState::Disabled)
                .unwrap(),
            LockKeyState::Disabled
        );
        assert_eq!(
            lock_key.state(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Disabled
        );
        assert_eq!(
            lock_key
                .set(LockKeys::CapitalLock, LockKeyState::Enabled)
                .unwrap(),
            LockKeyState::Enabled
        );
        assert_eq!(
            lock_key.state(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Enabled
        );
        lock_key
            .set(LockKeys::CapitalLock, old_lock_key_state)
            .unwrap();
    }

    #[test]
    fn enable() {
        let lock_key = LockKey::new();
        let old_lock_key_state = lock_key.state(LockKeys::CapitalLock).unwrap();
        lock_key.disable(LockKeys::CapitalLock).unwrap();
        assert_eq!(
            lock_key.state(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Disabled
        );
        assert_eq!(
            lock_key.enable(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Enabled
        );
        assert_eq!(
            lock_key.state(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Enabled
        );
        lock_key
            .set(LockKeys::CapitalLock, old_lock_key_state)
            .unwrap();
    }

    #[test]
    fn disable() {
        let lock_key = LockKey::new();
        let old_lock_key_state = lock_key.state(LockKeys::CapitalLock).unwrap();
        lock_key.enable(LockKeys::CapitalLock).unwrap();
        assert_eq!(
            lock_key.state(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Enabled
        );
        assert_eq!(
            lock_key.disable(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Disabled
        );
        assert_eq!(
            lock_key.state(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Disabled
        );
        lock_key
            .set(LockKeys::CapitalLock, old_lock_key_state)
            .unwrap();
    }

    #[test]
    fn toggle() {
        let lock_key = LockKey::new();
        let old_lock_key_state = lock_key.state(LockKeys::CapitalLock).unwrap();
        lock_key.enable(LockKeys::CapitalLock).unwrap();
        assert_eq!(
            lock_key.toggle(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Enabled
        );
        assert_eq!(
            lock_key.toggle(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Disabled
        );
        lock_key
            .set(LockKeys::CapitalLock, old_lock_key_state)
            .unwrap();
    }

    #[test]
    fn state() {
        let lock_key = LockKey::new();
        let old_lock_key_state = lock_key.state(LockKeys::CapitalLock).unwrap();
        lock_key.enable(LockKeys::CapitalLock).unwrap();
        assert_eq!(
            lock_key.state(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Enabled
        );
        lock_key.disable(LockKeys::CapitalLock).unwrap();
        assert_eq!(
            lock_key.toggle(LockKeys::CapitalLock).unwrap(),
            LockKeyState::Disabled
        );
        lock_key
            .set(LockKeys::CapitalLock, old_lock_key_state)
            .unwrap();
    }
}