2021-01-02 01:26:44 +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-02 01:26:44 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2021-01-02 01:26:44 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2021-01-02 01:26:44 +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-02 01:26:44 +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-02 01:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from evdev import ecodes
|
2021-09-26 10:44:56 +00:00
|
|
|
from evdev.ecodes import (
|
|
|
|
EV_KEY,
|
|
|
|
EV_ABS,
|
|
|
|
ABS_HAT0X,
|
|
|
|
KEY_A,
|
|
|
|
EV_REL,
|
|
|
|
REL_X,
|
|
|
|
REL_WHEEL,
|
|
|
|
REL_HWHEEL,
|
|
|
|
)
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.config import config, BUTTONS
|
|
|
|
from inputremapper.mapping import Mapping
|
|
|
|
from inputremapper import utils
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2021-04-02 10:16:34 +00:00
|
|
|
from tests.test import new_event, InputDevice, MAX_ABS, MIN_ABS
|
2021-01-02 01:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDevUtils(unittest.TestCase):
|
|
|
|
def test_max_abs(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(
|
|
|
|
utils.get_abs_range(InputDevice("/dev/input/event30"))[1], MAX_ABS
|
|
|
|
)
|
|
|
|
self.assertIsNone(utils.get_abs_range(InputDevice("/dev/input/event10")))
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2021-01-05 18:33:47 +00:00
|
|
|
def test_will_report_key_up(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertFalse(utils.will_report_key_up(new_event(EV_REL, REL_WHEEL, 1)))
|
|
|
|
self.assertFalse(utils.will_report_key_up(new_event(EV_REL, REL_HWHEEL, -1)))
|
2021-01-05 18:33:47 +00:00
|
|
|
self.assertTrue(utils.will_report_key_up(new_event(EV_KEY, KEY_A, 1)))
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertTrue(utils.will_report_key_up(new_event(EV_ABS, ABS_HAT0X, -1)))
|
2021-01-05 18:33:47 +00:00
|
|
|
|
|
|
|
def test_is_wheel(self):
|
|
|
|
self.assertTrue(utils.is_wheel(new_event(EV_REL, REL_WHEEL, 1)))
|
|
|
|
self.assertTrue(utils.is_wheel(new_event(EV_REL, REL_HWHEEL, -1)))
|
|
|
|
self.assertFalse(utils.is_wheel(new_event(EV_KEY, KEY_A, 1)))
|
|
|
|
self.assertFalse(utils.is_wheel(new_event(EV_ABS, ABS_HAT0X, -1)))
|
|
|
|
|
2021-02-18 19:38:14 +00:00
|
|
|
def test_should_map_as_btn(self):
|
2021-01-02 01:26:44 +00:00
|
|
|
mapping = Mapping()
|
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
def do(gamepad, event):
|
2021-02-18 19:38:14 +00:00
|
|
|
return utils.should_map_as_btn(event, mapping, gamepad)
|
2021-01-02 01:26:44 +00:00
|
|
|
|
|
|
|
"""D-Pad"""
|
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
self.assertTrue(do(1, new_event(EV_ABS, ABS_HAT0X, 1)))
|
|
|
|
self.assertTrue(do(0, new_event(EV_ABS, ABS_HAT0X, -1)))
|
2021-01-02 01:26:44 +00:00
|
|
|
|
|
|
|
"""Mouse movements"""
|
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
self.assertTrue(do(1, new_event(EV_REL, REL_WHEEL, 1)))
|
|
|
|
self.assertTrue(do(0, new_event(EV_REL, REL_WHEEL, -1)))
|
|
|
|
self.assertTrue(do(1, new_event(EV_REL, REL_HWHEEL, 1)))
|
|
|
|
self.assertTrue(do(0, new_event(EV_REL, REL_HWHEEL, -1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_REL, REL_X, -1)))
|
2021-01-02 01:26:44 +00:00
|
|
|
|
|
|
|
"""regular keys and buttons"""
|
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
self.assertTrue(do(1, new_event(EV_KEY, KEY_A, 1)))
|
|
|
|
self.assertTrue(do(0, new_event(EV_KEY, KEY_A, 1)))
|
|
|
|
self.assertTrue(do(1, new_event(EV_ABS, ABS_HAT0X, -1)))
|
|
|
|
self.assertTrue(do(0, new_event(EV_ABS, ABS_HAT0X, -1)))
|
2021-01-02 01:26:44 +00:00
|
|
|
|
|
|
|
"""mousepad events"""
|
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_MT_SLOT, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_MT_SLOT, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_MT_TOOL_Y, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_MT_TOOL_Y, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_MT_POSITION_X, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_MT_POSITION_X, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_KEY, ecodes.BTN_TOUCH, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_KEY, ecodes.BTN_TOUCH, 1)))
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2021-02-13 13:11:49 +00:00
|
|
|
"""stylus movements"""
|
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
self.assertFalse(do(0, new_event(EV_KEY, ecodes.BTN_DIGI, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_KEY, ecodes.BTN_DIGI, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_TILT_X, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_TILT_X, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_TILT_Y, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_TILT_Y, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_DISTANCE, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_DISTANCE, 1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_PRESSURE, 1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_PRESSURE, 1)))
|
2021-02-13 13:11:49 +00:00
|
|
|
|
2021-01-02 01:26:44 +00:00
|
|
|
"""joysticks"""
|
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
# without a purpose of BUTTONS it won't map any button, even for
|
|
|
|
# gamepads
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_RX, 1234)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_RX, 1234)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_Y, -1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_Y, -1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_RY, -1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_RY, -1)))
|
2021-02-13 13:11:49 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
mapping.set("gamepad.joystick.right_purpose", BUTTONS)
|
|
|
|
config.set("gamepad.joystick.left_purpose", BUTTONS)
|
2021-02-16 22:40:43 +00:00
|
|
|
# but only for gamepads
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_Y, -1)))
|
|
|
|
self.assertTrue(do(1, new_event(EV_ABS, ecodes.ABS_Y, -1)))
|
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_RY, -1)))
|
|
|
|
self.assertTrue(do(1, new_event(EV_ABS, ecodes.ABS_RY, -1)))
|
|
|
|
|
|
|
|
"""weird events"""
|
2021-01-25 23:15:30 +00:00
|
|
|
|
2021-02-16 22:40:43 +00:00
|
|
|
self.assertFalse(do(0, new_event(EV_ABS, ecodes.ABS_MISC, -1)))
|
|
|
|
self.assertFalse(do(1, new_event(EV_ABS, ecodes.ABS_MISC, -1)))
|
2021-01-25 23:15:30 +00:00
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
def test_classify_action(self):
|
2021-04-02 10:16:34 +00:00
|
|
|
""""""
|
|
|
|
|
|
|
|
"""0 to MAX_ABS"""
|
|
|
|
|
2021-01-25 23:15:30 +00:00
|
|
|
def do(event):
|
2021-09-29 18:17:45 +00:00
|
|
|
return utils.classify_action(event, (0, MAX_ABS))
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2021-01-05 18:33:47 +00:00
|
|
|
event = new_event(EV_ABS, ecodes.ABS_RX, MAX_ABS)
|
2021-01-25 23:15:30 +00:00
|
|
|
self.assertEqual(do(event), 1)
|
2021-04-02 10:16:34 +00:00
|
|
|
event = new_event(EV_ABS, ecodes.ABS_Y, MAX_ABS)
|
|
|
|
self.assertEqual(do(event), 1)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_Y, 0)
|
|
|
|
self.assertEqual(do(event), -1)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_X, MAX_ABS // 4)
|
2021-01-25 23:15:30 +00:00
|
|
|
self.assertEqual(do(event), -1)
|
2021-04-02 10:16:34 +00:00
|
|
|
event = new_event(EV_ABS, ecodes.ABS_X, MAX_ABS // 2)
|
2021-01-25 23:15:30 +00:00
|
|
|
self.assertEqual(do(event), 0)
|
2021-04-02 10:16:34 +00:00
|
|
|
|
|
|
|
"""MIN_ABS to MAX_ABS"""
|
|
|
|
|
|
|
|
def do2(event):
|
2021-09-29 18:17:45 +00:00
|
|
|
return utils.classify_action(event, (MIN_ABS, MAX_ABS))
|
2021-04-02 10:16:34 +00:00
|
|
|
|
2021-01-05 18:33:47 +00:00
|
|
|
event = new_event(EV_ABS, ecodes.ABS_RX, MAX_ABS)
|
2021-04-02 10:16:34 +00:00
|
|
|
self.assertEqual(do2(event), 1)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_Y, MIN_ABS)
|
|
|
|
self.assertEqual(do2(event), -1)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_X, MIN_ABS // 4)
|
|
|
|
self.assertEqual(do2(event), 0)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_RX, MAX_ABS)
|
|
|
|
self.assertEqual(do2(event), 1)
|
2021-01-05 18:33:47 +00:00
|
|
|
event = new_event(EV_ABS, ecodes.ABS_Y, MAX_ABS)
|
2021-04-02 10:16:34 +00:00
|
|
|
self.assertEqual(do2(event), 1)
|
2021-01-05 18:33:47 +00:00
|
|
|
event = new_event(EV_ABS, ecodes.ABS_X, MAX_ABS // 4)
|
2021-04-02 10:16:34 +00:00
|
|
|
self.assertEqual(do2(event), 0)
|
|
|
|
|
|
|
|
"""None"""
|
2021-01-25 23:15:30 +00:00
|
|
|
|
2021-04-02 10:16:34 +00:00
|
|
|
# it just forwards the value
|
2021-01-25 23:15:30 +00:00
|
|
|
event = new_event(EV_ABS, ecodes.ABS_RX, MAX_ABS)
|
2021-09-29 18:17:45 +00:00
|
|
|
self.assertEqual(utils.classify_action(event, None), MAX_ABS)
|
2021-04-02 10:16:34 +00:00
|
|
|
|
|
|
|
"""Not a joystick"""
|
|
|
|
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_Z, 1234)
|
|
|
|
self.assertEqual(do(event), 1)
|
|
|
|
self.assertEqual(do2(event), 1)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_Z, 0)
|
|
|
|
self.assertEqual(do(event), 0)
|
|
|
|
self.assertEqual(do2(event), 0)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_Z, -1234)
|
|
|
|
self.assertEqual(do(event), -1)
|
|
|
|
self.assertEqual(do2(event), -1)
|
|
|
|
|
|
|
|
event = new_event(EV_KEY, ecodes.KEY_A, 1)
|
|
|
|
self.assertEqual(do(event), 1)
|
|
|
|
self.assertEqual(do2(event), 1)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_HAT0X, 0)
|
|
|
|
self.assertEqual(do(event), 0)
|
|
|
|
self.assertEqual(do2(event), 0)
|
|
|
|
event = new_event(EV_ABS, ecodes.ABS_HAT0X, -1)
|
|
|
|
self.assertEqual(do(event), -1)
|
|
|
|
self.assertEqual(do2(event), -1)
|