Add --mouse option to select mice by default

pull/140/head
Frederick Zhang 2 years ago
parent 42d0842187
commit 5b0ad05960
No known key found for this signature in database
GPG Key ID: 980A192C361BE1AE

@ -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,9 @@ 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 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 +254,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())
}

@ -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),
};
@ -136,13 +140,19 @@ fn main() -> anyhow::Result<()> {
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),
};
@ -217,12 +227,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,6 +249,7 @@ fn handle_config_changes(
input_devices: &mut HashMap<PathBuf, InputDevice>,
device_filter: &[String],
ignore_filter: &[String],
mouse: bool,
config_path: &PathBuf,
) -> anyhow::Result<bool> {
for event in &events {
@ -256,7 +268,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