Exemplo n.º 1
0
    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]))
Exemplo n.º 2
0
    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]))
Exemplo n.º 3
0
    def test_autoload(self):
        devices = ['device 1234', 'device 2345']
        presets = ['preset', 'bar']
        paths = [
            get_preset_path(devices[0], presets[0]),
            get_preset_path(devices[1], presets[1])
        ]
        xmodmap = 'a/xmodmap.json'

        Mapping().save(paths[0])
        Mapping().save(paths[1])

        daemon = Daemon()

        start_history = []
        stop_history = []
        daemon.start_injecting = lambda *args: start_history.append(args)
        daemon.stop = lambda *args: stop_history.append(args)

        config.set_autoload_preset(devices[0], presets[0])
        config.set_autoload_preset(devices[1], presets[1])

        control(options('autoload', None, None, False, False), daemon, xmodmap)

        self.assertEqual(len(start_history), 2)
        self.assertEqual(len(stop_history), 1)
        self.assertEqual(start_history[0],
                         (devices[0], os.path.expanduser(paths[0]), xmodmap))
        self.assertEqual(start_history[1],
                         (devices[1], os.path.abspath(paths[1]), xmodmap))
Exemplo n.º 4
0
    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'

        path = get_preset_path(device, preset)

        custom_mapping.change(Key(event), to_name)
        custom_mapping.save(path)

        system_mapping.clear()

        config.set_autoload_preset(device, preset)

        pending_events[device] = [InputEvent(*event)]

        xmodmap_path = os.path.join(tmp, 'foobar.json')
        with open(xmodmap_path, 'w') as file:
            file.write(f'{{"{to_name}":{to_keycode}}}')

        self.daemon = Daemon()
        self.daemon.start_injecting(device, path, xmodmap_path)

        event = uinput_write_history_pipe[0].recv()
        self.assertEqual(event.type, EV_KEY)
        self.assertEqual(event.code, to_keycode)
        self.assertEqual(event.value, 1)
Exemplo n.º 5
0
 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())
Exemplo n.º 6
0
 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())
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    def test_autoload(self):
        self.assertEqual(len(config.iterate_autoload_presets()), 0)
        self.assertFalse(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2', 'b'))

        config.set_autoload_preset('d1', 'a')
        self.assertEqual(len(config.iterate_autoload_presets()), 1)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2', 'b'))

        config.set_autoload_preset('d2', 'b')
        self.assertEqual(len(config.iterate_autoload_presets()), 2)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertTrue(config.is_autoloaded('d2', 'b'))

        config.set_autoload_preset('d2', 'c')
        self.assertEqual(len(config.iterate_autoload_presets()), 2)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2', 'b'))
        self.assertTrue(config.is_autoloaded('d2', 'c'))
        self.assertListEqual(list(config.iterate_autoload_presets()),
                             [('d1', 'a'), ('d2', 'c')])

        config.set_autoload_preset('d2', None)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2', 'b'))
        self.assertFalse(config.is_autoloaded('d2', 'c'))
        self.assertListEqual(list(config.iterate_autoload_presets()),
                             [('d1', 'a')])
Exemplo n.º 11
0
    def on_rename_button_clicked(self, _):
        """Rename the preset based on the contents of the name input."""
        new_name = self.get('preset_name_input').get_text()

        if new_name in ['', self.preset_name]:
            return

        self.save_preset()

        new_name = rename_preset(self.group.name, self.preset_name, new_name)

        # if the old preset was being autoloaded, change the
        # name there as well
        is_autoloaded = config.is_autoloaded(self.group.key, self.preset_name)
        if is_autoloaded:
            config.set_autoload_preset(self.group.key, new_name)

        self.get('preset_name_input').set_text('')
        self.populate_presets()
Exemplo n.º 12
0
    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")
Exemplo n.º 13
0
    def test_refresh_devices_on_start(self):
        ev = (EV_KEY, 9)
        keycode_to = 100
        device = '9876 name'
        # this test only makes sense if this device is unknown yet
        self.assertIsNone(get_devices().get(device))
        custom_mapping.change(Key(*ev, 1), 'a')
        system_mapping.clear()
        system_mapping._set('a', keycode_to)
        preset = 'foo'
        custom_mapping.save(get_preset_path(device, preset))
        config.set_autoload_preset(device, preset)
        pending_events[device] = [InputEvent(*ev, 1)]
        self.daemon = Daemon()
        preset_path = get_preset_path(device, preset)

        # make sure the devices are populated
        get_devices()
        fixtures[self.new_fixture] = {
            'capabilities': {
                evdev.ecodes.EV_KEY: [ev[1]]
            },
            'phys': '9876 phys',
            'info': 'abcd',
            'name': device
        }

        self.daemon.start_injecting(device, preset_path)

        # test if the injector called refresh_devices successfully
        self.assertIsNotNone(get_devices().get(device))

        event = uinput_write_history_pipe[0].recv()
        self.assertEqual(event.type, EV_KEY)
        self.assertEqual(event.code, keycode_to)
        self.assertEqual(event.value, 1)

        self.daemon.stop_injecting(device)
        self.assertFalse(self.daemon.is_injecting(device))
Exemplo n.º 14
0
    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'))
Exemplo n.º 15
0
    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)
