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)]
output_file: Option<std::path::PathBuf>,
/// Display to run the macro on. This uses the $DISPLAY environment variable by default.
#[clap(short, long)]
#[clap(short='D', long)]
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() {
let args = Args::parse();
@ -37,13 +43,15 @@ fn main() {
let screen = display.get_default_screen();
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 {
Box::new(File::create(outfile).expect("Failed to create output file"))
} else {
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();
}
@ -67,13 +75,11 @@ fn get_stop_key(display: XDisplay) -> Keycode {
let ev = display.window_event(root, KeyPressMask);
unsafe {
match ev {
XEvent { key } => {
//if it's a key event, save the keycode and break out of the loop
if let XEvent { key } = ev {
stop_key = key.keycode;
break;
}
_ => {},
}
}
}
@ -83,8 +89,8 @@ fn get_stop_key(display: XDisplay) -> Keycode {
stop_key
}
fn ev_loop(display: XDisplay, recordeddpy: XDisplay, screen: i32, stop_key: Keycode, mut outfile: Box<dyn Write>) {
let root = display.get_root_window(screen);
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 protocol_ranges = unsafe { XRecordAllocRange() };
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,
x: pointer_pos.0 as i16,
y: pointer_pos.1 as i16,
ignore_delay_capturing,
no_keypress_yet: true,
last_event: 0,
moving: false,
@ -133,6 +140,7 @@ pub struct EvCallbackData {
ctx: XRecordContext,
x: i16,
y: i16,
ignore_delay_capturing: bool,
no_keypress_yet: bool,
last_event: u64,
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);
if ev_type == KEYPRESS_U8 && kc == data.stop_key as u8 {
// println!("stop key detected, recording done!");
data.working = false;
} else {
if ev_type == KEYPRESS_U8 {
data.no_keypress_yet = false;
}
if (intercept_data.server_time - data.last_event) != 0 {
println!("test");
if !data.ignore_delay_capturing && (intercept_data.server_time - data.last_event) != 0 {
writeln!(&mut data.outfile,"Delay {}", intercept_data.server_time - data.last_event)
.expect("Failed to write to outfile.");
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 => {
let bc: u8 = *((intercept_data.data as usize + 1) as *const u8);
if (intercept_data.server_time - data.last_event) != 0 {
println!("testb");
if !data.ignore_delay_capturing && (intercept_data.server_time - data.last_event) != 0 {
writeln!(&mut data.outfile,"Delay {}", intercept_data.server_time - data.last_event)
.expect("Failed to write to outfile.");
data.last_event = intercept_data.server_time;