diff --git a/nx/include/switch/applets/libapplet.h b/nx/include/switch/applets/libapplet.h index 47844f88..36a488f4 100644 --- a/nx/include/switch/applets/libapplet.h +++ b/nx/include/switch/applets/libapplet.h @@ -41,6 +41,14 @@ void libappletArgsSetPlayStartupSound(LibAppletArgs* a, bool flag); */ Result libappletArgsPush(LibAppletArgs* a, AppletHolder *h); +/** + * @brief Creates a storage using the input buffer which is pushed to the AppletHolder via \ref appletHolderPushInData. + * @param h AppletHolder object. + * @param buffer Input data buffer. + * @param size Input data size. + */ +Result libappletPushInData(AppletHolder *h, const void* buffer, size_t size); + /// Wrapper for \ref appletPushToGeneralChannel, see appletPushToGeneralChannel regarding the requirements for using this. /// Returns to the main Home Menu, equivalent to pressing the HOME button. Result libappletRequestHomeMenu(void); diff --git a/nx/source/applets/libapplet.c b/nx/source/applets/libapplet.c index ae8115e5..a386a288 100644 --- a/nx/source/applets/libapplet.c +++ b/nx/source/applets/libapplet.c @@ -19,43 +19,54 @@ void libappletArgsSetPlayStartupSound(LibAppletArgs* a, bool flag) { a->PlayStartupSound = flag!=0; } -Result libappletArgsPush(LibAppletArgs* a, AppletHolder *h) { +static Result _libappletCreateWriteStorage(AppletStorage* s, const void* buffer, size_t size) { + Result rc=0; + + rc = appletCreateStorage(s, size); + if (R_FAILED(rc)) return rc; + + rc = appletStorageWrite(s, 0, buffer, size); + if (R_FAILED(rc)) { + appletStorageClose(s); + return rc; + } + + return rc; +} + +static Result _libappletPushInData(AppletHolder *h, const void* buffer, size_t size) { Result rc=0; AppletStorage storage; + rc = _libappletCreateWriteStorage(&storage, buffer, size); + if (R_FAILED(rc)) return rc; + + return appletHolderPushInData(h, &storage); +} + +Result libappletArgsPush(LibAppletArgs* a, AppletHolder *h) { //Official sw stores the header in LibAppletArgs seperately (first 8-bytes), but here we're including it with the LibAppletCommonArguments struct. //Official sw uses appletStorageWrite twice, for writing the header then the rest of the struct. a->tick = armGetSystemTick(); - rc = appletCreateStorage(&storage, sizeof(LibAppletArgs)); - if (R_FAILED(rc)) return rc; - - rc = appletStorageWrite(&storage, 0, a, sizeof(LibAppletArgs)); - if (R_FAILED(rc)) { - appletStorageClose(&storage); - return rc; - } - - return appletHolderPushInData(h, &storage); + return _libappletPushInData(h, a, sizeof(LibAppletArgs)); } static Result _libappletQlaunchRequest(u8* buf, size_t size) { Result rc=0; AppletStorage storage; - rc = appletCreateStorage(&storage, size); + rc = _libappletCreateWriteStorage(&storage, buf, size); if (R_FAILED(rc)) return rc; - rc = appletStorageWrite(&storage, 0, buf, size); - if (R_FAILED(rc)) { - appletStorageClose(&storage); - return rc; - } - return appletPushToGeneralChannel(&storage); } +Result libappletPushInData(AppletHolder *h, const void* buffer, size_t size) { + return _libappletPushInData(h, buffer, size); +} + Result libappletRequestHomeMenu(void) { u8 storagedata[0x10] = {0x53, 0x41, 0x4d, 0x53, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00};//RequestHomeMenu return _libappletQlaunchRequest(storagedata, sizeof(storagedata));