Ejemplo n.º 1
0
    def test_clear(self):
        push_events('device 1', [
            new_event(EV_KEY, CODE_1, 1),
            new_event(EV_KEY, CODE_2, 1),
            new_event(EV_KEY, CODE_3, 1)
        ] * 15)

        self.create_helper()
        reader.start_reading('device 1')
        time.sleep(START_READING_DELAY + EVENT_READ_TIMEOUT * 3)

        reader.read()
        self.assertEqual(len(reader._unreleased), 3)
        self.assertIsNotNone(reader.previous_event)
        self.assertIsNotNone(reader.previous_result)

        # make the helper send more events to the reader
        time.sleep(EVENT_READ_TIMEOUT * 2)
        self.assertTrue(reader._results.poll())
        reader.clear()

        self.assertFalse(reader._results.poll())
        self.assertEqual(reader.read(), None)
        self.assertEqual(len(reader._unreleased), 0)
        self.assertIsNone(reader.get_unreleased_keys())
        self.assertIsNone(reader.previous_event)
        self.assertIsNone(reader.previous_result)
        self.tearDown()
Ejemplo n.º 2
0
    def test_reads_joysticks(self):
        # if their purpose is "buttons"
        custom_mapping.set('gamepad.joystick.left_purpose', BUTTONS)
        push_events(
            'gamepad',
            [
                new_event(EV_ABS, ABS_Y, MAX_ABS),
                # the value of that one is interpreted as release, because
                # it is too small
                new_event(EV_ABS, ABS_X, MAX_ABS // 10)
            ])
        self.create_helper()

        reader.start_reading('gamepad')
        time.sleep(0.2)
        self.assertEqual(reader.read(), (EV_ABS, ABS_Y, 1))
        self.assertEqual(reader.read(), None)
        self.assertEqual(len(reader._unreleased), 1)

        reader._unreleased = {}
        custom_mapping.set('gamepad.joystick.left_purpose', MOUSE)
        push_events('gamepad', [new_event(EV_ABS, ABS_Y, MAX_ABS)])
        self.create_helper()

        reader.start_reading('gamepad')
        time.sleep(0.1)
        self.assertEqual(reader.read(), None)
        self.assertEqual(len(reader._unreleased), 0)
Ejemplo n.º 3
0
    def test_change_device(self):
        push_events('device 1', [
            new_event(EV_KEY, 1, 1),
        ] * 100)

        push_events('device 2', [
            new_event(EV_KEY, 2, 1),
        ] * 100)

        self.create_helper()

        reader.start_reading('device 1')
        time.sleep(0.1)
        self.assertEqual(reader.read(), Key(EV_KEY, 1, 1))

        reader.start_reading('device 2')

        # it's plausible that right after sending the new read command more
        # events from the old device might still appear. Give the helper
        # some time to handle the new command.
        time.sleep(0.1)
        reader.clear()

        time.sleep(0.1)
        self.assertEqual(reader.read(), Key(EV_KEY, 2, 1))
Ejemplo n.º 4
0
    def test_gamepad_forward_joysticks(self):
        push_events(
            'gamepad',
            [
                # should forward them unmodified
                new_event(EV_ABS, ABS_X, 10),
                new_event(EV_ABS, ABS_Y, 20),
                new_event(EV_ABS, ABS_X, -30),
                new_event(EV_ABS, ABS_Y, -40),
                new_event(EV_KEY, BTN_A, 1),
                new_event(EV_KEY, BTN_A, 0)
            ] * 2)

        custom_mapping.set('gamepad.joystick.left_purpose', NONE)
        custom_mapping.set('gamepad.joystick.right_purpose', NONE)
        # BTN_A -> 77
        custom_mapping.change(Key((1, BTN_A, 1)), 'b')
        system_mapping._set('b', 77)
        self.injector = Injector(groups.find(name='gamepad'), custom_mapping)
        self.injector.start()

        # wait for the injector to start sending, at most 1s
        uinput_write_history_pipe[0].poll(1)
        time.sleep(0.2)

        # convert the write history to some easier to manage list
        history = read_write_history_pipe()

        self.assertEqual(history.count((EV_ABS, ABS_X, 10)), 2)
        self.assertEqual(history.count((EV_ABS, ABS_Y, 20)), 2)
        self.assertEqual(history.count((EV_ABS, ABS_X, -30)), 2)
        self.assertEqual(history.count((EV_ABS, ABS_Y, -40)), 2)
        self.assertEqual(history.count((EV_KEY, 77, 1)), 2)
        self.assertEqual(history.count((EV_KEY, 77, 0)), 2)
Ejemplo n.º 5
0
    def test_gamepad_trigger(self):
        # map one of the triggers to BTN_NORTH, while the other one
        # should be forwarded unchanged
        value = MAX_ABS // 2
        push_events('gamepad', [
            new_event(EV_ABS, ABS_Z, value),
            new_event(EV_ABS, ABS_RZ, value),
        ])

        # ABS_Z -> 77
        # ABS_RZ is not mapped
        custom_mapping.change(Key((EV_ABS, ABS_Z, 1)), 'b')
        system_mapping._set('b', 77)
        self.injector = Injector(groups.find(name='gamepad'), custom_mapping)
        self.injector.start()

        # wait for the injector to start sending, at most 1s
        uinput_write_history_pipe[0].poll(1)
        time.sleep(0.2)

        # convert the write history to some easier to manage list
        history = read_write_history_pipe()

        self.assertEqual(history.count((EV_KEY, 77, 1)), 1)
        self.assertEqual(history.count((EV_ABS, ABS_RZ, value)), 1)
Ejemplo n.º 6
0
        def do_stuff():
            if self.injector is not None:
                # discard the previous injector
                self.injector.stop_injecting()
                time.sleep(0.1)
                while uinput_write_history_pipe[0].poll():
                    uinput_write_history_pipe[0].recv()

            push_events('gamepad', [
                new_event(*w_down),
                new_event(*d_down),
                new_event(*w_up),
                new_event(*d_up),
            ])

            self.injector = Injector(groups.find(name='gamepad'),
                                     custom_mapping)

            # the injector will otherwise skip the device because
            # the capabilities don't contain EV_TYPE
            input = InputDevice('/dev/input/event30')
            self.injector._grab_device = lambda *args: input

            self.injector.start()
            uinput_write_history_pipe[0].poll(timeout=1)
            time.sleep(EVENT_READ_TIMEOUT * 10)
            return read_write_history_pipe()
Ejemplo n.º 7
0
    def test_reading_2(self):
        # a combination of events
        push_events('Foo Device 2', [
            new_event(EV_KEY, CODE_1, 1, 10000.1234),
            new_event(EV_KEY, CODE_3, 1, 10001.1234),
            new_event(EV_ABS, ABS_HAT0X, -1, 10002.1234)
        ])

        pipe = multiprocessing.Pipe()

        def refresh():
            # from within the helper process notify this test that
            # refresh was called as expected
            pipe[1].send('refreshed')

        with mock.patch.object(groups, 'refresh', refresh):
            self.create_helper()

        reader.start_reading(groups.find(key='Foo Device 2'))

        # sending anything arbitrary does not stop the helper
        reader._commands.send(856794)
        time.sleep(0.2)
        # but it makes it look for new devices because maybe its list of
        # groups is not up-to-date
        self.assertTrue(pipe[0].poll())
        self.assertEqual(pipe[0].recv(), 'refreshed')

        self.assertEqual(reader.read(), (
            (EV_KEY, CODE_1, 1),
            (EV_KEY, CODE_3, 1),
            (EV_ABS, ABS_HAT0X, -1)
        ))
        self.assertEqual(reader.read(), None)
        self.assertEqual(len(reader._unreleased), 3)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
 def test_reading_1(self):
     # a single event
     push_events('Foo Device 2', [new_event(EV_ABS, ABS_HAT0X, 1)])
     push_events('Foo Device 2', [new_event(EV_ABS, REL_X, 1)])  # mouse movements are ignored
     self.create_helper()
     reader.start_reading(groups.find(key='Foo Device 2'))
     time.sleep(0.2)
     self.assertEqual(reader.read(), (EV_ABS, ABS_HAT0X, 1))
     self.assertEqual(reader.read(), None)
     self.assertEqual(len(reader._unreleased), 1)
Ejemplo n.º 10
0
 def test_wrong_device(self):
     push_events('device 1', [
         new_event(EV_KEY, CODE_1, 1),
         new_event(EV_KEY, CODE_2, 1),
         new_event(EV_KEY, CODE_3, 1)
     ])
     self.create_helper()
     reader.start_reading('device 2')
     time.sleep(EVENT_READ_TIMEOUT * 5)
     self.assertEqual(reader.read(), None)
     self.assertEqual(len(reader._unreleased), 0)
Ejemplo n.º 11
0
 def test_blacklisted_events(self):
     push_events('Foo Device 2', [
         new_event(EV_KEY, BTN_TOOL_DOUBLETAP, 1),
         new_event(EV_KEY, CODE_2, 1),
         new_event(EV_KEY, BTN_TOOL_DOUBLETAP, 1),
     ])
     self.create_helper()
     reader.start_reading(groups.find(key='Foo Device 2'))
     time.sleep(0.1)
     self.assertEqual(reader.read(), (EV_KEY, CODE_2, 1))
     self.assertEqual(reader.read(), None)
     self.assertEqual(len(reader._unreleased), 1)
Ejemplo n.º 12
0
 def test_reading_ignore_up(self):
     push_events('device 1', [
         new_event(EV_KEY, CODE_1, 0, 10),
         new_event(EV_KEY, CODE_2, 1, 11),
         new_event(EV_KEY, CODE_3, 0, 12),
     ])
     self.create_helper()
     reader.start_reading('device 1')
     time.sleep(0.1)
     self.assertEqual(reader.read(), (EV_KEY, CODE_2, 1))
     self.assertEqual(reader.read(), None)
     self.assertEqual(len(reader._unreleased), 1)
Ejemplo n.º 13
0
 def test_ignore_value_2(self):
     # this is not a combination, because (EV_KEY CODE_3, 2) is ignored
     push_events(
         'device 1',
         [new_event(EV_ABS, ABS_HAT0X, 1),
          new_event(EV_KEY, CODE_3, 2)])
     self.create_helper()
     reader.start_reading('device 1')
     time.sleep(0.2)
     self.assertEqual(reader.read(), (EV_ABS, ABS_HAT0X, 1))
     self.assertEqual(reader.read(), None)
     self.assertEqual(len(reader._unreleased), 1)
Ejemplo n.º 14
0
 def test_blacklist(self):
     push_events('device 1', [
         new_event(EV_KEY, BTN_TOOL_DOUBLETAP, 1),
         new_event(EV_KEY, CODE_2, 1),
         new_event(EV_KEY, BTN_TOOL_DOUBLETAP, 1),
     ])
     self.create_helper()
     reader.start_reading('device 1')
     time.sleep(0.1)
     self.assertEqual(reader.read(), (EV_KEY, CODE_2, 1))
     self.assertEqual(reader.read(), None)
     self.assertEqual(len(reader._unreleased), 1)
Ejemplo n.º 15
0
    def test_gamepad_to_mouse(self):
        # maps gamepad joystick events to mouse events
        config.set('gamepad.joystick.non_linearity', 1)
        pointer_speed = 80
        config.set('gamepad.joystick.pointer_speed', pointer_speed)
        config.set('gamepad.joystick.left_purpose', MOUSE)

        # they need to sum up before something is written
        divisor = 10
        x = MAX_ABS / pointer_speed / divisor
        y = MAX_ABS / pointer_speed / divisor
        push_events('gamepad', [
            new_event(EV_ABS, ABS_X, x),
            new_event(EV_ABS, ABS_Y, y),
            new_event(EV_ABS, ABS_X, -x),
            new_event(EV_ABS, ABS_Y, -y),
        ])

        self.injector = Injector(groups.find(name='gamepad'), custom_mapping)
        self.injector.start()

        # 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 = read_write_history_pipe()

        if history[0][0] == EV_ABS:
            raise AssertionError(
                'The injector probably just forwarded them unchanged'
                # possibly in addition to writing mouse events
            )

        # 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
        count_x = history.count((EV_REL, REL_X, -1))
        count_y = history.count((EV_REL, REL_Y, -1))
        self.assertGreater(count_x, 1)
        self.assertGreater(count_y, 1)
        # only those two types of events were written
        self.assertEqual(len(history), count_x + count_y)
Ejemplo n.º 16
0
 def test_keymapper_devices(self):
     # Don't read from keymapper devices, their keycodes are not
     # representative for the original key. As long as this is not
     # intentionally programmed it won't even do that. But it was at some
     # point.
     push_events('key-mapper device 2', [
         new_event(EV_KEY, CODE_1, 1),
         new_event(EV_KEY, CODE_2, 1),
         new_event(EV_KEY, CODE_3, 1)
     ])
     self.create_helper()
     reader.start_reading('device 2')
     time.sleep(EVENT_READ_TIMEOUT * 5)
     self.assertEqual(reader.read(), None)
     self.assertEqual(len(reader._unreleased), 0)
Ejemplo n.º 17
0
    def test_terminate(self):
        self.create_helper()
        reader.start_reading('device 1')

        push_events('device 1', [new_event(EV_KEY, CODE_3, 1)])
        time.sleep(START_READING_DELAY + EVENT_READ_TIMEOUT)
        self.assertTrue(reader._results.poll())

        reader.terminate()
        reader.clear()
        time.sleep(EVENT_READ_TIMEOUT)

        # no new events arrive after terminating
        push_events('device 1', [new_event(EV_KEY, CODE_3, 1)])
        time.sleep(EVENT_READ_TIMEOUT * 3)
        self.assertFalse(reader._results.poll())
Ejemplo n.º 18
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'

        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)
