pl: Updated for new-ipc. Updated param types for plGetSharedFontByType and plGetSharedFont. Improved docs and minor other changes.

This commit is contained in:
yellows8 2019-10-11 01:47:23 -04:00
parent 44c45555cd
commit d25144afbd
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43
2 changed files with 84 additions and 252 deletions

View File

@ -6,9 +6,10 @@
*/ */
#pragma once #pragma once
#include "../types.h" #include "../types.h"
#include "../sf/service.h"
typedef enum /// SharedFontType
{ typedef enum {
PlSharedFontType_Standard = 0, ///< Japan, US and Europe PlSharedFontType_Standard = 0, ///< Japan, US and Europe
PlSharedFontType_ChineseSimplified = 1, ///< Chinese Simplified PlSharedFontType_ChineseSimplified = 1, ///< Chinese Simplified
PlSharedFontType_ExtChineseSimplified = 2, ///< Extended Chinese Simplified PlSharedFontType_ExtChineseSimplified = 2, ///< Extended Chinese Simplified
@ -18,21 +19,29 @@ typedef enum
PlSharedFontType_Total, ///< Total fonts supported by this enum. PlSharedFontType_Total, ///< Total fonts supported by this enum.
} PlSharedFontType; } PlSharedFontType;
/// FontData
typedef struct { typedef struct {
u32 type; u32 type; ///< \ref PlSharedFontType
u32 offset; u32 offset; ///< Offset of the font in sharedmem.
u32 size; u32 size; ///< Size of the font.
void* address; void* address; ///< Address of the actual font.
} PlFontData; } PlFontData;
/// Initialize pl.
Result plInitialize(void); Result plInitialize(void);
/// Exit pl.
void plExit(void); void plExit(void);
/// Gets the Service object for the actual pl service session.
Service* plGetServiceSession(void); Service* plGetServiceSession(void);
/// Gets the address of the SharedMemory.
void* plGetSharedmemAddr(void); void* plGetSharedmemAddr(void);
///< Gets a specific shared-font via SharedFontType, see \ref PlSharedFontType. ///< Gets a specific shared-font via \ref PlSharedFontType.
Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType); Result plGetSharedFontByType(PlFontData* font, PlSharedFontType SharedFontType);
///< Gets shared font(s). ///< 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);

View File

