diff --git a/nx/include/switch/applets/libapplet.h b/nx/include/switch/applets/libapplet.h index 36a488f4..e3342f72 100644 --- a/nx/include/switch/applets/libapplet.h +++ b/nx/include/switch/applets/libapplet.h @@ -34,6 +34,15 @@ void libappletArgsCreate(LibAppletArgs* a, u32 version); */ void libappletArgsSetPlayStartupSound(LibAppletArgs* a, bool flag); +/** + * @brief Reads data from offset 0 from the specified storage into the buffer. If the storage-size is smaller than the size param, the storage-size is used instead. + * @param s Storage object. + * @param buffer Output buffer. + * @param size Size to read. + * @param transfer_size Optional output size field for the actual size used for the read. + */ +Result libappletReadStorage(AppletStorage* s, void* buffer, size_t size, size_t *transfer_size); + /** * @brief Sets the tick field in LibAppletArgs, then creates a storage with it which is pushed to the AppletHolder via \ref appletHolderPushInData. * @param a LibAppletArgs struct. @@ -49,6 +58,15 @@ Result libappletArgsPush(LibAppletArgs* a, AppletHolder *h); */ Result libappletPushInData(AppletHolder *h, const void* buffer, size_t size); +/** + * @brief Pops a storage via \ref appletHolderPopOutData, uses \ref libappletReadStorage, then closes the storage. + * @param h AppletHolder object. + * @param buffer Output buffer. + * @param size Size to read. + * @param transfer_size Optional output size field for the actual size used for the read. + */ +Result libappletPopOutData(AppletHolder *h, void* buffer, size_t size, size_t *transfer_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 a386a288..5daf914d 100644 --- a/nx/source/applets/libapplet.c +++ b/nx/source/applets/libapplet.c @@ -34,6 +34,22 @@ static Result _libappletCreateWriteStorage(AppletStorage* s, const void* buffer, return rc; } +Result libappletReadStorage(AppletStorage* s, void* buffer, size_t size, size_t *transfer_size) { + Result rc=0; + s64 tmpsize=0; + + rc = appletStorageGetSize(s, &tmpsize); + + if (R_SUCCEEDED(rc)) { + if (tmpsize < size) size = tmpsize; + rc = appletStorageRead(s, 0, buffer, size); + } + + if (R_SUCCEEDED(rc) && transfer_size) *transfer_size = size; + + return rc; +} + static Result _libappletPushInData(AppletHolder *h, const void* buffer, size_t size) { Result rc=0; AppletStorage storage; @@ -67,6 +83,18 @@ Result libappletPushInData(AppletHolder *h, const void* buffer, size_t size) { return _libappletPushInData(h, buffer, size); } +Result libappletPopOutData(AppletHolder *h, void* buffer, size_t size, size_t *transfer_size) { + Result rc=0; + AppletStorage storage; + + rc = appletHolderPopOutData(h, &storage); + if (R_FAILED(rc)) return rc; + + rc = libappletReadStorage(&storage, buffer, size, transfer_size); + appletStorageClose(&storage); + return rc; +} + 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));