Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
380 changes: 207 additions & 173 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ members = [ "tools", "ffi", "backend", "widgets" ]

[dependencies]
cascade = "1"
cairo-rs = { git = "https://github.com/pop-os/gtk-rs" }
cairo-rs = "0.14.0"
futures = "0.3.13"
gdk = { git = "https://github.com/pop-os/gtk-rs" }
gio = { git = "https://github.com/pop-os/gtk-rs" }
glib = { git = "https://github.com/pop-os/gtk-rs" }
gtk = { git = "https://github.com/pop-os/gtk-rs", features = ["v3_22"] }
gdk = "0.14.0"
gio = "0.14.0"
glib = "0.14.0"
gtk = { version = "0.14.0", features = ["v3_22"] }
libc = "0.2"
once_cell = "1.4"
pango = { git = "https://github.com/pop-os/gtk-rs" }
pangocairo = { git = "https://github.com/pop-os/gtk-rs" }
pango = "0.14.0"
pangocairo = "0.14.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = "0.4.0"
Expand All @@ -33,7 +33,7 @@ i18n-embed-fl = "0.5.0"
rust-embed = { version = "5.9.0", features = ["debug-embed"] }

[build-dependencies]
gio = { git = "https://github.com/pop-os/gtk-rs" }
gio = "0.14.0"

[target.'cfg(target_os = "windows")'.dependencies]
winreg = "0.8"
Expand Down
4 changes: 3 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ license = "GPL-3.0-or-later"
edition = "2018"

[dependencies]
cascade = "1"
futures = "0.3.13"
futures-timer = "3.0.2"
glib = { git = "https://github.com/pop-os/gtk-rs" }
glib = "0.14.0"
hidapi = { version = "1.2", default-features = false, features = ["linux-static-hidraw"] }
libc = "0.2"
once_cell = "1.4"
ordered-float = { version = "2.0", features = ["serde"] }
palette = "0.5"
regex = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = "0.4.0"
Expand Down
4 changes: 2 additions & 2 deletions backend/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ impl Backend {

pub fn connect_board_added<F: Fn(Board) + 'static>(&self, cb: F) -> SignalHandlerId {
self.connect_local("board-added", false, move |values| {
cb(values[1].get::<Board>().unwrap().unwrap());
cb(values[1].get::<Board>().unwrap());
None
})
.unwrap()
}

pub fn connect_board_removed<F: Fn(Board) + 'static>(&self, cb: F) -> SignalHandlerId {
self.connect_local("board-removed", false, move |values| {
cb(values[1].get::<Board>().unwrap().unwrap());
cb(values[1].get::<Board>().unwrap());
None
})
.unwrap()
Expand Down
32 changes: 17 additions & 15 deletions backend/src/board.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use futures::{channel::mpsc as async_mpsc, prelude::*};
use glib::{
clone,
prelude::*,
subclass::{prelude::*, Signal},
SignalHandlerId,
};
use once_cell::sync::Lazy;
use std::{cell::Cell, collections::HashMap, sync::Arc};
use std::{
cell::{Cell, Ref, RefCell},
collections::HashMap,
sync::Arc,
};

