Skip to content

Commit d3bf668

Browse files
ids1024jackpot51
authored andcommitted
improv: Avoid duplicating layout data between laptops
This matches the changes in EC, and avoids duplicating `physical.json`. `keymap.json` remains duplicated since it contains a `board` key that is checked when loading.
1 parent a8959a2 commit d3bf668

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+105
-9735
lines changed

backend/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ features = ["hidapi", "std"]
2828
[target.'cfg(target_os = "linux")'.dependencies]
2929
zbus = "1.9.1"
3030

31+
[build-dependencies]
32+
serde_json = "1.0"
33+
3134
[dev-dependencies]
3235
libc = "0.2"
3336

backend/build.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::{
2+
env,
3+
fs::{self, File},
4+
io::Write,
5+
path::Path,
6+
};
7+
8+
fn main() {
9+
let out_dir = env::var_os("OUT_DIR").unwrap();
10+
let mut outfile = File::create(Path::new(&out_dir).join("keyboards.rs")).unwrap();
11+
12+
writeln!(outfile, "keyboards![").unwrap();
13+
14+
println!("cargo:rerun-if-changed=../layouts/system76");
15+
for i in fs::read_dir("../layouts/system76").unwrap() {
16+
let i = i.unwrap();
17+
18+
let path = i.path().join("meta.json");
19+
let meta = serde_json::from_reader::<_, serde_json::Map<_, _>>(File::open(&path).unwrap())
20+
.unwrap();
21+
let file_name = i.file_name().into_string().unwrap();
22+
let keyboard = meta
23+
.get("keyboard")
24+
.and_then(|x| x.as_str())
25+
.unwrap_or_else(|| {
26+
panic!("'keyboard:' not found in {}", path.display());
27+
});
28+
29+
writeln!(
30+
outfile,
31+
" (\"system76/{}\", \"{}\"),",
32+
file_name, keyboard
33+
)
34+
.unwrap();
35+
36+
println!("cargo:rerun-if-changed=../layouts/system76/{}", file_name);
37+
println!("cargo:rerun-if-changed=../layouts/keyboards/{}", keyboard);
38+
}
39+
40+
writeln!(outfile, "];").unwrap();
41+
}

backend/src/layout/mod.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ pub struct Layout {
2020
}
2121

