Fix unmapped paddle and share button presses

This commit is contained in:
Cameron Gutman
2023-07-02 20:32:57 -05:00
parent 4e1b778f31
commit d6bbfa1af1
@@ -1052,13 +1052,25 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
} }
} }
// Return a valid keycode, 0 to consume, or -1 to not consume the event private final int REMAP_IGNORE = -1;
private final int REMAP_CONSUME = -2;
// Return a valid keycode, -2 to consume, or -1 to not consume the event
// Device MAY BE NULL // Device MAY BE NULL
private int handleRemapping(InputDeviceContext context, KeyEvent event) { private int handleRemapping(InputDeviceContext context, KeyEvent event) {
// Don't capture the back button if configured // Don't capture the back button if configured
if (context.ignoreBack) { if (context.ignoreBack) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return -1; return REMAP_IGNORE;
}
}
// If we know this gamepad has a share button and receive an unmapped
// KEY_RECORD event, report that as a share button press.
if (context.hasShare) {
if (event.getKeyCode() == KeyEvent.KEYCODE_UNKNOWN &&
event.getScanCode() == 167) {
return KeyEvent.KEYCODE_MEDIA_RECORD;
} }
} }
@@ -1148,7 +1160,7 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
case 316: case 316:
return KeyEvent.KEYCODE_BUTTON_MODE; return KeyEvent.KEYCODE_BUTTON_MODE;
default: default:
return 0; return REMAP_CONSUME;
} }
} }
// If this is a Serval controller sending an unknown key code, it's probably // If this is a Serval controller sending an unknown key code, it's probably
@@ -1904,15 +1916,14 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
} }
int keyCode = handleRemapping(context, event); int keyCode = handleRemapping(context, event);
if (keyCode < 0) {
return (keyCode == REMAP_CONSUME);
}
if (prefConfig.flipFaceButtons) { if (prefConfig.flipFaceButtons) {
keyCode = handleFlipFaceButtons(keyCode); keyCode = handleFlipFaceButtons(keyCode);
} }
if (keyCode == 0) {
return true;
}
// If the button hasn't been down long enough, sleep for a bit before sending the up event // If the button hasn't been down long enough, sleep for a bit before sending the up event
// This allows "instant" button presses (like OUYA's virtual menu button) to work. This // This allows "instant" button presses (like OUYA's virtual menu button) to work. This
// path should not be triggered during normal usage. // path should not be triggered during normal usage.
@@ -2056,25 +2067,33 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
} }
context.rightTrigger = 0; context.rightTrigger = 0;
break; break;
default: case KeyEvent.KEYCODE_UNKNOWN:
// Paddles aren't mapped in any of the Android key layout files, // Paddles aren't mapped in any of the Android key layout files,
// so we need to handle the evdev key codes directly. // so we need to handle the evdev key codes directly.
switch (event.getScanCode()) { if (context.hasPaddles) {
case 0x2c4: // BTN_TRIGGER_HAPPY5 switch (event.getScanCode()) {
context.inputMap &= ~ControllerPacket.PADDLE1_FLAG; case 0x2c4: // BTN_TRIGGER_HAPPY5
break; context.inputMap &= ~ControllerPacket.PADDLE1_FLAG;
case 0x2c5: // BTN_TRIGGER_HAPPY6 break;
context.inputMap &= ~ControllerPacket.PADDLE2_FLAG; case 0x2c5: // BTN_TRIGGER_HAPPY6
break; context.inputMap &= ~ControllerPacket.PADDLE2_FLAG;
case 0x2c6: // BTN_TRIGGER_HAPPY7 break;
context.inputMap &= ~ControllerPacket.PADDLE3_FLAG; case 0x2c6: // BTN_TRIGGER_HAPPY7
break; context.inputMap &= ~ControllerPacket.PADDLE3_FLAG;
case 0x2c7: // BTN_TRIGGER_HAPPY8 break;
context.inputMap &= ~ControllerPacket.PADDLE4_FLAG; case 0x2c7: // BTN_TRIGGER_HAPPY8
break; context.inputMap &= ~ControllerPacket.PADDLE4_FLAG;
default: break;
return false; default:
return false;
}
} }
else {
return false;
}
break;
default:
return false;
} }
// Check if we're emulating the select button // Check if we're emulating the select button
@@ -2121,15 +2140,14 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
} }
int keyCode = handleRemapping(context, event); int keyCode = handleRemapping(context, event);
if (keyCode < 0) {
return (keyCode == REMAP_CONSUME);
}
if (prefConfig.flipFaceButtons) { if (prefConfig.flipFaceButtons) {
keyCode = handleFlipFaceButtons(keyCode); keyCode = handleFlipFaceButtons(keyCode);
} }
if (keyCode == 0) {
return true;
}
switch (keyCode) { switch (keyCode) {
case KeyEvent.KEYCODE_BUTTON_MODE: case KeyEvent.KEYCODE_BUTTON_MODE:
context.hasMode = true; context.hasMode = true;
@@ -2248,25 +2266,33 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
} }
context.rightTrigger = (byte)0xFF; context.rightTrigger = (byte)0xFF;
break; break;
default: case KeyEvent.KEYCODE_UNKNOWN:
// Paddles aren't mapped in any of the Android key layout files, // Paddles aren't mapped in any of the Android key layout files,
// so we need to handle the evdev key codes directly. // so we need to handle the evdev key codes directly.
switch (event.getScanCode()) { if (context.hasPaddles) {
case 0x2c4: // BTN_TRIGGER_HAPPY5 switch (event.getScanCode()) {
context.inputMap |= ControllerPacket.PADDLE1_FLAG; case 0x2c4: // BTN_TRIGGER_HAPPY5
break; context.inputMap |= ControllerPacket.PADDLE1_FLAG;
case 0x2c5: // BTN_TRIGGER_HAPPY6 break;
context.inputMap |= ControllerPacket.PADDLE2_FLAG; case 0x2c5: // BTN_TRIGGER_HAPPY6
break; context.inputMap |= ControllerPacket.PADDLE2_FLAG;
case 0x2c6: // BTN_TRIGGER_HAPPY7 break;
context.inputMap |= ControllerPacket.PADDLE3_FLAG; case 0x2c6: // BTN_TRIGGER_HAPPY7
break; context.inputMap |= ControllerPacket.PADDLE3_FLAG;
case 0x2c7: // BTN_TRIGGER_HAPPY8 break;
context.inputMap |= ControllerPacket.PADDLE4_FLAG; case 0x2c7: // BTN_TRIGGER_HAPPY8
break; context.inputMap |= ControllerPacket.PADDLE4_FLAG;
default: break;
return false; default:
return false;
}
} }
else {
return false;
}
break;
default:
return false;
} }
// Start+Back+LB+RB is the quit combo // Start+Back+LB+RB is the quit combo