Skip to content

Commit

Permalink
Revamp mobile key management again
Browse files Browse the repository at this point in the history
Some jars use the keypad and the action keys for different functions,
so we have to account for that. Furthermore, libretro now has a use
for those then unused numpad 2,4,6,8 input array positions, with
num 5 also being added as a separate input, being L3 by default. It's
either that, or R3... and num 5 acting as a pointer press also feels
a bit more intuitive to put on the left analog press rather than
any other key, especially since num 5 is covered by Ok/Fire in most
cases.
  • Loading branch information
AShiningRay committed Nov 24, 2024
1 parent b127396 commit 43efe1b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 51 deletions.
42 changes: 24 additions & 18 deletions src/javax/microedition/lcdui/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,36 @@ protected Canvas()
Mobile.log(Mobile.LOG_INFO, Canvas.class.getPackage().getName() + "." + Canvas.class.getSimpleName() + ": " + "Create Canvas:"+width+", "+height);

platformImage = new PlatformImage(width, height);

Mobile.getPlatform().canvas = this;

Mobile.getPlatform().canvas = this;
}

public int getGameAction(int keyCode) { return Mobile.getGameAction(keyCode); }

public int getKeyCode(int gameAction)
{
switch(gameAction)
switch(gameAction) // Look on Mobile.java for what these magic numbers mean ("J2ME Canvas standard keycodes")
{
//case Mobile.GAME_UP: return Mobile.KEY_NUM2;
//case Mobile.GAME_DOWN: return Mobile.KEY_NUM8;
//case Mobile.GAME_LEFT: return Mobile.KEY_NUM4;
//case Mobile.GAME_RIGHT: return Mobile.KEY_NUM6;
//case Mobile.GAME_FIRE: return Mobile.KEY_NUM5;
case Mobile.GAME_UP: return Mobile.getMobileKey(0, true);
case Mobile.GAME_DOWN: return Mobile.getMobileKey(1, true);
case Mobile.GAME_LEFT: return Mobile.getMobileKey(2, true);
case Mobile.KEY_NUM2: return Mobile.getMobileKey(14, true);
case Mobile.KEY_NUM8: return Mobile.getMobileKey(17, true);
case Mobile.KEY_NUM4: return Mobile.getMobileKey(15, true);
case Mobile.KEY_NUM6: return Mobile.getMobileKey(16, true);
case Mobile.KEY_NUM5: return Mobile.getMobileKey(18, true);
case Mobile.GAME_UP: return Mobile.getMobileKey(0, true);
case Mobile.GAME_DOWN: return Mobile.getMobileKey(1, true);
case Mobile.GAME_LEFT: return Mobile.getMobileKey(2, true);
case Mobile.GAME_RIGHT: return Mobile.getMobileKey(3, true);
case Mobile.GAME_FIRE: return Mobile.getMobileKey(7, true);
case Mobile.GAME_A: return Mobile.getMobileKey(10, true);
case Mobile.GAME_B: return Mobile.getMobileKey(11, true);
case Mobile.GAME_C: return Mobile.getMobileKey(5, true);
case Mobile.GAME_D: return Mobile.getMobileKey(4, true);
case Mobile.GAME_FIRE: return Mobile.getMobileKey(7, true);

// GAME_A through D don't show up in documentation at all.
case Mobile.GAME_A: case Mobile.KEY_NUM1: return Mobile.getMobileKey(10, true);
case Mobile.GAME_B: case Mobile.KEY_NUM3: return Mobile.getMobileKey(11, true);
case Mobile.GAME_C: case Mobile.KEY_NUM7: return Mobile.getMobileKey(5, true);
case Mobile.GAME_D: case Mobile.KEY_NUM9: return Mobile.getMobileKey(4, true);

case Mobile.KEY_NUM0: return Mobile.getMobileKey(6, true);
case Mobile.KEY_STAR: return Mobile.getMobileKey(12, true);
case Mobile.KEY_POUND: return Mobile.getMobileKey(13, true);
}
return 0;
}
Expand Down Expand Up @@ -205,8 +211,8 @@ protected void sizeChanged(int w, int h)
height = h;
}

public void doSizeChanged(int w, int h) { sizeChanged(w, h); }

public void doSizeChanged(int w, int h) { sizeChanged(w, h); }

public void notifySetCurrent() { repaint(); }

}
19 changes: 13 additions & 6 deletions src/libretro/freej2me_libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,8 @@ void retro_run(void)
InputPoll();

/*
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
* Input array in libretro: [Up, Down, Left, Right, 9, 7, 0, 5/Fire, RightSoft, LeftSoft, 1, 3. *. #, 2, 4, 6, 8]
* NOTE: 2,4,6,8 aren't used yet.
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
* Input array in libretro: [Up, Down, Left, Right, 9, 7, 0, Fire, RightSoft, LeftSoft, 1, 3. *. #, 2, 4, 6, 8, 5]
*/

