more precise mouse gamepad movement

This commit is contained in:
sezanzeb 2020-11-30 19:54:33 +01:00 committed by sezanzeb
parent 59bb7d5514
commit 661f56d043

View File

@ -293,55 +293,53 @@ class KeycodeInjector:
keymapper_device.device.path, keymapper_device.fd
)
newest_abs_event = None
abs_x = None
abs_y = None
self.abs_x = 0
self.abs_y = 0
# events only take ints, so a movement of 0.3 needs to add up to
# 1.2 to affect the cursor.
pending_x_rel = 0
pending_y_rel = 0
self.pending_x_rel = 0
self.pending_y_rel = 0
async def spam_mouse_movements():
# TODO get absinfo beforehand
max_value = 32767
max_speed = ((max_value ** 2) * 2) ** 0.5
while True:
# TODO get absinfo beforehand
max_value = 32767
if abs_y is not None and abs_x is not None:
speed = (abs_x ** 2 + abs_y ** 2) ** 0.5
max_speed = ((max_value ** 2) * 2) ** 0.5
non_linearity = 2
factor = (speed / max_speed) ** non_linearity
rel_x = abs_x / max_value * factor * 40
rel_y = abs_y / max_value * factor * 40
pending_x_rel = rel_x
pending_y_rel = rel_y
rel_x = int(pending_x_rel)
rel_y = int(pending_y_rel)
pending_x_rel -= rel_x
pending_y_rel -= rel_y
print('factor', factor)
print('speed', speed)
print(rel_x, rel_y)
self._write(
keymapper_device,
evdev.ecodes.EV_REL,
evdev.ecodes.ABS_Y,
rel_y
)
self._write(
keymapper_device,
evdev.ecodes.EV_REL,
evdev.ecodes.ABS_X,
rel_x
)
await asyncio.sleep(1 / 60)
abs_y = self.abs_y
abs_x = self.abs_x
# to make small movements smaller for more precision
speed = (abs_x ** 2 + abs_y ** 2) ** 0.5
non_linearity = 4
factor = (speed / max_speed) ** non_linearity
rel_x = abs_x * factor * 80 / max_value
rel_y = abs_y * factor * 80 / max_value
self.pending_x_rel += rel_x
self.pending_y_rel += rel_y
rel_x = int(self.pending_x_rel)
rel_y = int(self.pending_y_rel)
self.pending_x_rel -= rel_x
self.pending_y_rel -= rel_y
self._write(
keymapper_device,
evdev.ecodes.EV_REL,
evdev.ecodes.ABS_Y,
rel_y
)
self._write(
keymapper_device,
evdev.ecodes.EV_REL,
evdev.ecodes.ABS_X,
rel_x
)
asyncio.ensure_future(spam_mouse_movements())
async for event in device.async_read_loop():
@ -350,10 +348,10 @@ class KeycodeInjector:
continue
if event.code == evdev.ecodes.ABS_X:
abs_x = event.value
self.abs_x = event.value
if event.code == evdev.ecodes.ABS_Y:
abs_y = event.value
self.abs_y = event.value
continue