From 5e8a88ce85474a04e1fe4557f0f44a7d6994af0d Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 12 Dec 2018 21:13:17 +0100 Subject: [PATCH] Introduce nwindowGetDefault --- nx/include/switch/display/native_window.h | 11 +++++ nx/source/display/default_window.c | 51 +++++++++++++++++++++++ nx/source/runtime/init.c | 5 +++ 3 files changed, 67 insertions(+) create mode 100644 nx/source/display/default_window.c diff --git a/nx/include/switch/display/native_window.h b/nx/include/switch/display/native_window.h index 35f633d0..61a050bd 100644 --- a/nx/include/switch/display/native_window.h +++ b/nx/include/switch/display/native_window.h @@ -43,6 +43,17 @@ typedef struct NWindow { /// Checks whether a pointer refers to a valid \ref NWindow object. bool nwindowIsValid(NWindow* nw); +/** + * @brief Retrieves the default \ref NWindow object. + * @return Pointer to the default \ref NWindow object. + * @note When this function is used/referenced, libnx will initialize VI services + * and create a \ref NWindow object from a \ref ViLayer created on the default \ref ViDisplay; + * all of this happening automatically during application startup (i.e. before main is called). + * If creating the default \ref NWindow fails, libnx will throw a LibnxError_BadGfxInit fatal error. + * Likewise, after main returns (or exit is called) libnx will clean up all resources used by it. + */ +NWindow* nwindowGetDefault(void); + /** * @brief Creates a \ref NWindow. * @param[out] nw Output \ref NWindow structure. diff --git a/nx/source/display/default_window.c b/nx/source/display/default_window.c new file mode 100644 index 00000000..9d4146e3 --- /dev/null +++ b/nx/source/display/default_window.c @@ -0,0 +1,51 @@ +#include +#include "types.h" +#include "result.h" +#include "services/vi.h" +#include "services/fatal.h" +#include "display/binder.h" +#include "display/buffer_producer.h" +#include "display/native_window.h" +#include "nvidia/graphic_buffer.h" + +static ViDisplay g_viDisplay; +static ViLayer g_viLayer; +static NWindow g_defaultWin; + +NWindow* nwindowGetDefault(void) +{ + return &g_defaultWin; +} + +void __nx_win_init(void) +{ + Result rc; + rc = viInitialize(ViServiceType_Default); + if (R_SUCCEEDED(rc)) { + rc = viOpenDefaultDisplay(&g_viDisplay); + if (R_SUCCEEDED(rc)) { + rc = viCreateLayer(&g_viDisplay, &g_viLayer); + if (R_SUCCEEDED(rc)) { + rc = viSetLayerScalingMode(&g_viLayer, ViScalingMode_FitToLayer); + if (R_SUCCEEDED(rc)) + rc = nwindowCreateFromLayer(&g_defaultWin, &g_viLayer); + if (R_FAILED(rc)) + viCloseLayer(&g_viLayer); + } + if (R_FAILED(rc)) + viCloseDisplay(&g_viDisplay); + } + if (R_FAILED(rc)) + viExit(); + } + if (R_FAILED(rc)) + fatalSimple(MAKERESULT(Module_Libnx, LibnxError_BadGfxInit)); +} + +void __nx_win_exit(void) +{ + nwindowClose(&g_defaultWin); + viCloseLayer(&g_viLayer); + viCloseDisplay(&g_viDisplay); + viExit(); +} diff --git a/nx/source/runtime/init.c b/nx/source/runtime/init.c index d214c177..fc7b5c23 100644 --- a/nx/source/runtime/init.c +++ b/nx/source/runtime/init.c @@ -95,6 +95,7 @@ void __attribute__((weak)) __libnx_initheap(void) fake_heap_end = (char*)addr + size; } +void __attribute__((weak)) __nx_win_init(void); void __attribute__((weak)) userAppInit(void); void __attribute__((weak)) __appInit(void) @@ -128,14 +129,18 @@ void __attribute__((weak)) __appInit(void) fsdevMountSdmc(); + if (&__nx_win_init) __nx_win_init(); if (&userAppInit) userAppInit(); } void __attribute__((weak)) userAppExit(void); +void __attribute__((weak)) __nx_win_exit(void); void __attribute__((weak)) __appExit(void) { if (&userAppExit) userAppExit(); + if (&__nx_win_exit) __nx_win_exit(); + // Cleanup default services. fsdevUnmountAll(); fsExit();