Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 65 additions & 19 deletions source/ButtonComboManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,28 @@ ButtonComboModule_Error ButtonComboManager::DetectButtonCombo_Blocking(const But
std::lock_guard lock(mMutex);

if (options.controllerMask == BUTTON_COMBO_MODULE_CONTROLLER_NONE) {
DEBUG_FUNCTION_LINE_WARN("Failed to detect button combo: Controller Mask was empty.");
return BUTTON_COMBO_MODULE_ERROR_INVALID_ARGUMENT;
}

if (options.holdComboForInMs == 0 || options.holdAbortForInMs == 0 || options.abortButtonCombo == 0) {
DEBUG_FUNCTION_LINE_WARN("Failed to detect button combo: Invalid params. holdComboFor: %s ms, holdAbortFor: %d ms, abortButtonCombo: %08X", options.holdComboForInMs, options.holdAbortForInMs, options.abortButtonCombo);
return BUTTON_COMBO_MODULE_ERROR_INVALID_ARGUMENT;
}

bool doShutdownKPAD = false;
bool doDisableProController = false;
KPADStatus status;
KPADError err = KPAD_ERROR_OK;
if (KPADReadEx(WPAD_CHAN_0, &status, 0, &err) == 0 && err == KPAD_ERROR_UNINITIALIZED) {
doShutdownKPAD = true;
KPADInit();
}
if (!WPADIsEnabledURC()) {
WPADEnableURCC(true);
doDisableProController = true;
}

KPADStatus kpad_data{};
KPADError kpad_error;

Expand All @@ -611,37 +626,57 @@ ButtonComboModule_Error ButtonComboManager::DetectButtonCombo_Blocking(const But
const uint32_t holdAbortTarget = options.holdAbortForInMs >> 4; // roughly ms to frames. Works because we wait 16ms in the loop
const uint32_t abortButton = options.abortButtonCombo;


ButtonComboModule_Error result = BUTTON_COMBO_MODULE_ERROR_UNKNOWN_ERROR;
while (true) {
uint32_t buttonsHold = 0;
uint32_t buttonsHold = 0;
uint32_t buttonsHoldAbort = 0;
for (int i = 0; i < 2; i++) {
VPADReadError vpad_error = VPAD_READ_UNINITIALIZED;
VPADStatus vpad_data = {};
if (i == 0 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_VPAD_0)) { continue; }
if (i == 1 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_VPAD_1)) { continue; }
VPADReadError vpad_error = VPAD_READ_UNINITIALIZED;
VPADStatus vpad_data = {};
uint32_t convertedButtons = 0;
if (VPADRead(static_cast<VPADChan>(i), &vpad_data, 1, &vpad_error) > 0 && vpad_error == VPAD_READ_SUCCESS) {
buttonsHold = remapVPADButtons(vpad_data.hold);
convertedButtons = remapVPADButtons(vpad_data.hold);
}
buttonsHoldAbort |= convertedButtons;

if (i == 0 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_VPAD_0)) { continue; }
if (i == 1 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_VPAD_1)) { continue; }

buttonsHold |= convertedButtons;
}
for (int i = 0; i < 7; i++) {
if (i == 0 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_0)) { continue; }
if (i == 1 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_1)) { continue; }
if (i == 2 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_2)) { continue; }
if (i == 3 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_3)) { continue; }
if (i == 4 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_4)) { continue; }
if (i == 5 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_5)) { continue; }
if (i == 6 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_6)) { continue; }
kpad_data = {};
WPADExtensionType type;
if (WPADProbe(static_cast<WPADChan>(i), &type) != 0) {
continue;
}

uint32_t convertedButtons = 0;
kpad_data = {};
if (KPADReadEx(static_cast<KPADChan>(i), &kpad_data, 1, &kpad_error) > 0) {
DEBUG_FUNCTION_LINE_ERR("KPAD chan %d success %d %d", i, kpad_error, kpad_data.extensionType);
if (kpad_error == KPAD_ERROR_OK && kpad_data.extensionType != 0xFF) {
if (kpad_data.extensionType == WPAD_EXT_CORE || kpad_data.extensionType == WPAD_EXT_NUNCHUK) {
buttonsHold |= remapWiiMoteButtons(kpad_data.hold);
convertedButtons = remapWiiMoteButtons(kpad_data.hold);
} else if (kpad_data.extensionType == WPAD_EXT_PRO_CONTROLLER) {
buttonsHold |= remapProButtons(kpad_data.pro.hold);
convertedButtons = remapProButtons(kpad_data.pro.hold);
} else {
buttonsHold |= remapClassicButtons(kpad_data.classic.hold);
convertedButtons = remapClassicButtons(kpad_data.classic.hold);
}
}
}

buttonsHoldAbort |= convertedButtons;

if (i == 0 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_0)) { continue; }
if (i == 1 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_1)) { continue; }
if (i == 2 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_2)) { continue; }
if (i == 3 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_3)) { continue; }
if (i == 4 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_4)) { continue; }
if (i == 5 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_5)) { continue; }
if (i == 6 && !(options.controllerMask & BUTTON_COMBO_MODULE_CONTROLLER_WPAD_6)) { continue; }

