ControllerInterface/Android: Handle input events

Android doesn't let us poll inputs whenever we want. Instead, we
listen to input events (activities will have to forward them to the
input backend), and store the received values in atomic variables
in the Input classes. This is similar in concept to how ButtonManager
worked, but without its homegrown second input mapping system.
This commit is contained in:
JosJuice
2022-01-01 18:31:50 +01:00
parent 792cb62195
commit ca508e4503
3 changed files with 217 additions and 26 deletions

View File

@ -11,7 +11,6 @@ import android.os.Build;
import android.os.Bundle;
import android.util.Pair;
import android.util.SparseIntArray;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
@ -42,6 +41,7 @@ import org.dolphinemu.dolphinemu.databinding.ActivityEmulationBinding;
import org.dolphinemu.dolphinemu.databinding.DialogInputAdjustBinding;
import org.dolphinemu.dolphinemu.databinding.DialogIrSensitivityBinding;
import org.dolphinemu.dolphinemu.databinding.DialogSkylandersManagerBinding;
import org.dolphinemu.dolphinemu.features.input.model.ControllerInterface;
import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.IntSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
@ -61,7 +61,6 @@ import org.dolphinemu.dolphinemu.overlay.InputOverlayPointer;
import org.dolphinemu.dolphinemu.ui.main.MainPresenter;
import org.dolphinemu.dolphinemu.ui.main.ThemeProvider;
import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner;
import org.dolphinemu.dolphinemu.utils.ControllerMappingHelper;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.IniFile;
import org.dolphinemu.dolphinemu.utils.ThemeHelper;
@ -855,13 +854,15 @@ public final class EmulationActivity extends AppCompatActivity implements ThemeP
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (mMenuVisible || event.getKeyCode() == KeyEvent.KEYCODE_BACK)
if (!mMenuVisible)
{
return super.dispatchKeyEvent(event);
if (ControllerInterface.dispatchKeyEvent(event))
{
return true;
}
}
// TODO
return false;
return super.dispatchKeyEvent(event);
}
private void toggleControls()
@ -1217,13 +1218,15 @@ public final class EmulationActivity extends AppCompatActivity implements ThemeP
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event)
{
if (mMenuVisible)
if (!mMenuVisible)
{
return false;
if (ControllerInterface.dispatchGenericMotionEvent(event))
{
return true;
}
}
// TODO
return false;
return super.dispatchGenericMotionEvent(event);
}
private void showSubMenu(SaveLoadStateFragment.SaveOrLoad saveOrLoad)

View File

@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.input.model;
import android.view.KeyEvent;
import android.view.MotionEvent;
/**
* This class interfaces with the native ControllerInterface,
* which is where the emulator core gets inputs from.
*/
public final class ControllerInterface
{
/**
* Activities which want to pass on inputs to native code
* should call this in their own dispatchKeyEvent method.
*
* @return true if the emulator core seems to be interested in this event.
* false if the event should be passed on to the default dispatchKeyEvent.
*/
public static native boolean dispatchKeyEvent(KeyEvent event);
/**
* Activities which want to pass on inputs to native code
* should call this in their own dispatchGenericMotionEvent method.
*
* @return true if the emulator core seems to be interested in this event.
* false if the event should be passed on to the default dispatchGenericMotionEvent.
*/
public static native boolean dispatchGenericMotionEvent(MotionEvent event);
}