def test_autoload_other_path(self): devices = ['device 1', 'device 2'] presets = ['bar123', 'bar2'] config_dir = os.path.join(tmp, 'qux', 'quux') paths = [ os.path.join(config_dir, 'presets', devices[0], presets[0] + '.json'), os.path.join(config_dir, 'presets', devices[1], presets[1] + '.json') ] Mapping().save(paths[0]) Mapping().save(paths[1]) daemon = Daemon() start_history = [] daemon.start_injecting = lambda *args: start_history.append(args) config.path = os.path.join(config_dir, 'config.json') config.load_config() config.set_autoload_preset(devices[0], presets[0]) config.set_autoload_preset(devices[1], presets[1]) config.save_config() control( options('autoload', config_dir, None, None, False, False, False), daemon) self.assertEqual(len(start_history), 2) self.assertEqual(start_history[0], (devices[0], presets[0])) self.assertEqual(start_history[1], (devices[1], presets[1]))
def test_save_load(self): self.assertEqual(len(config.iterate_autoload_presets()), 0) config.load_config() self.assertEqual(len(config.iterate_autoload_presets()), 0) config.set_autoload_preset('d1', 'a') config.set_autoload_preset('d2.foo', 'b') config.save_config() # ignored after load config.set_autoload_preset('d3', 'c') config.load_config() self.assertListEqual(list(config.iterate_autoload_presets()), [('d1', 'a'), ('d2.foo', 'b')]) config_2 = os.path.join(tmp, 'config_2.json') touch(config_2) with open(config_2, 'w') as f: f.write('{"a":"b"}') config.load_config(config_2) self.assertEqual(config.get("a"), "b") self.assertEqual(config.get(["a"]), "b")
def test_autoload_other_path(self): device_names = ['Foo Device', 'Bar Device'] groups_ = [groups.find(name=name) for name in device_names] presets = ['bar123', 'bar2'] config_dir = os.path.join(tmp, 'qux', 'quux') paths = [ os.path.join(config_dir, 'presets', device_names[0], presets[0] + '.json'), os.path.join(config_dir, 'presets', device_names[1], presets[1] + '.json') ] Mapping().save(paths[0]) Mapping().save(paths[1]) daemon = Daemon() start_history = [] daemon.start_injecting = lambda *args: start_history.append(args) config.path = os.path.join(config_dir, 'config.json') config.load_config() config.set_autoload_preset(device_names[0], presets[0]) config.set_autoload_preset(device_names[1], presets[1]) config.save_config() communicate( options('autoload', config_dir, None, None, False, False, False), daemon) self.assertEqual(len(start_history), 2) self.assertEqual(start_history[0], (groups_[0].key, presets[0])) self.assertEqual(start_history[1], (groups_[1].key, presets[1]))
def on_autoload_switch(self, _, active): """Load the preset automatically next time the user logs in.""" key = self.group.key preset = self.preset_name config.set_autoload_preset(key, preset if active else None) config.save_config() # tell the service to refresh its config self.dbus.set_config_dir(get_config_path())
def on_autoload_switch(self, _, active): """Load the preset automatically next time the user logs in.""" device = self.selected_device preset = self.selected_preset config.set_autoload_preset(device, preset if active else None) config.save_config() # tell the service to refresh its config self.dbus.set_config_dir(get_config_path())
def test_autoload(self): preset = 'preset7' group = groups.find(key='Foo Device 2') daemon = Daemon() self.daemon = daemon self.daemon.set_config_dir(get_config_path()) mapping = Mapping() mapping.change(Key(3, 2, 1), 'a') mapping.save(group.get_preset_path(preset)) # no autoloading is configured yet self.daemon._autoload(group.key) self.assertNotIn(group.key, daemon.autoload_history._autoload_history) self.assertTrue(daemon.autoload_history.may_autoload( group.key, preset)) config.set_autoload_preset(group.key, preset) config.save_config() self.daemon.set_config_dir(get_config_path()) len_before = len(self.daemon.autoload_history._autoload_history) # now autoloading is configured, so it will autoload self.daemon._autoload(group.key) len_after = len(self.daemon.autoload_history._autoload_history) self.assertEqual( daemon.autoload_history._autoload_history[group.key][1], preset) self.assertFalse( daemon.autoload_history.may_autoload(group.key, preset)) injector = daemon.injectors[group.key] self.assertEqual(len_before + 1, len_after) # calling duplicate _autoload does nothing self.daemon._autoload(group.key) self.assertEqual( daemon.autoload_history._autoload_history[group.key][1], preset) self.assertEqual(injector, daemon.injectors[group.key]) self.assertFalse( daemon.autoload_history.may_autoload(group.key, preset)) # explicit start_injecting clears the autoload history self.daemon.start_injecting(group.key, preset) self.assertTrue(daemon.autoload_history.may_autoload( group.key, preset)) # calling autoload for (yet) unknown devices does nothing len_before = len(self.daemon.autoload_history._autoload_history) self.daemon._autoload('unknown-key-1234') len_after = len(self.daemon.autoload_history._autoload_history) self.assertEqual(len_before, len_after) # autoloading key-mapper devices does nothing len_before = len(self.daemon.autoload_history._autoload_history) self.daemon.autoload_single('Bar Device') len_after = len(self.daemon.autoload_history._autoload_history) self.assertEqual(len_before, len_after)
def test_autoload(self): device = 'device 1' preset = 'preset7' path = '/dev/input/event11' daemon = Daemon() self.daemon = daemon self.daemon.set_config_dir(get_config_path()) mapping = Mapping() mapping.change(Key(3, 2, 1), 'a') mapping.save(get_preset_path(device, preset)) # no autoloading is configured yet self.daemon._autoload(device) self.daemon._autoload(path) self.assertNotIn(device, daemon.autoload_history._autoload_history) self.assertTrue(daemon.autoload_history.may_autoload(device, preset)) config.set_autoload_preset(device, preset) config.save_config() self.daemon.set_config_dir(get_config_path()) len_before = len(self.daemon.autoload_history._autoload_history) self.daemon._autoload(path) len_after = len(self.daemon.autoload_history._autoload_history) self.assertEqual(daemon.autoload_history._autoload_history[device][1], preset) self.assertFalse(daemon.autoload_history.may_autoload(device, preset)) injector = daemon.injectors[device] self.assertEqual(len_before + 1, len_after) # calling duplicate _autoload does nothing self.daemon._autoload(path) self.assertEqual(daemon.autoload_history._autoload_history[device][1], preset) self.assertEqual(injector, daemon.injectors[device]) self.assertFalse(daemon.autoload_history.may_autoload(device, preset)) # explicit start_injecting clears the autoload history self.daemon.start_injecting(device, preset) self.assertTrue(daemon.autoload_history.may_autoload(device, preset)) # calling autoload for (yet) unknown devices does nothing len_before = len(self.daemon.autoload_history._autoload_history) self.daemon._autoload('/dev/input/qux') len_after = len(self.daemon.autoload_history._autoload_history) self.assertEqual(len_before, len_after) # autoloading key-mapper devices does nothing len_before = len(self.daemon.autoload_history._autoload_history) self.daemon.autoload_single('/dev/input/event40') len_after = len(self.daemon.autoload_history._autoload_history) self.assertEqual(len_before, len_after)
def test_save_load(self): self.assertEqual(len(config.iterate_autoload_presets()), 0) config.load_config() self.assertEqual(len(config.iterate_autoload_presets()), 0) config.set_autoload_preset('d1', 'a') config.set_autoload_preset('d2', 'b') config.save_config() # ignored after load config.set_autoload_preset('d3', 'c') config.load_config() self.assertListEqual(list(config.iterate_autoload_presets()), [('d1', 'a'), ('d2', 'b')])
def test_xmodmap_file(self): from_keycode = evdev.ecodes.KEY_A to_name = 'qux' to_keycode = 100 event = (EV_KEY, from_keycode, 1) device = 'device 2' preset = 'foo' config_dir = os.path.join(tmp, 'foo') path = os.path.join(config_dir, 'presets', device, f'{preset}.json') custom_mapping.change(Key(event), to_name) custom_mapping.save(path) system_mapping.clear() push_events(device, [ new_event(*event) ]) # an existing config file is needed otherwise set_config_dir refuses # to use the directory config_path = os.path.join(config_dir, 'config.json') config.path = config_path config.save_config() xmodmap_path = os.path.join(config_dir, 'xmodmap.json') with open(xmodmap_path, 'w') as file: file.write(f'{{"{to_name}":{to_keycode}}}') self.daemon = Daemon() self.daemon.set_config_dir(config_dir) self.daemon.start_injecting(device, preset) time.sleep(0.1) self.assertTrue(uinput_write_history_pipe[0].poll()) event = uinput_write_history_pipe[0].recv() self.assertEqual(event.type, EV_KEY) self.assertEqual(event.code, to_keycode) self.assertEqual(event.value, 1)
def cleanup(): """Reset the applications state.""" keycode_reader.stop_reading() keycode_reader.clear() keycode_reader.newest_event = None keycode_reader._unreleased = {} for task in asyncio.Task.all_tasks(): task.cancel() os.system('pkill -f key-mapper-service') time.sleep(0.05) if os.path.exists(tmp): shutil.rmtree(tmp) config.clear_config() config.save_config() system_mapping.populate() custom_mapping.empty() custom_mapping.clear_config() clear_write_history() for key in list(active_macros.keys()): del active_macros[key] for key in list(unreleased.keys()): del unreleased[key] for key in list(pending_events.keys()): del pending_events[key] for path in list(fixtures.keys()): if path not in _fixture_copy: del fixtures[path] for path in list(_fixture_copy.keys()): if path not in fixtures: fixtures[path] = _fixture_copy[path] refresh_devices()
def _autoload(self, device): """Check if autoloading is a good idea, and if so do it. Parameters ---------- device : str Device name. Expects a key that is present in get_devices(). Can also be a path starting with /dev/input/ """ self.refresh_devices(device) device = path_to_device_name(device) if device not in get_devices(): # even after refresh_devices, the device is not in # get_devices(), so it's either not relevant for key-mapper, # or not connected yet return preset = config.get(['autoload', device], log_unknown=False) if preset is None: # no autoloading is configured for this device return if not isinstance(preset, str): # might be broken due to a previous bug config.remove(['autoload', device]) config.save_config() return logger.info('Autoloading "%s"', device) if not self.autoload_history.may_autoload(device, preset): logger.info( 'Not autoloading the same preset "%s" again for device "%s"', preset, device) return self.start_injecting(device, preset) self.autoload_history.remember(device, preset)
def _autoload(self, group_key): """Check if autoloading is a good idea, and if so do it. Parameters ---------- group_key : str unique identifier used by the groups object """ self.refresh(group_key) group = groups.find(key=group_key) if group is None: # even after groups.refresh, the device is unknown, so it's # either not relevant for key-mapper, or not connected yet return preset = config.get(['autoload', group.key], log_unknown=False) if preset is None: # no autoloading is configured for this device return if not isinstance(preset, str): # might be broken due to a previous bug config.remove(['autoload', group.key]) config.save_config() return logger.info('Autoloading for "%s"', group.key) if not self.autoload_history.may_autoload(group.key, preset): logger.info( 'Not autoloading the same preset "%s" again for group "%s"', preset, group.key) return self.start_injecting(group.key, preset) self.autoload_history.remember(group.key, preset)
def test_autoload_3(self): # based on a bug preset = 'preset7' group = groups.find(key='Foo Device 2') mapping = Mapping() mapping.change(Key(3, 2, 1), 'a') mapping.save(group.get_preset_path(preset)) config.set_autoload_preset(group.key, preset) config.save_config() self.daemon = Daemon() self.daemon.set_config_dir(get_config_path()) groups.set_groups([]) # caused the bug self.assertIsNone(groups.find(key='Foo Device 2')) self.daemon.autoload() # it should try to refresh the groups because all the # group_keys are unknown at the moment history = self.daemon.autoload_history._autoload_history self.assertEqual(history[group.key][1], preset) self.assertEqual(self.daemon.get_state(group.key), STARTING) self.assertIsNotNone(groups.find(key='Foo Device 2'))
def test_autoload_2(self): self.daemon = Daemon() history = self.daemon.autoload_history._autoload_history # existing device preset = 'preset7' group = groups.find(key='Foo Device 2') mapping = Mapping() mapping.change(Key(3, 2, 1), 'a') mapping.save(group.get_preset_path(preset)) config.set_autoload_preset(group.key, preset) # ignored, won't cause problems: config.set_autoload_preset('non-existant-key', 'foo') # daemon is missing the config directory yet self.daemon.autoload() self.assertEqual(len(history), 0) config.save_config() self.daemon.set_config_dir(get_config_path()) self.daemon.autoload() self.assertEqual(len(history), 1) self.assertEqual(history[group.key][1], preset)
def on_autoload_switch(self, _, active): """Load the preset automatically next time the user logs in.""" device = self.selected_device preset = self.selected_preset config.set_autoload_preset(device, preset if active else None) config.save_config()
def setUp(self): self.grab = evdev.InputDevice.grab self.daemon = None mkdir(get_config_path()) config.save_config()
def test_start_stop(self): group = groups.find(key='Foo Device 2') preset = 'preset8' daemon = Daemon() self.daemon = daemon mapping = Mapping() mapping.change(Key(3, 2, 1), 'a') mapping.save(group.get_preset_path(preset)) # the daemon needs set_config_dir first before doing anything daemon.start_injecting(group.key, preset) self.assertNotIn(group.key, daemon.autoload_history._autoload_history) self.assertNotIn(group.key, daemon.injectors) self.assertTrue(daemon.autoload_history.may_autoload( group.key, preset)) # start config.save_config() daemon.set_config_dir(get_config_path()) daemon.start_injecting(group.key, preset) # explicit start, not autoload, so the history stays empty self.assertNotIn(group.key, daemon.autoload_history._autoload_history) self.assertTrue(daemon.autoload_history.may_autoload( group.key, preset)) # path got translated to the device name self.assertIn(group.key, daemon.injectors) # start again previous_injector = daemon.injectors[group.key] self.assertNotEqual(previous_injector.get_state(), STOPPED) daemon.start_injecting(group.key, preset) self.assertNotIn(group.key, daemon.autoload_history._autoload_history) self.assertTrue(daemon.autoload_history.may_autoload( group.key, preset)) self.assertIn(group.key, daemon.injectors) self.assertEqual(previous_injector.get_state(), STOPPED) # a different injetor is now running self.assertNotEqual(previous_injector, daemon.injectors[group.key]) self.assertNotEqual(daemon.injectors[group.key].get_state(), STOPPED) # trying to inject a non existing preset keeps the previous inejction # alive injector = daemon.injectors[group.key] daemon.start_injecting(group.key, 'qux') self.assertEqual(injector, daemon.injectors[group.key]) self.assertNotEqual(daemon.injectors[group.key].get_state(), STOPPED) # trying to start injecting for an unknown device also just does # nothing daemon.start_injecting('quux', 'qux') self.assertNotEqual(daemon.injectors[group.key].get_state(), STOPPED) # after all that stuff autoload_history is still unharmed self.assertNotIn(group.key, daemon.autoload_history._autoload_history) self.assertTrue(daemon.autoload_history.may_autoload( group.key, preset)) # stop daemon.stop_injecting(group.key) self.assertNotIn(group.key, daemon.autoload_history._autoload_history) self.assertEqual(daemon.injectors[group.key].get_state(), STOPPED) self.assertTrue(daemon.autoload_history.may_autoload( group.key, preset))
def quick_cleanup(log=True): """Reset the applications state.""" if log: print('quick cleanup') for device in list(pending_events.keys()): try: while pending_events[device][1].poll(): pending_events[device][1].recv() except (UnpicklingError, EOFError): pass # setup new pipes for the next test pending_events[device] = None setup_pipe(device) try: reader.terminate() except (BrokenPipeError, OSError): pass if asyncio.get_event_loop().is_running(): for task in asyncio.all_tasks(): task.cancel() if not macro_variables.process.is_alive(): raise AssertionError('the SharedDict manager is not running anymore') macro_variables._stop() join_children() macro_variables._start() if os.path.exists(tmp): shutil.rmtree(tmp) config.path = os.path.join(get_config_path(), 'config.json') config.clear_config() config.save_config() system_mapping.populate() custom_mapping.empty() custom_mapping.clear_config() custom_mapping.changed = False clear_write_history() for name in list(uinputs.keys()): del uinputs[name] for device in list(active_macros.keys()): del active_macros[device] for device in list(unreleased.keys()): del unreleased[device] for path in list(fixtures.keys()): if path not in _fixture_copy: del fixtures[path] for path in list(_fixture_copy.keys()): fixtures[path] = copy.deepcopy(_fixture_copy[path]) os.environ.update(environ_copy) for device in list(os.environ.keys()): if device not in environ_copy: del os.environ[device] reader.clear() for _, pipe in pending_events.values(): assert not pipe.poll() assert macro_variables.is_alive(1)
def test_autoload(self): devices = ['device 1', 'device 2'] presets = ['bar0', 'bar', 'bar2'] paths = [ get_preset_path(devices[0], presets[0]), get_preset_path(devices[1], presets[1]), get_preset_path(devices[1], presets[2]) ] Mapping().save(paths[0]) Mapping().save(paths[1]) Mapping().save(paths[2]) daemon = Daemon() start_history = [] stop_counter = 0 # using an actual injector is not within the scope of this test class Injector: def stop_injecting(self, *args, **kwargs): nonlocal stop_counter stop_counter += 1 def start_injecting(device, preset): print(f'\033[90mstart_injecting\033[0m') start_history.append((device, preset)) daemon.injectors[device] = Injector() daemon.start_injecting = start_injecting config.set_autoload_preset(devices[0], presets[0]) config.set_autoload_preset(devices[1], presets[1]) config.save_config() control(options('autoload', None, None, None, False, False, False), daemon) self.assertEqual(len(start_history), 2) self.assertEqual(start_history[0], (devices[0], presets[0])) self.assertEqual(start_history[1], (devices[1], presets[1])) self.assertIn(devices[0], daemon.injectors) self.assertIn(devices[1], daemon.injectors) self.assertFalse( daemon.autoload_history.may_autoload(devices[0], presets[0])) self.assertFalse( daemon.autoload_history.may_autoload(devices[1], presets[1])) # calling autoload again doesn't load redundantly control(options('autoload', None, None, None, False, False, False), daemon) self.assertEqual(len(start_history), 2) self.assertEqual(stop_counter, 0) self.assertFalse( daemon.autoload_history.may_autoload(devices[0], presets[0])) self.assertFalse( daemon.autoload_history.may_autoload(devices[1], presets[1])) # unless the injection in question ist stopped control(options('stop', None, None, devices[0], False, False, False), daemon) self.assertEqual(stop_counter, 1) self.assertTrue( daemon.autoload_history.may_autoload(devices[0], presets[0])) self.assertFalse( daemon.autoload_history.may_autoload(devices[1], presets[1])) control(options('autoload', None, None, None, False, False, False), daemon) self.assertEqual(len(start_history), 3) self.assertEqual(start_history[2], (devices[0], presets[0])) self.assertFalse( daemon.autoload_history.may_autoload(devices[0], presets[0])) self.assertFalse( daemon.autoload_history.may_autoload(devices[1], presets[1])) # if a device name is passed, will only start injecting for that one control(options('stop-all', None, None, None, False, False, False), daemon) self.assertTrue( daemon.autoload_history.may_autoload(devices[0], presets[0])) self.assertTrue( daemon.autoload_history.may_autoload(devices[1], presets[1])) self.assertEqual(stop_counter, 3) config.set_autoload_preset(devices[1], presets[2]) config.save_config() control( options('autoload', None, None, devices[1], False, False, False), daemon) self.assertEqual(len(start_history), 4) self.assertEqual(start_history[3], (devices[1], presets[2])) self.assertTrue( daemon.autoload_history.may_autoload(devices[0], presets[0])) self.assertFalse( daemon.autoload_history.may_autoload(devices[1], presets[2])) # autoloading for the same device again redundantly will not autoload # again control( options('autoload', None, None, devices[1], False, False, False), daemon) self.assertEqual(len(start_history), 4) self.assertEqual(stop_counter, 3) self.assertFalse( daemon.autoload_history.may_autoload(devices[1], presets[2])) # any other arbitrary preset may be autoloaded self.assertTrue( daemon.autoload_history.may_autoload(devices[1], 'quuuux')) # after 15 seconds it may be autoloaded again daemon.autoload_history._autoload_history[devices[1]] = (time.time() - 16, presets[2]) self.assertTrue( daemon.autoload_history.may_autoload(devices[1], presets[2]))