Ejemplo n.º 1
0
 def test_SDL_JoystickSetPlayerIndex(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         joystick.SDL_JoystickSetPlayerIndex(stick, index)
         player = joystick.SDL_JoystickGetPlayerIndex(stick)
         assert player == index
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 2
0
 def test_SDL_JoystickGetButton(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         for button in range(joystick.SDL_JoystickNumButtons(stick)):
             val = joystick.SDL_JoystickGetButton(stick, button)
             assert val in [0, 1]
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 3
0
 def test_SDL_JoystickNumButtons(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         assert isinstance(stick.contents, joystick.SDL_Joystick)
         buttons = joystick.SDL_JoystickNumButtons(stick)
         assert buttons >= 0
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 4
0
 def test_SDL_JoystickOpen(self):
     if self.jcount == 0:
         self.skipTest("no joysticks detected")
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         self.assertIsInstance(stick.contents, joystick.SDL_Joystick)
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 5
0
 def test_SDL_JoystickRumble(self):
     # If we ever add an interactive test suite, this should be moved there
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         # 50% strength low-frequency, 25% high-frequency rumble for 500ms
         ret = joystick.SDL_JoystickRumble(stick, 32767, 16384, 500)
         assert ret in [-1, 0]
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 6
0
 def test_SDL_JoystickGetGUID(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         guid = joystick.SDL_JoystickGetGUID(stick)
         assert isinstance(guid, joystick.SDL_JoystickGUID)
         guidlist = list(guid.data)
         assert isinstance(guidlist[0], int)
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 7
0
 def test_SDL_JoystickGetAxis(self):
     if self.jcount == 0:
         self.skipTest("no joysticks detected")
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         for axis in range(joystick.SDL_JoystickNumAxes(stick)):
             val = joystick.SDL_JoystickGetAxis(stick, axis)
             self.assertTrue(-32768 <= val <= 32767)
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 8
0
 def test_SDL_JoystickNumButtons(self):
     if self.jcount == 0:
         self.skipTest("no joysticks detected")
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         self.assertIsInstance(stick.contents, joystick.SDL_Joystick)
         buttons = joystick.SDL_JoystickNumButtons(stick)
         self.assertGreaterEqual(buttons, 0)
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 9
0
 def test_SDL_JoystickGetButton(self):
     if self.jcount == 0:
         self.skipTest("no joysticks detected")
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         for button in range(joystick.SDL_JoystickNumButtons(stick)):
             val = joystick.SDL_JoystickGetButton(stick, button)
             # TODO: x
             # self.assertIsInstance(val, bool)
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 10
0
 def test_SDL_JoystickGetHat(self):
     if self.jcount == 0:
         self.skipTest("no joysticks detected")
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         for hat in range(joystick.SDL_JoystickNumHats(stick)):
             val = joystick.SDL_JoystickGetHat(stick, hat)
             self.assertIsInstance(val, int)
             # TODO
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 11
0
 def test_SDL_JoystickGetAxis(self):
     sticks = [joystick.SDL_JoystickOpen(i) for i in range(self.jcount)]
     numaxes = [joystick.SDL_JoystickNumAxes(s) for s in sticks]
     if not any(numaxes):
         pytest.skip("no axis on any connected controller")
     for stick in sticks:
         for axis in range(joystick.SDL_JoystickNumAxes(stick)):
             val = joystick.SDL_JoystickGetAxis(stick, axis)
             assert -32768 <= val <= 32767
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 12
0
 def test_SDL_JoystickGetBall(self):
     if self.jcount == 0:
         self.skipTest("no joysticks detected")
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         for ball in range(joystick.SDL_JoystickNumBalls(stick)):
             dx, dy = joystick.SDL_JoystickGetBall(stick, ball)
             self.assertIsInstance(dx, int)
             self.assertIsInstance(dy, int)
             # TODO
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 13
0
 def test_SDL_JoystickGetBall(self):
     sticks = [joystick.SDL_JoystickOpen(i) for i in range(self.jcount)]
     numball = [joystick.SDL_JoystickNumBalls(s) for s in sticks]
     if not any(numball):
         pytest.skip("no trackball on any connected controller")
     dx, dy = c_int(0), c_int(0)
     get_ball = joystick.SDL_JoystickGetBall
     for stick in sticks:
         for ball in range(joystick.SDL_JoystickNumBalls(stick)):
             ret = get_ball(stick, ball, byref(dx), byref(dy))
             err = SDL_GetError()
             assert ret == 0
             assert len(err) == 0
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 14
0
 def test_SDL_JoystickGetHat(self):
     hatvals = [
         joystick.SDL_HAT_UP, joystick.SDL_HAT_DOWN, joystick.SDL_HAT_LEFT,
         joystick.SDL_HAT_RIGHT, joystick.SDL_HAT_CENTERED,
         joystick.SDL_HAT_LEFTUP, joystick.SDL_HAT_LEFTDOWN,
         joystick.SDL_HAT_RIGHTUP, joystick.SDL_HAT_RIGHTDOWN
     ]
     sticks = [joystick.SDL_JoystickOpen(i) for i in range(self.jcount)]
     numhats = [joystick.SDL_JoystickNumHats(s) for s in sticks]
     if not any(numhats):
         pytest.skip("no POV hat on any connected controller")
     for stick in sticks:
         for hat in range(joystick.SDL_JoystickNumHats(stick)):
             val = joystick.SDL_JoystickGetHat(stick, hat)
             assert val in hatvals
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 15
0
 def test_SDL_JoystickCurrentPowerLevel(self):
     levels = [
         joystick.SDL_JOYSTICK_POWER_UNKNOWN,
         joystick.SDL_JOYSTICK_POWER_EMPTY,
         joystick.SDL_JOYSTICK_POWER_LOW,
         joystick.SDL_JOYSTICK_POWER_MEDIUM,
         joystick.SDL_JOYSTICK_POWER_FULL,
         joystick.SDL_JOYSTICK_POWER_WIRED,
         joystick.SDL_JOYSTICK_POWER_MAX,
     ]
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         pwr = joystick.SDL_JoystickCurrentPowerLevel(stick)
         err = SDL_GetError()
         assert pwr in levels
         assert len(err) == 0
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 16
0
 def test_SDL_JoystickOpenClose(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         assert isinstance(stick.contents, joystick.SDL_Joystick)
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 17
0
 def test_SDL_JoystickGetAttached(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         ret = joystick.SDL_JoystickGetAttached(stick)
         assert ret in [SDL_FALSE, SDL_TRUE]
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 18
0
 def test_SDL_JoystickName(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         name = joystick.SDL_JoystickName(stick)
         assert type(name) in (str, bytes)
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 19
0
 def test_SDL_JoystickGetPlayerIndex(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         player = joystick.SDL_JoystickGetPlayerIndex(stick)
         assert player in [-1, 0, 1, 2, 3]
         joystick.SDL_JoystickClose(stick)
Ejemplo n.º 20
0
def event_loop(configs, joy_map, tty_fd):
    event = SDL_Event()

    # keep of dict of active joystick devices as a dict of
    #  instance_id -> (config_id, SDL_Joystick object)
    active_devices = {}

    # keep an event queue populated with the current active inputs
    # indexed by joystick index, input type and input index
    # the values consist of:
    # - the event list (as taked from the event configuration)
    # - the number of times event was emitted (repeated)
    # - the last time when the event was fired
    # e.g. { event_hash -> ([event_list], repeat_no, last_fire_time) }
    event_queue = {}

    # keep track of axis previous values
    axis_prev_values = {}

    def handle_new_input(e: SDL_Event, axis_norm_value: int = 0) -> bool:
        """
        Event handling for button press/hat movement/axis movement
        Only needed when an new input is present
        Returns True when 'event_queue' is modified with a new event
        """
        dev_index = active_devices[event.jdevice.which][0]
        if e.type == SDL_JOYBUTTONDOWN:
            mapped_events = configs[dev_index].get_btn_event(
                event.jbutton.button)
            event_index = f'{dev_index}_btn{event.jbutton.button}'
        elif e.type == SDL_JOYHATMOTION:
            mapped_events = configs[dev_index].get_hat_event(
                event.jhat.hat, event.jhat.value)
            event_index = f'{dev_index}_hat{event.jhat.hat}'
        elif e.type == SDL_JOYAXISMOTION and axis_norm_value != 0:
            mapped_events = configs[dev_index].get_axis_event(
                event.jaxis.axis, axis_norm_value)
            event_index = f'{dev_index}_axis{event.jaxis.axis}'

        if mapped_events is not None:
            event_queue[event_index] = [mapped_events, 0, SDL_GetTicks()]
            return True

        return False

    running = True
    while running:
        input_started = False

        while SDL_PollEvent(byref(event)):

            if event.type == SDL_QUIT:
                running = False
                break

            if event.type == SDL_JOYDEVICEADDED:
                stick = joystick.SDL_JoystickOpen(event.jdevice.which)
                name = joystick.SDL_JoystickName(stick).decode('utf-8')
                guid = create_string_buffer(33)
                joystick.SDL_JoystickGetGUIDString(
                    joystick.SDL_JoystickGetGUID(stick), guid, 33)
                LOG.debug(
                    f'Joystick #{joystick.SDL_JoystickInstanceID(stick)} {name} added'
                )
                conf_found = False
                # try to find a configuration for the joystick
                for key, dev_conf in enumerate(configs):
                    if dev_conf.name == str(
                            name) or dev_conf.guid == guid.value.decode():
                        # Add the matching joystick configuration to the watched list
                        active_devices[joystick.SDL_JoystickInstanceID(
                            stick)] = (key, stick)
                        LOG.debug(
                            f'Added configuration for known device {configs[key]}'
                        )
                        conf_found = True
                        break

                # add the default configuration for unknown/un-configured joysticks
                if not conf_found:
                    LOG.debug(
                        f'Un-configured device "{str(name)}", mapped using generic mapping'
                    )
                    active_devices[joystick.SDL_JoystickInstanceID(stick)] = (
                        0, stick)

                # if the device has axis inputs, initialize to zero their initial position
                if joystick.SDL_JoystickNumAxes(stick) > 0:
                    axis_prev_values[joystick.SDL_JoystickInstanceID(
                        stick)] = [
                            0
                            for x in range(joystick.SDL_JoystickNumAxes(stick))
                        ]

                continue

            if event.jdevice.which not in active_devices:
                continue
            else:
                dev_index = active_devices[event.jdevice.which][0]

            if event.type == SDL_JOYDEVICEREMOVED:
                joystick.SDL_JoystickClose(
                    active_devices[event.jdevice.which][1])
                active_devices.pop(event.jdevice.which, None)
                axis_prev_values.pop(event.jdevice.which, None)
                LOG.debug(f'Removed joystick #{event.jdevice.which}')

            if event.type == SDL_JOYBUTTONDOWN:
                input_started = handle_new_input(event)

            if event.type == SDL_JOYBUTTONUP:
                event_queue.pop(f'{dev_index}_btn{event.jbutton.button}', None)

            if event.type == SDL_JOYHATMOTION:
                if event.jhat.value != SDL_HAT_CENTERED:
                    input_started = handle_new_input(event)
                else:
                    event_queue.pop(f'{dev_index}_hat{event.jhat.hat}', None)

            if event.type == SDL_JOYAXISMOTION:
                # check if the axis value went over the deadzone threshold
                if (abs(event.jaxis.value) > JS_AXIS_DEADZONE) \
                        != (abs(axis_prev_values[event.jdevice.which][event.jaxis.axis]) > JS_AXIS_DEADZONE):
                    # normalize the axis value to the movement direction or stop the input
                    if abs(event.jaxis.value) <= JS_AXIS_DEADZONE:
                        event_queue.pop(f'{dev_index}_axis{event.jaxis.axis}',
                                        None)
                    else:
                        if event.jaxis.value < 0:
                            axis_norm_value = -1
                        else:
                            axis_norm_value = 1
                        input_started = handle_new_input(
                            event, axis_norm_value)
                # store the axis current values for tracking
                axis_prev_values[event.jdevice.which][
                    event.jaxis.axis] = event.jaxis.value

        # process the current events in the queue
        if len(event_queue):
            emitted_events = filter_active_events(event_queue)
            if len(emitted_events):
                LOG.debug(f'Events emitted: {emitted_events}')
            # send the events mapped key code(s) to the terminal
            for k in emitted_events:
                if k in joy_map:
                    for c in joy_map[k]:
                        fcntl.ioctl(tty_fd, termios.TIOCSTI, c)

        SDL_Delay(JS_POLL_DELAY)
Ejemplo n.º 21
0
 def test_SDL_JoystickInstanceID(self):
     for index in range(self.jcount):
         stick = joystick.SDL_JoystickOpen(index)
         ret = joystick.SDL_JoystickInstanceID(stick)
         assert ret > 0
         joystick.SDL_JoystickClose(stick)