indicates if a mapping is going on

This commit is contained in:
sezanzeb 2020-12-02 18:07:46 +01:00
parent 386879752b
commit e18fbeaf60
4 changed files with 68 additions and 14 deletions

View File

@ -79,6 +79,10 @@ class Daemon:
<method name='stop_injecting'> <method name='stop_injecting'>
<arg type='s' name='device' direction='in'/> <arg type='s' name='device' direction='in'/>
</method> </method>
<method name='is_injecting'>
<arg type='s' name='device' direction='in'/>
<arg type='b' name='response' direction='out'/>
</method>
<method name='start_injecting'> <method name='start_injecting'>
<arg type='s' name='device' direction='in'/> <arg type='s' name='device' direction='in'/>
<arg type='s' name='preset' direction='in'/> <arg type='s' name='preset' direction='in'/>
@ -121,6 +125,11 @@ class Daemon:
return return
self.injectors[device].stop_injecting() self.injectors[device].stop_injecting()
del self.injectors[device]
def is_injecting(self, device):
"""Is this device being mapped?"""
return device in self.injectors
def start_injecting(self, device, preset): def start_injecting(self, device, preset):
"""Start injecting the preset for the device. """Start injecting the preset for the device.

View File

@ -241,9 +241,7 @@ class Window:
CTX_APPLY, CTX_APPLY,
'Applied the system default' 'Applied the system default'
) )
# restart reading because after injecting the device landscape GLib.timeout_add(10, self.show_device_mapping_status)
# changes a bit
keycode_reader.start_reading(self.selected_device)
def on_save_preset_clicked(self, button): def on_save_preset_clicked(self, button):
"""Save changes to a preset to the file system.""" """Save changes to a preset to the file system."""
@ -301,6 +299,7 @@ class Window:
# restart reading because after injecting the device landscape # restart reading because after injecting the device landscape
# changes a bit # changes a bit
keycode_reader.start_reading(self.selected_device) keycode_reader.start_reading(self.selected_device)
GLib.timeout_add(10, self.show_device_mapping_status)
def on_preset_autoload_switch_activate(self, _, active): def on_preset_autoload_switch_activate(self, _, active):
"""Load the preset automatically next time the user logs in.""" """Load the preset automatically next time the user logs in."""
@ -326,9 +325,17 @@ class Window:
self.selected_preset = None self.selected_preset = None
self.populate_presets() self.populate_presets()
GLib.idle_add( GLib.idle_add(lambda: keycode_reader.start_reading(device))
lambda: keycode_reader.start_reading(self.selected_device)
) self.show_device_mapping_status()
def show_device_mapping_status(self):
"""Figure out if this device is currently under keymappers control."""
if self.dbus.is_injecting(self.selected_device):
logger.info('This device is currently mapped.')
self.get('apply_system_layout').set_opacity(1)
else:
self.get('apply_system_layout').set_opacity(0.4)
def on_create_preset_clicked(self, _): def on_create_preset_clicked(self, _):
"""Create a new preset and select it.""" """Create a new preset and select it."""

View File

@ -79,25 +79,58 @@ class TestDaemon(unittest.TestCase):
config.clear_config() config.clear_config()
def test_daemon(self): def test_daemon(self):
keycode_from = 9 keycode_from_1 = 9
keycode_to = 100 keycode_to_1 = 100
keycode_from_2 = 12
keycode_to_2 = 100
custom_mapping.change(keycode_from, 'a') custom_mapping.change(keycode_from_1, 'a')
custom_mapping.change(keycode_from_2, 'b')
clear_system_mapping() clear_system_mapping()
system_mapping['a'] = keycode_to system_mapping['a'] = keycode_to_1
system_mapping['b'] = keycode_to_2
custom_mapping.save('device 2', 'foo') preset = 'foo'
config.set_autoload_preset('device 2', 'foo')
custom_mapping.save('device 2', preset)
config.set_autoload_preset('device 2', preset)
pending_events['device 2'] = [ pending_events['device 2'] = [
Event(evdev.events.EV_KEY, keycode_from - 8, 0) Event(evdev.events.EV_KEY, keycode_from_1 - 8, 0),
] ]
self.daemon = Daemon() self.daemon = Daemon()
# starts mapping right after creation
self.assertTrue(self.daemon.is_injecting('device 2'))
self.assertFalse(self.daemon.is_injecting('device 1'))
event = uinput_write_history_pipe[0].recv() event = uinput_write_history_pipe[0].recv()
self.assertEqual(event.type, evdev.events.EV_KEY) self.assertEqual(event.type, evdev.events.EV_KEY)
self.assertEqual(event.code, keycode_to - 8) self.assertEqual(event.code, keycode_to_1 - 8)
self.assertEqual(event.value, 0)
self.daemon.stop_injecting('device 2')
self.assertFalse(self.daemon.is_injecting('device 2'))
pending_events['device 2'] = [
Event(evdev.events.EV_KEY, keycode_from_2 - 8, 1),
Event(evdev.events.EV_KEY, keycode_from_2 - 8, 0),
]
time.sleep(0.2)
self.assertFalse(uinput_write_history_pipe[0].poll())
self.daemon.start_injecting('device 2', preset)
event = uinput_write_history_pipe[0].recv()
self.assertEqual(event.type, evdev.events.EV_KEY)
self.assertEqual(event.code, keycode_to_2 - 8)
self.assertEqual(event.value, 1)
event = uinput_write_history_pipe[0].recv()
self.assertEqual(event.type, evdev.events.EV_KEY)
self.assertEqual(event.code, keycode_to_2 - 8)
self.assertEqual(event.value, 0) self.assertEqual(event.value, 0)

View File

@ -112,6 +112,11 @@ class TestIntegration(unittest.TestCase):
def get_rows(self): def get_rows(self):
return self.window.get('key_list').get_children() return self.window.get('key_list').get_children()
def test_show_device_mapping_status(self):
# this function may not return True, otherwise the timeout
# runs forever
self.assertFalse(self.window.show_device_mapping_status())
def test_autoload(self): def test_autoload(self):
self.window.on_preset_autoload_switch_activate(None, False) self.window.on_preset_autoload_switch_activate(None, False)
self.assertFalse(config.is_autoloaded( self.assertFalse(config.is_autoloaded(