2017-07-23 22:44:14 -04:00
/*
* This file is part of Luma3DS
2022-03-13 18:00:00 +00:00
* Copyright ( C ) 2016 - 2021 Aurora Wright , TuxSH
2017-07-23 22:44:14 -04:00
*
* 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 "menus/sysconfig.h"
# include "memory.h"
# include "draw.h"
# include "fmt.h"
# include "utils.h"
# include "ifile.h"
Menu sysconfigMenu = {
" System configuration menu " ,
{
2020-07-15 22:24:08 +01:00
{ " Control Wireless connection " , METHOD , . method = & SysConfigMenu_ControlWifi } ,
2017-07-23 22:44:14 -04:00
{ " Toggle LEDs " , METHOD , . method = & SysConfigMenu_ToggleLEDs } ,
{ " Toggle Wireless " , METHOD , . method = & SysConfigMenu_ToggleWireless } ,
2019-07-05 15:04:38 -07:00
{ " Toggle Power Button " , METHOD , . method = & SysConfigMenu_TogglePowerButton } ,
2022-04-15 21:41:23 +01:00
{ " Toggle power to card slot " , METHOD , . method = & SysConfigMenu_ToggleCardIfPower } ,
2020-05-17 16:42:44 +01:00
{ } ,
2017-07-23 22:44:14 -04:00
}
} ;
2020-04-27 20:00:41 +01:00
bool isConnectionForced = false ;
2017-07-23 22:44:14 -04:00
void SysConfigMenu_ToggleLEDs ( void )
{
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
do
{
Draw_Lock ( ) ;
Draw_DrawString ( 10 , 10 , COLOR_TITLE , " System configuration menu " ) ;
Draw_DrawString ( 10 , 30 , COLOR_WHITE , " Press A to toggle, press B to go back. " ) ;
Draw_DrawString ( 10 , 50 , COLOR_RED , " WARNING: " ) ;
Draw_DrawString ( 10 , 60 , COLOR_WHITE , " * Entering sleep mode will reset the LED state! " ) ;
Draw_DrawString ( 10 , 70 , COLOR_WHITE , " * LEDs cannot be toggled when the battery is low! " ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
u32 pressed = waitInputWithTimeout ( 1000 ) ;
2020-05-15 02:06:52 +01:00
if ( pressed & KEY_A )
2017-07-23 22:44:14 -04:00
{
2017-10-05 18:41:11 +02:00
mcuHwcInit ( ) ;
2017-07-23 22:44:14 -04:00
u8 result ;
2018-01-01 11:05:22 -06:00
MCUHWC_ReadRegister ( 0x28 , & result , 1 ) ;
2017-10-05 18:41:11 +02:00
result = ~ result ;
2018-01-01 11:05:22 -06:00
MCUHWC_WriteRegister ( 0x28 , & result , 1 ) ;
2017-10-05 18:41:11 +02:00
mcuHwcExit ( ) ;
2017-07-23 22:44:14 -04:00
}
2020-05-15 02:06:52 +01:00
else if ( pressed & KEY_B )
2017-07-23 22:44:14 -04:00
return ;
}
2020-05-15 20:00:13 +01:00
while ( ! menuShouldExit ) ;
2017-07-23 22:44:14 -04:00
}
void SysConfigMenu_ToggleWireless ( void )
{
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
2021-01-27 23:14:59 +00:00
bool nwmRunning = isServiceUsable ( " nwm::EXT " ) ;
2017-08-11 04:29:38 +02:00
2017-07-23 22:44:14 -04:00
do
{
Draw_Lock ( ) ;
Draw_DrawString ( 10 , 10 , COLOR_TITLE , " System configuration menu " ) ;
Draw_DrawString ( 10 , 30 , COLOR_WHITE , " Press A to toggle, press B to go back. " ) ;
u8 wireless = ( * ( vu8 * ) ( ( 0x10140000 | ( 1u < < 31 ) ) + 0x180 ) ) ;
2017-08-11 04:29:38 +02:00
if ( nwmRunning )
{
Draw_DrawString ( 10 , 50 , COLOR_WHITE , " Current status: " ) ;
Draw_DrawString ( 100 , 50 , ( wireless ? COLOR_GREEN : COLOR_RED ) , ( wireless ? " ON " : " OFF " ) ) ;
}
else
{
Draw_DrawString ( 10 , 50 , COLOR_RED , " NWM isn't running. " ) ;
Draw_DrawString ( 10 , 60 , COLOR_RED , " If you're currently on Test Menu, " ) ;
Draw_DrawString ( 10 , 70 , COLOR_RED , " exit then press R+RIGHT to toggle the WiFi. " ) ;
Draw_DrawString ( 10 , 80 , COLOR_RED , " Otherwise, simply exit and wait a few seconds. " ) ;
}
2017-07-23 22:44:14 -04:00
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
u32 pressed = waitInputWithTimeout ( 1000 ) ;
2020-05-15 02:06:52 +01:00
if ( pressed & KEY_A & & nwmRunning )
2017-07-23 22:44:14 -04:00
{
nwmExtInit ( ) ;
NWMEXT_ControlWirelessEnabled ( ! wireless ) ;
nwmExtExit ( ) ;
}
2020-05-15 02:06:52 +01:00
else if ( pressed & KEY_B )
2017-07-23 22:44:14 -04:00
return ;
}
2020-05-15 20:00:13 +01:00
while ( ! menuShouldExit ) ;
2017-07-23 22:44:14 -04:00
}
2019-07-05 15:04:38 -07:00
2020-04-28 00:05:55 +01:00
void SysConfigMenu_UpdateStatus ( bool control )
2020-04-27 20:00:41 +01:00
{
2021-01-27 23:22:09 +00:00
MenuItem * item = & sysconfigMenu . items [ 0 ] ;
2020-04-27 20:00:41 +01:00
if ( control )
{
item - > title = " Control Wireless connection " ;
item - > method = & SysConfigMenu_ControlWifi ;
}
else
{
item - > title = " Disable forced wireless connection " ;
item - > method = & SysConfigMenu_DisableForcedWifiConnection ;
}
}
static bool SysConfigMenu_ForceWifiConnection ( int slot )
2019-07-21 19:05:37 +02:00
{
char ssid [ 0x20 + 1 ] = { 0 } ;
2020-04-28 00:05:55 +01:00
isConnectionForced = false ;
2019-07-21 19:05:37 +02:00
2020-04-28 00:05:55 +01:00
if ( R_FAILED ( acInit ( ) ) )
return false ;
2020-04-26 20:55:52 +01:00
2019-07-23 00:13:17 +02:00
acuConfig config = { 0 } ;
ACU_CreateDefaultConfig ( & config ) ;
ACU_SetNetworkArea ( & config , 2 ) ;
ACU_SetAllowApType ( & config , 1 < < slot ) ;
ACU_SetRequestEulaVersion ( & config ) ;
2019-07-21 19:05:37 +02:00
Handle connectEvent = 0 ;
svcCreateEvent ( & connectEvent , RESET_ONESHOT ) ;
bool forcedConnection = false ;
2019-07-23 00:13:17 +02:00
if ( R_SUCCEEDED ( ACU_ConnectAsync ( & config , connectEvent ) ) )
2019-07-21 19:05:37 +02:00
{
2020-04-28 00:05:55 +01:00
if ( R_SUCCEEDED ( svcWaitSynchronization ( connectEvent , - 1 ) ) & & R_SUCCEEDED ( ACU_GetSSID ( ssid ) ) )
2019-07-21 19:05:37 +02:00
forcedConnection = true ;
}
svcCloseHandle ( connectEvent ) ;
2020-04-27 20:00:41 +01:00
if ( forcedConnection )
{
isConnectionForced = true ;
SysConfigMenu_UpdateStatus ( false ) ;
}
2020-04-28 00:05:55 +01:00
else
acExit ( ) ;
2020-04-26 20:55:52 +01:00
2019-07-21 19:05:37 +02:00
char infoString [ 80 ] = { 0 } ;
u32 infoStringColor = forcedConnection ? COLOR_GREEN : COLOR_RED ;
if ( forcedConnection )
sprintf ( infoString , " Succesfully forced a connection to: %s " , ssid ) ;
else
sprintf ( infoString , " Failed to connect to slot %d " , slot + 1 ) ;
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
do
{
Draw_Lock ( ) ;
Draw_DrawString ( 10 , 10 , COLOR_TITLE , " System configuration menu " ) ;
Draw_DrawString ( 10 , 30 , infoStringColor , infoString ) ;
Draw_DrawString ( 10 , 40 , COLOR_WHITE , " Press B to go back. " ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
u32 pressed = waitInputWithTimeout ( 1000 ) ;
2020-05-15 02:06:52 +01:00
if ( pressed & KEY_B )
2020-04-27 20:00:41 +01:00
break ;
2019-07-21 19:05:37 +02:00
}
2020-05-15 20:00:13 +01:00
while ( ! menuShouldExit ) ;
2020-04-27 20:00:41 +01:00
return forcedConnection ;
2019-07-21 19:05:37 +02:00
}
2019-07-05 15:04:38 -07:00
void SysConfigMenu_TogglePowerButton ( void )
{
2019-07-05 15:37:54 -07:00
u32 mcuIRQMask ;
2020-04-17 00:36:07 +02:00
2019-07-05 15:04:38 -07:00
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
2020-04-17 00:36:07 +02:00
2019-07-05 15:04:38 -07:00
mcuHwcInit ( ) ;
2019-07-05 15:37:54 -07:00
MCUHWC_ReadRegister ( 0x18 , ( u8 * ) & mcuIRQMask , 4 ) ;
2019-07-05 15:04:38 -07:00
mcuHwcExit ( ) ;
do
{
Draw_Lock ( ) ;
Draw_DrawString ( 10 , 10 , COLOR_TITLE , " System configuration menu " ) ;
Draw_DrawString ( 10 , 30 , COLOR_WHITE , " Press A to toggle, press B to go back. " ) ;
Draw_DrawString ( 10 , 50 , COLOR_WHITE , " Current status: " ) ;
2019-07-05 15:37:54 -07:00
Draw_DrawString ( 100 , 50 , ( ( ( mcuIRQMask & 0x00000001 ) = = 0x00000001 ) ? COLOR_RED : COLOR_GREEN ) , ( ( ( mcuIRQMask & 0x00000001 ) = = 0x00000001 ) ? " DISABLED " : " ENABLED " ) ) ;
2020-04-17 00:36:07 +02:00
2019-07-05 15:04:38 -07:00
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
2020-04-17 00:36:07 +02:00
2019-07-05 15:04:38 -07:00
u32 pressed = waitInputWithTimeout ( 1000 ) ;
2020-05-15 02:06:52 +01:00
if ( pressed & KEY_A )
2019-07-05 15:04:38 -07:00
{
mcuHwcInit ( ) ;
2019-07-05 15:37:54 -07:00
MCUHWC_ReadRegister ( 0x18 , ( u8 * ) & mcuIRQMask , 4 ) ;
mcuIRQMask ^ = 0x00000001 ;
MCUHWC_WriteRegister ( 0x18 , ( u8 * ) & mcuIRQMask , 4 ) ;
2019-07-05 15:04:38 -07:00
mcuHwcExit ( ) ;
}
2020-05-15 02:06:52 +01:00
else if ( pressed & KEY_B )
2019-07-05 15:04:38 -07:00
return ;
}
2020-05-15 20:00:13 +01:00
while ( ! menuShouldExit ) ;
2019-07-05 15:04:38 -07:00
}
2020-04-17 00:36:07 +02:00
2019-07-21 19:05:37 +02:00
void SysConfigMenu_ControlWifi ( void )
{
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
int slot = 0 ;
char slotString [ 12 ] = { 0 } ;
sprintf ( slotString , " >1< 2 3 " ) ;
do
{
Draw_Lock ( ) ;
Draw_DrawString ( 10 , 10 , COLOR_TITLE , " System configuration menu " ) ;
Draw_DrawString ( 10 , 30 , COLOR_WHITE , " Press A to force a connection to slot: " ) ;
Draw_DrawString ( 10 , 40 , COLOR_WHITE , slotString ) ;
Draw_DrawString ( 10 , 60 , COLOR_WHITE , " Press B to go back. " ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
u32 pressed = waitInputWithTimeout ( 1000 ) ;
2020-05-15 02:06:52 +01:00
if ( pressed & KEY_A )
2019-07-21 19:05:37 +02:00
{
2020-04-27 20:00:41 +01:00
if ( SysConfigMenu_ForceWifiConnection ( slot ) )
{
// Connection successfully forced, return from this menu to prevent ac handle refcount leakage.
break ;
}
2019-07-21 19:05:37 +02:00
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
}
2020-05-15 02:06:52 +01:00
else if ( pressed & KEY_LEFT )
2019-07-21 19:05:37 +02:00
{
slotString [ slot * 4 ] = ' ' ;
slotString [ ( slot * 4 ) + 2 ] = ' ' ;
slot - - ;
if ( slot = = - 1 )
slot = 2 ;
slotString [ slot * 4 ] = ' > ' ;
slotString [ ( slot * 4 ) + 2 ] = ' < ' ;
}
2020-05-15 02:06:52 +01:00
else if ( pressed & KEY_RIGHT )
2019-07-21 19:05:37 +02:00
{
slotString [ slot * 4 ] = ' ' ;
slotString [ ( slot * 4 ) + 2 ] = ' ' ;
slot + + ;
if ( slot = = 3 )
slot = 0 ;
slotString [ slot * 4 ] = ' > ' ;
slotString [ ( slot * 4 ) + 2 ] = ' < ' ;
}
2020-05-15 02:06:52 +01:00
else if ( pressed & KEY_B )
2019-07-21 19:05:37 +02:00
return ;
}
2020-05-15 20:00:13 +01:00
while ( ! menuShouldExit ) ;
2019-07-21 19:05:37 +02:00
}
2020-04-27 20:00:41 +01:00
void SysConfigMenu_DisableForcedWifiConnection ( void )
{
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
acExit ( ) ;
SysConfigMenu_UpdateStatus ( true ) ;
do
{
Draw_Lock ( ) ;
Draw_DrawString ( 10 , 10 , COLOR_TITLE , " System configuration menu " ) ;
2020-04-28 00:05:55 +01:00
Draw_DrawString ( 10 , 30 , COLOR_WHITE , " Forced connection successfully disabled. \n Note: auto-connection may remain broken. " ) ;
2020-04-27 20:00:41 +01:00
u32 pressed = waitInputWithTimeout ( 1000 ) ;
2020-05-15 02:06:52 +01:00
if ( pressed & KEY_B )
2020-04-27 20:00:41 +01:00
return ;
}
2020-05-15 20:00:13 +01:00
while ( ! menuShouldExit ) ;
2020-04-27 20:00:41 +01:00
}
2022-04-15 21:41:23 +01:00
void SysConfigMenu_ToggleCardIfPower ( void )
{
Draw_Lock ( ) ;
Draw_ClearFramebuffer ( ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
bool cardIfStatus = false ;
bool updatedCardIfStatus = false ;
do
{
Result res = FSUSER_CardSlotGetCardIFPowerStatus ( & cardIfStatus ) ;
if ( R_FAILED ( res ) ) cardIfStatus = false ;
Draw_Lock ( ) ;
Draw_DrawString ( 10 , 10 , COLOR_TITLE , " System configuration menu " ) ;
u32 posY = Draw_DrawString ( 10 , 30 , COLOR_WHITE , " Press A to toggle, press B to go back. \n \n " ) ;
posY = Draw_DrawString ( 10 , posY , COLOR_WHITE , " Inserting or removing a card will reset the status, \n and you'll need to reinsert the cart if you want to \n play it. \n \n " ) ;
Draw_DrawString ( 10 , posY , COLOR_WHITE , " Current status: " ) ;
Draw_DrawString ( 100 , posY , ! cardIfStatus ? COLOR_RED : COLOR_GREEN , ! cardIfStatus ? " DISABLED " : " ENABLED " ) ;
Draw_FlushFramebuffer ( ) ;
Draw_Unlock ( ) ;
u32 pressed = waitInputWithTimeout ( 1000 ) ;
if ( pressed & KEY_A )
{
if ( ! cardIfStatus )
res = FSUSER_CardSlotPowerOn ( & updatedCardIfStatus ) ;
else
res = FSUSER_CardSlotPowerOff ( & updatedCardIfStatus ) ;
if ( R_SUCCEEDED ( res ) )
cardIfStatus = ! updatedCardIfStatus ;
}
else if ( pressed & KEY_B )
return ;
}
while ( ! menuShouldExit ) ;
}