Simplify NotificationUtils

This commit is contained in:
Maschell 2024-03-01 11:53:16 +01:00
parent 950a55cd8f
commit a66bd1f275
3 changed files with 9 additions and 101 deletions

View File

@ -1,61 +1,8 @@
#include "NotificationsUtils.h"
#include "globals.h"
#include "utils/logger.h"
#include <coreinit/cache.h>
#include <coreinit/messagequeue.h>
#include <coreinit/thread.h>
#include <cstring>
#include <notifications/notifications.h>
#include <thread>
std::unique_ptr<std::thread> sNotificationsThread;
static bool sShutdownNotificationsThread = false;
OSMessageQueue sNotificationQueue;
OSMessage sNotificationMessages[0x10];
#define NOTIFICATION_QUEUE_COMMAND_STOP 0
#define NOTIFICATION_QUEUE_COMMAND_ERROR 1
struct NotificationMessageWrapper {
NotificationModuleNotificationType type = NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO;
char text[512] = {};
float duration = 10.0f;
};
void NotificationMainLoop() {
bool isOverlayReady = false;
while (!sShutdownNotificationsThread &&
NotificationModule_IsOverlayReady(&isOverlayReady) == NOTIFICATION_MODULE_RESULT_SUCCESS && !isOverlayReady) {
OSSleepTicks(OSMillisecondsToTicks(16));
}
if (sShutdownNotificationsThread || !isOverlayReady) {
return;
}
OSMessage recv;
while (OSReceiveMessage(&sNotificationQueue, &recv, OS_MESSAGE_FLAGS_BLOCKING)) {
if (recv.args[0] == NOTIFICATION_QUEUE_COMMAND_STOP) {
break;
}
if (recv.args[0] == NOTIFICATION_QUEUE_COMMAND_ERROR) {
auto *param = (NotificationMessageWrapper *) recv.message;
if (param->type == NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO) {
NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO, NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT, param->duration);
NotificationModule_AddInfoNotification(param->text);
} else if (param->type == NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR) {
NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR, NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT, param->duration);
NotificationModule_AddErrorNotification(param->text);
} else {
DEBUG_FUNCTION_LINE_WARN("Unsupported notification type: %d", param->type);
}
free(param);
continue;
}
}
}
bool DisplayNotificationMessage(std::string_view text, NotificationModuleNotificationType type, float duration) {
if (!gNotificationModuleLoaded) {
@ -64,23 +11,17 @@ bool DisplayNotificationMessage(std::string_view text, NotificationModuleNotific
if (type == NOTIFICATION_MODULE_NOTIFICATION_TYPE_DYNAMIC) {
return false;
}
auto *param = (NotificationMessageWrapper *) malloc(sizeof(NotificationMessageWrapper));
if (!param) {
return false;
}
strncpy(param->text, text.data(), sizeof(param->text) - 1);
param->type = type;
param->duration = duration;
OSMessage send;
send.message = param;
send.args[0] = NOTIFICATION_QUEUE_COMMAND_ERROR;
auto res = OSSendMessage(&sNotificationQueue, &send, OS_MESSAGE_FLAGS_NONE);
if (!res) {
DEBUG_FUNCTION_LINE_ERR("Failed to add Error Notification: Queue full");
free(param);
return false;
if (type == NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO) {
NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO, NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT, duration);
NotificationModule_AddInfoNotification(text.data());
} else if (type == NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR) {
NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR, NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT, duration);
NotificationModule_AddErrorNotification(text.data());
} else {
DEBUG_FUNCTION_LINE_WARN("Unsupported notification type: %d", type);
}
return true;
}
@ -91,27 +32,3 @@ bool DisplayInfoNotificationMessage(std::string_view text, float duration) {
bool DisplayErrorNotificationMessage(std::string_view text, float duration) {
return DisplayNotificationMessage(text, NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR, duration);
}
void StartNotificationThread() {
sNotificationsThread.reset();
sShutdownNotificationsThread = false;
if (!gNotificationModuleLoaded) {
return;
}
constexpr int32_t messageSize = sizeof(sNotificationMessages) / sizeof(sNotificationMessages[0]);
OSInitMessageQueue(&sNotificationQueue, sNotificationMessages, messageSize);
sNotificationsThread = make_unique_nothrow<std::thread>(NotificationMainLoop);
}
void StopNotificationThread() {
if (sNotificationsThread != nullptr) {
OSMessage message;
message.args[0] = NOTIFICATION_QUEUE_COMMAND_STOP;
OSSendMessage(&sNotificationQueue, &message, OS_MESSAGE_FLAGS_NONE);
sShutdownNotificationsThread = true;
OSMemoryBarrier();
sNotificationsThread->join();
sNotificationsThread.reset();
}
}

View File

@ -2,10 +2,6 @@
#include <string>
void StartNotificationThread();
void StopNotificationThread();
bool DisplayInfoNotificationMessage(std::string_view text, float duration);
bool DisplayErrorNotificationMessage(std::string_view text, float duration);

View File

@ -1,4 +1,3 @@
#include "NotificationsUtils.h"
#include "PluginManagement.h"
#include "coreinit/interrupts.h"
#include "coreinit/scheduler.h"
@ -65,8 +64,6 @@ WUMS_APPLICATION_ENDS() {
}
gUsedRPLs.clear();
StopNotificationThread();
deinitLogging();
}
@ -80,8 +77,6 @@ WUMS_APPLICATION_STARTS() {
OSReport("Running WiiUPluginLoaderBackend " VERSION_FULL "\n");
StartNotificationThread();
gUsedRPLs.clear();
// If an allocated rpl was not released properly (e.g. if something else calls OSDynload_Acquire without releasing it) memory get leaked.