Developing for Xperia Play Gamepad!

Monkey Targets Forums/Android/Developing for Xperia Play Gamepad!

Aman(Posted 2011) [#1]
I want to take advantage of the gamepad in the Sony Ericsson Xperia Play. I know that monkey does not support it by default but it should not be that hard.

This is my attempt and I think I am on the right direction. Sadly, I don't have an Xperia Play and cannot verify if this works or not. I have a track pad and was able to test the KEYCODE_DPAD_CENTER which is triggered when pressing the X button on Xperia Play and it worked!

Can any body with Xperia Play confirm if this works or not?

Add this code to mojo.android.java:
public boolean onKeyDown(int keyCode, KeyEvent event) { // Attmpt to Handle GamePad & DPad Events
	if( app!=null ){
		switch(keyCode){
			case 23:  // KEYCODE_DPAD_CENTER 'trackpad or X button(Xperia Play) is pressed
				app.input.OnKeyDown( 13 );	  // KEY_ENTER
				break;
			case 4:	 // KEYCODE_BACK 'Back Button or O Button(Xperia Play) is pressed
				app.input.OnKeyDown( 27 );	  // KEY_ESCAPE
				break;
			case 99:  // KEYCODE_BUTTON_X	'Square button(Xperia Play) is pressed
				app.input.OnKeyDown( 88 );	  // KEY_X
				break;
			case 100: // KEYCODE_BUTTON_Y	'Triangle button(Xperia Play) is pressed
				app.input.OnKeyDown( 89 );	  // KEY_Y
				break;
			case 21:						  // KEYCODE_DPAD_LEFT
				app.input.OnKeyDown( 37 );	  // KEY_LEFT
				break;
			case 19:						  // KEYCODE_DPAD_UP
				app.input.OnKeyDown( 38 );	  // KEY_UP
				break;
			case 22:						  // KEYCODE_DPAD_RIGHT
				app.input.OnKeyDown( 39 );	  // KEY_RIGHT
				break;
			case 20:						  // KEYCODE_DPAD_DOWN
				app.input.OnKeyDown( 40 );	  // KEY_DOWN
				break;
			case 102:						  // KEYCODE_BUTTON_L1
				app.input.OnKeyDown( 122 );	  // KEY_F11
				break;
			case 103:						  // KEYCODE_BUTTON_R1
				app.input.OnKeyDown( 123 );	  // KEY_F12
				break;
			case 108:						  // KEYCODE_BUTTON_START
				app.input.OnKeyDown( 8 );	 	  // KEY_BACKSPACE
				break;
			case 109:						  // KEYCODE_BUTTON_SELECT
				app.input.OnKeyDown( 16 );	  // KEY_SHIFT
				break;
		}
	}
    return false;
}
public boolean onKeyUp(int keyCode, KeyEvent event) { // Attmpt to Handle GamePad & DPad Events
	if( app!=null ){
		switch(keyCode){
			case 23:  // KEYCODE_DPAD_CENTER 'trackpad or X button(Xperia Play) is pressed
				app.input.OnKeyUp( 13 );	  // KEY_ENTER
				break;
			case 4:	  // KEYCODE_BACK 'Back Button or O Button(Xperia Play) is pressed
				app.input.OnKeyUp( 27 );	  // KEY_ESCAPE
				break;
			case 99:  // KEYCODE_BUTTON_X	'Square button(Xperia Play) is pressed
				app.input.OnKeyUp( 88 );	  // KEY_X
				break;
			case 100: // KEYCODE_BUTTON_Y	'Triangle button(Xperia Play) is pressed
				app.input.OnKeyUp( 89 );	  // KEY_Y
				break;
			case 21:  // KEYCODE_DPAD_LEFT
				app.input.OnKeyUp( 37 );	  // KEY_LEFT
				break;
			case 19:						  // KEYCODE_DPAD_UP
				app.input.OnKeyUp( 38 );	  // KEY_UP
				break;
			case 22:						  // KEYCODE_DPAD_RIGHT
				app.input.OnKeyUp( 39 );	  // KEY_RIGHT
				break;
			case 20:						  // KEYCODE_DPAD_DOWN
				app.input.OnKeyUp( 40 );	  // KEY_DOWN
				break;
			case 102:						  // KEYCODE_BUTTON_L1
				app.input.OnKeyUp( 122 );	  // KEY_F11
				break;
			case 103:						  // KEYCODE_BUTTON_R1
				app.input.OnKeyUp( 123 );	  // KEY_F12
				break;
			case 108:						  // KEYCODE_BUTTON_START
				app.input.OnKeyUp( 8 );	 	  // KEY_BACKSPACE
				break;
			case 109:						  // KEYCODE_BUTTON_SELECT
				app.input.OnKeyUp( 16 );	  // KEY_SHIFT
				break;
		}
	}
    return false;
}


Keymapping:
L1 = KEY_F11
R1 = KEY_F12
o button = KEY_ESCAPE
x button = KEY_ENTER
square button = KEY_X
Triangle button = KEY_Y
Start = KEY_BACKSPACE
Select = KEY_SHIFT

If it works I will do something to differentiate between back button and o button. Blame Sony for the confusion. I don't want to invest more time on something I am not sure if it works or not.

Edit: This will override the onBackPressed() function So I had to implement KEY_ESCAPE in here as well. Otherwise back button will not function


FlameDuck(Posted 2011) [#2]
Do you have an apk I can simply install that tests this?


Amnesia(Posted 2011) [#3]
I just tested your code on a Xperia Play and it works perfectly. The only problem is that it doesn't make a distinction between the back key and the circle button. So I found this entry on the sony developer blog http://blogs.sonyericsson.com/wp/2011/02/13/xperia-play-game-keys/
and added the fix to your code:
public boolean onKeyDown(int keyCode, KeyEvent event) { // Attmpt to Handle GamePad & DPad Events
	if( app!=null ){
		switch(keyCode){
			case 23:  // KEYCODE_DPAD_CENTER 'trackpad or X button(Xperia Play) is pressed
				app.input.OnKeyDown( 13 );	  // KEY_ENTER
				break;
			case 4:	 // KEYCODE_BACK 'Back Button or O Button(Xperia Play) is pressed
				if (!event.isAltPressed()) {
					app.input.OnKeyDown( 27 );    //KEY_ESCAPE
				} else {
					app.input.OnKeyDown( 79 );	  // KEY_O
				}
				break;
			case 99:  // KEYCODE_BUTTON_X	'Square button(Xperia Play) is pressed
				app.input.OnKeyDown( 88 );	  // KEY_X
				break;
			case 100: // KEYCODE_BUTTON_Y	'Triangle button(Xperia Play) is pressed
				app.input.OnKeyDown( 89 );	  // KEY_Y
				break;
			case 21:						  // KEYCODE_DPAD_LEFT
				app.input.OnKeyDown( 37 );	  // KEY_LEFT
				break;
			case 19:						  // KEYCODE_DPAD_UP
				app.input.OnKeyDown( 38 );	  // KEY_UP
				break;
			case 22:						  // KEYCODE_DPAD_RIGHT
				app.input.OnKeyDown( 39 );	  // KEY_RIGHT
				break;
			case 20:						  // KEYCODE_DPAD_DOWN
				app.input.OnKeyDown( 40 );	  // KEY_DOWN
				break;
			case 102:						  // KEYCODE_BUTTON_L1
				app.input.OnKeyDown( 122 );	  // KEY_F11
				break;
			case 103:						  // KEYCODE_BUTTON_R1
				app.input.OnKeyDown( 123 );	  // KEY_F12
				break;
			case 108:						  // KEYCODE_BUTTON_START
				app.input.OnKeyDown( 8 );	 	  // KEY_BACKSPACE
				break;
			case 109:						  // KEYCODE_BUTTON_SELECT
				app.input.OnKeyDown( 16 );	  // KEY_SHIFT
				break;
		}
	}
	return false;
}
public boolean onKeyUp(int keyCode, KeyEvent event) { // Attmpt to Handle GamePad & DPad Events
	if( app!=null ){
		switch(keyCode){
			case 23:  // KEYCODE_DPAD_CENTER 'trackpad or X button(Xperia Play) is pressed
				app.input.OnKeyUp( 13 );	  // KEY_ENTER
				break;
			case 4:	  // KEYCODE_BACK 'Back Button or O Button(Xperia Play) is pressed
				if (!event.isAltPressed()) {
					app.input.OnKeyUp( 27 );      // KEY_ESCAPE
				} else {
					app.input.OnKeyUp( 79 );	  // KEY_O
				}
				break;
			case 99:  // KEYCODE_BUTTON_X	'Square button(Xperia Play) is pressed
				app.input.OnKeyUp( 88 );	  // KEY_X
				break;
			case 100: // KEYCODE_BUTTON_Y	'Triangle button(Xperia Play) is pressed
				app.input.OnKeyUp( 89 );	  // KEY_Y
				break;
			case 21:  // KEYCODE_DPAD_LEFT
				app.input.OnKeyUp( 37 );	  // KEY_LEFT
				break;
			case 19:						  // KEYCODE_DPAD_UP
				app.input.OnKeyUp( 38 );	  // KEY_UP
				break;
			case 22:						  // KEYCODE_DPAD_RIGHT
				app.input.OnKeyUp( 39 );	  // KEY_RIGHT
				break;
			case 20:						  // KEYCODE_DPAD_DOWN
				app.input.OnKeyUp( 40 );	  // KEY_DOWN
				break;
			case 102:						  // KEYCODE_BUTTON_L1
				app.input.OnKeyUp( 122 );	  // KEY_F11
				break;
			case 103:						  // KEYCODE_BUTTON_R1
				app.input.OnKeyUp( 123 );	  // KEY_F12
				break;
			case 108:						  // KEYCODE_BUTTON_START
				app.input.OnKeyUp( 8 );	 	  // KEY_BACKSPACE
				break;
			case 109:						  // KEYCODE_BUTTON_SELECT
				app.input.OnKeyUp( 16 );	  // KEY_SHIFT
				break;
		}
	}
	return false;
}


And to everyone else, this code has to be added inside the Activity:
public class MonkeyGame extends Activity{



Amnesia(Posted 2011) [#4]
I forgot, now the circle button is mapped to KEY_O


Gary Leeds(Posted 2011) [#5]
Is there not also a key activated when the screen is slid up? If you could read that you could display on screen buttons when the phone is closed (or not an xperia play) and remove the on screen controls when the phone is open


Aman(Posted 2011) [#6]
Thanks Amnesia

I was actually planning to do the fix the same way you did but wanted to make sure it was working. Thanks for testing it and for the fix.


Aman(Posted 2011) [#7]
Gary, I couldn't find anything in sony's documentation that relates to that. Besides, it would not make any sense to play a game that supports the gamepad and decide not to use it.

Android market now supports adding multiple apk files for the same app but for different devices. I plan to take advantage of that by supporting Apps2SD for newer devices and add an apk for the xperia play. This is also useful for fixing bugs that are hardware specific.


Gary Leeds(Posted 2011) [#8]
Aman

There must be some trigger though for the Play to switch to the different menu as you slide the screen up even if its not documented.

Might have to have a look and see what I can find


FBEpyon(Posted 2011) [#9]
Hello,

Okay instead of doubleposting,

Has anyone figured out the touchpad settings for the Xperia and getting that to work inside of monkey yet..?

William Mc.


Hezkore(Posted 2014) [#10]
How would one apply this to the newest version of Monkey X?