From 063ae7c942a9ee2f1f9684e387a850535eb65e31 Mon Sep 17 00:00:00 2001 From: Konkitoman Date: Sat, 9 Mar 2024 22:46:22 +0200 Subject: [PATCH] Changelog 0.1.7: * minor refactoring * fix typos * added `-D` for specifing the device dev path and `-K`, `-M` to set the device type, this was requested by @lnee94 --- .gitignore | 1 + Cargo.lock | 2 +- Cargo.toml | 2 +- src/args.rs | 16 ++++++++++++++-- src/device.rs | 2 +- src/lib.rs | 44 +++++++++++++++++++++++++++++++++++++------- src/main.rs | 23 +++++++++++++++++------ 7 files changed, 72 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..57ff555 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/target_ra diff --git a/Cargo.lock b/Cargo.lock index 21b4cb9..80ab60c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,7 +258,7 @@ dependencies = [ [[package]] name = "theclicker" -version = "0.1.6" +version = "0.1.7" dependencies = [ "clap", "input-linux", diff --git a/Cargo.toml b/Cargo.toml index 5d265be..1ea6d6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "theclicker" -version = "0.1.6" +version = "0.1.7" edition = "2021" license = "MIT" authors = ["konkitoman"] diff --git a/src/args.rs b/src/args.rs index c7b24db..93905f3 100644 --- a/src/args.rs +++ b/src/args.rs @@ -34,11 +34,11 @@ pub struct Args { #[arg(long, default_value_t = false)] pub debug: bool, - /// Grabs the input device and emulates any unused action from it + /// I will not: grab the input device and emulates any unused action from it #[arg(long, default_value_t = false)] pub no_grab: bool, - /// This will let the keyboard to be grabed! + /// This will let the keyboard to be grabbed! #[arg(long, default_value_t = false)] pub grab_kbd: bool, @@ -46,4 +46,16 @@ pub struct Args { /// (first looks for exact match, then takes the first device that contains the string) #[arg(short = 'd', long)] pub use_device: Option, + + /// This will try to open the specified dev path, you need to specify what type of device is! + #[arg(short = 'D')] + pub use_dev_path: Option, + + /// Set the device type as Keyboard + #[arg(short = 'K', default_value_t = false)] + pub is_keyboard: bool, + + /// Set the device type as Mouse + #[arg(short = 'M', default_value_t = false)] + pub is_mouse: bool, } diff --git a/src/device.rs b/src/device.rs index d22eab3..c33c48a 100644 --- a/src/device.rs +++ b/src/device.rs @@ -281,7 +281,7 @@ impl Device { .create(true) .open("/tmp/TheClicker") .unwrap(); - let mut args = crate::args::Args::parse(); + let mut args = crate::Args::parse(); args.use_device = Some(device.name.clone()); args.clear_cache = false; diff --git a/src/lib.rs b/src/lib.rs index 93d7898..11e5a2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,18 @@ -pub mod args; +mod args; mod device; +pub use args::Args; + use std::{ io::{stdout, IsTerminal}, path::PathBuf, + str::FromStr, sync::{mpsc, Arc}, thread, time::Duration, }; -use crate::device::Device; +pub use device::{Device, DeviceType}; use input_linux::{sys::input_event, Key, KeyState}; #[derive(Default)] @@ -32,6 +35,8 @@ pub struct StateArgs { pub grab: bool, pub grab_kbd: bool, pub use_device: Option, + pub use_dev_path: Option, + pub device_type: Option, } pub struct State { @@ -62,18 +67,43 @@ impl State { grab, use_device, grab_kbd, + use_dev_path, + device_type, }: StateArgs, ) -> Self { let input; - if let Some(device_name) = use_device { - if let Some(device) = Device::find_device(&device_name) { - input = device; - } else { + 'try_set_input: { + if let Some(dev_path) = use_dev_path { + let Ok(path) = PathBuf::from_str(&dev_path) else { + eprintln!("Cannot make {dev_path} to path, invalid path"); + std::process::exit(3); + }; + + let Some(device_type) = device_type else { + eprintln!("You didn't specified the device type!"); + std::process::exit(5); + }; + + if let Ok(device) = Device::dev_open(path, device_type) { + input = device; + break 'try_set_input; + } + + eprintln!("Cannot open device: {dev_path}"); + std::process::exit(2); + } + + if let Some(device_name) = use_device { + if let Some(device) = Device::find_device(&device_name) { + input = device; + break 'try_set_input; + } + eprintln!("Cannot find device: {device_name}"); std::process::exit(1); } - } else { + input = Device::select_device(); } diff --git a/src/main.rs b/src/main.rs index cdf1264..367af05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,10 @@ -pub mod args; -pub mod device; - use std::{ fs::{self, File}, io::Read, }; use clap::Parser; -use theclicker::State; - -use crate::args::Args; +use theclicker::{Args, DeviceType, State}; fn main() { let Args { @@ -24,6 +19,9 @@ fn main() { mut no_grab, mut use_device, mut grab_kbd, + mut use_dev_path, + mut is_keyboard, + mut is_mouse, } = Args::parse(); if clear_cache { @@ -48,6 +46,9 @@ fn main() { no_grab, grab_kbd, use_device, + use_dev_path, + is_keyboard, + is_mouse, } = ron::from_str::(&string).unwrap(); } @@ -65,6 +66,16 @@ fn main() { grab, use_device, grab_kbd, + use_dev_path, + device_type: match (is_mouse, is_keyboard) { + (true, false) => Some(DeviceType::Mouse), + (false, true) => Some(DeviceType::Keyboard), + (false, false) => None, + _ => { + eprintln!("You cannot set both -K and -M"); + std::process::exit(4) + } + }, }); println!();