input-remapper/keymapper/utils.py

208 lines
5.9 KiB
Python
Raw Normal View History

2021-01-01 21:20:33 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# key-mapper - GUI for device specific keyboard mappings
2021-02-22 18:48:20 +00:00
# Copyright (C) 2021 sezanzeb <proxima@sezanzeb.de>
2021-01-01 21:20:33 +00:00
#
# 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/>.
2021-02-13 19:34:33 +00:00
"""Utility functions."""
2021-01-01 21:20:33 +00:00
import math
2021-01-01 21:20:33 +00:00
import evdev
from evdev.ecodes import EV_KEY, EV_ABS, ABS_X, ABS_Y, ABS_RX, ABS_RY, \
EV_REL, REL_WHEEL, REL_HWHEEL
from keymapper.logger import logger
from keymapper.config import BUTTONS
# other events for ABS include buttons
JOYSTICK = [
evdev.ecodes.ABS_X,
evdev.ecodes.ABS_Y,
evdev.ecodes.ABS_RX,
evdev.ecodes.ABS_RY,
]
2021-02-13 13:11:49 +00:00
# drawing table stylus movements
STYLUS = [
(EV_ABS, evdev.ecodes.ABS_DISTANCE),
(EV_ABS, evdev.ecodes.ABS_TILT_X),
(EV_ABS, evdev.ecodes.ABS_TILT_Y),
2021-02-15 17:18:36 +00:00
(EV_KEY, evdev.ecodes.BTN_DIGI),
(EV_ABS, evdev.ecodes.ABS_PRESSURE)
2021-02-13 13:11:49 +00:00
]
# a third of a quarter circle, so that each quarter is divided in 3 areas:
# up, left and up-left. That makes up/down/left/right larger than the
# overlapping sections though, maybe it should be 8 equal areas though, idk
JOYSTICK_BUTTON_THRESHOLD = math.sin((math.pi / 2) / 3 * 1)
def sign(value):
2021-02-07 14:00:36 +00:00
"""Return -1, 0 or 1 depending on the input value."""
if value > 0:
return 1
if value < 0:
return -1
return 0
2021-04-02 10:16:34 +00:00
def normalize_value(event, abs_range=None):
"""Fit the event value to one of 0, 1 or -1."""
if event.type == EV_ABS and event.code in JOYSTICK:
2021-04-02 10:16:34 +00:00
if abs_range is None:
logger.error(
'Got %s, but max_abs is %s',
2021-04-02 10:16:34 +00:00
(event.type, event.code, event.value), abs_range
)
return event.value
2021-04-02 10:16:34 +00:00
# center is the value of the resting position
center = (abs_range[1] + abs_range[0]) / 2
# normalizer is the maximum possible value after centering
normalizer = (abs_range[1] - abs_range[0]) / 2
threshold = normalizer * JOYSTICK_BUTTON_THRESHOLD
triggered = abs(event.value - center) > threshold
return sign(event.value - center) if triggered else 0
# non-joystick abs events (triggers) usually start at 0 and go up to 255,
# but anything that is > 0 was safe to be treated as pressed so far
return sign(event.value)
2021-01-05 18:33:47 +00:00
def is_wheel(event):
"""Check if this is a wheel event."""
return event.type == EV_REL and event.code in [REL_WHEEL, REL_HWHEEL]
def will_report_key_up(event):
"""Check if the key is expected to report a down event as well."""
return not is_wheel(event)
def should_map_as_btn(event, mapping, gamepad):
"""Does this event describe a button.
If it does, this function will make sure its value is one of [-1, 0, 1],
so that it matches the possible values in a mapping object if needed.
2021-01-01 21:20:33 +00:00
If a new kind of event should be mappable to buttons, this is the place
to add it.
2021-01-01 21:20:33 +00:00
Especially important for gamepad events, some of the buttons
require special rules.
Parameters
----------
event : evdev.InputEvent
mapping : Mapping
gamepad : bool
If the device is treated as gamepad
"""
2021-02-13 13:11:49 +00:00
if (event.type, event.code) in STYLUS:
return False
is_mousepad = event.type == EV_ABS and 47 <= event.code <= 61
if is_mousepad:
return False
if event.type == EV_ABS:
2021-02-13 13:11:49 +00:00
if event.code == evdev.ecodes.ABS_MISC:
# what is that even supposed to be.
# the intuos 5 spams those with every event
return False
if event.code in JOYSTICK:
if not gamepad:
return False
l_purpose = mapping.get('gamepad.joystick.left_purpose')
r_purpose = mapping.get('gamepad.joystick.right_purpose')
if event.code in [ABS_X, ABS_Y] and l_purpose == BUTTONS:
return True
if event.code in [ABS_RX, ABS_RY] and r_purpose == BUTTONS:
return True
else:
# for non-joystick buttons just always offer mapping them to
# buttons
return True
2021-02-13 13:11:49 +00:00
if is_wheel(event):
return True
if event.type == EV_KEY:
2021-02-15 17:18:36 +00:00
# usually all EV_KEY events are allright, except for
if event.code == evdev.ecodes.BTN_TOUCH:
return False
2021-02-13 13:11:49 +00:00
return True
return False
2021-04-02 10:16:34 +00:00
def get_abs_range(device, code=ABS_X):
"""Figure out the max and min value of EV_ABS events of that device.
2021-01-01 21:20:33 +00:00
Like joystick movements or triggers.
"""
# since input_device.absinfo(EV_ABS).max is too new for (some?) ubuntus,
# figure out the max value via the capabilities
capabilities = device.capabilities(absinfo=True)
if EV_ABS not in capabilities:
return None
2021-04-02 10:16:34 +00:00
absinfo = [
2021-01-01 21:20:33 +00:00
entry[1] for entry in
capabilities[EV_ABS]
2021-04-02 10:16:34 +00:00
if (
entry[0] == code
and isinstance(entry, tuple)
and isinstance(entry[1], evdev.AbsInfo)
)
2021-01-01 21:20:33 +00:00
]
2021-04-02 10:16:34 +00:00
if len(absinfo) == 0:
logger.error(
'Failed to get ABS info of "%s" for key %d: %s',
device, code, capabilities
)
2021-01-01 21:20:33 +00:00
return None
2021-04-02 10:16:34 +00:00
absinfo = absinfo[0]
return absinfo.min, absinfo.max
2021-04-02 10:16:34 +00:00
def get_max_abs(device, code=ABS_X):
"""Figure out the max value of EV_ABS events of that device.
Like joystick movements or triggers.
"""
abs_range = get_abs_range(device, code)
return abs_range and abs_range[1]