Skip to content

Commit

Permalink
InputCommon: Decouple ButtonManager and Touchscreen from Android
Browse files Browse the repository at this point in the history
Changes were also made for codestyle compliance.
  • Loading branch information
OatmealDome committed Nov 28, 2019
1 parent c2c8a14 commit ef32a10
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 530 deletions.
1 change: 0 additions & 1 deletion Source/Android/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ add_library(main SHARED
AndroidCommon/IDCache.cpp
GameList/GameFile.cpp
GameList/GameFileCache.cpp
ButtonManager.cpp
MainAndroid.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion Source/Android/jni/MainAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "DiscIO/Volume.h"

#include "InputCommon/ControllerInterface/Android/Android.h"
#include "InputCommon/ControllerInterface/Touch/ButtonManager.h"

#include "UICommon/UICommon.h"

Expand All @@ -58,7 +59,6 @@
#include "../../Core/Common/WindowSystemInfo.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"
#include "jni/ButtonManager.h"

namespace
{
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/InputCommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ elseif(ANDROID)
target_sources(inputcommon PRIVATE
ControllerInterface/Android/Android.cpp
ControllerInterface/Android/Android.h
ControllerInterface/Touch/ButtonManager.cpp
ControllerInterface/Touch/ButtonManager.h
ControllerInterface/Touch/Touchscreen.cpp
ControllerInterface/Touch/Touchscreen.h
)
endif()

Expand Down
252 changes: 3 additions & 249 deletions Source/Core/InputCommon/ControllerInterface/Android/Android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
// Refer to the license.txt file included.

#include "InputCommon/ControllerInterface/Android/Android.h"
#include <jni/AndroidCommon/IDCache.h>
#include <sstream>
#include <thread>
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/Touch/Touchscreen.h"

namespace ciface::Android
{
Expand All @@ -28,251 +26,7 @@ void SetMotionSensorsEnabled(bool accelerometer_enabled, bool gyroscope_enabled)
void PopulateDevices()
{
for (int i = 0; i < 8; ++i)
g_controller_interface.AddDevice(std::make_shared<Touchscreen>(i));
}

// Touchscreens and stuff
std::string Touchscreen::GetName() const
{
return "Touchscreen";
}

std::string Touchscreen::GetSource() const
{
return "Android";
}

Touchscreen::Touchscreen(int padID) : _padID(padID)
{
// GC
AddInput(new Button(_padID, ButtonManager::BUTTON_A));
AddInput(new Button(_padID, ButtonManager::BUTTON_B));
AddInput(new Button(_padID, ButtonManager::BUTTON_START));
AddInput(new Button(_padID, ButtonManager::BUTTON_X));
AddInput(new Button(_padID, ButtonManager::BUTTON_Y));
AddInput(new Button(_padID, ButtonManager::BUTTON_Z));
AddInput(new Button(_padID, ButtonManager::BUTTON_UP));
AddInput(new Button(_padID, ButtonManager::BUTTON_DOWN));
AddInput(new Button(_padID, ButtonManager::BUTTON_LEFT));
AddInput(new Button(_padID, ButtonManager::BUTTON_RIGHT));
AddInput(new Axis(_padID, ButtonManager::STICK_MAIN_LEFT));
AddInput(new Axis(_padID, ButtonManager::STICK_MAIN_RIGHT));
AddInput(new Axis(_padID, ButtonManager::STICK_MAIN_UP));
AddInput(new Axis(_padID, ButtonManager::STICK_MAIN_DOWN));
AddInput(new Axis(_padID, ButtonManager::STICK_C_LEFT));
AddInput(new Axis(_padID, ButtonManager::STICK_C_RIGHT));
AddInput(new Axis(_padID, ButtonManager::STICK_C_UP));
AddInput(new Axis(_padID, ButtonManager::STICK_C_DOWN));
AddInput(new Axis(_padID, ButtonManager::TRIGGER_L));
AddInput(new Axis(_padID, ButtonManager::TRIGGER_R));

// Wiimote
AddInput(new Button(_padID, ButtonManager::WIIMOTE_BUTTON_A));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_BUTTON_B));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_BUTTON_MINUS));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_BUTTON_PLUS));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_BUTTON_HOME));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_BUTTON_1));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_BUTTON_2));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_UP));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_DOWN));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_LEFT));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_RIGHT));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_IR_HIDE));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_TILT_MODIFIER));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_SHAKE_X));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_SHAKE_Y));
AddInput(new Button(_padID, ButtonManager::WIIMOTE_SHAKE_Z));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_IR_UP));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_IR_DOWN));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_IR_LEFT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_IR_RIGHT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_IR_FORWARD));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_IR_BACKWARD));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_SWING_UP));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_SWING_DOWN));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_SWING_LEFT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_SWING_RIGHT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_SWING_FORWARD));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_SWING_BACKWARD));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_TILT_LEFT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_TILT_RIGHT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_TILT_FORWARD));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_TILT_BACKWARD));

