Fix formating and add further functionality

This commit is contained in:
Oreo639 2019-04-10 20:08:29 -07:00
parent 134e022568
commit d8cc4a1dc8
2 changed files with 102 additions and 15 deletions

View File

@ -48,9 +48,6 @@ typedef struct
///< applet. ///< applet.
} MiiSelectorConf; } MiiSelectorConf;
/// Size of the data describing a single Mii
#define MIISELECTOR_MIIDATA_SIZE 0x5c
/// Maximum length of the localized name of a Guest Mii /// Maximum length of the localized name of a Guest Mii
#define MIISELECTOR_GUESTMII_NAME_LEN 12 #define MIISELECTOR_GUESTMII_NAME_LEN 12
@ -73,6 +70,15 @@ typedef struct
///< string). Zeroed otherwise. ///< string). Zeroed otherwise.
} MiiSelectorReturn; } MiiSelectorReturn;
/// AppletEd options
enum
{
MIISELECTOR_CANCEL = BIT(0), ///< Show the cancel button
MIISELECTOR_GUESTS = BIT(1), ///< Make Guets Miis selectable
MIISELECTOR_TOP = BIT(2), ///< Show AppletEd on top screen
MIISELECTOR_GUESTSTART = BIT(3), ///< Start on guest page
};
/** /**
* @brief Launch the Mii selector library applet * @brief Launch the Mii selector library applet
* *
@ -84,17 +90,49 @@ Result miiSelectorLaunch(const MiiSelectorConf *conf, MiiSelectorReturn* returnb
/** /**
* @brief Sets title of the Mii selector library applet * @brief Sets title of the Mii selector library applet
* *
* @param conf Pointer to the current Mii selector configuration * @param conf Pointer to miiSelector configuration
* @param text Title text * @param text Title text of Mii selector
*/ */
void miiSelectorSetTitle(MiiSelectorConf *conf, const char* text); void miiSelectorSetTitle(MiiSelectorConf *conf, const char* text);
/**
* @brief Specifies which special options are enabled in the Mii selector
*
* @param conf Pointer to miiSelector configuration
* @param options Options bitmask
*/
void miiSelectorSetOptions(MiiSelectorConf *conf, char options);
/**
* @brief Specifies which mii the cursor should start from
*
* @param conf Pointer to miiSelector configuration
* @param index Indexed number of the Mii that the cursor will start on.
* If there is no mii with that index, the the cursor will be at index 0.
*/
void miiSelectorSetIndex(MiiSelectorConf *conf, u32 index);
/**
* @brief Get Mii name
*
* @param returnbuf Pointer to miiSelector return
* @param out String containing a Mii's name (a length of at least 36 is expected)
*/
void miiSelectorReturnGetName(const MiiSelectorReturn *returnbuf, char* out);
/**
* @brief Get Mii Author
*
* @param returnbuf Pointer to miiSelector return
* @param out String containing a Mii's author (a length of at least 30 is expected)
*/
void miiSelectorReturnGetAuthor(const MiiSelectorReturn *returnbuf, char* out);
/** /**
* @brief Verifies that the Mii data returned from the applet matches its * @brief Verifies that the Mii data returned from the applet matches its
* checksum * checksum
* *
* @param returnbuffer Buffer filled by Mii selector applet * @param returnbuffer Buffer filled by Mii selector applet
*
* @return `true` if `returnbuf->checksum` is the same as the one computed from `returnbuf` * @return `true` if `returnbuf->checksum` is the same as the one computed from `returnbuf`
*/ */
bool miiSelectorChecksumIsValid(const MiiSelectorReturn *returnbuf); bool miiSelectorChecksumIsValid(const MiiSelectorReturn *returnbuf);

View File

