SDL_PollEvent() takes over 500ms when adding gamepad


I’ve been working on an emulator, currently doing my development on Ubuntu. In my main thread, for each frame I loop over SDL_PollEvent before updating the screen.

This normally works fine, however, I’ve noticed that when adding a new joystick to the computer, seen with both Bluetooth and USB, my display locks up for around half a second.

I’ve narrowed this down to some of what happens in SDL_PollEvent:

  • In SDL_UDEV_Poll, there is a call to SDL_Delay(100). This delay occurs three times when I attach my USB gamepad. (maybe instead this should just note the add-time and wait for a future call to SDL_UDEV_Poll instead of using a delay?)
  • In MaybeAddDevice (both in SDL_sysjoystick.c and SDL_syshaptic.c), we open each /dev/input/js0 and /dev/input/event25. This adds up to four calls to the open syscall, taking around 60ms each on my system.

I’d like to remove this temporary lock-up that my GUI has when plugging in a gamepad.

The documentation around SDL_PollEvent / SDL_PumpEvents suggests that they need to be called from the same thread that set up the video, although at least for these operations I can’t see why the joystick polling couldn’t be pushed off into another thread.

Has this type of issue come up before, and what would be the recommended solution?


Check the output of dmesg, it is possible that the device reconnects many times when connecting or gives errors or something else that eventually generates a large pool of messages that the SDL collects and is forced to process them.

Try to make a test, turn off the event handling and reconnect the device, maybe there is even something else here.

If it does not work, then at startup you need to find out if there is a device and save the information yes there is or no no. Then, before processing events in the loop, check every time and there is a device, then clear the event pool once in order to avoid processing a large queue that probably arises when the device is connected.

But these are my guesses. Now I have no way to test this

I apologize if that for my English :slight_smile: