Connecting game controllers to the Flash Player is something I experimented in the past with FlashStick and a few years ago with WiiFlash with Joa. One of the limitations with those implementations is that you had to launch a standalone server acting as a gateway between the hardware and Flash. This was usually done through the use of binary socket which usually could bring some headaches due to the Flash Player security restrictions. The server also had to be started manually and this was a required dependency making applications deployment harder.
With AIR 2.0 and the native processes things are now made easier if you want to connect a physical device to ActionScript. Just embed your native application (Java, C, C#, F#, etc.) and communicate with it through STDIN and STDOUT. Of course, the Java runtime will be required if you are using a Java app, and .NET if you need it in your C# or F# code.
Yesterday, I thought about connecting game controllers to AIR in a simple and more abstract way so that any game device, whatever it is could be connected and its info retrieved in the AIR application. This way, a game could be distributed and whatever the device plugged (XBox, PS3, etc.) this would work. I came across JInput, a Java library which does the hard job for me :
The JInput Project hosts an implementation of an API for game controller discovery and polled input. It is part of a suite of open-source technologies initiated by the Game Technology Group at Sun Microsystems with intention of making the development of high performance games in Java a reality. The API itself is pure Java and presents a platform-neutral completely portable model of controller discovery and polling. It can handle arbitrary controllers and returns both human and machine understandable descriptions of the inputs available.
I created a simple Java wrapper as a prototype to communicate between Jinput and AIR and reverse, the Java code is incredibly simple :
class Main
{
public static void main (String[] args)
{
Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
Controller currentController;
List<Controller> currentGameControllerArray = new ArrayList<Controller>();
int lng = ca.length;
for (int i = 0; i < lng; i++)
{
currentController = ca[i];
System.out.println( "Found Device : " + currentController.getType() + " known as " + currentController.getName() );
if ( currentController.getType() == Controller.Type.GAMEPAD )
currentGameControllerArray.add(currentController);
}
System.out.println(currentGameControllerArray.size() + " game controllers found!");
}
}
On the ActionScript side, you just read the stream using the ProgressEvent.STANDARD_OUTPUT_DATA event :
output += process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
Just like a socket server, the Java code is pushing the real data to ActionScript just like a socket connection :
DataOutputStream dataOutputStream = new DataOutputStream(System.out); dataOutputStream.writeChars(controllerName); dataOutputStream.writeInt(controllerId); dataOutputStream.writeFloat(analogX); dataOutputStream.writeFloat(analogY); dataOutputStream.flush();
This way, just like in WiiFlash, a simple AS3 wrapper could be created to let you communicate with any game controller connected to the computer :
var controllers:Vector.<IController> = Controllers.getControllers();

The image on the left illustrates the early prototype. By clicking the Scan Devices button, the Java library scan devices and send the report to AIR.
The cool thing with JInput is that any kind of device can be detected and interacted with like a joystick, a fingerstick, a wheel mouse, a headtracker, a trackpad, etc. As soon as I got more time, I will post an early version of it with sources so that people can contribute and test it!
Comments (24)
Nice ! My sixaxis controller is jumping around waiting for this
J’ai encore beaucoup à apprendre, decidement.. merci Thibault, je te suis toujours avec autant d’admiration (pas de fanatisme sois tranquille
@+
This is awesome stuff. Can’t wait to try it out.
I see a universal Joystick enabler that let’s users install the AIR-app once and use their gamepad for any enabled game.
If you’d bundle this up in a nice web-based installer it would be a great service for any webgame portal (Kongregate, ArmorGames and the like).
Hi Bart,
Yes exactly something really compatible and easily deployable. That would be really nice for games portal yes!
Thibault
is that will be available for browser based flash player ? i mean will be possible to embed C, C++, Java, etc projects on browser without need to rewrite them in AS3 ???
Hi dimitar,
No this will not be possible for now in the browser, only for AIR on the desktop.
Thibault
Thank you for the answer Thibault, It’s pity
Thank you for sharing some source to play around with.
Cooooooooooooooool!!!!
Hi there, Thebault.
Thx for the post as always.
I’m on trying to connect game controllers myself now.
Btw, i’ve recently launched an open source project called SWFSize.
Which solves the mac os mouse wheel issue once and for all.
If u intend to check it – click on my nick name (don’t wanna post links here)
Wicked, i can t wait to try out, it will solve many problems! Will we use new classes or would we keep on using the same old wiiflash classes?
You, sir, are a legend amongst men.
I was just thinking to myself, earlier today, that the new native process access would allow this; I was eyeing off your previous implementation for port material.
I’m a newcomer to AIR, but have been using Flash and C# for ages. I was wondering, why not just use C# all the way? What kind of benefits does AIR + C# provide, apart from a better Flex UI? C# and Java provides rapid development just like Flex, and surely it’s easier to not to have to port connections to AIR too?
yeah! do this, do this! take your time…soon
please! this is the thing we all are waiting for!!!!
If you compile the air app as a native app why not compile the java part as native app? There is a free java to native compiler at http://gcc.gnu.org/java/ Dont know if it would work with jinput
It doesn’t seem to work on my osx !
with hugly code in console
21/04/10 18:15:09 [0x0-0x85b85b].com.apple.JarLauncher[12289] SystemFlippers: didn’t consume all data for long ID 0 (pBase = 0x100132b00, p = 0x100132b04, pEnd = 0x100132b08)
21/04/10 18:15:09 [0x0-0x85b85b].com.apple.JarLauncher[12289] Failed to load Main-Class manifest attribute from
Hey Thibault,
Did you ever get around to posting the source code?
Can anyone provide an example of how all this is implemented together in an AIR application please?
So I am getting pretty close to having an example for this. I have built a .jar file that should be passing all of the controller data. It looks like it runs just fine when I tested it. I then created a very simple Flex app to display the data from the controller. So I am not sure why but when I run the AIR app in debug mode from Flash Builder it is comes back saying No Controller Found. Hopefully I will figure out this last issue. I am hoping to post a version of this shortly that will recognize multiple controllers in to an AIR app. If anyone has run in to the issue I am having with No Controller Found and can point me in the right direction on fixing it that would be great. Hopefully I will have some good news to post in the next few days.
Wade (and others): Game controller support is coming natively in Flash 10.2 soon.
Ok maybe I skimmed the information out about the Flash 10.2 Beta and the new Flex SDK beta. but I am not seeing any examples on game controller support. Do you have any links to anything like that??? Thanks!
Its not coming to the Flash Player for a while. The native gamepad support is part of the Molehill API stuff that they havent released as a public beta. If I remember correctly, it wont be till this summer or later before we actually get to play with these new APIs.
But since I dont want to wait that long and didnt really understand this post, I went searching for means of connecting controllers to AIR and found an in-between solution of Flerry. That gives AIR 2.0 direct communication with Java and I hooked up JInput with that and it works great.
Hit up this link for details:
http://www.riaspace.com/2010/08/flerry-1-2-0-released/
Alas, Molehill (and native controller support) is not yet in the wild. Would love to get a copy of the full source and/or example project.