SDL_NumJoysticks() - Cross Platform Behaviour. |
raskie
|
Hi.
First time poster here, please be gentle! I'm currently working on an OSX application I'm porting across from Windows. It's written in SDL 2.0.3. A key feature is Joystick hotplugging... To make the feature work on Windows, I infrequently execute the SDL_NumJoysticks() method to get a running count of the number of devices plugged into the host computer. Between polling intervals, I store the SDL_NumJoysticks() result in a var, and compare it to the latest reading to determine when to attempt to open new Joysticks. It's probably not an optimal technique, but it works well enough. The problem comes with working on OSX... When I attempt to run the same code on a Mac, it breaks, because all successive calls to SDL_NumJoysticks() after initialisation return the same value as the initial call. I can't imagine that being expected behaviour... For the record, I tend not to use the eventing system, and would much rather avoid doing so (as I'm binding to a dynamic runtime, and am trying to keep the application logic in script). Is the SDL_NumJoysticks() method deprecated, or should I expect the method to work differently across different operating systems? And if the method is deprecated, do you have any advice on how I should effect hotplugging support on OSX? So far, my research has led me to the following links: http://stackoverflow.com/questions/21224023/why-does-sdl-numjoysticks-never-update#comment35466354_21224023 - Which appears to be a similar issue... http://forums.libsdl.org/viewtopic.php?t=9592&sid=f1468617007f482a33de58d62234405f - Which appears to be somewhat related, and explores alternative hotplugging schemes... Is there anyone out there who can shed a little light on this? Thank you. |
|||||||||||
|
SDL_NumJoysticks() - Cross Platform Behaviour. |
Alex Szpakowski
Guest
|
Have you tried using the SDL_JOYDEVICEADDED and SDL_JOYDEVICEREMOVED event types (corresponding to SDL_JoyDeviceEvent / event.jdevice) instead?
On Apr 22, 2014, at 9:07 AM, raskie wrote:
|
|||||||||||||
|
SDL_NumJoysticks() - Cross Platform Behaviour. |
Jared Maddox
Guest
|
Sorry to rain on your parade, but events simply ARE the correct way to go about it. However, it should be fully possible to do this in basically the same manner as you do the _NumJoysticks() polling, because the callback portion of the events system is OPTIONAL (even on e.g. iOS, I think), and was actually added to the already existing event system part way through SDL 2's initial development. As for how...
Those are the joystick hotplug events. You call _PollEvent() with the pointer to your event structure, check the type, ignore it if you don't care about it, otherwise you do something about it. Because _PollEvent() is called (and to the best of my knowledge doesn't block) you can use it to replace _NumJoysticks() (_PumpEvents() with _PeepEvents() might be a better fit to your particular engine model). After that you would need to decide exactly how you want things to work: if you automatically open joysticks then you're basically set, if the script does it then you'll need to store the hotplugging data in some format, maybe by maintaining a tree of available joysticks. Thanks to the magic of Turing Completeness you can do the script-side stuff however you feel like, but events are the correct way to do this with SDL2 (consider: what happens if a joysticks gets plugged in, and another gets unplugged? Your technique can miss this, resulting in the erroneous belief that joystick A isn't available but joystick B is.). _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||
|
SDL_NumJoysticks() - Cross Platform Behaviour. |
Ryan C. Gordon
Guest
|
The joystick count updates when we get hotplug events from Cocoa (even if you don't personally want events, SDL still needs them from the OS). If you absolutely never ever want SDL events, you should still do this in your program, somewhere that happens with some frequency... { SDL_PollEvent e; while (SDL_PollEvent(&e) {} } ...because SDL needs to talk to the OS, and does so in this function. The loop makes sure SDL's internal event queue doesn't fill up, so we throw it all away here. If you care about other events, you'll need to check them here. (SDL_JoystickUpdate() isn't enough for hotplug on Mac OS X, since it doesn't run the Cocoa runloop, it just sorts out data we got from the runloop last time it ran.) Lots of things will break in subtle ways if you don't pump the SDL event queue, this is one of them. --ryan. _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
raskie
|
Thanks for the suggestions.
I'll test out the event queue stuff over the week/weekend, and will let you know if the changes work. |
|||||||||||
|
SDL_NumJoysticks() - Cross Platform Behaviour. |
Sam Lantinga
|
If you know you're not using SDL for anything besides joysticks, you can call SDL_JoystickUpdate() instead of running the event loop, and that should take care of any periodic work that needs to be done by the joystick system.  If you do this, you should always call it before you check the state of joysticks or gamepads.
On Tue, Apr 22, 2014 at 5:07 AM, raskie wrote:
|
|||||||||||||
|
Re: SDL_NumJoysticks() - Cross Platform Behaviour. |
raskie
|
Btw, I've only just realized that you have to initialise the SDL video subsystem to access the device IO event queue - https://wiki.libsdl.org/CategoryEvents
As Sam rightly pointed out, I'm keen to avoid that as I don't need a graphics window. Just the joystick/gamepad subsystem. |
|||||||||||
|
raskie
|
Ok...
So I'll have to retest and make sure, but Ryan C. Gordon suggestion works as described on OSX, but for anyone looking at the issue, just bear in mind that this: [quote=Ryan C. Gordon]{ SDL_PollEvent e; while (SDL_PollEvent(&e) {} }[/quote] Should be this:
And before applying the 'hack', you'll need to initialise the SDL_VIDEO subsystem, which isn't ideal for me, but it's nice to have the feature working. It's certainly a bug, but I suppose it's a minor one. Thanks for the help. |
|||||||||||||
|
SDL_NumJoysticks() - Cross Platform Behaviour. |
Ryan C. Gordon
Guest
|
Doh, yes, that should be "SDL_Event e;" ... I mistyped. Sorry for the confusion!
For initial detection, we run joystick events in a temporary runloop, and then move it over to the main runloop, so the events land with everything else. Maybe we should leave these in the separate runloop, and run it in SDL_JoystickUpdate()? This will still work as usual, as SDL_PollEvent() calls SDL_PumpEvents() which calls SDL_JoystickUpdate(), but it also lets SDL_JoystickUpdate() run separately and collect joystick events from the OS. --ryan. _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||
|
SDL_NumJoysticks() - Cross Platform Behaviour. |
Ryan C. Gordon
Guest
|
(by "runloop" I mean "runloop mode," fwiw.) Attached is a patch that implements what I described here. I'm away from the office and don't have any joysticks with me, so I'll push this when I get home and test, but if anyone wants to test in the meantime, this _should_ fix the problem. --ryan. _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
SDL_NumJoysticks() - Cross Platform Behaviour. |
Ryan C. Gordon
Guest
|
Tested and pushed, enjoy! --ryan. _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|