2222
macro_rules! keyboards {
23-
($( $board:expr ),* $(,)?) => {
23+
($( ($board:expr, $keyboard:expr) ),* $(,)?) => {
2424
fn layout_data(board: &str) -> Option<(&'static str, &'static str, &'static str, &'static str, &'static str, &'static str)> {
2525
match board {
2626
$(
2727
$board => {
2828
let meta_json =
29-
include_str!(concat!("../../../layouts/", $board, "/meta.json"));
29+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../layouts/", $board, "/meta.json"));
3030
let default_json =
31-
include_str!(concat!("../../../layouts/", $board, "/default.json"));
31+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../layouts/", $board, "/default.json"));
3232
let keymap_json =
33-
include_str!(concat!("../../../layouts/", $board, "/keymap.json"));
33+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../layouts/keyboards/", $keyboard, "/keymap.json"));
3434
let layout_json =
35-
include_str!(concat!("../../../layouts/", $board, "/layout.json"));
35+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../layouts/keyboards/", $keyboard, "/layout.json"));
3636
let leds_json =
37-
include_str!(concat!("../../../layouts/", $board, "/leds.json"));
37+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../layouts/keyboards/", $keyboard, "/leds.json"));
3838
let physical_json =
39-
include_str!(concat!("../../../layouts/", $board, "/physical.json"));
39+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../layouts/keyboards/", $keyboard, "/physical.json"));
4040
Some((meta_json, default_json, keymap_json, layout_json, leds_json, physical_json))
4141
}
4242
)*
@@ -51,26 +51,8 @@ macro_rules! keyboards {
5151
};
5252
}
5353

54-
keyboards![
55-
"system76/addw1",
56-
"system76/addw2",
57-
"system76/bonw14",
58-
"system76/darp5",
59-
"system76/darp6",
60-
"system76/darp7",
61-
"system76/galp3-c",
62-
"system76/galp4",
63-
"system76/galp5",
64-
"system76/gaze15",
65-
"system76/launch_alpha_1",
66-
"system76/launch_alpha_2",
67-
"system76/launch_1",
68-
"system76/lemp9",
69-
"system76/lemp10",
70-
"system76/oryp5",
71-
"system76/oryp6",
72-
"system76/oryp7",
73-
];
54+
// Calls the `keyboards!` macro
55+
include!(concat!(env!("OUT_DIR"), "/keyboards.rs"));
7456

7557
impl Layout {
7658
pub fn from_data(

layouts.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,22 @@ def gen_default_json(path: str, board: str, keymap: Dict[str, List[str]], is_qmk
281281
json.dump({"model": board, "version": 1, "map": keymap, "key_leds": key_leds, "layers": layers}, f, indent=2)
282282

283283

284-
def update_meta_json(meta_json: str, has_brightness: bool, has_color: bool):
284+
def update_meta_json(meta_json: str, has_brightness: bool, has_color: bool, keyboard: str):
285285
meta = {}
286286
if os.path.exists(meta_json):
287287
with open(meta_json, 'r') as f:
288288
meta = json.load(f, object_pairs_hook=OrderedDict)
289289

290290
meta['has_brightness'] = has_brightness
291291
meta['has_color'] = has_color
292+
meta['keyboard'] = keyboard
292293

293294
with open(meta_json, 'w') as f:
294295
json.dump(meta, f, indent=2)
295296

296297

297298
def generate_layout_dir(ecdir: str, board: str, is_qmk: bool) -> None:
298-
layoutdir = f'layouts/{board}'
299-
print(f'Generating {layoutdir}...')
299+
print(f'Generating layouts/{board}...')
300300

301301
has_brightness = True
302302
has_color = True
@@ -308,6 +308,7 @@ def generate_layout_dir(ecdir: str, board: str, is_qmk: bool) -> None:
308308
f"{ecdir}/keyboards/{board}/keymaps/default/keymap.c").read()
309309
led_c = open(
310310
f"{ecdir}/keyboards/{board}/{board.split('/')[-1]}.c").read()
311+
keyboard = board
311312
else:
312313
with open(f"{ecdir}/src/board/{board}/board.mk") as f:
313314
board_mk = f.read()
@@ -333,17 +334,18 @@ def generate_layout_dir(ecdir: str, board: str, is_qmk: bool) -> None:
333334
default_c = open(f"{ecdir}/src/keyboard/{keyboard}/keymap/default.c").read()
334335
led_c = ""
335336

336-
os.makedirs(f'{layoutdir}', exist_ok=True)
337+
os.makedirs(f'layouts/{board}', exist_ok=True)
338+
os.makedirs(f'layouts/keyboards/{keyboard}', exist_ok=True)
337339

338340
physical, physical2 = parse_layout_define(keymap_h, is_qmk)
339341
leds = parse_led_config(led_c, physical)
340342
scancodes, mapping = extract_scancodes(ecdir, board, is_qmk, has_brightness, has_color)
341343
default_keymap = parse_keymap(default_c, mapping, physical, is_qmk)
342-
gen_layout_json(f'{layoutdir}/layout.json', physical, physical2)
343-
gen_leds_json(f'{layoutdir}/leds.json', leds)
344-
gen_keymap_json(f'{layoutdir}/keymap.json', scancodes)
345-
gen_default_json(f'{layoutdir}/default.json', board, default_keymap, is_qmk)
346-
update_meta_json(f'{layoutdir}/meta.json', has_brightness, has_color)
344+
gen_layout_json(f'layouts/keyboards/{keyboard}/layout.json', physical, physical2)
345+
gen_leds_json(f'layouts/keyboards/{keyboard}/leds.json', leds)
346+
gen_keymap_json(f'layouts/keyboards/{keyboard}/keymap.json', scancodes)
347+
gen_default_json(f'layouts/{board}/default.json', board, default_keymap, is_qmk)
348+
update_meta_json(f'layouts/{board}/meta.json', has_brightness, has_color, keyboard)
347349

348350
parser = argparse.ArgumentParser()
349351
parser.add_argument("ecdir")

layouts/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
Within each layout:
44
* `default.json` - The default keymap and LED settings, in the same format the Configurator can import/export through its UI.
5+
* `meta.json` - Micellanous values associated with the keyboard.
6+
7+
`meta.json` includes a `keyboard` key that refers to a subdirectory of `keyboards/`, since multiple laptop models have the same keyboard, so they share this data.
8+
9+
In `keyboards/`:
510
* `keymap.json` - Maps keycode names to their numerical values.
611
* `layout.json` - Maps key position to electrical matrix indices.
712
* `leds.json` - For a keyboard with per-key LEDs, maps key position to LED index.
8-
* `meta.json` - Micellanous values associated with the keyboard.
913
* `physical.json` - Defines the physical layout of keys, the colors to display as their backgrounds, and labels (only shown in a tab when `--debug-layers` is passed to the Configurator).
1014

1115
Other than `meta.json` and `physical.json`, these files are generated from the EC/QMK source using `layouts.py` from the root of this repository. `meta.json` is written manually, with other keys added by `layouts.py`. `physical.json` is created with <http://www.keyboard-layout-editor.com>.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)