Skip to content

Instantly share code, notes, and snippets.

@lightdiscord
Last active September 30, 2021 02:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightdiscord/6c004a106a0a2fc9751ec28208d8fa5c to your computer and use it in GitHub Desktop.
Save lightdiscord/6c004a106a0a2fc9751ec28208d8fa5c to your computer and use it in GitHub Desktop.
Troobleshooting of controller hot plugging for steam-run

Every command is done under a nix-shell opened with the following command.

$ nix-shell -p gcc SDL2 steam-run

First thing compile the executable to debug controller hot plugging.

$ gcc ./main.c -o ./main -lSDL2

Now there are three ways to test.

  • ./main
  • steam-run ./main
  • sudo steam-run ./main

The result for the first method and the third are the same.

The second only sees the controller if it was plugged before running the program, but trying to re-plug it will not show any message.

#include <SDL2/SDL.h>
// The issue we try to reproduce can be done with only one controller.
// So this code does not support multiple controller for simplicity sake.
SDL_GameController *controller;
int main(void) {
SDL_Init(SDL_INIT_GAMECONTROLLER);
while (1) {
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT:
SDL_Quit();
return 0;
case SDL_CONTROLLERDEVICEADDED:
puts("hot plug added");
controller = SDL_GameControllerOpen(event.cdevice.which);
break;
case SDL_CONTROLLERDEVICEREMOVED:
puts("hot plug removed");
SDL_GameControllerClose(controller);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment