did stuff
This commit is contained in:
parent
7748132fca
commit
c4ebaa35be
4 changed files with 42 additions and 26 deletions
|
@ -41,6 +41,7 @@ fn main() {
|
|||
let stdin = stdin();
|
||||
|
||||
loop {
|
||||
// TODO: Unify with macro_writer using trait objects
|
||||
let mut line = String::new();
|
||||
stdin
|
||||
.read_line(&mut line)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::macro_writer::MacroWriter;
|
||||
use crate::x11_safe_wrapper::XDisplay;
|
||||
use crate::{Instructions, Keycode, Position};
|
||||
use std::mem::size_of;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use x11::xlib::Time;
|
||||
use std::mem;
|
||||
use std::time;
|
||||
use x11::xlib;
|
||||
use x11::xrecord::{XRecordContext, XRecordInterceptData};
|
||||
|
||||
#[repr(C)]
|
||||
|
@ -13,11 +13,11 @@ pub struct EvCallbackData {
|
|||
pub recdpy: XDisplay,
|
||||
pub ctx: XRecordContext,
|
||||
pub working: bool,
|
||||
pub last_event: Time,
|
||||
pub last_event: xlib::Time,
|
||||
pub pos: Position<i16>,
|
||||
pub stop_key: Keycode,
|
||||
pub ev_nr: u32,
|
||||
pub max_delay: Option<Time>,
|
||||
pub max_delay: Option<xlib::Time>,
|
||||
pub no_keypress_yet: bool,
|
||||
pub moving: bool,
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ impl EvCallbackData {
|
|||
ctx: XRecordContext,
|
||||
stop_key: Keycode,
|
||||
pos: Position<i16>,
|
||||
max_delay: Option<Time>,
|
||||
max_delay: Option<xlib::Time>,
|
||||
) -> Self {
|
||||
EvCallbackData {
|
||||
writer,
|
||||
|
@ -43,10 +43,10 @@ impl EvCallbackData {
|
|||
pos,
|
||||
max_delay,
|
||||
no_keypress_yet: true,
|
||||
last_event: SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
last_event: time::SystemTime::now()
|
||||
.duration_since(time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as Time,
|
||||
.as_millis() as xlib::Time,
|
||||
moving: false,
|
||||
}
|
||||
}
|
||||
|
@ -59,8 +59,8 @@ impl EvCallbackData {
|
|||
&mut self,
|
||||
intercept_data: &mut XRecordInterceptData,
|
||||
) -> Position<i16> {
|
||||
self.pos.0 = *((intercept_data.data as usize + size_of::<i16>() * 10) as *const i16);
|
||||
self.pos.1 = *((intercept_data.data as usize + size_of::<i16>() * 11) 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 + mem::size_of::<i16>() * 11) as *const i16);
|
||||
self.pos
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ impl EvCallbackData {
|
|||
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 {
|
||||
self.writer.write(Instructions::Delay(calculate_delay(
|
||||
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 {
|
||||
let max = max as u64;
|
||||
let delay = server_time - last_event;
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
use crate::Instructions;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::{io, fs};
|
||||
|
||||
pub struct MacroWriter {
|
||||
outfile: Box<dyn Write>,
|
||||
outfile: Box<dyn io::Write>,
|
||||
ignore_delay_capturing: bool,
|
||||
}
|
||||
|
||||
impl MacroWriter {
|
||||
pub fn new(outfile: Option<std::path::PathBuf>, ignore_delay_capturing: bool) -> Self {
|
||||
Self {
|
||||
outfile: if let Some(outfile) = outfile {
|
||||
Box::new(File::create(outfile).expect("Failed to create output file"))
|
||||
outfile:
|
||||
if let Some(outfile) = outfile {
|
||||
Box::new(fs::File::create(outfile).expect("Failed to create output file"))
|
||||
} else {
|
||||
Box::new(io::stdout())
|
||||
},
|
||||
|
@ -22,9 +21,7 @@ impl MacroWriter {
|
|||
|
||||
pub fn write(&mut self, instruction: Instructions) {
|
||||
if self.ignore_delay_capturing {
|
||||
if let Instructions::Delay(_) = instruction {
|
||||
return;
|
||||
}
|
||||
if let Instructions::Delay(_) = instruction { () }
|
||||
}
|
||||
|
||||
writeln!(&mut self.outfile, "{}", instruction)
|
||||
|
|
|
@ -10,7 +10,7 @@ pub struct 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.
|
||||
pub fn open(display_name: Option<String>) -> anyhow::Result<Self> {
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue