Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1538 from reicast/fh/threaded-haptic
android: run the vibrator in a separate thread
  • Loading branch information
skmp committed Apr 4, 2019
2 parents 4dca8a2 + cc26e25 commit 5009cab
Showing 1 changed file with 55 additions and 4 deletions.
Expand Up @@ -15,7 +15,7 @@
import com.reicast.emulator.periph.VJoy;

public class VirtualJoystickDelegate {
private Vibrator vib;
private VibratorThread vibratorThread;

private boolean editVjoyMode = false;
private int selectedVjoyElement = -1;
Expand All @@ -39,7 +39,10 @@ public void run() {
public VirtualJoystickDelegate(View view) {
this.view = view;
this.context = view.getContext();
vib = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);

vibratorThread = new VibratorThread(context);
vibratorThread.start();

readCustomVjoyValues();
scaleGestureDetector = new ScaleGestureDetector(context, new OscOnScaleGestureListener());
}
Expand Down Expand Up @@ -224,8 +227,9 @@ public boolean onTouchEvent(MotionEvent event, int width, int height)
if (y > vjoy[j][1] && y <= (vjoy[j][1] + vjoy[j][3])) {
if (vjoy[j][4] >= -2) {
if (vjoy[j][5] == 0)
if (!editVjoyMode && Emulator.vibrationDuration > 0)
vib.vibrate(Emulator.vibrationDuration);
if (!editVjoyMode) {
vibratorThread.vibrate();
}
vjoy[j][5] = 2;
}

Expand Down Expand Up @@ -397,4 +401,51 @@ public void onScaleEnd(ScaleGestureDetector detector) {
selectedVjoyElement = -1;
}
}

private class VibratorThread extends Thread
{
private Vibrator vibrator;
private boolean vibrate = false;
private boolean stopping = false;

VibratorThread(Context context) {
vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
}

@Override
public void run() {
while (!stopping) {
boolean doVibrate;
synchronized (this) {
doVibrate = false;
try {
this.wait();
} catch (InterruptedException e) {
}
if (vibrate) {
doVibrate = true;
vibrate = false;
}
}
if (doVibrate)
vibrator.vibrate(Emulator.vibrationDuration);
}
}

public void stopVibrator() {
synchronized (this) {
stopping = true;
notify();
}
}

public void vibrate() {
if (Emulator.vibrationDuration > 0) {
synchronized (this) {
vibrate = true;
notify();
}
}
}
}
}

0 comments on commit 5009cab

Please sign in to comment.