Ejemplo n.º 1
0
def main():
    # install a signal handler so the script can stop safely
    def signal_handler(signum, frame):
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        if tty_fd:
            os.close(tty_fd)
        if SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK:
            SDL_QuitSubSystem(SDL_INIT_JOYSTICK)
        SDL_Quit()
        LOG.debug(f'{sys.argv[0]} exiting cleanly')
        sys.exit(0)

    debug_flag, hex_chars = parse_arguments(sys.argv)
    if debug_flag:
        LOG.setLevel(logging.DEBUG)

    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)
    # daemonize after signal handlers are registered
    if os.fork():
        os._exit(0)

    try:
        tty_fd = os.open('/dev/tty', os.O_WRONLY)
    except IOError:
        LOG.error('Unable to open /dev/tty', file=sys.stderr)
        sys.exit(1)

    curses.setupterm()
    mapped_chars = [
        get_hex_chars(code) for code in hex_chars
        if get_hex_chars(code) is not None
    ]

    def_buttons = [
        'left', 'right', 'up', 'down', 'a', 'b', 'x', 'y', 'pageup', 'pagedown'
    ]
    joy_map = {}
    # add for each button the mapped keycode, based on the arguments received
    for i, btn in enumerate(def_buttons):
        if i < len(mapped_chars):
            joy_map[btn] = mapped_chars[i]

    menu_swap = ra_btn_swap_config()
    # if button A is <enter> and menu_swap_ok_cancel_buttons is true, swap buttons A and B functions
    if menu_swap \
            and 'a' in joy_map.keys() \
            and 'b' in joy_map.keys() \
            and joy_map['a'] == '\n':
        joy_map['a'] = joy_map['b']
        joy_map['b'] = '\n'

    # tell SDL that we don't want to grab and lock the keyboard
    os.environ['SDL_INPUT_LINUX_KEEP_KBD'] = '1'

    # disable the HIDAPI joystick driver in SDL
    if not (SDL_USE_HIDAPI):
        os.environ['SDL_JOYSTICK_HIDAPI'] = '0'

    # tell SDL to not add any signal handlers for TERM/INT
    os.environ['SDL_NO_SIGNAL_HANDLERS'] = '1'

    configs = get_all_ra_config(def_buttons)

    if SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO) < 0:
        LOG.error(f'Error in SDL_Init: {SDL_GetError()}')
        exit(2)

    if LOG.isEnabledFor(logging.DEBUG):
        sdl_ver = version.SDL_version()
        version.SDL_GetVersion(byref(sdl_ver))
        wrapper_version = '.'.join(str(i) for i in version_info)
        LOG.debug(
            f'Using SDL Version {sdl_ver.major}.{sdl_ver.minor}.{sdl_ver.patch}, PySDL2 version {wrapper_version}'
        )

    if joystick.SDL_NumJoysticks() < 1:
        LOG.debug(f'No available joystick devices found on startup')

    event_loop(configs, joy_map, tty_fd)

    SDL_QuitSubSystem(SDL_INIT_JOYSTICK)
    SDL_Quit()
    return 0
Ejemplo n.º 2
0
 def test_SDL_NumJoysticks(self):
     retval = joystick.SDL_NumJoysticks()
     self.assertGreaterEqual(retval, 0)
Ejemplo n.º 3
0
 def setup_class(cls):
     SDL_Init(SDL_INIT_JOYSTICK)
     num = joystick.SDL_NumJoysticks()
     if num < 1:
         pytest.skip("no available joystick devices")
     cls.jcount = num
Ejemplo n.º 4
0
 def setUpClass(cls):
     if SDL_Init(SDL_INIT_JOYSTICK) != 0:
         raise unittest.SkipTest('Joystick subsystem not supported')
     cls.jcount = joystick.SDL_NumJoysticks()
Ejemplo n.º 5
0
def test_SDL_NumJoysticks():
    if SDL_Init(SDL_INIT_JOYSTICK) != 0:
        pytest.skip("joystick subsystem not supported")
    retval = joystick.SDL_NumJoysticks()
    SDL_Quit()
    assert retval >= 0