use crate::daemon::ThreadClient;
use crate::{
Expand All @@ -30,6 +35,7 @@ pub struct BoardInner {
has_matrix: DerefCell<bool>,
is_fake: DerefCell<bool>,
has_keymap: DerefCell<bool>,
matrix: RefCell<Matrix>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -121,20 +127,12 @@ impl Board {
.collect();
self_.inner().layers.set(layers);

{
let self_ = self_.clone();
glib::MainContext::default().spawn(async move {
while let Some(matrix) = matrix_reciever.next().await {
for key in self_.keys() {
let pressed = matrix
.get(key.electrical.0 as usize, key.electrical.1 as usize)
.unwrap_or(false);
key.pressed.set(pressed);
}
self_.emit_by_name("matrix-changed", &[]).unwrap();
}
});
}
glib::MainContext::default().spawn(clone!(@strong self_ => async move {
while let Some(matrix) = matrix_reciever.next().await {
self_.inner().matrix.replace(matrix);
self_.emit_by_name("matrix-changed", &[]).unwrap();
}
}));

Ok(self_)
}
Expand Down Expand Up @@ -256,6 +254,10 @@ impl Board {
&*self.inner().keys
}

pub(crate) fn matrix(&self) -> Ref<Matrix> {
self.inner().matrix.borrow()
}

pub fn export_keymap(&self) -> KeyMap {
let mut map = HashMap::new();
let mut key_leds = HashMap::new();
Expand Down
4 changes: 2 additions & 2 deletions backend/src/daemon/daemon_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl ThreadClient {
if let Response::Benchmark(benchmark) = resp {
Ok(benchmark)
} else {
panic!(format!("'{:?}' unexpected", resp));
panic!("{}", format!("'{:?}' unexpected", resp));
}
}

Expand All @@ -231,7 +231,7 @@ impl ThreadClient {
if let Response::Nelson(nelson) = resp {
Ok(*nelson)
} else {
panic!(format!("'{:?}' unexpected", resp));
panic!("{}", format!("'{:?}' unexpected", resp));
}
}

Expand Down
10 changes: 5 additions & 5 deletions backend/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub struct Key {
/// LED name
pub led_name: String,
led_color: Cell<Option<Hs>>,
/// Key is currently pressed
pub(crate) pressed: Cell<bool>,
/// Currently loaded scancodes and their names
scancodes: Vec<Cell<u16>>,
/// Background color
Expand Down Expand Up @@ -111,7 +109,6 @@ impl Key {
leds,
led_name,
led_color: Cell::new(led_color),
pressed: Cell::new(false),
scancodes,
background_color,
}
Expand All @@ -122,7 +119,10 @@ impl Key {
}

pub fn pressed(&self) -> bool {
self.pressed.get()
self.board()
.matrix()
.get(self.electrical.0 as usize, self.electrical.1 as usize)
.unwrap_or(false)
}

pub fn color(&self) -> Option<Hs> {
Expand All @@ -147,7 +147,7 @@ impl Key {
let board = self.board();
let scancode = self.scancodes.get(layer)?.get();
let scancode_name = match board.layout().scancode_to_name(scancode) {
Some(some) => some.to_string(),
Some(some) => some,
None => String::new(),
};
Some((scancode, scancode_name))
Expand Down
3 changes: 3 additions & 0 deletions backend/src/layout/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub struct Meta {
pub has_brightness: bool,
/// Has LED with color (i.e. not monochrome)
pub has_color: bool,
/// Supports mod-tap bindings (assumes QMK mod-tap encoding)
#[serde(default)]
pub has_mod_tap: bool,
/// Number or layers; e.g. 2 where layer 2 is used when `Fn` is held
#[serde(default = "num_layers_default")]
pub num_layers: u8,
Expand Down
42 changes: 39 additions & 3 deletions backend/src/layout/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
use cascade::cascade;
use regex::Regex;
use std::{collections::HashMap, fs, path::Path};

mod meta;
use once_cell::sync::Lazy;
mod physical_layout;
pub use self::meta::Meta;
pub(crate) use physical_layout::{PhysicalLayout, PhysicalLayoutKey};

use crate::KeyMap;

const QK_MOD_TAP: u16 = 0x6000;
const QK_MOD_TAP_MAX: u16 = 0x7FFF;

pub static MOD_TAP_MODS: Lazy<HashMap<&str, u16>> = Lazy::new(|| {
cascade! {
HashMap::new();
..insert("LEFT_CTRL", 0x01);
..insert("LEFT_SHIFT", 0x02);
..insert("LEFT_ALT", 0x04);
..insert("LEFT_SUPER", 0x08);
..insert("RIGHT_CTRL", 0x11);
..insert("RIGHT_SHIFT", 0x12);
..insert("RIGHT_ALT", 0x14);
..insert("RIGHT_SUPER", 0x18);
}
});

pub struct Layout {
/// Metadata for keyboard
pub meta: Meta,
Expand Down Expand Up @@ -123,13 +143,29 @@ impl Layout {
}

/// Get the scancode number corresponding to a name
pub fn scancode_to_name(&self, scancode: u16) -> Option<&str> {
self.scancode_names.get(&scancode).map(String::as_str)
pub fn scancode_to_name(&self, scancode: u16) -> Option<String> {
if scancode >= QK_MOD_TAP && scancode <= QK_MOD_TAP_MAX {
let mod_ = (scancode >> 8) & 0x1f;
let kc = scancode & 0xff;
let mod_name = MOD_TAP_MODS.iter().find(|(_, v)| **v == mod_)?.0;
let kc_name = self.scancode_names.get(&kc)?;
Some(format!("MT({}, {})", mod_name, kc_name))
} else {
self.scancode_names.get(&scancode).cloned()
}
}

/// Get the name corresponding to a scancode number
pub fn scancode_from_name(&self, name: &str) -> Option<u16> {
self.keymap.get(name).copied()
// Check if mod-tap
let mt_re = Regex::new("MT\\(([^()]+), ([^()]+)\\)").unwrap();
if let Some(captures) = mt_re.captures(name) {
let mod_ = *MOD_TAP_MODS.get(&captures.get(1).unwrap().as_str())?;
let kc = *self.keymap.get(captures.get(2).unwrap().as_str())?;
Some(QK_MOD_TAP | ((mod_ & 0x1f) << 8) | (kc & 0xff))
} else {
self.keymap.get(name).copied()
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "system76_keyboard_configurator"
crate-type = [ "cdylib" ]

[dependencies]
glib = { git = "https://github.com/pop-os/gtk-rs" }
gtk-sys = { git = "https://github.com/pop-os/gtk-rs" }
gtk = { git = "https://github.com/pop-os/gtk-rs" }
glib = "0.14.0"
gtk-sys = "0.14.0"
gtk = "0.14.0"
system76-keyboard-configurator-widgets = { path = "../widgets" }
3 changes: 2 additions & 1 deletion layouts/system76/launch_1/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"num_layers": 4,
"has_brightness": true,
"has_color": true,
"has_mod_tap": true,
"pressed_color": "#202020",
"keyboard": "system76/launch_1"
}
}
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.47.0
1.51.0
Loading