2020-11-18 23:04:04 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# key-mapper - GUI for device specific keyboard mappings
|
|
|
|
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
|
2020-12-02 16:40:21 +00:00
|
|
|
import asyncio
|
2020-11-18 23:04:04 +00:00
|
|
|
import unittest
|
2020-11-30 17:59:34 +00:00
|
|
|
import time
|
2020-11-18 23:04:04 +00:00
|
|
|
|
|
|
|
import evdev
|
2020-11-30 21:42:53 +00:00
|
|
|
from evdev.ecodes import EV_REL, EV_KEY, EV_ABS
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-11-28 14:43:24 +00:00
|
|
|
from keymapper.dev.injector import is_numlock_on, toggle_numlock,\
|
2020-11-30 15:22:17 +00:00
|
|
|
ensure_numlock, KeycodeInjector
|
2020-12-02 16:40:21 +00:00
|
|
|
from keymapper.dev.keycode_mapper import handle_keycode
|
2020-11-30 15:22:17 +00:00
|
|
|
from keymapper.state import custom_mapping, system_mapping, \
|
|
|
|
clear_system_mapping, KEYCODE_OFFSET
|
2020-11-28 14:43:24 +00:00
|
|
|
from keymapper.mapping import Mapping
|
2020-11-30 21:42:53 +00:00
|
|
|
from keymapper.config import config
|
2020-12-02 16:40:21 +00:00
|
|
|
from keymapper.dev.macros import parse
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-12-02 13:36:17 +00:00
|
|
|
from tests.test import uinput_write_history, Event, pending_events, fixtures, \
|
2020-11-30 21:42:53 +00:00
|
|
|
clear_write_history, EVENT_READ_TIMEOUT, uinput_write_history_pipe, \
|
|
|
|
MAX_ABS
|
2020-11-18 23:04:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestInjector(unittest.TestCase):
|
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
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.failed = 0
|
|
|
|
|
|
|
|
def grab_fail_twice(_):
|
|
|
|
if self.failed < 2:
|
|
|
|
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()
|
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-01 22:53:32 +00:00
|
|
|
keys = list(pending_events.keys())
|
|
|
|
for key in keys:
|
|
|
|
del pending_events[key]
|
2020-11-28 23:28:46 +00:00
|
|
|
clear_write_history()
|
2020-11-19 10:30:01 +00:00
|
|
|
|
|
|
|
def test_modify_capabilities(self):
|
|
|
|
class FakeDevice:
|
|
|
|
def capabilities(self, absinfo=True):
|
|
|
|
assert absinfo is False
|
|
|
|
return {
|
|
|
|
evdev.ecodes.EV_SYN: [1, 2, 3],
|
|
|
|
evdev.ecodes.EV_FF: [1, 2, 3]
|
|
|
|
}
|
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
mapping = Mapping()
|
|
|
|
mapping.change(
|
|
|
|
new_keycode=80,
|
|
|
|
character='a'
|
|
|
|
)
|
|
|
|
|
|
|
|
maps_to = system_mapping['a'] - KEYCODE_OFFSET
|
|
|
|
|
|
|
|
self.injector = KeycodeInjector('foo', mapping)
|
2020-12-01 22:53:32 +00:00
|
|
|
fake_device = FakeDevice()
|
|
|
|
capabilities = self.injector._modify_capabilities(
|
|
|
|
fake_device,
|
2020-12-01 23:02:41 +00:00
|
|
|
map_ev_abs=False
|
2020-12-01 22:53:32 +00:00
|
|
|
)
|
2020-11-19 10:30:01 +00:00
|
|
|
|
2020-11-30 21:42:53 +00:00
|
|
|
self.assertIn(EV_KEY, capabilities)
|
|
|
|
keys = capabilities[EV_KEY]
|
2020-11-30 17:59:34 +00:00
|
|
|
self.assertEqual(keys[0], maps_to)
|
2020-11-19 10:30:01 +00:00
|
|
|
|
|
|
|
self.assertNotIn(evdev.ecodes.EV_SYN, capabilities)
|
|
|
|
self.assertNotIn(evdev.ecodes.EV_FF, capabilities)
|
|
|
|
|
|
|
|
def test_grab(self):
|
|
|
|
# path is from the fixtures
|
2020-11-28 14:43:24 +00:00
|
|
|
custom_mapping.change(10, 'a')
|
|
|
|
|
|
|
|
self.injector = KeycodeInjector('device 1', custom_mapping)
|
|
|
|
path = '/dev/input/event10'
|
|
|
|
# this test needs to pass around all other constraints of
|
|
|
|
# _prepare_device
|
2020-12-01 23:02:41 +00:00
|
|
|
device, map_ev_abs = self.injector._prepare_device(path)
|
|
|
|
self.assertFalse(map_ev_abs)
|
2020-11-19 10:30:01 +00:00
|
|
|
self.assertEqual(self.failed, 2)
|
|
|
|
# success on the third try
|
|
|
|
device.name = fixtures[path]['name']
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
def test_gamepad_capabilities(self):
|
|
|
|
self.injector = KeycodeInjector('gamepad', custom_mapping)
|
|
|
|
|
|
|
|
path = '/dev/input/event30'
|
2020-12-01 23:02:41 +00:00
|
|
|
device, map_ev_abs = self.injector._prepare_device(path)
|
|
|
|
self.assertTrue(map_ev_abs)
|
2020-12-01 22:53:32 +00:00
|
|
|
|
2020-12-01 23:02:41 +00:00
|
|
|
capabilities = self.injector._modify_capabilities(device, map_ev_abs)
|
2020-12-01 22:53:32 +00:00
|
|
|
self.assertNotIn(evdev.ecodes.EV_ABS, capabilities)
|
|
|
|
self.assertIn(evdev.ecodes.EV_REL, capabilities)
|
|
|
|
|
2020-11-28 14:43:24 +00:00
|
|
|
def test_skip_unused_device(self):
|
|
|
|
# skips a device because its capabilities are not used in the mapping
|
|
|
|
custom_mapping.change(10, 'a')
|
|
|
|
self.injector = KeycodeInjector('device 1', custom_mapping)
|
|
|
|
path = '/dev/input/event11'
|
2020-12-01 23:02:41 +00:00
|
|
|
device, map_ev_abs = self.injector._prepare_device(path)
|
|
|
|
self.assertFalse(map_ev_abs)
|
2020-11-28 14:43:24 +00:00
|
|
|
self.assertEqual(self.failed, 0)
|
|
|
|
self.assertIsNone(device)
|
|
|
|
|
|
|
|
def test_skip_unknown_device(self):
|
|
|
|
# skips a device because its capabilities are not used in the mapping
|
|
|
|
self.injector = KeycodeInjector('device 1', custom_mapping)
|
|
|
|
path = '/dev/input/event11'
|
2020-12-01 22:53:32 +00:00
|
|
|
device, _ = self.injector._prepare_device(path)
|
2020-11-28 14:43:24 +00:00
|
|
|
|
2020-11-30 21:42:53 +00:00
|
|
|
# make sure the test uses a fixture without interesting capabilities
|
2020-11-28 14:43:24 +00:00
|
|
|
capabilities = evdev.InputDevice(path).capabilities()
|
2020-11-30 21:42:53 +00:00
|
|
|
self.assertEqual(len(capabilities.get(EV_KEY, [])), 0)
|
|
|
|
self.assertEqual(len(capabilities.get(EV_ABS, [])), 0)
|
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)
|
|
|
|
|
2020-11-19 10:12:28 +00:00
|
|
|
def test_numlock(self):
|
|
|
|
before = is_numlock_on()
|
|
|
|
|
|
|
|
toggle_numlock() # should change
|
|
|
|
self.assertEqual(not before, is_numlock_on())
|
|
|
|
|
|
|
|
@ensure_numlock
|
|
|
|
def wrapped():
|
|
|
|
toggle_numlock()
|
|
|
|
|
|
|
|
wrapped() # should not change
|
|
|
|
self.assertEqual(not before, is_numlock_on())
|
|
|
|
|
|
|
|
# toggle one more time to restore the previous configuration
|
|
|
|
toggle_numlock()
|
|
|
|
self.assertEqual(before, is_numlock_on())
|
|
|
|
|
2020-11-30 20:16:58 +00:00
|
|
|
def test_abs_to_rel(self):
|
|
|
|
# maps gamepad joystick events to mouse events
|
2020-12-01 23:02:41 +00:00
|
|
|
# TODO enable this somewhere so that map_ev_abs returns true
|
2020-11-30 21:42:53 +00:00
|
|
|
# in the .json file of the mapping.
|
|
|
|
config.set('gamepad.non_linearity', 1)
|
|
|
|
pointer_speed = 80
|
|
|
|
config.set('gamepad.pointer_speed', pointer_speed)
|
|
|
|
|
|
|
|
# same for ABS, 0 for x, 1 for y
|
|
|
|
rel_x = evdev.ecodes.REL_X
|
|
|
|
rel_y = evdev.ecodes.REL_Y
|
|
|
|
|
|
|
|
# they need to sum up before something is written
|
|
|
|
divisor = 10
|
|
|
|
x = MAX_ABS / pointer_speed / divisor
|
|
|
|
y = MAX_ABS / pointer_speed / divisor
|
|
|
|
pending_events['gamepad'] = [
|
|
|
|
Event(EV_ABS, rel_x, x),
|
|
|
|
Event(EV_ABS, rel_y, y),
|
|
|
|
Event(EV_ABS, rel_x, -x),
|
|
|
|
Event(EV_ABS, rel_y, -y),
|
|
|
|
]
|
|
|
|
|
|
|
|
self.injector = KeycodeInjector('gamepad', custom_mapping)
|
|
|
|
self.injector.start_injecting()
|
|
|
|
|
|
|
|
# wait for the injector to start sending, at most 1s
|
|
|
|
uinput_write_history_pipe[0].poll(1)
|
|
|
|
|
|
|
|
# wait a bit more for it to sum up
|
|
|
|
sleep = 0.5
|
|
|
|
time.sleep(sleep)
|
|
|
|
|
|
|
|
# convert the write history to some easier to manage list
|
|
|
|
history = []
|
|
|
|
while uinput_write_history_pipe[0].poll():
|
|
|
|
event = uinput_write_history_pipe[0].recv()
|
|
|
|
history.append((event.type, event.code, event.value))
|
|
|
|
|
2020-12-01 22:53:32 +00:00
|
|
|
if history[0][0] == EV_ABS:
|
|
|
|
raise AssertionError(
|
|
|
|
'The injector probably just forwarded them unchanged'
|
|
|
|
)
|
|
|
|
|
2020-11-30 21:42:53 +00:00
|
|
|
# movement is written at 60hz and it takes `divisor` steps to
|
|
|
|
# move 1px. take it times 2 for both x and y events.
|
|
|
|
self.assertGreater(len(history), 60 * sleep * 0.9 * 2 / divisor)
|
|
|
|
self.assertLess(len(history), 60 * sleep * 1.1 * 2 / divisor)
|
|
|
|
|
|
|
|
# those may be in arbitrary order, the injector happens to write
|
|
|
|
# y first
|
|
|
|
self.assertEqual(history[-1][0], EV_REL)
|
|
|
|
self.assertEqual(history[-1][1], rel_x)
|
|
|
|
self.assertAlmostEqual(history[-1][2], -1)
|
|
|
|
self.assertEqual(history[-2][0], EV_REL)
|
|
|
|
self.assertEqual(history[-2][1], rel_y)
|
|
|
|
self.assertAlmostEqual(history[-2][2], -1)
|
2020-11-30 20:16:58 +00:00
|
|
|
|
2020-12-02 16:40:21 +00:00
|
|
|
def test_handle_keycode(self):
|
|
|
|
code_code_mapping = {
|
|
|
|
1: 101,
|
|
|
|
2: 102
|
|
|
|
}
|
|
|
|
|
|
|
|
history = []
|
|
|
|
|
|
|
|
class UInput:
|
|
|
|
def write(self, type, code, value):
|
|
|
|
history.append((type, code, value))
|
|
|
|
|
|
|
|
def syn(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
uinput = UInput()
|
|
|
|
|
|
|
|
EV_KEY = evdev.ecodes.EV_KEY
|
|
|
|
|
|
|
|
handle_keycode(code_code_mapping, {}, Event(EV_KEY, 1, 1), uinput)
|
|
|
|
handle_keycode(code_code_mapping, {}, Event(EV_KEY, 3, 1), uinput)
|
|
|
|
handle_keycode(code_code_mapping, {}, Event(EV_KEY, 2, 1), uinput)
|
|
|
|
|
|
|
|
self.assertEqual(len(history), 3)
|
|
|
|
self.assertEqual(history[0], (EV_KEY, 101, 1))
|
|
|
|
self.assertEqual(history[1], (EV_KEY, 3, 1))
|
|
|
|
self.assertEqual(history[2], (EV_KEY, 102, 1))
|
|
|
|
|
|
|
|
def test_handle_keycode_macro(self):
|
|
|
|
history = []
|
|
|
|
|
|
|
|
macro_mapping = {
|
|
|
|
1: parse('k(a)', lambda *args: history.append(args)),
|
|
|
|
2: parse('r(5, k(b))', lambda *args: history.append(args))
|
|
|
|
}
|
|
|
|
|
|
|
|
code_a = 100
|
|
|
|
code_b = 101
|
|
|
|
system_mapping['a'] = code_a
|
|
|
|
system_mapping['b'] = code_b
|
|
|
|
clear_system_mapping()
|
|
|
|
|
|
|
|
EV_KEY = evdev.ecodes.EV_KEY
|
|
|
|
|
|
|
|
handle_keycode({}, macro_mapping, Event(EV_KEY, 1, 1), None)
|
|
|
|
handle_keycode({}, macro_mapping, Event(EV_KEY, 2, 1), None)
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
sleeptime = config.get('macros.keystroke_sleep_ms', 10) * 12
|
|
|
|
|
|
|
|
async def sleep():
|
|
|
|
await asyncio.sleep(sleeptime / 1000 + 0.1)
|
|
|
|
|
|
|
|
loop.run_until_complete(sleep())
|
|
|
|
|
|
|
|
# 6 keycodes written, with down and up events
|
|
|
|
self.assertEqual(len(history), 12)
|
|
|
|
self.assertIn(('a', 1), history)
|
|
|
|
self.assertIn(('a', 0), history)
|
|
|
|
self.assertIn(('b', 1), history)
|
|
|
|
self.assertIn(('b', 0), history)
|
|
|
|
|
2020-11-18 23:04:04 +00:00
|
|
|
def test_injector(self):
|
2020-12-01 23:58:48 +00:00
|
|
|
custom_mapping.change(8, 'k(KEY_Q).k(w)')
|
2020-11-18 23:04:04 +00:00
|
|
|
custom_mapping.change(9, 'a')
|
|
|
|
# one mapping that is unknown in the system_mapping on purpose
|
2020-12-02 16:40:21 +00:00
|
|
|
input_b = 10
|
|
|
|
custom_mapping.change(input_b, 'b')
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-11-30 15:22:17 +00:00
|
|
|
clear_system_mapping()
|
2020-11-28 23:13:13 +00:00
|
|
|
code_a = 100
|
|
|
|
code_q = 101
|
|
|
|
code_w = 102
|
2020-11-30 15:22:17 +00:00
|
|
|
system_mapping['a'] = code_a
|
2020-12-01 23:58:48 +00:00
|
|
|
system_mapping['KEY_Q'] = code_q
|
2020-11-30 15:22:17 +00:00
|
|
|
system_mapping['w'] = code_w
|
2020-11-18 23:04:04 +00:00
|
|
|
|
|
|
|
# the second arg of those event objects is 8 lower than the
|
|
|
|
# keycode used in X and in the mappings
|
|
|
|
pending_events['device 2'] = [
|
2020-11-28 23:13:13 +00:00
|
|
|
# should execute a macro
|
2020-11-30 21:42:53 +00:00
|
|
|
Event(EV_KEY, 0, 1),
|
|
|
|
Event(EV_KEY, 0, 0),
|
2020-11-28 23:13:13 +00:00
|
|
|
# normal keystroke
|
2020-11-30 21:42:53 +00:00
|
|
|
Event(EV_KEY, 1, 1),
|
|
|
|
Event(EV_KEY, 1, 0),
|
2020-12-02 16:40:21 +00:00
|
|
|
# just pass those over without modifying
|
2020-11-30 21:42:53 +00:00
|
|
|
Event(EV_KEY, 2, 1),
|
|
|
|
Event(EV_KEY, 2, 0),
|
2020-11-18 23:04:04 +00:00
|
|
|
Event(3124, 3564, 6542),
|
|
|
|
]
|
|
|
|
|
2020-11-28 14:43:24 +00:00
|
|
|
self.injector = KeycodeInjector('device 2', custom_mapping)
|
2020-11-30 17:59:34 +00:00
|
|
|
self.injector.start_injecting()
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-11-30 17:59:34 +00:00
|
|
|
uinput_write_history_pipe[0].poll(timeout=1)
|
|
|
|
time.sleep(EVENT_READ_TIMEOUT * 10)
|
2020-11-18 23:04:04 +00:00
|
|
|
|
2020-11-28 23:13:13 +00:00
|
|
|
# convert the write history to some easier to manage list
|
2020-11-30 17:59:34 +00:00
|
|
|
history = []
|
|
|
|
while uinput_write_history_pipe[0].poll():
|
|
|
|
event = uinput_write_history_pipe[0].recv()
|
|
|
|
history.append((event.type, event.code, event.value))
|
|
|
|
|
2020-11-30 19:57:09 +00:00
|
|
|
# 4 events for the macro
|
2020-12-02 16:40:21 +00:00
|
|
|
# 2 for mapped keys
|
|
|
|
# 3 for forwarded events
|
|
|
|
self.assertEqual(len(history), 9)
|
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.
|
2020-11-30 21:42:53 +00:00
|
|
|
ev_key = EV_KEY
|
2020-11-28 23:13:13 +00:00
|
|
|
self.assertIn((ev_key, code_q - KEYCODE_OFFSET, 1), history)
|
|
|
|
self.assertIn((ev_key, code_q - KEYCODE_OFFSET, 0), history)
|
|
|
|
self.assertIn((ev_key, code_w - KEYCODE_OFFSET, 1), history)
|
|
|
|
self.assertIn((ev_key, code_w - KEYCODE_OFFSET, 0), history)
|
|
|
|
index_q_1 = history.index((ev_key, code_q - KEYCODE_OFFSET, 1))
|
|
|
|
index_q_0 = history.index((ev_key, code_q - KEYCODE_OFFSET, 0))
|
|
|
|
index_w_1 = history.index((ev_key, code_w - KEYCODE_OFFSET, 1))
|
|
|
|
index_w_0 = history.index((ev_key, code_w - KEYCODE_OFFSET, 0))
|
|
|
|
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_q_1]
|
|
|
|
index_q_0 = history.index((ev_key, code_q - KEYCODE_OFFSET, 0))
|
|
|
|
del history[index_q_0]
|
|
|
|
index_w_1 = history.index((ev_key, code_w - KEYCODE_OFFSET, 1))
|
|
|
|
del history[index_w_1]
|
|
|
|
index_w_0 = history.index((ev_key, code_w - KEYCODE_OFFSET, 0))
|
|
|
|
del history[index_w_0]
|
|
|
|
|
|
|
|
# the rest should be in order.
|
|
|
|
self.assertEqual(history[0], (ev_key, code_a - KEYCODE_OFFSET, 1))
|
|
|
|
self.assertEqual(history[1], (ev_key, code_a - KEYCODE_OFFSET, 0))
|
2020-12-02 16:40:21 +00:00
|
|
|
self.assertEqual(history[2], (ev_key, input_b - KEYCODE_OFFSET, 1))
|
|
|
|
self.assertEqual(history[3], (ev_key, input_b - KEYCODE_OFFSET, 0))
|
|
|
|
self.assertEqual(history[4], (3124, 3564, 6542))
|
2020-11-18 23:04:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|