Skip to content

Commit ab1cab3

Browse files
committed
chore: Rearrange so Key::new() can be called by DaemonBoard
1 parent 076e9c0 commit ab1cab3

File tree

4 files changed

+93
-73
lines changed

4 files changed

+93
-73
lines changed

backend/src/board.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,16 @@ impl DaemonBoard {
7676
has_matrix,
7777
}));
7878

79-
let mut keys = self_.0.layout.keys();
80-
for key in &mut keys {
79+
let mut keys = Vec::new();
80+
for i in &self_.0.layout.physical {
81+
let key = Key::new(
82+
&self_,
83+
i.logical,
84+
i.physical,
85+
i.physical_name.clone(),
86+
i.background_color,
87+
);
88+
8189
for layer in 0..self_.0.layout.meta.num_layers {
8290
debug!(" Layer {}", layer);
8391
let scancode = match self_.0.daemon.keymap_get(
@@ -110,7 +118,7 @@ impl DaemonBoard {
110118
}
111119
}
112120

113-
key.board.set(self_.downgrade()).unwrap();
121+
keys.push(key);
114122
}
115123
self_.0.keys.set(keys).unwrap();
116124

backend/src/key.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use once_cell::unsync::OnceCell;
21
use std::{
32
cell::{Cell, RefCell},
43
char,
54
};
65

7-
use crate::{DaemonBoard, DaemonBoardWeak, Hs, Layout, Rect, Rgb};
6+
use crate::{DaemonBoard, DaemonBoardWeak, Hs, Rect, Rgb};
87

98
#[derive(Debug)]
109
pub struct Key {
11-
pub(crate) board: OnceCell<DaemonBoardWeak>,
10+
pub(crate) board: DaemonBoardWeak,
1211
// Logical position (row, column)
1312
pub logical: (u8, u8),
1413
// Logical name (something like K01, where 0 is the row and 1 is the column)
@@ -36,7 +35,7 @@ pub struct Key {
3635

3736
impl Key {
3837
pub(crate) fn new(
39-
layout: &Layout,
38+
board: &DaemonBoard,
4039
logical: (u8, u8),
4140
physical: Rect,
4241
physical_name: String,
@@ -53,14 +52,16 @@ impl Key {
5352
let logical_name = format!("K{}{}", row_char, col_char).to_uppercase();
5453
debug!(" Logical Name: {}", logical_name);
5554

56-
let electrical = *layout
55+
let electrical = *board
56+
.layout()
5757
.layout
5858
.get(logical_name.as_str())
5959
//.expect("Failed to find electrical mapping");
6060
.unwrap_or(&(0, 0));
6161
debug!(" Electrical: {:?}", electrical);
6262

63-
let leds = layout
63+
let leds = board
64+
.layout()
6465
.leds
6566
.get(logical_name.as_str())
6667
.map_or(Vec::new(), |x| x.clone());
@@ -75,7 +76,7 @@ impl Key {
7576
}
7677

7778
Self {
78-
board: Default::default(),
79+
board: board.downgrade(),
7980
logical,
8081
logical_name,
8182
physical,
@@ -92,7 +93,7 @@ impl Key {
9293
}
9394

9495
fn board(&self) -> DaemonBoard {
95-
self.board.get().unwrap().upgrade().unwrap()
96+
self.board.upgrade().unwrap()
9697
}
9798

9899
pub fn pressed(&self) -> bool {

backend/src/layout/mod.rs

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ use std::{collections::HashMap, fs, path::Path};
33
mod meta;
44
mod physical_layout;
55
pub use self::meta::Meta;
6-
pub use physical_layout::PhysicalLayout;
6+
pub(crate) use physical_layout::{PhysicalLayout, PhysicalLayoutKey};
77

8-
use crate::{Key, KeyMap, Rect, Rgb};
9-
use physical_layout::{PhysicalKeyEnum, PhysicalLayoutEntry};
8+
use crate::KeyMap;
109

1110
pub struct Layout {
1211
pub meta: Meta,
1312
pub default: KeyMap,
1413
pub keymap: HashMap<String, u16>,
1514
pub scancode_names: HashMap<u16, String>,
16-
physical: PhysicalLayout,
15+
pub(crate) physical: Vec<PhysicalLayoutKey>,
1716
pub(crate) layout: HashMap<String, (u8, u8)>,
1817
pub(crate) leds: HashMap<String, Vec<u8>>,
1918
}
@@ -79,7 +78,9 @@ impl Layout {
7978
let (keymap, scancode_names) = parse_keymap_json(keymap_json);
8079
let layout = serde_json::from_str(layout_json).unwrap();
8180
let leds = serde_json::from_str(leds_json).unwrap();
82-
let physical = serde_json::from_str(physical_json).unwrap();
81+
let physical = serde_json::from_str::<PhysicalLayout>(physical_json)
82+
.unwrap()
83+
.keys();
8384
Self {
8485
meta,
8586
default,
@@ -132,63 +133,6 @@ impl Layout {
132133
},
133134
)
134135
}
135-
136-
pub(crate) fn keys(&self) -> Vec<Key> {
137-
let mut keys = Vec::new();
138-
139-
let mut row_i = 0;
140-
let mut col_i = 0;
141-
let mut physical = Rect::new(0.0, 0.0, 1.0, 1.0);
142-
let mut background_color = Rgb::new(0xcc, 0xcc, 0xcc);
143-
144-
for entry in &self.physical.0 {
145-
if let PhysicalLayoutEntry::Row(row) = entry {
146-
for i in &row.0 {
147-
match i {
148-
PhysicalKeyEnum::Meta(meta) => {
149-
debug!("Key metadata {:?}", meta);
150-
physical.x += meta.x;
151-
physical.y -= meta.y;
152-
physical.w = meta.w.unwrap_or(physical.w);
153-
physical.h = meta.h.unwrap_or(physical.h);
154-
background_color = meta
155-
.c
156-
.as_ref()
157-
.map(|c| {
158-
let err = format!("Failed to parse color {}", c);
159-
Rgb::parse(&c[1..]).expect(&err)
160-
})
161-
.unwrap_or(background_color);
162-
}
163-
PhysicalKeyEnum::Name(name) => {
164-
keys.push(Key::new(
165-
self,
166-
(row_i as u8, col_i as u8),
167-
physical,
168-
name.clone(),
169-
background_color,
170-
));
171-
172-
physical.x += physical.w;
173-
174-
physical.w = 1.0;
175-
physical.h = 1.0;
176-
177-
col_i += 1;
178-
}
179-
}
180-
}
181-
182-
physical.x = 0.0;
183-
physical.y -= 1.0;
184-
185-
col_i = 0;
186-
row_i += 1;
187-
}
188-
}
189-
190-
keys
191-
}
192136
}
193137

194138
fn parse_keymap_json(keymap_json: &str) -> (HashMap<String, u16>, HashMap<u16, String>) {

backend/src/layout/physical_layout.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,76 @@
11
/// Serde based deserialization for physical.json
22
use serde::Deserialize;
33

4+
use crate::{Rect, Rgb};
5+
6+
pub(crate) struct PhysicalLayoutKey {
7+
pub logical: (u8, u8),
8+
pub physical: Rect,
9+
pub physical_name: String,
10+
pub background_color: Rgb,
11+
}
12+
413
#[derive(Debug, Deserialize)]
514
pub struct PhysicalLayout(pub Vec<PhysicalLayoutEntry>);
615

16+
impl PhysicalLayout {
17+
pub(crate) fn keys(&self) -> Vec<PhysicalLayoutKey> {
18+
let mut keys = Vec::new();
19+
20+
let mut row_i = 0;
21+
let mut col_i = 0;
22+
let mut physical = Rect::new(0.0, 0.0, 1.0, 1.0);
23+
let mut background_color = Rgb::new(0xcc, 0xcc, 0xcc);
24+
25+
for entry in &self.0 {
26+
if let PhysicalLayoutEntry::Row(row) = entry {
27+
for i in &row.0 {
28+
match i {
29+
PhysicalKeyEnum::Meta(meta) => {
30+
debug!("Key metadata {:?}", meta);
31+
physical.x += meta.x;
32+
physical.y -= meta.y;
33+
physical.w = meta.w.unwrap_or(physical.w);
34+
physical.h = meta.h.unwrap_or(physical.h);
35+
background_color = meta
36+
.c
37+
.as_ref()
38+
.map(|c| {
39+
let err = format!("Failed to parse color {}", c);
40+
Rgb::parse(&c[1..]).expect(&err)
41+
})
42+
.unwrap_or(background_color);
43+
}
44+
PhysicalKeyEnum::Name(name) => {
45+
keys.push(PhysicalLayoutKey {
46+
logical: (row_i as u8, col_i as u8),
47+
physical,
48+
physical_name: name.clone(),
49+
background_color,
50+
});
51+
52+
physical.x += physical.w;
53+
54+
physical.w = 1.0;
55+
physical.h = 1.0;
56+
57+
col_i += 1;
58+
}
59+
}
60+
}
61+
62+
physical.x = 0.0;
63+
physical.y -= 1.0;
64+
65+
col_i = 0;
66+
row_i += 1;
67+
}
68+
}
69+
70+
keys
71+
}
72+
}
73+
774
#[derive(Debug, Deserialize)]
875
#[serde(untagged)]
976
pub enum PhysicalLayoutEntry {

0 commit comments

Comments
 (0)