2023-01-24 21:11:53 +01:00
# include "Hints.h"
# include "utils/config.h"
# include <coreinit/cache.h>
# include <coreinit/thread.h>
# include <notifications/notification_defines.h>
# include <notifications/notifications.h>
2023-03-26 17:28:27 +02:00
# include <sdutils/sdutils.h>
2023-12-22 15:32:55 +01:00
# include <sys/fcntl.h>
# include <sys/unistd.h>
2023-01-24 21:11:53 +01:00
# include <thread>
std : : unique_ptr < std : : thread > sShowHintThread ;
static bool sShutdownHintThread = false ;
2023-12-22 15:32:55 +01:00
bool SaveHintShownToStorage ( bool hintShown ) ;
2023-01-24 21:11:53 +01:00
void ShowHints ( ) {
bool isOverlayReady = false ;
while ( ! sShutdownHintThread & &
NotificationModule_IsOverlayReady ( & isOverlayReady ) = = NOTIFICATION_MODULE_RESULT_SUCCESS & & ! isOverlayReady ) {
OSSleepTicks ( OSMillisecondsToTicks ( 16 ) ) ;
}
if ( sShutdownHintThread | | ! isOverlayReady ) {
return ;
}
2023-03-12 16:17:01 +01:00
2023-03-26 17:28:27 +02:00
bool isMounted = false ;
if ( SDUtils_IsSdCardMounted ( & isMounted ) ! = SDUTILS_RESULT_SUCCESS ) {
DEBUG_FUNCTION_LINE_ERR ( " SDUtils_IsSdCardMounted failed " ) ;
}
if ( isMounted ) {
const char * tmp_file = " fs:/vol/external01/wiiu/write_lock " ;
int fd = - 1 ;
if ( ( fd = open ( tmp_file , O_CREAT | O_TRUNC | O_RDWR ) ) < 0 ) {
DEBUG_FUNCTION_LINE_VERBOSE ( " SD Card mounted but not writable " ) ;
NotificationModuleStatus err ;
NMColor red = { 237 , 28 , 36 , 255 } ;
NotificationModuleHandle outHandle ;
if ( ( err = NotificationModule_SetDefaultValue ( NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC , NOTIFICATION_MODULE_DEFAULT_OPTION_BACKGROUND_COLOR , red ) ) ! = NOTIFICATION_MODULE_RESULT_SUCCESS | |
( err = NotificationModule_AddDynamicNotification ( " Failed to write to the sd card. Please restart the console and make sure the sd card is not write locked. " , & outHandle ) ) ! = NOTIFICATION_MODULE_RESULT_SUCCESS ) {
DEBUG_FUNCTION_LINE_ERR ( " Failed to display notification: %s " , NotificationModule_GetStatusStr ( err ) ) ;
}
} else {
DEBUG_FUNCTION_LINE_VERBOSE ( " SD Card is mounted and writeable " ) ;
close ( fd ) ;
remove ( tmp_file ) ;
2023-03-12 16:17:01 +01:00
}
} else {
2023-03-26 17:28:27 +02:00
DEBUG_FUNCTION_LINE_VERBOSE ( " SD Card is not mounted " ) ;
2023-03-12 16:17:01 +01:00
}
2023-01-24 21:11:53 +01:00
if ( ! gConfigMenuHintShown ) {
NotificationModuleStatus err ;
if ( ( err = NotificationModule_SetDefaultValue ( NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO , NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT , 15.0f ) ) = = NOTIFICATION_MODULE_RESULT_SUCCESS & &
( err = NotificationModule_AddInfoNotification ( " Tip: You can open a configuration menu by pressing \ue052 + \ue07A + \ue046 " ) ) = = NOTIFICATION_MODULE_RESULT_SUCCESS ) {
2023-12-22 15:32:55 +01:00
gConfigMenuHintShown = true ;
SaveHintShownToStorage ( gConfigMenuHintShown ) ;
2023-01-24 21:11:53 +01:00
} else {
DEBUG_FUNCTION_LINE_ERR ( " Failed to show Notification: %d %s " , err , NotificationModule_GetStatusStr ( err ) ) ;
}
}
}
2023-12-22 15:32:55 +01:00
bool SaveHintShownToStorage ( bool hintShown ) {
WUPSStorageError storageError ;
auto subItem = WUPSStorageAPI : : GetSubItem ( CAT_OTHER , storageError ) ;
if ( ! subItem ) {
DEBUG_FUNCTION_LINE_ERR ( " Failed to get sub category " ) ;
return false ;
}
storageError = subItem - > Store ( CONFIG_MENU_HINT_SHOWN_ID , hintShown ) ;
if ( storageError ! = WUPS_STORAGE_ERROR_SUCCESS ) {
DEBUG_FUNCTION_LINE_ERR ( " Failed to store hint shown " ) ;
return false ;
}
return WUPSStorageAPI : : SaveStorage ( ) = = WUPS_STORAGE_ERROR_SUCCESS ;
}
2023-01-24 21:11:53 +01:00
void StartHintThread ( ) {
2023-03-12 16:17:01 +01:00
sShowHintThread . reset ( ) ;
sShutdownHintThread = false ;
sShowHintThread = std : : make_unique < std : : thread > ( ShowHints ) ;
2023-01-24 21:11:53 +01:00
}
void StopHintThread ( ) {
if ( sShowHintThread ! = nullptr ) {
sShutdownHintThread = true ;
OSMemoryBarrier ( ) ;
sShowHintThread - > join ( ) ;
sShowHintThread . reset ( ) ;
}
}