Skip to content

Commit

Permalink
Merge pull request #18497 from hrydgard/more-input-refactor
Browse files Browse the repository at this point in the history
Android: Batch motion events.
  • Loading branch information
hrydgard committed Dec 9, 2023
2 parents 3e20fab + fe732f1 commit 7f65db9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 21 deletions.
23 changes: 16 additions & 7 deletions android/jni/app-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typedef _jobject *jobject;
typedef _jclass *jclass;
typedef jobject jstring;
typedef jobject jbyteArray;
typedef jobject jintArray;
typedef jobject jfloatArray;

struct JNIEnv {};

Expand Down Expand Up @@ -1229,18 +1231,25 @@ extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_keyUp(JNIEnv *, jclass, jin
return NativeKey(keyInput);
}

// TODO: Make a batched interface, since we get these in batches on the Java side.
extern "C" void Java_org_ppsspp_ppsspp_NativeApp_joystickAxis(
JNIEnv *env, jclass, jint deviceId, jint axisId, jfloat value) {
JNIEnv *env, jclass, jint deviceId, jintArray axisIds, jfloatArray values, jint count) {
if (!renderer_inited)
return;

AxisInput *axis = new AxisInput[count];
_dbg_assert_(count <= env->GetArrayLength(axisIds));
_dbg_assert_(count <= env->GetArrayLength(values));
jint *axisIdBuffer = env->GetIntArrayElements(axisIds, NULL);
jfloat *valueBuffer = env->GetFloatArrayElements(values, NULL);

// These are dirty-filtered on the Java side.
AxisInput axis;
axis.deviceId = (InputDeviceID)deviceId;
axis.axisId = (InputAxis)axisId;
axis.value = value;
NativeAxis(&axis, 1);
for (int i = 0; i < count; i++) {
axis[i].deviceId = (InputDeviceID)(int)deviceId;
axis[i].axisId = (InputAxis)(int)axisIdBuffer[i];
axis[i].value = valueBuffer[i];
}
NativeAxis(axis, count);
delete[] axis;
}

extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_mouseWheelEvent(
Expand Down
12 changes: 11 additions & 1 deletion android/src/org/ppsspp/ppsspp/InputDeviceState.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class InputDeviceState {
private int[] mAxes;
private float[] mAxisPrevValue;

// Buffers for the native calls.
private int[] mAxisIds = null;
private float[] mValues = null;

private int sources;

InputDevice getDevice() {
Expand Down Expand Up @@ -125,6 +129,8 @@ public InputDeviceState(InputDevice device) {

mAxes = new int[numAxes];
mAxisPrevValue = new float[numAxes];
mAxisIds = new int[numAxes];
mValues = new float[numAxes];

int i = 0;
for (MotionRange range : device.getMotionRanges()) {
Expand Down Expand Up @@ -157,14 +163,18 @@ public boolean onJoystickMotion(MotionEvent event) {
Log.i(TAG, "Not a joystick event: source = " + event.getSource());
return false;
}
int count = 0;
for (int i = 0; i < mAxes.length; i++) {
int axisId = mAxes[i];
float value = event.getAxisValue(axisId);
if (value != mAxisPrevValue[i]) {
NativeApp.joystickAxis(deviceId, axisId, value);
mAxisIds[count] = axisId;
mValues[count] = value;
count++;
mAxisPrevValue[i] = value;
}
}
NativeApp.joystickAxis(deviceId, mAxisIds, mValues, count);
return true;
}
}
2 changes: 1 addition & 1 deletion android/src/org/ppsspp/ppsspp/NativeApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class NativeApp {
public static native boolean keyDown(int deviceId, int key, boolean isRepeat);
public static native boolean keyUp(int deviceId, int key);

public static native void joystickAxis(int deviceId, int axis, float value);
public static native void joystickAxis(int deviceId, int []axis, float []value, int count);

public static native boolean mouseWheelEvent(float x, float y);

Expand Down
23 changes: 17 additions & 6 deletions android/src/org/ppsspp/ppsspp/NativeGLView.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,23 @@ public void onKeyEvent(KeyEvent event) {
// MOGA Controller - from ControllerListener
@Override
public void onMotionEvent(com.bda.controller.MotionEvent event) {
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_X, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_X));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_Y, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Y));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_Z, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Z));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_RZ, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RZ));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_LTRIGGER, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_LTRIGGER));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_RTRIGGER, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RTRIGGER));
int [] axisIds = new int[]{
com.bda.controller.MotionEvent.AXIS_X,
com.bda.controller.MotionEvent.AXIS_Y,
com.bda.controller.MotionEvent.AXIS_Z,
com.bda.controller.MotionEvent.AXIS_RZ,
com.bda.controller.MotionEvent.AXIS_LTRIGGER,
com.bda.controller.MotionEvent.AXIS_RTRIGGER,
};
float [] values = new float[]{
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_X),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Y),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Z),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RZ),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_LTRIGGER),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RTRIGGER),
};
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, axisIds, values, 6);
}

// MOGA Controller - from ControllerListener
Expand Down
24 changes: 18 additions & 6 deletions android/src/org/ppsspp/ppsspp/NativeSurfaceView.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,24 @@ public void onKeyEvent(KeyEvent event) {
// MOGA Controller - from ControllerListener
@Override
public void onMotionEvent(com.bda.controller.MotionEvent event) {
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_X, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_X));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_Y, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Y));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_Z, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Z));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_RZ, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RZ));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_LTRIGGER, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_LTRIGGER));
NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, com.bda.controller.MotionEvent.AXIS_RTRIGGER, event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RTRIGGER));
int [] axisIds = new int[]{
com.bda.controller.MotionEvent.AXIS_X,
com.bda.controller.MotionEvent.AXIS_Y,
com.bda.controller.MotionEvent.AXIS_Z,
com.bda.controller.MotionEvent.AXIS_RZ,
com.bda.controller.MotionEvent.AXIS_LTRIGGER,
com.bda.controller.MotionEvent.AXIS_RTRIGGER,
};
float [] values = new float[]{
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_X),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Y),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_Z),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RZ),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_LTRIGGER),
event.getAxisValue(com.bda.controller.MotionEvent.AXIS_RTRIGGER),
};

NativeApp.joystickAxis(NativeApp.DEVICE_ID_PAD_0, axisIds, values, 6);
}

// MOGA Controller - from ControllerListener
Expand Down

0 comments on commit 7f65db9

Please sign in to comment.