2020-11-16 14:39:15 +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>
|
2020-11-16 14:39:15 +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-04-23 09:51:21 +00:00
|
|
|
import os
|
2020-11-16 14:39:15 +00:00
|
|
|
import unittest
|
2021-04-23 09:51:21 +00:00
|
|
|
import json
|
2020-11-16 14:39:15 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
import evdev
|
2021-04-23 09:51:21 +00:00
|
|
|
from evdev.ecodes import EV_KEY, KEY_A
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
from keymapper.paths import CONFIG_PATH
|
|
|
|
from keymapper.groups import _FindGroups, groups, classify, \
|
|
|
|
GAMEPAD, MOUSE, UNKNOWN, GRAPHICS_TABLET, TOUCHPAD, \
|
|
|
|
KEYBOARD, _Group
|
2021-02-03 18:42:26 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
from tests.test import quick_cleanup, fixtures
|
2020-11-16 14:39:15 +00:00
|
|
|
|
|
|
|
|
2020-11-22 15:54:40 +00:00
|
|
|
class FakePipe:
|
2021-04-23 09:51:21 +00:00
|
|
|
groups = None
|
2020-11-22 15:54:40 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def send(self, groups):
|
|
|
|
self.groups = groups
|
2020-11-16 15:30:27 +00:00
|
|
|
|
2020-11-16 14:39:15 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
class TestGroups(unittest.TestCase):
|
2021-02-03 18:42:26 +00:00
|
|
|
def tearDown(self):
|
2021-04-23 09:51:21 +00:00
|
|
|
quick_cleanup()
|
|
|
|
|
|
|
|
def test_group(self):
|
|
|
|
group = _Group(
|
|
|
|
paths=['/dev/a', '/dev/b', '/dev/c'],
|
|
|
|
names=['name_bar', 'name_a', 'name_foo'],
|
|
|
|
types=[MOUSE, KEYBOARD, UNKNOWN],
|
|
|
|
key='key'
|
|
|
|
)
|
|
|
|
self.assertEqual(group.name, 'name_a')
|
|
|
|
self.assertEqual(group.key, 'key')
|
|
|
|
self.assertEqual(
|
|
|
|
group.get_preset_path('preset1234'),
|
|
|
|
os.path.join(CONFIG_PATH, 'presets', group.name, 'preset1234.json')
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_find_groups(self):
|
2020-11-16 15:30:27 +00:00
|
|
|
pipe = FakePipe()
|
2021-04-23 09:51:21 +00:00
|
|
|
_FindGroups(pipe).run()
|
|
|
|
self.assertIsInstance(pipe.groups, str)
|
|
|
|
|
|
|
|
groups.loads(pipe.groups)
|
|
|
|
self.maxDiff = None
|
|
|
|
self.assertEqual(groups.dumps(), json.dumps([
|
|
|
|
json.dumps({
|
2020-11-16 14:39:15 +00:00
|
|
|
'paths': [
|
2021-04-23 09:51:21 +00:00
|
|
|
'/dev/input/event1',
|
2020-11-22 15:54:40 +00:00
|
|
|
],
|
2021-04-23 09:51:21 +00:00
|
|
|
'names': [
|
|
|
|
'Foo Device'
|
2020-12-19 23:34:37 +00:00
|
|
|
],
|
2021-04-23 09:51:21 +00:00
|
|
|
'types': [KEYBOARD],
|
|
|
|
'key': 'Foo Device'
|
|
|
|
}),
|
|
|
|
json.dumps({
|
2020-11-22 15:54:40 +00:00
|
|
|
'paths': [
|
|
|
|
'/dev/input/event11',
|
|
|
|
'/dev/input/event10',
|
|
|
|
'/dev/input/event13'
|
|
|
|
],
|
2021-04-23 09:51:21 +00:00
|
|
|
'names': [
|
|
|
|
'Foo Device foo',
|
|
|
|
'Foo Device',
|
|
|
|
'Foo Device'
|
2020-12-19 23:34:37 +00:00
|
|
|
],
|
2021-04-23 09:51:21 +00:00
|
|
|
'types': [KEYBOARD, MOUSE],
|
|
|
|
'key': 'Foo Device 2'
|
|
|
|
}),
|
|
|
|
json.dumps({
|
2020-12-19 23:34:37 +00:00
|
|
|
'paths': ['/dev/input/event20'],
|
2021-04-23 09:51:21 +00:00
|
|
|
'names': ['Bar Device'],
|
|
|
|
'types': [KEYBOARD],
|
|
|
|
'key': 'Bar Device'
|
|
|
|
}),
|
|
|
|
json.dumps({
|
2020-11-30 21:42:53 +00:00
|
|
|
'paths': ['/dev/input/event30'],
|
2021-04-23 09:51:21 +00:00
|
|
|
'names': ['gamepad'],
|
|
|
|
'types': [GAMEPAD],
|
|
|
|
'key': 'gamepad'
|
|
|
|
}),
|
|
|
|
json.dumps({
|
|
|
|
'paths': ['/dev/input/event40'],
|
|
|
|
'names': ['key-mapper Bar Device'],
|
|
|
|
'types': [KEYBOARD],
|
|
|
|
'key': 'key-mapper Bar Device'
|
|
|
|
}),
|
|
|
|
]))
|
|
|
|
|
|
|
|
groups2 = json.dumps([
|
|
|
|
group.dumps() for group in
|
|
|
|
groups.filter(include_keymapper=True)
|
|
|
|
])
|
|
|
|
self.assertEqual(pipe.groups, groups2)
|
|
|
|
|
|
|
|
def test_list_group_names(self):
|
|
|
|
self.assertListEqual(groups.list_group_names(), [
|
|
|
|
'Foo Device',
|
|
|
|
'Foo Device',
|
|
|
|
'Bar Device',
|
|
|
|
'gamepad',
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_filter(self):
|
|
|
|
# by default no key-mapper devices are present
|
|
|
|
filtered = groups.filter()
|
|
|
|
keys = [group.key for group in filtered]
|
|
|
|
self.assertIn('Foo Device 2', keys)
|
|
|
|
self.assertNotIn('key-mapper Bar Device', keys)
|
2020-11-22 15:54:40 +00:00
|
|
|
|
2021-02-03 18:42:26 +00:00
|
|
|
def test_skip_camera(self):
|
|
|
|
fixtures['/foo/bar'] = {
|
|
|
|
'name': 'camera', 'phys': 'abcd1',
|
|
|
|
'info': evdev.DeviceInfo(1, 2, 3, 4),
|
|
|
|
'capabilities': {
|
|
|
|
evdev.ecodes.EV_KEY: [
|
|
|
|
evdev.ecodes.KEY_CAMERA
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
groups.refresh()
|
|
|
|
self.assertIsNone(groups.find(name='camera'))
|
|
|
|
self.assertIsNotNone(groups.find(name='gamepad'))
|
2021-02-03 18:42:26 +00:00
|
|
|
|
2021-02-15 22:11:59 +00:00
|
|
|
def test_device_with_only_ev_abs(self):
|
|
|
|
# could be anything, a lot of devices have ABS_X capabilities,
|
|
|
|
# so it is not treated as gamepad joystick and since it also
|
|
|
|
# doesn't have key capabilities, there is nothing to map.
|
2021-02-03 18:42:26 +00:00
|
|
|
fixtures['/foo/bar'] = {
|
2021-02-15 22:11:59 +00:00
|
|
|
'name': 'qux', 'phys': 'abcd2',
|
2021-02-03 18:42:26 +00:00
|
|
|
'info': evdev.DeviceInfo(1, 2, 3, 4),
|
|
|
|
'capabilities': {
|
|
|
|
evdev.ecodes.EV_ABS: [
|
|
|
|
evdev.ecodes.ABS_X
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
groups.refresh()
|
|
|
|
self.assertIsNotNone(groups.find(name='gamepad'))
|
|
|
|
self.assertIsNone(groups.find(name='qux'))
|
|
|
|
|
|
|
|
# verify this test even works at all
|
|
|
|
fixtures['/foo/bar']['capabilities'][EV_KEY] = [KEY_A]
|
|
|
|
groups.refresh()
|
|
|
|
self.assertIsNotNone(groups.find(name='qux'))
|
|
|
|
|
|
|
|
def test_duplicate_device(self):
|
|
|
|
fixtures['/dev/input/event20']['name'] = 'Foo Device'
|
|
|
|
groups.refresh()
|
|
|
|
|
|
|
|
group1 = groups.find(key='Foo Device')
|
|
|
|
group2 = groups.find(key='Foo Device 2')
|
|
|
|
group3 = groups.find(key='Foo Device 3')
|
|
|
|
|
|
|
|
self.assertIn('/dev/input/event1', group1.paths)
|
|
|
|
self.assertIn('/dev/input/event10', group2.paths)
|
|
|
|
self.assertIn('/dev/input/event20', group3.paths)
|
|
|
|
|
|
|
|
self.assertEqual(group1.key, 'Foo Device')
|
|
|
|
self.assertEqual(group2.key, 'Foo Device 2')
|
|
|
|
self.assertEqual(group3.key, 'Foo Device 3')
|
|
|
|
|
|
|
|
self.assertEqual(group1.name, 'Foo Device')
|
|
|
|
self.assertEqual(group2.name, 'Foo Device')
|
|
|
|
self.assertEqual(group3.name, 'Foo Device')
|
2021-02-03 18:42:26 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
def test_classify(self):
|
2020-12-24 00:26:34 +00:00
|
|
|
# properly detects if the device is a gamepad
|
|
|
|
EV_ABS = evdev.ecodes.EV_ABS
|
|
|
|
EV_KEY = evdev.ecodes.EV_KEY
|
2021-02-15 17:18:36 +00:00
|
|
|
EV_REL = evdev.ecodes.EV_REL
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-01-17 14:09:47 +00:00
|
|
|
class FakeDevice:
|
|
|
|
def __init__(self, capabilities):
|
|
|
|
self.c = capabilities
|
|
|
|
|
|
|
|
def capabilities(self, absinfo):
|
|
|
|
assert not absinfo
|
|
|
|
return self.c
|
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
"""gamepads"""
|
2021-02-15 22:11:59 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
self.assertEqual(classify(FakeDevice({
|
2021-02-15 22:11:59 +00:00
|
|
|
EV_ABS: [evdev.ecodes.ABS_X, evdev.ecodes.ABS_Y],
|
|
|
|
EV_KEY: [evdev.ecodes.BTN_A]
|
2021-03-27 12:21:35 +00:00
|
|
|
})), GAMEPAD)
|
2021-02-15 22:11:59 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
"""mice"""
|
2021-02-15 22:11:59 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
self.assertEqual(classify(FakeDevice({
|
2021-04-02 10:16:34 +00:00
|
|
|
EV_REL: [evdev.ecodes.REL_X, evdev.ecodes.REL_Y, evdev.ecodes.REL_WHEEL],
|
2021-03-27 12:21:35 +00:00
|
|
|
EV_KEY: [evdev.ecodes.BTN_LEFT]
|
|
|
|
})), MOUSE)
|
|
|
|
|
|
|
|
"""keyboard"""
|
|
|
|
|
|
|
|
self.assertEqual(classify(FakeDevice({
|
|
|
|
EV_KEY: [evdev.ecodes.KEY_A]
|
|
|
|
})), KEYBOARD)
|
|
|
|
|
|
|
|
"""touchpads"""
|
|
|
|
|
|
|
|
self.assertEqual(classify(FakeDevice({
|
|
|
|
EV_KEY: [evdev.ecodes.KEY_A],
|
|
|
|
EV_ABS: [evdev.ecodes.ABS_MT_POSITION_X]
|
|
|
|
})), TOUCHPAD)
|
|
|
|
|
2021-04-02 10:16:34 +00:00
|
|
|
"""graphics tablets"""
|
2021-03-27 12:21:35 +00:00
|
|
|
|
|
|
|
self.assertEqual(classify(FakeDevice({
|
2021-02-15 22:11:59 +00:00
|
|
|
EV_ABS: [evdev.ecodes.ABS_X, evdev.ecodes.ABS_Y],
|
2021-04-02 10:16:34 +00:00
|
|
|
EV_KEY: [evdev.ecodes.BTN_STYLUS]
|
|
|
|
})), GRAPHICS_TABLET)
|
|
|
|
|
|
|
|
"""weird combos"""
|
2021-02-15 22:11:59 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
self.assertEqual(classify(FakeDevice({
|
2021-02-15 22:11:59 +00:00
|
|
|
EV_ABS: [evdev.ecodes.ABS_X, evdev.ecodes.ABS_Y],
|
|
|
|
EV_KEY: [evdev.ecodes.KEY_1]
|
2021-03-27 12:21:35 +00:00
|
|
|
})), UNKNOWN)
|
2021-02-15 22:11:59 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
self.assertEqual(classify(FakeDevice({
|
2021-02-15 22:11:59 +00:00
|
|
|
EV_ABS: [evdev.ecodes.ABS_X],
|
|
|
|
EV_KEY: [evdev.ecodes.BTN_A]
|
2021-03-27 12:21:35 +00:00
|
|
|
})), UNKNOWN)
|
2021-02-15 22:11:59 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
self.assertEqual(classify(FakeDevice({
|
2021-02-15 22:11:59 +00:00
|
|
|
EV_KEY: [evdev.ecodes.BTN_A]
|
2021-03-27 12:21:35 +00:00
|
|
|
})), UNKNOWN)
|
2021-02-15 22:11:59 +00:00
|
|
|
|
2021-03-27 12:21:35 +00:00
|
|
|
self.assertEqual(classify(FakeDevice({
|
2021-02-15 22:11:59 +00:00
|
|
|
EV_ABS: [evdev.ecodes.ABS_X]
|
2021-03-27 12:21:35 +00:00
|
|
|
})), UNKNOWN)
|
2020-11-16 14:39:15 +00:00
|
|
|
|
2021-01-01 21:20:33 +00:00
|
|
|
|
2020-11-16 14:39:15 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|