vts_rs/
lib.rs

1#![allow(unused)]
2
3/// Base structure, that stores information about currently stored queue and the module entrypoint.
4pub struct VirtualTouchScreen {
5    pub filename: String,
6    queue: Vec<String>,
7}
8
9impl VirtualTouchScreen {
10    /// Create new instance with default location at `/dev/virtual_touchscreen`
11    pub fn new() -> VirtualTouchScreen {
12        VirtualTouchScreen {
13            filename: "/dev/virtual_touchscreen".to_string(),
14            queue: Vec::new(),
15        }
16    }
17
18    /// Create new instance with a custom location of the kernel module entry
19    pub fn new_with_path(path: String) -> VirtualTouchScreen {
20        VirtualTouchScreen {
21            filename: path,
22            queue: Vec::new(),
23        }
24    }
25
26    /// Remove all actions from the queue
27    pub fn clear_queue(&mut self) {
28        self.queue.clear();
29    }
30
31    /// Remove last action from the queue
32    pub fn pop_queue(&mut self) {
33        self.queue.pop();
34    }
35
36    /// Execute the queue
37    pub fn submit_queue(&mut self) {
38        let mut full_command = String::new();
39        for s in self.queue.clone() {
40            full_command += &s;
41        }
42        full_command += "S 0\n";
43        use std::fs;
44        use std::io::prelude::*;
45        fs::write(&self.filename, full_command).expect("Unable to write to module entrypoint.");
46        self.clear_queue();
47    }
48
49    /// Set active pointer
50    pub fn set_pointer(&mut self, pointer: u32) {
51        self.queue.push(format!("s {}\na {}\n", pointer, pointer));
52    }
53
54    /// Set pointer position
55    pub fn set_position(&mut self, x: u32, y: u32) {
56        self.set_x(x);
57        self.set_y(y);
58    }
59
60    /// Set X coordinate
61    pub fn set_x(&mut self, x: u32) {
62        self.queue.push(format!("X {}\nx {}\n", x, x));
63    }
64
65    /// Set Y coordinate
66    pub fn set_y(&mut self, y: u32) {
67        self.queue.push(format!("Y {}\ny {}\n", y, y));
68    }
69}