joypad[0] = InputState(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP);
Expand All @@ -701,6 +700,14 @@ void retro_run(void)
joypad[12] = InputState(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2);
joypad[13] = InputState(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2);

// ANALOG_THRESHOLD comes from freej2me_libretro.h.
joypad[14] = (int) ((InputState(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y ) / 32767.0f) < -ANALOG_THRESHOLD); // Num 2
joypad[15] = (int) ((InputState(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X ) / 32767.0f) < -ANALOG_THRESHOLD); // Num 4
joypad[16] = (int) ((InputState(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X ) / 32767.0f) > ANALOG_THRESHOLD); // Num 6
joypad[17] = (int) ((InputState(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y ) / 32767.0f) > ANALOG_THRESHOLD); // Num 8

joypad[18] = InputState(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3); // Num 5

/* Right analog will control the pointer, freeing the left analog to mirror the D-Pad if needed. */
int joyRx = InputState(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X);
int joyRy = InputState(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
Expand Down Expand Up @@ -809,12 +816,12 @@ void retro_run(void)
/* joypad - spot the difference, send corresponding keyup/keydown events */
if(joypad[i]!=joypre[i])
{
if(i==7 && joymouseTime>0 && joymouseAnalog)
if(i==18 && joymouseTime>0 && joymouseAnalog)
{
/* when mouse is visible, and using analog stick for mouse, Y / [5] clicks */
/* when mouse is visible, and using analog stick for mouse, L3 / [num 5] clicks */
if(joypad[i] == 1) { joymouseClickedTime = DEFAULT_FPS * 0.1; }
joymouseTime = DEFAULT_FPS / 2;
joyevent[0] = 4+joypad[7];
joyevent[0] = 4+joypad[18];
joyevent[1] = (joymouseX >> 8) & 0xFF;
joyevent[2] = (joymouseX) & 0xFF;
joyevent[3] = (joymouseY >> 8) & 0xFF;
Expand Down
22 changes: 13 additions & 9 deletions src/libretro/freej2me_libretro.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#define MAX_WIDTH 800
#define MAX_HEIGHT 800

/* Used as a limit to the string of core option updates to be sent to the Java app*/
/* Used as a limit to the string of core option updates to be sent to the Java app */
#define PIPE_MAX_LEN 255


#define PHONE_KEYS 18
#define PHONE_KEYS 19
#define ANALOG_THRESHOLD 0.25f

/* Input mapping variables and descriptions */
static const struct retro_controller_description port_1[] =
Expand All @@ -46,22 +47,25 @@ static const struct retro_controller_info ports[] =
/* This is responsible for exposing the joypad input mappings to the frontend */
static const struct retro_input_descriptor desc[] =
{
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Arrow Left, Num 4" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Arrow Up, Num 2" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Arrow Down, Num 8" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Arrow Right, Num 6" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Arrow Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Arrow Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Arrow Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Arrow Right" },
{ 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Num 4(-), Num 6(+) : " },
{ 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Num 2(-), Num 8(+) : " },
{ 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Pointer Horizontal Move"},
{ 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Pointer Vertical Move"},
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Num 7" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Num 9" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Num 0" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Num 5, Pointer Click" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "OK/Fire" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "Num 1" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Num 3" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "Num #" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "Num *" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Left Options Key" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Right Options Key" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "Num 5/Pointer Press" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Left Soft Key" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Right Soft Key" },

{ 0 },
};
Expand Down
54 changes: 36 additions & 18 deletions src/org/recompile/mobile/Mobile.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public class Mobile
* While on Libretro, it's: [Up, Down, Left, Right, 9, 7, 0, Fire, RightSoft, LeftSoft, 1, 3. *. #, 2, 4, 6, 8] (5 isn't even considered, as the core already abstracts it)
* Anbu still isn't considered here, but it should match libretro at some point.
*/
private static final int[] awtguiKeycodes = {9, 8, 0, 2, 7, 3, 1, 10, 14, 11, 15, 7, 16, 5, 17, 4, 12, 6, 13};
private static final int[] awtguiKeycodes = {9, 8, 0, 2, 7, 3, 1, 10, 14, 11, 15, 18, 16, 5, 17, 4, 12, 6, 13};
//private static final int[] libretroKeycodes = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; // 5 and Fire are also the same, and this array doesn't have to be used.

private static final String[] keyArray = {"Up", "Down", "Left", "Right", "9", "7", "0", "Fire/5", "RightSoft", "LeftSoft", "1", "3", "*", "#", "2", "4", "6", "8"};
private static final String[] keyArray = {"Up", "Down", "Left", "Right", "9", "7", "0", "Fire", "RightSoft", "LeftSoft", "1", "3", "*", "#", "2", "4", "6", "8", "5"};

// Set whether audio should be enabled or not. Can work around jars that crash FreeJ2ME due to audio
public static boolean sound = true;
Expand Down Expand Up @@ -317,6 +317,7 @@ public static final int getMobileKey(int keycode, boolean isLibretro)
case 15: return NOKIAKB_NUM4; // Left
case 16: return NOKIAKB_NUM6; // Right
case 17: return NOKIAKB_NUM8; // Down
case 18: return NOKIAKB_NUM5; // User-Mappable (often same as case 7)
}
}
if(sagem)
Expand Down Expand Up @@ -358,14 +359,14 @@ public static final int getMobileKey(int keycode, boolean isLibretro)
// J2ME Canvas standard keycodes, to match against any keys not covered above.
switch(keycode)
{
case 0: return KEY_NUM2; // Up
case 1: return KEY_NUM8; // Down
case 2: return KEY_NUM4; // Left
case 3: return KEY_NUM6; // Right
case 0: return GAME_UP; // Up
case 1: return GAME_DOWN; // Down
case 2: return GAME_LEFT; // Left
case 3: return GAME_RIGHT; // Right
case 4: return KEY_NUM9; // A
case 5: return KEY_NUM7; // B
case 6: return KEY_NUM0; // X
case 7: return KEY_NUM5; // Y
case 7: return GAME_FIRE; // Y
case 8: return NOKIA_SOFT2; // Start
case 9: return NOKIA_SOFT1; // Select
case 10: return KEY_NUM1; // L
Expand All @@ -376,6 +377,7 @@ public static final int getMobileKey(int keycode, boolean isLibretro)
case 15: return KEY_NUM4; // Left
case 16: return KEY_NUM6; // Right
case 17: return KEY_NUM8; // Down
case 18: return KEY_NUM5; // User-Mappable (often same as case 7)
}

// If a matching key wasn't found, return 0;
Expand Down Expand Up @@ -451,6 +453,14 @@ public static final int getGameAction(int keycode)
case NOKIAKB_SOFT3: return Canvas.FIRE; // Y
case NOKIAKB_NUM1: return Canvas.GAME_A; // L
case NOKIAKB_NUM3: return Canvas.GAME_B; // R
case NOKIAKB_NUM5: return Canvas.KEY_NUM5;
case NOKIAKB_NUM2: return Canvas.KEY_NUM2;
case NOKIAKB_NUM8: return Canvas.KEY_NUM8;
case NOKIAKB_NUM4: return Canvas.KEY_NUM4;
case NOKIAKB_NUM6: return Canvas.KEY_NUM6;
case NOKIAKB_NUM0: return Canvas.KEY_NUM0;
case NOKIAKB_STAR: return Canvas.KEY_STAR;
case NOKIAKB_POUND: return Canvas.KEY_POUND;
}
}
if (sagem)
Expand All @@ -476,18 +486,26 @@ public static final int getGameAction(int keycode)
}
}