// Wii ext: Nunchuk
AddInput(new Button(_padID, ButtonManager::NUNCHUK_BUTTON_C));
AddInput(new Button(_padID, ButtonManager::NUNCHUK_BUTTON_Z));
AddInput(new Button(_padID, ButtonManager::NUNCHUK_TILT_MODIFIER));
AddInput(new Button(_padID, ButtonManager::NUNCHUK_SHAKE_X));
AddInput(new Button(_padID, ButtonManager::NUNCHUK_SHAKE_Y));
AddInput(new Button(_padID, ButtonManager::NUNCHUK_SHAKE_Z));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_STICK_LEFT));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_STICK_RIGHT));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_STICK_UP));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_STICK_DOWN));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_SWING_LEFT));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_SWING_RIGHT));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_SWING_UP));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_SWING_DOWN));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_SWING_FORWARD));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_SWING_BACKWARD));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_TILT_LEFT));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_TILT_RIGHT));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_TILT_FORWARD));
AddInput(new Axis(_padID, ButtonManager::NUNCHUK_TILT_BACKWARD));

// Wii ext: Classic
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_A));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_B));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_X));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_Y));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_MINUS));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_PLUS));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_HOME));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_ZL));
AddInput(new Button(_padID, ButtonManager::CLASSIC_BUTTON_ZR));
AddInput(new Button(_padID, ButtonManager::CLASSIC_DPAD_UP));
AddInput(new Button(_padID, ButtonManager::CLASSIC_DPAD_DOWN));
AddInput(new Button(_padID, ButtonManager::CLASSIC_DPAD_LEFT));
AddInput(new Button(_padID, ButtonManager::CLASSIC_DPAD_RIGHT));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_LEFT_LEFT));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_LEFT_RIGHT));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_LEFT_UP));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_LEFT_DOWN));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_RIGHT_LEFT));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_RIGHT_RIGHT));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_RIGHT_UP));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_STICK_RIGHT_DOWN));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_TRIGGER_L));
AddInput(new Axis(_padID, ButtonManager::CLASSIC_TRIGGER_R));

// Wii-ext: Guitar
AddInput(new Button(_padID, ButtonManager::GUITAR_BUTTON_MINUS));
AddInput(new Button(_padID, ButtonManager::GUITAR_BUTTON_PLUS));
AddInput(new Button(_padID, ButtonManager::GUITAR_FRET_GREEN));
AddInput(new Button(_padID, ButtonManager::GUITAR_FRET_RED));
AddInput(new Button(_padID, ButtonManager::GUITAR_FRET_YELLOW));
AddInput(new Button(_padID, ButtonManager::GUITAR_FRET_BLUE));
AddInput(new Button(_padID, ButtonManager::GUITAR_FRET_ORANGE));
AddInput(new Button(_padID, ButtonManager::GUITAR_STRUM_UP));
AddInput(new Button(_padID, ButtonManager::GUITAR_STRUM_DOWN));
AddInput(new Axis(_padID, ButtonManager::GUITAR_STICK_LEFT));
AddInput(new Axis(_padID, ButtonManager::GUITAR_STICK_RIGHT));
AddInput(new Axis(_padID, ButtonManager::GUITAR_STICK_UP));
AddInput(new Axis(_padID, ButtonManager::GUITAR_STICK_DOWN));
AddInput(new Axis(_padID, ButtonManager::GUITAR_WHAMMY_BAR));

// Wii-ext: Drums
AddInput(new Button(_padID, ButtonManager::DRUMS_BUTTON_MINUS));
AddInput(new Button(_padID, ButtonManager::DRUMS_BUTTON_PLUS));
AddInput(new Button(_padID, ButtonManager::DRUMS_PAD_RED));
AddInput(new Button(_padID, ButtonManager::DRUMS_PAD_YELLOW));
AddInput(new Button(_padID, ButtonManager::DRUMS_PAD_BLUE));
AddInput(new Button(_padID, ButtonManager::DRUMS_PAD_GREEN));
AddInput(new Button(_padID, ButtonManager::DRUMS_PAD_ORANGE));
AddInput(new Button(_padID, ButtonManager::DRUMS_PAD_BASS));
AddInput(new Axis(_padID, ButtonManager::DRUMS_STICK_LEFT));
AddInput(new Axis(_padID, ButtonManager::DRUMS_STICK_RIGHT));
AddInput(new Axis(_padID, ButtonManager::DRUMS_STICK_UP));
AddInput(new Axis(_padID, ButtonManager::DRUMS_STICK_DOWN));

