Skip to content

Commit

Permalink
Input fixes/improvements (libretro#246)
Browse files Browse the repository at this point in the history
* Fixed the issue where you had to manually edit `Dolphin.ini` to enable GC controllers 2-4.
* Added the possibility to un/plug Wiimotes or GC controllers on-the-fly. Previously not possible with GC controllers, and it was messing with the players order if Wiimotes were not reconnected in the correct order.
* Added the possibility to use GC controller as players 1-4 in Wii mode. Makes it possible to play Brawl, MK Wii, etc. with a GC layout.
* Fix Wiimotes displaying GC labels by default.
* (not related to inputs) Removed the unused "efb_scale" variable. It was harmless but that's one less warning during compilation :p
  • Loading branch information
bslenul authored Mar 8, 2022
1 parent 3b19e6d commit 3b66006
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 34 deletions.
123 changes: 90 additions & 33 deletions Source/Core/DolphinLibretro/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "Core/HW/WiimoteEmu/Extension/Nunchuk.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
#include "Core/HW/WiimoteReal/WiimoteReal.h"
#include "Core/HW/SI/SI.h"
#include "Core/HW/SI/SI_Device.h"
#include "Core/Host.h"
#include "DolphinLibretro/Input.h"
#include "DolphinLibretro/Options.h"
Expand All @@ -33,6 +35,7 @@
#define RETRO_DEVICE_WIIMOTE_NC ((3 << 8) | RETRO_DEVICE_JOYPAD)
#define RETRO_DEVICE_WIIMOTE_CC ((4 << 8) | RETRO_DEVICE_JOYPAD)
#define RETRO_DEVICE_WIIMOTE_CC_PRO ((5 << 8) | RETRO_DEVICE_JOYPAD)
#define RETRO_DEVICE_GC_ON_WII ((6 << 8) | RETRO_DEVICE_JOYPAD)
#define RETRO_DEVICE_REAL_WIIMOTE ((6 << 8) | RETRO_DEVICE_NONE)

namespace Libretro
Expand Down Expand Up @@ -397,6 +400,9 @@ static void AddDevicesForPort(unsigned port)
g_controller_interface.AddDevice(std::make_shared<Device>(RETRO_DEVICE_POINTER, port));
}

#if 0
/* Disabled as it messes with the controllers index, so player 2 may end up controlling player 3, etc.
* if controllers are not plugged back in the correct order. */
static void RemoveDevicesForPort(unsigned port)
{
g_controller_interface.RemoveDevice([&port](const auto& device) {
Expand All @@ -408,6 +414,7 @@ static void RemoveDevicesForPort(unsigned port)
dynamic_cast<const Device*>(device)->GetPort() == port;
});
}
#endif

