Skip to content

Commit

Permalink
Fix bug when matching keyboards
Browse files Browse the repository at this point in the history
It turns out hidutil does not error if you pass invalid JSON for the
`--matching` key and instead it just matches all keyboards.
  • Loading branch information
rossmacarthur committed Aug 18, 2023
1 parent 132693d commit 3cb0bea
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn dump(device: &Option<Device>, mappings: &[Map]) -> Result<String> {

fn dump_matching_option(device: &Device) -> String {
format!(
"{{\" \"VendorID\" = 0x{:x}, \"ProductID\" = 0x{:04x} }}",
"{{\"VendorID\": 0x{:04x}, \"ProductID\": 0x{:04x}}}",
device.vendor_id, device.product_id,
)
}
Expand Down Expand Up @@ -160,6 +160,23 @@ fn split_whitespace_indices(s: &str) -> impl Iterator<Item = (&str, usize)> + '_
mod tests {
use super::*;

#[test]
fn test_dump() {
let mappings = vec![Map(Key::Raw(0x7000000e), Key::Raw(0x7000000f))];
let device = Device {
vendor_id: 0x1234,
product_id: 0x5678,
name: "test".to_owned(),
};
let output = dump(&Some(device), &mappings).unwrap();
assert_eq!(
output,
r#"hidutil property \
--matching '{"VendorID": 0x1234, "ProductID": 0x5678}' \
--set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x77000000e,"HIDKeyboardModifierMappingDst":0x77000000f}]}'"#
)
}

#[test]
fn test_parse_hidutil_output_empty() {
let output = r#"Devices:
Expand Down

0 comments on commit 3cb0bea

Please sign in to comment.