Ejemplo n.º 19
0
    def test_reading_2(self):
        # a combination of events
        push_events('device 1', [
            new_event(EV_KEY, CODE_1, 1, 10000.1234),
            new_event(EV_KEY, CODE_3, 1, 10001.1234),
            new_event(EV_ABS, ABS_HAT0X, -1, 10002.1234)
        ])
        self.create_helper()
        reader.start_reading('device 1')

        # sending anything arbitrary does not stop the helper
        reader._commands.send(856794)

        time.sleep(0.2)

        self.assertEqual(reader.read(),
                         ((EV_KEY, CODE_1, 1), (EV_KEY, CODE_3, 1),
                          (EV_ABS, ABS_HAT0X, -1)))
        self.assertEqual(reader.read(), None)
        self.assertEqual(len(reader._unreleased), 3)
Ejemplo n.º 20
0
    def test_push_events(self):
        """Test that push_event works properly between helper and reader.

        Using push_events after the helper is already forked should work,
        as well as using push_event twice
        """
        def create_helper():
            # this will cause pending events to be copied over to the helper
            # process
            def start_helper():
                helper = RootHelper()
                helper.run()

            self.helper = multiprocessing.Process(target=start_helper)
            self.helper.start()
            time.sleep(0.1)

        def wait_for_results():
            # wait for the helper to send stuff
            for _ in range(10):
                time.sleep(EVENT_READ_TIMEOUT)
                if reader._results.poll():
                    break

        event = new_event(EV_KEY, 102, 1)
        create_helper()
        reader.start_reading(groups.find(key='Foo Device 2'))
        time.sleep(START_READING_DELAY)

        push_events('Foo Device 2', [event])
        wait_for_results()
        self.assertTrue(reader._results.poll())

        reader.clear()
        self.assertFalse(reader._results.poll())

        # can push more events to the helper that is inside a separate
        # process, which end up being sent to the reader
        push_events('Foo Device 2', [event])
        wait_for_results()
        self.assertTrue(reader._results.poll())
