Mincus's picture

Joysticky stuff.

I thought the new Joystick API in OpenTK was worth looking into.
I figured the best way to learn it would be by writing an example app, so that's what I've done.

It's crude right now, needs cleaning up, but if it's wanted I'm happy to submit it with the other examples.

Screenshot below:
Basically each shaded square represents two axes. They tilt left/right for one and up/down for the other.
The red squares represent the buttons and turn green when the buttons are pressed.

I've had it running in both Windows and Linux (with 3 joysticks).
The top joystick shown in the screenshot is an X-Box 360 controller. It doesn't really get on with Linux and has a substantially different layout and even a button that doesn't seem to exist on the hardware. Still, all the axes are accessible and the buttons you can find on the hardware all work.
The other pad is a Saitek P880.
I've also tried a very basic pad with a just a digital pair of axes, no problems with that.

Note: My recent questions about the text under Linux are because I noticed my list on the right-hand-side wasn't working under it. ;o)

Updated: Changed all the graphics so it looks a lot nicer, moved the layout around a bit. Screenshot shows 3 joysticks.
Attached the source code, it probably still wants cleaning up some ideally, but it's straightforward.
Uses Display lists at the moment, I may change that to VBOs.
Should be room for 4 joysticks on there, can be easily edited if someone wants to add more. ;o)

There's some code in there that should, if OpenTK ever supports it, cope with unplugging / plugging in joysticks and update the display accordingly.
As it currently stands, it uses the number of joysticks OpenTK reports to initiate that rebuilding (if it changes, the joystick information is re-read) and if you pull a joystick at the moment it just reports whatever OpenTK gives it (which seems to be -1 for all axes).

JoystickExample.zip38.4 KB


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Nice, I haven't had the chance to try with >= 2 joysticks at the same time. I'm relieved it works :)

It seems that Microsoft intentionally "crippled" the DirectInput drivers for the X-Box 360 controller, in order to promote the new XInput API. Not really crippled, but the drivers expose one less axis on DI and some functionality is missing. Some kind actually took the time and fixed the drivers (sorry, no link!), making them work the same with both APIs.

I don't know how it looks on Linux, but I'd guess the layout exposed there is closer to the real thing. Maybe I'm evil, but I enjoy the irony in this. :)

Question: do you also get the "right stick is rotated 90 degrees" problem with your P880? I don't know if this a hardware issue, but mine always reports the right axes rotated in games (horizontal <-> vertical).

Mincus's picture

With the P880, yes, the right axes are like that.
I also noticed it on my previous Saitek pad (almost identical layout, wireless, I forget the exact model number).
I always assumed that was just how axes 3 and 4 reported themselves.

The 360 pad reports similarly: The first two axes are correct, then the 3rd is represented by the triggers (left is one direction, right is the other), then you have the standard two axes on the second stick.
These axes are reversed in the same way as the P880.
For the first stick you get horizontal as 0, vertical as 1.
For the second stick you get vertical, then horizontal.

I've no other joysticks to test this on (the 3rd I mentioned just has a single D-Pad that reports as 2 axes), but it does look like having the second stick axes reversed is expected behaviour.

Mincus's picture

Just mentioning I've updated it and added source code.

Also wanted to ask if there are plans for OpenTK to support plugging/unplugging of joysticks whilst it's running.
I know theoretically it shouldn't happen, but at the least being able to detect it and kick up a pause screen in a game would I think be handy.

the Fiddler's picture

Looks nice!

I've given some thought to joystick plugging/unplugging, but this functionality seems quite complicated to get right. We first need to investigate how this works on each platform and design an easy-to-use API that can cover all necessary use-cases. For example should there be an IsConnected property? DeviceConnected / DeviceDisconnected events? Does the actual device disappear once disconnected or does it stay there, just not sending any data?

This is not high-priority at the moment, but things always move faster if someone volunteers to do some research. Links to existing implementations are especially useful (regardless of the coding language).

Off-topic, but updates, new screenshots or things like that should generally go to fresh blog posts. The reason is that updates to existing posts are easy to miss: they won't show up on the "recent blog posts" box and also won't trigger new RSS feeds. Also, the more content to a blog, the better.