2021-01-01 21:20:33 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2021-01-01 21:20:33 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2021-01-01 21:20:33 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2021-01-01 21:20:33 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2021-01-01 21:20:33 +00:00
|
|
|
# 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
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2021-01-01 21:20:33 +00:00
|
|
|
|
|
|
|
|
2021-02-13 19:34:33 +00:00
|
|
|
"""Utility functions."""
|
2021-01-01 21:20:33 +00:00
|
|
|
|
|
|
|
|
2021-01-02 01:26:44 +00:00
|
|
|
import math
|
2021-11-21 20:45:02 +00:00
|
|
|
import sys
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2021-01-01 21:20:33 +00:00
|
|
|
import evdev
|
2021-09-26 10:44:56 +00:00
|
|
|
from evdev.ecodes import (
|
|
|
|
EV_KEY,
|
|
|
|
EV_ABS,
|
|
|
|
ABS_X,
|
|
|
|
ABS_Y,
|
|
|
|
ABS_RX,
|
|
|
|
ABS_RY,
|
|
|
|
EV_REL,
|
|
|
|
REL_WHEEL,
|
|
|
|
REL_HWHEEL,
|
|
|
|
)
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.logger import logger
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.global_config import BUTTONS
|
2021-01-02 01:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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),
|
2021-09-26 10:44:56 +00:00
|
|
|
(EV_ABS, evdev.ecodes.ABS_PRESSURE),
|
2021-02-13 13:11:49 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-01-25 23:15:30 +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
|
2021-01-02 01:26:44 +00:00
|
|
|
JOYSTICK_BUTTON_THRESHOLD = math.sin((math.pi / 2) / 3 * 1)
|
|
|
|
|
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
PRESS = 1
|
|
|
|
# D-Pads and joysticks can have a second press event, which moves the knob to the
|
|
|
|
# opposite side, reporting a negative value
|
|
|
|
PRESS_NEGATIVE = -1
|
|
|
|
RELEASE = 0
|
|
|
|
|
|
|
|
|
2021-01-02 01:26:44 +00:00
|
|
|
def sign(value):
|
2021-02-07 14:00:36 +00:00
|
|
|
"""Return -1, 0 or 1 depending on the input value."""
|
2021-01-02 01:26:44 +00:00
|
|
|
if value > 0:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if value < 0:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
def classify_action(event, abs_range=None):
|
|
|
|
"""Fit the event value to one of PRESS, PRESS_NEGATIVE or RELEASE
|
|
|
|
|
|
|
|
A joystick that is pushed to the very side will probably send a high value, whereas
|
|
|
|
having it close to the middle might send values close to 0 with some noise. A value
|
|
|
|
of 1 is usually noise or from touching the joystick very gently and considered in
|
|
|
|
resting position.
|
|
|
|
"""
|
2021-01-25 23:15:30 +00:00
|
|
|
if event.type == EV_ABS and event.code in JOYSTICK:
|
2021-04-02 10:16:34 +00:00
|
|
|
if abs_range is None:
|
2021-01-25 23:15:30 +00:00
|
|
|
logger.error(
|
2021-09-26 10:44:56 +00:00
|
|
|
"Got %s, but abs_range is %s",
|
|
|
|
(event.type, event.code, event.value),
|
|
|
|
abs_range,
|
2021-01-25 23:15:30 +00:00
|
|
|
)
|
|
|
|
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
|
2021-01-25 23:15:30 +00:00
|
|
|
|
|
|
|
return sign(event.value)
|
|
|
|
|
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
def is_key_down(action):
|
|
|
|
"""Is this action a key press."""
|
|
|
|
return action in [PRESS, PRESS_NEGATIVE]
|
|
|
|
|
|
|
|
|
|
|
|
def is_key_up(action):
|
|
|
|
"""Is this action a key release."""
|
|
|
|
return action == RELEASE
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
def should_map_as_btn(event, preset, gamepad):
|
2021-09-29 18:17:45 +00:00
|
|
|
"""Does this event describe a button that is or can be mapped.
|
2021-01-02 01:26:44 +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
|
|
|
|
2021-01-02 01:26:44 +00:00
|
|
|
Especially important for gamepad events, some of the buttons
|
|
|
|
require special rules.
|
2021-02-16 22:40:43 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
event : evdev.InputEvent
|
2022-01-31 19:58:37 +00:00
|
|
|
preset : Preset
|
2021-02-16 22:40:43 +00:00
|
|
|
gamepad : bool
|
|
|
|
If the device is treated as gamepad
|
2021-01-02 01:26:44 +00:00
|
|
|
"""
|
2021-02-13 13:11:49 +00:00
|
|
|
if (event.type, event.code) in STYLUS:
|
|
|
|
return False
|
2021-01-02 01:26:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-01-02 01:26:44 +00:00
|
|
|
if event.code in JOYSTICK:
|
2021-02-16 22:40:43 +00:00
|
|
|
if not gamepad:
|
|
|
|
return False
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
if event.code in [ABS_X, ABS_Y]:
|
2021-01-02 01:26:44 +00:00
|
|
|
return True
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
if event.code in [ABS_RX, ABS_RY]:
|
2021-01-02 01:26:44 +00:00
|
|
|
return True
|
|
|
|
else:
|
2021-02-16 22:40:43 +00:00
|
|
|
# for non-joystick buttons just always offer mapping them to
|
|
|
|
# buttons
|
2021-01-02 01:26:44 +00:00
|
|
|
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
|
|
|
|
|
2021-01-02 01:26:44 +00:00
|
|
|
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-09-26 10:44:56 +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:
|
2021-11-23 18:59:28 +00:00
|
|
|
logger.warning(
|
2022-04-18 11:52:59 +00:00
|
|
|
'Failed to get ABS info of "%s" for key %d: %s',
|
|
|
|
device,
|
|
|
|
code,
|
|
|
|
capabilities,
|
2021-04-02 10:16:34 +00:00
|
|
|
)
|
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-01-02 01:26:44 +00:00
|
|
|
|
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]
|
2021-11-21 20:45:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_service():
|
2022-01-01 12:00:49 +00:00
|
|
|
return sys.argv[0].endswith("input-remapper-service")
|