def test_refresh_for_unknown_key(self): device = '9876 name' # this test only makes sense if this device is unknown yet self.assertIsNone(groups.find(name=device)) self.daemon = Daemon() # make sure the devices are populated groups.refresh() self.daemon.refresh() fixtures[self.new_fixture_path] = { 'capabilities': { evdev.ecodes.EV_KEY: [evdev.ecodes.KEY_A] }, 'phys': '9876 phys', 'info': evdev.device.DeviceInfo(4, 5, 6, 7), 'name': device } self.daemon._autoload('25v7j9q4vtj') # this is unknown, so the daemon will scan the devices again # test if the injector called groups.refresh successfully self.assertIsNotNone(groups.find(name=device))
def _handle_commands(self): """Handle all unread commands.""" # wait for something to do select.select([self._commands], [], []) while self._commands.poll(): cmd = self._commands.recv() logger.debug('Received command "%s"', cmd) if cmd == TERMINATE: logger.debug('Helper terminates') sys.exit(0) if cmd == REFRESH_GROUPS: groups.refresh() self._send_groups() continue group = groups.find(key=cmd) if group is None: groups.refresh() group = groups.find(key=cmd) if group is not None: self.group = group continue logger.error('Received unknown command "%s"', cmd)
def test_refresh_on_start(self): if os.path.exists(get_config_path('xmodmap.json')): os.remove(get_config_path('xmodmap.json')) ev = (EV_KEY, 9) keycode_to = 100 group_name = '9876 name' # expected key of the group group_key = group_name group = groups.find(name=group_name) # this test only makes sense if this device is unknown yet self.assertIsNone(group) custom_mapping.change(Key(*ev, 1), 'a') system_mapping.clear() system_mapping._set('a', keycode_to) # make the daemon load the file instead with open(get_config_path('xmodmap.json'), 'w') as file: json.dump(system_mapping._mapping, file, indent=4) system_mapping.clear() preset = 'foo' custom_mapping.save(get_preset_path(group_name, preset)) config.set_autoload_preset(group_key, preset) push_events(group_key, [new_event(*ev, 1)]) self.daemon = Daemon() # make sure the devices are populated groups.refresh() # the daemon is supposed to find this device by calling refresh fixtures[self.new_fixture_path] = { 'capabilities': { evdev.ecodes.EV_KEY: [ev[1]] }, 'phys': '9876 phys', 'info': evdev.device.DeviceInfo(4, 5, 6, 7), 'name': group_name } self.daemon.set_config_dir(get_config_path()) self.daemon.start_injecting(group_key, preset) # 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) time.sleep(0.1) self.assertTrue(uinput_write_history_pipe[0].poll()) event = uinput_write_history_pipe[0].recv() self.assertEqual(event.t, (EV_KEY, keycode_to, 1)) self.daemon.stop_injecting(group_key) self.assertEqual(self.daemon.get_state(group_key), STOPPED)
def cleanup(): """Reset the applications state. Using this is slower, usually quick_cleanup() is sufficient. """ print('cleanup') os.system('pkill -f key-mapper-service') os.system('pkill -f key-mapper-control') time.sleep(0.05) quick_cleanup(log=False) groups.refresh()
def test_skip_camera(self): fixtures['/foo/bar'] = { 'name': 'camera', 'phys': 'abcd1', 'info': evdev.DeviceInfo(1, 2, 3, 4), 'capabilities': { evdev.ecodes.EV_KEY: [evdev.ecodes.KEY_CAMERA] } } groups.refresh() self.assertIsNone(groups.find(name='camera')) self.assertIsNotNone(groups.find(name='gamepad'))
def refresh(self, group_key=None): """Refresh groups if the specified group is unknown. Parameters ---------- group_key : str unique identifier used by the groups object """ now = time.time() if now - 10 > self.refreshed_devices_at: logger.debug('Refreshing because last info is too old') groups.refresh() self.refreshed_devices_at = now return if not groups.find(key=group_key): logger.debug('Refreshing because "%s" is unknown', group_key) groups.refresh() self.refreshed_devices_at = now
def test_duplicate_device(self): fixtures['/dev/input/event20']['name'] = 'Foo Device' groups.refresh() group1 = groups.find(key='Foo Device') group2 = groups.find(key='Foo Device 2') group3 = groups.find(key='Foo Device 3') self.assertIn('/dev/input/event1', group1.paths) self.assertIn('/dev/input/event10', group2.paths) self.assertIn('/dev/input/event20', group3.paths) self.assertEqual(group1.key, 'Foo Device') self.assertEqual(group2.key, 'Foo Device 2') self.assertEqual(group3.key, 'Foo Device 3') self.assertEqual(group1.name, 'Foo Device') self.assertEqual(group2.name, 'Foo Device') self.assertEqual(group3.name, 'Foo Device')
def test_device_with_only_ev_abs(self): # could be anything, a lot of devices have ABS_X capabilities, # so it is not treated as gamepad joystick and since it also # doesn't have key capabilities, there is nothing to map. fixtures['/foo/bar'] = { 'name': 'qux', 'phys': 'abcd2', 'info': evdev.DeviceInfo(1, 2, 3, 4), 'capabilities': { evdev.ecodes.EV_ABS: [evdev.ecodes.ABS_X] } } groups.refresh() self.assertIsNotNone(groups.find(name='gamepad')) self.assertIsNone(groups.find(name='qux')) # verify this test even works at all fixtures['/foo/bar']['capabilities'][EV_KEY] = [KEY_A] groups.refresh() self.assertIsNotNone(groups.find(name='qux'))
def tearDown(self): quick_cleanup() if self.helper is not None: self.helper.join() groups.refresh()