2020-11-22 14:14:43 +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-22 14:14:43 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-22 14:14:43 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-22 14:14:43 +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-22 14:14:43 +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/>.
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
from tests.test import (
|
|
|
|
new_event,
|
|
|
|
push_events,
|
|
|
|
send_event_to_reader,
|
|
|
|
EVENT_READ_TIMEOUT,
|
|
|
|
START_READING_DELAY,
|
|
|
|
quick_cleanup,
|
|
|
|
MAX_ABS,
|
|
|
|
)
|
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
import unittest
|
2021-03-21 18:56:15 +00:00
|
|
|
from unittest import mock
|
2020-11-30 17:59:34 +00:00
|
|
|
import time
|
2020-12-31 22:16:46 +00:00
|
|
|
import multiprocessing
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
from evdev.ecodes import (
|
|
|
|
EV_KEY,
|
|
|
|
EV_ABS,
|
|
|
|
ABS_HAT0X,
|
|
|
|
KEY_COMMA,
|
|
|
|
BTN_TOOL_DOUBLETAP,
|
|
|
|
ABS_Z,
|
|
|
|
ABS_Y,
|
|
|
|
KEY_A,
|
|
|
|
EV_REL,
|
|
|
|
REL_WHEEL,
|
|
|
|
REL_X,
|
|
|
|
ABS_X,
|
|
|
|
ABS_RZ,
|
|
|
|
)
|
2020-12-26 18:36:47 +00:00
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.gui.reader import reader, will_report_up
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.gui.active_preset import active_preset
|
|
|
|
from inputremapper.configs.global_config import BUTTONS, MOUSE
|
|
|
|
from inputremapper.event_combination import EventCombination
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.gui.helper import RootHelper
|
|
|
|
from inputremapper.groups import groups
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
CODE_1 = 100
|
|
|
|
CODE_2 = 101
|
|
|
|
CODE_3 = 102
|
|
|
|
|
|
|
|
|
2020-12-03 19:03:53 +00:00
|
|
|
def wait(func, timeout=1.0):
|
|
|
|
"""Wait for func to return True."""
|
|
|
|
iterations = 0
|
|
|
|
sleepytime = 0.1
|
|
|
|
while not func():
|
|
|
|
time.sleep(sleepytime)
|
|
|
|
iterations += 1
|
|
|
|
if iterations * sleepytime > timeout:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
class TestReader(unittest.TestCase):
|
2020-11-30 17:59:34 +00:00
|
|
|
def setUp(self):
|
2021-03-21 18:15:20 +00:00
|
|
|
self.helper = None
|
2020-11-30 17:59:34 +00:00
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
def tearDown(self):
|
2021-02-12 20:43:40 +00:00
|
|
|
quick_cleanup()
|
2021-03-21 18:15:20 +00:00
|
|
|
if self.helper is not None:
|
|
|
|
self.helper.join()
|
2021-04-23 09:51:21 +00:00
|
|
|
groups.refresh()
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
def create_helper(self):
|
|
|
|
# this will cause pending events to be copied over to the helper
|
|
|
|
# process
|
|
|
|
def start_helper():
|
|
|
|
helper = RootHelper()
|
|
|
|
helper.run()
|
|
|
|
|
|
|
|
self.helper = multiprocessing.Process(target=start_helper)
|
|
|
|
self.helper.start()
|
|
|
|
time.sleep(0.1)
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
def test_will_report_up(self):
|
|
|
|
self.assertFalse(will_report_up(EV_REL))
|
|
|
|
self.assertTrue(will_report_up(EV_ABS))
|
|
|
|
self.assertTrue(will_report_up(EV_KEY))
|
|
|
|
|
2020-12-03 19:03:53 +00:00
|
|
|
def test_reading_1(self):
|
2020-12-31 20:46:57 +00:00
|
|
|
# a single event
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events("Foo Device 2", [new_event(EV_ABS, ABS_HAT0X, 1)])
|
|
|
|
push_events(
|
2022-04-18 11:52:59 +00:00
|
|
|
"Foo Device 2",
|
|
|
|
[new_event(EV_ABS, REL_X, 1)],
|
2021-09-26 10:44:56 +00:00
|
|
|
) # mouse movements are ignored
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(0.2)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_ABS, ABS_HAT0X, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2020-12-31 20:46:57 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
def test_reading_wheel(self):
|
|
|
|
# will be treated as released automatically at some point
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_REL, REL_WHEEL, 0))
|
|
|
|
self.assertIsNone(reader.read())
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_REL, REL_WHEEL, 1))
|
|
|
|
result = reader.read()
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertIsInstance(result, EventCombination)
|
|
|
|
self.assertIsInstance(result, tuple)
|
|
|
|
self.assertEqual(result, EventCombination((EV_REL, REL_WHEEL, 1)))
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(result, ((EV_REL, REL_WHEEL, 1),))
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertNotEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
result,
|
|
|
|
EventCombination(((EV_REL, REL_WHEEL, 1), (1, 1, 1))),
|
2022-04-17 10:19:23 +00:00
|
|
|
)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# it won't return the same event twice
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# but it is still remembered unreleased
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
reader.get_unreleased_keys(),
|
|
|
|
EventCombination((EV_REL, REL_WHEEL, 1)),
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
|
|
|
self.assertIsInstance(reader.get_unreleased_keys(), EventCombination)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# as long as new wheel events arrive, it is considered unreleased
|
|
|
|
for _ in range(10):
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_REL, REL_WHEEL, 1))
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# read a few more times, at some point it is treated as unreleased
|
|
|
|
for _ in range(4):
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 0)
|
|
|
|
self.assertIsNone(reader.get_unreleased_keys())
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Combinations"""
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_REL, REL_WHEEL, 1, 1000))
|
|
|
|
send_event_to_reader(new_event(EV_KEY, KEY_COMMA, 1, 1001))
|
2022-04-17 10:19:23 +00:00
|
|
|
combi_1 = EventCombination(((EV_REL, REL_WHEEL, 1), (EV_KEY, KEY_COMMA, 1)))
|
|
|
|
combi_2 = EventCombination(((EV_KEY, KEY_COMMA, 1), (EV_KEY, KEY_A, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
read = reader.read()
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(read, combi_1)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 2)
|
|
|
|
self.assertEqual(reader.get_unreleased_keys(), combi_1)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# don't send new wheel down events, it should get released again
|
|
|
|
i = 0
|
2021-03-21 18:15:20 +00:00
|
|
|
while len(reader._unreleased) == 2:
|
|
|
|
read = reader.read()
|
2021-01-07 16:15:12 +00:00
|
|
|
if i == 100:
|
2021-09-26 10:44:56 +00:00
|
|
|
raise AssertionError("Did not release the wheel")
|
2021-01-07 16:15:12 +00:00
|
|
|
i += 1
|
|
|
|
# and only the comma remains. However, a changed combination is
|
|
|
|
# only returned when a new key is pressed. Only then the pressed
|
|
|
|
# down keys are collected in a new Key object.
|
|
|
|
self.assertEqual(read, None)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.get_unreleased_keys(), EventCombination(combi_1[1]))
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# press down a new key, now it will return a different combination
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_KEY, KEY_A, 1, 1002))
|
|
|
|
self.assertEqual(reader.read(), combi_2)
|
|
|
|
self.assertEqual(len(reader._unreleased), 2)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# release all of them
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_KEY, KEY_COMMA, 0))
|
|
|
|
send_event_to_reader(new_event(EV_KEY, KEY_A, 0))
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 0)
|
|
|
|
self.assertEqual(reader.get_unreleased_keys(), None)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-01-05 18:33:47 +00:00
|
|
|
def test_change_wheel_direction(self):
|
2021-01-07 16:15:12 +00:00
|
|
|
# not just wheel, anything that suddenly reports a different value.
|
|
|
|
# as long as type and code are equal its the same key, so there is no
|
|
|
|
# way both directions can be held down.
|
2021-03-21 18:56:15 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.create_helper()
|
|
|
|
self.assertEqual(reader.read(), None)
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
2021-01-05 18:33:47 +00:00
|
|
|
|
2021-03-21 18:56:15 +00:00
|
|
|
send_event_to_reader(new_event(EV_REL, REL_WHEEL, 1))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_REL, REL_WHEEL, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
|
|
|
self.assertEqual(reader.read(), None)
|
2021-01-05 18:33:47 +00:00
|
|
|
|
2021-03-21 18:56:15 +00:00
|
|
|
send_event_to_reader(new_event(EV_REL, REL_WHEEL, -1))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_REL, REL_WHEEL, -1)))
|
2021-01-05 18:33:47 +00:00
|
|
|
# notice that this is no combination of two sides, the previous
|
|
|
|
# entry in unreleased has to get overwritten. So there is still only
|
|
|
|
# one element in it.
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
|
|
|
|
def test_change_device(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"Foo Device 2",
|
|
|
|
[
|
|
|
|
new_event(EV_KEY, 1, 1),
|
|
|
|
]
|
|
|
|
* 100,
|
|
|
|
)
|
|
|
|
|
|
|
|
push_events(
|
|
|
|
"Bar Device",
|
|
|
|
[
|
|
|
|
new_event(EV_KEY, 2, 1),
|
|
|
|
]
|
|
|
|
* 100,
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
self.create_helper()
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(0.1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_KEY, 1, 1)))
|
2021-01-05 18:33:47 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(name="Bar Device"))
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
# it's plausible that right after sending the new read command more
|
|
|
|
# events from the old device might still appear. Give the helper
|
|
|
|
# some time to handle the new command.
|
2021-01-07 16:15:12 +00:00
|
|
|
time.sleep(0.1)
|
2021-03-21 18:15:20 +00:00
|
|
|
reader.clear()
|
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
time.sleep(0.1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_KEY, 2, 1)))
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2020-12-31 20:46:57 +00:00
|
|
|
def test_reading_2(self):
|
|
|
|
# a combination of events
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"Foo Device 2",
|
|
|
|
[
|
|
|
|
new_event(EV_KEY, CODE_1, 1, 10000.1234),
|
|
|
|
new_event(EV_KEY, CODE_3, 1, 10001.1234),
|
|
|
|
new_event(EV_ABS, ABS_HAT0X, -1, 10002.1234),
|
|
|
|
],
|
|
|
|
)
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
pipe = multiprocessing.Pipe()
|
|
|
|
|
|
|
|
def refresh():
|
|
|
|
# from within the helper process notify this test that
|
|
|
|
# refresh was called as expected
|
2021-09-26 10:44:56 +00:00
|
|
|
pipe[1].send("refreshed")
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
with mock.patch.object(groups, "refresh", refresh):
|
2021-04-23 09:51:21 +00:00
|
|
|
self.create_helper()
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2020-12-01 22:53:32 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
# sending anything arbitrary does not stop the helper
|
|
|
|
reader._commands.send(856794)
|
|
|
|
time.sleep(0.2)
|
2021-04-23 09:51:21 +00:00
|
|
|
# but it makes it look for new devices because maybe its list of
|
|
|
|
# groups is not up-to-date
|
|
|
|
self.assertTrue(pipe[0].poll())
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(pipe[0].recv(), "refreshed")
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
reader.read(),
|
2021-09-29 18:17:45 +00:00
|
|
|
((EV_KEY, CODE_1, 1), (EV_KEY, CODE_3, 1), (EV_ABS, ABS_HAT0X, -1)),
|
2021-09-26 10:44:56 +00:00
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 3)
|
2020-12-19 15:04:07 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
def test_reading_3(self):
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
|
|
|
# a combination of events via Socket with reads inbetween
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(name="gamepad"))
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_KEY, CODE_1, 1, 1001))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_KEY, CODE_1, 1)))
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# active_preset.set("gamepad.joystick.left_purpose", BUTTONS)
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_Y, 1, 1002))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
reader.read(),
|
|
|
|
EventCombination(((EV_KEY, CODE_1, 1), (EV_ABS, ABS_Y, 1))),
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_HAT0X, -1, 1003))
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(
|
|
|
|
reader.read(),
|
2022-01-31 19:58:37 +00:00
|
|
|
EventCombination(
|
2022-04-18 11:52:59 +00:00
|
|
|
((EV_KEY, CODE_1, 1), (EV_ABS, ABS_Y, 1), (EV_ABS, ABS_HAT0X, -1)),
|
2022-01-31 19:58:37 +00:00
|
|
|
),
|
2021-09-26 10:44:56 +00:00
|
|
|
)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# adding duplicate down events won't report a different combination.
|
|
|
|
# import for triggers, as they keep reporting more down-events before
|
|
|
|
# they are released
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_Y, 1, 1005))
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_HAT0X, -1, 1006))
|
|
|
|
self.assertEqual(reader.read(), None)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_KEY, CODE_1, 0, 1004))
|
|
|
|
read = reader.read()
|
2021-01-07 16:15:12 +00:00
|
|
|
self.assertEqual(read, None)
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_Y, 0, 1007))
|
|
|
|
self.assertEqual(reader.read(), None)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_KEY, ABS_HAT0X, 0, 1008))
|
|
|
|
self.assertEqual(reader.read(), None)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-01-01 21:20:33 +00:00
|
|
|
def test_reads_joysticks(self):
|
|
|
|
# if their purpose is "buttons"
|
2022-04-17 10:19:23 +00:00
|
|
|
# active_preset.set("gamepad.joystick.left_purpose", BUTTONS)
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"gamepad",
|
|
|
|
[
|
|
|
|
new_event(EV_ABS, ABS_Y, MAX_ABS),
|
|
|
|
# the value of that one is interpreted as release, because
|
|
|
|
# it is too small
|
|
|
|
new_event(EV_ABS, ABS_X, MAX_ABS // 10),
|
|
|
|
],
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(name="gamepad"))
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(0.2)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_ABS, ABS_Y, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
|
|
|
|
|
|
|
reader._unreleased = {}
|
2022-04-17 10:19:23 +00:00
|
|
|
# active_preset.set("gamepad.joystick.left_purpose", MOUSE)
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events("gamepad", [new_event(EV_ABS, ABS_Y, MAX_ABS)])
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(name="gamepad"))
|
2021-01-01 21:20:33 +00:00
|
|
|
time.sleep(0.1)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 0)
|
2021-01-01 21:20:33 +00:00
|
|
|
|
2021-01-25 23:15:30 +00:00
|
|
|
def test_combine_triggers(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-01-25 23:15:30 +00:00
|
|
|
|
|
|
|
i = 0
|
2021-03-21 18:15:20 +00:00
|
|
|
|
2021-01-25 23:15:30 +00:00
|
|
|
def next_timestamp():
|
|
|
|
nonlocal i
|
|
|
|
i += 1
|
2021-03-21 18:15:20 +00:00
|
|
|
return time.time() + i
|
2021-01-25 23:15:30 +00:00
|
|
|
|
|
|
|
# based on an observed bug
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(3, 1, 0, next_timestamp()))
|
|
|
|
send_event_to_reader(new_event(3, 0, 0, next_timestamp()))
|
|
|
|
send_event_to_reader(new_event(3, 2, 1, next_timestamp()))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_ABS, ABS_Z, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(3, 0, 0, next_timestamp()))
|
|
|
|
send_event_to_reader(new_event(3, 5, 1, next_timestamp()))
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(
|
2022-04-18 11:52:59 +00:00
|
|
|
reader.read(),
|
|
|
|
EventCombination(((EV_ABS, ABS_Z, 1), (EV_ABS, ABS_RZ, 1))),
|
2022-01-31 19:58:37 +00:00
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(3, 5, 0, next_timestamp()))
|
|
|
|
send_event_to_reader(new_event(3, 0, 0, next_timestamp()))
|
|
|
|
send_event_to_reader(new_event(3, 1, 0, next_timestamp()))
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
send_event_to_reader(new_event(3, 2, 1, next_timestamp()))
|
|
|
|
send_event_to_reader(new_event(3, 1, 0, next_timestamp()))
|
|
|
|
send_event_to_reader(new_event(3, 0, 0, next_timestamp()))
|
2021-01-25 23:15:30 +00:00
|
|
|
# due to not properly handling the duplicate down event it cleared
|
|
|
|
# the combination and returned it. Instead it should report None
|
|
|
|
# and by doing that keep the previous combination.
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
2021-01-25 23:15:30 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def test_blacklisted_events(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"Foo Device 2",
|
|
|
|
[
|
|
|
|
new_event(EV_KEY, BTN_TOOL_DOUBLETAP, 1),
|
|
|
|
new_event(EV_KEY, CODE_2, 1),
|
|
|
|
new_event(EV_KEY, BTN_TOOL_DOUBLETAP, 1),
|
|
|
|
],
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2020-12-27 12:09:28 +00:00
|
|
|
time.sleep(0.1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_KEY, CODE_2, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2020-12-31 20:46:57 +00:00
|
|
|
|
|
|
|
def test_ignore_value_2(self):
|
|
|
|
# this is not a combination, because (EV_KEY CODE_3, 2) is ignored
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"Foo Device 2",
|
|
|
|
[new_event(EV_ABS, ABS_HAT0X, 1), new_event(EV_KEY, CODE_3, 2)],
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(0.2)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_ABS, ABS_HAT0X, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2020-12-27 12:09:28 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
def test_reading_ignore_up(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"Foo Device 2",
|
|
|
|
[
|
|
|
|
new_event(EV_KEY, CODE_1, 0, 10),
|
|
|
|
new_event(EV_KEY, CODE_2, 1, 11),
|
|
|
|
new_event(EV_KEY, CODE_3, 0, 12),
|
|
|
|
],
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2020-12-19 15:04:07 +00:00
|
|
|
time.sleep(0.1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_KEY, CODE_2, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2020-12-03 19:03:53 +00:00
|
|
|
|
2020-12-31 22:16:46 +00:00
|
|
|
def test_reading_ignore_duplicate_down(self):
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_Z, 1, 10))
|
2020-12-31 22:16:46 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_ABS, ABS_Z, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
2020-12-31 22:16:46 +00:00
|
|
|
|
2021-01-07 16:15:12 +00:00
|
|
|
# duplicate
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_Z, 1, 10))
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
|
|
|
self.assertEqual(len(reader.get_unreleased_keys()), 1)
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertIsInstance(reader.get_unreleased_keys(), EventCombination)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
|
|
|
# release
|
2021-03-21 18:15:20 +00:00
|
|
|
send_event_to_reader(new_event(EV_ABS, ABS_Z, 0, 10))
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 0)
|
|
|
|
self.assertIsNone(reader.get_unreleased_keys())
|
2020-12-31 22:16:46 +00:00
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
def test_wrong_device(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"Foo Device 2",
|
|
|
|
[
|
|
|
|
new_event(EV_KEY, CODE_1, 1),
|
|
|
|
new_event(EV_KEY, CODE_2, 1),
|
|
|
|
new_event(EV_KEY, CODE_3, 1),
|
|
|
|
],
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(name="Bar Device"))
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 0)
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
def test_inputremapper_devices(self):
|
|
|
|
# Don't read from inputremapper devices, their keycodes are not
|
2020-12-01 22:53:32 +00:00
|
|
|
# representative for the original key. As long as this is not
|
|
|
|
# intentionally programmed it won't even do that. But it was at some
|
|
|
|
# point.
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
2022-01-01 12:00:49 +00:00
|
|
|
"input-remapper Bar Device",
|
2021-09-26 10:44:56 +00:00
|
|
|
[
|
|
|
|
new_event(EV_KEY, CODE_1, 1),
|
|
|
|
new_event(EV_KEY, CODE_2, 1),
|
|
|
|
new_event(EV_KEY, CODE_3, 1),
|
|
|
|
],
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(name="Bar Device"))
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 0)
|
2020-11-22 16:30:06 +00:00
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
def test_clear(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events(
|
|
|
|
"Foo Device 2",
|
|
|
|
[
|
|
|
|
new_event(EV_KEY, CODE_1, 1),
|
|
|
|
new_event(EV_KEY, CODE_2, 1),
|
|
|
|
new_event(EV_KEY, CODE_3, 1),
|
|
|
|
]
|
|
|
|
* 15,
|
|
|
|
)
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(START_READING_DELAY + EVENT_READ_TIMEOUT * 3)
|
|
|
|
|
|
|
|
reader.read()
|
|
|
|
self.assertEqual(len(reader._unreleased), 3)
|
|
|
|
self.assertIsNotNone(reader.previous_event)
|
|
|
|
self.assertIsNotNone(reader.previous_result)
|
|
|
|
|
|
|
|
# make the helper send more events to the reader
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 2)
|
|
|
|
self.assertTrue(reader._results.poll())
|
|
|
|
reader.clear()
|
|
|
|
|
|
|
|
self.assertFalse(reader._results.poll())
|
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 0)
|
|
|
|
self.assertIsNone(reader.get_unreleased_keys())
|
|
|
|
self.assertIsNone(reader.previous_event)
|
|
|
|
self.assertIsNone(reader.previous_result)
|
|
|
|
self.tearDown()
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
def test_switch_device(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events("Bar Device", [new_event(EV_KEY, CODE_1, 1)])
|
|
|
|
push_events("Foo Device 2", [new_event(EV_KEY, CODE_3, 1)])
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2020-11-30 17:59:34 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(name="Bar Device"))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertFalse(reader._results.poll())
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(reader.group.name, "Bar Device")
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertTrue(reader._results.poll())
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
|
|
|
self.assertEqual(reader.group.name, "Foo Device")
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertFalse(reader._results.poll()) # pipe resets
|
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 5)
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertTrue(reader._results.poll())
|
2020-11-22 14:14:43 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertEqual(reader.read(), EventCombination((EV_KEY, CODE_3, 1)))
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertEqual(reader.read(), None)
|
|
|
|
self.assertEqual(len(reader._unreleased), 1)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def test_terminate(self):
|
|
|
|
self.create_helper()
|
2021-09-26 10:44:56 +00:00
|
|
|
reader.start_reading(groups.find(key="Foo Device 2"))
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events("Foo Device 2", [new_event(EV_KEY, CODE_3, 1)])
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(START_READING_DELAY + EVENT_READ_TIMEOUT)
|
|
|
|
self.assertTrue(reader._results.poll())
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
reader.terminate()
|
|
|
|
reader.clear()
|
2021-03-27 07:10:14 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT)
|
2021-01-07 16:15:12 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
# no new events arrive after terminating
|
2021-09-26 10:44:56 +00:00
|
|
|
push_events("Foo Device 2", [new_event(EV_KEY, CODE_3, 1)])
|
2021-03-21 18:15:20 +00:00
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 3)
|
|
|
|
self.assertFalse(reader._results.poll())
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
def test_are_new_groups_available(self):
|
2021-03-21 18:15:20 +00:00
|
|
|
self.create_helper()
|
2021-04-23 09:51:21 +00:00
|
|
|
groups.set_groups({})
|
2021-03-21 18:15:20 +00:00
|
|
|
|
|
|
|
# read stuff from the helper, which includes the devices
|
2022-01-10 19:37:22 +00:00
|
|
|
self.assertFalse(reader.are_new_groups_available())
|
2021-03-21 18:15:20 +00:00
|
|
|
reader.read()
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
self.assertTrue(reader.are_new_groups_available())
|
2021-04-02 13:08:36 +00:00
|
|
|
# a bit weird, but it assumes the gui handled that and returns
|
|
|
|
# false afterwards
|
2022-01-10 19:37:22 +00:00
|
|
|
self.assertFalse(reader.are_new_groups_available())
|
2021-04-02 13:08:36 +00:00
|
|
|
|
|
|
|
# send the same devices again
|
2021-09-26 10:44:56 +00:00
|
|
|
reader._get_event({"type": "groups", "message": groups.dumps()})
|
2022-01-10 19:37:22 +00:00
|
|
|
self.assertFalse(reader.are_new_groups_available())
|
2021-04-02 13:08:36 +00:00
|
|
|
|
|
|
|
# send changed devices
|
2021-04-23 09:51:21 +00:00
|
|
|
message = groups.dumps()
|
2021-09-26 10:44:56 +00:00
|
|
|
message = message.replace("Foo Device", "foo_device")
|
|
|
|
reader._get_event({"type": "groups", "message": message})
|
2022-01-10 19:37:22 +00:00
|
|
|
self.assertTrue(reader.are_new_groups_available())
|
|
|
|
self.assertFalse(reader.are_new_groups_available())
|
2020-12-12 14:22:34 +00:00
|
|
|
|
2020-11-22 14:14:43 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|