Skip to content

Commit

Permalink
Merge pull request #217 from waycrate/udev_async
Browse files Browse the repository at this point in the history
Udev async
  • Loading branch information
Shinyzenith authored Jul 23, 2023
2 parents ddada68 + eda3e39 commit 605c935
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 27 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
15 changes: 12 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ jobs:
uses: actions/checkout@v2

- name: Build
run: cargo build --release
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends libudev-dev
cargo build --release
clippy:
runs-on: ubuntu-latest
Expand All @@ -24,7 +27,10 @@ jobs:
uses: actions/checkout@v2

- name: Clippy
run: cargo clippy
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends libudev-dev
cargo clippy
test:
runs-on: ubuntu-latest
Expand All @@ -34,7 +40,10 @@ jobs:
uses: actions/checkout@v2

- name: Run tests
run: cargo test --verbose
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends libudev-dev
cargo test --verbose
documentation:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `DESTDIR` variable for the `install` target in the `Makefile` to help
packaging and installation. To install in a subdirectory, just call `make
DESTDIR=subdir install`.
- Detection of added/removed devices (e.g., when plugging or unplugging a
keyboard). The devices are grabbed by `swhkd` if they match the `--device`
parameters if present or if they are recognized as keyboard devices otherwise.
- `Altgr` modifier added (https://github.com/waycrate/swhkd/pull/213).

### Changed
Expand Down
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@
rustc
scdoc

# libs
udev

# Tools
zip
rustfmt
pkg-config
clippy
gdb
gnumake
rust-analyzer
rustfmt
strace
valgrind
zip
];
};
});
Expand Down
1 change: 1 addition & 0 deletions swhkd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ signal-hook-tokio = { version = "0.3.1", features = ["futures-v0_3"] }
sysinfo = "0.23.5"
tokio = { version = "1.24.2", features = ["full"] }
tokio-stream = "0.1.8"
tokio-udev = "0.9.1"

[[bin]]
name = "swhkd"
Expand Down
93 changes: 71 additions & 22 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use tokio::select;
use tokio::time::Duration;
use tokio::time::{sleep, Instant};
use tokio_stream::{StreamExt, StreamMap};
use tokio_udev::{AsyncMonitorSocket, EventType, MonitorBuilder};

mod config;
mod perms;
Expand Down Expand Up @@ -142,23 +143,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
};
}

let keyboard_devices: Vec<Device> = {
if let Some(arg_devices) = args.values_of("device") {
// for device in arg_devices {
// let device_path = Path::new(device);
// if let Ok(device_to_use) = Device::open(device_path) {
// log::info!("Using device: {}", device_to_use.name().unwrap_or(device));
// keyboard_devices.push(device_to_use);
// }
// }
let arg_devices = arg_devices.collect::<Vec<&str>>();
let arg_devices: Vec<&str> = args.values_of("device").unwrap_or_default().collect();

let keyboard_devices: Vec<_> = {
if arg_devices.is_empty() {
log::trace!("Attempting to find all keyboard file descriptors.");
evdev::enumerate().filter(|(_, dev)| check_device_is_keyboard(dev)).collect()
} else {
evdev::enumerate()
.map(|(_, device)| device)
.filter(|device| arg_devices.contains(&device.name().unwrap_or("")))
.filter(|(_, dev)| arg_devices.contains(&dev.name().unwrap_or("")))
.collect()
} else {
log::trace!("Attempting to find all keyboard file descriptors.");
evdev::enumerate().map(|(_, device)| device).filter(check_device_is_keyboard).collect()
}
};

Expand Down Expand Up @@ -189,6 +183,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
};

let mut udev =
AsyncMonitorSocket::new(MonitorBuilder::new()?.match_subsystem("input")?.listen()?)?;

let modifiers_map: HashMap<Key, config::Modifier> = HashMap::from([
(Key::KEY_LEFTMETA, config::Modifier::Super),
(Key::KEY_RIGHTMETA, config::Modifier::Super),
Expand All @@ -206,21 +203,27 @@ async fn main() -> Result<(), Box<dyn Error>> {
250
};

let mut signals = Signals::new(&[
let mut signals = Signals::new([
SIGUSR1, SIGUSR2, SIGHUP, SIGABRT, SIGBUS, SIGCHLD, SIGCONT, SIGINT, SIGPIPE, SIGQUIT,
SIGSYS, SIGTERM, SIGTRAP, SIGTSTP, SIGVTALRM, SIGXCPU, SIGXFSZ,
])?;

let mut execution_is_paused = false;
let mut last_hotkey: Option<config::Hotkey> = None;
let mut pending_release: bool = false;
let mut keyboard_states: Vec<KeyboardState> = Vec::new();
let mut keyboard_states = HashMap::new();
let mut keyboard_stream_map = StreamMap::new();

for (i, mut device) in keyboard_devices.into_iter().enumerate() {
for (path, mut device) in keyboard_devices.into_iter() {
let _ = device.grab();
keyboard_stream_map.insert(i, device.into_event_stream()?);
keyboard_states.push(KeyboardState::new());
let path = match path.to_str() {
Some(p) => p,
None => {
continue;
}
};
keyboard_states.insert(path.to_string(), KeyboardState::new());
keyboard_stream_map.insert(path.to_string(), device.into_event_stream()?);
}

// The initial sleep duration is never read because last_hotkey is initialized to None
Expand Down Expand Up @@ -281,8 +284,54 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

Some((i, Ok(event))) = keyboard_stream_map.next() => {
let keyboard_state = &mut keyboard_states[i];
Some(Ok(event)) = udev.next() => {
if !event.is_initialized() {
log::warn!("Received udev event with uninitialized device.");
}

let node = match event.devnode() {
None => { continue; },
Some(node) => {
match node.to_str() {
None => { continue; },
Some(node) => node,
}
},
};

match event.event_type() {
EventType::Add => {
let mut device = match Device::open(node) {
Err(e) => {
log::error!("Could not open evdev device at {}: {}", node, e);
continue;
},
Ok(device) => device
};
let name = device.name().unwrap_or("[unknown]");
if arg_devices.contains(&name) || check_device_is_keyboard(&device) {
log::info!("Device '{}' at '{}' added.", name, node);
let _ = device.grab();
keyboard_states.insert(node.to_string(), KeyboardState::new());
keyboard_stream_map.insert(node.to_string(), device.into_event_stream()?);
}
}
EventType::Remove => {
if keyboard_stream_map.contains_key(node) {
keyboard_states.remove(node);
let stream = keyboard_stream_map.remove(node).expect("device not in stream_map");
let name = stream.device().name().unwrap_or("[unknown]");
log::info!("Device '{}' at '{}' removed", name, node);
}
}
_ => {
log::trace!("Ignored udev event of type: {:?}", event.event_type());
}
}
}

Some((node, Ok(event))) = keyboard_stream_map.next() => {
let keyboard_state = &mut keyboard_states.get_mut(&node).expect("device not in states map");

let key = match event.kind() {
InputEventKind::Key(keycode) => keycode,
Expand Down

0 comments on commit 605c935

Please sign in to comment.