the Fiddler's picture

[Input] Add Joystick support on Mac OS X

Project:The Open Toolkit library
Version:1.1.0-stable
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:closed
Description

I just committed support for joystick devices to trunk.

The Windows implementation is based in Win MM, while the Linux implementation reads events from the /dev/input/* device nodes. In theory, this means joystick input can be used independently, without using a GameWindow, GLControl, etc. In practice, it is necessary to change the interface around a bit before this will work right, so it's not implemented right now.

I have not touched the Mac OS X implementation, apart from making sure the library compiles. Kanato, if you have the time, can you please look into this? The code on Windows and Linux was surprisingly simple: discover devices, create a JoystickDevice for each device (where T is a platform-specific structure that contains extra device information, if necessary) and implement the Poll() function to update state with the JoystickDevice.SetAxis/.SetButton methods.

The question is, how can we handle the case where a device is disconnected or connected after initialization? Right now, devices are only discovered upon initialization, which is consistent with most libraries and games out there.

Also, does it make sense to write a Raw Input driver for windows? WinMM is limited in that it only supports 6 axes - are there joysticks out there with more than that?

Edit: Should use the real, hardware dependent joystick name in JoystickDevice.Description.

Edit 2: The implementation provides normalized axis coordinates, in the [-1, 1] range. Left and down are in the negative range, while up and right are in the positive.


Comments

Comment viewing options

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

#1

Yeah, I've been keeping my eye on the discussion on joystick support and I've also been perusing Apple's docs on the subject. I don't think this will be hard to implement, so I will start on it this tomorrow most likely.

I don't know of joysticks that have more than 6 axes, but I recently bought an Xbox-type gamepad which does have 6, the left stick and the right stick are 4 axes, and then two triggers on each side that are axes. (This one, which I highly recommend if any one wants to buy a gamepad.)

What about support for POV hats? On a gamepad like mine the D-pad is reported as a POV hat, rather than being tied to the joystick axes. It's essentially 4 more buttons, but it isn't reported that way on windows at least.

How does polling work? I remember when I was working on DirectInput support in AgateLib I found that it was best to poll the joysticks every 50ms or so. IIRC it turned out that doing it every frame was disastrous for performance. Is this left up to the API consumer to determine, or should we consider making it an automatic part of OpenTK? Performance might be different on different platforms and it might be best for us to figure out the best rate at which to poll on each platform, at least to provide some functional default behavior.

Edited to add: Oh yeah, and what about force feedback? I have never really looked into this but it might be useful if we can include this in the API.

the Fiddler's picture

#2

Saitek makes some of the best gamepads around. I have a relatively old model, a P880 "dual analog" and it's by far the most comfortable gamepad I've used.

[POV]
On Linux, POV hats are mapped to axes. It makes sense, too: a POV hat provides essentially two axes with no sensitivity.

I don't think we should treat POV hats any differently than other axes, or even buttons if they are reported as such on some platform.

[Polling rate]
I hadn't considered that polling too fast could have a detrimental effect to performance. AFAIK, most joystick drivers are interrupt driven, not polled, so I'd guess most of the DirectInput overhead comes from cross-thread data marshaling. I also wonder if it might be an artifact of the ManagedDX implementation that we could improve upon.

I'll have to fire the profiler and see where it leads us.

[Force feedback]
A quick search indicates that WinMM does not support FF. Even worse, WinMM is actually implemented on top of DirectInput - if I knew that, I would have gone for DirectInput directly.

The Linux kernel includes FF support, starting with 2.6.28 (released on December 2008). That's surprising, but at least it's there.

Mac OS X supports FF starting around 10.2. Some sample code that might be useful.

The APIs are quite extensive, so FF will take some time to get right. However, I agree that it would be awesome to have a reliable, cross-platform API available.

the Fiddler's picture

#3

Status:open» in progress
kanato's picture

#4

Ok, this is taking me longer than I expected. The code to use IOKit routines to interact with the joystick is more complex than I had expected. On top of that I have been programming a lot at work so it has been difficult to motivate myself to do coding at home. Next week I will be at a conference in Pennsylvania away from my mac, so we probably won't see much progress on this front until after then.

the Fiddler's picture

#5

No pressure. In typical open-source fashion, It's done when it's done. :)

BlueMonkMN's picture

#6

Until this is complete, is there any way to access the existing joystick code? I see there's a bunch there, but it appears inaccessible because I can't seem to get an instance of a JoystickDriver, which is what I need to get a Joystick object. I guess it's only accessible through GameWindow right now?

the Fiddler's picture

#7

Yes, it's only accessible through the GameWindow right now. That's only temporary, as there's nothing GameWindow-specific in the code.

the Fiddler's picture

#8

Title:Joystick support» [Input] Add Joystick support on Mac OS X
Status:in progress» open
laimr's picture

#10

Hi, any work being done on this issue? I am looking for a way to use joystick and GLControl on both Windows and MacOS. I am using a WinMM based solution on Windows, but have not been able to find a corresponding solution for MacOS. Any suggestions? Many thanks.

the Fiddler's picture

#11

Version:0.9.x-dev» 0.9.9-2b

OpenTK doesn't support joystick input on Mac OS X yet, but you can refer to the SDL sourcecode.

Note that the new input API in OpenTK is designed to be independent of GameWindow.