# Hotplugging Joysticks And Gamepads On OSX

I've tested the cross platform joystick support features on GLFW, SDL2 and SFML. SDL2 appears to be the most complete, but it can't detect when joysticks have been unplugged on OSX. To me, this is a fundamental feature.

Joystick detection support appears to be much weaker on the Mac than in Windows. What can I do to overcome this?

• The help center explains very clearly what questions are considered off topic (as does Game Development Meta). "Lists-of" and "what-library-should-I-use" ("Are there any cross platform C++ libraries...") questions, as well as broad and opinion-oriented questions ("what makes this hotplugging hard...?") are off-topic. You appear to have misunderstood the goal of a StackExchange site, which is to archive concrete objective questions and answers and not to "appeal to developers in a similar position" necessarily. Fortunately the help center also includes a list of places you can ask off-topic questions. – user1430 Apr 9 '14 at 15:04
• What are you suggesting? That there's no technical question to answer? It's certainly not a subjective question. – Charlie Apr 9 '14 at 16:48
• This is not a 'which tech to use?' question. I'm trying to understand why OSX has such a large problem with USB devices, and how one might approach the hotplugging problem in particular. If you knew the answer, or had anything to add, then you should just share it or move on and allow someone else to answer, as that is what the Stackexchange is primarily... A way for people to help others. I shan't post on this board again. A small loss I guess, but you'll understand why. I've had 4 answers to my question, and none of them are at all relevant. Or even helpful. – Charlie Apr 9 '14 at 17:24
• @Charlie, I voted for reopening. I almost always try to explain these votes, and yours is one of the first votes where I get explain why it gets the affirmative. The reason that "which tech" questions are off-topic is that they can only be answered based on specific project requirements, which usually means they can't be answered correctly. In your case, where you have a very specific feature requirement, I feel that the question is answerable and useful. Good luck in getting the rest of your votes. I also edited your question again; feel free to revert or reedit as you see fit. – Seth Battin Apr 10 '14 at 0:05
• I'd argue that "Is there a specific reason that joystick detection support is weaker for OSX" is completely off-topic here. The "What can be done" question is better. Maybe something like: "How can I detect a USB gamepad being disconnected during gameplay on OSX, using SDL2?", removing the first paragraph entirely. That phrasing seems like it'd be beyond reproach. – Trevor Powell Apr 10 '14 at 4:30

Using the latest SFML from github (2.1 should also work but I didn't test with it) you can listen for the sf::Event::JoystickConnected and sf::Event::JoystickDisconnected events as described in the SFML Events Tutorial. I've tested the events and they are both fired on Mac and Windows.

A sample program to test this would look like:

#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML Joystick Events!");

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event:: JoystickConnected)
{
std::cout << "joystick connected: " << event.joystickConnect.joystickId << std::endl;
}
else if (event.type == sf::Event::JoystickDisconnected)
{
std::cout << "joystick disconnected: " << event.joystickConnect.joystickId << std::endl;
}
else if (event.type == sf::Event::Closed)
{
window.close();
}
}
}

return 0;
}