Exemplo n.º 16
0
    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')])
Exemplo n.º 17
0
    def test_autoload(self):
        self.assertEqual(len(config.iterate_autoload_presets()), 0)
        self.assertFalse(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2.foo', 'b'))
        self.assertEqual(config.get(['autoload', 'd1']), None)
        self.assertEqual(config.get(['autoload', 'd2.foo']), None)

        config.set_autoload_preset('d1', 'a')
        self.assertEqual(len(config.iterate_autoload_presets()), 1)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2.foo', 'b'))

        config.set_autoload_preset('d2.foo', 'b')
        self.assertEqual(len(config.iterate_autoload_presets()), 2)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertTrue(config.is_autoloaded('d2.foo', 'b'))
        self.assertEqual(config.get(['autoload', 'd1']), 'a')
        self.assertEqual(config.get('autoload.d1'), 'a')
        self.assertEqual(config.get(['autoload', 'd2.foo']), 'b')

        config.set_autoload_preset('d2.foo', 'c')
        self.assertEqual(len(config.iterate_autoload_presets()), 2)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2.foo', 'b'))
        self.assertTrue(config.is_autoloaded('d2.foo', 'c'))
        self.assertEqual(config._config['autoload']['d2.foo'], 'c')
        self.assertListEqual(list(config.iterate_autoload_presets()),
                             [('d1', 'a'), ('d2.foo', 'c')])

        config.set_autoload_preset('d2.foo', None)
        self.assertTrue(config.is_autoloaded('d1', 'a'))
        self.assertFalse(config.is_autoloaded('d2.foo', 'b'))
        self.assertFalse(config.is_autoloaded('d2.foo', 'c'))
        self.assertListEqual(list(config.iterate_autoload_presets()),
                             [('d1', 'a')])
        self.assertEqual(config.get(['autoload', 'd1']), 'a')
Exemplo n.º 18
0
 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()
Exemplo n.º 19
0
    def test_daemon(self):
        # remove the existing system mapping to force our own into it
        if os.path.exists(get_config_path('xmodmap.json')):
            os.remove(get_config_path('xmodmap.json'))

        ev_1 = (EV_KEY, 9)
        ev_2 = (EV_ABS, 12)
        keycode_to_1 = 100
        keycode_to_2 = 101

        group = groups.find(name='Bar Device')

        # unrelated group that shouldn't be affected at all
        group2 = groups.find(name='gamepad')

        custom_mapping.change(Key(*ev_1, 1), 'a')
        custom_mapping.change(Key(*ev_2, -1), 'b')

        system_mapping.clear()
        # since this is in the same memory as the daemon, there is no need
        # to save it to disk
        system_mapping._set('a', keycode_to_1)
        system_mapping._set('b', keycode_to_2)

        preset = 'foo'

        custom_mapping.save(group.get_preset_path(preset))
        config.set_autoload_preset(group.key, preset)
        """injection 1"""

        # should forward the event unchanged
        push_events(group.key, [new_event(EV_KEY, 13, 1)])

        self.daemon = Daemon()
        self.daemon.set_config_dir(get_config_path())

        self.assertFalse(uinput_write_history_pipe[0].poll())
        self.daemon.start_injecting(group.key, preset)

        self.assertEqual(self.daemon.get_state(group.key), STARTING)
        self.assertEqual(self.daemon.get_state(group2.key), UNKNOWN)

        event = uinput_write_history_pipe[0].recv()
        self.assertEqual(self.daemon.get_state(group.key), RUNNING)
        self.assertEqual(event.type, EV_KEY)
        self.assertEqual(event.code, 13)
        self.assertEqual(event.value, 1)

        self.daemon.stop_injecting(group.key)
        self.assertEqual(self.daemon.get_state(group.key), STOPPED)

        time.sleep(0.1)
        try:
            self.assertFalse(uinput_write_history_pipe[0].poll())
        except AssertionError:
            print('Unexpected', uinput_write_history_pipe[0].recv())
            # possibly a duplicate write!
            raise
        """injection 2"""

        # -1234 will be normalized to -1 by the injector
        push_events(group.key, [new_event(*ev_2, -1234)])

        self.daemon.start_injecting(group.key, preset)

        time.sleep(0.1)
        self.assertTrue(uinput_write_history_pipe[0].poll())

        # the written key is a key-down event, not the original
        # event value of -1234
        event = uinput_write_history_pipe[0].recv()

        self.assertEqual(event.type, EV_KEY)
        self.assertEqual(event.code, keycode_to_2)
        self.assertEqual(event.value, 1)
Exemplo n.º 20
0
    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]))
