wudd/source/main.cpp

159 lines
3.8 KiB
C++
Raw Permalink Normal View History

#include "MainApplicationState.h"
#include "input/VPADInput.h"
#include "utils/WiiUScreen.h"
#include "utils/logger.h"
#include <coreinit/debug.h>
#include <coreinit/energysaver.h>
#include <coreinit/title.h>
#include <input/CombinedInput.h>
2022-07-26 08:16:27 +02:00
#include <input/WPADInput.h>
#include <mocha/disc_interface.h>
#include <mocha/mocha.h>
2022-07-26 08:16:27 +02:00
#include <ntfs.h>
#include <padscore/kpad.h>
#include <thread>
#include <whb/log.h>
#include <whb/proc.h>
2021-10-09 00:58:55 +02:00
void initMochaLib();
2021-10-09 00:58:55 +02:00
void deInitMochaLib();
2021-10-17 14:31:33 +02:00
void main_loop();
2021-10-09 00:58:55 +02:00
bool slibMochaMount = false;
2021-10-09 00:58:55 +02:00
extern "C" void ACPInitialize();
2021-10-09 00:58:55 +02:00
static uint32_t
procHomeButtonDeniedCustom(void *context) {
if (!gBlockHomeButton && gRunFromHBL) {
WHBProcStopRunning();
} else {
gBlockHomeButtonCooldown = 5;
}
return 0;
}
2021-10-09 00:58:55 +02:00
int main(int argc, char **argv) {
initLogging();
2021-10-09 00:58:55 +02:00
DEBUG_FUNCTION_LINE("Hello from wudump!");
WHBProcInit();
WiiUScreen::Init();
initMochaLib();
ACPInitialize();
2021-10-09 00:58:55 +02:00
uint64_t titleID = OSGetTitleID();
if (titleID == 0x0005000013374842 ||
titleID == 0x000500101004A000 ||
titleID == 0x000500101004A100 ||
titleID == 0x000500101004A200) {
gRunFromHBL = true;
ProcUIClearCallbacks();
ProcUIRegisterCallback(PROCUI_CALLBACK_HOME_BUTTON_DENIED,
&procHomeButtonDeniedCustom, nullptr, 100);
} else {
gRunFromHBL = false;
}
uint32_t isAPDEnabled;
IMIsAPDEnabled(&isAPDEnabled);
if (isAPDEnabled) {
DEBUG_FUNCTION_LINE("Disable auto shutdown");
IMDisableAPD();
}
2021-10-09 00:58:55 +02:00
ntfs_mount_count = ntfsMountAll((ntfs_md **) &ntfs_mounts, NTFS_DEFAULT | NTFS_RECOVER);
for (int i = 0; i < ntfs_mount_count; i++) {
DEBUG_FUNCTION_LINE("%s:", ntfs_mounts[i].name);
}
2021-10-17 14:31:33 +02:00
WPADInput::init();
gFSAClientHandle = FSAAddClient(nullptr);
if (!gFSAClientHandle) {
OSFatal("FSAAddClient failed");
}
if (Mocha_UnlockFSClientEx(gFSAClientHandle) != MOCHA_RESULT_SUCCESS) {
OSFatal("Failed to unlock FSAClientHandle");
}
2021-10-09 00:58:55 +02:00
main_loop();
FSADelClient(gFSAClientHandle);
2021-10-17 14:31:33 +02:00
WPADInput::close();
2021-10-09 00:58:55 +02:00
if (ntfs_mounts != nullptr) {
int i;
2021-10-09 00:58:55 +02:00
for (i = 0; i < ntfs_mount_count; i++) {
ntfsUnmount(ntfs_mounts[i].name, true);
}
free(ntfs_mounts);
}
if (isAPDEnabled) {
DEBUG_FUNCTION_LINE("Enable auto shutdown");
IMEnableAPD();
}
deInitMochaLib();
2021-10-09 00:58:55 +02:00
WiiUScreen::DeInit();
WHBProcShutdown();
return 0;
}
void main_loop() {
DEBUG_FUNCTION_LINE("Creating state");
std::unique_ptr<MainApplicationState> state = std::make_unique<MainApplicationState>();
CombinedInput baseInput;
2021-10-17 14:31:33 +02:00
VPadInput vpadInput;
WPADInput wpadInputs[4] = {
WPAD_CHAN_0,
WPAD_CHAN_1,
WPAD_CHAN_2,
2022-07-26 08:16:27 +02:00
WPAD_CHAN_3};
2021-10-09 00:58:55 +02:00
DEBUG_FUNCTION_LINE("Entering main loop");
while (WHBProcIsRunning()) {
baseInput.reset();
if (vpadInput.update(1280, 720)) {
baseInput.combine(vpadInput);
2021-10-17 14:31:33 +02:00
}
2022-07-26 08:16:27 +02:00
for (auto &wpadInput : wpadInputs) {
if (wpadInput.update(1280, 720)) {
baseInput.combine(wpadInput);
}
}
baseInput.process();
state->update(&baseInput);
state->render();
2021-10-09 00:58:55 +02:00
}
}
void initMochaLib() {
slibMochaMount = false;
MochaUtilsStatus res = Mocha_InitLibrary();
if (res != MOCHA_RESULT_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Mocha_InitLibrary failed: %s", Mocha_GetStatusStr(res));
OSFatal("Failed to init libmocha. Please update MochaPayload.");
2021-10-09 00:58:55 +02:00
} else {
slibMochaMount = true;
2021-10-09 00:58:55 +02:00
}
}
void deInitMochaLib() {
if (slibMochaMount) {
Mocha_DeinitLibrary();
2021-10-09 00:58:55 +02:00
}
Mocha_sdio_disc_interface.shutdown();
Mocha_usb_disc_interface.shutdown();
2021-10-09 00:58:55 +02:00
}