wip more gamepad stuff

pull/14/head
sezanzeb 4 years ago
parent ada7858f37
commit 1af1ee7bf7

@ -267,12 +267,13 @@ class KeycodeInjector:
value value
) )
async def spam_mouse_movements(self, keymapper_device): async def spam_mouse_movements(self, keymapper_device, input_device):
"""Keep writing mouse movements based on the gamepad stick position.""" """Keep writing mouse movements based on the gamepad stick position."""
# TODO get absinfo beforehand max_value = input_device.absinfo(evdev.ecodes.EV_ABS).max
max_value = 32767
max_speed = ((max_value ** 2) * 2) ** 0.5 max_speed = ((max_value ** 2) * 2) ** 0.5
while True: while True:
# this is part of the spawned process, so terminating that one
# will also stop this loop
await asyncio.sleep(1 / 60) await asyncio.sleep(1 / 60)
abs_y = self.abs_y abs_y = self.abs_y
@ -322,7 +323,6 @@ class KeycodeInjector:
keymapper_device : evdev.UInput keymapper_device : evdev.UInput
where to write keycodes to where to write keycodes to
""" """
# TODO this function is too long
# Parse all macros beforehand # Parse all macros beforehand
logger.debug('Parsing macros') logger.debug('Parsing macros')
macros = {} macros = {}
@ -339,27 +339,26 @@ class KeycodeInjector:
keymapper_device.device.path, keymapper_device.fd keymapper_device.device.path, keymapper_device.fd
) )
self.abs_x = 0 if self.map_abs_to_rel():
self.abs_y = 0 self.abs_x = 0
self.abs_y = 0
# events only take ints, so a movement of 0.3 needs to add up to # events only take ints, so a movement of 0.3 needs to add up to
# 1.2 to affect the cursor. # 1.2 to affect the cursor.
self.pending_x_rel = 0 self.pending_x_rel = 0
self.pending_y_rel = 0 self.pending_y_rel = 0
asyncio.ensure_future(self.spam_mouse_movements(
asyncio.ensure_future(self.spam_mouse_movements(keymapper_device)) keymapper_device,
device
))
async for event in device.async_read_loop(): async for event in device.async_read_loop():
if self.map_abs_to_rel() and event.type == evdev.ecodes.EV_ABS: if self.map_abs_to_rel() and event.type == evdev.ecodes.EV_ABS:
if event.code not in [evdev.ecodes.ABS_X, evdev.ecodes.ABS_Y]: if event.code not in [evdev.ecodes.ABS_X, evdev.ecodes.ABS_Y]:
continue continue
if event.code == evdev.ecodes.ABS_X: if event.code == evdev.ecodes.ABS_X:
self.abs_x = event.value self.abs_x = event.value
if event.code == evdev.ecodes.ABS_Y: if event.code == evdev.ecodes.ABS_Y:
self.abs_y = event.value self.abs_y = event.value
continue continue
if event.type != evdev.ecodes.EV_KEY: if event.type != evdev.ecodes.EV_KEY:
@ -372,7 +371,6 @@ class KeycodeInjector:
continue continue
input_keycode = event.code + KEYCODE_OFFSET input_keycode = event.code + KEYCODE_OFFSET
character = self.mapping.get_character(input_keycode) character = self.mapping.get_character(input_keycode)
if character is None: if character is None:
@ -381,7 +379,6 @@ class KeycodeInjector:
elif macros.get(input_keycode) is not None: elif macros.get(input_keycode) is not None:
if event.value == 0: if event.value == 0:
continue continue
logger.spam( logger.spam(
'got code:%s value:%s, maps to macro %s', 'got code:%s value:%s, maps to macro %s',
event.code + KEYCODE_OFFSET, event.code + KEYCODE_OFFSET,

@ -56,6 +56,8 @@ class Mapping:
self.changed = False self.changed = False
self.config = {}
def __iter__(self): def __iter__(self):
"""Iterate over tuples of unique keycodes and their character.""" """Iterate over tuples of unique keycodes and their character."""
return iter(sorted(self._mapping.items())) return iter(sorted(self._mapping.items()))
@ -142,12 +144,12 @@ class Mapping:
return return
with open(path, 'r') as file: with open(path, 'r') as file:
mapping = json.load(file) preset_dict = json.load(file)
if mapping.get('mapping') is None: if preset_dict.get('mapping') is None:
logger.error('Invalid preset config at "%s"', path) logger.error('Invalid preset config at "%s"', path)
return return
for keycode, character in mapping['mapping'].items(): for keycode, character in preset_dict['mapping'].items():
try: try:
keycode = int(keycode) keycode = int(keycode)
except ValueError: except ValueError:
@ -155,6 +157,12 @@ class Mapping:
continue continue
self._mapping[keycode] = character self._mapping[keycode] = character
for key in preset_dict:
if key == 'mapping':
continue
# TODO test self.config
self.config[key] = preset_dict[key]
self.changed = False self.changed = False
def clone(self): def clone(self):
@ -175,9 +183,10 @@ class Mapping:
with open(path, 'w') as file: with open(path, 'w') as file:
# make sure to keep the option to add metadata if ever needed, # make sure to keep the option to add metadata if ever needed,
# so put the mapping into a special key # so put the mapping into a special key
json.dump({ preset_dict = {'mapping': self._mapping}
'mapping': self._mapping # TODO test self.config
}, file, indent=4) preset_dict.update(self.config)
json.dump(preset_dict, file, indent=4)
file.write('\n') file.write('\n')
self.changed = False self.changed = False

Loading…
Cancel
Save