|
@@ -61,6 +61,7 @@ |
|
|
protected static View mTextEdit; |
|
|
protected static ViewGroup mLayout; |
|
|
protected static SDLJoystickHandler mJoystickHandler; |
|
|
protected static SDLHapticHandler mHapticHandler; |
|
|
|
|
|
// This is what SDL runs in. It invokes SDL_main(), eventually |
|
|
protected static Thread mSDLThread; |
|
@@ -113,6 +114,7 @@ public static void initialize() { |
|
|
mTextEdit = null; |
|
|
mLayout = null; |
|
|
mJoystickHandler = null; |
|
|
mHapticHandler = null; |
|
|
mSDLThread = null; |
|
|
mAudioTrack = null; |
|
|
mAudioRecord = null; |
|
@@ -182,6 +184,7 @@ public void onClick(DialogInterface dialog,int id) { |
|
|
else { |
|
|
mJoystickHandler = new SDLJoystickHandler(); |
|
|
} |
|
|
mHapticHandler = new SDLHapticHandler(); |
|
|
|
|
|
mLayout = new RelativeLayout(this); |
|
|
mLayout.addView(mSurface); |
|
@@ -498,6 +501,8 @@ public static native int nativeAddJoystick(int device_id, String name, |
|
|
int is_accelerometer, int nbuttons, |
|
|
int naxes, int nhats, int nballs); |
|
|
public static native int nativeRemoveJoystick(int device_id); |
|
|
public static native int nativeAddHaptic(int device_id, String name); |
|
|
public static native int nativeRemoveHaptic(int device_id); |
|
|
public static native String nativeGetHint(String name); |
|
|
|
|
|
/** |
|
@@ -1704,6 +1709,18 @@ protected SDLJoystick getJoystick(int device_id) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
public static void pollHapticDevices() { |
|
|
if (SDLActivity.mSDLThread != null) { |
|
|
mHapticHandler.pollHapticDevices(); |
|
|
} |
|
|
} |
|
|
|
|
|
public static void hapticRun(int device_id, int length) { |
|
|
if (SDLActivity.mSDLThread != null) { |
|
|
mHapticHandler.run(device_id, length); |
|
|
} |
|
|
} |
|
|
|
|
|
@Override |
|
|
public boolean handleMotionEvent(MotionEvent event) { |
|
|
if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) { |
|
@@ -1779,3 +1796,82 @@ public boolean onGenericMotion(View v, MotionEvent event) { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
class SDLHapticHandler { |
|
|
|
|
|
class SDLHaptic { |
|
|
public int device_id; |
|
|
public String name; |
|
|
public Vibrator vib; |
|
|
} |
|
|
|
|
|
private ArrayList<SDLHaptic> mHaptics; |
|
|
|
|
|
public SDLHapticHandler() { |
|
|
mHaptics = new ArrayList<SDLHaptic>(); |
|
|
} |
|
|
|
|
|
public void run(int device_id, int length) { |
|
|
SDLHaptic haptic = getHaptic(device_id); |
|
|
if (haptic != null) { |
|
|
haptic.vib.vibrate (length); |
|
|
} |
|
|
} |
|
|
|
|
|
public void pollHapticDevices() { |
|
|
int[] deviceIds = InputDevice.getDeviceIds(); |
|
|
// It helps processing the device ids in reverse order |
|
|
// For example, in the case of the XBox 360 wireless dongle, |
|
|
// so the first controller seen by SDL matches what the receiver |
|
|
// considers to be the first controller |
|
|
|
|
|
for(int i=deviceIds.length-1; i>-1; i--) { |
|
|
SDLHaptic haptic = getHaptic(deviceIds[i]); |
|
|
if (haptic == null) { |
|
|
InputDevice device = InputDevice.getDevice(deviceIds[i]); |
|
|
Vibrator vib = device.getVibrator (); |
|
|
if(vib.hasVibrator ()) { |
|
|
haptic = new SDLHaptic(); |
|
|
haptic.device_id = deviceIds[i]; |
|
|
haptic.name = device.getName(); |
|
|
haptic.vib = vib; |
|
|
mHaptics.add(haptic); |
|
|
SDLActivity.nativeAddHaptic(haptic.device_id, haptic.name); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
/* Check removed devices */ |
|
|
ArrayList<Integer> removedDevices = new ArrayList<Integer>(); |
|
|
for(int i=0; i < mHaptics.size(); i++) { |
|
|
int device_id = mHaptics.get(i).device_id; |
|
|
int j; |
|
|
for (j=0; j < deviceIds.length; j++) { |
|
|
if (device_id == deviceIds[j]) break; |
|
|
} |
|
|
if (j == deviceIds.length) { |
|
|
removedDevices.add(device_id); |
|
|
} |
|
|
} |
|
|
|
|
|
for(int i=0; i < removedDevices.size(); i++) { |
|
|
int device_id = removedDevices.get(i); |
|
|
SDLActivity.nativeRemoveHaptic(device_id); |
|
|
for (int j=0; j < mHaptics.size(); j++) { |
|
|
if (mHaptics.get(j).device_id == device_id) { |
|
|
mHaptics.remove(j); |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
protected SDLHaptic getHaptic(int device_id) { |
|
|
for(int i=0; i < mHaptics.size(); i++) { |
|
|
if (mHaptics.get(i).device_id == device_id) { |
|
|
return mHaptics.get(i); |
|
|
} |
|
|
} |
|
|
return null; |
|
|
} |
|
|
} |