Ejemplo n.º 21
0
    def test_switch_device(self):
        push_events('device 2', [new_event(EV_KEY, CODE_1, 1)])
        push_events('device 1', [new_event(EV_KEY, CODE_3, 1)])
        self.create_helper()

        reader.start_reading('device 2')
        self.assertFalse(reader._results.poll())
        self.assertEqual(reader.device_name, 'device 2')
        time.sleep(EVENT_READ_TIMEOUT * 5)

        self.assertTrue(reader._results.poll())
        reader.start_reading('device 1')
        self.assertEqual(reader.device_name, 'device 1')
        self.assertFalse(reader._results.poll())  # pipe resets

        time.sleep(EVENT_READ_TIMEOUT * 5)
        self.assertTrue(reader._results.poll())

        self.assertEqual(reader.read(), (EV_KEY, CODE_3, 1))
        self.assertEqual(reader.read(), None)
        self.assertEqual(len(reader._unreleased), 1)
Ejemplo n.º 22
0
    def test_wheel(self):
        # this tests both keycode_mapper and event_producer, and it seems
        # to test stuff not covered in test_keycode_mapper, so it's a quite
        # important one.

        # wheel release events are made up with a debouncer

        # map those two to stuff
        w_up = (EV_REL, REL_WHEEL, -1)
        hw_right = (EV_REL, REL_HWHEEL, 1)

        # should be forwarded and present in the capabilities
        hw_left = (EV_REL, REL_HWHEEL, -1)

        custom_mapping.change(Key(*hw_right), 'k(b)')
        custom_mapping.change(Key(*w_up), 'c')

        system_mapping.clear()
        code_b = 91
        code_c = 92
        system_mapping._set('b', code_b)
        system_mapping._set('c', code_c)

        group_key = 'Foo Device 2'
        push_events(group_key, [
            new_event(*w_up),
        ] * 10 + [
            new_event(*hw_right),
            new_event(*w_up),
        ] * 5 + [new_event(*hw_left)])

        group = groups.find(key=group_key)
        self.injector = Injector(group, custom_mapping)

        device = InputDevice('/dev/input/event11')
        # make sure this test uses a device that has the needed capabilities
        # for the injector to grab it
        self.assertIn(EV_REL, device.capabilities())
        self.assertIn(REL_WHEEL, device.capabilities()[EV_REL])
        self.assertIn(REL_HWHEEL, device.capabilities()[EV_REL])
        self.assertIn(device.path, group.paths)

        self.injector.start()

        # wait for the first injected key down event
        uinput_write_history_pipe[0].poll(timeout=1)
        self.assertTrue(uinput_write_history_pipe[0].poll())
        event = uinput_write_history_pipe[0].recv()
        self.assertEqual(event.t, (EV_KEY, code_c, 1))

        time.sleep(EVENT_READ_TIMEOUT * 5)
        # in 5 more read-loop ticks, nothing new should have happened
        self.assertFalse(uinput_write_history_pipe[0].poll())

        time.sleep(EVENT_READ_TIMEOUT * 6)
        # 5 more and it should be within the second phase in which
        # the horizontal wheel is used. add some tolerance
        self.assertTrue(uinput_write_history_pipe[0].poll())
        event = uinput_write_history_pipe[0].recv()
        self.assertEqual(event.t, (EV_KEY, code_b, 1))

        time.sleep(EVENT_READ_TIMEOUT * 10 + 5 / 60)
        # after 21 read-loop ticks all events should be consumed, wait for
        # at least 3 (=5) producer-ticks so that the debouncers are triggered.
        # Key-up events for both wheel events should be written now that no
        # new key-down event arrived.
        events = read_write_history_pipe()
        self.assertEqual(events.count((EV_KEY, code_b, 0)), 1)
        self.assertEqual(events.count((EV_KEY, code_c, 0)), 1)
        self.assertEqual(events.count(hw_left), 1)  # the unmapped wheel

        # the unmapped wheel won't get a debounced release command, it's
        # forwarded as is
        self.assertNotIn((EV_REL, REL_HWHEEL, 0), events)

        self.assertEqual(len(events), 3)
