From 697714edf3d0c3753b26d752a573df49e2504d6e Mon Sep 17 00:00:00 2001 From: Enrico Lumetti Date: Sun, 19 Apr 2020 18:34:06 +0200 Subject: [PATCH] Add program to capture using libinput --- .gitignore | 1 + Cargo.lock | 197 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 14 +++ src/bin/libinput_capture.rs | 67 ++++++++++++ 4 files changed, 279 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/bin/libinput_capture.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..703c74f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,197 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "bstr" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2889e6d50f394968c8bf4240dc3f2a7eb4680844d27308f798229ac9d4725f41" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", + "serde", +] + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + +[[package]] +name = "csv" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00affe7f6ab566df61b4be3ce8cf16bc2576bca0963ceb0955e45d514bf9a279" +dependencies = [ + "bstr", + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + +[[package]] +name = "errno" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a071601ed01b988f896ab14b95e67335d1eeb50190932a1320f7fe3cadc84e" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14ca354e36190500e1e1fb267c647932382b54053c50b14970856c0b00a35067" +dependencies = [ + "gcc", + "libc", +] + +[[package]] +name = "focus" +version = "0.1.0" +dependencies = [ + "csv", + "errno", + "input", + "libc", + "udev", +] + +[[package]] +name = "gcc" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" + +[[package]] +name = "input" +version = "0.4.1" +source = "git+https://github.com/Smithay/input.rs#521943d94bd5bd3ab7ea050883e1423ae33da378" +dependencies = [ + "bitflags", + "input-sys", + "libc", + "udev", +] + +[[package]] +name = "input-sys" +version = "1.9.0" +source = "git+https://github.com/Smithay/input.rs#521943d94bd5bd3ab7ea050883e1423ae33da378" +dependencies = [ + "libc", +] + +[[package]] +name = "itoa" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb147597cdf94ed43ab7a9038716637d2d1bf2bc571da995d0028dec06bd3018" + +[[package]] +name = "libudev-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324" +dependencies = [ + "libc", + "pkg-config", +] + +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + +[[package]] +name = "pkg-config" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" + +[[package]] +name = "regex-automata" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" +dependencies = [ + "byteorder", +] + +[[package]] +name = "ryu" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "535622e6be132bccd223f4bb2b8ac8d53cda3c7a6394944d3b2b33fb974f9d76" + +[[package]] +name = "serde" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" + +[[package]] +name = "udev" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47504d1a49b2ea1b133e7ddd1d9f0a83cf03feb9b440c2c470d06db4589cf301" +dependencies = [ + "libc", + "libudev-sys", +] + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..61359ec --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "focus" +version = "0.1.0" +authors = ["Enrico Lumetti "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +input = { git = "https://github.com/Smithay/input.rs" } +udev = "0.2" +libc = "0.2" +errno = "0.2" +csv = "1.1.3" diff --git a/src/bin/libinput_capture.rs b/src/bin/libinput_capture.rs new file mode 100644 index 0000000..0e3a019 --- /dev/null +++ b/src/bin/libinput_capture.rs @@ -0,0 +1,67 @@ +use input::{Libinput, LibinputInterface}; +use input::event::Event; +use input::event::tablet_tool::{TabletToolEvent, TabletToolEventTrait}; +use std::ffi::CString; +use std::path::Path; +use std::os::unix::io::{RawFd}; + +struct SimpleLibinputInterface; + + +impl LibinputInterface for SimpleLibinputInterface { + fn open_restricted(&mut self, path: &Path, flags: i32) -> Result { + let c_path = path.to_str() + .map(|s| CString::new(s).unwrap()) + .unwrap(); + + let fd = unsafe { + libc::open64(c_path.as_ptr(), flags) + }; + + if fd < 0 { + let err: i32 = errno::errno().into(); + std::println!("{:?}, flags({}), fd({})", path, flags, fd); + Err(-err) + } else { + Ok(fd) + } + } + fn close_restricted(&mut self, fd: RawFd) { + unsafe { libc::close(fd); } + } +} + +fn main() { + let udev_ctx = udev::Context::new().unwrap(); + let libinput_interface = SimpleLibinputInterface{}; + let mut libinput_ctx = Libinput::new_from_udev(libinput_interface, &udev_ctx); + + libinput_ctx.udev_assign_seat("seat0").unwrap(); + + let mut csv_writer = csv::Writer::from_path("acquired_data.csv").unwrap(); + csv_writer.write_record(&["x", "y", "pressure", "time_usec"]).expect("write error"); + loop { + while let Some(event) = libinput_ctx.next() { + if let Event::Tablet(tablet_evt) = event { + if let TabletToolEvent::Axis(axis_evt) = tablet_evt { + if axis_evt.pressure() > 1e-3 { + let pressure = axis_evt.pressure(); + let x_t = axis_evt.x_transformed(1080); + let y_t = axis_evt.y_transformed(1920); + let time_usec = axis_evt.time_usec(); + std::println!( + "(x, y) = ({}, {})\npressure = {}\n(tilt_x, tilt_y) = ({}, {})\n", + x_t, + y_t, + pressure, + axis_evt.tilt_x(), + axis_evt.tilt_y()); + csv_writer.write_record(&[x_t.to_string(), y_t.to_string(), pressure.to_string(), time_usec.to_string()]).expect("write error"); + csv_writer.flush().expect("write error"); + } + } + } + } + libinput_ctx.dispatch().unwrap(); + } +}