Game Controllers in AIR 2.0 by Thibault Imbert

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)

  1. Dhaya wrote::

    Nice ! My sixaxis controller is jumping around waiting for this :)

    Tuesday, January 19, 2010 at 6:43 pm #
  2. Baptiste wrote::

    J’ai encore beaucoup à apprendre, decidement.. merci Thibault, je te suis toujours avec autant d’admiration (pas de fanatisme sois tranquille ;-) @+

    Tuesday, January 19, 2010 at 6:45 pm #
  3. kode80 wrote::

    This is awesome stuff. Can’t wait to try it out.

    Tuesday, January 19, 2010 at 6:52 pm #
  4. Bart wrote::

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

    Wednesday, January 20, 2010 at 4:44 pm #
  5. Thibault Imbert wrote::

    Hi Bart,

    Yes exactly something really compatible and easily deployable. That would be really nice for games portal yes!

    Thibault

    Wednesday, January 20, 2010 at 6:09 pm #
  6. dimitar wrote::

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

    Thursday, January 21, 2010 at 11:07 am #
  7. Thibault Imbert wrote::

    Hi dimitar,

    No this will not be possible for now in the browser, only for AIR on the desktop.

    Thibault

    Thursday, January 21, 2010 at 3:55 pm #
  8. dimitar wrote::

    Thank you for the answer Thibault, It’s pity :(

    Thursday, January 21, 2010 at 4:17 pm #
  9. sharedtut wrote::

    Thank you for sharing some source to play around with.

    Thursday, January 21, 2010 at 7:59 pm #
  10. Morris wrote::

    Cooooooooooooooool!!!!

    Friday, January 22, 2010 at 11:42 am #
  11. Jloa wrote::

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

    Friday, January 29, 2010 at 7:42 pm #
  12. Gaelle wrote::

    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?

    Sunday, January 31, 2010 at 8:44 am #
  13. 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.

    Wednesday, February 10, 2010 at 7:37 am #
  14. Cardin wrote::

    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?

    Monday, February 15, 2010 at 9:51 am #
  15. peter wrote::

    yeah! do this, do this! take your time…soon :)
    please! this is the thing we all are waiting for!!!!

    Tuesday, February 16, 2010 at 12:22 pm #
  16. browny wrote::

    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

    Friday, February 19, 2010 at 12:11 am #
  17. mrbbp wrote::

    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

    :( (

    Wednesday, April 21, 2010 at 6:17 pm #
  18. Darius Julian wrote::

    Hey Thibault,
    Did you ever get around to posting the source code?

    Wednesday, December 8, 2010 at 10:34 pm #
  19. Wade wrote::

    Can anyone provide an example of how all this is implemented together in an AIR application please?

    Thursday, January 20, 2011 at 9:39 pm #
  20. Kyle wrote::

    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.

    Wednesday, February 2, 2011 at 4:04 am #
  21. analytik wrote::

    Wade (and others): Game controller support is coming natively in Flash 10.2 soon.

    Saturday, February 5, 2011 at 2:45 pm #
  22. Kyle wrote::

    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!

    Tuesday, February 8, 2011 at 12:13 am #
  23. Wade wrote::

    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/

    Thursday, February 10, 2011 at 10:05 pm #
  24. Tronster wrote::

    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.

    Thursday, September 1, 2011 at 6:32 pm #