2020-11-20 20:38:59 +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-20 20:38:59 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-20 20:38:59 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-20 20:38:59 +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-20 20:38:59 +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-20 20:38:59 +00:00
|
|
|
|
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
from tests.test import (
|
|
|
|
cleanup,
|
|
|
|
uinput_write_history_pipe,
|
|
|
|
new_event,
|
|
|
|
push_events,
|
|
|
|
is_service_running,
|
|
|
|
fixtures,
|
|
|
|
tmp,
|
2022-04-17 10:19:23 +00:00
|
|
|
get_key_mapping,
|
2022-03-03 22:42:37 +00:00
|
|
|
)
|
|
|
|
|
2020-11-29 00:44:14 +00:00
|
|
|
import os
|
2020-11-20 20:38:59 +00:00
|
|
|
import unittest
|
2020-11-29 00:44:14 +00:00
|
|
|
import time
|
2020-12-26 18:36:47 +00:00
|
|
|
import subprocess
|
2021-02-07 14:00:36 +00:00
|
|
|
import json
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
import evdev
|
2022-08-28 10:38:40 +00:00
|
|
|
from evdev.ecodes import EV_KEY, EV_ABS, KEY_B, KEY_A, ABS_X, BTN_A, BTN_B
|
2020-12-26 18:36:47 +00:00
|
|
|
from pydbus import SystemBus
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.system_mapping import system_mapping
|
|
|
|
from inputremapper.configs.global_config import global_config
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.groups import groups
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.paths import get_config_path, mkdir, get_preset_path
|
|
|
|
from inputremapper.event_combination import EventCombination
|
|
|
|
from inputremapper.configs.preset import Preset
|
2022-11-01 11:07:12 +00:00
|
|
|
from inputremapper.injection.injector import InjectorState
|
2022-01-17 23:55:01 +00:00
|
|
|
from inputremapper.daemon import Daemon
|
2022-03-17 09:32:22 +00:00
|
|
|
from inputremapper.injection.global_uinputs import global_uinputs
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
|
2020-12-26 18:36:47 +00:00
|
|
|
check_output = subprocess.check_output
|
2021-03-21 18:15:20 +00:00
|
|
|
os_system = os.system
|
2020-12-26 18:36:47 +00:00
|
|
|
dbus_get = type(SystemBus()).get
|
|
|
|
|
|
|
|
|
2020-11-20 20:38:59 +00:00
|
|
|
class TestDaemon(unittest.TestCase):
|
2021-09-26 10:44:56 +00:00
|
|
|
new_fixture_path = "/dev/input/event9876"
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2020-11-25 22:36:03 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.grab = evdev.InputDevice.grab
|
|
|
|
self.daemon = None
|
2021-03-21 18:15:20 +00:00
|
|
|
mkdir(get_config_path())
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config._save_config()
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
# the daemon should be able to create them on demand:
|
|
|
|
global_uinputs.devices = {}
|
|
|
|
global_uinputs.is_service = True
|
|
|
|
|
2020-11-20 20:38:59 +00:00
|
|
|
def tearDown(self):
|
|
|
|
# avoid race conditions with other tests, daemon may run processes
|
|
|
|
if self.daemon is not None:
|
2021-02-07 14:00:36 +00:00
|
|
|
self.daemon.stop_all()
|
2020-11-20 20:38:59 +00:00
|
|
|
self.daemon = None
|
|
|
|
evdev.InputDevice.grab = self.grab
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2020-12-26 18:36:47 +00:00
|
|
|
subprocess.check_output = check_output
|
2021-03-21 18:15:20 +00:00
|
|
|
os.system = os_system
|
2020-12-26 18:36:47 +00:00
|
|
|
type(SystemBus()).get = dbus_get
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
cleanup()
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
def test_connect(self):
|
|
|
|
os_system_history = []
|
|
|
|
os.system = os_system_history.append
|
2020-12-26 18:36:47 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertFalse(is_service_running())
|
2021-10-22 13:37:04 +00:00
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
# no daemon runs, should try to run it via pkexec instead.
|
2021-10-22 13:37:04 +00:00
|
|
|
# It fails due to the patch on os.system and therefore exits the process
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertRaises(SystemExit, Daemon.connect)
|
|
|
|
self.assertEqual(len(os_system_history), 1)
|
|
|
|
self.assertIsNone(Daemon.connect(False))
|
2020-12-26 18:36:47 +00:00
|
|
|
|
2021-10-22 13:37:04 +00:00
|
|
|
# make the connect command work this time by acting like a connection is
|
|
|
|
# available:
|
|
|
|
|
|
|
|
set_config_dir_callcount = 0
|
|
|
|
|
2020-12-26 18:36:47 +00:00
|
|
|
class FakeConnection:
|
2021-10-22 13:37:04 +00:00
|
|
|
def set_config_dir(self, *args, **kwargs):
|
|
|
|
nonlocal set_config_dir_callcount
|
|
|
|
set_config_dir_callcount += 1
|
2020-12-26 18:36:47 +00:00
|
|
|
|
2021-11-14 13:32:22 +00:00
|
|
|
type(SystemBus()).get = lambda *args, **kwargs: FakeConnection()
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertIsInstance(Daemon.connect(), FakeConnection)
|
2021-10-22 13:37:04 +00:00
|
|
|
self.assertEqual(set_config_dir_callcount, 1)
|
|
|
|
|
2021-03-21 18:15:20 +00:00
|
|
|
self.assertIsInstance(Daemon.connect(False), FakeConnection)
|
2021-10-22 13:37:04 +00:00
|
|
|
self.assertEqual(set_config_dir_callcount, 2)
|
2020-12-26 18:36:47 +00:00
|
|
|
|
2020-11-20 20:38:59 +00:00
|
|
|
def test_daemon(self):
|
2021-02-07 14:00:36 +00:00
|
|
|
# remove the existing system mapping to force our own into it
|
2021-09-26 10:44:56 +00:00
|
|
|
if os.path.exists(get_config_path("xmodmap.json")):
|
|
|
|
os.remove(get_config_path("xmodmap.json"))
|
2021-02-07 14:00:36 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset_name = "foo"
|
2020-12-19 15:04:07 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
ev_1 = (EV_KEY, BTN_A)
|
|
|
|
ev_2 = (EV_ABS, ABS_X)
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
group = groups.find(name="gamepad")
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
# unrelated group that shouldn't be affected at all
|
|
|
|
group2 = groups.find(name="Bar Device")
|
2020-12-02 17:07:46 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset(group.get_preset_path(preset_name))
|
|
|
|
preset.add(get_key_mapping(EventCombination([*ev_1, 1]), "keyboard", "a"))
|
|
|
|
preset.add(get_key_mapping(EventCombination([*ev_2, -1]), "keyboard", "b"))
|
|
|
|
preset.save()
|
|
|
|
global_config.set_autoload_preset(group.key, preset_name)
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Injection 1"""
|
2020-12-19 15:04:07 +00:00
|
|
|
|
|
|
|
# should forward the event unchanged
|
2022-08-28 10:38:40 +00:00
|
|
|
push_events(fixtures.gamepad, [new_event(EV_KEY, BTN_B, 1)])
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
self.daemon = Daemon()
|
2020-12-02 17:07:46 +00:00
|
|
|
|
2021-01-01 21:20:33 +00:00
|
|
|
self.assertFalse(uinput_write_history_pipe[0].poll())
|
2022-03-17 09:32:22 +00:00
|
|
|
|
|
|
|
# has been cleanedUp in setUp
|
|
|
|
self.assertNotIn("keyboard", global_uinputs.devices)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.daemon.start_injecting(group.key, preset_name)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2022-03-17 09:32:22 +00:00
|
|
|
# created on demand
|
|
|
|
self.assertIn("keyboard", global_uinputs.devices)
|
|
|
|
self.assertNotIn("gamepad", global_uinputs.devices)
|
|
|
|
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertEqual(self.daemon.get_state(group.key), InjectorState.STARTING)
|
|
|
|
self.assertEqual(self.daemon.get_state(group2.key), InjectorState.UNKNOWN)
|
2020-12-02 17:07:46 +00:00
|
|
|
|
|
|
|
event = uinput_write_history_pipe[0].recv()
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertEqual(self.daemon.get_state(group.key), InjectorState.RUNNING)
|
2020-12-19 15:04:07 +00:00
|
|
|
self.assertEqual(event.type, EV_KEY)
|
2022-08-28 10:38:40 +00:00
|
|
|
self.assertEqual(event.code, BTN_B)
|
2020-12-19 15:04:07 +00:00
|
|
|
self.assertEqual(event.value, 1)
|
2020-12-02 17:07:46 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.daemon.stop_injecting(group.key)
|
2022-07-23 08:53:41 +00:00
|
|
|
time.sleep(0.2)
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertEqual(self.daemon.get_state(group.key), InjectorState.STOPPED)
|
2020-12-02 17:07:46 +00:00
|
|
|
|
2021-01-01 21:20:33 +00:00
|
|
|
try:
|
|
|
|
self.assertFalse(uinput_write_history_pipe[0].poll())
|
|
|
|
except AssertionError:
|
2021-09-26 10:44:56 +00:00
|
|
|
print("Unexpected", uinput_write_history_pipe[0].recv())
|
2021-01-05 18:33:47 +00:00
|
|
|
# possibly a duplicate write!
|
2021-01-01 21:20:33 +00:00
|
|
|
raise
|
|
|
|
|
2022-04-18 11:52:59 +00:00
|
|
|
"""Injection 2"""
|
2022-07-23 08:53:41 +00:00
|
|
|
self.daemon.start_injecting(group.key, preset_name)
|
2020-12-19 15:04:07 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
time.sleep(0.1)
|
2021-09-29 18:17:45 +00:00
|
|
|
# -1234 will be classified as -1 by the injector
|
2022-08-28 10:38:40 +00:00
|
|
|
push_events(fixtures.gamepad, [new_event(*ev_2, -1234)])
|
2021-02-07 14:00:36 +00:00
|
|
|
time.sleep(0.1)
|
2022-07-23 08:53:41 +00:00
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
self.assertTrue(uinput_write_history_pipe[0].poll())
|
2020-12-02 17:07:46 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
# the written key is a key-down event, not the original
|
2021-01-05 18:33:47 +00:00
|
|
|
# event value of -1234
|
2020-12-02 17:07:46 +00:00
|
|
|
event = uinput_write_history_pipe[0].recv()
|
2021-02-07 14:00:36 +00:00
|
|
|
|
2020-12-19 15:04:07 +00:00
|
|
|
self.assertEqual(event.type, EV_KEY)
|
2021-11-21 20:45:02 +00:00
|
|
|
self.assertEqual(event.code, KEY_B)
|
2020-12-02 17:07:46 +00:00
|
|
|
self.assertEqual(event.value, 1)
|
2020-11-20 20:38:59 +00:00
|
|
|
|
2021-10-22 14:07:16 +00:00
|
|
|
def test_config_dir(self):
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.set("foo", "bar")
|
|
|
|
self.assertEqual(global_config.get("foo"), "bar")
|
2021-10-22 14:07:16 +00:00
|
|
|
|
|
|
|
# freshly loads the config and therefore removes the previosly added key.
|
|
|
|
# This is important so that if the service is started via sudo or pkexec
|
|
|
|
# it knows where to look for configuration files.
|
|
|
|
self.daemon = Daemon()
|
|
|
|
self.assertEqual(self.daemon.config_dir, get_config_path())
|
2022-01-31 19:58:37 +00:00
|
|
|
self.assertIsNone(global_config.get("foo"))
|
2021-10-22 14:07:16 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def test_refresh_on_start(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
if os.path.exists(get_config_path("xmodmap.json")):
|
|
|
|
os.remove(get_config_path("xmodmap.json"))
|
2021-02-07 14:00:36 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset_name = "foo"
|
2020-12-24 00:26:34 +00:00
|
|
|
ev = (EV_KEY, 9)
|
2021-09-26 10:44:56 +00:00
|
|
|
group_name = "9876 name"
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
# expected key of the group
|
|
|
|
group_key = group_name
|
|
|
|
|
|
|
|
group = groups.find(name=group_name)
|
2020-12-24 00:26:34 +00:00
|
|
|
# this test only makes sense if this device is unknown yet
|
2021-04-23 09:51:21 +00:00
|
|
|
self.assertIsNone(group)
|
2022-04-17 10:19:23 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
system_mapping.clear()
|
2021-11-21 20:45:02 +00:00
|
|
|
system_mapping._set("a", KEY_A)
|
2021-02-07 14:00:36 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset(get_preset_path(group_name, preset_name))
|
|
|
|
preset.add(get_key_mapping(EventCombination([*ev, 1]), "keyboard", "a"))
|
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
# make the daemon load the file instead
|
2021-09-26 10:44:56 +00:00
|
|
|
with open(get_config_path("xmodmap.json"), "w") as file:
|
2021-02-07 14:00:36 +00:00
|
|
|
json.dump(system_mapping._mapping, file, indent=4)
|
|
|
|
system_mapping.clear()
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset.save()
|
|
|
|
global_config.set_autoload_preset(group_key, preset_name)
|
2020-12-24 00:26:34 +00:00
|
|
|
self.daemon = Daemon()
|
|
|
|
|
|
|
|
# make sure the devices are populated
|
2021-04-23 09:51:21 +00:00
|
|
|
groups.refresh()
|
|
|
|
|
|
|
|
# the daemon is supposed to find this device by calling refresh
|
|
|
|
fixtures[self.new_fixture_path] = {
|
2021-09-26 10:44:56 +00:00
|
|
|
"capabilities": {evdev.ecodes.EV_KEY: [ev[1]]},
|
|
|
|
"phys": "9876 phys",
|
|
|
|
"info": evdev.device.DeviceInfo(4, 5, 6, 7),
|
|
|
|
"name": group_name,
|
2020-12-24 00:26:34 +00:00
|
|
|
}
|
2022-08-28 10:38:40 +00:00
|
|
|
push_events(fixtures[self.new_fixture_path], [new_event(*ev, 1)])
|
2022-04-17 10:19:23 +00:00
|
|
|
self.daemon.start_injecting(group_key, preset_name)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
# test if the injector called groups.refresh successfully
|
|
|
|
group = groups.find(key=group_key)
|
|
|
|
self.assertEqual(group.name, group_name)
|
|
|
|
self.assertEqual(group.key, group_key)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
self.assertTrue(uinput_write_history_pipe[0].poll())
|
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
event = uinput_write_history_pipe[0].recv()
|
2021-11-21 20:45:02 +00:00
|
|
|
self.assertEqual(event.t, (EV_KEY, KEY_A, 1))
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.daemon.stop_injecting(group_key)
|
2022-07-23 08:53:41 +00:00
|
|
|
time.sleep(0.2)
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertEqual(self.daemon.get_state(group_key), InjectorState.STOPPED)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def test_refresh_for_unknown_key(self):
|
2021-09-26 10:44:56 +00:00
|
|
|
device = "9876 name"
|
2021-02-12 19:29:26 +00:00
|
|
|
# this test only makes sense if this device is unknown yet
|
2021-04-23 09:51:21 +00:00
|
|
|
self.assertIsNone(groups.find(name=device))
|
2021-02-12 19:29:26 +00:00
|
|
|
|
|
|
|
self.daemon = Daemon()
|
|
|
|
|
|
|
|
# make sure the devices are populated
|
2021-04-23 09:51:21 +00:00
|
|
|
groups.refresh()
|
2021-02-12 19:29:26 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
self.daemon.refresh()
|
2021-02-12 19:29:26 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
fixtures[self.new_fixture_path] = {
|
2021-09-26 10:44:56 +00:00
|
|
|
"capabilities": {evdev.ecodes.EV_KEY: [evdev.ecodes.KEY_A]},
|
|
|
|
"phys": "9876 phys",
|
|
|
|
"info": evdev.device.DeviceInfo(4, 5, 6, 7),
|
|
|
|
"name": device,
|
2021-02-12 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
self.daemon._autoload("25v7j9q4vtj")
|
2021-04-23 09:51:21 +00:00
|
|
|
# this is unknown, so the daemon will scan the devices again
|
2021-02-12 19:29:26 +00:00
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
# test if the injector called groups.refresh successfully
|
|
|
|
self.assertIsNotNone(groups.find(name=device))
|
2021-02-12 19:29:26 +00:00
|
|
|
|
2020-12-24 00:26:34 +00:00
|
|
|
def test_xmodmap_file(self):
|
|
|
|
from_keycode = evdev.ecodes.KEY_A
|
2022-01-14 17:50:57 +00:00
|
|
|
target = "keyboard"
|
2022-04-17 10:19:23 +00:00
|
|
|
to_name = "q"
|
2020-12-24 00:26:34 +00:00
|
|
|
to_keycode = 100
|
|
|
|
event = (EV_KEY, from_keycode, 1)
|
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
name = "Bar Device"
|
2022-04-17 10:19:23 +00:00
|
|
|
preset_name = "foo"
|
2021-04-23 09:51:21 +00:00
|
|
|
group = groups.find(name=name)
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
config_dir = os.path.join(tmp, "foo")
|
2021-02-07 14:00:36 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
path = os.path.join(config_dir, "presets", name, f"{preset_name}.json")
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset(path)
|
|
|
|
preset.add(get_key_mapping(EventCombination(event), target, to_name))
|
|
|
|
preset.save()
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
system_mapping.clear()
|
|
|
|
|
2022-08-28 10:38:40 +00:00
|
|
|
push_events(fixtures.bar_device, [new_event(*event)])
|
2020-12-24 00:26:34 +00:00
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
# an existing config file is needed otherwise set_config_dir refuses
|
|
|
|
# to use the directory
|
2021-09-26 10:44:56 +00:00
|
|
|
config_path = os.path.join(config_dir, "config.json")
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.path = config_path
|
|
|
|
global_config._save_config()
|
2021-01-04 19:50:05 +00:00
|
|
|
|
2021-09-26 10:44:56 +00:00
|
|
|
xmodmap_path = os.path.join(config_dir, "xmodmap.json")
|
|
|
|
with open(xmodmap_path, "w") as file:
|
2020-12-24 00:26:34 +00:00
|
|
|
file.write(f'{{"{to_name}":{to_keycode}}}')
|
|
|
|
|
|
|
|
self.daemon = Daemon()
|
2021-02-07 14:00:36 +00:00
|
|
|
self.daemon.set_config_dir(config_dir)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.daemon.start_injecting(group.key, preset_name)
|
2021-01-04 19:50:05 +00:00
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
self.assertTrue(uinput_write_history_pipe[0].poll())
|
2020-12-24 00:26:34 +00:00
|
|
|
|
|
|
|
event = uinput_write_history_pipe[0].recv()
|
|
|
|
self.assertEqual(event.type, EV_KEY)
|
|
|
|
self.assertEqual(event.code, to_keycode)
|
|
|
|
self.assertEqual(event.value, 1)
|
|
|
|
|
2021-02-07 14:00:36 +00:00
|
|
|
def test_start_stop(self):
|
2022-11-01 12:26:27 +00:00
|
|
|
group_key = "Qux/Device?"
|
|
|
|
group = groups.find(key=group_key)
|
2022-04-17 10:19:23 +00:00
|
|
|
preset_name = "preset8"
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
daemon = Daemon()
|
|
|
|
self.daemon = daemon
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
pereset = Preset(group.get_preset_path(preset_name))
|
2022-11-01 12:26:27 +00:00
|
|
|
pereset.add(
|
|
|
|
get_key_mapping(EventCombination((EV_KEY, KEY_A, 1)), "keyboard", "a")
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
pereset.save()
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# start
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.start_injecting(group_key, preset_name)
|
2021-02-07 14:00:36 +00:00
|
|
|
# explicit start, not autoload, so the history stays empty
|
2022-11-01 12:26:27 +00:00
|
|
|
self.assertNotIn(group_key, daemon.autoload_history._autoload_history)
|
|
|
|
self.assertTrue(daemon.autoload_history.may_autoload(group_key, preset_name))
|
2021-02-07 14:00:36 +00:00
|
|
|
# path got translated to the device name
|
2022-11-01 12:26:27 +00:00
|
|
|
self.assertIn(group_key, daemon.injectors)
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# start again
|
2022-11-01 12:26:27 +00:00
|
|
|
previous_injector = daemon.injectors[group_key]
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertNotEqual(previous_injector.get_state(), InjectorState.STOPPED)
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.start_injecting(group_key, preset_name)
|
|
|
|
self.assertNotIn(group_key, daemon.autoload_history._autoload_history)
|
|
|
|
self.assertTrue(daemon.autoload_history.may_autoload(group_key, preset_name))
|
|
|
|
self.assertIn(group_key, daemon.injectors)
|
2022-07-23 08:53:41 +00:00
|
|
|
time.sleep(0.2)
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertEqual(previous_injector.get_state(), InjectorState.STOPPED)
|
2021-02-07 14:00:36 +00:00
|
|
|
# a different injetor is now running
|
2022-11-01 12:26:27 +00:00
|
|
|
self.assertNotEqual(previous_injector, daemon.injectors[group_key])
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertNotEqual(
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.injectors[group_key].get_state(), InjectorState.STOPPED
|
2022-11-01 11:07:12 +00:00
|
|
|
)
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# trying to inject a non existing preset keeps the previous inejction
|
|
|
|
# alive
|
2022-11-01 12:26:27 +00:00
|
|
|
injector = daemon.injectors[group_key]
|
|
|
|
daemon.start_injecting(group_key, "qux")
|
|
|
|
self.assertEqual(injector, daemon.injectors[group_key])
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertNotEqual(
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.injectors[group_key].get_state(), InjectorState.STOPPED
|
2022-11-01 11:07:12 +00:00
|
|
|
)
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# trying to start injecting for an unknown device also just does
|
|
|
|
# nothing
|
2021-09-26 10:44:56 +00:00
|
|
|
daemon.start_injecting("quux", "qux")
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertNotEqual(
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.injectors[group_key].get_state(), InjectorState.STOPPED
|
2022-11-01 11:07:12 +00:00
|
|
|
)
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# after all that stuff autoload_history is still unharmed
|
2022-11-01 12:26:27 +00:00
|
|
|
self.assertNotIn(group_key, daemon.autoload_history._autoload_history)
|
|
|
|
self.assertTrue(daemon.autoload_history.may_autoload(group_key, preset_name))
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# stop
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.stop_injecting(group_key)
|
2022-07-23 08:53:41 +00:00
|
|
|
time.sleep(0.2)
|
2022-11-01 12:26:27 +00:00
|
|
|
self.assertNotIn(group_key, daemon.autoload_history._autoload_history)
|
|
|
|
self.assertEqual(daemon.injectors[group_key].get_state(), InjectorState.STOPPED)
|
|
|
|
self.assertTrue(daemon.autoload_history.may_autoload(group_key, preset_name))
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
def test_autoload(self):
|
2022-04-17 10:19:23 +00:00
|
|
|
preset_name = "preset7"
|
2022-11-01 12:26:27 +00:00
|
|
|
group_key = "Qux/Device?"
|
|
|
|
group = groups.find(key=group_key)
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
daemon = Daemon()
|
|
|
|
self.daemon = daemon
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset(group.get_preset_path(preset_name))
|
2022-11-01 12:26:27 +00:00
|
|
|
preset.add(
|
|
|
|
get_key_mapping(EventCombination((EV_KEY, KEY_A, 1)), "keyboard", "a")
|
|
|
|
)
|
2022-04-17 10:19:23 +00:00
|
|
|
preset.save()
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# no autoloading is configured yet
|
2022-11-01 12:26:27 +00:00
|
|
|
self.daemon._autoload(group_key)
|
|
|
|
self.assertNotIn(group_key, daemon.autoload_history._autoload_history)
|
|
|
|
self.assertTrue(daemon.autoload_history.may_autoload(group_key, preset_name))
|
2021-02-07 14:00:36 +00:00
|
|
|
|
2022-11-01 12:26:27 +00:00
|
|
|
global_config.set_autoload_preset(group_key, preset_name)
|
2021-02-12 19:29:26 +00:00
|
|
|
len_before = len(self.daemon.autoload_history._autoload_history)
|
2021-04-23 09:51:21 +00:00
|
|
|
# now autoloading is configured, so it will autoload
|
2022-11-01 12:26:27 +00:00
|
|
|
self.daemon._autoload(group_key)
|
2021-02-12 19:29:26 +00:00
|
|
|
len_after = len(self.daemon.autoload_history._autoload_history)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.autoload_history._autoload_history[group_key][1], preset_name
|
2021-09-26 10:44:56 +00:00
|
|
|
)
|
2022-11-01 12:26:27 +00:00
|
|
|
self.assertFalse(daemon.autoload_history.may_autoload(group_key, preset_name))
|
|
|
|
injector = daemon.injectors[group_key]
|
2021-02-12 19:29:26 +00:00
|
|
|
self.assertEqual(len_before + 1, len_after)
|
2021-02-07 14:00:36 +00:00
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
# calling duplicate get_autoload does nothing
|
2022-11-01 12:26:27 +00:00
|
|
|
self.daemon._autoload(group_key)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertEqual(
|
2022-11-01 12:26:27 +00:00
|
|
|
daemon.autoload_history._autoload_history[group_key][1], preset_name
|
2021-09-26 10:44:56 +00:00
|
|
|
)
|
2022-11-01 12:26:27 +00:00
|
|
|
self.assertEqual(injector, daemon.injectors[group_key])
|
|
|
|
self.assertFalse(daemon.autoload_history.may_autoload(group_key, preset_name))
|
2021-02-07 14:00:36 +00:00
|
|
|
|
|
|
|
# explicit start_injecting clears the autoload history
|
2022-11-01 12:26:27 +00:00
|
|
|
self.daemon.start_injecting(group_key, preset_name)
|
|
|
|
self.assertTrue(daemon.autoload_history.may_autoload(group_key, preset_name))
|
2021-01-04 19:50:05 +00:00
|
|
|
|
2021-02-12 19:29:26 +00:00
|
|
|
# calling autoload for (yet) unknown devices does nothing
|
|
|
|
len_before = len(self.daemon.autoload_history._autoload_history)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.daemon._autoload("unknown-key-1234")
|
2021-02-12 19:29:26 +00:00
|
|
|
len_after = len(self.daemon.autoload_history._autoload_history)
|
|
|
|
self.assertEqual(len_before, len_after)
|
|
|
|
|
2022-01-01 12:00:49 +00:00
|
|
|
# autoloading input-remapper devices does nothing
|
2021-02-12 19:29:26 +00:00
|
|
|
len_before = len(self.daemon.autoload_history._autoload_history)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.daemon.autoload_single("Bar Device")
|
2021-02-12 19:29:26 +00:00
|
|
|
len_after = len(self.daemon.autoload_history._autoload_history)
|
|
|
|
self.assertEqual(len_before, len_after)
|
|
|
|
|
2021-04-23 09:51:21 +00:00
|
|
|
def test_autoload_2(self):
|
|
|
|
self.daemon = Daemon()
|
|
|
|
history = self.daemon.autoload_history._autoload_history
|
|
|
|
|
|
|
|
# existing device
|
2022-04-17 10:19:23 +00:00
|
|
|
preset_name = "preset7"
|
2021-09-26 10:44:56 +00:00
|
|
|
group = groups.find(key="Foo Device 2")
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset(group.get_preset_path(preset_name))
|
|
|
|
preset.add(get_key_mapping(EventCombination([3, 2, 1]), "keyboard", "a"))
|
|
|
|
preset.save()
|
|
|
|
global_config.set_autoload_preset(group.key, preset_name)
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
# ignored, won't cause problems:
|
2022-01-31 19:58:37 +00:00
|
|
|
global_config.set_autoload_preset("non-existant-key", "foo")
|
2021-04-23 09:51:21 +00:00
|
|
|
|
|
|
|
self.daemon.autoload()
|
|
|
|
self.assertEqual(len(history), 1)
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(history[group.key][1], preset_name)
|
2021-04-23 09:51:21 +00:00
|
|
|
|
2021-04-29 20:22:05 +00:00
|
|
|
def test_autoload_3(self):
|
|
|
|
# based on a bug
|
2022-04-17 10:19:23 +00:00
|
|
|
preset_name = "preset7"
|
2021-09-26 10:44:56 +00:00
|
|
|
group = groups.find(key="Foo Device 2")
|
2021-04-29 20:22:05 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
preset = Preset(group.get_preset_path(preset_name))
|
|
|
|
preset.add(get_key_mapping(EventCombination([3, 2, 1]), "keyboard", "a"))
|
|
|
|
preset.save()
|
2021-04-29 20:22:05 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
global_config.set_autoload_preset(group.key, preset_name)
|
2021-04-29 20:22:05 +00:00
|
|
|
|
|
|
|
self.daemon = Daemon()
|
2021-04-29 20:24:38 +00:00
|
|
|
groups.set_groups([]) # caused the bug
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIsNone(groups.find(key="Foo Device 2"))
|
2021-04-29 20:22:05 +00:00
|
|
|
self.daemon.autoload()
|
|
|
|
|
2021-04-29 20:24:38 +00:00
|
|
|
# it should try to refresh the groups because all the
|
|
|
|
# group_keys are unknown at the moment
|
2021-04-29 20:22:05 +00:00
|
|
|
history = self.daemon.autoload_history._autoload_history
|
2022-04-17 10:19:23 +00:00
|
|
|
self.assertEqual(history[group.key][1], preset_name)
|
2022-11-01 11:07:12 +00:00
|
|
|
self.assertEqual(self.daemon.get_state(group.key), InjectorState.STARTING)
|
2021-09-26 10:44:56 +00:00
|
|
|
self.assertIsNotNone(groups.find(key="Foo Device 2"))
|
2021-04-29 20:22:05 +00:00
|
|
|
|
2020-11-20 20:38:59 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|