@ -18,13 +18,30 @@ Result miiSelectorLaunch(const MiiSelectorConf *conf, MiiSelectorReturn *returnb
ctx.config.magic = MIISELECTOR_MAGIC; ctx.config.magic = MIISELECTOR_MAGIC;
Result ret = aptLaunchLibraryApplet(APPID_APPLETED, &ctx.config, sizeof(MiiSelectorConf), 0); Result ret = aptLaunchLibraryApplet(APPID_APPLETED, &ctx.config, sizeof(MiiSelectorConf), 0);
if(R_SUCCEEDED(ret) && returnbuf) { if(R_SUCCEEDED(ret) && returnbuf)
memcpy(returnbuf, &ctx.ret, sizeof(MiiSelectorReturn)); memcpy(returnbuf, &ctx.ret, sizeof(MiiSelectorReturn));
}
return ret; return ret;
} }
static void miiSelectorConvertToUTF8(char* out, const u16* in, int max)
{
if (!in || !*in)
{
out[0] = 0;
return;
}
ssize_t units = utf16_to_utf8((uint8_t*)out, in, max);
if (units < 0)
{
out[0] = 0;
return;
}
out[units] = 0;
}
static void miiSelectorConvertToUTF16(u16* out, const char* in, int max) static void miiSelectorConvertToUTF16(u16* out, const char* in, int max)
{ {
if (!in || !*in) if (!in || !*in)
@ -48,26 +65,58 @@ void miiSelectorSetTitle(MiiSelectorConf *conf, const char* text)
miiSelectorConvertToUTF16(conf->title, text, MIISELECTOR_TITLE_LEN); miiSelectorConvertToUTF16(conf->title, text, MIISELECTOR_TITLE_LEN);
} }
static const char miiSelectorOptions[] =
{
offsetof(MiiSelectorConf, enable_cancel_button),
offsetof(MiiSelectorConf, enable_selecting_guests),
offsetof(MiiSelectorConf, show_on_top_screen),
offsetof(MiiSelectorConf, show_guest_page),
};
void miiSelectorSetOptions(MiiSelectorConf *conf, char options)
{
for (int i = 0; i < (sizeof(miiSelectorOptions)/sizeof(char)); i ++)
*((u8*)conf + miiSelectorOptions[i]) = (options & BIT(i)) ? 1 : 0;
}
void miiSelectorSetIndex(MiiSelectorConf *conf, u32 index) {
conf->initial_index = index;
}
void miiSelectorReturnGetName(const MiiSelectorReturn *returnbuf, char* out)
{
if (!out)
return;
if (returnbuf->guest_mii_was_selected)
miiSelectorConvertToUTF8(out, returnbuf->guest_mii_name, 36);
else
miiSelectorConvertToUTF8(out, returnbuf->mii.mii_name, 36);
}
void miiSelectorReturnGetAuthor(const MiiSelectorReturn *returnbuf, char* out)
{
miiSelectorConvertToUTF8(out, returnbuf->mii.author_name, 30);
}
static u16 crc16_ccitt(void const *buf, size_t len, uint32_t starting_val) static u16 crc16_ccitt(void const *buf, size_t len, uint32_t starting_val)
{ {
if(buf == NULL) { if(buf == NULL)
return -1; return -1;
}
u8 const *cbuf = buf; u8 const *cbuf = buf;
u32 crc = starting_val; u32 crc = starting_val;
static const u16 POLY = 0x1021; static const u16 POLY = 0x1021;
for(size_t i = 0; i < len; i++) { for(size_t i = 0; i < len; i++)
for(int bit = 7; bit >= 0; bit--) { {
for(int bit = 7; bit >= 0; bit--)
crc = ((crc << 1) | ((cbuf[i] >> bit) & 0x1)) ^ (crc & 0x8000 ? POLY : 0); crc = ((crc << 1) | ((cbuf[i] >> bit) & 0x1)) ^ (crc & 0x8000 ? POLY : 0);
}
} }
for(int _ = 0; _ < 16; _++) { for(int _ = 0; _ < 16; _++)
crc = (crc << 1) ^ (crc & 0x8000 ? POLY : 0); crc = (crc << 1) ^ (crc & 0x8000 ? POLY : 0);
}
return (u16)(crc & 0xffff); return (u16)(crc & 0xffff);
} }