Ejemplo n.º 23
0
    def test_injector(self):
        # the tests in test_keycode_mapper.py test this stuff in detail

        numlock_before = is_numlock_on()

        combination = Key((EV_KEY, 8, 1), (EV_KEY, 9, 1))
        custom_mapping.change(combination, 'k(KEY_Q).k(w)')
        custom_mapping.change(Key(EV_ABS, ABS_HAT0X, -1), 'a')
        # one mapping that is unknown in the system_mapping on purpose
        input_b = 10
        custom_mapping.change(Key(EV_KEY, input_b, 1), 'b')

        # stuff the custom_mapping outputs (except for the unknown b)
        system_mapping.clear()
        code_a = 100
        code_q = 101
        code_w = 102
        system_mapping._set('a', code_a)
        system_mapping._set('key_q', code_q)
        system_mapping._set('w', code_w)

        push_events(
            'Bar Device',
            [
                # should execute a macro...
                new_event(EV_KEY, 8, 1),
                new_event(EV_KEY, 9, 1),  # ...now
                new_event(EV_KEY, 8, 0),
                new_event(EV_KEY, 9, 0),
                # gamepad stuff. trigger a combination
                new_event(EV_ABS, ABS_HAT0X, -1),
                new_event(EV_ABS, ABS_HAT0X, 0),
                # just pass those over without modifying
                new_event(EV_KEY, 10, 1),
                new_event(EV_KEY, 10, 0),
                new_event(3124, 3564, 6542),
            ])

        self.injector = Injector(groups.find(name='Bar Device'),
                                 custom_mapping)
        self.assertEqual(self.injector.get_state(), UNKNOWN)
        self.injector.start()
        self.assertEqual(self.injector.get_state(), STARTING)

        uinput_write_history_pipe[0].poll(timeout=1)
        self.assertEqual(self.injector.get_state(), RUNNING)
        time.sleep(EVENT_READ_TIMEOUT * 10)

        # sending anything arbitrary does not stop the process
        # (is_alive checked later after some time)
        self.injector._msg_pipe[1].send(1234)

        # convert the write history to some easier to manage list
        history = read_write_history_pipe()

        # 1 event before the combination was triggered (+1 for release)
        # 4 events for the macro
        # 2 for mapped keys
        # 3 for forwarded events
        self.assertEqual(len(history), 11)

        # 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.
        self.assertIn((EV_KEY, 8, 1), history)
        self.assertIn((EV_KEY, code_q, 1), history)
        self.assertIn((EV_KEY, code_q, 1), history)
        self.assertIn((EV_KEY, code_q, 0), history)
        self.assertIn((EV_KEY, code_w, 1), history)
        self.assertIn((EV_KEY, code_w, 0), history)
        index_q_1 = history.index((EV_KEY, code_q, 1))
        index_q_0 = history.index((EV_KEY, code_q, 0))
        index_w_1 = history.index((EV_KEY, code_w, 1))
        index_w_0 = history.index((EV_KEY, code_w, 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, 0))
        del history[index_q_0]
        index_w_1 = history.index((EV_KEY, code_w, 1))
        del history[index_w_1]
        index_w_0 = history.index((EV_KEY, code_w, 0))
        del history[index_w_0]

        # the rest should be in order.
        # first the incomplete combination key that wasn't mapped to anything
        # and just forwarded. The input event that triggered the macro
        # won't appear here.
        self.assertEqual(history[0], (EV_KEY, 8, 1))
        self.assertEqual(history[1], (EV_KEY, 8, 0))
        # value should be 1, even if the input event was -1.
        # Injected keycodes should always be either 0 or 1
        self.assertEqual(history[2], (EV_KEY, code_a, 1))
        self.assertEqual(history[3], (EV_KEY, code_a, 0))
        self.assertEqual(history[4], (EV_KEY, input_b, 1))
        self.assertEqual(history[5], (EV_KEY, input_b, 0))
        self.assertEqual(history[6], (3124, 3564, 6542))

        time.sleep(0.1)
        self.assertTrue(self.injector.is_alive())

        numlock_after = is_numlock_on()
        self.assertEqual(numlock_before, numlock_after)
        self.assertEqual(self.injector.get_state(), RUNNING)
Ejemplo n.º 24
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)