The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_NumJoysticks() - Cross Platform Behaviour.
raskie


Joined: 08 Apr 2014
Posts: 5
Hi.

First time poster here, please be gentle! Very Happy

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:
Quote:
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.

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL_NumJoysticks() - Cross Platform Behaviour.
Jared Maddox
Guest

Quote:
Date: Tue, 22 Apr 2014 12:07:11 +0000
From: "raskie"
To:
Subject: [SDL] SDL_NumJoysticks() - Cross Platform Behaviour.
Message-ID:
Content-Type: text/plain; charset="iso-8859-1"

Hi.

First time poster here, please be gentle! Very Happy

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).


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...

Quote:
Date: Tue, 22 Apr 2014 18:48:14 -0300
From: Alex Szpakowski
To:
Subject: Re: [SDL] SDL_NumJoysticks() - Cross Platform Behaviour.
Message-ID:
Content-Type: text/plain; charset="iso-8859-1"

Have you tried using the SDL_JOYDEVICEADDED and SDL_JOYDEVICEREMOVED event types (corresponding to SDL_JoyDeviceEvent / event.jdevice) instead?


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

Quote:
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 there anyone out there who can shed a little light on this?

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


Joined: 08 Apr 2014
Posts: 5
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. Very Happy
SDL_NumJoysticks() - Cross Platform Behaviour.
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
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:
Quote:
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 mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: SDL_NumJoysticks() - Cross Platform Behaviour.
raskie


Joined: 08 Apr 2014
Posts: 5
[quote="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.
[quote]

Heh!...

Hi, Sam!

The SDL_JoystickUpdate() method appears to work fine on OSX.
This, for example, works as expected:

Code:
SDL_JoystickUpdate();
SDL_JoystickGetButton(*device pointer here*); // Returns an array of button states


But this code excerpt, in a manner very similar to the SDL_NumJoystick() issue will always return "SDL_TRUE" on OSX after pulling the joystick out of the USB socket:

Code:
SDL_JoystickUpdate();
SDL_JoystickGetAttached(*device pointer here*); // Evaluates to SDL_TRUE


On Windows however, SDL_JoystickGetAttached() works as expected.
I think I'll try out the event queue stuff tonight, if not, before the end of the week...

It shouldn't be too painful to rewrite my code to take advantage of it, but SDL_NumJoystick() behaviour on OSX is quite confusing.
raskie


Joined: 08 Apr 2014
Posts: 5
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


Joined: 08 Apr 2014
Posts: 5
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:

Code:
[b]SDL_Event e[/b];
while (SDL_PollEvent(&e) {};


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. Very Happy
SDL_NumJoysticks() - Cross Platform Behaviour.
Ryan C. Gordon
Guest

Quote:
{ SDL_PollEvent e; while (SDL_PollEvent(&e) {} }

Doh, yes, that should be "SDL_Event e;" ... I mistyped. Sorry for the
confusion!

Quote:
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.

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

Quote:
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()?

(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

Quote:
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.

Tested and pushed, enjoy!

--ryan.



_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org