added macro playing delay!!!!!!!!1

This commit is contained in:
Schrottkatze 2022-07-25 17:14:00 +02:00
parent b9770f43f1
commit f52e2914d7
2 changed files with 125 additions and 130 deletions

View file

@ -16,24 +16,24 @@ struct Args {
#[clap(value_parser, value_name = "input_file", value_hint = clap::ValueHint::FilePath)]
input_file: Option<std::path::PathBuf>,
/// Display to run the macro on. This uses the $DISPLAY environment variable by default.
#[clap(short = "D", long)]
#[clap(short = 'D', long)]
display: Option<String>,
// Delay for events to be sent.
// #[clap(short, long)]
// event_delay: Option<u16>,
/// Delay for events to be sent.
#[clap(short, long)]
delay: Option<u64>,
}
fn main() {
let args = Args::parse();
let display = get_remote(args.display);
let delay = args.delay.unwrap_or(10);
if let Some(input_file_path) = args.input_file {
let input_file_contents = fs::read_to_string(input_file_path)
.expect("Couldn't read macro file");
let input_file_contents = fs::read_to_string(input_file_path).expect("Couldn't read macro file");
for instruction in input_file_contents.lines() {
run_instruction(instruction, &display);
run_instruction(instruction, &display, delay);
}
} else {
println!("No input file specified, reading from stdin.");
@ -45,7 +45,7 @@ fn main() {
// Without this it crashes because apparently it doesn't properly read the next input line?
println!();
line = line.trim().to_string();
run_instruction(&*line, &display);
run_instruction(&*line, &display, delay);
}
}
@ -67,39 +67,34 @@ fn get_remote(display_name: Option<String>) -> XDisplay {
display
}
fn run_instruction(instruction: &str, dpy: &XDisplay) {
fn run_instruction(instruction: &str, dpy: &XDisplay, delay: u64) {
let instruction_split: Vec<&str> = instruction.split(' ').collect();
match instruction_split[0] {
"Delay" => thread::sleep(Duration::from_millis(instruction_split[1].parse().unwrap())),
"ButtonPress" => dpy.send_fake_buttonpress(instruction_split[1].parse().unwrap()),
"ButtonRelease" => dpy.send_fake_buttonrelease(instruction_split[1].parse().unwrap()),
"MotionNotify" => dpy
.send_fake_motion_event(instruction_split[1].parse().unwrap(), instruction_split[2].parse().unwrap()),
"KeyCodePress" => dpy.send_fake_keypress_from_code(instruction_split[1].parse().unwrap()),
"KeyCodeRelease" => dpy.send_fake_keyrelease_from_code(instruction_split[1].parse().unwrap()),
"KeySymPress" => dpy.send_fake_keypress_from_keysym(instruction_split[1].parse().unwrap()),
"KeySymRelease" => {
dpy.send_fake_keyrelease_from_keysym(instruction_split[1].parse().unwrap())
}
"ButtonPress" => dpy.send_fake_buttonpress(instruction_split[1].parse().unwrap(), delay),
"ButtonRelease" => dpy.send_fake_buttonrelease(instruction_split[1].parse().unwrap(), delay),
"MotionNotify" => dpy.send_fake_motion_event(instruction_split[1].parse().unwrap(), instruction_split[2].parse().unwrap(), delay),
"KeyCodePress" => dpy.send_fake_keypress_from_code(instruction_split[1].parse().unwrap(), delay),
"KeyCodeRelease" => dpy.send_fake_keyrelease_from_code(instruction_split[1].parse().unwrap(), delay),
"KeySymPress" => dpy.send_fake_keypress_from_keysym(instruction_split[1].parse().unwrap(), delay),
"KeySymRelease" => dpy.send_fake_keyrelease_from_keysym(instruction_split[1].parse().unwrap(), delay),
"KeySym" => {
let key: Keysym = instruction_split[1].parse().unwrap();
dpy.send_fake_keypress_from_keysym(key);
dpy.send_fake_keyrelease_from_keysym(key);
dpy.send_fake_keypress_from_keysym(key, delay);
dpy.send_fake_keyrelease_from_keysym(key, delay);
}
"KeyStrPress" => {
dpy.send_fake_keypress_from_string(CString::new(instruction_split[1]).unwrap().as_bytes())
}
"KeyStrRelease" => dpy
.send_fake_keyrelease_from_string(CString::new(instruction_split[1]).unwrap().as_bytes()),
"KeyStrPress" => dpy.send_fake_keypress_from_string(CString::new(instruction_split[1]).unwrap().as_bytes(), delay),
"KeyStrRelease" => dpy.send_fake_keyrelease_from_string(CString::new(instruction_split[1]).unwrap().as_bytes(), delay),
"KeyStr" => {
let keystring = CString::new(instruction_split[1]).unwrap();
dpy.send_fake_keypress_from_string(keystring.as_bytes());
dpy.send_fake_keyrelease_from_string(keystring.as_bytes());
dpy.send_fake_keypress_from_string(keystring.as_bytes(), delay);
dpy.send_fake_keyrelease_from_string(keystring.as_bytes(), delay);
}
"String" => {
for c in instruction["String".len() + 1..].chars() {
send_char(dpy, c);
send_char(dpy, c, delay);
}
}
"ExecBlock" | "ExecNoBlock" => {
@ -117,7 +112,7 @@ fn run_instruction(instruction: &str, dpy: &XDisplay) {
}
}
fn send_char(dpy: &XDisplay, c: char) {
fn send_char(dpy: &XDisplay, c: char, delay: u64) {
// get keystring from character and turn it into a keysym
let keysym = string_to_keysym(CHARTBL[c as usize].as_ref());
let keycode = dpy.keysym_to_keycode(keysym);
@ -144,8 +139,8 @@ fn send_char(dpy: &XDisplay, c: char) {
shift_needed = false;
}
if shift_needed { dpy.send_fake_keypress_from_keysym(XK_Shift_L as Keysym); }
dpy.send_fake_keypress_from_code(keycode);
dpy.send_fake_keyrelease_from_code(keycode);
if shift_needed { dpy.send_fake_keyrelease_from_keysym(XK_Shift_L as Keysym); }
if shift_needed { dpy.send_fake_keypress_from_keysym(XK_Shift_L as Keysym, delay); }
dpy.send_fake_keypress_from_code(keycode, delay);
dpy.send_fake_keyrelease_from_code(keycode, delay);
if shift_needed { dpy.send_fake_keyrelease_from_keysym(XK_Shift_L as Keysym, delay); }
}

View file

@ -133,42 +133,42 @@ impl XDisplay {
has_extension != 0
}
pub fn send_fake_keypress_from_string(&self, string: &[u8]) {
self.send_fake_keypress_from_keysym(string_to_keysym(string))
pub fn send_fake_keypress_from_string(&self, string: &[u8], delay: u64) {
self.send_fake_keypress_from_keysym(string_to_keysym(string), delay)
}
pub fn send_fake_keypress_from_keysym(&self, ks: Keysym) {
self.send_fake_keypress_from_code(self.keysym_to_keycode(ks))
pub fn send_fake_keypress_from_keysym(&self, ks: Keysym, delay: u64) {
self.send_fake_keypress_from_code(self.keysym_to_keycode(ks), delay)
}
pub fn send_fake_keypress_from_code(&self, code: Keycode) {
unsafe { XTestFakeKeyEvent(self.ptr, code, TRUE_C, 10) };
pub fn send_fake_keypress_from_code(&self, code: Keycode, delay: u64) {
unsafe { XTestFakeKeyEvent(self.ptr, code, TRUE_C, delay) };
self.flush();
}
pub fn send_fake_buttonpress(&self, button: u32) {
unsafe { XTestFakeButtonEvent(self.ptr, button, TRUE_C, 10) };
pub fn send_fake_buttonpress(&self, button: u32, delay: u64) {
unsafe { XTestFakeButtonEvent(self.ptr, button, TRUE_C, delay) };
}
pub fn send_fake_buttonrelease(&self, button: u32) {
unsafe { XTestFakeButtonEvent(self.ptr, button, FALSE_C, 10) };
pub fn send_fake_buttonrelease(&self, button: u32, delay: u64) {
unsafe { XTestFakeButtonEvent(self.ptr, button, FALSE_C, delay) };
}
pub fn send_fake_keyrelease_from_string(&self, string: &[u8]) {
self.send_fake_keyrelease_from_keysym(string_to_keysym(string))
pub fn send_fake_keyrelease_from_string(&self, string: &[u8], delay: u64) {
self.send_fake_keyrelease_from_keysym(string_to_keysym(string), delay)
}
pub fn send_fake_keyrelease_from_keysym(&self, ks: Keysym) {
self.send_fake_keyrelease_from_code(self.keysym_to_keycode(ks))
pub fn send_fake_keyrelease_from_keysym(&self, ks: Keysym, delay: u64) {
self.send_fake_keyrelease_from_code(self.keysym_to_keycode(ks), delay)
}
pub fn send_fake_keyrelease_from_code(&self, code: Keycode) {
unsafe { XTestFakeKeyEvent(self.ptr, code, FALSE_C, 10) };
pub fn send_fake_keyrelease_from_code(&self, code: Keycode, delay: u64) {
unsafe { XTestFakeKeyEvent(self.ptr, code, FALSE_C, delay) };
self.flush();
}
pub fn send_fake_motion_event(&self, x: i32, y: i32) {
unsafe { XTestFakeMotionEvent(self.ptr, -1, x, y, 10) };
pub fn send_fake_motion_event(&self, x: i32, y: i32, delay: u64) {
unsafe { XTestFakeMotionEvent(self.ptr, -1, x, y, delay) };
self.flush();
}