Skip to content

Commit 5d17eba

Browse files
committed
added runtime changes to the overlay-config using inotify, fixes #11
1 parent 0c8af41 commit 5d17eba

6 files changed

Lines changed: 62 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fontdb = "0.7"
3535

3636
# Directory utilities
3737
dirs = "5.0"
38+
notify = "8.2.0"
3839

3940
[package.metadata.deb]
4041
maintainer = "Your Name <your.email@example.com>"

src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,20 @@ fn main() -> Result<()> {
5757

5858
log::info!("Loaded overlay config: {:?}", overlay_config);
5959

60-
let _state = State::new(
60+
let state = Arc::new(State::new(
6161
Arc::new(Mutex::new(overlay_config.clone())),
6262
Arc::new(Mutex::new(shortcuts.clone())),
63-
);
64-
// watcher code
63+
));
6564

65+
// watcher code
66+
let state_clone = Arc::clone(&state);
67+
let handle = std::thread::spawn(|| {
68+
watcher::watch_overlay_config(state_clone).unwrap();
69+
});
6670
// start
67-
overlay::start(shortcuts, overlay_config)?;
71+
overlay::start(state)?;
72+
73+
handle.join().unwrap();
6874

6975
Ok(())
7076
}

src/overlay.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::blur::box_blur_multi_pass;
22
use crate::config::OverlayConfig;
33
use crate::input_listener::{start_alt_listener, AltState};
4+
use crate::state::State;
45
use anyhow::{Context, Result};
56
use smithay_client_toolkit::{
67
compositor::{CompositorHandler, CompositorState},
@@ -914,7 +915,7 @@ fn run_single_overlay(
914915
Ok(())
915916
}
916917

917-
pub fn start(shortcuts: Vec<KeyBinding>, config: OverlayConfig) -> Result<()> {
918+
pub fn start(state: Arc<State>) -> Result<()> {
918919
let ctrl_receiver = match start_alt_listener() {
919920
Ok(rx) => {
920921
log::info!("Successfully started libinput Alt key listener");
@@ -936,10 +937,10 @@ pub fn start(shortcuts: Vec<KeyBinding>, config: OverlayConfig) -> Result<()> {
936937
Ok(AltState::Pressed) => {
937938
log::debug!("==> Alt pressed alone - spawning new overlay thread");
938939

939-
let shortcuts_clone = shortcuts.clone();
940+
let shortcuts_clone = state.clone_keybindings();
940941
let flag = Arc::new(AtomicBool::new(false));
941942
let flag_clone = Arc::clone(&flag);
942-
let config_clone = config.clone();
943+
let config_clone = state.clone_config();
943944

944945
let handle = std::thread::spawn(move || {
945946
if let Err(e) = run_single_overlay(shortcuts_clone, flag_clone, config_clone) {

src/state.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,28 @@ impl State {
1919
}
2020
}
2121

22-
fn _set_config(&self, new_config: config::OverlayConfig) {
22+
pub(crate) fn _set_config(&self, new_config: config::OverlayConfig) {
2323
let mut config = self._config.lock().unwrap();
2424
*config = new_config;
2525
}
2626

27-
fn _set_keybindings(&self, new_keybindings: Vec<KeyBinding>) {
27+
pub(crate) fn reload_overlay_config(&self) {
28+
let mut config = self._config.lock().unwrap();
29+
*config = config::OverlayConfig::load().unwrap();
30+
}
31+
32+
pub(crate) fn _set_keybindings(&self, new_keybindings: Vec<KeyBinding>) {
2833
let mut keybindings = self._keybindings.lock().unwrap();
2934
*keybindings = new_keybindings;
3035
}
36+
37+
pub(crate) fn clone_config(&self) -> config::OverlayConfig {
38+
let config = self._config.lock().unwrap();
39+
config.clone()
40+
}
41+
42+
pub(crate) fn clone_keybindings(&self) -> Vec<KeyBinding> {
43+
let keybindings = self._keybindings.lock().unwrap();
44+
keybindings.clone()
45+
}
3146
}

src/watcher.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1+
use crate::state::State;
2+
use notify::{Event, RecursiveMode, Result, Watcher};
3+
use std::sync::Arc;
4+
use std::{path::Path, sync::mpsc};
15

6+
const OVERLAY_CONFIG_FILE: &str = "/usr/share/shortcuts-overlay/overlay-config.toml";
7+
8+
pub(crate) fn watch_overlay_config(state: Arc<State>) -> Result<()> {
9+
let (tx, rx) = mpsc::channel::<Result<Event>>();
10+
11+
let mut watcher = notify::recommended_watcher(tx)?;
12+
13+
// Add a path to be watched. All files and directories at that path and
14+
// below will be monitored for changes.
15+
watcher.watch(Path::new(OVERLAY_CONFIG_FILE), RecursiveMode::Recursive)?;
16+
// Block forever, printing out events as they come in
17+
for res in rx {
18+
match res {
19+
Ok(event) => {
20+
log::debug!("event: {:?}", event);
21+
if event.kind.is_modify() {
22+
state.reload_overlay_config();
23+
}
24+
}
25+
Err(e) => log::error!("watch error: {:?}", e),
26+
}
27+
}
28+
29+
Ok(())
30+
}

0 commit comments

Comments
 (0)