// J2ME Canvas standard keycodes, to match against any keys not covered above.
switch (keycode)
// J2ME Canvas standard keycodes, to match against any keys not covered above (Canvas does not handle left/right soft keys).
switch (keycode) // This can probably be turned into a single 'return Canvas.getKeyCode(keycode)''
{
case KEY_NUM2: return Canvas.UP; // Up
case KEY_NUM8: return Canvas.DOWN; // Down
case KEY_NUM4: return Canvas.LEFT; // Left
case KEY_NUM6: return Canvas.RIGHT; // Right
case KEY_NUM9: return Canvas.GAME_D; // A
case KEY_NUM7: return Canvas.GAME_C; // B
case KEY_NUM5: return Canvas.FIRE; // Y
case KEY_NUM1: return Canvas.GAME_A; // L
case KEY_NUM3: return Canvas.GAME_B; // R
case GAME_UP: return Canvas.UP;
case GAME_DOWN: return Canvas.DOWN;
case GAME_LEFT: return Canvas.LEFT;
case GAME_RIGHT: return Canvas.RIGHT;
case KEY_NUM2: return Canvas.KEY_NUM2;
case KEY_NUM8: return Canvas.KEY_NUM8;
case KEY_NUM4: return Canvas.KEY_NUM4;
case KEY_NUM6: return Canvas.KEY_NUM6;
case KEY_NUM9: return Canvas.KEY_NUM9;
case KEY_NUM7: return Canvas.KEY_NUM7;
case KEY_NUM5: return Canvas.KEY_NUM5;
case KEY_NUM1: return Canvas.KEY_NUM1;
case KEY_NUM3: return Canvas.KEY_NUM3;
case KEY_NUM0: return Canvas.KEY_NUM0;
case KEY_STAR: return Canvas.KEY_STAR;
case KEY_POUND: return Canvas.KEY_POUND;
case GAME_FIRE: return Canvas.FIRE;
}

// If a matching key wasn't found, return 0;
Expand Down

0 comments on commit 43efe1b

Please sign in to comment.