Adds a function for grabbing physical device from virtual slot.

This commit is contained in:
Kenix3 2022-08-02 21:58:30 -04:00
parent b3c3882b12
commit c1659d3dcf
5 changed files with 132 additions and 125 deletions

View File

@ -7,14 +7,15 @@
#include "SDLController.h" #include "SDLController.h"
#include <Utils/StringHelper.h> #include <Utils/StringHelper.h>
namespace Ship {
uint8_t* controllerBits; uint8_t* controllerBits;
void Ship::ControlDeck::Init(uint8_t* bits) { void ControlDeck::Init(uint8_t* bits) {
ScanPhysicalDevices(); ScanPhysicalDevices();
controllerBits = bits; controllerBits = bits;
} }
void Ship::ControlDeck::ScanPhysicalDevices() { void ControlDeck::ScanPhysicalDevices() {
virtualDevices.clear(); virtualDevices.clear();
physicalDevices.clear(); physicalDevices.clear();
@ -44,13 +45,13 @@ void Ship::ControlDeck::ScanPhysicalDevices() {
LoadControllerSettings(); LoadControllerSettings();
} }
void Ship::ControlDeck::SetPhysicalDevice(int slot, int deviceSlot) { void ControlDeck::SetPhysicalDevice(int slot, int deviceSlot) {
const std::shared_ptr<Controller> backend = physicalDevices[deviceSlot]; const std::shared_ptr<Controller> backend = physicalDevices[deviceSlot];
virtualDevices[slot] = deviceSlot; virtualDevices[slot] = deviceSlot;
*controllerBits |= (backend->Connected()) << slot; *controllerBits |= (backend->Connected()) << slot;
} }
void Ship::ControlDeck::WriteToPad(OSContPad* pad) const { void ControlDeck::WriteToPad(OSContPad* pad) const {
for (size_t i = 0; i < virtualDevices.size(); i++) { for (size_t i = 0; i < virtualDevices.size(); i++) {
const std::shared_ptr<Controller> backend = physicalDevices[virtualDevices[i]]; const std::shared_ptr<Controller> backend = physicalDevices[virtualDevices[i]];
if (backend->GetGuid() == "Auto") { if (backend->GetGuid() == "Auto") {
@ -65,7 +66,7 @@ void Ship::ControlDeck::WriteToPad(OSContPad* pad) const {
#define NESTED(key, ...) StringHelper::Sprintf("Controllers.%s.Slot_%d." key, device->GetGuid().c_str(), slot, __VA_ARGS__) #define NESTED(key, ...) StringHelper::Sprintf("Controllers.%s.Slot_%d." key, device->GetGuid().c_str(), slot, __VA_ARGS__)
void Ship::ControlDeck::LoadControllerSettings() { void ControlDeck::LoadControllerSettings() {
std::shared_ptr<Mercury> Config = GlobalCtx2::GetInstance()->GetConfig(); std::shared_ptr<Mercury> Config = GlobalCtx2::GetInstance()->GetConfig();
for (auto const& val : Config->rjson["Controllers"]["Deck"].items()) { for (auto const& val : Config->rjson["Controllers"]["Deck"].items()) {
@ -112,7 +113,7 @@ void Ship::ControlDeck::LoadControllerSettings() {
} }
} }
void Ship::ControlDeck::SaveControllerSettings() { void ControlDeck::SaveControllerSettings() {
std::shared_ptr<Mercury> Config = GlobalCtx2::GetInstance()->GetConfig(); std::shared_ptr<Mercury> Config = GlobalCtx2::GetInstance()->GetConfig();
for (size_t i = 0; i < virtualDevices.size(); i++) { for (size_t i = 0; i < virtualDevices.size(); i++) {
@ -153,18 +154,23 @@ void Ship::ControlDeck::SaveControllerSettings() {
Config->save(); Config->save();
} }
std::shared_ptr<Ship::Controller> Ship::ControlDeck::GetPhysicalDevice(int deviceSlot) { std::shared_ptr<Controller> ControlDeck::GetPhysicalDevice(int deviceSlot) {
return physicalDevices[deviceSlot]; return physicalDevices[deviceSlot];
} }
size_t Ship::ControlDeck::GetNumPhysicalDevices() { size_t ControlDeck::GetNumPhysicalDevices() {
return physicalDevices.size(); return physicalDevices.size();
} }
int Ship::ControlDeck::GetVirtualDevice(int slot) { int ControlDeck::GetVirtualDevice(int slot) {
return virtualDevices[slot]; return virtualDevices[slot];
} }
size_t Ship::ControlDeck::GetNumVirtualDevices() { size_t ControlDeck::GetNumVirtualDevices() {
return virtualDevices.size(); return virtualDevices.size();
} }
std::shared_ptr<Controller> ControlDeck::GetPhysicalDeviceFromVirtualSlot(int slot) {
return GetPhysicalDevice(GetVirtualDevice(slot));
}
}

View File

@ -15,6 +15,7 @@ namespace Ship {
void SaveControllerSettings(); void SaveControllerSettings();
void SetPhysicalDevice(int slot, int deviceSlot); void SetPhysicalDevice(int slot, int deviceSlot);
std::shared_ptr<Ship::Controller> GetPhysicalDevice(int deviceSlot); std::shared_ptr<Ship::Controller> GetPhysicalDevice(int deviceSlot);
std::shared_ptr<Ship::Controller> GetPhysicalDeviceFromVirtualSlot(int slot);
size_t GetNumPhysicalDevices(); size_t GetNumPhysicalDevices();
int GetVirtualDevice(int slot); int GetVirtualDevice(int slot);
size_t GetNumVirtualDevices(); size_t GetNumVirtualDevices();

View File

@ -17,7 +17,7 @@ namespace Ship {
std::shared_ptr<Controller> GetControllerPerSlot(int slot) { std::shared_ptr<Controller> GetControllerPerSlot(int slot) {
auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck(); auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
return controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(slot)); return controlDeck->GetPhysicalDeviceFromVirtualSlot(slot);
} }
void InputEditor::DrawButton(const char* label, int n64Btn) { void InputEditor::DrawButton(const char* label, int n64Btn) {
@ -83,7 +83,7 @@ namespace Ship {
void InputEditor::DrawControllerSchema() { void InputEditor::DrawControllerSchema() {
auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck(); auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
auto Backend = controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(CurrentPort)); auto Backend = controlDeck->GetPhysicalDeviceFromVirtualSlot(CurrentPort);
DeviceProfile& profile = Backend->profiles[CurrentPort]; DeviceProfile& profile = Backend->profiles[CurrentPort];
float sensitivity = profile.Thresholds[SENSITIVITY]; float sensitivity = profile.Thresholds[SENSITIVITY];
bool IsKeyboard = Backend->GetGuid() == "Keyboard" || Backend->GetGuid() == "Auto" || !Backend->Connected(); bool IsKeyboard = Backend->GetGuid() == "Keyboard" || Backend->GetGuid() == "Auto" || !Backend->Connected();

View File

@ -1309,7 +1309,7 @@ extern "C" void OTRControllerCallback(ControllerCallback* controller) {
auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck(); auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
for (int i = 0; i < controlDeck->GetNumVirtualDevices(); ++i) { for (int i = 0; i < controlDeck->GetNumVirtualDevices(); ++i) {
auto physicalDevice = controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(i)); auto physicalDevice = controlDeck->GetPhysicalDeviceFromVirtualSlot(i);
physicalDevice->WriteToSource(i, controller); physicalDevice->WriteToSource(i, controller);
} }
} }
@ -1367,7 +1367,7 @@ extern "C" int Controller_ShouldRumble(size_t i) {
auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck(); auto controlDeck = Ship::GlobalCtx2::GetInstance()->GetWindow()->GetControlDeck();
for (int i = 0; i < controlDeck->GetNumVirtualDevices(); ++i) { for (int i = 0; i < controlDeck->GetNumVirtualDevices(); ++i) {
auto physicalDevice = controlDeck->GetPhysicalDevice(controlDeck->GetVirtualDevice(i)); auto physicalDevice = controlDeck->GetPhysicalDeviceFromVirtualSlot(i);
if (physicalDevice->CanRumble()) { if (physicalDevice->CanRumble()) {
return 1; return 1;
} }