-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
110 lines (88 loc) · 3.33 KB
/
main.rs
File metadata and controls
110 lines (88 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! The user of the plugin API.
//!
//! This crate loads binary plugins using the API, and performs some operations with mandatory and
//! optional traits.
use ::std::time::Duration;
use inputflow::{
cglue::{result::from_int_result, *},
error::InputFlowError,
prelude::*,
};
use std::ffi::CString;
use std::io;
use std::mem::MaybeUninit;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn main() -> Result<()> {
let mut lib = String::new();
println!("Enter name of the plugin library [inputflow_native]:");
io::stdin().read_line(&mut lib)?;
if lib.trim().is_empty() {
lib = "inputflow_native".to_string();
}
println!("Enter plugin args:");
let mut args = String::new();
io::stdin().read_line(&mut args)?;
let mut obj = MaybeUninit::uninit();
let res = unsafe {
load_plugin(
CString::new(lib.trim()).unwrap().as_c_str().into(),
CString::new(args.trim()).unwrap().as_c_str().into(),
&mut obj,
)
};
let mut obj = unsafe { from_int_result::<_, InputFlowError>(res, obj) }?;
{
let mut borrowed = obj.borrow_features();
if let Some(features) = FeatureSupport::from_bits(borrowed.capabilities()) {
println!("loaded {} with features: {:?}", borrowed.name(), features);
// borrow a generic trait object of type &mut (impl Loadable + MouseWriter)
if let Some(obj) = as_mut!(borrowed impl MouseWriter) {
println!("Using borrowed mouse:");
obj.send_button_down(MouseButton::Left)?;
obj.send_button_up(MouseButton::Left)?;
}
if let Some(obj) = as_mut!(borrowed impl MouseWriter) {
println!("clearing buttons:");
obj.clear_buttons()?;
}
} else {
println!(
"ERROR: Some features were not valid in bytes: {:#b}",
borrowed.capabilities()
);
}
println!("Borrowed done.");
}
{
let mut owned = obj.into_features();
if let Some(obj) = as_mut!(owned impl MouseWriter) {
println!("Using owned MouseWriter:");
//click once
let mut click_result = obj.click_button(MouseButton::Left);
println!("Click Result: {click_result:?}");
click_result = obj.send_button_down(MouseButton::Left);
println!("Press Result: {click_result:?}");
// wiggle
let scale = 5;
// wigg the mouse out for a few seconds
for i in 0..1000 {
let x = (i % (5 * scale)) - 2 * scale;
let y = (i - 2) % (7 * scale) - 3 * scale;
obj.mouse_move_relative(x, y)?;
std::thread::sleep(Duration::from_millis(4));
}
click_result = obj.send_button_up(MouseButton::Left);
println!("Release Result: {click_result:?}");
}
// Casting can be combined with a multiple of optional traits.
if let Some(mut obj) = cast!(owned impl MouseWriter + KeyboardWriter) {
println!("Clearing keyboard keys");
obj.clear_keys()?;
// You can still use the mandatory traits.
obj.name();
}
println!("Owned done.");
}
println!("Quitting");
Ok(())
}