From 900df7882dbf613b581098be9c392385b897e193 Mon Sep 17 00:00:00 2001 From: Michael Theall Date: Mon, 27 Apr 2020 19:57:09 -0500 Subject: [PATCH] Press select/minus to toggle backlight --- README.md | 3 ++ source/3ds/platform.cpp | 63 +++++++++++++++++++++++++++++++++++++- source/nds/platform.cpp | 22 +++++++------ source/switch/platform.cpp | 42 +++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c09e069..c0fc6e5 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ FTP Server for 3DS/Switch/Linux. - Supports multiple simultaneous clients. The 3DS itself only appears to support enough sockets to perform 4-5 simultaneous data transfers, so it will help if you limit your FTP client to this many parallel requests. - Cutting-edge graphics. +- Toggle backlight on 3DS with SELECT button +- Toggle backlight on Switch with MINUS button + ## Latest Builds NDS: https://mtheall.com/~mtheall/ftpd.nds diff --git a/source/3ds/platform.cpp b/source/3ds/platform.cpp index 4f2bacf..f89e25a 100644 --- a/source/3ds/platform.cpp +++ b/source/3ds/platform.cpp @@ -70,6 +70,12 @@ u32 *s_socuBuffer = nullptr; /// \brief ac:u fence platform::Mutex s_acuFence; +/// \brief Whether to power backlight +bool s_backlight = true; + +/// \brief APT hook cookie +aptHookCookie s_aptHookCookie; + #ifdef CLASSIC in_addr_t s_addr = 0; #else @@ -118,6 +124,43 @@ C3D_Tex s_gfxTexture; Tex3DS_Texture s_gfxT3x; #endif +/// \brief Enable backlight +/// \param enable_ Whether to enable backligh +void enableBacklight (bool const enable_) +{ + if (R_FAILED (gspLcdInit ())) + return; + + (enable_ ? GSPLCD_PowerOnBacklight : GSPLCD_PowerOffBacklight) (GSPLCD_SCREEN_BOTH); + + gspLcdExit (); +} + +/// \brief Handle APT cookie +/// \param type_ Hook type +/// \param param_ User param +void handleAPTHook (APT_HookType const type_, void *const param_) +{ + switch (type_) + { + case APTHOOK_ONSUSPEND: + case APTHOOK_ONSLEEP: + // turn on backlight, or you can't see the home menu! + if (!s_backlight) + enableBacklight (true); + break; + + case APTHOOK_ONRESTORE: + case APTHOOK_ONWAKEUP: + // restore backlight setting + enableBacklight (s_backlight); + break; + + default: + break; + } +} + /// \brief Get network visibility bool getNetworkVisibility () { @@ -328,6 +371,8 @@ bool platform::init () std::setvbuf (stderr, nullptr, _IOLBF, 0); #endif + aptHook (&s_aptHookCookie, handleAPTHook, nullptr); + #ifndef CLASSIC // initialize citro3d C3D_Init (2 * C3D_DEFAULT_CMDBUF_SIZE); @@ -392,9 +437,19 @@ bool platform::loop () hidScanInput (); - if (hidKeysDown () & KEY_START) + auto const kDown = hidKeysDown (); + + // check if the user wants to exit + if (kDown & KEY_START) return false; + // check if the user wants to toggle the backlight + if (kDown & KEY_SELECT) + { + s_backlight = !s_backlight; + enableBacklight (s_backlight); + } + #ifndef CLASSIC auto &io = ImGui::GetIO (); @@ -455,6 +510,12 @@ void platform::exit () s_socuActive = false; std::free (s_socuBuffer); + aptUnhook (&s_aptHookCookie); + + // turn backlight back on + if (!s_backlight) + enableBacklight (true); + gfxExit (); #ifndef CLASSIC romfsExit (); diff --git a/source/nds/platform.cpp b/source/nds/platform.cpp index 8a72283..4ff844e 100644 --- a/source/nds/platform.cpp +++ b/source/nds/platform.cpp @@ -40,6 +40,8 @@ PrintConsole g_sessionConsole; namespace { struct in_addr s_addr = {0}; +/// \brief Whether to power backlight +bool s_backlight = true; } bool platform::networkVisible () @@ -99,9 +101,18 @@ bool platform::loop () { scanKeys (); - if (keysDown () & KEY_START) + // check if the user wants to exit + auto const kDown = keysDown (); + if (kDown & KEY_START) return false; + // check if the user wants to toggle the backlight + if (kDown & KEY_SELECT) + { + s_backlight = !s_backlight; + (s_backlight ? powerOn : powerOff) (POWER_LCD); + } + return true; } @@ -112,12 +123,5 @@ void platform::render () void platform::exit () { - info ("Press any key to exit\n"); - render (); - - do - { - swiWaitForVBlank (); - scanKeys (); - } while (!keysDown ()); + powerOn (POWER_LCD); } diff --git a/source/switch/platform.cpp b/source/switch/platform.cpp index 5d17260..279edca 100644 --- a/source/switch/platform.cpp +++ b/source/switch/platform.cpp @@ -50,6 +50,12 @@ PrintConsole g_sessionConsole; namespace { +/// \brief Whether to power backlight +bool s_backlight = true; + +/// \brief Applet hook cookie +AppletHookCookie s_appletHookCookie; + #ifdef CLASSIC in_addr_t s_addr = 0; #else @@ -397,6 +403,24 @@ void deko3dExit () } #endif +/// \brief Handle applet hook +/// \param hook_ Callback reason +/// \param param_ User param +void handleAppletHook (AppletHookType const hook_, void *const param_) +{ + (void)param_; + switch (hook_) + { + case AppletHookType_OnFocusState: + if (appletGetFocusState () == AppletFocusState_Focused) + appletSetLcdBacklightOffEnabled (!s_backlight); + break; + + default: + break; + } +} + /// \brief Draw status void drawStatus () { @@ -503,6 +527,8 @@ void drawStatus () } } + + bool platform::init () { #ifdef CLASSIC @@ -534,6 +560,8 @@ bool platform::init () FB_NUM); #endif + appletHook (&s_appletHookCookie, handleAppletHook, nullptr); + return true; } @@ -561,9 +589,18 @@ bool platform::loop () hidScanInput (); auto const keysDown = hidKeysDown (CONTROLLER_P1_AUTO); + + // check if the user wants to exit if (keysDown & KEY_PLUS) return false; + // check if the user wants to toggle the backlight + if (keysDown & KEY_MINUS) + { + s_backlight = !s_backlight; + appletSetLcdBacklightOffEnabled (!s_backlight); + } + #ifndef CLASSIC imgui::nx::newFrame (); ImGui::NewFrame (); @@ -647,6 +684,11 @@ void platform::exit () imgui::deko3d::exit (); deko3dExit (); #endif + + appletUnhook (&s_appletHookCookie); + + if (!s_backlight) + appletSetLcdBacklightOffEnabled (false); } ///////////////////////////////////////////////////////////////////////////