Skip to content

Commit 2bb7307

Browse files
ids1024jackpot51
authored andcommitted
improv: define daemon_opt as Option<Rc<RefCell<dyn Daemon>>>
This will allow the type to be used in multiple places with reference counting. Probably it would be more consistent with Gtk conventions to embed the interior mutability withing the `Daemon` trait. That seems to be a little complicated to change as is, though.
1 parent 3177cc3 commit 2bb7307

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/application/keyboard.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::picker::Picker;
2222
use super::rect::Rect;
2323

2424
pub struct Keyboard {
25-
daemon_opt: RefCell<Option<Box<dyn Daemon>>>,
25+
daemon_opt: Option<Rc<RefCell<dyn Daemon>>>,
2626
daemon_board: usize,
2727
keymap: HashMap<String, u16>,
2828
keys: RefCell<Vec<Key>>,
@@ -32,7 +32,7 @@ pub struct Keyboard {
3232
}
3333

3434
impl Keyboard {
35-
pub fn new<P: AsRef<Path>>(dir: P, daemon_opt: Option<Box<dyn Daemon>>, daemon_board: usize) -> Rc<Self> {
35+
pub fn new<P: AsRef<Path>>(dir: P, daemon_opt: Option<Rc<RefCell<dyn Daemon>>>, daemon_board: usize) -> Rc<Self> {
3636
let dir = dir.as_ref();
3737

3838
let keymap_csv = fs::read_to_string(dir.join("keymap.csv"))
@@ -44,7 +44,7 @@ impl Keyboard {
4444
Self::new_data(&keymap_csv, &layout_csv, &physical_json, daemon_opt, daemon_board)
4545
}
4646

47-
pub fn new_board(board: &str, daemon_opt: Option<Box<dyn Daemon>>, daemon_board: usize) -> Option<Rc<Self>> {
47+
pub fn new_board(board: &str, daemon_opt: Option<Rc<RefCell<dyn Daemon>>>, daemon_board: usize) -> Option<Rc<Self>> {
4848
macro_rules! keyboard {
4949
($board:expr) => (if board == $board {
5050
let keymap_csv = include_str!(concat!("../../layouts/", $board, "/keymap.csv"));
@@ -68,7 +68,7 @@ impl Keyboard {
6868
None
6969
}
7070

71-
pub fn new_data(keymap_csv: &str, layout_csv: &str, physical_json: &str, mut daemon_opt: Option<Box<dyn Daemon>>, daemon_board: usize) -> Rc<Self> {
71+
fn new_data(keymap_csv: &str, layout_csv: &str, physical_json: &str, daemon_opt: Option<Rc<RefCell<dyn Daemon>>>, daemon_board: usize) -> Rc<Self> {
7272
let mut keymap = HashMap::new();
7373
let mut scancode_names = HashMap::new();
7474
scancode_names.insert(0, "NONE");
@@ -179,7 +179,8 @@ impl Keyboard {
179179
let mut scancodes = Vec::new();
180180
for layer in 0..2 {
181181
println!(" Layer {}", layer);
182-
let scancode = if let Some(ref mut daemon) = daemon_opt {
182+
let scancode = if let Some(ref daemon) = daemon_opt {
183+
let mut daemon = daemon.borrow_mut();
183184
match daemon.keymap_get(daemon_board, layer, electrical.0, electrical.1) {
184185
Ok(value) => value,
185186
Err(err) => {
@@ -237,7 +238,7 @@ impl Keyboard {
237238
}
238239

239240
Rc::new(Self {
240-
daemon_opt: RefCell::new(daemon_opt),
241+
daemon_opt,
241242
daemon_board,
242243
keymap,
243244
keys: RefCell::new(keys),
@@ -350,7 +351,8 @@ button {
350351
return;
351352
}
352353
println!(" set {}, {}, {} to {:04X}", layer, k.electrical.0, k.electrical.1, k.scancodes[layer].0);
353-
if let Some(ref mut daemon) = *kb.daemon_opt.borrow_mut() {
354+
if let Some(ref daemon) = kb.daemon_opt {
355+
let mut daemon = daemon.borrow_mut();
354356
if let Err(err) = daemon.keymap_set(kb.daemon_board, layer as u8, k.electrical.0, k.electrical.1, k.scancodes[layer].0) {
355357
eprintln!("Failed to set keymap: {:?}", err);
356358
}

src/application/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use cascade::cascade;
22
use gio::prelude::*;
33
use gtk::prelude::*;
4+
use std::cell::RefCell;
45
use std::rc::Rc;
56

67
use crate::daemon::{Daemon, DaemonClient, daemon_server};
@@ -43,8 +44,8 @@ fn main_keyboard(app: &gtk::Application, keyboard: Rc<Keyboard>) {
4344
});
4445
}
4546

46-
fn main_app(app: &gtk::Application, mut daemon: Box<dyn Daemon>) {
47-
let boards = daemon.boards().expect("Failed to load boards");
47+
fn main_app(app: &gtk::Application, daemon: Rc<RefCell<dyn Daemon>>) {
48+
let boards = daemon.borrow_mut().boards().expect("Failed to load boards");
4849
let i = 0;
4950
if let Some(board) = boards.get(i) {
5051
if let Some(keyboard) = Keyboard::new_board(board, Some(daemon), i) {
@@ -62,7 +63,7 @@ fn main_app(app: &gtk::Application, mut daemon: Box<dyn Daemon>) {
6263
}
6364

6465
#[cfg(target_os = "linux")]
65-
fn with_daemon<F: Fn(Box<dyn Daemon>)>(f: F) {
66+
fn with_daemon<F: Fn(Rc<RefCell<dyn Daemon>>)>(f: F) {
6667
use std::{
6768
process::{
6869
Command,
@@ -73,7 +74,7 @@ fn with_daemon<F: Fn(Box<dyn Daemon>)>(f: F) {
7374
if unsafe { libc::geteuid() == 0 } {
7475
eprintln!("Already running as root");
7576
let server = daemon_server().expect("Failed to create server");
76-
f(Box::new(server));
77+
f(Rc::new(RefCell::new(server)));
7778
return;
7879
}
7980

@@ -95,7 +96,7 @@ fn with_daemon<F: Fn(Box<dyn Daemon>)>(f: F) {
9596
let stdin = child.stdin.take().expect("Failed to get stdin of daemon");
9697
let stdout = child.stdout.take().expect("Failed to get stdout of daemon");
9798

98-
f(Box::new(DaemonClient::new(stdout, stdin)));
99+
f(Rc::new(RefCell::new(DaemonClient::new(stdout, stdin))));
99100

100101
let status = child.wait().expect("Failed to wait for daemon");
101102
if ! status.success() {

0 commit comments

Comments
 (0)