Implement error applet

This commit is contained in:
Liam Minopulos 2019-02-26 22:57:31 +01:00
parent 904deeec10
commit 320f17a94f
2 changed files with 150 additions and 0 deletions

View File

@ -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);

View File

@ -0,0 +1,76 @@
#include <malloc.h>
#include <string.h>
#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);
}