diff --git a/nx/include/switch.h b/nx/include/switch.h index 83cba60b..538684f3 100644 --- a/nx/include/switch.h +++ b/nx/include/switch.h @@ -106,6 +106,7 @@ extern "C" { #include "switch/audio/driver.h" #include "switch/applets/libapplet.h" +#include "switch/applets/error.h" #include "switch/applets/swkbd.h" #include "switch/applets/web.h" diff --git a/nx/include/switch/applets/error.h b/nx/include/switch/applets/error.h new file mode 100644 index 00000000..8d7744f2 --- /dev/null +++ b/nx/include/switch/applets/error.h @@ -0,0 +1,77 @@ +/** + * @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 { + u8 custom_text; ///< Whether to show a custom error message. If this is false, a default message will be shown. + u8 unk[7]; + u32 module; ///< Module code. + u32 description; ///< Description code. + u8 unk2[8]; + char short_description[0x800]; ///< Short description. + char detailed_description[0x800]; ///< Detailed description (displayed 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 = "". + * @warning This applet creates an error report that is logged in the system. Proceed at your own risk! + */ +void errorCreate(ErrorConfig* c); + +/** + * @brief Closes an ErrorConfig struct. + * @param c ErrorConfig struct. + */ +void errorClose(ErrorConfig* c); + +/** + * @brief Launches with the specified config. + * @param c ErrorConfig struct. + */ +void errorShow(ErrorConfig* c); + +/** + * @brief Sets the error module. + * @param c ErrorConfig struct. + * @param code Code. + */ +void errorConfigSetModule(ErrorConfig* c, u32 code); + +/** + * @brief Sets the error description. + * @param c ErrorConfig struct. + * @param code Code. + */ +void errorConfigSetDescription(ErrorConfig* c, u32 code); + +/** + * @brief Sets whether to use a custom error message. + * @param c ErrorConfig struct. + * @param custom_text Whether to use a custom message. + */ +void errorConfigSetCustomText(ErrorConfig* c, bool custom_text); + +/** + * @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.c b/nx/source/applets/error.c new file mode 100644 index 00000000..fe2fa634 --- /dev/null +++ b/nx/source/applets/error.c @@ -0,0 +1,51 @@ +#include +#include +#include "types.h" +#include "result.h" +#include "services/applet.h" +#include "applets/libapplet.h" +#include "applets/error.h" + +void errorCreate(ErrorConfig* c) { + memset(c, 0, sizeof(ErrorConfig)); + c->custom_text = false; + c->module = 2000; +} + +void errorClose(ErrorConfig* c) { + memset(c, 0, sizeof(ErrorConfig)); +} + +void errorShow(ErrorConfig* c) { + AppletHolder err; + LibAppletArgs errArgs; + + appletCreateLibraryApplet(&err, AppletId_error, LibAppletMode_AllForeground); + libappletArgsCreate(&errArgs, 1); + libappletArgsPush(&errArgs, &err); + libappletPushInData(&err, c, sizeof(ErrorConfig)); + + appletHolderStart(&err); + appletHolderJoin(&err); + appletHolderClose(&err); +} + +void errorConfigSetModule(ErrorConfig* c, u32 code) { + c->module = code; +} + +void errorConfigSetDescription(ErrorConfig* c, u32 code) { + c->description = code; +} + +void errorConfigSetCustomText(ErrorConfig* c, bool custom_text) { + c->custom_text = custom_text; +} + +void errorConfigSetShortDescription(ErrorConfig* c, const char* str) { + strncpy(c->short_description, str, sizeof(c->short_description) - 1); +} + +void errorConfigSetDetailedDescription(ErrorConfig* c, const char* str) { + strncpy(c->detailed_description, str, sizeof(c->detailed_description) - 1); +}