Merge pull request #140 from Frederick888/mice-option

Add --mouse command line option
pull/104/head
Takashi Kokubun 2 years ago committed by GitHub
commit 8c19b0bb88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -50,6 +50,19 @@ jobs:
- name: cargo fmt
run: cargo fmt -- --check
clippy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: clippy
- name: cargo clippy
run: cargo clippy --locked --tests -- -D warnings
test:
runs-on: ubuntu-18.04
steps:

@ -31,10 +31,10 @@ where
D: Deserializer<'de>,
{
let action = RemapActions::deserialize(deserializer)?;
return Ok(Remap {
Ok(Remap {
remap: action.remap.into_iter().map(|(k, v)| (k, v.into_vec())).collect(),
timeout: action.timeout_millis.map(|ms| Duration::from_millis(ms)),
});
timeout: action.timeout_millis.map(Duration::from_millis),
})
}
fn deserialize_launch<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>

@ -178,6 +178,6 @@ fn test_keymap_mark() {
fn assert_parse(yaml: &str) {
let result: Result<Config, Error> = serde_yaml::from_str(yaml);
if let Err(e) = result {
assert!(false, "{}", e)
panic!("{}", e)
}
}

@ -76,6 +76,7 @@ pub fn device_watcher(watch: bool) -> anyhow::Result<Option<Inotify>> {
pub fn get_input_devices(
device_opts: &[String],
ignore_opts: &[String],
mouse: bool,
watch: bool,
) -> anyhow::Result<HashMap<PathBuf, InputDevice>> {
let mut devices: Vec<_> = InputDevice::devices()?.collect();
@ -87,7 +88,11 @@ pub fn get_input_devices(
println!("{}", SEPARATOR);
if device_opts.is_empty() {
print!("Selected keyboards automatically since --device options weren't specified");
if mouse {
print!("Selected keyboards and mice automatically since --device options weren't specified");
} else {
print!("Selected keyboards automatically since --device options weren't specified");
}
} else {
print!("Selected devices matching {:?}", device_opts);
};
@ -103,7 +108,7 @@ pub fn get_input_devices(
// alternative is `Vec::retain_mut` whenever that gets stabilized
.filter_map(|mut device| {
// filter out any not matching devices and devices that error on grab
(device.is_input_device(device_opts, ignore_opts) && device.grab()).then(|| device)
(device.is_input_device(device_opts, ignore_opts, mouse) && device.grab()).then(|| device)
})
.collect();
@ -188,9 +193,12 @@ impl InputDevice {
}
impl InputDevice {
pub fn is_input_device(&self, device_filter: &[String], ignore_filter: &[String]) -> bool {
pub fn is_input_device(&self, device_filter: &[String], ignore_filter: &[String], mouse: bool) -> bool {
if self.device_name() == Self::current_name() {
return false;
}
(if device_filter.is_empty() {
self.is_keyboard()
self.is_keyboard() || (mouse && self.is_mouse())
} else {
self.matches(device_filter)
}) && (ignore_filter.is_empty() || !self.matches(ignore_filter))
@ -249,6 +257,12 @@ impl InputDevice {
}
}
fn is_mouse(&self) -> bool {
self.device
.supported_keys()
.map_or(false, |keys| keys.contains(Key::BTN_LEFT))
}
pub fn print(&self) {
println!("{:18}: {}", self.path.display(), self.device_name())
}

@ -145,7 +145,7 @@ impl EventHandler {
self.pressed_keys.insert(key, event.0);
} else {
if let Some(original_key) = self.pressed_keys.get(&key) {
events[0].0 = original_key.clone();
events[0].0 = *original_key;
}
if value == RELEASE {
self.pressed_keys.remove(&key);
@ -268,7 +268,7 @@ impl EventHandler {
let expiration = Expiration::OneShot(TimeSpec::from_duration(timeout));
self.override_timer.unset()?;
self.override_timer.set(expiration, TimerSetTimeFlags::empty())?;
self.override_timeout_key = Some(key.clone());
self.override_timeout_key = Some(*key);
}
}
Action::Launch(command) => self.run_command(command.clone()),

@ -15,7 +15,7 @@ use nix::sys::timerfd::{ClockId, TimerFd, TimerFlags};
use std::collections::HashMap;
use std::io::stdout;
use std::os::unix::io::{AsRawFd, RawFd};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
mod client;
mod config;
@ -31,6 +31,9 @@ struct Opts {
/// Ignore a device name or path
#[clap(long, use_delimiter = true)]
ignore: Vec<String>,
/// Match mice by default
#[clap(long)]
mouse: bool,
/// Targets to watch
///
/// - device: add new devices automatically
@ -80,6 +83,7 @@ fn main() -> anyhow::Result<()> {
let Opts {
device: device_filter,
ignore: ignore_filter,
mouse,
watch,
config,
completions,
@ -107,7 +111,7 @@ fn main() -> anyhow::Result<()> {
let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty())?;
let timer_fd = timer.as_raw_fd();
let mut handler = EventHandler::new(output_device, timer, &config.default_mode);
let mut input_devices = match get_input_devices(&device_filter, &ignore_filter, watch_devices) {
let mut input_devices = match get_input_devices(&device_filter, &ignore_filter, mouse, watch_devices) {
Ok(input_devices) => input_devices,
Err(e) => bail!("Failed to prepare input devices: {}", e),
};
@ -126,23 +130,29 @@ fn main() -> anyhow::Result<()> {
}
for input_device in input_devices.values_mut() {
if readable_fds.contains(input_device.as_raw_fd()) {
if !handle_input_events(input_device, &mut handler, &mut config)? {
println!("Found a removed device. Reselecting devices.");
break 'event_loop Event::ReloadDevices;
}
if readable_fds.contains(input_device.as_raw_fd())
&& !handle_input_events(input_device, &mut handler, &mut config)?
{
println!("Found a removed device. Reselecting devices.");
break 'event_loop Event::ReloadDevices;
}
}
if let Some(inotify) = device_watcher {
if let Ok(events) = inotify.read_events() {
handle_device_changes(events, &mut input_devices, &device_filter, &ignore_filter)?;
handle_device_changes(events, &mut input_devices, &device_filter, &ignore_filter, mouse)?;
}
}
if let Some(inotify) = config_watcher {
if let Ok(events) = inotify.read_events() {
if !handle_config_changes(events, &mut input_devices, &device_filter, &ignore_filter, &config_path)?
{
if !handle_config_changes(
events,
&mut input_devices,
&device_filter,
&ignore_filter,
mouse,
&config_path,
)? {
break 'event_loop Event::ReloadConfig;
}
}
@ -152,7 +162,7 @@ fn main() -> anyhow::Result<()> {
for input_device in input_devices.values_mut() {
input_device.ungrab();
}
input_devices = match get_input_devices(&device_filter, &ignore_filter, watch_devices) {
input_devices = match get_input_devices(&device_filter, &ignore_filter, mouse, watch_devices) {
Ok(input_devices) => input_devices,
Err(e) => bail!("Failed to prepare input devices: {}", e),
};
@ -193,21 +203,19 @@ fn handle_input_events(
config: &mut Config,
) -> anyhow::Result<bool> {
match input_device.fetch_events().map_err(|e| (e.raw_os_error(), e)) {
Err((Some(ENODEV), _)) => {
return Ok(false);
}
Err((_, error)) => return Err(error).context("Error fetching input events"),
Err((Some(ENODEV), _)) => Ok(false),
Err((_, error)) => Err(error).context("Error fetching input events"),
Ok(events) => {
for event in events {
if event.event_type() == EventType::KEY {
handler
.on_event(event, &config)
.on_event(event, config)
.map_err(|e| anyhow!("Failed handling {event:?}:\n {e:?}"))?;
} else {
handler.send_event(event)?;
}
}
return Ok(true);
Ok(true)
}
}
}
@ -217,12 +225,13 @@ fn handle_device_changes(
input_devices: &mut HashMap<PathBuf, InputDevice>,
device_filter: &[String],
ignore_filter: &[String],
mouse: bool,
) -> anyhow::Result<()> {
input_devices.extend(events.into_iter().filter_map(|event| {
event.name.and_then(|name| {
let path = PathBuf::from("/dev/input/").join(name);
let mut device = InputDevice::try_from(path).ok()?;
if device.is_input_device(device_filter, &ignore_filter) && device.grab() {
if device.is_input_device(device_filter, ignore_filter, mouse) && device.grab() {
device.print();
Some(device.into())
} else {
@ -238,7 +247,8 @@ fn handle_config_changes(
input_devices: &mut HashMap<PathBuf, InputDevice>,
device_filter: &[String],
ignore_filter: &[String],
config_path: &PathBuf,
mouse: bool,
config_path: &Path,
) -> anyhow::Result<bool> {
for event in &events {
match (event.mask, &event.name) {
@ -256,7 +266,7 @@ fn handle_config_changes(
event.name.and_then(|name| {
let path = PathBuf::from("/dev/input/").join(name);
let mut device = InputDevice::try_from(path).ok()?;
if device.is_input_device(&device_filter, &ignore_filter) && device.grab() {
if device.is_input_device(device_filter, ignore_filter, mouse) && device.grab() {
device.print();
Some(device.into())
} else {

Loading…
Cancel
Save