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
mod command;
mod key;

use std::io;
use std::iter;
use std::process;
use std::str;

use anyhow::{Context, Result};
use serde::Serialize;
use thiserror::Error;

use crate::command::CommandExt;
pub use crate::key::{Key, ParseKeyError};

/// A keyboard modification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
pub struct Mod {
    #[serde(
        rename = "HIDKeyboardModifierMappingSrc",
        serialize_with = "crate::key::serialize"
    )]
    src: Key,
    #[serde(
        rename = "HIDKeyboardModifierMappingDst",
        serialize_with = "crate::key::serialize"
    )]
    dst: Key,
}

/// A list of keyboard modifications.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Mods {
    #[serde(rename = "UserKeyMapping")]
    mods: Vec<Mod>,
}

/// A unique keyboard.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub struct Keyboard {
    #[serde(skip)]
    product_name: String,
    #[serde(skip)]
    vendor_name: String,
    #[serde(rename = "VendorID")]
    vendor_id: u64,
    #[serde(rename = "ProductID")]
    product_id: u64,
}

impl Mod {
    /// The source key.
    pub fn src(&self) -> Key {
        self.src
    }

    /// The destination key.
    pub fn dst(&self) -> Key {
        self.dst
    }

    /// Returns a new modification with the source and destination swapped.
    pub fn swapped(self) -> Self {
        Self {
            src: self.dst,
            dst: self.src,
        }
    }
}

/// Recursively parse USB information, recursing into any
/// `IORegistryEntryChildren` entries.
fn parse_plist_recurse(value: plist::Value, result: &mut Vec<plist::Dictionary>) -> Option<()> {
    let mut dict = value.into_dictionary()?;
    if let Some(array) = dict.remove("IORegistryEntryChildren") {
        for value in array.into_array()?.into_iter() {
            parse_plist_recurse(value, result)?;
        }
    } else {
        result.push(dict);
    }
    Some(())
}

fn parse_plist(value: plist::Value) -> Option<Vec<plist::Dictionary>> {
    let mut result = Default::default();
    parse_plist_recurse(value, &mut result)?;
    Some(result)
}

fn parse_keyboards(value: plist::Value) -> Result<Vec<Keyboard>> {
    parse_plist(value)
        .context("failed to parse plist")?
        .into_iter()
        .map(Keyboard::from_plist_dict)
        .collect()
}

impl Keyboard {
    /// Parse a keyboard from a plist dictionary.
    fn from_plist_dict(mut dict: plist::Dictionary) -> Result<Self> {
        let product_name = dict
            .remove("USB Product Name")
            .context("expected `USB Product Name`")?
            .into_string()
            .context("expected valid `USB Product Name` value")?;
        let vendor_name = dict
            .remove("USB Vendor Name")
            .context("expected `USB Vendor Name`")?
            .into_string()
            .context("expected valid `USB Vendor Name` value")?;
        let vendor_id = dict
            .remove("idVendor")
            .context("expected `idVendor`")?
            .as_unsigned_integer()
            .context("expected valid `idVendor` value")?;
        let product_id = dict
            .remove("idProduct")
            .context("expected `idProduct` key")?
            .as_unsigned_integer()
            .context("expected valid `idProduct` value")?;
        Ok(Keyboard {
            product_name,
            vendor_name,
            vendor_id,
            product_id,
        })
    }

    /// List all the keyboards.
    pub fn list() -> Result<Vec<Self>> {
        let text = process::Command::new("ioreg")
            .args(&["-a", "-l", "-p", "IOUSB"])
            .output_text()?;
        let obj = plist::Value::from_reader(io::Cursor::new(text))?;
        parse_keyboards(obj)
    }

    /// Find a keyboard matching the given predicate.
    pub fn find<P>(predicate: P) -> Result<Option<Self>>
    where
        P: FnMut(&Self) -> bool,
    {
        Ok(Self::list()?.into_iter().find(predicate))
    }

    /// The product name of the keyboard.
    pub fn product_name(&self) -> &str {
        &self.product_name.trim()
    }

    /// The product name of the keyboard.
    pub fn vendor_name(&self) -> &str {
        &self.vendor_name.trim()
    }

    /// Apply the modifications to the keyboard.
    pub fn apply(&mut self, mods: Mods) -> Result<()> {
        process::Command::new("hidutil")
            .arg("property")
            .arg("--matching")
            .arg(&serde_json::to_string(self)?)
            .arg("--set")
            .arg(&serde_json::to_string(&mods)?)
            .output_text()?;
        Ok(())
    }

    /// Remove all modifications from the keyboard.
    pub fn reset(&mut self) -> Result<()> {
        self.apply(Mods::default())
    }
}

/// An error produced when we fail to parse a [`Mod`] from a string.
#[derive(Debug, Error)]
pub enum ParseModError {
    #[error(transparent)]
    Key(#[from] ParseKeyError),

    #[error("failed to parse mod from `{0}`")]
    Other(String),
}

impl str::FromStr for Mod {
    type Err = ParseModError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let err = || ParseModError::Other(s.to_owned());
        if s.is_empty() {
            return Err(err());
        }
        let mut it = s.splitn(2, ':');
        let src = it.next().ok_or_else(err)?.parse()?;
        let dst = it.next().ok_or_else(err)?.parse()?;
        Ok(Self { src, dst })
    }
}

impl iter::FromIterator<Mod> for Mods {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = Mod>,
    {
        Mods {
            mods: iter.into_iter().collect(),
        }
    }
}

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

    use std::str::FromStr;

    #[test]
    fn mod_from_str() {
        let test_cases = &[
            (
                "return:A",
                Mod {
                    src: Key::Return,
                    dst: Key::Char('A'),
                },
            ),
            (
                "capslock:0x64",
                Mod {
                    src: Key::CapsLock,
                    dst: Key::Raw(0x64),
                },
            ),
        ];

        for tc in test_cases {
            assert_eq!(Mod::from_str(tc.0).unwrap(), tc.1);
        }
    }
}