LWJGL2 experimental controller support

This commit is contained in:
artdeell 2021-01-03 17:33:57 +03:00
parent afed2315af
commit 2a93ef6b07
7 changed files with 709 additions and 2 deletions

View file

@ -13,6 +13,8 @@ import androidx.drawerlayout.widget.*;
import com.google.android.material.navigation.*;
import java.io.*;
import java.lang.reflect.*;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.*;
import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.utils.*;
@ -754,6 +756,54 @@ public class BaseMainActivity extends LoggableActivity {
return super.dispatchKeyEvent(event);
}
*/
@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
if(ev.getSource() == InputDevice.SOURCE_CLASS_JOYSTICK) {
CallbackBridge.nativePutControllerAxes((FloatBuffer)FloatBuffer.allocate(8)
.put(ev.getAxisValue(MotionEvent.AXIS_X))
.put(ev.getAxisValue(MotionEvent.AXIS_Y))
.put(ev.getAxisValue(MotionEvent.AXIS_Z))
.put(ev.getAxisValue(MotionEvent.AXIS_RX))
.put(ev.getAxisValue(MotionEvent.AXIS_RY))
.put(ev.getAxisValue(MotionEvent.AXIS_RZ))
.put(ev.getAxisValue(MotionEvent.AXIS_HAT_X))
.put(ev.getAxisValue(MotionEvent.AXIS_HAT_Y))
.flip());
return true;//consume the cum chalice
}else{
return false;
}
}
byte[] kevArray = new byte[8];
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getSource() == InputDevice.SOURCE_CLASS_JOYSTICK) {
switch(event.getKeyCode()) {
case KeyEvent.KEYCODE_BUTTON_A:
kevArray[0]= (byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
case KeyEvent.KEYCODE_BUTTON_B:
kevArray[1]=(byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
case KeyEvent.KEYCODE_BUTTON_X:
kevArray[2]=(byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
case KeyEvent.KEYCODE_BUTTON_Y:
kevArray[3]=(byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
case KeyEvent.KEYCODE_DPAD_LEFT:
kevArray[4]=(byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
case KeyEvent.KEYCODE_DPAD_RIGHT:
kevArray[5]=(byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
case KeyEvent.KEYCODE_DPAD_UP:
kevArray[6]=(byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
case KeyEvent.KEYCODE_DPAD_DOWN:
kevArray[7]=(byte) ((event.getAction() == KeyEvent.ACTION_DOWN)?1:0);
}
CallbackBridge.nativePutControllerButtons(ByteBuffer.wrap(kevArray));
return true;
}else{
return false;
}
}
//private Dialog menuDial;
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {

View file

@ -1,5 +1,7 @@
package org.lwjgl.glfw;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.*;
import android.widget.*;
import net.kdt.pojavlaunch.*;
@ -164,7 +166,8 @@ public class CallbackBridge {
private static native void nativeSendScreenSize(int width, int height);
public static native boolean nativeIsGrabbing();
public static native void nativePutControllerAxes(FloatBuffer axBuf);
public static native void nativePutControllerButtons(ByteBuffer axBuf);
static {
System.loadLibrary("pojavexec");
}

View file

@ -353,4 +353,43 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendScroll(JNIEn
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_GLFW_nglfwSetShowingWindow(JNIEnv* env, jclass clazz, jlong window) {
showingWindow = (long) window;
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_CallbackBridge_nativePutControllerAxes(JNIEnv *env, jclass clazz,
jobject ax_buf) {
// TODO: implement nativePutControllerAxes()
if(isInputReady) {
jbyte *src = (jbyte *)((*env)->GetDirectBufferAddress(*env,ax_buf));
jclass glfw_joystick_class = (*runtimeJNIEnvPtr_ANDROID)->FindClass(runtimeJNIEnvPtr_ANDROID,"org/lwjgl/glfw/GLFW");
if(glfw_joystick_class == NULL) {
__android_log_print(ANDROID_LOG_ERROR,"ControllerPipeNative","GLFW is not attached!");
return;
}
jfieldID glfw_controller_axis_data = (*runtimeJNIEnvPtr_ANDROID)->GetStaticFieldID(runtimeJNIEnvPtr_ANDROID,glfw_joystick_class,"joystickData",
"Ljava/nio/FloatBuffer;");
if(glfw_controller_axis_data == NULL) {
__android_log_print(ANDROID_LOG_ERROR,"ControllerPipeNative","Unable to find the field!");
return;
}
(*runtimeJNIEnvPtr_ANDROID)->SetStaticObjectField(runtimeJNIEnvPtr_ANDROID,glfw_joystick_class,glfw_controller_axis_data,(*runtimeJNIEnvPtr_ANDROID)->NewDirectByteBuffer(runtimeJNIEnvPtr_ANDROID,src,(*env)->GetDirectBufferCapacity(env,ax_buf)));
}
}
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_CallbackBridge_nativePutControllerButtons(JNIEnv *env, jclass clazz,
jobject ax_buf) {
// TODO: implement nativePutControllerButtons()
if(isInputReady) {
jbyte *src = (jbyte *)((*env)->GetDirectBufferAddress(*env,ax_buf));
jclass glfw_joystick_class = (*runtimeJNIEnvPtr_ANDROID)->FindClass(runtimeJNIEnvPtr_ANDROID,"org/lwjgl/glfw/GLFW");
if(glfw_joystick_class == NULL) {
__android_log_print(ANDROID_LOG_ERROR,"ControllerPipeNative","GLFW is not attached!");
return;
}
jfieldID glfw_controller_button_data = (*runtimeJNIEnvPtr_ANDROID)->GetStaticFieldID(runtimeJNIEnvPtr_ANDROID,glfw_joystick_class,"buttonData",
"Ljava/nio/ByteBuffer;");
if(glfw_controller_button_data == NULL) {
__android_log_print(ANDROID_LOG_ERROR,"ControllerPipeNative","Unable to find the field!");
return;
}
(*runtimeJNIEnvPtr_ANDROID)->SetStaticObjectField(runtimeJNIEnvPtr_ANDROID,glfw_joystick_class,glfw_controller_button_data,(*runtimeJNIEnvPtr_ANDROID)->NewDirectByteBuffer(runtimeJNIEnvPtr_ANDROID,src,(*env)->GetDirectBufferCapacity(env,ax_buf)));
}
}

View file

@ -24,6 +24,8 @@ import java.util.*;
public class GLFW
{
static FloatBuffer joystickData;
static ByteBuffer buttonData;
/** The major version number of the GLFW library. This is incremented when the API is changed in non-compatible ways. */
public static final int GLFW_VERSION_MAJOR = 3;
@ -1221,4 +1223,28 @@ public class GLFW
public static String glfwGetClipboardString(@NativeType("GLFWwindow *") long window) {
return CallbackBridge.nativeClipboard(CallbackBridge.CLIPBOARD_PASTE, null);
}
public static boolean glfwJoystickPresent(int jid) {
if(jid == 0) {
return true;
}else return false;
}
public static String glfwGetJoystickName(int jid) {
if(jid == 0) {
return "AIC event bus controller";
}else return null;
}
public static FloatBuffer glfwGetJoystickAxes(int jid) {
if(jid == 0) {
return joystickData;
}else return null;
}
public static ByteBuffer glfwGetJoystickButtons(int jid) {
if(jid == 0) {
return buttonData;
}else return null;
}
public static ByteBuffer glfwGetjoystickHats(int jid) {
return null;
}
}

View file

@ -0,0 +1,290 @@
/*
* Copyright (c) 2002-2008 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.input;
/**
* A game controller of some sort that will provide input. The controller
* presents buttons and axes. Buttons are either pressed or not pressed. Axis
* provide analogue values.
*
* @author Kevin Glass
*/
public interface Controller {
/**
* Get the name assigned to this controller.
*
* @return The name assigned to this controller
*/
String getName();
/**
* Get the index of this controller in the collection
*
* @return The index of this controller in the collection
*/
int getIndex();
/**
* Retrieve the number of buttons available on this controller
*
* @return The number of butotns available on this controller
*/
int getButtonCount();
/**
* Get the name of the specified button. Be warned, often this is
* as exciting as "Button X"
*
* @param index The index of the button whose name should be retrieved
* @return The name of the button requested
*/
String getButtonName(int index);
/**
* Check if a button is currently pressed
*
* @param index The button to check
* @return True if the button is currently pressed
*/
boolean isButtonPressed(int index);
/**
* Poll the controller for new data. This will also update
* events
*/
void poll();
/**
* Get the X-Axis value of the POV on this controller
*
* @return The X-Axis value of the POV on this controller
*/
float getPovX();
/**
* Get the Y-Axis value of the POV on this controller
*
* @return The Y-Axis value of the POV on this controller
*/
float getPovY();
/**
* Get the dead zone for a specified axis
*
* @param index The index of the axis for which to retrieve the dead zone
* @return The dead zone for the specified axis
*/
float getDeadZone(int index);
/**
* Set the dead zone for the specified axis
*
* @param index The index of hte axis for which to set the dead zone
* @param zone The dead zone to use for the specified axis
*/
void setDeadZone(int index,float zone);
/**
* Retrieve the number of axes available on this controller.
*
* @return The number of axes available on this controller.
*/
int getAxisCount();
/**
* Get the name that's given to the specified axis
*
* @param index The index of the axis whose name should be retrieved
* @return The name of the specified axis.
*/
String getAxisName(int index);
/**
* Retrieve the value thats currently available on a specified axis. The
* value will always be between 1.0 and -1.0 and will calibrate as values
* are passed read. It may be useful to get the player to wiggle the joystick
* from side to side to get the calibration right.
*
* @param index The index of axis to be read
* @return The value from the specified axis.
*/
float getAxisValue(int index);
/**
* Get the value from the X axis if there is one. If no X axis is
* defined a zero value will be returned.
*
* @return The value from the X axis
*/
float getXAxisValue();
/**
* Get the dead zone for the X axis.
*
* @return The dead zone for the X axis
*/
float getXAxisDeadZone();
/**
* Set the dead zone for the X axis
*
* @param zone The dead zone to use for the X axis
*/
void setXAxisDeadZone(float zone);
/**
* Get the value from the Y axis if there is one. If no Y axis is
* defined a zero value will be returned.
*
* @return The value from the Y axis
*/
float getYAxisValue();
/**
* Get the dead zone for the Y axis.
*
* @return The dead zone for the Y axis
*/
float getYAxisDeadZone();
/**
* Set the dead zone for the Y axis
*
* @param zone The dead zone to use for the Y axis
*/
void setYAxisDeadZone(float zone);
/**
* Get the value from the Z axis if there is one. If no Z axis is
* defined a zero value will be returned.
*
* @return The value from the Z axis
*/
float getZAxisValue();
/**
* Get the dead zone for the Z axis.
*
* @return The dead zone for the Z axis
*/
float getZAxisDeadZone();
/**
* Set the dead zone for the Z axis
*
* @param zone The dead zone to use for the Z axis
*/
void setZAxisDeadZone(float zone);
/**
* Get the value from the RX axis if there is one. If no RX axis is
* defined a zero value will be returned.
*
* @return The value from the RX axis
*/
float getRXAxisValue();
/**
* Get the dead zone for the RX axis.
*
* @return The dead zone for the RX axis
*/
float getRXAxisDeadZone();
/**
* Set the dead zone for the RX axis
*
* @param zone The dead zone to use for the RX axis
*/
void setRXAxisDeadZone(float zone);
/**
* Get the value from the RY axis if there is one. If no RY axis is
* defined a zero value will be returned.
*
* @return The value from the RY axis
*/
float getRYAxisValue();
/**
* Get the dead zone for the RY axis.
*
* @return The dead zone for the RY axis
*/
float getRYAxisDeadZone();
/**
* Set the dead zone for the RY axis
*
* @param zone The dead zone to use for the RY axis
*/
void setRYAxisDeadZone(float zone);
/**
* Get the value from the RZ axis if there is one. If no RZ axis is
* defined a zero value will be returned.
*
* @return The value from the RZ axis
*/
float getRZAxisValue();
/**
* Get the dead zone for the RZ axis.
*
* @return The dead zone for the RZ axis
*/
float getRZAxisDeadZone();
/**
* Set the dead zone for the RZ axis
*
* @param zone The dead zone to use for the RZ axis
*/
void setRZAxisDeadZone(float zone);
/** Returns the number of rumblers this controller supports */
int getRumblerCount();
/** Returns the name of the specified rumbler
*
* @param index The rumbler index
*/
String getRumblerName(int index);
/** Sets the vibration strength of the specified rumbler
*
* @param index The index of the rumbler
* @param strength The strength to vibrate at
*/
void setRumblerStrength(int index, float strength);
}

View file

@ -0,0 +1,78 @@
package org.lwjgl.input;
import org.lwjgl.Sys;
import org.lwjgl.glfw.GLFWJoystickCallback;
public class Controllers {
static GLFWController ctrlr;
public static void create() {
ctrlr = new GLFWController();
ctrlr.jid = 0;
}
public static Controller getController() {
return ctrlr;
}
public static int getControllerCount() {
return 1;
}
public static void poll() {
ctrlr.poll();
}
public static boolean next() {
ctrlr.poll();
return true;
}
public static boolean isCreated() {
return true;
}
public static void destroy() {
}
public static Controller getEventSource() {
return ctrlr;
}
public static int getEventControlIndex() {
return 0;
}
public static boolean isEventButton() {
return true;
}
public static boolean isEventAxis() {
return true;
}
public static boolean isEventXAxis() {
return true;
}
public static boolean isEventYAxis() {
return true;
}
public static boolean isEventPovX() {
return true;
}
public static boolean isEventPovY() {
return true;
}
public static long getEventNanoseconds() {
return Sys.getNanoTime();
}
public static boolean getEventButtonState() {
return true;
}
public static float getEventXAxisValue() {
return ctrlr.getXAxisValue();
}
public static float getEventYAxisValue() {
return ctrlr.getYAxisValue();
}
}

View file

@ -0,0 +1,221 @@
package org.lwjgl.input;
import org.lwjgl.glfw.GLFW;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
public class GLFWController implements Controller{
int jid;
FloatBuffer axisData;
ByteBuffer buttonData;
@Override
public String getName() {
return GLFW.glfwGetJoystickName(jid);
}
@Override
public int getIndex() {
return jid;
}
@Override
public int getButtonCount() {
return 0;
}
@Override
public String getButtonName(int index) {
switch(index) {
case 0:
return "A";
case 1:
return "B";
case 2:
return "X";
case 3:
return "Y";
case 4:
return "DPAD LEFT";
case 5:
return "DPAD RIGHT";
case 6:
return "DPAD UP";
case 7:
return "DPAD DOWN";
default:
return null;
}
}
@Override
public boolean isButtonPressed(int index) {
return (buttonData.get(index) == 1);
}
@Override
public void poll() {
axisData = GLFW.glfwGetJoystickAxes(jid);
buttonData = GLFW.glfwGetJoystickButtons(jid);
}
@Override
public float getPovX() {
return axisData.get(6);
}
@Override
public float getPovY() {
return axisData.get(7);
}
@Override
public float getDeadZone(int index) {
return 0;
}
@Override
public void setDeadZone(int index, float zone) {
}
@Override
public int getAxisCount() {
return 0;
}
@Override
public String getAxisName(int index) {
switch(index) {
case 0:
return "AXIS X";
case 1:
return "AXIS Y";
case 2:
return "AXIS Z";
case 3:
return "AXIS RX";
case 4:
return "AXIS RY";
case 5:
return "AXIS RZ";
case 6:
return "HAT/POV X";
case 7:
return "HAT/POV Y";
default:
return null;
}
}
@Override
public float getAxisValue(int index) {
return axisData.get(index);
}
@Override
public float getXAxisValue() {
return axisData.get(0);
}
@Override
public float getXAxisDeadZone() {
return 0;
}
@Override
public void setXAxisDeadZone(float zone) {
}
@Override
public float getYAxisValue() {
return axisData.get(1);
}
@Override
public float getYAxisDeadZone() {
return 0;
}
@Override
public void setYAxisDeadZone(float zone) {
}
@Override
public float getZAxisValue() {
return axisData.get(2);
}
@Override
public float getZAxisDeadZone() {
return 0;
}
@Override
public void setZAxisDeadZone(float zone) {
}
@Override
public float getRXAxisValue() {
return axisData.get(3);
}
@Override
public float getRXAxisDeadZone() {
return 0;
}
@Override
public void setRXAxisDeadZone(float zone) {
}
@Override
public float getRYAxisValue() {
return axisData.get(4);
}
@Override
public float getRYAxisDeadZone() {
return 0;
}
@Override
public void setRYAxisDeadZone(float zone) {
}
@Override
public float getRZAxisValue() {
return axisData.get(5);
}
@Override
public float getRZAxisDeadZone() {
return 0;
}
@Override
public void setRZAxisDeadZone(float zone) {
}
@Override
public int getRumblerCount() {
return 0;
}
@Override
public String getRumblerName(int index) {
return null;
}
@Override
public void setRumblerStrength(int index, float strength) {
}
}