Avoid dependency of libstdc++

This commit is contained in:
Maschell 2026-01-22 13:29:12 +01:00
parent 3792c97d68
commit b52b7ae80a

View File

@ -1,9 +1,10 @@
#include "internal.h" #include "internal.h"
#include "logger.h" #include "logger.h"
#include <stdarg.h>
#include <coreinit/debug.h> #include <coreinit/debug.h>
#include <coreinit/dynload.h> #include <coreinit/dynload.h>
#include <cstdarg>
#include <map>
#include <notifications/notifications.h> #include <notifications/notifications.h>
static OSDynLoad_Module sModuleHandle = nullptr; static OSDynLoad_Module sModuleHandle = nullptr;
@ -59,7 +60,14 @@ static NotificationModuleStatus (*sNMFinishDynamicNotification)(NotificationModu
float shakeDurationInSeconds) = nullptr; float shakeDurationInSeconds) = nullptr;
static bool sLibInitDone = false; static bool sLibInitDone = false;
std::map<NotificationModuleNotificationType, NMDefaultValueStore> sDefaultValues;
#define MAX_NOTIFICATION_TYPES 3
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO < MAX_NOTIFICATION_TYPES);
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR < MAX_NOTIFICATION_TYPES);
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC < MAX_NOTIFICATION_TYPES);
static NMDefaultValueStore sDefaultValues[MAX_NOTIFICATION_TYPES];
static NotificationModuleAPIVersion sNotificationModuleVersion = NOTIFICATION_MODULE_API_VERSION_ERROR; static NotificationModuleAPIVersion sNotificationModuleVersion = NOTIFICATION_MODULE_API_VERSION_ERROR;
@ -155,11 +163,14 @@ NotificationModuleStatus NotificationModule_InitLibrary() {
sNMAddStaticNotificationV2 = nullptr; sNMAddStaticNotificationV2 = nullptr;
} }
sDefaultValues.clear(); for (auto &sDefaultValue : sDefaultValues) {
sDefaultValue = NMDefaultValueStore(); // Reset to defaults
}
sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO]; // Set specific default for Error
sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR].backgroundColor = {237, 28, 36, 255}; if constexpr (NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR < MAX_NOTIFICATION_TYPES) {
sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC]; sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR].backgroundColor = {237, 28, 36, 255};
}
sLibInitDone = true; sLibInitDone = true;
return NOTIFICATION_MODULE_RESULT_SUCCESS; return NOTIFICATION_MODULE_RESULT_SUCCESS;
@ -171,8 +182,7 @@ NotificationModuleStatus NotificationModule_DeInitLibrary() {
sNotificationModuleVersion = NOTIFICATION_MODULE_API_VERSION_ERROR; sNotificationModuleVersion = NOTIFICATION_MODULE_API_VERSION_ERROR;
OSDynLoad_Release(sModuleHandle); OSDynLoad_Release(sModuleHandle);
sModuleHandle = nullptr; sModuleHandle = nullptr;
sDefaultValues.clear(); sLibInitDone = false;
sLibInitDone = false;
} }
return NOTIFICATION_MODULE_RESULT_SUCCESS; return NOTIFICATION_MODULE_RESULT_SUCCESS;
} }
@ -190,7 +200,7 @@ NotificationModuleStatus NotificationModule_GetVersion(NotificationModuleAPIVers
} }
} }
return reinterpret_cast<decltype(&NotificationModule_GetVersion)>(sNMGetVersion)(outVersion); return sNMGetVersion(outVersion);
} }
NotificationModuleStatus NotificationModule_IsOverlayReady(bool *outIsReady) { NotificationModuleStatus NotificationModule_IsOverlayReady(bool *outIsReady) {
@ -205,7 +215,7 @@ NotificationModuleStatus NotificationModule_IsOverlayReady(bool *outIsReady) {
return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT; return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT;
} }
return reinterpret_cast<decltype(&NotificationModule_IsOverlayReady)>(sNMIsOverlayReady)(outIsReady); return sNMIsOverlayReady(outIsReady);
} }
NotificationModuleStatus NotificationModule_AddDynamicNotificationEx(const char *text, NotificationModuleStatus NotificationModule_AddDynamicNotificationEx(const char *text,
@ -230,24 +240,25 @@ NotificationModuleStatus NotificationModule_AddDynamicNotificationEx(const char
if (sNMAddDynamicNotificationV2 == nullptr) { if (sNMAddDynamicNotificationV2 == nullptr) {
return NOTIFICATION_MODULE_RESULT_UNSUPPORTED_COMMAND; return NOTIFICATION_MODULE_RESULT_UNSUPPORTED_COMMAND;
} }
return reinterpret_cast<decltype(sNMAddDynamicNotificationV2)>(sNMAddDynamicNotificationV2)(text, return sNMAddDynamicNotificationV2(text,
textColor, textColor,
backgroundColor, backgroundColor,
finishFunc, finishFunc,
context, context,
keepUntilShown, keepUntilShown,
outHandle); outHandle);
} }
return reinterpret_cast<decltype(sNMAddDynamicNotification)>(sNMAddDynamicNotification)(text, return sNMAddDynamicNotification(text,
textColor, textColor,
backgroundColor, backgroundColor,
finishFunc, finishFunc,
context, context,
outHandle); outHandle);
} }
NotificationModuleStatus NotificationModule_AddDynamicNotification(const char *text, NotificationModuleHandle *outHandle) { NotificationModuleStatus NotificationModule_AddDynamicNotification(const char *text, NotificationModuleHandle *outHandle) {
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC < MAX_NOTIFICATION_TYPES);
auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC]; auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC];
return NotificationModule_AddDynamicNotificationEx(text, return NotificationModule_AddDynamicNotificationEx(text,
outHandle, outHandle,
@ -262,6 +273,7 @@ NotificationModuleStatus NotificationModule_AddDynamicNotificationWithCallback(c
NotificationModuleHandle *outHandle, NotificationModuleHandle *outHandle,
NotificationModuleNotificationFinishedCallback callback, NotificationModuleNotificationFinishedCallback callback,
void *callbackContext) { void *callbackContext) {
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC < MAX_NOTIFICATION_TYPES);
auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC]; auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC];
return NotificationModule_AddDynamicNotificationEx(text, return NotificationModule_AddDynamicNotificationEx(text,
outHandle, outHandle,
@ -296,24 +308,24 @@ static NotificationModuleStatus NotificationModule_AddStaticNotification(const c
if (sNMAddStaticNotificationV2 == nullptr) { if (sNMAddStaticNotificationV2 == nullptr) {
return NOTIFICATION_MODULE_RESULT_UNSUPPORTED_COMMAND; return NOTIFICATION_MODULE_RESULT_UNSUPPORTED_COMMAND;
} }
return reinterpret_cast<decltype(sNMAddStaticNotificationV2)>(sNMAddStaticNotificationV2)(text, return sNMAddStaticNotificationV2(text,
type, type,
durationBeforeFadeOutInSeconds, durationBeforeFadeOutInSeconds,
shakeDurationInSeconds, shakeDurationInSeconds,
textColor, textColor,
backgroundColor, backgroundColor,
callback, callback,
callbackContext, callbackContext,
keepUntilShown); keepUntilShown);
} }
return reinterpret_cast<decltype(sNMAddStaticNotification)>(sNMAddStaticNotification)(text, return sNMAddStaticNotification(text,
type, type,
durationBeforeFadeOutInSeconds, durationBeforeFadeOutInSeconds,
shakeDurationInSeconds, shakeDurationInSeconds,
textColor, textColor,
backgroundColor, backgroundColor,
callback, callback,
callbackContext); callbackContext);
} }
#undef NotificationModule_SetDefaultValue #undef NotificationModule_SetDefaultValue
@ -323,12 +335,13 @@ NotificationModuleStatus NotificationModule_SetDefaultValue(NotificationModuleNo
if (sModuleHandle == nullptr) { if (sModuleHandle == nullptr) {
return NOTIFICATION_MODULE_RESULT_LIB_UNINITIALIZED; return NOTIFICATION_MODULE_RESULT_LIB_UNINITIALIZED;
} }
va_list va;
if (!sDefaultValues.contains(type) && type == NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR) { if (type < 0 || type >= MAX_NOTIFICATION_TYPES) {
sDefaultValues[type].backgroundColor = {237, 28, 36, 255}; return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT;
} }
va_list va;
auto &cur = sDefaultValues[type]; auto &cur = sDefaultValues[type];
va_start(va, valueType); va_start(va, valueType);
auto res = NOTIFICATION_MODULE_RESULT_SUCCESS; auto res = NOTIFICATION_MODULE_RESULT_SUCCESS;
@ -392,6 +405,7 @@ NotificationModuleStatus NotificationModule_AddInfoNotificationEx(const char *te
} }
NotificationModuleStatus NotificationModule_AddInfoNotification(const char *text) { NotificationModuleStatus NotificationModule_AddInfoNotification(const char *text) {
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO < MAX_NOTIFICATION_TYPES);
auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO]; auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO];
return NotificationModule_AddInfoNotificationEx(text, return NotificationModule_AddInfoNotificationEx(text,
cur.durationBeforeFadeOutInSeconds, cur.durationBeforeFadeOutInSeconds,
@ -404,6 +418,7 @@ NotificationModuleStatus NotificationModule_AddInfoNotification(const char *text
NotificationModuleStatus NotificationModule_AddInfoNotificationWithCallback(const char *text, NotificationModuleStatus NotificationModule_AddInfoNotificationWithCallback(const char *text,
NotificationModuleNotificationFinishedCallback callback, NotificationModuleNotificationFinishedCallback callback,
void *callbackContext) { void *callbackContext) {
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO < MAX_NOTIFICATION_TYPES);
auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO]; auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO];
return NotificationModule_AddInfoNotificationEx(text, return NotificationModule_AddInfoNotificationEx(text,
cur.durationBeforeFadeOutInSeconds, cur.durationBeforeFadeOutInSeconds,
@ -438,6 +453,7 @@ NotificationModuleStatus NotificationModule_AddErrorNotification(const char *tex
return NOTIFICATION_MODULE_RESULT_LIB_UNINITIALIZED; return NOTIFICATION_MODULE_RESULT_LIB_UNINITIALIZED;
} }
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR < MAX_NOTIFICATION_TYPES);
auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR]; auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR];
return NotificationModule_AddErrorNotificationEx(text, return NotificationModule_AddErrorNotificationEx(text,
cur.durationBeforeFadeOutInSeconds, cur.durationBeforeFadeOutInSeconds,
@ -456,6 +472,7 @@ NotificationModuleStatus NotificationModule_AddErrorNotificationWithCallback(con
return NOTIFICATION_MODULE_RESULT_LIB_UNINITIALIZED; return NOTIFICATION_MODULE_RESULT_LIB_UNINITIALIZED;
} }
static_assert(NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR < MAX_NOTIFICATION_TYPES);
auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR]; auto &cur = sDefaultValues[NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR];
return NotificationModule_AddErrorNotificationEx(text, return NotificationModule_AddErrorNotificationEx(text,
cur.durationBeforeFadeOutInSeconds, cur.durationBeforeFadeOutInSeconds,
@ -480,8 +497,8 @@ NotificationModuleStatus NotificationModule_UpdateDynamicNotificationText(Notifi
return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT; return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT;
} }
return reinterpret_cast<decltype(&NotificationModule_UpdateDynamicNotificationText)>(sNMUpdateDynamicNotificationText)(handle, return sNMUpdateDynamicNotificationText(handle,
text); text);
} }
NotificationModuleStatus NotificationModule_UpdateDynamicNotificationBackgroundColor(NotificationModuleHandle handle, NotificationModuleStatus NotificationModule_UpdateDynamicNotificationBackgroundColor(NotificationModuleHandle handle,
@ -497,8 +514,8 @@ NotificationModuleStatus NotificationModule_UpdateDynamicNotificationBackgroundC
return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT; return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT;
} }
return reinterpret_cast<decltype(&NotificationModule_UpdateDynamicNotificationBackgroundColor)>(sNMUpdateDynamicNotificationBackgroundColor)(handle, return sNMUpdateDynamicNotificationBackgroundColor(handle,
backgroundColor); backgroundColor);
} }
NotificationModuleStatus NotificationModule_UpdateDynamicNotificationTextColor(NotificationModuleHandle handle, NotificationModuleStatus NotificationModule_UpdateDynamicNotificationTextColor(NotificationModuleHandle handle,
@ -514,8 +531,8 @@ NotificationModuleStatus NotificationModule_UpdateDynamicNotificationTextColor(N
return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT; return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT;
} }
return reinterpret_cast<decltype(&NotificationModule_UpdateDynamicNotificationBackgroundColor)>(sNMUpdateDynamicNotificationTextColor)(handle, return sNMUpdateDynamicNotificationTextColor(handle,
textColor); textColor);
} }
static NotificationModuleStatus NotificationModule_FinishDynamicNotificationEx(NotificationModuleHandle handle, static NotificationModuleStatus NotificationModule_FinishDynamicNotificationEx(NotificationModuleHandle handle,
@ -533,10 +550,10 @@ static NotificationModuleStatus NotificationModule_FinishDynamicNotificationEx(N
return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT; return NOTIFICATION_MODULE_RESULT_INVALID_ARGUMENT;
} }
return reinterpret_cast<decltype(&NotificationModule_FinishDynamicNotificationEx)>(sNMFinishDynamicNotification)(handle, return sNMFinishDynamicNotification(handle,
finishMode, finishMode,
durationBeforeFadeOutInSeconds, durationBeforeFadeOutInSeconds,
shakeDurationInSeconds); shakeDurationInSeconds);
} }
NotificationModuleStatus NotificationModule_FinishDynamicNotification(NotificationModuleHandle handle, NotificationModuleStatus NotificationModule_FinishDynamicNotification(NotificationModuleHandle handle,