2020-11-18 23:04:04 +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>
|
2020-11-18 23:04:04 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-18 23:04:04 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-18 23:04:04 +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,
|
2020-11-18 23:04:04 +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/>.
|
2022-04-17 10:19:23 +00:00
|
|
|
from pydantic import ValidationError
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
from tests.test import (
|
|
|
|
new_event,
|
|
|
|
push_events,
|
|
|
|
fixtures,
|
|
|
|
EVENT_READ_TIMEOUT,
|
|
|
|
uinput_write_history_pipe,
|
|
|
|
quick_cleanup,
|
|
|
|
read_write_history_pipe,
|
|
|
|
uinputs,
|
|
|
|
keyboard_keys,
|
2022-04-17 10:19:23 +00:00
|
|
|
get_key_mapping,
|
2022-03-03 22:42:37 +00:00
|
|
|
)
|
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
import unittest
|
2021-03-21 18:15:20 +00:00
|
|
|
from unittest import mock
|
2020-11-30 17:59:34 +00:00
|
|
|
import time
|
2020-11-18 23:04:04 +00:00
|
|
|
|
|
|
|
import evdev
|
2021-09-26 10:44:56 +00:00
|
|
|
from evdev.ecodes import (
|
|
|
|
EV_REL,
|
|
|
|
EV_KEY,
|
|
|
|
EV_ABS,
|
|
|
|
ABS_HAT0X,
|
|
|
|
KEY_A,
|
|
|
|
REL_HWHEEL,
|
|
|
|
BTN_A,
|
|
|
|
ABS_X,
|
|
|
|
ABS_VOLUME,
|
|
|
|
)
|
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.injection.injector import (
|
2021-09-26 10:44:56 +00:00
|
|
|
Injector,
|
|
|
|
is_in_capabilities,
|
|
|
|
STARTING,
|
|
|
|
RUNNING,
|
|
|
|
STOPPED,
|
|
|
|
NO_GRAB,
|
|
|
|
UNKNOWN,
|
2021-10-01 22:55:10 +00:00
|
|
|
get_udev_name,
|
2021-09-26 10:44:56 +00:00
|
|
|
)
|
2022-01-31 13:02:03 +00:00
|
|
|
from inputremapper.injection.numlock import is_numlock_on
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.system_mapping import (
|
|
|
|
system_mapping,
|
|
|
|
DISABLE_CODE,
|
|
|
|
DISABLE_NAME,
|
|
|
|
)
|
|
|
|
from inputremapper.gui.active_preset import active_preset
|
|
|
|
from inputremapper.configs.preset import Preset
|
|
|
|
from inputremapper.event_combination import EventCombination
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.injection.macros.parse import parse
|
|
|
|
from inputremapper.injection.context import Context
|
|
|
|
from inputremapper.groups import groups, classify, GAMEPAD
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2021-01-02 01:26:44 +00:00
|
|
|
|
2021-11-20 12:20:40 +00:00
|
|
|
def wait_for_uinput_write():
|
|
|
|
start = time.time()
|
|
|
|
if not uinput_write_history_pipe[0].poll(timeout=10):
|
2021-11-20 12:41:34 +00:00
|
|
|
raise AssertionError("No event written within 10 seconds")
|
2021-11-20 12:20:40 +00:00
|
|
|
return float(time.time() - start)
|
|
|
|
|
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
class TestInjector(unittest.IsolatedAsyncioTestCase):
|
2021-09-26 10:44:56 +00:00
|
|
|
new_gamepad_path = "/dev/input/event100"
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2020-11-19 00:02:27 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.injector = None
|
2020-11-19 10:30:01 +00:00
|
|
|
cls.grab = evdev.InputDevice.grab
|
2021-09-29 18:17:45 +00:00
|
|
|
quick_cleanup()
|
2020-11-19 10:30:01 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.failed = 0
|
2020-12-03 19:37:36 +00:00
|
|
|
self.make_it_fail = 2
|
2020-11-19 10:30:01 +00:00
|
|
|
|
|
|
|
def grab_fail_twice(_):
|
2020-12-03 19:37:36 +00:00
|
|
|
if self.failed < self.make_it_fail:
|
2020-11-19 10:30:01 +00:00
|
|
|
self.failed += 1
|
|
|
|
raise OSError()
|
|
|
|
|
|
|
|
evdev.InputDevice.grab = grab_fail_twice
|
2020-11-18 23:04:04 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if self.injector is not None:
|
|
|
|
self.injector.stop_injecting()
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), STOPPED)
|
2020-11-19 00:02:27 +00:00
|
|
|
self.injector = None
|
2020-11-19 10:30:01 +00:00
|
|
|
evdev.InputDevice.grab = self.grab
|
2020-12-26 15:46:01 +00:00
|
|
|
|
2021-02-12 20:43:40 +00:00
|
|
|
quick_cleanup()
|
2020-12-12 14:22:34 +00:00
|
|
|
|
2020-11-19 10:30:01 +00:00
|
|
|
def test_grab(self):
|
|
|
|
# path is from the fixtures
|
2021-09-26 10:44:56 +00:00
|
|
|
path = "/dev/input/event10"
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
preset.add(get_key_mapping(EventCombination([EV_KEY, 10, 1]), "keyboard", "a"))
|
2021-04-26 21:21:52 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.injector = Injector(groups.find(key="Foo Device 2"), preset)
|
2020-11-28 14:43:24 +00:00
|
|
|
# this test needs to pass around all other constraints of
|
2021-01-17 14:09:47 +00:00
|
|
|
# _grab_device
|
2022-04-17 10:19:23 +00:00
|
|
|
self.injector.context = Context(preset)
|
2021-01-17 14:09:47 +00:00
|
|
|
device = self.injector._grab_device(path)
|
2021-03-27 12:21:35 +00:00
|
|
|
gamepad = classify(device) == GAMEPAD
|
2021-02-13 21:34:12 +00:00
|
|
|
self.assertFalse(gamepad)
|
2020-11-19 10:30:01 +00:00
|
|
|
self.assertEqual(self.failed, 2)
|
|
|
|
# success on the third try
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(device.name, fixtures[path]["name"])
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-12-03 19:37:36 +00:00
|
|
|
def test_fail_grab(self):
|
2021-04-26 21:21:52 +00:00
|
|
|
self.make_it_fail = 999
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
preset.add(get_key_mapping(EventCombination([EV_KEY, 10, 1]), "keyboard", "a"))
|
2020-12-03 19:37:36 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.injector = Injector(groups.find(key="Foo Device 2"), preset)
|
2021-09-26 10:44:56 +00:00
|
|
|
path = "/dev/input/event10"
|
2022-04-17 10:19:23 +00:00
|
|
|
self.injector.context = Context(preset)
|
2021-01-17 14:09:47 +00:00
|
|
|
device = self.injector._grab_device(path)
|
2020-12-03 19:37:36 +00:00
|
|
|
self.assertIsNone(device)
|
2021-01-17 14:09:47 +00:00
|
|
|
self.assertGreaterEqual(self.failed, 1)
|
2020-12-03 19:37:36 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), UNKNOWN)
|
2021-02-13 20:17:08 +00:00
|
|
|
self.injector.start()
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), STARTING)
|
2020-12-03 19:37:36 +00:00
|
|
|
# since none can be grabbed, the process will terminate. But that
|
|
|
|
# actually takes quite some time.
|
2021-04-26 21:21:52 +00:00
|
|
|
time.sleep(self.injector.regrab_timeout * 12)
|
2021-02-13 20:17:08 +00:00
|
|
|
self.assertFalse(self.injector.is_alive())
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), NO_GRAB)
|
2020-12-03 19:37:36 +00:00
|
|
|
|
2021-01-17 14:09:47 +00:00
|
|
|
def test_grab_device_1(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
preset.add(
|
2022-04-18 11:52:59 +00:00
|
|
|
get_key_mapping(EventCombination([EV_ABS, ABS_HAT0X, 1]), "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
self.injector = Injector(groups.find(name="gamepad"), preset)
|
|
|
|
self.injector.context = Context(preset)
|
2020-12-03 19:03:53 +00:00
|
|
|
|
2021-01-17 14:09:47 +00:00
|
|
|
_grab_device = self.injector._grab_device
|
2021-04-23 09:51:21 +00:00
|
|
|
# doesn't have the required capability
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIsNone(_grab_device("/dev/input/event10"))
|
2021-04-23 09:51:21 +00:00
|
|
|
# according to the fixtures, /dev/input/event30 can do ABS_HAT0X
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIsNotNone(_grab_device("/dev/input/event30"))
|
2021-04-23 09:51:21 +00:00
|
|
|
# this doesn't exist
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIsNone(_grab_device("/dev/input/event1234"))
|
2020-12-03 19:37:36 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def test_forward_gamepad_events(self):
|
2021-01-17 14:09:47 +00:00
|
|
|
# forward abs joystick events
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
self.injector = Injector(groups.find(name="gamepad"), preset)
|
|
|
|
self.injector.context = Context(preset)
|
2021-01-17 14:09:47 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
path = "/dev/input/event30"
|
2021-01-17 14:09:47 +00:00
|
|
|
device = self.injector._grab_device(path)
|
|
|
|
self.assertIsNone(device) # no capability is used, so it won't grab
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset.add(
|
2022-04-18 11:52:59 +00:00
|
|
|
get_key_mapping(EventCombination([EV_KEY, BTN_A, 1]), "keyboard", "a"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2021-01-17 14:09:47 +00:00
|
|
|
device = self.injector._grab_device(path)
|
|
|
|
self.assertIsNotNone(device)
|
2021-03-27 12:21:35 +00:00
|
|
|
gamepad = classify(device) == GAMEPAD
|
2021-01-17 14:09:47 +00:00
|
|
|
self.assertTrue(gamepad)
|
|
|
|
|
2020-11-28 14:43:24 +00:00
|
|
|
def test_skip_unused_device(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
# skips a device because its capabilities are not used in the preset
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
preset.add(get_key_mapping(EventCombination([EV_KEY, 10, 1]), "keyboard", "a"))
|
|
|
|
self.injector = Injector(groups.find(key="Foo Device 2"), preset)
|
|
|
|
self.injector.context = Context(preset)
|
2021-09-26 10:44:56 +00:00
|
|
|
path = "/dev/input/event11"
|
2021-01-17 14:09:47 +00:00
|
|
|
device = self.injector._grab_device(path)
|
2020-11-28 14:43:24 +00:00
|
|
|
self.assertIsNone(device)
|
2021-01-17 14:09:47 +00:00
|
|
|
self.assertEqual(self.failed, 0)
|
2020-11-28 14:43:24 +00:00
|
|
|
|
|
|
|
def test_skip_unknown_device(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
preset.add(get_key_mapping(EventCombination([EV_KEY, 10, 1]), "keyboard", "a"))
|
2021-03-27 12:21:35 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
# skips a device because its capabilities are not used in the preset
|
2022-04-17 10:19:23 +00:00
|
|
|
self.injector = Injector(groups.find(key="Foo Device 2"), preset)
|
|
|
|
self.injector.context = Context(preset)
|
2021-09-26 10:44:56 +00:00
|
|
|
path = "/dev/input/event11"
|
2021-01-17 14:09:47 +00:00
|
|
|
device = self.injector._grab_device(path)
|
2020-11-28 14:43:24 +00:00
|
|
|
|
2020-11-30 21:42:53 +00:00
|
|
|
# skips the device alltogether, so no grab attempts fail
|
2020-11-28 14:43:24 +00:00
|
|
|
self.assertEqual(self.failed, 0)
|
|
|
|
self.assertIsNone(device)
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def test_get_udev_name(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
self.injector = Injector(groups.find(key="Foo Device 2"), active_preset)
|
2021-09-26 10:44:56 +00:00
|
|
|
suffix = "mapped"
|
2022-01-01 12:00:49 +00:00
|
|
|
prefix = "input-remapper"
|
2021-03-21 01:22:41 +00:00
|
|
|
expected = f'{prefix} {"a" * (80 - len(suffix) - len(prefix) - 2)} {suffix}'
|
|
|
|
self.assertEqual(len(expected), 80)
|
2021-10-01 22:55:10 +00:00
|
|
|
self.assertEqual(get_udev_name("a" * 100, suffix), expected)
|
2021-03-21 01:22:41 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.injector.device = "abcd"
|
2021-03-21 01:22:41 +00:00
|
|
|
self.assertEqual(
|
2021-10-01 22:55:10 +00:00
|
|
|
get_udev_name("abcd", "forwarded"),
|
2022-01-01 12:00:49 +00:00
|
|
|
"input-remapper abcd forwarded",
|
2021-03-21 01:22:41 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
@mock.patch("evdev.InputDevice.ungrab")
|
2021-04-26 21:21:52 +00:00
|
|
|
def test_capabilities_and_uinput_presence(self, ungrab_patch):
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
m1 = get_key_mapping(EventCombination([EV_KEY, KEY_A, 1]), "keyboard", "c")
|
|
|
|
m2 = get_key_mapping(
|
2022-04-18 11:52:59 +00:00
|
|
|
EventCombination([EV_REL, REL_HWHEEL, 1]),
|
|
|
|
"keyboard",
|
|
|
|
"key(b)",
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
preset.add(m1)
|
|
|
|
preset.add(m2)
|
|
|
|
self.injector = Injector(groups.find(key="Foo Device 2"), preset)
|
2021-02-18 19:38:14 +00:00
|
|
|
self.injector.stop_injecting()
|
|
|
|
self.injector.run()
|
|
|
|
|
2021-02-22 22:09:55 +00:00
|
|
|
self.assertEqual(
|
2022-01-31 19:58:37 +00:00
|
|
|
self.injector.context.preset.get_mapping(
|
|
|
|
EventCombination([EV_KEY, KEY_A, 1])
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
m1,
|
2021-02-22 22:09:55 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-01-31 19:58:37 +00:00
|
|
|
self.injector.context.preset.get_mapping(
|
|
|
|
EventCombination([EV_REL, REL_HWHEEL, 1])
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
m2,
|
2021-02-22 22:09:55 +00:00
|
|
|
)
|
|
|
|
|
2021-02-20 13:16:05 +00:00
|
|
|
self.assertListEqual(
|
|
|
|
sorted(uinputs.keys()),
|
2021-09-26 10:44:56 +00:00
|
|
|
sorted(
|
|
|
|
[
|
|
|
|
# reading and preventing original events from reaching the
|
|
|
|
# display server
|
2022-01-01 12:00:49 +00:00
|
|
|
"input-remapper Foo Device foo forwarded",
|
|
|
|
"input-remapper Foo Device forwarded",
|
2021-09-26 10:44:56 +00:00
|
|
|
]
|
|
|
|
),
|
2021-02-20 13:16:05 +00:00
|
|
|
)
|
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
forwarded_foo = uinputs.get("input-remapper Foo Device foo forwarded")
|
|
|
|
forwarded = uinputs.get("input-remapper Foo Device forwarded")
|
2021-02-18 19:38:14 +00:00
|
|
|
self.assertIsNotNone(forwarded_foo)
|
|
|
|
self.assertIsNotNone(forwarded)
|
|
|
|
|
|
|
|
# copies capabilities for all other forwarded devices
|
|
|
|
self.assertIn(EV_REL, forwarded_foo.capabilities())
|
|
|
|
self.assertIn(EV_KEY, forwarded.capabilities())
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(sorted(forwarded.capabilities()[EV_KEY]), keyboard_keys)
|
2021-01-17 14:09:47 +00:00
|
|
|
|
2021-04-26 21:21:52 +00:00
|
|
|
self.assertEqual(ungrab_patch.call_count, 2)
|
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
def test_injector(self):
|
2020-12-16 11:57:09 +00:00
|
|
|
numlock_before = is_numlock_on()
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# stuff the preset outputs
|
2020-12-04 13:38:41 +00:00
|
|
|
system_mapping.clear()
|
2020-11-28 23:13:13 +00:00
|
|
|
code_a = 100
|
|
|
|
code_q = 101
|
|
|
|
code_w = 102
|
2021-09-26 10:44:56 +00:00
|
|
|
system_mapping._set("a", code_a)
|
|
|
|
system_mapping._set("key_q", code_q)
|
|
|
|
system_mapping._set("w", code_w)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
preset.add(
|
|
|
|
get_key_mapping(
|
|
|
|
EventCombination(((EV_KEY, 8, 1), (EV_KEY, 9, 1))),
|
|
|
|
"keyboard",
|
|
|
|
"k(KEY_Q).k(w)",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
preset.add(
|
2022-04-18 11:52:59 +00:00
|
|
|
get_key_mapping(
|
|
|
|
EventCombination([EV_ABS, ABS_HAT0X, -1]),
|
|
|
|
"keyboard",
|
|
|
|
"a",
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
# one mapping that is unknown in the system_mapping on purpose
|
|
|
|
input_b = 10
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
preset.add(
|
2022-04-18 11:52:59 +00:00
|
|
|
get_key_mapping(
|
|
|
|
EventCombination([EV_KEY, input_b, 1]),
|
|
|
|
"keyboard",
|
|
|
|
"b",
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
2022-04-17 10:19:23 +00:00
|
|
|
"gamepad",
|
2021-09-26 10:44:56 +00:00
|
|
|
[
|
|
|
|
# should execute a macro...
|
2022-04-17 10:19:23 +00:00
|
|
|
new_event(EV_KEY, 8, 1), # forwarded
|
|
|
|
new_event(EV_KEY, 9, 1), # triggers macro
|
|
|
|
new_event(EV_KEY, 8, 0), # releases macro
|
|
|
|
new_event(EV_KEY, 9, 0), # forwarded
|
2021-09-26 10:44:56 +00:00
|
|
|
# gamepad stuff. trigger a combination
|
|
|
|
new_event(EV_ABS, ABS_HAT0X, -1),
|
|
|
|
new_event(EV_ABS, ABS_HAT0X, 0),
|
|
|
|
# just pass those over without modifying
|
|
|
|
new_event(EV_KEY, 10, 1),
|
|
|
|
new_event(EV_KEY, 10, 0),
|
|
|
|
new_event(3124, 3564, 6542),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.injector = Injector(groups.find(name="gamepad"), preset)
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), UNKNOWN)
|
2021-02-13 20:17:08 +00:00
|
|
|
self.injector.start()
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), STARTING)
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
uinput_write_history_pipe[0].poll(timeout=1)
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), RUNNING)
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 10)
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-12-03 19:37:36 +00:00
|
|
|
# sending anything arbitrary does not stop the process
|
|
|
|
# (is_alive checked later after some time)
|
|
|
|
self.injector._msg_pipe[1].send(1234)
|
|
|
|
|
2020-11-28 23:13:13 +00:00
|
|
|
# convert the write history to some easier to manage list
|
2021-01-01 21:20:33 +00:00
|
|
|
history = read_write_history_pipe()
|
2020-11-30 17:59:34 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# 1 event before the combination was triggered
|
|
|
|
# 2 events for releasing the combination trigger (by combination handler)
|
2020-11-30 19:57:09 +00:00
|
|
|
# 4 events for the macro
|
2022-04-17 10:19:23 +00:00
|
|
|
# 1 release of the event that didn't release the macro
|
2020-12-02 16:40:21 +00:00
|
|
|
# 2 for mapped keys
|
|
|
|
# 3 for forwarded events
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(len(history), 13)
|
|
|
|
|
|
|
|
# the first bit is ordered properly
|
|
|
|
self.assertEqual(history[0], (EV_KEY, 8, 1)) # forwarded
|
|
|
|
del history[0]
|
|
|
|
self.assertIn((EV_KEY, 8, 0), history[0:2]) # released by combination handler
|
|
|
|
self.assertIn((EV_KEY, 9, 0), history[0:2]) # released by combination handler
|
|
|
|
del history[0]
|
|
|
|
del history[0]
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-11-28 23:13:13 +00:00
|
|
|
# since the macro takes a little bit of time to execute, its
|
|
|
|
# keystrokes are all over the place.
|
|
|
|
# just check if they are there and if so, remove them from the list.
|
2022-04-17 10:19:23 +00:00
|
|
|
# the macro itself
|
2020-12-31 20:46:57 +00:00
|
|
|
self.assertIn((EV_KEY, code_q, 1), history)
|
|
|
|
self.assertIn((EV_KEY, code_q, 0), history)
|
|
|
|
self.assertIn((EV_KEY, code_w, 1), history)
|
|
|
|
self.assertIn((EV_KEY, code_w, 0), history)
|
|
|
|
index_q_1 = history.index((EV_KEY, code_q, 1))
|
|
|
|
index_q_0 = history.index((EV_KEY, code_q, 0))
|
|
|
|
index_w_1 = history.index((EV_KEY, code_w, 1))
|
|
|
|
index_w_0 = history.index((EV_KEY, code_w, 0))
|
2020-11-28 23:13:13 +00:00
|
|
|
self.assertGreater(index_q_0, index_q_1)
|
|
|
|
self.assertGreater(index_w_1, index_q_0)
|
|
|
|
self.assertGreater(index_w_0, index_w_1)
|
|
|
|
del history[index_w_0]
|
2022-04-17 10:19:23 +00:00
|
|
|
del history[index_w_1]
|
|
|
|
del history[index_q_0]
|
|
|
|
del history[index_q_1]
|
2020-11-28 23:13:13 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# the rest should be in order now.
|
|
|
|
# first the released combination key which did not release the macro.
|
|
|
|
# the combination key which released the macro won't appear here.
|
|
|
|
self.assertEqual(history[0], (EV_KEY, 9, 0))
|
2020-12-31 20:46:57 +00:00
|
|
|
# value should be 1, even if the input event was -1.
|
|
|
|
# Injected keycodes should always be either 0 or 1
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(history[1], (EV_KEY, code_a, 1))
|
|
|
|
self.assertEqual(history[2], (EV_KEY, code_a, 0))
|
|
|
|
self.assertEqual(history[3], (EV_KEY, input_b, 1))
|
|
|
|
self.assertEqual(history[4], (EV_KEY, input_b, 0))
|
|
|
|
self.assertEqual(history[5], (3124, 3564, 6542))
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-12-03 19:37:36 +00:00
|
|
|
time.sleep(0.1)
|
2021-02-13 20:17:08 +00:00
|
|
|
self.assertTrue(self.injector.is_alive())
|
2020-12-03 19:37:36 +00:00
|
|
|
|
2020-12-16 11:57:09 +00:00
|
|
|
numlock_after = is_numlock_on()
|
|
|
|
self.assertEqual(numlock_before, numlock_after)
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(self.injector.get_state(), RUNNING)
|
2020-12-16 11:57:09 +00:00
|
|
|
|
2020-12-31 20:46:57 +00:00
|
|
|
def test_is_in_capabilities(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
key = EventCombination((1, 2, 1))
|
2021-09-26 10:44:56 +00:00
|
|
|
capabilities = {1: [9, 2, 5]}
|
2020-12-31 20:46:57 +00:00
|
|
|
self.assertTrue(is_in_capabilities(key, capabilities))
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
key = EventCombination(((1, 2, 1), (1, 3, 1)))
|
2021-09-26 10:44:56 +00:00
|
|
|
capabilities = {1: [9, 2, 5]}
|
2020-12-31 20:46:57 +00:00
|
|
|
# only one of the codes of the combination is required.
|
2022-01-31 19:58:37 +00:00
|
|
|
# The goal is to make combinations= across those sub-devices possible,
|
2020-12-31 20:46:57 +00:00
|
|
|
# that make up one hardware device
|
|
|
|
self.assertTrue(is_in_capabilities(key, capabilities))
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
key = EventCombination(((1, 2, 1), (1, 5, 1)))
|
2021-09-26 10:44:56 +00:00
|
|
|
capabilities = {1: [9, 2, 5]}
|
2020-12-31 20:46:57 +00:00
|
|
|
self.assertTrue(is_in_capabilities(key, capabilities))
|
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2021-01-17 14:09:47 +00:00
|
|
|
class TestModifyCapabilities(unittest.TestCase):
|
2021-09-29 18:17:45 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
quick_cleanup()
|
|
|
|
|
2021-01-17 14:09:47 +00:00
|
|
|
def setUp(self):
|
|
|
|
class FakeDevice:
|
2021-02-07 21:16:41 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._capabilities = {
|
2021-01-17 14:09:47 +00:00
|
|
|
evdev.ecodes.EV_SYN: [1, 2, 3],
|
|
|
|
evdev.ecodes.EV_FF: [1, 2, 3],
|
2021-02-13 13:11:49 +00:00
|
|
|
EV_ABS: [
|
2021-09-26 10:44:56 +00:00
|
|
|
(
|
|
|
|
1,
|
|
|
|
evdev.AbsInfo(
|
|
|
|
value=None,
|
|
|
|
min=None,
|
|
|
|
max=1234,
|
|
|
|
fuzz=None,
|
|
|
|
flat=None,
|
|
|
|
resolution=None,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
2,
|
|
|
|
evdev.AbsInfo(
|
|
|
|
value=None,
|
|
|
|
min=50,
|
|
|
|
max=2345,
|
|
|
|
fuzz=None,
|
|
|
|
flat=None,
|
|
|
|
resolution=None,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
3,
|
|
|
|
],
|
2021-01-17 14:09:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 13:11:49 +00:00
|
|
|
def capabilities(self, absinfo=False):
|
|
|
|
assert absinfo is True
|
2021-02-07 21:16:41 +00:00
|
|
|
return self._capabilities
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset()
|
|
|
|
preset.add(get_key_mapping(EventCombination([EV_KEY, 80, 1]), "keyboard", "a"))
|
|
|
|
preset.add(
|
2022-04-18 11:52:59 +00:00
|
|
|
get_key_mapping(
|
|
|
|
EventCombination([EV_KEY, 81, 1]),
|
|
|
|
"keyboard",
|
|
|
|
DISABLE_NAME,
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2021-01-17 14:09:47 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
macro_code = "r(2, m(sHiFt_l, r(2, k(1).k(2))))"
|
2022-04-17 10:19:23 +00:00
|
|
|
macro = parse(macro_code, preset)
|
2021-01-17 14:09:47 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset.add(
|
2022-04-18 11:52:59 +00:00
|
|
|
get_key_mapping(
|
|
|
|
EventCombination([EV_KEY, 60, 111]), "keyboard", macro_code
|
|
|
|
),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2021-01-17 14:09:47 +00:00
|
|
|
|
|
|
|
# going to be ignored, because EV_REL cannot be mapped, that's
|
|
|
|
# mouse movements.
|
2022-04-17 10:19:23 +00:00
|
|
|
preset.add(
|
2022-04-18 11:52:59 +00:00
|
|
|
get_key_mapping(EventCombination([EV_REL, 1234, 3]), "keyboard", "b"),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2021-01-17 14:09:47 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.a = system_mapping.get("a")
|
|
|
|
self.shift_l = system_mapping.get("ShIfT_L")
|
2021-01-17 14:09:47 +00:00
|
|
|
self.one = system_mapping.get(1)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.two = system_mapping.get("2")
|
|
|
|
self.left = system_mapping.get("BtN_lEfT")
|
2021-01-17 14:09:47 +00:00
|
|
|
self.fake_device = FakeDevice()
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset = preset
|
2021-01-17 14:09:47 +00:00
|
|
|
self.macro = macro
|
|
|
|
|
|
|
|
def check_keys(self, capabilities):
|
|
|
|
"""No matter the configuration, EV_KEY will be mapped to EV_KEY."""
|
|
|
|
self.assertIn(EV_KEY, capabilities)
|
|
|
|
keys = capabilities[EV_KEY]
|
|
|
|
self.assertIn(self.a, keys)
|
|
|
|
self.assertIn(self.one, keys)
|
|
|
|
self.assertIn(self.two, keys)
|
|
|
|
self.assertIn(self.shift_l, keys)
|
|
|
|
self.assertNotIn(DISABLE_CODE, keys)
|
|
|
|
|
|
|
|
def tearDown(self):
|
2021-02-12 20:43:40 +00:00
|
|
|
quick_cleanup()
|
2021-01-17 14:09:47 +00:00
|
|
|
|
2021-02-18 19:38:14 +00:00
|
|
|
def test_copy_capabilities(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
self.preset.add(
|
|
|
|
get_key_mapping(
|
2022-04-18 11:52:59 +00:00
|
|
|
EventCombination([EV_KEY, 60, 1]),
|
|
|
|
"keyboard",
|
|
|
|
self.macro.code,
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2021-02-07 21:16:41 +00:00
|
|
|
# I don't know what ABS_VOLUME is, for now I would like to just always
|
2021-02-18 19:38:14 +00:00
|
|
|
# remove it until somebody complains, since its presence broke stuff
|
2022-04-17 10:19:23 +00:00
|
|
|
self.injector = Injector(None, self.preset)
|
2021-02-07 21:16:41 +00:00
|
|
|
self.fake_device._capabilities = {
|
2021-02-18 19:38:14 +00:00
|
|
|
EV_ABS: [ABS_VOLUME, (ABS_X, evdev.AbsInfo(0, 0, 500, 0, 0, 0))],
|
|
|
|
EV_KEY: [1, 2, 3],
|
|
|
|
EV_REL: [11, 12, 13],
|
|
|
|
evdev.ecodes.EV_SYN: [1],
|
|
|
|
evdev.ecodes.EV_FF: [2],
|
2021-02-07 21:16:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 19:38:14 +00:00
|
|
|
capabilities = self.injector._copy_capabilities(self.fake_device)
|
2021-02-07 21:16:41 +00:00
|
|
|
self.assertNotIn(ABS_VOLUME, capabilities[EV_ABS])
|
2021-02-18 19:38:14 +00:00
|
|
|
self.assertNotIn(evdev.ecodes.EV_SYN, capabilities)
|
|
|
|
self.assertNotIn(evdev.ecodes.EV_FF, capabilities)
|
|
|
|
self.assertListEqual(capabilities[EV_KEY], [1, 2, 3])
|
|
|
|
self.assertListEqual(capabilities[EV_REL], [11, 12, 13])
|
|
|
|
self.assertEqual(capabilities[EV_ABS][0][1].max, 500)
|
2021-02-07 21:16:41 +00:00
|
|
|
|
2021-01-17 14:09:47 +00:00
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|