diff --git a/source/BootUtils.cpp b/source/BootUtils.cpp index 635800f..2dcbb8c 100644 --- a/source/BootUtils.cpp +++ b/source/BootUtils.cpp @@ -3,7 +3,10 @@ #include "MenuUtils.h" #include "logger.h" #include +#include #include +#include +#include #include #include #include @@ -11,12 +14,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include void handleAccountSelection(); @@ -85,22 +90,81 @@ void handleAccountSelection() { nn::act::Finalize(); } +// Exported by nn_cmpt.rpl but not declared in wut's cmpt.h; the Wii U Menu +// calls this before launching vWii titles to signal GamePad support. +extern "C" int32_t CMPTAcctSetDrcCtrlEnabled(int32_t enabled); + +// With no GamePad connected, nn_cmpt refuses vWii launches (-512): its switch of the +// TV to Wii mode (AVMSetDCWiiCompat) also reprograms the GamePad's display pipe +// from avm.rpl's GamePad settings, and with no GamePad those say "off" (what +// GX2SetDRCBuffer with GX2_DRC_RENDER_MODE_DISABLE stores), which dc.rpl rejects +// (DCGetBufferRequirement = -2). Give the GamePad pipe a normal 854x480 mode +// first, with the call GX2SetDRCBuffer makes for GX2_DRC_RENDER_MODE_SINGLE and +// double buffering: AVMSetDRCBufferAttr(bufferingMode, mode, pitch). +static void setDrcModeForWiiMode() { + OSDynLoad_Module avm = nullptr; + void *setDrcBufferAttr = nullptr; + if (OSDynLoad_Acquire("avm.rpl", &avm) != OS_DYNLOAD_OK || + OSDynLoad_FindExport(avm, OS_DYNLOAD_EXPORT_FUNC, "AVMSetDRCBufferAttr", &setDrcBufferAttr) != OS_DYNLOAD_OK) { + DEBUG_FUNCTION_LINE_ERR("AVMSetDRCBufferAttr not found"); + return; + } + [[maybe_unused]] int32_t result = ((int32_t(*)(int32_t, int32_t, int32_t)) setDrcBufferAttr)(2, 1, 896); + DEBUG_FUNCTION_LINE("AVMSetDRCBufferAttr(2, 1, 896) = %d", result); +} + +// Whether a GamePad is connected: it answers a ping over its radio link. Not VPADRead, which +// a plugin can make succeed with no GamePad there (the DRC Read Result Spoofer does). +// Asked for up to half a second, as a GamePad switched on with the console may still be +// connecting when autoboot gets here. +static bool isGamePadConnected() { + for (int i = 0; i < 10; i++) { + if (CCRCDCDevicePing(CCR_CDC_DESTINATION_DRC0) == 0) { + return true; + } + OSSleepTicks(OSMillisecondsToTicks(50)); + } + return false; +} + static void launchvWiiTitle(uint64_t titleId) { // we need to init kpad for cmpt KPADInit(); - // Try to find a screen type that works - CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_BOTH); - if (CMPTCheckScreenState() < 0) { - CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_DRC); + // With a GamePad connected, launch as before: the vWii can show on both screens. Without + // one, the launch hangs on a black screen or is refused, see + // https://github.com/wiiu-env/AutobootModule/issues/67, so launch for the TV alone. + const bool gamePadConnected = isGamePadConnected(); + if (gamePadConnected) { + // Try to find a screen type that works + CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_BOTH); if (CMPTCheckScreenState() < 0) { - CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_TV); + CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_DRC); + if (CMPTCheckScreenState() < 0) { + CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_TV); + } + } + } else { + DEBUG_FUNCTION_LINE("No GamePad connected: launching the vWii for the TV only"); + // GamePad control off and the TV as the only screen + CMPTAcctSetDrcCtrlEnabled(0); + CMPTAcctSetScreenType(CMPT_SCREEN_TYPE_TV); + + // On autoboot this code runs much earlier than a manual selection from + // the boot menu, and launching before the compat subsystem is ready + // results in a black screen. Wait for it, but launch anyway after 5s. + for (int i = 0; i < 50 && CMPTCheckScreenState() < 0; i++) { + OSSleepTicks(OSMillisecondsToTicks(100)); } } uint32_t dataSize = 0; CMPTGetDataSize(&dataSize); + if (!gamePadConnected) { + setDrcModeForWiiMode(); + } + void *dataBuffer = memalign(0x40, dataSize); if (titleId == 0) {