Skip to content
Permalink
Browse files

Fixed bug 3191 - haptic system on android?

Sylvain

- add vibrator service in the list of haptic devices. I use an hard-coded device_id for it ...
  • Loading branch information
slouken committed Aug 14, 2017
1 parent c59d992 commit d03409e118785c0a7135173ff4492a0c9d45799d
Showing with 43 additions and 30 deletions.
  1. +43 −30 android-project/src/org/libsdl/app/SDLActivity.java
@@ -593,23 +593,6 @@ public static boolean isScreenKeyboardShown()
return false;
}



/**
* This method is called by SDL using JNI.
*/
public static int openURL(String url)
{
try {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
mSingleton.startActivity(i);
} catch (Exception ex) {
return -1;
}
return 0;
}

/**
* This method is called by SDL using JNI.
*/
@@ -892,6 +875,24 @@ public static void pollInputDevices() {
}
}

/**
* This method is called by SDL using JNI.
*/
public static void pollHapticDevices() {
if (SDLActivity.mSDLThread != null) {
mHapticHandler.pollHapticDevices();
}
}

/**
* This method is called by SDL using JNI.
*/
public static void hapticRun(int device_id, int length) {
if (SDLActivity.mSDLThread != null) {
mHapticHandler.run(device_id, length);
}
}

// Check if a given device is considered a possible SDL joystick
public static boolean isDeviceSDLJoystick(int deviceId) {
InputDevice device = InputDevice.getDevice(deviceId);
@@ -1808,18 +1809,6 @@ 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) {
@@ -1918,6 +1907,10 @@ public void run(int device_id, int length) {
}

public void pollHapticDevices() {

final int deviceId_VIBRATOR_SERVICE = 999999;
boolean hasVibrator = false;

int[] deviceIds = InputDevice.getDeviceIds();
// It helps processing the device ids in reverse order
// For example, in the case of the XBox 360 wireless dongle,
@@ -1940,6 +1933,23 @@ public void pollHapticDevices() {
}
}

/* Check VIBRATOR_SERVICE */
{
Vibrator vib = (Vibrator) SDLActivity.mSingleton.getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (vib != null && vib.hasVibrator()) {
hasVibrator = true;
SDLHaptic haptic = getHaptic(deviceId_VIBRATOR_SERVICE);
if (haptic == null) {
haptic = new SDLHaptic();
haptic.device_id = deviceId_VIBRATOR_SERVICE;
haptic.name = "VIBRATOR_SERVICE";
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++) {
@@ -1948,7 +1958,10 @@ public void pollHapticDevices() {
for (j=0; j < deviceIds.length; j++) {
if (device_id == deviceIds[j]) break;
}
if (j == deviceIds.length) {

if (device_id == deviceId_VIBRATOR_SERVICE && hasVibrator) {
// don't remove the vibrator if it is still present
} else if (j == deviceIds.length) {
removedDevices.add(device_id);
}
}

0 comments on commit d03409e

Please sign in to comment.