buttonsHold |= convertedButtons;
}

if (buttonsHold == lastHold) {
Expand All @@ -655,16 +690,27 @@ ButtonComboModule_Error ButtonComboManager::DetectButtonCombo_Blocking(const But

if (holdFor >= holdAbortTarget && lastHold == abortButton) {
DEBUG_FUNCTION_LINE("Aborted button combo detection");
return BUTTON_COMBO_MODULE_ERROR_ABORTED;
result = BUTTON_COMBO_MODULE_ERROR_ABORTED;
break;
}

if (holdFor >= holdForTarget) {
DEBUG_FUNCTION_LINE_INFO("Detected button combo %08X", lastHold);
outButtonCombo = static_cast<ButtonComboModule_Buttons>(lastHold);
result = BUTTON_COMBO_MODULE_ERROR_SUCCESS;
break;
}
OSSleepTicks(OSMillisecondsToTicks(16));
}

return BUTTON_COMBO_MODULE_ERROR_SUCCESS;
if (doDisableProController) {
WPADEnableURCC(false);
}

if (doShutdownKPAD) {
KPADShutdown();
}


return result;
}
76 changes: 0 additions & 76 deletions source/DRCAttachCallback.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions source/DRCAttachCallback.h

This file was deleted.

28 changes: 13 additions & 15 deletions source/function_patches.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "ButtonComboInfo.h"
#include "ButtonComboManager.h"
#include "DRCAttachCallback.h"
#include "globals.h"

#include <function_patcher/fpatching_defines.h>
#include <logger.h>

#include <coreinit/messagequeue.h>
#include <padscore/wpad.h>
#include <vpad/input.h>

Expand All @@ -29,28 +28,27 @@ DECL_FUNCTION(void, WPADRead, WPADChan chan, WPADStatus *data) {
gButtonComboManager->UpdateInputWPAD(chan, data);
}
}
struct WUT_PACKED CCRCDCCallbackData {
uint32_t attached;
VPADChan chan;
WUT_UNKNOWN_BYTES(6);
};

DECL_FUNCTION(void, __VPADBASEAttachCallback, CCRCDCCallbackData *data, void *context) {
real___VPADBASEAttachCallback(data, context);

static uint32_t lastData0 = 0;
DECL_FUNCTION(uint32_t, OSReceiveMessage, OSMessageQueue *queue, OSMessage *message, uint32_t flags) {
const uint32_t res = real_OSReceiveMessage(queue, message, flags);
if (queue == OSGetSystemMessageQueue()) {
if (message != nullptr && res) {
if (lastData0 != message->args[0]) {
if (message->args[0] == 0xFACEF000) {
InitDRCAttachCallbacks();
}
}
lastData0 = message->args[0];
if (data && data->attached) {
if (gButtonComboManager) {
const bool block = gButtonComboManager->hasActiveComboWithTVButton();
VPADSetTVMenuInvalid(data->chan, block);
}
}
return res;
}

function_replacement_data_t function_replacements[] = {
REPLACE_FUNCTION(VPADRead, LIBRARY_VPAD, VPADRead),
REPLACE_FUNCTION(WPADRead, LIBRARY_PADSCORE, WPADRead),
REPLACE_FUNCTION(OSReceiveMessage, LIBRARY_COREINIT, OSReceiveMessage),
REPLACE_FUNCTION_VIA_ADDRESS(__VPADBASEAttachCallback, 0x31000000 + 0x0200146c - 0x00EE0100, 0x0200146c - 0x00EE0100),
};

uint32_t function_replacements_size = sizeof(function_replacements) / sizeof(function_replacement_data_t);
8 changes: 0 additions & 8 deletions source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ButtonComboManager.h"
#include "DRCAttachCallback.h"
#include "function_patches.h"
#include "globals.h"
#include "logger.h"
Expand Down Expand Up @@ -31,11 +30,6 @@ WUMS_INITIALIZE() {
OSFatal("ButtonComboModule: Failed to patch ButtonComboModule function");
}
}
for (uint32_t i = 0; i < drc_function_replacements_size; i++) {
if (FunctionPatcher_AddFunctionPatch(&drc_function_replacements[i], nullptr, &patchSuccess) != FUNCTION_PATCHER_RESULT_SUCCESS || !patchSuccess) {
OSFatal("ButtonComboModule: Failed to patch ButtonComboModule function");
}
}
DEBUG_FUNCTION_LINE("Patch ButtonComboModule functions finished");


Expand All @@ -51,8 +45,6 @@ WUMS_DEINITIALIZE() {
WUMS_APPLICATION_STARTS() {
initLogging();
OSReport("Running ButtonComboModule " MODULE_VERSION MODULE_VERSION_EXTRA "\n");

InitDRCAttachCallbacks();
}

WUMS_APPLICATION_ENDS() {
Expand Down