From 320f17a94fd916ef7d07fb344d2acf09ee534c5d Mon Sep 17 00:00:00 2001 From: Liam Minopulos Date: Tue, 26 Feb 2019 22:57:31 +0100 Subject: [PATCH] Implement error applet --- nx/include/switch/applets/error.h | 74 ++++++++++++++++++++++++++++++ nx/source/applets/error.cpp | 76 +++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 nx/include/switch/applets/error.h create mode 100644 nx/source/applets/error.cpp diff --git a/nx/include/switch/applets/error.h b/nx/include/switch/applets/error.h new file mode 100644 index 00000000..bd169919 --- /dev/null +++ b/nx/include/switch/applets/error.h @@ -0,0 +1,74 @@ +/** + * @file error.h + * @brief Wrapper for using the error LibraryApplet. + * @author StuntHacks + * @copyright libnx Authors + */ +#pragma once +#include "../types.h" +#include "../services/applet.h" + +/// Error configuration struct. +typedef struct { + u32 major_code; ///< First part of the error code. + u32 minor_code; ///< Second part of the error code. + bool custom_text; ///< Whether to display a custom error message. + char* short_description; ///< Short description of the error. + char* detailed_description; ///< More detailed description of the error. Shown when the user clicks on "Details". +} ErrorConfig; + +/** + * @brief Creates an ErrorConfg struct. + * @param c ErrorConfg struct. + * @note Sets the following fields: majorCode = 2000, minorCode = 0, customText = false, shortDescription = "", detailedDescription = "". + */ +void errorCreate(ErrorConfig* c); + +/** + * @brief Closes an ErrorConfg struct. + * @param c ErrorConfg struct. + */ +void errorClose(ErrorConfig* c); + +/** + * @brief Launches with the specified config. + * @param c ErrorConfig struct + */ +void errorShow(ErrorConfig* c); + +/** + * @brief Sets the first part of the error code. + * @param c ErrorConfig struct + * @param code Code. + */ +void errorConfigSetMajorCode(ErrorConfig* c, u32 code); + +/** + * @brief Sets the second part of the error code. + * @param c ErrorConfig struct + * @param code Code. + */ +void errorConfigSetMinorCode(ErrorConfig* c, u32 code); + +/** + * @brief Sets whether to use a custom error message. + * @param c ErrorConfig struct + * @param customText Whether to use a custom message. + */ +void errorConfigSetCustomText(ErrorConfig* c, bool customText); + +/** + * @brief Sets the short description. + * @param c ErrorConfig struct + * @param str Description. + */ +void errorConfigSetShortDescription(ErrorConfig* c, const char* str); + +/** + * @brief Sets the detailed description. + * @param c ErrorConfig struct + * @param str Description. + * + * This gets displayed when the user clicks on "Details". + */ +void errorConfigSetDetailedDescription(ErrorConfig* c, const char* str); diff --git a/nx/source/applets/error.cpp b/nx/source/applets/error.cpp new file mode 100644 index 00000000..1bf30f2d --- /dev/null +++ b/nx/source/applets/error.cpp @@ -0,0 +1,76 @@ +#include +#include +#include "types.h" +#include "result.h" +#include "services/applet.h" +#include "applets/libapplet.h" +#include "applets/error.h" + +void errorCreate(ErrorConfig* c) { + c->major_code = 2000; + c->minor_code = 0; + c->custom_text = false; + + c->detailed_description = (char*) malloc(sizeof(char) * (strlen("") + 1)); + strcpy(c->detailed_description, ""); + + c->short_description = (char*) malloc(sizeof(char) * (strlen("") + 1)); + strcpy(c->short_description, ""); +} + +void errorClose(ErrorConfig* c) { + free(c->short_description); + free(c->detailed_description); + memset(c, 0, sizeof(ErrorConfig)); +} + +void errorShow(ErrorConfig* c) { + AppletHolder err; + AppletStorage errStor; + LibAppletArgs errArgs; + + appletCreateLibraryApplet(&err, AppletId_error, LibAppletMode_AllForeground); + libappletArgsCreate(&errArgs, 1); + libappletArgsPush(&errArgs, &err); + + appletCreateStorage(&errStor, 4120); + + u8 args[0x1018] = {0}; + args[0] = (c->custom_text ? 1 : 0); + + *(u64*) &args[8] = c->major_code; + *(u64*) &args[0xC] = c->minor_code; + strcpy((char*) &args[0x18], c->short_description); + strcpy((char*) &args[0x818], c->detailed_description); + appletStorageWrite(&errStor, 0, args, 0x1018); + + appletHolderPushInData(&err, &errStor); + + appletHolderStart(&err); + appletHolderJoin(&err); + appletHolderClose(&err); +} + +void errorConfigSetMajorCode(ErrorConfig* c, u32 code) { + c->major_code = code; +} + +void errorConfigSetMinorCode(ErrorConfig* c, u32 code) { + c->minor_code = code; +} + +void errorConfigSetCustomText(ErrorConfig* c, bool customText) { + c->custom_text = customText; +} + +void errorConfigSetShortDescription(ErrorConfig* c, const char* str) { + free(c->short_description); + c->short_description = (char*) malloc(sizeof(char) * (strlen(str) + 1)); + strcpy(c->short_description, str); +} + +void errorConfigSetDetailedDescription(ErrorConfig* c, const char* str) { + free(c->detailed_description); + c->detailed_description = (char*) malloc(sizeof(char) * (strlen(str) + 1)); + strcpy(c->detailed_description, str); +}