Skip to content

Commit 6bfd074

Browse files
ids1024jackpot51
authored andcommitted
improv: Add function to list all layouts
This simplifies the unit test here, and results in the dummy supporting every layout.
1 parent 6ec6a78 commit 6bfd074

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

src/application/layout/mod.rs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,45 @@ pub(super) struct Layout<'a> {
1515
layout: HashMap<&'a str, (u8, u8)>,
1616
}
1717

18+
macro_rules! keyboards {
19+
($( $board:expr ),* $(,)?) => {
20+
fn layout_data(board: &str) -> Option<(&'static str, &'static str, &'static str)> {
21+
match board {
22+
$(
23+
$board => {
24+
let keymap_csv =
25+
include_str!(concat!("../../../layouts/", $board, "/keymap.csv"));
26+
let layout_csv =
27+
include_str!(concat!("../../../layouts/", $board, "/layout.csv"));
28+
let physical_json =
29+
include_str!(concat!("../../../layouts/", $board, "/physical.json"));
30+
Some((keymap_csv, layout_csv, physical_json))
31+
}
32+
)*
33+
_ => None
34+
}
35+
}
36+
37+
pub fn layouts() -> &'static [&'static str] {
38+
&[$( $board ),*]
39+
}
40+
};
41+
}
42+
43+
keyboards![
44+
"system76/addw1",
45+
"system76/addw2",
46+
"system76/bonw14",
47+
"system76/darp5",
48+
"system76/darp6",
49+
"system76/gaze15",
50+
"system76/launch_alpha_1",
51+
"system76/launch_alpha_2",
52+
"system76/lemp9",
53+
"system76/oryp5",
54+
"system76/oryp6",
55+
];
56+
1857
impl<'a> Layout<'a> {
1958
pub fn from_data(keymap_csv: &'a str, layout_csv: &'a str, physical_json: &'a str) -> Self {
2059
let (keymap, scancode_names) = parse_keymap_csv(keymap_csv);
@@ -29,33 +68,9 @@ impl<'a> Layout<'a> {
2968
}
3069

3170
pub fn from_board(board: &'a str) -> Option<Self> {
32-
macro_rules! keyboard {
33-
($board:expr) => {
34-
if board == $board {
35-
let keymap_csv =
36-
include_str!(concat!("../../../layouts/", $board, "/keymap.csv"));
37-
let layout_csv =
38-
include_str!(concat!("../../../layouts/", $board, "/layout.csv"));
39-
let physical_json =
40-
include_str!(concat!("../../../layouts/", $board, "/physical.json"));
41-
return Some(Self::from_data(keymap_csv, layout_csv, physical_json));
42-
}
43-
};
44-
}
45-
46-
keyboard!("system76/addw1");
47-
keyboard!("system76/addw2");
48-
keyboard!("system76/bonw14");
49-
keyboard!("system76/darp5");
50-
keyboard!("system76/darp6");
51-
keyboard!("system76/gaze15");
52-
keyboard!("system76/launch_alpha_1");
53-
keyboard!("system76/launch_alpha_2");
54-
keyboard!("system76/lemp9");
55-
keyboard!("system76/oryp5");
56-
keyboard!("system76/oryp6");
57-
58-
None
71+
layout_data(board).map(|(keymap_csv, layout_csv, physical_json)| {
72+
Self::from_data(keymap_csv, layout_csv, physical_json)
73+
})
5974
}
6075

6176
pub fn keys(&self) -> Vec<Key> {
@@ -186,19 +201,7 @@ mod tests {
186201

187202
#[test]
188203
fn layout_from_board() {
189-
for i in &[
190-
"system76/addw1",
191-
"system76/addw2",
192-
"system76/bonw14",
193-
"system76/darp5",
194-
"system76/darp6",
195-
"system76/gaze15",
196-
"system76/launch_alpha_1",
197-
"system76/launch_alpha_2",
198-
"system76/lemp9",
199-
"system76/oryp5",
200-
"system76/oryp6",
201-
] {
204+
for i in layouts() {
202205
Layout::from_board(i).unwrap();
203206
}
204207
}

src/application/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::daemon::{Daemon, DaemonClient, DaemonDummy, daemon_server};
77

88
mod key;
99
mod keyboard;
10-
mod layout;
10+
pub(crate) mod layout;
1111
mod page;
1212
mod picker;
1313
mod rect;
@@ -66,7 +66,7 @@ fn main_app(app: &gtk::Application, daemon: Rc<dyn Daemon>) {
6666
}
6767
}
6868

69-
if count == 1 {
69+
if count == 0 {
7070
eprintln!("Failed to locate any keyboards, showing demo");
7171

7272
let daemon = Rc::new(DaemonDummy::new());

src/daemon/dummy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88

99
use crate::color::Rgb;
1010
use super::Daemon;
11+
use crate::application::layout;
1112

1213
#[derive(Default)]
1314
struct BoardDummy {
@@ -29,7 +30,7 @@ impl DaemonDummy {
2930

3031
impl DaemonDummy {
3132
pub fn new() -> Self {
32-
let board_names = vec!["system76/launch_alpha_2".to_string()];
33+
let board_names: Vec<_> = layout::layouts().iter().map(|s| s.to_string()).collect();
3334
let boards = board_names.iter().map(|_| BoardDummy::default()).collect();
3435
Self {
3536
board_names,

0 commit comments

Comments
 (0)