From d25144afbd6c7be049706448e856d921cb1926f3 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Fri, 11 Oct 2019 01:47:23 -0400 Subject: [PATCH] pl: Updated for new-ipc. Updated param types for plGetSharedFontByType and plGetSharedFont. Improved docs and minor other changes. --- nx/include/switch/services/pl.h | 27 ++- nx/source/services/pl.c | 309 +++++++------------------------- 2 files changed, 84 insertions(+), 252 deletions(-) diff --git a/nx/include/switch/services/pl.h b/nx/include/switch/services/pl.h index a7c6ac32..ed5acd7d 100644 --- a/nx/include/switch/services/pl.h +++ b/nx/include/switch/services/pl.h @@ -6,9 +6,10 @@ */ #pragma once #include "../types.h" +#include "../sf/service.h" -typedef enum -{ +/// SharedFontType +typedef enum { PlSharedFontType_Standard = 0, ///< Japan, US and Europe PlSharedFontType_ChineseSimplified = 1, ///< Chinese Simplified PlSharedFontType_ExtChineseSimplified = 2, ///< Extended Chinese Simplified @@ -18,21 +19,29 @@ typedef enum PlSharedFontType_Total, ///< Total fonts supported by this enum. } PlSharedFontType; +/// FontData typedef struct { - u32 type; - u32 offset; - u32 size; - void* address; + u32 type; ///< \ref PlSharedFontType + u32 offset; ///< Offset of the font in sharedmem. + u32 size; ///< Size of the font. + void* address; ///< Address of the actual font. } PlFontData; +/// Initialize pl. Result plInitialize(void); + +/// Exit pl. void plExit(void); + +/// Gets the Service object for the actual pl service session. Service* plGetServiceSession(void); + +/// Gets the address of the SharedMemory. void* plGetSharedmemAddr(void); -///< Gets a specific shared-font via SharedFontType, see \ref PlSharedFontType. -Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType); +///< Gets a specific shared-font via \ref PlSharedFontType. +Result plGetSharedFontByType(PlFontData* font, PlSharedFontType SharedFontType); ///< Gets shared font(s). -Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, size_t* total_fonts); +Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, s32 max_fonts, s32* total_fonts); diff --git a/nx/source/services/pl.c b/nx/source/services/pl.c index b142454f..b8f9187c 100644 --- a/nx/source/services/pl.c +++ b/nx/source/services/pl.c @@ -1,39 +1,28 @@ +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "service_guard.h" #include - -#include "types.h" -#include "result.h" -#include "arm/atomics.h" -#include "kernel/ipc.h" #include "kernel/shmem.h" -#include "services/sm.h" #include "services/pl.h" #define SHAREDMEMFONT_SIZE 0x1100000 static Service g_plSrv; -static u64 g_plRefCnt; static SharedMemory g_plSharedmem; static Result _plGetSharedMemoryNativeHandle(Handle* handle_out); -Result plInitialize(void) -{ +NX_GENERATE_SERVICE_GUARD(pl); + +Result _plInitialize(void) { Result rc=0; Handle sharedmem_handle=0; - atomicIncrement64(&g_plRefCnt); - - if (serviceIsActive(&g_plSrv)) - return 0; - rc = smGetService(&g_plSrv, "pl:u"); - if (R_SUCCEEDED(rc)) - { + if (R_SUCCEEDED(rc)) { rc = _plGetSharedMemoryNativeHandle(&sharedmem_handle); - if (R_SUCCEEDED(rc)) - { + if (R_SUCCEEDED(rc)) { shmemLoadRemote(&g_plSharedmem, sharedmem_handle, 0x1100000, Perm_R); rc = shmemMap(&g_plSharedmem); @@ -45,12 +34,9 @@ Result plInitialize(void) return rc; } -void plExit(void) -{ - if (atomicDecrement64(&g_plRefCnt) == 0) { - serviceClose(&g_plSrv); - shmemClose(&g_plSharedmem); - } +void _plCleanup(void) { + serviceClose(&g_plSrv); + shmemClose(&g_plSharedmem); } Service* plGetServiceSession(void) { @@ -61,180 +47,39 @@ void* plGetSharedmemAddr(void) { return shmemGetAddr(&g_plSharedmem); } +static Result _plCmdGetHandle(Service* srv, Handle* handle_out, u32 cmd_id) { + return serviceDispatch(srv, cmd_id, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = handle_out, + ); +} + +static Result _plCmdInU32NoOut(Service* srv, u32 inval, u32 cmd_id) { + return serviceDispatchIn(srv, cmd_id, inval); +} + +static Result _plCmdInU32OutU32(Service* srv, u32 inval, u32 *out, u32 cmd_id) { + return serviceDispatchInOut(srv, cmd_id, inval, *out); +} + static Result _plRequestLoad(u32 SharedFontType) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 SharedFontType; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->SharedFontType = SharedFontType; - - Result rc = serviceIpcDispatch(&g_plSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - } - - return rc; + return _plCmdInU32NoOut(&g_plSrv, SharedFontType, 0); } static Result _plGetLoadState(u32 SharedFontType, u32* LoadState) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 SharedFontType; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 1; - raw->SharedFontType = SharedFontType; - - Result rc = serviceIpcDispatch(&g_plSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - u32 LoadState; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && LoadState) *LoadState = resp->LoadState; - } - - return rc; + return _plCmdInU32OutU32(&g_plSrv, SharedFontType, LoadState, 1); } static Result _plGetSize(u32 SharedFontType, u32* size) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 SharedFontType; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 2; - raw->SharedFontType = SharedFontType; - - Result rc = serviceIpcDispatch(&g_plSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - u32 size; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && size) *size = resp->size; - } - - return rc; + return _plCmdInU32OutU32(&g_plSrv, SharedFontType, size, 2); } static Result _plGetSharedMemoryAddressOffset(u32 SharedFontType, u32* offset) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 SharedFontType; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 3; - raw->SharedFontType = SharedFontType; - - Result rc = serviceIpcDispatch(&g_plSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - u32 offset; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc) && offset) *offset = resp->offset; - } - - return rc; + return _plCmdInU32OutU32(&g_plSrv, SharedFontType, offset, 3); } static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 4; - - Result rc = serviceIpcDispatch(&g_plSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *handle_out = r.Handles[0]; - } - } - - return rc; + return _plCmdGetHandle(&g_plSrv, handle_out, 4); } static Result _plVerifyFontRange(u32 offset, u32 size) { @@ -264,7 +109,7 @@ static Result _plRequestLoadWait(u32 SharedFontType) { return rc; } -Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType) { +Result plGetSharedFontByType(PlFontData* font, PlSharedFontType SharedFontType) { Result rc=0; u8* sharedmem_addr = (u8*)plGetSharedmemAddr(); @@ -289,18 +134,15 @@ Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType) { return rc; } -Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, size_t* total_fonts) { +Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, s32 max_fonts, s32* total_fonts) { Result rc=0; - u32 types[PlSharedFontType_Total]; - u32 offsets[PlSharedFontType_Total]; - u32 sizes[PlSharedFontType_Total]; - size_t size = sizeof(u32) * PlSharedFontType_Total; - u32 font_count=0, i; + u32 types[PlSharedFontType_Total]={0}; + u32 offsets[PlSharedFontType_Total]={0}; + u32 sizes[PlSharedFontType_Total]={0}; + size_t size = PlSharedFontType_Total*sizeof(u32); + s32 font_count=0, i; u8* sharedmem_addr = (u8*)plGetSharedmemAddr(); - memset(types, 0, sizeof(types)); - memset(offsets, 0, sizeof(offsets)); - memset(sizes, 0, sizeof(sizes)); memset(fonts, 0, sizeof(PlFontData) * max_fonts); if (total_fonts) *total_fonts = 0; @@ -310,60 +152,41 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si if (R_FAILED(rc)) return rc; } - IpcCommand c; - ipcInitialize(&c); - ipcAddRecvBuffer(&c, types, size, 0); - ipcAddRecvBuffer(&c, offsets, size, 0); - ipcAddRecvBuffer(&c, sizes, size, 0); - struct { - u64 magic; - u64 cmd_id; - u64 LanguageCode; - } *raw; + u8 fonts_loaded; + s32 total_fonts; + } out; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + rc = serviceDispatchInOut(&g_plSrv, 5, LanguageCode, out, + .buffer_attrs = { + SfBufferAttr_HipcMapAlias | SfBufferAttr_Out, + SfBufferAttr_HipcMapAlias | SfBufferAttr_Out, + SfBufferAttr_HipcMapAlias | SfBufferAttr_Out, + }, + .buffers = { + { types, size }, + { offsets, size }, + { sizes, size }, + }, + ); + if (R_SUCCEEDED(rc) && out.fonts_loaded) { + font_count = out.total_fonts; + if (font_count > PlSharedFontType_Total) font_count = PlSharedFontType_Total; + if (font_count > max_fonts) font_count = max_fonts; + if (font_count < 0) font_count = 0; + if (total_fonts) *total_fonts = font_count; + if (font_count==0) return rc; - raw->magic = SFCI_MAGIC; - raw->cmd_id = 5; - raw->LanguageCode = LanguageCode; + for (i=0; iresult; - - if (R_SUCCEEDED(rc)) { - if (resp->fonts_loaded==0) return rc; - - font_count = resp->total_fonts; - if (font_count > PlSharedFontType_Total) font_count = PlSharedFontType_Total; - if (font_count > max_fonts) font_count = max_fonts; - if (total_fonts) *total_fonts = font_count; - if (font_count==0) return rc; - - for (i=0; i