How to use a joystick from SDL on a Mac?

2.1k views Asked by At

The source is very basic:

#include <SDL.h>

int main(int argc, char *argv[])
{
  if (SDL_Init(SDL_INIT_JOYSTICK))
  {
    return 1;
  }
  int num_joy, i;
  num_joy=SDL_NumJoysticks();
  printf("%d joysticks found\n", num_joy);
  for(i=0;i<num_joy;i++)
      printf("%s\n", SDL_JoystickName(i));

  SDL_Quit();
  return 0;
}

This outputs the names of connected joysticks on Ubuntu 12.04, but not on my Mavericks Mac, with SDL 1.2.15, installed from Homebrew. Running it as root makes no difference. I assuming at this point that this is just a bug in SDL, but I'd love to be shown that I'm doing something wrong here.

Source here: https://github.com/mikepurvis/joystick_sdl

2

There are 2 answers

1
mikepurvis On BEST ANSWER

This is ancient history now, but the issue turned out to be that my controller (Logitech F710) was in XInput mode instead of DirectInput mode, by the switch on top of the unit. XInput is not supported on Mac OS X.

Flipping the switch (to "D") makes the controller work as expected.

cf. http://cantonbecker.com/etcetera/2015/logitech-f710-for-os-x-macintosh-setup/

0
Bram On

You need to take a look at the following SDL2 function call:

SDL_GameControllerAddMappingsFromFile

This will read in gamepad configurations for gamepads that are not supported by SDL2 out of the box.

To get a community maintained list of controllers, please take a look at: gamecontrollerdb.txt

To create your own mappings for obscure gamepads that you may have lying around, you need to run the SDL/test/controllermap.c example. To build this example on OSX, I used:

$ gcc controllermap.c -lSDL2 -I ../include -o controllermap -L ~/lib/ -framework AudioUnit -framework Carbon -framework ForceFeedback -framework CoreAudio -framework CoreVideo -framework IOKit -framework Cocoa