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() pending_events['gamepad'] = [ InputEvent(*w_down), InputEvent(*d_down), InputEvent(*w_up), InputEvent(*d_up), ] self.injector = KeycodeInjector('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._prepare_device = lambda *args: (input, False) self.injector.start_injecting() uinput_write_history_pipe[0].poll(timeout=1) time.sleep(EVENT_READ_TIMEOUT * 10) return read_write_history_pipe()
def test_abs_to_rel(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) rel_x = evdev.ecodes.REL_X rel_y = evdev.ecodes.REL_Y # they need to sum up before something is written divisor = 10 x = MAX_ABS / pointer_speed / divisor y = MAX_ABS / pointer_speed / divisor pending_events['gamepad'] = [ InputEvent(EV_ABS, rel_x, x), InputEvent(EV_ABS, rel_y, y), InputEvent(EV_ABS, rel_x, -x), InputEvent(EV_ABS, rel_y, -y), ] self.injector = KeycodeInjector('gamepad', custom_mapping) self.injector.start_injecting() # 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 = [] while uinput_write_history_pipe[0].poll(): event = uinput_write_history_pipe[0].recv() history.append((event.type, event.code, event.value)) if history[0][0] == EV_ABS: raise AssertionError( 'The injector probably just forwarded them unchanged') # 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, the injector happens to write # y first self.assertEqual(history[-1][0], EV_REL) self.assertEqual(history[-1][1], rel_x) self.assertAlmostEqual(history[-1][2], -1) self.assertEqual(history[-2][0], EV_REL) self.assertEqual(history[-2][1], rel_y) self.assertAlmostEqual(history[-2][2], -1)