This repository has been archived on 2022-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Luma3DS-3GX/sysmodules/rosalina/source/menu.c

381 lines
9.8 KiB
C
Raw Normal View History

/*
* This file is part of Luma3DS
2020-04-25 14:26:21 +02:00
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include <3ds.h>
#include "menu.h"
#include "draw.h"
#include "fmt.h"
#include "memory.h"
#include "ifile.h"
#include "menus.h"
#include "utils.h"
#include "menus/n3ds.h"
2017-12-21 18:14:04 +01:00
#include "menus/cheats.h"
#include "minisoc.h"
bool isHidInitialized = false;
// libctru redefinition:
bool hidShouldUseIrrst(void)
{
// ir:rst exposes only two sessions :(
return false;
}
2020-05-15 03:06:52 +02:00
static inline u32 convertHidKeys(u32 keys)
{
2020-05-15 03:06:52 +02:00
// Nothing to do yet
return keys;
}
u32 waitInputWithTimeout(s32 msec)
{
s32 n = 0;
u32 keys;
do
{
svcSleepThread(1 * 1000 * 1000LL);
2020-05-15 03:06:52 +02:00
Draw_Lock();
2020-05-15 21:00:13 +02:00
if (!isHidInitialized || menuShouldExit)
2020-05-15 03:06:52 +02:00
{
keys = 0;
Draw_Unlock();
break;
}
n++;
hidScanInput();
keys = convertHidKeys(hidKeysDown()) | (convertHidKeys(hidKeysDownRepeat()) & DIRECTIONAL_KEYS);
2020-05-15 03:06:52 +02:00
Draw_Unlock();
2020-05-15 21:00:13 +02:00
} while (keys == 0 && !menuShouldExit && isHidInitialized && (msec < 0 || n < msec));
return keys;
}
u32 waitInput(void)
{
return waitInputWithTimeout(-1);
}
2020-05-15 03:06:52 +02:00
static u32 scanHeldKeys(void)
{
u32 keys;
Draw_Lock();
2020-05-15 21:00:13 +02:00
if (!isHidInitialized || menuShouldExit)
2020-05-15 03:06:52 +02:00
keys = 0;
else
{
hidScanInput();
keys = convertHidKeys(hidKeysHeld());
}
Draw_Unlock();
return keys;
}
u32 waitComboWithTimeout(s32 msec)
2017-06-06 01:21:52 +02:00
{
s32 n = 0;
2020-05-15 03:06:52 +02:00
u32 keys = 0;
u32 tempKeys = 0;
2017-06-06 01:21:52 +02:00
2020-05-15 03:06:52 +02:00
// Wait for nothing to be pressed
2020-05-15 21:00:13 +02:00
while (scanHeldKeys() != 0 && !menuShouldExit && isHidInitialized && (msec < 0 || n < msec))
2020-05-15 03:06:52 +02:00
{
svcSleepThread(1 * 1000 * 1000LL);
n++;
}
2020-05-15 21:00:13 +02:00
if (menuShouldExit || !isHidInitialized || !(msec < 0 || n < msec))
2020-05-15 03:06:52 +02:00
return 0;
2017-06-06 01:21:52 +02:00
do
{
svcSleepThread(1 * 1000 * 1000LL);
2020-05-15 03:06:52 +02:00
n++;
tempKeys = scanHeldKeys();
for (u32 i = 0x10000; i > 0; i--)
{
if (tempKeys != scanHeldKeys()) break;
if (i == 1) keys = tempKeys;
}
}
2020-05-15 21:00:13 +02:00
while((keys == 0 || scanHeldKeys() != 0) && !menuShouldExit && isHidInitialized && (msec < 0 || n < msec));
2017-06-06 01:21:52 +02:00
return keys;
2017-06-06 01:21:52 +02:00
}
u32 waitCombo(void)
{
return waitComboWithTimeout(-1);
2017-06-06 01:21:52 +02:00
}
static MyThread menuThread;
static u8 ALIGN(8) menuThreadStack[0x1000];
static u8 batteryLevel = 255;
MyThread *menuCreateThread(void)
{
if(R_FAILED(MyThread_Create(&menuThread, menuThreadMain, menuThreadStack, 0x1000, 52, CORE_SYSTEM)))
svcBreak(USERBREAK_PANIC);
return &menuThread;
}
extern bool isN3DS;
u32 menuCombo;
void menuThreadMain(void)
{
if(!isN3DS)
{
rosalinaMenu.nbItems--;
2020-05-15 03:29:17 +02:00
for(u32 i = 6; i <= rosalinaMenu.nbItems; i++)
rosalinaMenu.items[i] = rosalinaMenu.items[i+1];
}
else
N3DSMenu_UpdateStatus();
while (!isServiceUsable("ac:u") || !isServiceUsable("hid:USER"))
svcSleepThread(500 * 1000 * 1000LL);
hidInit(); // assume this doesn't fail
isHidInitialized = true;
2020-05-15 21:00:13 +02:00
while(!preTerminationRequested)
{
2020-05-15 21:00:13 +02:00
if (menuShouldExit)
continue;
Cheat_ApplyCheats();
2020-05-15 03:06:52 +02:00
if((scanHeldKeys() & menuCombo) == menuCombo)
2017-12-21 18:14:04 +01:00
{
menuEnter();
if(isN3DS) N3DSMenu_UpdateStatus();
menuShow(&rosalinaMenu);
menuLeave();
2017-12-21 18:14:04 +01:00
}
svcSleepThread(50 * 1000 * 1000LL);
}
}
static s32 menuRefCount = 0;
void menuEnter(void)
{
Draw_Lock();
2020-05-15 21:00:13 +02:00
if(!menuShouldExit && menuRefCount == 0)
{
2020-05-15 21:00:13 +02:00
menuRefCount++;
svcKernelSetState(0x10000, 1);
svcSleepThread(5 * 1000 * 100LL);
if (Draw_AllocateFramebufferCache() == 0)
{
// Oops
menuRefCount = 0;
svcKernelSetState(0x10000, 1);
svcSleepThread(5 * 1000 * 100LL);
}
Draw_SetupFramebuffer();
}
Draw_Unlock();
}
void menuLeave(void)
{
svcSleepThread(50 * 1000 * 1000);
Draw_Lock();
if(--menuRefCount == 0)
{
Draw_RestoreFramebuffer();
Draw_FreeFramebufferCache();
svcKernelSetState(0x10000, 1);
}
Draw_Unlock();
}
static void menuDraw(Menu *menu, u32 selected)
{
char versionString[16];
s64 out;
u32 version, commitHash;
bool isRelease;
bool isMcuHwcRegistered;
if(R_SUCCEEDED(srvIsServiceRegistered(&isMcuHwcRegistered, "mcu::HWC")) && isMcuHwcRegistered && R_SUCCEEDED(mcuHwcInit()))
{
if(R_FAILED(MCUHWC_GetBatteryLevel(&batteryLevel)))
batteryLevel = 255;
mcuHwcExit();
}
else
batteryLevel = 255;
svcGetSystemInfo(&out, 0x10000, 0);
version = (u32)out;
svcGetSystemInfo(&out, 0x10000, 1);
commitHash = (u32)out;
svcGetSystemInfo(&out, 0x10000, 0x200);
isRelease = (bool)out;
if(GET_VERSION_REVISION(version) == 0)
sprintf(versionString, "v%lu.%lu", GET_VERSION_MAJOR(version), GET_VERSION_MINOR(version));
else
sprintf(versionString, "v%lu.%lu.%lu", GET_VERSION_MAJOR(version), GET_VERSION_MINOR(version), GET_VERSION_REVISION(version));
Draw_DrawString(10, 10, COLOR_TITLE, menu->title);
for(u32 i = 0; i < 15; i++)
{
if(i >= menu->nbItems)
break;
Draw_DrawString(30, 30 + i * SPACING_Y, COLOR_WHITE, menu->items[i].title);
Draw_DrawCharacter(10, 30 + i * SPACING_Y, COLOR_TITLE, i == selected ? '>' : ' ');
}
if(miniSocEnabled)
{
char ipBuffer[17];
u32 ip = socGethostid();
u8 *addr = (u8 *)&ip;
int n = sprintf(ipBuffer, "%hhu.%hhu.%hhu.%hhu", addr[0], addr[1], addr[2], addr[3]);
Draw_DrawString(SCREEN_BOT_WIDTH - 10 - SPACING_X * n, 10, COLOR_WHITE, ipBuffer);
}
2017-06-25 00:14:07 +02:00
Draw_DrawFormattedString(SCREEN_BOT_WIDTH - 10 - 4 * SPACING_X, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, " ");
if(batteryLevel != 255)
Draw_DrawFormattedString(SCREEN_BOT_WIDTH - 10 - 4 * SPACING_X, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, "%02hhu%%", batteryLevel);
else
Draw_DrawString(SCREEN_BOT_WIDTH - 10 - 4 * SPACING_X, SCREEN_BOT_HEIGHT - 20, COLOR_WHITE, " ");
if(isRelease)
Draw_DrawFormattedString(10, SCREEN_BOT_HEIGHT - 20, COLOR_TITLE, "Luma3DS %s", versionString);
else
Draw_DrawFormattedString(10, SCREEN_BOT_HEIGHT - 20, COLOR_TITLE, "Luma3DS %s-%08lx", versionString, commitHash);
Draw_FlushFramebuffer();
}
void menuShow(Menu *root)
{
u32 selectedItem = 0;
Menu *currentMenu = root;
u32 nbPreviousMenus = 0;
Menu *previousMenus[0x80];
2017-08-16 23:46:36 +02:00
u32 previousSelectedItems[0x80];
Draw_Lock();
Draw_ClearFramebuffer();
Draw_FlushFramebuffer();
2020-05-15 03:06:52 +02:00
hidSetRepeatParameters(0, 0);
menuDraw(currentMenu, selectedItem);
Draw_Unlock();
2020-05-15 03:06:52 +02:00
bool menuComboReleased = false;
do
{
u32 pressed = waitInputWithTimeout(1000);
2020-05-15 03:06:52 +02:00
if(!menuComboReleased && (scanHeldKeys() & menuCombo) != menuCombo)
{
menuComboReleased = true;
Draw_Lock();
hidSetRepeatParameters(200, 100);
Draw_Unlock();
}
if(pressed & KEY_A)
{
Draw_Lock();
Draw_ClearFramebuffer();
Draw_FlushFramebuffer();
Draw_Unlock();
switch(currentMenu->items[selectedItem].action_type)
{
case METHOD:
if(currentMenu->items[selectedItem].method != NULL)
currentMenu->items[selectedItem].method();
break;
case MENU:
2017-08-16 23:46:36 +02:00
previousSelectedItems[nbPreviousMenus] = selectedItem;
previousMenus[nbPreviousMenus++] = currentMenu;
currentMenu = currentMenu->items[selectedItem].menu;
selectedItem = 0;
break;
}
Draw_Lock();
Draw_ClearFramebuffer();
Draw_FlushFramebuffer();
Draw_Unlock();
}
2020-05-15 03:06:52 +02:00
else if(pressed & KEY_B)
{
Draw_Lock();
Draw_ClearFramebuffer();
Draw_FlushFramebuffer();
Draw_Unlock();
if(nbPreviousMenus > 0)
2017-08-16 23:46:36 +02:00
{
currentMenu = previousMenus[--nbPreviousMenus];
2017-08-16 23:46:36 +02:00
selectedItem = previousSelectedItems[nbPreviousMenus];
}
else
break;
}
2020-05-15 03:06:52 +02:00
else if(pressed & KEY_DOWN)
{
if(++selectedItem >= currentMenu->nbItems)
selectedItem = 0;
}
2020-05-15 03:06:52 +02:00
else if(pressed & KEY_UP)
{
if(selectedItem-- <= 0)
selectedItem = currentMenu->nbItems - 1;
}
Draw_Lock();
menuDraw(currentMenu, selectedItem);
Draw_Unlock();
}
2020-05-15 21:00:13 +02:00
while(!menuShouldExit);
}