dividing injector.py into multiple files
parent
ac73d5cc86
commit
c93da68956
@ -0,0 +1,100 @@
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# key-mapper - GUI for device specific keyboard mappings
|
||||
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
|
||||
#
|
||||
# This file is part of key-mapper.
|
||||
#
|
||||
# key-mapper is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# key-mapper is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
"""Keeps mapping joystick to mouse movements."""
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
import evdev
|
||||
from evdev.ecodes import EV_ABS, EV_REL
|
||||
|
||||
from keymapper.logger import logger
|
||||
from keymapper.config import config
|
||||
|
||||
|
||||
def _write(device, ev_type, keycode, value):
|
||||
"""Inject."""
|
||||
device.write(ev_type, keycode, value)
|
||||
device.syn()
|
||||
|
||||
|
||||
async def ev_abs_mapper(abs_state, input_device, keymapper_device):
|
||||
"""Keep writing mouse movements based on the gamepad stick position.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
abs_state : [int, int]
|
||||
array to read the current abs values from. Like a pointer.
|
||||
input_device : evdev.InputDevice
|
||||
keymapper_device : evdev.UInput
|
||||
"""
|
||||
# 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
|
||||
|
||||
logger.info('Mapping gamepad to mouse movements')
|
||||
max_value = input_device.absinfo(EV_ABS).max
|
||||
max_speed = ((max_value ** 2) * 2) ** 0.5
|
||||
|
||||
pointer_speed = config.get('gamepad.pointer_speed', 80)
|
||||
non_linearity = config.get('gamepad.non_linearity', 4)
|
||||
|
||||
while True:
|
||||
# this is part of the spawned process, so terminating that one
|
||||
# will also stop this loop
|
||||
await asyncio.sleep(1 / 60)
|
||||
|
||||
abs_x, abs_y = abs_state
|
||||
|
||||
if non_linearity != 1:
|
||||
# to make small movements smaller for more precision
|
||||
speed = (abs_x ** 2 + abs_y ** 2) ** 0.5
|
||||
factor = (speed / max_speed) ** non_linearity
|
||||
else:
|
||||
factor = 1
|
||||
|
||||
rel_x = abs_x * factor * pointer_speed / max_value
|
||||
rel_y = abs_y * factor * pointer_speed / max_value
|
||||
|
||||
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
|
||||
|
||||
if rel_y != 0:
|
||||
_write(
|
||||
keymapper_device,
|
||||
EV_REL,
|
||||
evdev.ecodes.ABS_Y,
|
||||
rel_y
|
||||
)
|
||||
|
||||
if rel_x != 0:
|
||||
_write(
|
||||
keymapper_device,
|
||||
EV_REL,
|
||||
evdev.ecodes.ABS_X,
|
||||
rel_x
|
||||
)
|
@ -0,0 +1,73 @@
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# key-mapper - GUI for device specific keyboard mappings
|
||||
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
|
||||
#
|
||||
# This file is part of key-mapper.
|
||||
#
|
||||
# key-mapper is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# key-mapper is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
"""Inject a keycode based on the mapping."""
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
from keymapper.logger import logger
|
||||
from keymapper.state import system_mapping, KEYCODE_OFFSET
|
||||
|
||||
|
||||
def handle_keycode(mapping, macros, event, uinput):
|
||||
"""Write the mapped keycode."""
|
||||
input_keycode = event.code + KEYCODE_OFFSET
|
||||
character = mapping.get_character(input_keycode)
|
||||
|
||||
if character is None:
|
||||
# unknown keycode, forward it
|
||||
target_keycode = input_keycode
|
||||
elif macros.get(input_keycode) is not None:
|
||||
if event.value == 0:
|
||||
return
|
||||
logger.spam(
|
||||
'got code:%s value:%s, maps to macro %s',
|
||||
event.code + KEYCODE_OFFSET,
|
||||
event.value,
|
||||
character
|
||||
)
|
||||
macro = macros.get(input_keycode)
|
||||
if macro is not None:
|
||||
asyncio.ensure_future(macro.run())
|
||||
return
|
||||
else:
|
||||
# TODO compile int-int mapping instead of going this route.
|
||||
# I think that makes the reverse mapping obsolete.
|
||||
# It already is actually.
|
||||
target_keycode = system_mapping.get(character)
|
||||
if target_keycode is None:
|
||||
logger.error(
|
||||
'Don\'t know what %s maps to',
|
||||
character
|
||||
)
|
||||
return
|
||||
|
||||
logger.spam(
|
||||
'got code:%s value:%s, maps to code:%s char:%s',
|
||||
event.code + KEYCODE_OFFSET,
|
||||
event.value,
|
||||
target_keycode,
|
||||
character
|
||||
)
|
||||
|
||||
uinput.write(event.type, target_keycode - KEYCODE_OFFSET, event.value)
|
||||
uinput.syn()
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Loading…
Reference in New Issue