implemented support for ignoring delays when recording macros

This commit is contained in:
Schrottkatze 2022-07-11 11:43:46 +02:00
parent 6cf826f936
commit d8c8a7606e

View file

@ -23,8 +23,14 @@ struct Args {
#[clap(value_parser, value_name = "output_file", value_hint = clap::ValueHint::FilePath)] #[clap(value_parser, value_name = "output_file", value_hint = clap::ValueHint::FilePath)]
output_file: Option<std::path::PathBuf>, output_file: Option<std::path::PathBuf>,
/// Display to run the macro on. This uses the $DISPLAY environment variable by default. /// Display to run the macro on. This uses the $DISPLAY environment variable by default.
#[clap(short, long)] #[clap(short='D', long)]
display: Option<String>, display: Option<String>,
/// Max Delay in milliseconds for macro delays
#[clap(short, long)]
max_delay: Option<u64>,
/// Allow delay capturing in recording output. If this flag is set, the program will ignore the max_delay.
#[clap(short, long)]
ignore_delay_capturing: bool,
} }
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
@ -37,13 +43,15 @@ fn main() {
let screen = display.get_default_screen(); let screen = display.get_default_screen();
dbg!(stop_key); dbg!(stop_key);
// check if the output file argument is specified, if not use stdout
let mut outfile: Box<dyn Write> = if let Some(outfile) = args.output_file { let mut outfile: Box<dyn Write> = if let Some(outfile) = args.output_file {
Box::new(File::create(outfile).expect("Failed to create output file")) Box::new(File::create(outfile).expect("Failed to create output file"))
} else { } else {
Box::new(io::stdout()) Box::new(io::stdout())
}; };
ev_loop(display, recorded_display, screen, stop_key, outfile); event_loop(display, recorded_display, screen, stop_key, outfile, args.ignore_delay_capturing);
display.close(); display.close();
} }
@ -67,13 +75,11 @@ fn get_stop_key(display: XDisplay) -> Keycode {
let ev = display.window_event(root, KeyPressMask); let ev = display.window_event(root, KeyPressMask);
unsafe { unsafe {
match ev { //if it's a key event, save the keycode and break out of the loop
XEvent { key } => { if let XEvent { key } = ev {
stop_key = key.keycode; stop_key = key.keycode;
break; break;
} }
_ => {},
}
} }
} }
@ -83,8 +89,8 @@ fn get_stop_key(display: XDisplay) -> Keycode {
stop_key stop_key
} }
fn ev_loop(display: XDisplay, recordeddpy: XDisplay, screen: i32, stop_key: Keycode, mut outfile: Box<dyn Write>) { fn event_loop(display: XDisplay, recordeddpy: XDisplay, screen: i32, stop_key: Keycode, mut outfile: Box<dyn Write>, ignore_delay_capturing: bool) {
let root = display.get_root_window(screen); // let root = display.get_root_window(screen);
let protocol_ranges = unsafe { XRecordAllocRange() }; let protocol_ranges = unsafe { XRecordAllocRange() };
let pointer_pos = display.query_pointer_pos(); let pointer_pos = display.query_pointer_pos();
@ -105,6 +111,7 @@ fn ev_loop(display: XDisplay, recordeddpy: XDisplay, screen: i32, stop_key: Keyc
working: true, working: true,
x: pointer_pos.0 as i16, x: pointer_pos.0 as i16,
y: pointer_pos.1 as i16, y: pointer_pos.1 as i16,
ignore_delay_capturing,
no_keypress_yet: true, no_keypress_yet: true,
last_event: 0, last_event: 0,
moving: false, moving: false,
@ -133,6 +140,7 @@ pub struct EvCallbackData {
ctx: XRecordContext, ctx: XRecordContext,
x: i16, x: i16,
y: i16, y: i16,
ignore_delay_capturing: bool,
no_keypress_yet: bool, no_keypress_yet: bool,
last_event: u64, last_event: u64,
moving: bool, moving: bool,
@ -183,15 +191,13 @@ unsafe extern "C" fn ev_callback(closure: *mut c_char, intercept_data: *mut XRec
let keyname = data.xdpy.keycode_to_string(kc as u32); let keyname = data.xdpy.keycode_to_string(kc as u32);
if ev_type == KEYPRESS_U8 && kc == data.stop_key as u8 { if ev_type == KEYPRESS_U8 && kc == data.stop_key as u8 {
// println!("stop key detected, recording done!");
data.working = false; data.working = false;
} else { } else {
if ev_type == KEYPRESS_U8 { if ev_type == KEYPRESS_U8 {
data.no_keypress_yet = false; data.no_keypress_yet = false;
} }
if (intercept_data.server_time - data.last_event) != 0 { if !data.ignore_delay_capturing && (intercept_data.server_time - data.last_event) != 0 {
println!("test");
writeln!(&mut data.outfile,"Delay {}", intercept_data.server_time - data.last_event) writeln!(&mut data.outfile,"Delay {}", intercept_data.server_time - data.last_event)
.expect("Failed to write to outfile."); .expect("Failed to write to outfile.");
data.last_event = intercept_data.server_time; data.last_event = intercept_data.server_time;
@ -214,8 +220,7 @@ unsafe extern "C" fn ev_callback(closure: *mut c_char, intercept_data: *mut XRec
BUTTONPRESS_U8 | BUTTONRELEASE_U8 => { BUTTONPRESS_U8 | BUTTONRELEASE_U8 => {
let bc: u8 = *((intercept_data.data as usize + 1) as *const u8); let bc: u8 = *((intercept_data.data as usize + 1) as *const u8);
if (intercept_data.server_time - data.last_event) != 0 { if !data.ignore_delay_capturing && (intercept_data.server_time - data.last_event) != 0 {
println!("testb");
writeln!(&mut data.outfile,"Delay {}", intercept_data.server_time - data.last_event) writeln!(&mut data.outfile,"Delay {}", intercept_data.server_time - data.last_event)
.expect("Failed to write to outfile."); .expect("Failed to write to outfile.");
data.last_event = intercept_data.server_time; data.last_event = intercept_data.server_time;