forked from obv-mikhail/InputBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
320 lines (288 loc) · 9.98 KB
/
mod.rs
File metadata and controls
320 lines (288 loc) · 9.98 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use crate::{common::*, linux::inputs::*, public::*};
use input::{
event::{
keyboard::{
KeyState, {KeyboardEvent, KeyboardEventTrait},
},
pointer::{ButtonState, PointerEvent::*},
Event::{self, *},
},
Libinput, LibinputInterface,
};
use nix::{
fcntl::{open, OFlag},
sys::stat::Mode,
unistd::close,
};
use once_cell::sync::Lazy;
use std::{
mem::MaybeUninit,
os::fd::{FromRawFd, IntoRawFd, OwnedFd},
path::Path,
ptr::null,
sync::atomic::{AtomicBool, Ordering},
sync::Mutex,
thread::sleep,
time::Duration,
};
use uinput::event::{
controller::{Controller, Mouse},
relative::Position,
Event as UinputEvent,
};
use x11::xlib::*;
mod inputs;
type ButtonStatesMap = HashMap<MouseButton, bool>;
type KeyStatesMap = HashMap<KeybdKey, bool>;
static BUTTON_STATES: Lazy<Mutex<ButtonStatesMap>> =
Lazy::new(|| Mutex::new(ButtonStatesMap::new()));
static KEY_STATES: Lazy<Mutex<KeyStatesMap>> = Lazy::new(|| Mutex::new(KeyStatesMap::new()));
static FAKE_DEVICE: Lazy<Mutex<uinput::Device>> = Lazy::new(|| {
Mutex::new(
uinput::default()
.unwrap()
.name("inputbot")
.unwrap()
.event(uinput::event::Keyboard::All)
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Left)))
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Right)))
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Middle)))
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Side)))
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Extra)))
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Forward)))
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Back)))
.unwrap()
.event(UinputEvent::Controller(Controller::Mouse(Mouse::Task)))
.unwrap()
.event(Position::X)
.unwrap()
.event(Position::Y)
.unwrap()
.create()
.unwrap(),
)
});
static SEND_DISPLAY: Lazy<AtomicPtr<Display>> = Lazy::new(|| {
unsafe { XInitThreads() };
AtomicPtr::new(unsafe { XOpenDisplay(null()) })
});
/// Requests the fake device to be generated.
///
/// Can be called before using the fake device to prevent it from
/// building when you first try to use it.
pub fn init_device() {
drop(FAKE_DEVICE.lock().unwrap());
}
impl KeybdKey {
/// Returns true if a given `KeybdKey` is currently pressed (in the down position).
pub fn is_pressed(self) -> bool {
*KEY_STATES.lock().unwrap().entry(self).or_insert(false)
}
/// Presses a given `KeybdKey`. Note: this means the key will remain in the down
/// position. You must manually call release to create a full 'press'.
pub fn press(self) {
let mut device = FAKE_DEVICE.lock().unwrap();
device.write(0x01, key_to_scan_code(self), 1).unwrap();
device.synchronize().unwrap();
}
/// Releases a given `KeybdKey`. This means the key would be in the up position.
pub fn release(self) {
let mut device = FAKE_DEVICE.lock().unwrap();
device.write(0x01, key_to_scan_code(self), 0).unwrap();
device.synchronize().unwrap();
}
/// Returns true if a keyboard key which supports toggling (ScrollLock, NumLock,
/// CapsLock) is on.
pub fn is_toggled(self) -> bool {
if let Some(key) = match self {
KeybdKey::ScrollLockKey => Some(4),
KeybdKey::NumLockKey => Some(2),
KeybdKey::CapsLockKey => Some(1),
_ => None,
} {
let mut state: XKeyboardState = unsafe { MaybeUninit::zeroed().assume_init() };
SEND_DISPLAY.with(|display| unsafe {
XGetKeyboardControl(display, &mut state);
});
state.led_mask & key != 0
} else {
false
}
}
}
impl MouseButton {
/// Returns true if a given `MouseButton` is currently pressed (in the down position).
pub fn is_pressed(self) -> bool {
*BUTTON_STATES.lock().unwrap().entry(self).or_insert(false)
}
/// Presses a given `MouseButton`. Note: this means the button will remain in the down
/// position. You must manually call release to create a full 'click'.
pub fn press(self) {
let mut device = FAKE_DEVICE.lock().unwrap();
device.press(&Controller::Mouse(Mouse::from(self))).unwrap();
device.synchronize().unwrap();
}
/// Releases a given `MouseButton`. This means the button would be in the up position.
pub fn release(self) {
let mut device = FAKE_DEVICE.lock().unwrap();
device
.release(&Controller::Mouse(Mouse::from(self)))
.unwrap();
device.synchronize().unwrap();
}
}
impl MouseCursor {
/// Moves the mouse relative to its current position by a given amount of pixels.
pub fn move_rel(x: i32, y: i32) {
let mut device = FAKE_DEVICE.lock().unwrap();
device.position(&Position::X, x).unwrap();
device.position(&Position::Y, y).unwrap();
SEND_DISPLAY.with(|display| unsafe {
XWarpPointer(display, 0, 0, 0, 0, 0, 0, x, y);
});
device.synchronize().unwrap();
}
/// Moves the mouse to a given position based on absolute coordinates. The top left
/// corner of the screen is (0, 0).
pub fn move_abs(x: i32, y: i32) {
let mut device = FAKE_DEVICE.lock().unwrap();
SEND_DISPLAY.with(|display| unsafe {
XWarpPointer(
display,
0,
XRootWindow(display, XDefaultScreen(display)),
0,
0,
0,
0,
x,
y,
);
});
device.synchronize().unwrap();
}
}
impl MouseWheel {
/// Scrolls the mouse wheel vertically by a given amount.
pub fn scroll_ver(y: i32) {
if y < 0 {
MouseButton::OtherButton(4).press();
MouseButton::OtherButton(4).release();
} else {
MouseButton::OtherButton(5).press();
MouseButton::OtherButton(5).release();
}
}
/// Scrolls the mouse wheel horizontally by a given amount.
pub fn scroll_hor(x: i32) {
if x < 0 {
MouseButton::OtherButton(6).press();
MouseButton::OtherButton(6).release();
} else {
MouseButton::OtherButton(7).press();
MouseButton::OtherButton(7).release();
}
}
}
struct LibinputInterfaceRaw;
impl LibinputInterfaceRaw {
fn seat(&self) -> String {
String::from("seat0")
}
}
impl LibinputInterface for LibinputInterfaceRaw {
fn open_restricted(&mut self, path: &Path, flags: i32) -> std::result::Result<OwnedFd, i32> {
if let Ok(fd) = open(path, OFlag::from_bits_truncate(flags), Mode::empty()) {
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
} else {
Err(1)
}
}
fn close_restricted(&mut self, fd: OwnedFd) {
let _ = close(fd.into_raw_fd());
}
}
/// Starts listening for bound input events (otionally stopping when binds are removed).
pub fn handle_input_events(auto_stop: bool) {
let mut libinput_context = Libinput::new_with_udev(LibinputInterfaceRaw);
libinput_context
.udev_assign_seat(&LibinputInterfaceRaw.seat())
.unwrap();
HANDLE_EVENTS.store(true, Ordering::Relaxed);
while should_continue(auto_stop) {
libinput_context.dispatch().unwrap();
for event in libinput_context.by_ref() {
handle_input_event(event);
}
sleep(Duration::from_millis(10));
}
}
fn handle_input_event(event: Event) {
match event {
Keyboard(KeyboardEvent::Key(keyboard_key_event)) => {
let key = keyboard_key_event.key();
if let Some(keybd_key) = scan_code_to_key(key) {
if keyboard_key_event.key_state() == KeyState::Pressed {
KEY_STATES.lock().unwrap().insert(keybd_key, true);
if let Some(Bind::Normal(cb)) = KEYBD_BINDS.lock().unwrap().get(&keybd_key) {
let cb = Arc::clone(cb);
spawn(move || cb());
}
} else {
KEY_STATES.lock().unwrap().insert(keybd_key, false);
}
}
}
Pointer(Button(button_event)) => {
let button = button_event.button();
if let Some(mouse_button) = match button {
272 => Some(MouseButton::LeftButton),
273 => Some(MouseButton::RightButton),
274 => Some(MouseButton::MiddleButton),
275 => Some(MouseButton::X1Button),
276 => Some(MouseButton::X2Button),
_ => None,
} {
if button_event.button_state() == ButtonState::Pressed {
BUTTON_STATES.lock().unwrap().insert(mouse_button, true);
if let Some(Bind::Normal(cb)) = MOUSE_BINDS.lock().unwrap().get(&mouse_button) {
let cb = Arc::clone(cb);
spawn(move || cb());
};
} else {
BUTTON_STATES.lock().unwrap().insert(mouse_button, false);
}
}
}
_ => {}
}
}
trait DisplayAcquirable {
fn with<F, Z>(&self, cb: F) -> Z
where
F: FnOnce(*mut Display) -> Z;
}
impl DisplayAcquirable for AtomicPtr<Display> {
fn with<F, Z>(&self, cb: F) -> Z
where
F: FnOnce(*mut Display) -> Z,
{
let display = self.load(Ordering::Relaxed);
unsafe {
XLockDisplay(display);
};
let cb_result = cb(display);
unsafe {
XFlush(display);
XUnlockDisplay(display);
};
cb_result
}
}