Skip to content

Commit

Permalink
InputCommon/ControllerEmu: Break out functionality of EmulatedController
Browse files Browse the repository at this point in the history
to eliminate redundant unused members in Wii Remote extension objects.
  • Loading branch information
jordan-woyak committed Feb 2, 2025
1 parent 84d28a4 commit 4a754d0
Show file tree
Hide file tree
Showing 27 changed files with 316 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ package org.dolphinemu.dolphinemu.features.input.model.controlleremu

import androidx.annotation.Keep

/**
* Represents a C++ ControllerEmu::ControlGroupContainer.
*
* The lifetime of this class is managed by C++ code. Calling methods on it after it's destroyed
* in C++ is undefined behavior!
*/
@Keep
open class ControlGroupContainer constructor(private val pointer: Long) {
external fun getGroupCount(): Int

external fun getGroup(index: Int): ControlGroup
}

/**
* Represents a C++ ControllerEmu::EmulatedController.
*
* The lifetime of this class is managed by C++ code. Calling methods on it after it's destroyed
* in C++ is undefined behavior!
*/
@Keep
class EmulatedController private constructor(private val pointer: Long) {
class EmulatedController private constructor(private val pointer: Long) : ControlGroupContainer(pointer) {
external fun getDefaultDevice(): String

external fun setDefaultDevice(device: String)

external fun getGroupCount(): Int

external fun getGroup(index: Int): ControlGroup

external fun updateSingleControlReference(controlReference: ControlReference)

external fun loadDefaultSettings()
Expand Down Expand Up @@ -50,7 +59,7 @@ class EmulatedController private constructor(private val pointer: Long) {
external fun getWiimoteAttachment(
controllerIndex: Int,
attachmentIndex: Int
): EmulatedController
): ControlGroupContainer

@JvmStatic
external fun getSelectedWiimoteAttachment(controllerIndex: Int): Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package org.dolphinemu.dolphinemu.features.input.model.view

import android.content.Context
import org.dolphinemu.dolphinemu.features.input.model.ControllerInterface
import org.dolphinemu.dolphinemu.features.input.model.controlleremu.ControlGroupContainer
import org.dolphinemu.dolphinemu.features.input.model.controlleremu.EmulatedController
import org.dolphinemu.dolphinemu.features.settings.model.Settings
import org.dolphinemu.dolphinemu.features.settings.model.view.StringSingleChoiceSetting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.dolphinemu.dolphinemu.features.input.model.InputMappingBooleanSetting
import org.dolphinemu.dolphinemu.features.input.model.InputMappingDoubleSetting
import org.dolphinemu.dolphinemu.features.input.model.InputMappingIntSetting
import org.dolphinemu.dolphinemu.features.input.model.controlleremu.ControlGroup
import org.dolphinemu.dolphinemu.features.input.model.controlleremu.ControlGroupContainer
import org.dolphinemu.dolphinemu.features.input.model.controlleremu.EmulatedController
import org.dolphinemu.dolphinemu.features.input.model.controlleremu.NumericSetting
import org.dolphinemu.dolphinemu.features.input.model.view.InputDeviceSetting
Expand Down Expand Up @@ -2255,8 +2256,9 @@ class SettingsFragmentPresenter(
wiimoteNumber: Int,
extensionType: Int
) {
addControllerMappingSettings(
addContainerMappingSettings(
sl,
EmulatedController.getWiimote(wiimoteNumber),
EmulatedController.getWiimoteAttachment(wiimoteNumber, extensionType),
null
)
Expand Down Expand Up @@ -2404,15 +2406,32 @@ class SettingsFragmentPresenter(
* @param groupTypeFilter If this is non-null, only groups whose types match this are considered.
*/
private fun addControllerMappingSettings(
sl: ArrayList<SettingsItem>,
controller: EmulatedController,
groupTypeFilter: Set<Int>?
) {
addContainerMappingSettings(sl, controller, controller, groupTypeFilter)
}

/**
* Adds mapping settings and other control-specific settings.
*
* @param sl The list to place controller settings into.
* @param controller The encompassing controller.
* @param container The container of control groups to add settings for.
* @param groupTypeFilter If this is non-null, only groups whose types match this are considered.
*/
private fun addContainerMappingSettings(
sl: ArrayList<SettingsItem>,
controller: EmulatedController,
container: ControlGroupContainer,
groupTypeFilter: Set<Int>?
) {
updateOldControllerSettingsWarningVisibility(controller)

val groupCount = controller.getGroupCount()
val groupCount = container.getGroupCount()
for (i in 0 until groupCount) {
val group = controller.getGroup(i)
val group = container.getGroup(i)
val groupType = group.getGroupType()
if (groupTypeFilter != null && !groupTypeFilter.contains(groupType)) continue

Expand Down
29 changes: 29 additions & 0 deletions Source/Android/jni/AndroidCommon/IDCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ static jclass s_control_reference_class;
static jfieldID s_control_reference_pointer;
static jmethodID s_control_reference_constructor;

static jclass s_control_group_container_class;
static jfieldID s_control_group_container_pointer;
static jmethodID s_control_group_container_constructor;

static jclass s_emulated_controller_class;
static jfieldID s_emulated_controller_pointer;
static jmethodID s_emulated_controller_constructor;
Expand Down Expand Up @@ -452,6 +456,21 @@ jmethodID GetControlReferenceConstructor()
return s_control_reference_constructor;
}

jclass GetControlGroupContainerClass()
{
return s_control_group_container_class;
}

jfieldID GetControlGroupContainerPointer()
{
return s_control_group_container_pointer;
}

jmethodID GetControlGroupContainerConstructor()
{
return s_control_group_container_constructor;
}

jclass GetEmulatedControllerClass()
{
return s_emulated_controller_class;
Expand Down Expand Up @@ -692,6 +711,16 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
s_control_reference_constructor = env->GetMethodID(control_reference_class, "<init>", "(J)V");
env->DeleteLocalRef(control_reference_class);

const jclass control_group_container_class = env->FindClass(
"org/dolphinemu/dolphinemu/features/input/model/controlleremu/ControlGroupContainer");
s_control_group_container_class =
reinterpret_cast<jclass>(env->NewGlobalRef(control_group_container_class));
s_control_group_container_pointer =
env->GetFieldID(control_group_container_class, "pointer", "J");
s_control_group_container_constructor =
env->GetMethodID(control_group_container_class, "<init>", "(J)V");
env->DeleteLocalRef(control_group_container_class);

const jclass emulated_controller_class = env->FindClass(
"org/dolphinemu/dolphinemu/features/input/model/controlleremu/EmulatedController");
s_emulated_controller_class =
Expand Down
4 changes: 4 additions & 0 deletions Source/Android/jni/AndroidCommon/IDCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ jclass GetControlReferenceClass();
jfieldID GetControlReferencePointer();
jmethodID GetControlReferenceConstructor();

jclass GetControlGroupContainerClass();
jfieldID GetControlGroupContainerPointer();
jmethodID GetControlGroupContainerConstructor();

jclass GetEmulatedControllerClass();
jfieldID GetEmulatedControllerPointer();
jmethodID GetEmulatedControllerConstructor();
Expand Down
29 changes: 23 additions & 6 deletions Source/Android/jni/Input/EmulatedController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@
#include "jni/Input/ControlReference.h"
#include "jni/Input/NumericSetting.h"

ControllerEmu::ControlGroupContainer* ControlGroupContainerFromJava(JNIEnv* env, jobject obj)
{
return reinterpret_cast<ControllerEmu::ControlGroupContainer*>(
env->GetLongField(obj, IDCache::GetEmulatedControllerPointer()));
}

static jobject ControlGroupContainerToJava(JNIEnv* env,
ControllerEmu::ControlGroupContainer* container)
{
if (!container)
return nullptr;

return env->NewObject(IDCache::GetControlGroupContainerClass(),
IDCache::GetControlGroupContainerConstructor(),
reinterpret_cast<jlong>(container));
}

ControllerEmu::EmulatedController* EmulatedControllerFromJava(JNIEnv* env, jobject obj)
{
return reinterpret_cast<ControllerEmu::EmulatedController*>(
Expand Down Expand Up @@ -53,18 +70,18 @@ Java_org_dolphinemu_dolphinemu_features_input_model_controlleremu_EmulatedContro
}

JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_controlleremu_EmulatedController_getGroupCount(
Java_org_dolphinemu_dolphinemu_features_input_model_controlleremu_ControlGroupContainer_getGroupCount(
JNIEnv* env, jobject obj)
{
return static_cast<jint>(EmulatedControllerFromJava(env, obj)->groups.size());
return static_cast<jint>(ControlGroupContainerFromJava(env, obj)->groups.size());
}

JNIEXPORT jobject JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_controlleremu_EmulatedController_getGroup(
Java_org_dolphinemu_dolphinemu_features_input_model_controlleremu_ControlGroupContainer_getGroup(
JNIEnv* env, jobject obj, jint controller_index)
{
return ControlGroupToJava(env,
EmulatedControllerFromJava(env, obj)->groups[controller_index].get());
return ControlGroupToJava(
env, ControlGroupContainerFromJava(env, obj)->groups[controller_index].get());
}

JNIEXPORT void JNICALL
Expand Down Expand Up @@ -175,7 +192,7 @@ Java_org_dolphinemu_dolphinemu_features_input_model_controlleremu_EmulatedContro
{
auto* attachments = static_cast<ControllerEmu::Attachments*>(
Wiimote::GetWiimoteGroup(controller_index, WiimoteEmu::WiimoteGroup::Attachments));
return EmulatedControllerToJava(env, attachments->GetAttachmentList()[attachment_index].get());
return ControlGroupContainerToJava(env, attachments->GetAttachmentList()[attachment_index].get());
}

JNIEXPORT jint JNICALL
Expand Down
16 changes: 8 additions & 8 deletions Source/Core/Core/HW/GCPadEmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include "Core/HW/GCPad.h"

#include "InputCommon/ControllerEmu/Control/Input.h"
#include "InputCommon/ControllerEmu/Control/Output.h"
#include "InputCommon/ControllerEmu/ControlGroup/AnalogStick.h"
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
Expand Down Expand Up @@ -128,6 +126,8 @@ ControllerEmu::ControlGroup* GCPad::GetGroup(PadGroup group)

GCPadStatus GCPad::GetInput() const
{
using ControllerEmu::MapFloat;

const auto lock = GetStateLock();
GCPadStatus pad = {};

Expand Down Expand Up @@ -182,12 +182,12 @@ void GCPad::LoadDefaults(const ControllerInterface& ciface)
// Rumble
m_rumble->SetControlExpression(0, "`Android/0/Device Sensors:Motor 0`");
#else
// Buttons
m_buttons->SetControlExpression(0, "`X`"); // A
m_buttons->SetControlExpression(1, "`Z`"); // B
m_buttons->SetControlExpression(2, "`C`"); // X
m_buttons->SetControlExpression(3, "`S`"); // Y
m_buttons->SetControlExpression(4, "`D`"); // Z
// Buttons: A, B, X, Y, Z
m_buttons->SetControlExpression(0, "`X`");
m_buttons->SetControlExpression(1, "`Z`");
m_buttons->SetControlExpression(2, "`C`");
m_buttons->SetControlExpression(3, "`S`");
m_buttons->SetControlExpression(4, "`D`");
#ifdef _WIN32
m_buttons->SetControlExpression(5, "`RETURN`"); // Start
#else
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Classic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
#include "Core/HW/WiimoteEmu/Extension/Classic.h"

#include <array>
#include <string_view>

#include "Common/Assert.h"
#include "Common/BitUtils.h"
#include "Common/Common.h"
#include "Common/CommonTypes.h"

#include "Core/HW/WiimoteEmu/Extension/DesiredExtensionState.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"

#include "InputCommon/ControllerEmu/Control/Input.h"
#include "InputCommon/ControllerEmu/ControlGroup/AnalogStick.h"
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
Expand Down Expand Up @@ -88,6 +84,8 @@ Classic::Classic() : Extension1stParty("Classic", _trans("Classic Controller"))

void Classic::BuildDesiredExtensionState(DesiredExtensionState* target_state)
{
using ControllerEmu::MapFloat;

DataFormat classic_data = {};

// left stick
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Extension/DrawsomeTablet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
#include <array>

#include "Common/Assert.h"
#include "Common/BitUtils.h"
#include "Common/Common.h"
#include "Common/CommonTypes.h"

#include "Core/HW/WiimoteEmu/Extension/DesiredExtensionState.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"

#include "InputCommon/ControllerEmu/Control/Input.h"
#include "InputCommon/ControllerEmu/ControlGroup/AnalogStick.h"
#include "InputCommon/ControllerEmu/ControlGroup/Triggers.h"

Expand All @@ -37,6 +35,8 @@ DrawsomeTablet::DrawsomeTablet() : Extension3rdParty("Drawsome", _trans("Drawsom

void DrawsomeTablet::BuildDesiredExtensionState(DesiredExtensionState* target_state)
{
using ControllerEmu::MapFloat;

DataFormat& tablet_data = target_state->data.emplace<DataFormat>();

// Stylus X/Y (calibrated values):
Expand Down
5 changes: 2 additions & 3 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "Core/HW/WiimoteEmu/Extension/Drums.h"

#include <type_traits>

#include "Common/Assert.h"
#include "Common/BitUtils.h"
#include "Common/Common.h"
Expand All @@ -13,7 +11,6 @@
#include "Core/HW/WiimoteEmu/Extension/DesiredExtensionState.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"

#include "InputCommon/ControllerEmu/Control/Input.h"
#include "InputCommon/ControllerEmu/ControlGroup/AnalogStick.h"
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"

Expand Down Expand Up @@ -83,6 +80,8 @@ Drums::Drums() : Extension1stParty("Drums", _trans("Drum Kit"))

void Drums::BuildDesiredExtensionState(DesiredExtensionState* target_state)
{
using ControllerEmu::MapFloat;

DesiredState& state = target_state->data.emplace<DesiredState>();

{
Expand Down
10 changes: 0 additions & 10 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@

#include "Core/HW/WiimoteEmu/Extension/Extension.h"

#include <algorithm>
#include <array>
#include <cstring>

#include "Common/CommonTypes.h"
#include "Common/Inline.h"

#include "Core/HW/Wiimote.h"
#include "Core/HW/WiimoteEmu/Extension/DesiredExtensionState.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"

#include "Common/Logging/Log.h"

namespace WiimoteEmu
{
Extension::Extension(const char* name) : Extension(name, name)
Expand All @@ -37,11 +32,6 @@ std::string Extension::GetDisplayName() const
return m_display_name;
}

InputConfig* Extension::GetConfig() const
{
return ::Wiimote::GetConfig();
}

None::None() : Extension("None")
{
}
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
#include "Common/CommonTypes.h"
#include "Core/HW/WiimoteEmu/Encryption.h"
#include "Core/HW/WiimoteEmu/I2CBus.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerEmu/ControlGroup/Attachments.h"

namespace WiimoteEmu
{
struct DesiredExtensionState;

class Extension : public ControllerEmu::EmulatedController, public I2CSlave
class Extension : public ControllerEmu::AttachedController, public I2CSlave
{
public:
explicit Extension(const char* name);
Expand All @@ -27,8 +27,6 @@ class Extension : public ControllerEmu::EmulatedController, public I2CSlave
std::string GetName() const override;
std::string GetDisplayName() const override;

InputConfig* GetConfig() const override;

// Used by the wiimote to detect extension changes.
// The normal extensions short this pin so it's always connected,
// but M+ does some tricks with it during activation.
Expand Down
Loading

0 comments on commit 4a754d0

Please sign in to comment.