// Wii-ext: Turntable
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_GREEN_LEFT));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_RED_LEFT));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_BLUE_LEFT));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_GREEN_RIGHT));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_RED_RIGHT));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_BLUE_RIGHT));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_MINUS));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_PLUS));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_HOME));
AddInput(new Button(_padID, ButtonManager::TURNTABLE_BUTTON_EUPHORIA));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_TABLE_LEFT_LEFT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_TABLE_LEFT_RIGHT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_TABLE_RIGHT_LEFT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_TABLE_RIGHT_RIGHT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_STICK_LEFT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_STICK_RIGHT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_STICK_UP));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_STICK_DOWN));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_CROSSFADE_LEFT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_CROSSFADE_RIGHT));
AddInput(new Axis(_padID, ButtonManager::TURNTABLE_EFFECT_DIAL));

// Wiimote IMU
// Only add inputs if we actually can receive data from the relevant sensor.
// Whether inputs exist affects what WiimoteEmu gets when calling ControlReference::BoundCount.
if (s_accelerometer_enabled)
{
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_ACCEL_LEFT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_ACCEL_RIGHT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_ACCEL_FORWARD));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_ACCEL_BACKWARD));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_ACCEL_UP));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_ACCEL_DOWN));
}
if (s_gyroscope_enabled)
{
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_GYRO_PITCH_UP));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_GYRO_PITCH_DOWN));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_GYRO_ROLL_LEFT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_GYRO_ROLL_RIGHT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_GYRO_YAW_LEFT));
AddInput(new Axis(_padID, ButtonManager::WIIMOTE_GYRO_YAW_RIGHT));
}

// Rumble
AddOutput(new Motor(_padID, ButtonManager::RUMBLE));
}
// Buttons and stuff

std::string Touchscreen::Button::GetName() const
{
std::ostringstream ss;
ss << "Button " << (int)_index;
return ss.str();
}

ControlState Touchscreen::Button::GetState() const
{
return ButtonManager::GetButtonPressed(_padID, _index);
}

std::string Touchscreen::Axis::GetName() const
{
std::ostringstream ss;
ss << "Axis " << (int)_index;
return ss.str();
}

ControlState Touchscreen::Axis::GetState() const
{
return ButtonManager::GetAxisValue(_padID, _index) * _neg;
}

Touchscreen::Motor::~Motor()
{
}

std::string Touchscreen::Motor::GetName() const
{
std::ostringstream ss;
ss << "Rumble " << (int)_index;
return ss.str();
}

void Touchscreen::Motor::SetState(ControlState state)
{
if (state > 0)
{
std::thread(Rumble, _padID, state).detach();
}
}

void Touchscreen::Motor::Rumble(int padID, double state)
{
JNIEnv* env = IDCache::GetEnvForThread();
env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), IDCache::GetDoRumble(), padID, state);
g_controller_interface.AddDevice(std::make_shared<ciface::Touch::Touchscreen>(
i, s_accelerometer_enabled, s_gyroscope_enabled));
}
} // namespace ciface::Android
57 changes: 0 additions & 57 deletions Source/Core/InputCommon/ControllerInterface/Android/Android.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,9 @@

#pragma once

#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "jni/ButtonManager.h"

namespace ciface::Android
{
void SetMotionSensorsEnabled(bool accelerometer_enabled, bool gyroscope_enabled);

void PopulateDevices();

class Touchscreen : public Core::Device
{
private:
class Button : public Input
{
public:
std::string GetName() const;
Button(int padID, ButtonManager::ButtonType index) : _padID(padID), _index(index) {}
ControlState GetState() const;

private:
const int _padID;
const ButtonManager::ButtonType _index;
};
class Axis : public Input
{
public:
std::string GetName() const;
bool IsDetectable() override { return false; }
Axis(int padID, ButtonManager::ButtonType index, float neg = 1.0f)
: _padID(padID), _index(index), _neg(neg)
{
}
ControlState GetState() const;

private:
const int _padID;
const ButtonManager::ButtonType _index;
const float _neg;
};
class Motor : public Core::Device::Output
{
public:
Motor(int padID, ButtonManager::ButtonType index) : _padID(padID), _index(index) {}
~Motor();
std::string GetName() const override;
void SetState(ControlState state) override;

private:
const int _padID;
const ButtonManager::ButtonType _index;
static void Rumble(int padID, double state);
};

public:
Touchscreen(int padID);
~Touchscreen() {}
std::string GetName() const;
std::string GetSource() const;

private:
const int _padID;
};
} // namespace ciface::Android
Loading

0 comments on commit ef32a10

Please sign in to comment.