mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-22 04:52:39 +02:00
Added plGetSharedFontByType(). Added _plRequestLoadWait which is used internally for both plGetSharedFontByType() and plGetSharedFont(). Moved offset/size validation code in pl into a dedicated func.
This commit is contained in:
parent
9bb649b5e2
commit
6e1cc4dc1e
@ -29,6 +29,9 @@ Result plInitialize(void);
|
|||||||
void plExit(void);
|
void plExit(void);
|
||||||
void* plGetSharedmemAddr(void);
|
void* plGetSharedmemAddr(void);
|
||||||
|
|
||||||
|
///< Gets a specific shared-font via SharedFontType, see \ref PlSharedFontType.
|
||||||
|
Result plGetSharedFontByType(PlFontData* font, u32 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, size_t max_fonts, size_t* total_fonts);
|
||||||
|
|
||||||
|
@ -57,6 +57,147 @@ void* plGetSharedmemAddr(void) {
|
|||||||
return shmemGetAddr(&g_plSharedmem);
|
return shmemGetAddr(&g_plSharedmem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) {
|
static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) {
|
||||||
IpcCommand c;
|
IpcCommand c;
|
||||||
ipcInitialize(&c);
|
ipcInitialize(&c);
|
||||||
@ -92,7 +233,60 @@ static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) {
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Result _plVerifyFontRange(u32 offset, u32 size) {
|
||||||
|
if (offset >= SHAREDMEMFONT_SIZE || size > SHAREDMEMFONT_SIZE || ((u64)offset + (u64)size) > SHAREDMEMFONT_SIZE)
|
||||||
|
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Result _plRequestLoadWait(u32 SharedFontType) {
|
||||||
|
Result rc=0;
|
||||||
|
u32 LoadState=0;
|
||||||
|
bool load_started = 0;
|
||||||
|
|
||||||
|
while(1) {//Wait/request for the font to be loaded if it's not already loaded.
|
||||||
|
if (load_started) svcSleepThread(1000000000ULL / 60);
|
||||||
|
|
||||||
|
rc = _plGetLoadState(SharedFontType, &LoadState);
|
||||||
|
if (R_FAILED(rc) || LoadState==0x1) break;
|
||||||
|
|
||||||
|
if (load_started) continue;
|
||||||
|
load_started = 1;
|
||||||
|
rc = _plRequestLoad(SharedFontType);
|
||||||
|
if (R_FAILED(rc)) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType) {
|
||||||
|
Result rc=0;
|
||||||
|
u8* sharedmem_addr = (u8*)plGetSharedmemAddr();
|
||||||
|
|
||||||
|
memset(font, 0, sizeof(PlFontData));
|
||||||
|
|
||||||
|
font->type = SharedFontType;
|
||||||
|
|
||||||
|
rc = _plRequestLoadWait(SharedFontType);
|
||||||
|
if (R_FAILED(rc)) return rc;
|
||||||
|
|
||||||
|
rc = _plGetSize(SharedFontType, &font->size);
|
||||||
|
if (R_FAILED(rc)) return rc;
|
||||||
|
|
||||||
|
rc = _plGetSharedMemoryAddressOffset(SharedFontType, &font->offset);
|
||||||
|
if (R_FAILED(rc)) return rc;
|
||||||
|
|
||||||
|
rc = _plVerifyFontRange(font->offset, font->size);
|
||||||
|
if (R_FAILED(rc)) return rc;
|
||||||
|
|
||||||
|
font->address = &sharedmem_addr[font->offset];
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, size_t* total_fonts) {
|
Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, size_t* total_fonts) {
|
||||||
|
Result rc=0;
|
||||||
u32 types[PlSharedFontType_Total];
|
u32 types[PlSharedFontType_Total];
|
||||||
u32 offsets[PlSharedFontType_Total];
|
u32 offsets[PlSharedFontType_Total];
|
||||||
u32 sizes[PlSharedFontType_Total];
|
u32 sizes[PlSharedFontType_Total];
|
||||||
@ -100,12 +294,6 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si
|
|||||||
u32 font_count=0, i;
|
u32 font_count=0, i;
|
||||||
u8* sharedmem_addr = (u8*)plGetSharedmemAddr();
|
u8* sharedmem_addr = (u8*)plGetSharedmemAddr();
|
||||||
|
|
||||||
IpcCommand c;
|
|
||||||
ipcInitialize(&c);
|
|
||||||
ipcAddRecvBuffer(&c, types, size, 0);
|
|
||||||
ipcAddRecvBuffer(&c, offsets, size, 0);
|
|
||||||
ipcAddRecvBuffer(&c, sizes, size, 0);
|
|
||||||
|
|
||||||
memset(types, 0, sizeof(types));
|
memset(types, 0, sizeof(types));
|
||||||
memset(offsets, 0, sizeof(offsets));
|
memset(offsets, 0, sizeof(offsets));
|
||||||
memset(sizes, 0, sizeof(sizes));
|
memset(sizes, 0, sizeof(sizes));
|
||||||
@ -113,6 +301,17 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si
|
|||||||
|
|
||||||
if (total_fonts) *total_fonts = 0;
|
if (total_fonts) *total_fonts = 0;
|
||||||
|
|
||||||
|
for (i=0; i<PlSharedFontType_Total; i++) {
|
||||||
|
rc = _plRequestLoadWait(i);
|
||||||
|
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 magic;
|
||||||
u64 cmd_id;
|
u64 cmd_id;
|
||||||
@ -125,7 +324,7 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si
|
|||||||
raw->cmd_id = 5;
|
raw->cmd_id = 5;
|
||||||
raw->LanguageCode = LanguageCode;
|
raw->LanguageCode = LanguageCode;
|
||||||
|
|
||||||
Result rc = serviceIpcDispatch(&g_plSrv);
|
rc = serviceIpcDispatch(&g_plSrv);
|
||||||
|
|
||||||
if (R_SUCCEEDED(rc)) {
|
if (R_SUCCEEDED(rc)) {
|
||||||
IpcParsedCommand r;
|
IpcParsedCommand r;
|
||||||
@ -150,8 +349,8 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si
|
|||||||
if (font_count==0) return rc;
|
if (font_count==0) return rc;
|
||||||
|
|
||||||
for (i=0; i<font_count; i++) {
|
for (i=0; i<font_count; i++) {
|
||||||
if (offsets[i] >= SHAREDMEMFONT_SIZE || sizes[i] > SHAREDMEMFONT_SIZE || ((u64)offsets[i] + (u64)sizes[i]) > SHAREDMEMFONT_SIZE)
|
rc = _plVerifyFontRange(offsets[i], sizes[i]);
|
||||||
return MAKERESULT(Module_Libnx, LibnxError_BadInput);
|
if (R_FAILED(rc)) return rc;
|
||||||
|
|
||||||
fonts[i].type = types[i];
|
fonts[i].type = types[i];
|
||||||
fonts[i].offset = offsets[i];
|
fonts[i].offset = offsets[i];
|
||||||
|
Loading…
Reference in New Issue
Block a user