From dc67d438dc43e3b8192b035cad2b742eb802cfc2 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Fri, 10 Jul 2020 22:58:07 +0100 Subject: [PATCH] rosalina: properly restore screen filters when lid is reopened --- .../rosalina/include/menus/screen_filters.h | 2 + sysmodules/rosalina/source/main.c | 9 +++++ .../rosalina/source/menus/screen_filters.c | 38 +++++++++++++------ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/sysmodules/rosalina/include/menus/screen_filters.h b/sysmodules/rosalina/include/menus/screen_filters.h index 72323b9..d624cb4 100644 --- a/sysmodules/rosalina/include/menus/screen_filters.h +++ b/sysmodules/rosalina/include/menus/screen_filters.h @@ -32,6 +32,8 @@ extern Menu screenFiltersMenu; extern int screenFiltersCurrentTemperature; +void ScreenFiltersMenu_RestoreCct(void); + void ScreenFiltersMenu_SetDefault(void); // 6500K (default) void ScreenFiltersMenu_SetAquarium(void); // 10000K diff --git a/sysmodules/rosalina/source/main.c b/sysmodules/rosalina/source/main.c index 91a0cfe..297b8e4 100644 --- a/sysmodules/rosalina/source/main.c +++ b/sysmodules/rosalina/source/main.c @@ -156,6 +156,14 @@ static void handleSleepNotification(u32 notificationId) ptmSysmExit(); } +static void handleShellOpenedNotification(u32 notificationId) +{ + (void)notificationId; + + // Note that this is called on system init + ScreenFiltersMenu_RestoreCct(); +} + static void handlePreTermNotification(u32 notificationId) { (void)notificationId; @@ -213,6 +221,7 @@ static const ServiceManagerNotificationEntry notifications[] = { { PTMNOTIFID_FULLY_WAKING_UP, handleSleepNotification }, { PTMNOTIFID_FULLY_AWAKE, handleSleepNotification }, { PTMNOTIFID_HALF_AWAKE, handleSleepNotification }, + { 0x213, handleShellOpenedNotification }, { 0x1000, handleNextApplicationDebuggedByForce }, { 0x2000, handlePreTermNotification }, { 0x3000, handleRestartHbAppNotification }, diff --git a/sysmodules/rosalina/source/menus/screen_filters.c b/sysmodules/rosalina/source/menus/screen_filters.c index 7fe3f9e..1d3d257 100644 --- a/sysmodules/rosalina/source/menus/screen_filters.c +++ b/sysmodules/rosalina/source/menus/screen_filters.c @@ -32,26 +32,29 @@ #include "redshift/redshift.h" #include "redshift/colorramp.h" -typedef struct { - u8 r; - u8 g; - u8 b; - u8 z; +typedef union { + struct { + u8 r; + u8 g; + u8 b; + u8 z; + }; + u32 raw; } Pixel; static u16 g_c[0x600]; static Pixel g_px[0x400]; -int screenFiltersCurrentTemperature = 6500; +int screenFiltersCurrentTemperature = -1; -static void ScreenFiltersMenu_WriteLut(const u32* lut) +static void ScreenFiltersMenu_WriteLut(const Pixel* lut) { GPU_FB_TOP_COL_LUT_INDEX = 0; GPU_FB_BOTTOM_COL_LUT_INDEX = 0; for (int i = 0; i <= 255; i++) { - GPU_FB_TOP_COL_LUT_ELEM = *lut; - GPU_FB_BOTTOM_COL_LUT_ELEM = *lut; + GPU_FB_TOP_COL_LUT_ELEM = lut->raw; + GPU_FB_BOTTOM_COL_LUT_ELEM = lut->raw; lut++; } } @@ -84,7 +87,7 @@ static void ScreenFiltersMenu_ApplyColorSettings(color_setting_t* cs) g_px[i].b = *(g_c + i + 0x200) >> 8; } while(++i); - ScreenFiltersMenu_WriteLut((u32*)g_px); + ScreenFiltersMenu_WriteLut(g_px); } static void ScreenFiltersMenu_SetCct(int cct) @@ -101,7 +104,6 @@ static void ScreenFiltersMenu_SetCct(int cct) ScreenFiltersMenu_ApplyColorSettings(&cs); } - Menu screenFiltersMenu = { "Screen filters menu", { @@ -126,6 +128,20 @@ void ScreenFiltersMenu_Set##name(void)\ ScreenFiltersMenu_SetCct(temp);\ } +void ScreenFiltersMenu_RestoreCct(void) +{ + // Not initialized: return + if (screenFiltersCurrentTemperature == -1) + return; + + // Wait for GSP to restore the CCT table + while (GPU_FB_TOP_COL_LUT_ELEM != g_px[0].raw) + svcSleepThread(10 * 1000 * 1000LL); + + svcSleepThread(10 * 1000 * 1000LL); + ScreenFiltersMenu_WriteLut(g_px); +} + DEF_CCT_SETTER(6500, Default) DEF_CCT_SETTER(10000, Aquarium)