zeth / inputs

Cross-platform Python support for keyboards, mice and gamepads
BSD 3-Clause "New" or "Revised" License
273 stars 88 forks source link

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

Open Fuzzyma opened 8 years ago

Fuzzyma commented 8 years ago

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)

SolidGeek commented 5 years ago

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?

uzunozan41 commented 4 years ago

Thank You..