inputs icon indicating copy to clipboard operation
inputs copied to clipboard

add blocking parameter to `get_gamepad` method (fix #7)

Open Fuzzyma opened this issue 9 years ago • 2 comments

As mentioned in #7 the get_gamepad method blocks further execution of the program which is often not desired. This PR intends to add a blocking parameter which can be used to disable this behavior. You now can do something like this:

while True:

    # collect all events
    events = []

    while True:
        try:
            events.append(inputs.get_gamepad(blocking=False)[0])
        except inputs.UnpluggedError:
            print "Gamepad is not connected"
            waitForGamepad() # some logic to check if gamepad is connected
            continue
        except inputs.NoDataError:
            break

Note the array index [0] I have to use because it always returns only one array element. I dont know the internals of other devices or other systems. So I leave it as it is. However: For xbox one controller with win7 this could be simplified (see #8)

Fuzzyma avatar Oct 07 '16 14:10 Fuzzyma

This works on Windows, however for Linux (rasberry pi) the read() command still blocks. I have tracked down the function that blocks, which is the _get_data(self, read_size) function, when it performs self._character_device.read(read_size).

When running on linux the _character_device returns a BufferedReader object, from using the io.open(path, 'rb') command. When running on windows it uses a simpler approach with io.BytesIO() which I guess does not block?

def _character_device(self):
    if not self._character_file:
        if WIN:
            self._character_file = io.BytesIO()
            return self._character_file
        try:
            self._character_file = io.open(
                self._character_device_path, 'rb')
        except IOError as err:
            ...
    return self._character_file

I am trying to implement this on a Rasberry Pi, thus I need this to work. Anyone got any ideas of how to implement this differently?

SolidGeek avatar Oct 21 '19 14:10 SolidGeek

Thank You..

uzunozan41 avatar Jul 13 '20 09:07 uzunozan41