void Init()
{
Expand All @@ -421,36 +428,66 @@ void Init()
Pad::Initialize();
Keyboard::Initialize();

int port_max = (SConfig::GetInstance().bWii && Libretro::Options::altGCPorts) ? 8 : 4;
for (int i = 0; i < port_max; i++)
Libretro::Input::AddDevicesForPort(i);

static const struct retro_controller_description gcpad_desc[] = {
{"GameCube Controller", RETRO_DEVICE_JOYPAD},
};

if (SConfig::GetInstance().bWii && !SConfig::GetInstance().m_bt_passthrough_enabled)
{
init_wiimotes = true;
Wiimote::Initialize(Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES);

static const struct retro_controller_description wiimote_desc[] = {
{"WiiMote", RETRO_DEVICE_WIIMOTE},
{"WiiMote (sideways)", RETRO_DEVICE_WIIMOTE_SW},
{"WiiMote + Nunchuk", RETRO_DEVICE_WIIMOTE_NC},
{"WiiMote + Classic Controller", RETRO_DEVICE_WIIMOTE_CC},
{"WiiMote + Classic Controller Pro", RETRO_DEVICE_WIIMOTE_CC_PRO},
{"Real WiiMote", RETRO_DEVICE_REAL_WIIMOTE},
};

static const struct retro_controller_info ports[] = {
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{0},
};

environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
if (Libretro::Options::altGCPorts) // Wii devices listed in ports 1-4, GC controllers in ports 5-8
{
static const struct retro_controller_description wiimote_desc[] = {
{"WiiMote", RETRO_DEVICE_WIIMOTE},
{"WiiMote (sideways)", RETRO_DEVICE_WIIMOTE_SW},
{"WiiMote + Nunchuk", RETRO_DEVICE_WIIMOTE_NC},
{"WiiMote + Classic Controller", RETRO_DEVICE_WIIMOTE_CC},
{"WiiMote + Classic Controller Pro", RETRO_DEVICE_WIIMOTE_CC_PRO},
{"Real WiiMote", RETRO_DEVICE_REAL_WIIMOTE},
};

static const struct retro_controller_info ports[] = {
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{wiimote_desc, sizeof(wiimote_desc) / sizeof(*wiimote_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{gcpad_desc, sizeof(gcpad_desc) / sizeof(*gcpad_desc)},
{0},
};

environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
}
else // Both Wii devices and GC controllers listed in ports 1-4, ports 5-8 are unused
{
static const struct retro_controller_description wii_and_gc_desc[] = {
{"WiiMote", RETRO_DEVICE_WIIMOTE},
{"WiiMote (sideways)", RETRO_DEVICE_WIIMOTE_SW},
{"WiiMote + Nunchuk", RETRO_DEVICE_WIIMOTE_NC},
{"WiiMote + Classic Controller", RETRO_DEVICE_WIIMOTE_CC},
{"WiiMote + Classic Controller Pro", RETRO_DEVICE_WIIMOTE_CC_PRO},
{"Real WiiMote", RETRO_DEVICE_REAL_WIIMOTE},
{"GameCube Controller", RETRO_DEVICE_GC_ON_WII},
};

static const struct retro_controller_info ports[] = {
{wii_and_gc_desc, sizeof(wii_and_gc_desc) / sizeof(*wii_and_gc_desc)},
{wii_and_gc_desc, sizeof(wii_and_gc_desc) / sizeof(*wii_and_gc_desc)},
{wii_and_gc_desc, sizeof(wii_and_gc_desc) / sizeof(*wii_and_gc_desc)},
{wii_and_gc_desc, sizeof(wii_and_gc_desc) / sizeof(*wii_and_gc_desc)},
{0},
};

environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
}
}
else
{
Expand Down Expand Up @@ -520,7 +557,7 @@ void retro_set_input_state(retro_input_state_t cb)

void retro_set_controller_port_device(unsigned port, unsigned device)
{
if (port > 7)
if (((!SConfig::GetInstance().bWii || !Libretro::Options::altGCPorts) && port > 3) || port > 7)
return;

Libretro::Input::input_types[port] = device;
Expand All @@ -534,12 +571,30 @@ void retro_set_controller_port_device(unsigned port, unsigned device)
std::string devLightgun = Libretro::Input::GetQualifiedName(port, RETRO_DEVICE_LIGHTGUN);
#endif

Libretro::Input::RemoveDevicesForPort(port);
if (device != RETRO_DEVICE_NONE && device != RETRO_DEVICE_REAL_WIIMOTE)
Libretro::Input::AddDevicesForPort(port);
if (device == RETRO_DEVICE_NONE)
{
if (SConfig::GetInstance().bWii && port < 4)
WiimoteCommon::SetSource(port, WiimoteSource::None);

if (!SConfig::GetInstance().bWii || port > 3)
if (!SConfig::GetInstance().bWii || !Libretro::Options::altGCPorts)
{
SConfig::GetInstance().m_SIDevice[port] = SerialInterface::SIDEVICE_NONE;
SerialInterface::ChangeDevice(SConfig::GetInstance().m_SIDevice[port], port);
}
else if (port > 3)
{
SConfig::GetInstance().m_SIDevice[port - 4] = SerialInterface::SIDEVICE_NONE;
SerialInterface::ChangeDevice(SConfig::GetInstance().m_SIDevice[port - 4], port - 4);
}
}
else if (!SConfig::GetInstance().bWii || device == RETRO_DEVICE_GC_ON_WII || port > 3)
{
if (device == RETRO_DEVICE_GC_ON_WII) // Disconnect Wii device if we're using GC controller as device type to avoid conflict
WiimoteCommon::SetSource(port, WiimoteSource::None);

SConfig::GetInstance().m_SIDevice[port > 3 ? port - 4 : port] = SerialInterface::SIDEVICE_GC_CONTROLLER;
SerialInterface::ChangeDevice(SConfig::GetInstance().m_SIDevice[port > 3 ? port - 4 : port], port > 3 ? port - 4 : port);

GCPad* gcPad = (GCPad*)Pad::GetConfig()->GetController(port > 3 ? port - 4 : port);
// load an empty inifile section, clears everything
IniFile::Section sec;
Expand Down Expand Up @@ -590,6 +645,12 @@ void retro_set_controller_port_device(unsigned port, unsigned device)
}
else if (!SConfig::GetInstance().m_bt_passthrough_enabled)
{
if (!Libretro::Options::altGCPorts) // Disconnect GC controller to avoid conflict with Wii device
{
SConfig::GetInstance().m_SIDevice[port] = SerialInterface::SIDEVICE_NONE;
SerialInterface::ChangeDevice(SConfig::GetInstance().m_SIDevice[port], port);
}

WiimoteEmu::Wiimote* wm = (WiimoteEmu::Wiimote*)Wiimote::GetConfig()->GetController(port);
// load an empty inifile section, clears everything
IniFile::Section sec;
Expand Down Expand Up @@ -805,17 +866,13 @@ void retro_set_controller_port_device(unsigned port, unsigned device)
case RETRO_DEVICE_REAL_WIIMOTE:
WiimoteCommon::SetSource(port, WiimoteSource::Real);
break;

default:
WiimoteCommon::SetSource(port, WiimoteSource::None);
break;
}
wm->UpdateReferences(g_controller_interface);
::Wiimote::GetConfig()->SaveConfig();
}
std::vector<retro_input_descriptor> all_descs;

int port_max = SConfig::GetInstance().bWii ? 8 : 4;
int port_max = (SConfig::GetInstance().bWii && Libretro::Options::altGCPorts) ? 8 : 4;
for (int i = 0; i < port_max; i++)
{
retro_input_descriptor* desc;
Expand Down Expand Up @@ -843,7 +900,7 @@ void retro_set_controller_port_device(unsigned port, unsigned device)
continue;

default:
if (!SConfig::GetInstance().bWii || port > 3)
if (!SConfig::GetInstance().bWii || i > 3)
{
desc = Libretro::Input::descGC;
}
Expand Down
1 change: 0 additions & 1 deletion Source/Core/DolphinLibretro/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ void retro_get_system_info(retro_system_info* info)

void retro_get_system_av_info(retro_system_av_info* info)
{
unsigned efb_scale = Libretro::Options::efbScale;
info->geometry.base_width = EFB_WIDTH * Libretro::Options::efbScale;
info->geometry.base_height = EFB_HEIGHT * Libretro::Options::efbScale;

Expand Down
1 change: 1 addition & 0 deletions Source/Core/DolphinLibretro/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ Option<bool> enableRumble("dolphin_enable_rumble", "Rumble", true);
Option<u32> sensorBarPosition("dolphin_sensor_bar_position", "Sensor Bar Position",
{"Bottom", "Top"});
Option<bool> WiimoteContinuousScanning("dolphin_wiimote_continuous_scanning", "Wiimote Continuous Scanning", false);
Option<bool> altGCPorts("dolphin_alt_gc_ports_on_wii", "Use ports 5-8 for GameCube controllers in Wii mode", false);
Option<unsigned int> audioMixerRate("dolphin_mixer_rate", "Audio Mixer Rate",
{{"32000", 32000u}, {"48000", 48000u}});
Option<bool> DSPHLE("dolphin_dsp_hle", "DSP HLE", true);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DolphinLibretro/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ extern Option<int> irHeight;
extern Option<bool> enableRumble;
extern Option<u32> sensorBarPosition;
extern Option<bool> WiimoteContinuousScanning;
extern Option<bool> altGCPorts;
extern Option<unsigned int> audioMixerRate;
extern Option<bool> DSPHLE;
extern Option<bool> DSPEnableJIT;
Expand Down

0 comments on commit 3b66006

Please sign in to comment.