@ -1,39 +1,28 @@
#define NX_SERVICE_ASSUME_NON_DOMAIN
#include "service_guard.h"
#include <string.h> #include <string.h>
#include "types.h"
#include "result.h"
#include "arm/atomics.h"
#include "kernel/ipc.h"
#include "kernel/shmem.h" #include "kernel/shmem.h"
#include "services/sm.h"
#include "services/pl.h" #include "services/pl.h"
#define SHAREDMEMFONT_SIZE 0x1100000 #define SHAREDMEMFONT_SIZE 0x1100000
static Service g_plSrv; static Service g_plSrv;
static u64 g_plRefCnt;
static SharedMemory g_plSharedmem; static SharedMemory g_plSharedmem;
static Result _plGetSharedMemoryNativeHandle(Handle* handle_out); static Result _plGetSharedMemoryNativeHandle(Handle* handle_out);
Result plInitialize(void) NX_GENERATE_SERVICE_GUARD(pl);
{
Result _plInitialize(void) {
Result rc=0; Result rc=0;
Handle sharedmem_handle=0; Handle sharedmem_handle=0;
atomicIncrement64(&g_plRefCnt);
if (serviceIsActive(&g_plSrv))
return 0;
rc = smGetService(&g_plSrv, "pl:u"); rc = smGetService(&g_plSrv, "pl:u");
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc)) {
{
rc = _plGetSharedMemoryNativeHandle(&sharedmem_handle); rc = _plGetSharedMemoryNativeHandle(&sharedmem_handle);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc)) {
{
shmemLoadRemote(&g_plSharedmem, sharedmem_handle, 0x1100000, Perm_R); shmemLoadRemote(&g_plSharedmem, sharedmem_handle, 0x1100000, Perm_R);
rc = shmemMap(&g_plSharedmem); rc = shmemMap(&g_plSharedmem);
@ -45,12 +34,9 @@ Result plInitialize(void)
return rc; return rc;
} }
void plExit(void) void _plCleanup(void) {
{
if (atomicDecrement64(&g_plRefCnt) == 0) {
serviceClose(&g_plSrv); serviceClose(&g_plSrv);
shmemClose(&g_plSharedmem); shmemClose(&g_plSharedmem);
}
} }
Service* plGetServiceSession(void) { Service* plGetServiceSession(void) {
@ -61,180 +47,39 @@ void* plGetSharedmemAddr(void) {
return shmemGetAddr(&g_plSharedmem); 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) { static Result _plRequestLoad(u32 SharedFontType) {
IpcCommand c; return _plCmdInU32NoOut(&g_plSrv, SharedFontType, 0);
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;
} }
static Result _plGetLoadState(u32 SharedFontType, u32* LoadState) { static Result _plGetLoadState(u32 SharedFontType, u32* LoadState) {
IpcCommand c; return _plCmdInU32OutU32(&g_plSrv, SharedFontType, LoadState, 1);
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;
} }
static Result _plGetSize(u32 SharedFontType, u32* size) { static Result _plGetSize(u32 SharedFontType, u32* size) {
IpcCommand c; return _plCmdInU32OutU32(&g_plSrv, SharedFontType, size, 2);
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;
} }
static Result _plGetSharedMemoryAddressOffset(u32 SharedFontType, u32* offset) { static Result _plGetSharedMemoryAddressOffset(u32 SharedFontType, u32* offset) {
IpcCommand c; return _plCmdInU32OutU32(&g_plSrv, SharedFontType, offset, 3);
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;
} }
static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) { static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) {
IpcCommand c; return _plCmdGetHandle(&g_plSrv, handle_out, 4);
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;
} }
static Result _plVerifyFontRange(u32 offset, u32 size) { static Result _plVerifyFontRange(u32 offset, u32 size) {
@ -264,7 +109,7 @@ static Result _plRequestLoadWait(u32 SharedFontType) {
return rc; return rc;
} }
Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType) { Result plGetSharedFontByType(PlFontData* font, PlSharedFontType SharedFontType) {
Result rc=0; Result rc=0;
u8* sharedmem_addr = (u8*)plGetSharedmemAddr(); u8* sharedmem_addr = (u8*)plGetSharedmemAddr();
@ -289,18 +134,15 @@ Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType) {
return rc; 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; Result rc=0;
u32 types[PlSharedFontType_Total]; u32 types[PlSharedFontType_Total]={0};
u32 offsets[PlSharedFontType_Total]; u32 offsets[PlSharedFontType_Total]={0};
u32 sizes[PlSharedFontType_Total]; u32 sizes[PlSharedFontType_Total]={0};
size_t size = sizeof(u32) * PlSharedFontType_Total; size_t size = PlSharedFontType_Total*sizeof(u32);
u32 font_count=0, i; s32 font_count=0, i;
u8* sharedmem_addr = (u8*)plGetSharedmemAddr(); 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); memset(fonts, 0, sizeof(PlFontData) * max_fonts);
if (total_fonts) *total_fonts = 0; if (total_fonts) *total_fonts = 0;
@ -310,45 +152,28 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si
if (R_FAILED(rc)) return rc; 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 { struct {
u64 magic;
u64 cmd_id;
u64 LanguageCode;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 5;
raw->LanguageCode = LanguageCode;
rc = serviceIpcDispatch(&g_plSrv);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u8 fonts_loaded; u8 fonts_loaded;
u32 total_fonts; s32 total_fonts;
} *resp = r.Raw; } out;
rc = resp->result; rc = serviceDispatchInOut(&g_plSrv, 5, LanguageCode, out,
.buffer_attrs = {
if (R_SUCCEEDED(rc)) { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out,
if (resp->fonts_loaded==0) return rc; SfBufferAttr_HipcMapAlias | SfBufferAttr_Out,
SfBufferAttr_HipcMapAlias | SfBufferAttr_Out,
font_count = resp->total_fonts; },
.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 > PlSharedFontType_Total) font_count = PlSharedFontType_Total;
if (font_count > max_fonts) font_count = max_fonts; if (font_count > max_fonts) font_count = max_fonts;
if (font_count < 0) font_count = 0;
if (total_fonts) *total_fonts = font_count; if (total_fonts) *total_fonts = font_count;
if (font_count==0) return rc; if (font_count==0) return rc;
@ -363,7 +188,5 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si
fonts[i].address = &sharedmem_addr[offsets[i]]; fonts[i].address = &sharedmem_addr[offsets[i]];
} }
} }
}
return rc; return rc;
} }