Skip to content

Commit

Permalink
Add support for EC FnLock key
Browse files Browse the repository at this point in the history
System76 EC supports FnLock since 2023-08-01_ec529da. It is not included
on any keyboards by default.

Based on the logic for EC Pause key.

Signed-off-by: Tim Crawford <[email protected]>
  • Loading branch information
crawfxrd authored and jackpot51 committed Oct 5, 2023
1 parent 0a055cb commit 1593c66
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
40 changes: 36 additions & 4 deletions backend/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::KeyMap;
// Merge date of https://github.com/system76/ec/pull/229
// Before this, `PAUSE` will not work.
const EC_PAUSE_DATE: (u16, u16, u16) = (2022, 5, 23);
// https://github.com/system76/ec/pull/263
const EC_FNLOCK_DATE: (u16, u16, u16) = (2023, 8, 1);

const QK_MOD_TAP_LEGACY: u16 = 0x6000;
const QK_MOD_TAP_MAX_LEGACY: u16 = 0x7FFF;
Expand Down Expand Up @@ -100,17 +102,33 @@ impl Layout {
use_legacy_scancodes: bool,
) -> Self {
let meta: Meta = serde_json::from_str(meta_json).unwrap();
let mut default = KeyMap::try_from(default_json).unwrap();

let has_pause_scancode = if meta.is_qmk {
true
} else {
parse_ec_date(version).map_or(true, |date| date >= EC_PAUSE_DATE)
};
let mut default = KeyMap::try_from(default_json).unwrap();
if !has_pause_scancode {
keymap_remove_pause(&mut default);
}
let (keymap, scancode_names) =
parse_keymap_json(keymap_json, board, &meta, has_pause_scancode);

let has_fnlock_scancode = if meta.is_qmk {
false
} else {
parse_ec_date(version).map_or(true, |date| date >= EC_FNLOCK_DATE)
};
if !has_fnlock_scancode {
keymap_remove_fnlock(&mut default);
}

let (keymap, scancode_names) = parse_keymap_json(
keymap_json,
board,
&meta,
has_pause_scancode,
has_fnlock_scancode,
);
let layout = serde_json::from_str(layout_json).unwrap();
let leds = serde_json::from_str(leds_json).unwrap();
let physical = PhysicalLayout::from_str(physical_json);
Expand Down Expand Up @@ -236,6 +254,7 @@ fn parse_keymap_json(
board: &str,
meta: &Meta,
has_pause_scancode: bool,
has_fnlock_scancode: bool,
) -> (HashMap<String, u16>, HashMap<u16, String>) {
let mut keymap: HashMap<String, u16> = serde_json::from_str(keymap_json).unwrap();

Expand All @@ -254,6 +273,9 @@ fn parse_keymap_json(
if !has_pause_scancode {
keymap.remove("PAUSE");
}
if !has_fnlock_scancode {
keymap.remove("FNLOCK");
}

// Generate reverse mapping, from scancode to names
let mut scancode_names = HashMap::new();
Expand Down Expand Up @@ -282,7 +304,16 @@ fn parse_ec_date(version: &str) -> Option<(u16, u16, u16)> {
fn keymap_remove_pause(keymap: &mut KeyMap) {
for values in keymap.map.values_mut() {
if values.get(1).map(String::as_str) == Some("PAUSE") {
// Change `PAUSE` on layer 2 to match layer 1
// Change `PAUSE` on layer 1 to match layer 0
values[1] = values[0].clone();
}
}
}

fn keymap_remove_fnlock(keymap: &mut KeyMap) {
for values in keymap.map.values_mut() {
if values.get(1).map(String::as_str) == Some("FNLOCK") {
// Change `FNLOCK` on layer 1 to match layer 0
values[1] = values[0].clone();
}
}
Expand Down Expand Up @@ -335,6 +366,7 @@ mod tests {
|| k == "CAMERA_TOGGLE"
|| k == "AIRPLANE_MODE"
|| k == "MIC_MUTE"
|| k == "FNLOCK"
{
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion layouts/keymap/ec.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@
"NUM_6": 116,
"NUM_7": 108,
"NUM_8": 117,
"NUM_9": 125
"NUM_9": 125,
"FNLOCK": 513
}
4 changes: 4 additions & 0 deletions layouts/picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@
{
"keysym": "LAYER_SWITCH_4",
"label": "Switch to\nLayer\u00a04"
},
{
"keysym": "FNLOCK",
"label": "FnLock"
}
]
}
Expand Down

0 comments on commit 1593c66

Please sign in to comment.