mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
ControllerInterface/Android: Implement hotplug
This commit is contained in:
@ -2,15 +2,50 @@
|
||||
|
||||
package org.dolphinemu.dolphinemu.features.input.model;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.input.InputManager;
|
||||
import android.os.Handler;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import androidx.annotation.Keep;
|
||||
|
||||
import org.dolphinemu.dolphinemu.DolphinApplication;
|
||||
import org.dolphinemu.dolphinemu.utils.LooperThread;
|
||||
|
||||
/**
|
||||
* This class interfaces with the native ControllerInterface,
|
||||
* which is where the emulator core gets inputs from.
|
||||
*/
|
||||
public final class ControllerInterface
|
||||
{
|
||||
private static final class InputDeviceListener implements InputManager.InputDeviceListener
|
||||
{
|
||||
@Override
|
||||
public void onInputDeviceAdded(int deviceId)
|
||||
{
|
||||
// Simple implementation for now. We could do something fancier if we wanted to.
|
||||
refreshDevices();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputDeviceRemoved(int deviceId)
|
||||
{
|
||||
// Simple implementation for now. We could do something fancier if we wanted to.
|
||||
refreshDevices();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputDeviceChanged(int deviceId)
|
||||
{
|
||||
// Simple implementation for now. We could do something fancier if we wanted to.
|
||||
refreshDevices();
|
||||
}
|
||||
}
|
||||
|
||||
private static InputDeviceListener mInputDeviceListener;
|
||||
private static LooperThread mLooperThread;
|
||||
|
||||
/**
|
||||
* Activities which want to pass on inputs to native code
|
||||
* should call this in their own dispatchKeyEvent method.
|
||||
@ -28,4 +63,41 @@ public final class ControllerInterface
|
||||
* false if the event should be passed on to the default dispatchGenericMotionEvent.
|
||||
*/
|
||||
public static native boolean dispatchGenericMotionEvent(MotionEvent event);
|
||||
|
||||
/**
|
||||
* Rescans for input devices.
|
||||
*/
|
||||
public static native void refreshDevices();
|
||||
|
||||
@Keep
|
||||
private static void registerInputDeviceListener()
|
||||
{
|
||||
if (mLooperThread == null)
|
||||
{
|
||||
mLooperThread = new LooperThread("Hotplug thread");
|
||||
mLooperThread.start();
|
||||
}
|
||||
|
||||
if (mInputDeviceListener == null)
|
||||
{
|
||||
InputManager im = (InputManager)
|
||||
DolphinApplication.getAppContext().getSystemService(Context.INPUT_SERVICE);
|
||||
|
||||
mInputDeviceListener = new InputDeviceListener();
|
||||
im.registerInputDeviceListener(mInputDeviceListener, new Handler(mLooperThread.getLooper()));
|
||||
}
|
||||
}
|
||||
|
||||
@Keep
|
||||
private static void unregisterInputDeviceListener()
|
||||
{
|
||||
if (mInputDeviceListener != null)
|
||||
{
|
||||
InputManager im = (InputManager)
|
||||
DolphinApplication.getAppContext().getSystemService(Context.INPUT_SERVICE);
|
||||
|
||||
im.unregisterInputDeviceListener(mInputDeviceListener);
|
||||
mInputDeviceListener = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,58 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
package org.dolphinemu.dolphinemu.utils;
|
||||
|
||||
import android.os.Looper;
|
||||
|
||||
public class LooperThread extends Thread
|
||||
{
|
||||
private Looper mLooper;
|
||||
|
||||
public LooperThread()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public LooperThread(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
Looper.prepare();
|
||||
|
||||
synchronized (this)
|
||||
{
|
||||
mLooper = Looper.myLooper();
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
Looper.loop();
|
||||
}
|
||||
|
||||
public Looper getLooper()
|
||||
{
|
||||
if (!isAlive())
|
||||
{
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
synchronized (this)
|
||||
{
|
||||
while (mLooper == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
wait();
|
||||
}
|
||||
catch (InterruptedException ignored)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mLooper;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user