ControllerInterface/Android: Return whether input was handled

When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.

It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.

What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:

1. If a controller is not being polled (e.g. GameCube controllers in
   Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
   we will be able to use this "polled recently" tracking to power down
   the sensors at times when the game is using a Wii Remote reporting
   mode that doesn't include motion data. (Assuming that the sensor
   inputs only are mapped to Wii Remote motion controls, that is.)
This commit is contained in:
JosJuice 2022-03-06 11:51:59 +01:00
parent ca508e4503
commit d6af294a23

View file

@ -3,7 +3,9 @@
#include "InputCommon/ControllerInterface/Android/Android.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <memory>
#include <string>
#include <unordered_map>
@ -58,6 +60,9 @@ jmethodID s_motion_event_get_source;
jintArray s_keycodes_array;
using Clock = std::chrono::steady_clock;
constexpr Clock::duration ACTIVE_INPUT_TIMEOUT = std::chrono::milliseconds(1000);
std::unordered_map<jint, ciface::Core::DeviceQualifier> s_device_id_to_device_qualifier;
constexpr int MAX_KEYCODE = AKEYCODE_PROFILE_SWITCH; // Up to date as of SDK 31
@ -429,13 +434,20 @@ public:
std::string GetName() const override { return m_name; }
ControlState GetState() const override { return m_state.load(std::memory_order_relaxed); }
ControlState GetState() const override
{
m_last_polled.store(Clock::now(), std::memory_order_relaxed);
return m_state.load(std::memory_order_relaxed);
}
void SetState(ControlState state) { m_state.store(state, std::memory_order_relaxed); }
Clock::time_point GetLastPolled() const { return m_last_polled.load(std::memory_order_relaxed); }
private:
std::string m_name;
std::atomic<ControlState> m_state = 0;
mutable std::atomic<Clock::time_point> m_last_polled{};
};
class AndroidKey final : public AndroidInput
@ -718,13 +730,14 @@ Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatch
return false;
}
static_cast<ciface::Android::AndroidInput*>(input)->SetState(state);
auto casted_input = static_cast<ciface::Android::AndroidInput*>(input);
casted_input->SetState(state);
const Clock::time_point last_polled = casted_input->GetLastPolled();
DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Set {} of {} to {}", input_name, device->GetQualifiedName(),
state);
// TODO: Return true when appropriate
return JNI_FALSE;
return last_polled >= Clock::now() - ACTIVE_INPUT_TIMEOUT;
}
JNIEXPORT jboolean JNICALL
@ -741,6 +754,8 @@ Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatch
const jint source = env->CallIntMethod(motion_event, s_motion_event_get_source);
const std::string axis_name_prefix = ConstructAxisNamePrefix(source);
Clock::time_point last_polled{};
for (ciface::Core::Device::Input* input : device->Inputs())
{
const std::string input_name = input->GetName();
@ -758,14 +773,16 @@ Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatch
}
float value = env->CallFloatMethod(motion_event, s_motion_event_get_axis_value, axis_id);
static_cast<ciface::Android::AndroidInput*>(input)->SetState(value);
auto casted_input = static_cast<ciface::Android::AndroidInput*>(input);
casted_input->SetState(value);
last_polled = std::max(last_polled, casted_input->GetLastPolled());
DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Set {} of {} to {}", input_name,
device->GetQualifiedName(), value);
}
}
// TODO: Return true when appropriate
return JNI_FALSE;
return last_polled >= Clock::now() - ACTIVE_INPUT_TIMEOUT;
}
}