did stuff

This commit is contained in:
Schrottkatze 2022-10-10 20:36:25 +02:00
parent 7748132fca
commit c4ebaa35be
4 changed files with 42 additions and 26 deletions

View file

@ -41,6 +41,7 @@ fn main() {
let stdin = stdin(); let stdin = stdin();
loop { loop {
// TODO: Unify with macro_writer using trait objects
let mut line = String::new(); let mut line = String::new();
stdin stdin
.read_line(&mut line) .read_line(&mut line)

View file

@ -1,9 +1,9 @@
use crate::macro_writer::MacroWriter; use crate::macro_writer::MacroWriter;
use crate::x11_safe_wrapper::XDisplay; use crate::x11_safe_wrapper::XDisplay;
use crate::{Instructions, Keycode, Position}; use crate::{Instructions, Keycode, Position};
use std::mem::size_of; use std::mem;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time;
use x11::xlib::Time; use x11::xlib;
use x11::xrecord::{XRecordContext, XRecordInterceptData}; use x11::xrecord::{XRecordContext, XRecordInterceptData};
#[repr(C)] #[repr(C)]
@ -13,11 +13,11 @@ pub struct EvCallbackData {
pub recdpy: XDisplay, pub recdpy: XDisplay,
pub ctx: XRecordContext, pub ctx: XRecordContext,
pub working: bool, pub working: bool,
pub last_event: Time, pub last_event: xlib::Time,
pub pos: Position<i16>, pub pos: Position<i16>,
pub stop_key: Keycode, pub stop_key: Keycode,
pub ev_nr: u32, pub ev_nr: u32,
pub max_delay: Option<Time>, pub max_delay: Option<xlib::Time>,
pub no_keypress_yet: bool, pub no_keypress_yet: bool,
pub moving: bool, pub moving: bool,
} }
@ -30,7 +30,7 @@ impl EvCallbackData {
ctx: XRecordContext, ctx: XRecordContext,
stop_key: Keycode, stop_key: Keycode,
pos: Position<i16>, pos: Position<i16>,
max_delay: Option<Time>, max_delay: Option<xlib::Time>,
) -> Self { ) -> Self {
EvCallbackData { EvCallbackData {
writer, writer,
@ -43,10 +43,10 @@ impl EvCallbackData {
pos, pos,
max_delay, max_delay,
no_keypress_yet: true, no_keypress_yet: true,
last_event: SystemTime::now() last_event: time::SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(time::UNIX_EPOCH)
.unwrap() .unwrap()
.as_millis() as Time, .as_millis() as xlib::Time,
moving: false, moving: false,
} }
} }
@ -59,8 +59,8 @@ impl EvCallbackData {
&mut self, &mut self,
intercept_data: &mut XRecordInterceptData, intercept_data: &mut XRecordInterceptData,
) -> Position<i16> { ) -> Position<i16> {
self.pos.0 = *((intercept_data.data as usize + size_of::<i16>() * 10) as *const i16); self.pos.0 = *((intercept_data.data as usize + mem::size_of::<i16>() * 10) as *const i16);
self.pos.1 = *((intercept_data.data as usize + size_of::<i16>() * 11) as *const i16); self.pos.1 = *((intercept_data.data as usize + mem::size_of::<i16>() * 11) as *const i16);
self.pos self.pos
} }
@ -69,7 +69,7 @@ impl EvCallbackData {
self.moving = false; self.moving = false;
} }
pub fn maybe_write_delay(&mut self, server_time: Time) { pub fn maybe_write_delay(&mut self, server_time: xlib::Time) {
if server_time - self.last_event > 1 { if server_time - self.last_event > 1 {
self.writer.write(Instructions::Delay(calculate_delay( self.writer.write(Instructions::Delay(calculate_delay(
server_time, server_time,
@ -81,7 +81,7 @@ impl EvCallbackData {
} }
} }
fn calculate_delay(server_time: Time, last_event: Time, max_delay: Option<Time>) -> Time { fn calculate_delay(server_time: xlib::Time, last_event: xlib::Time, max_delay: Option<xlib::Time>) -> xlib::Time {
if let Some(max) = max_delay { if let Some(max) = max_delay {
let max = max as u64; let max = max as u64;
let delay = server_time - last_event; let delay = server_time - last_event;

View file

@ -1,30 +1,27 @@
use crate::Instructions; use crate::Instructions;
use std::fs::File; use std::{io, fs};
use std::io;
use std::io::Write;
pub struct MacroWriter { pub struct MacroWriter {
outfile: Box<dyn Write>, outfile: Box<dyn io::Write>,
ignore_delay_capturing: bool, ignore_delay_capturing: bool,
} }
impl MacroWriter { impl MacroWriter {
pub fn new(outfile: Option<std::path::PathBuf>, ignore_delay_capturing: bool) -> Self { pub fn new(outfile: Option<std::path::PathBuf>, ignore_delay_capturing: bool) -> Self {
Self { Self {
outfile: if let Some(outfile) = outfile { outfile:
Box::new(File::create(outfile).expect("Failed to create output file")) if let Some(outfile) = outfile {
} else { Box::new(fs::File::create(outfile).expect("Failed to create output file"))
Box::new(io::stdout()) } else {
}, Box::new(io::stdout())
},
ignore_delay_capturing, ignore_delay_capturing,
} }
} }
pub fn write(&mut self, instruction: Instructions) { pub fn write(&mut self, instruction: Instructions) {
if self.ignore_delay_capturing { if self.ignore_delay_capturing {
if let Instructions::Delay(_) = instruction { if let Instructions::Delay(_) = instruction { () }
return;
}
} }
writeln!(&mut self.outfile, "{}", instruction) writeln!(&mut self.outfile, "{}", instruction)

View file

@ -10,7 +10,7 @@ pub struct Display {
} }
impl Display { impl Display {
/// Call XOpenDisplay to open a display. /// Call XOpenDisplay to open a connection to the X Server.
/// If `display_name` is `None`, the value of the `DISPLAY` environment variable will be used. /// If `display_name` is `None`, the value of the `DISPLAY` environment variable will be used.
pub fn open(display_name: Option<String>) -> anyhow::Result<Self> { pub fn open(display_name: Option<String>) -> anyhow::Result<Self> {
let name = ffi::CString::new(if let Some(name) = display_name { let name = ffi::CString::new(if let Some(name) = display_name {
@ -34,6 +34,24 @@ impl Display {
}) })
} }
} }
/// Calls XFlush to flush the output buffer.
pub fn flush(&self) {
unsafe {
xlib::XFlush(self.ptr)
}
}
// TODO: Figure out how to properly handle errors
/// Calls XSync to flush the output buffer and then wait until all events have been received and processed
/// by the server.
/// The `discard` parameter specifies, whether to discard all events in the queue.
pub fn sync(&self, discard: bool) {
unsafe {
xlib::XSync(self.ptr, discard.into());
}
}
} }
impl Drop for Display { impl Drop for Display {