-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.py
65 lines (53 loc) · 2.07 KB
/
handle.py
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
# pyright: reportAttributeAccessIssue=false
from evdev import ecodes, UInput
from time import sleep
import evdev
import select
SENS = ? # In-game sensitivity
def findMouse():
return next((device for device in map(evdev.InputDevice, evdev.list_devices())
if "pointer" in device.name.lower()), None)
def move(ui, start_x, start_y, end_x, end_y, steps=?):
x, y = int((end_x - start_x) * SENS/15), int((end_y - start_y) * SENS/15)
x_i = y_i = 0
for i in range(1, steps + 1):
to_x, to_y = int((x * i) / steps), int((y * i) / steps)
dx, dy = to_x - x_i, to_y - y_i
if dx != 0:
ui.write(ecodes.EV_REL, ecodes.REL_X, dx)
x_i += dx
if dy != 0:
ui.write(ecodes.EV_REL, ecodes.REL_Y, dy)
y_i += dy
ui.syn()
sleep(0.001)
def inputLoop(ui, mouse, getTarget, center):
while True:
r, w, x = select.select([mouse.fd], [], [], 0.01)
if r:
for event in mouse.read():
if event.type == ecodes.EV_KEY and event.code == ecodes.BTN_LEFT:
if event.value == 1: # press
target = getTarget()
if target: move(ui, center, center, target[0], target[1])
ui.write(ecodes.EV_KEY, ecodes.BTN_LEFT, event.value)
else:
ui.write_event(event)
ui.syn()
class MouseHandler:
def __init__(self, center):
self.center = center
self.mouse = findMouse()
if not self.mouse:
raise RuntimeError("Mouse not found")
self.mouse.grab()
self.cap = {
ecodes.EV_KEY: [ecodes.BTN_LEFT, ecodes.BTN_RIGHT, ecodes.BTN_MIDDLE, ecodes.BTN_SIDE, ecodes.BTN_EXTRA],
ecodes.EV_REL: [ecodes.REL_X, ecodes.REL_Y, ecodes.REL_WHEEL],
}
self.ui = UInput(self.cap, name="virtual-mouse")
def start(self, target):
inputLoop(self.ui, self.mouse, target, self.center)
def cleanup(self):
self.mouse.ungrab()
self.ui.close()