Skip to content

Commit 1593c66

Browse files
crawfxrdjackpot51
authored andcommitted
Add support for EC FnLock key
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]>
1 parent 0a055cb commit 1593c66

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

backend/src/layout/mod.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::KeyMap;
1313
// Merge date of https://github.com/system76/ec/pull/229
1414
// Before this, `PAUSE` will not work.
1515
const EC_PAUSE_DATE: (u16, u16, u16) = (2022, 5, 23);
16+
// https://github.com/system76/ec/pull/263
17+
const EC_FNLOCK_DATE: (u16, u16, u16) = (2023, 8, 1);
1618

1719
const QK_MOD_TAP_LEGACY: u16 = 0x6000;
1820
const QK_MOD_TAP_MAX_LEGACY: u16 = 0x7FFF;
@@ -100,17 +102,33 @@ impl Layout {
100102
use_legacy_scancodes: bool,
101103
) -> Self {
102104
let meta: Meta = serde_json::from_str(meta_json).unwrap();
105+
let mut default = KeyMap::try_from(default_json).unwrap();
106+
103107
let has_pause_scancode = if meta.is_qmk {
104108
true
105109
} else {
106110
parse_ec_date(version).map_or(true, |date| date >= EC_PAUSE_DATE)
107111
};
108-
let mut default = KeyMap::try_from(default_json).unwrap();
109112
if !has_pause_scancode {
110113
keymap_remove_pause(&mut default);
111114
}
112-
let (keymap, scancode_names) =
113-
parse_keymap_json(keymap_json, board, &meta, has_pause_scancode);
115+
116+
let has_fnlock_scancode = if meta.is_qmk {
117+
false
118+
} else {
119+
parse_ec_date(version).map_or(true, |date| date >= EC_FNLOCK_DATE)
120+
};
121+
if !has_fnlock_scancode {
122+
keymap_remove_fnlock(&mut default);
123+
}
124+
125+
let (keymap, scancode_names) = parse_keymap_json(
126+
keymap_json,
127+
board,
128+
&meta,
129+
has_pause_scancode,
130+
has_fnlock_scancode,
131+
);
114132
let layout = serde_json::from_str(layout_json).unwrap();
115133
let leds = serde_json::from_str(leds_json).unwrap();
116134
let physical = PhysicalLayout::from_str(physical_json);
@@ -236,6 +254,7 @@ fn parse_keymap_json(
236254
board: &str,
237255
meta: &Meta,
238256
has_pause_scancode: bool,
257+
has_fnlock_scancode: bool,
239258
) -> (HashMap<String, u16>, HashMap<u16, String>) {
240259
let mut keymap: HashMap<String, u16> = serde_json::from_str(keymap_json).unwrap();
241260

@@ -254,6 +273,9 @@ fn parse_keymap_json(
254273
if !has_pause_scancode {
255274
keymap.remove("PAUSE");
256275
}
276+
if !has_fnlock_scancode {
277+
keymap.remove("FNLOCK");
278+
}
257279

258280
// Generate reverse mapping, from scancode to names
259281
let mut scancode_names = HashMap::new();
@@ -282,7 +304,16 @@ fn parse_ec_date(version: &str) -> Option<(u16, u16, u16)> {
282304
fn keymap_remove_pause(keymap: &mut KeyMap) {
283305
for values in keymap.map.values_mut() {
284306
if values.get(1).map(String::as_str) == Some("PAUSE") {
285-
// Change `PAUSE` on layer 2 to match layer 1
307+
// Change `PAUSE` on layer 1 to match layer 0
308+
values[1] = values[0].clone();
309+
}
310+
}
311+
}
312+
313+
fn keymap_remove_fnlock(keymap: &mut KeyMap) {
314+
for values in keymap.map.values_mut() {
315+
if values.get(1).map(String::as_str) == Some("FNLOCK") {
316+
// Change `FNLOCK` on layer 1 to match layer 0
286317
values[1] = values[0].clone();
287318
}
288319
}
@@ -335,6 +366,7 @@ mod tests {
335366
|| k == "CAMERA_TOGGLE"
336367
|| k == "AIRPLANE_MODE"
337368
|| k == "MIC_MUTE"
369+
|| k == "FNLOCK"
338370
{
339371
continue;
340372
}

layouts/keymap/ec.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,6 @@
125125
"NUM_6": 116,
126126
"NUM_7": 108,
127127
"NUM_8": 117,
128-
"NUM_9": 125
128+
"NUM_9": 125,
129+
"FNLOCK": 513
129130
}

layouts/picker.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@
636636
{
637637
"keysym": "LAYER_SWITCH_4",
638638
"label": "Switch to\nLayer\u00a04"
639+
},
640+
{
641+
"keysym": "FNLOCK",
642+
"label": "FnLock"
639643
}
640644
]
641645
}

0 commit comments

Comments
 (0)