magic numbers

This commit is contained in:
sezanzeb 2020-11-22 18:02:55 +01:00 committed by sezanzeb
parent 3bce3f50f6
commit 1a018dbf90
2 changed files with 16 additions and 11 deletions

View File

@ -43,6 +43,9 @@ DEVICE_CREATED = 1
FAILED = 2 FAILED = 2
DEVICE_SKIPPED = 3 DEVICE_SKIPPED = 3
# offset between xkb and linux keycodes. linux keycodes are lower
KEYCODE_OFFSET = 8
def _grab(path): def _grab(path):
"""Try to grab, repeat a few times with time inbetween on failure.""" """Try to grab, repeat a few times with time inbetween on failure."""
@ -99,8 +102,8 @@ def _is_device_mapped(device, mapping):
""" """
capabilities = device.capabilities(absinfo=False)[evdev.ecodes.EV_KEY] capabilities = device.capabilities(absinfo=False)[evdev.ecodes.EV_KEY]
needed = False needed = False
for keycode, _ in custom_mapping: for keycode, _ in mapping:
if keycode in capabilities: if keycode - KEYCODE_OFFSET in capabilities:
needed = True needed = True
if not needed: if not needed:
logger.debug('No need to grab %s', device.path) logger.debug('No need to grab %s', device.path)
@ -128,6 +131,8 @@ def _start_injecting_worker(path, pipe, mapping):
return return
if not _is_device_mapped(device, mapping): if not _is_device_mapped(device, mapping):
# skipping reading and checking on events from those devices
# may be beneficial for performance.
pipe.send(DEVICE_SKIPPED) pipe.send(DEVICE_SKIPPED)
return return
@ -158,9 +163,7 @@ def _start_injecting_worker(path, pipe, mapping):
# linux does them itself, no need to trigger them # linux does them itself, no need to trigger them
continue continue
# this happens to report key codes that are 8 lower input_keycode = event.code + KEYCODE_OFFSET
# than the ones reported by xev and that X expects
input_keycode = event.code + 8
character = mapping.get_character(input_keycode) character = mapping.get_character(input_keycode)
@ -178,12 +181,12 @@ def _start_injecting_worker(path, pipe, mapping):
logger.spam( logger.spam(
'got code:%s value:%s, maps to code:%s char:%s', 'got code:%s value:%s, maps to code:%s char:%s',
event.code + 8, event.value, target_keycode, character event.code + KEYCODE_OFFSET, event.value, target_keycode, character
) )
keymapper_device.write( keymapper_device.write(
evdev.ecodes.EV_KEY, evdev.ecodes.EV_KEY,
target_keycode - 8, target_keycode - KEYCODE_OFFSET,
event.value event.value
) )
keymapper_device.syn() keymapper_device.syn()

View File

@ -28,6 +28,10 @@ from keymapper.logger import logger
from keymapper.getdevices import get_devices, refresh_devices from keymapper.getdevices import get_devices, refresh_devices
# offset between xkb and linux keycodes
KEYCODE_OFFSET = 8
class _KeycodeReader: class _KeycodeReader:
"""Keeps reading keycodes in the background for the UI to use. """Keeps reading keycodes in the background for the UI to use.
@ -86,12 +90,10 @@ class _KeycodeReader:
if event.type == evdev.ecodes.EV_KEY and event.value == 1: if event.type == evdev.ecodes.EV_KEY and event.value == 1:
logger.spam( logger.spam(
'got code:%s value:%s', 'got code:%s value:%s',
event.code + 8, event.value event.code + KEYCODE_OFFSET, event.value
) )
# value: 1 for down, 0 for up, 2 for hold. # value: 1 for down, 0 for up, 2 for hold.
# this happens to report key codes that are 8 lower newest_keycode = event.code + KEYCODE_OFFSET
# than the ones reported by evtest and used in xkb files
newest_keycode = event.code + 8
return newest_keycode return newest_keycode