Exemplo n.º 21
0
    def test_daemon(self):
        ev_1 = (EV_KEY, 9)
        ev_2 = (EV_ABS, 12)
        keycode_to_1 = 100
        keycode_to_2 = 101

        device = 'device 2'

        custom_mapping.change(Key(*ev_1, 1), 'a')
        custom_mapping.change(Key(*ev_2, -1), 'b')

        system_mapping.clear()
        system_mapping._set('a', keycode_to_1)
        system_mapping._set('b', keycode_to_2)

        preset = 'foo'

        custom_mapping.save(get_preset_path(device, preset))
        config.set_autoload_preset(device, preset)
        """injection 1"""

        # should forward the event unchanged
        pending_events[device] = [InputEvent(EV_KEY, 13, 1)]

        self.daemon = Daemon()
        preset_path = get_preset_path(device, preset)

        self.assertFalse(uinput_write_history_pipe[0].poll())
        self.daemon.start_injecting(device, preset_path)

        self.assertTrue(self.daemon.is_injecting(device))
        self.assertFalse(self.daemon.is_injecting('device 1'))

        event = uinput_write_history_pipe[0].recv()
        self.assertEqual(event.type, EV_KEY)
        self.assertEqual(event.code, 13)
        self.assertEqual(event.value, 1)

        self.daemon.stop_injecting(device)
        self.assertFalse(self.daemon.is_injecting(device))

        time.sleep(0.2)
        try:
            self.assertFalse(uinput_write_history_pipe[0].poll())
        except AssertionError:
            print(uinput_write_history_pipe[0].recv())
            raise
        """injection 2"""

        # -1234 will be normalized to -1 by the injector
        pending_events[device] = [InputEvent(*ev_2, -1234)]

        path = get_preset_path(device, preset)
        self.daemon.start_injecting(device, path)

        # the written key is a key-down event, not the original
        # event value of -5678
        event = uinput_write_history_pipe[0].recv()
        self.assertEqual(event.type, EV_KEY)
        self.assertEqual(event.code, keycode_to_2)
        self.assertEqual(event.value, 1)