From 2d1116743ee83adf05fec005c0c97e110145e800 Mon Sep 17 00:00:00 2001 From: exelix Date: Tue, 5 Mar 2019 03:37:33 +0100 Subject: [PATCH] Add wrappers for brightness-related functions (#246) * Add wrappers for brightness-related functions --- nx/include/switch/services/lbl.h | 12 ++- nx/source/services/lbl.c | 146 ++++++++++++++++++++++++++++++- 2 files changed, 156 insertions(+), 2 deletions(-) diff --git a/nx/include/switch/services/lbl.h b/nx/include/switch/services/lbl.h index 04062b2b..b9ff7cd6 100644 --- a/nx/include/switch/services/lbl.h +++ b/nx/include/switch/services/lbl.h @@ -1,7 +1,7 @@ /** * @file lbl.h * @brief LBL service IPC wrapper. - * @author SciresM + * @author SciresM, exelix * @copyright libnx Authors */ #pragma once @@ -12,3 +12,13 @@ void lblExit(void); Result lblSwitchBacklightOn(u64 fade_time); Result lblSwitchBacklightOff(u64 fade_time); + +/** + * @note The brightness goes from 0 to 1.0. + */ +Result lblSetCurrentBrightnessSetting(float brightness); +Result lblGetCurrentBrightnessSetting(float *out_value); + +Result lblEnableAutoBrightnessControl(void); +Result lblDisableAutoBrightnessControl(void); +Result lblIsAutoBrightnessControlEnabled(bool *out_value); diff --git a/nx/source/services/lbl.c b/nx/source/services/lbl.c index e4b59806..46e0a649 100644 --- a/nx/source/services/lbl.c +++ b/nx/source/services/lbl.c @@ -29,7 +29,39 @@ void lblExit(void) { } } -static Result _lblSwitchBacklight(u32 cmd_id, u64 fade_time) { +static Result _lblCmdNoIO(u64 cmd_id) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = serviceIpcPrepareHeader(&g_lblSrv, &c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = cmd_id; + + Result rc = serviceIpcDispatch(&g_lblSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + struct { + u64 magic; + u64 result; + } *resp; + + serviceIpcParse(&g_lblSrv, &r, sizeof(*resp)); + resp = r.Raw; + + rc = resp->result; + } + + return rc; +} + +static Result _lblSwitchBacklight(u64 cmd_id, u64 fade_time) { IpcCommand c; ipcInitialize(&c); @@ -70,3 +102,115 @@ Result lblSwitchBacklightOn(u64 fade_time) { Result lblSwitchBacklightOff(u64 fade_time) { return _lblSwitchBacklight(7, fade_time); } + +Result lblSetCurrentBrightnessSetting(float brightness) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + float value; + } *raw; + + raw = serviceIpcPrepareHeader(&g_lblSrv, &c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 2; + raw->value = brightness; + + Result rc = serviceIpcDispatch(&g_lblSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + struct { + u64 magic; + u64 result; + } *resp; + + serviceIpcParse(&g_lblSrv, &r, sizeof(*resp)); + resp = r.Raw; + + rc = resp->result; + } + + return rc; +} + +Result lblGetCurrentBrightnessSetting(float *out_value) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = serviceIpcPrepareHeader(&g_lblSrv, &c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 3; + + Result rc = serviceIpcDispatch(&g_lblSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + struct { + u64 magic; + u64 result; + float brightness; + } *resp; + + serviceIpcParse(&g_lblSrv, &r, sizeof(*resp)); + resp = r.Raw; + + rc = resp->result; + if (R_SUCCEEDED(rc)) + *out_value = resp->brightness; + } + + return rc; +} + +Result lblEnableAutoBrightnessControl(void) { + return _lblCmdNoIO(12); +} + +Result lblDisableAutoBrightnessControl(void) { + return _lblCmdNoIO(13); +} + +Result lblIsAutoBrightnessControlEnabled(bool *out_value){ + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = serviceIpcPrepareHeader(&g_lblSrv, &c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 14; + + Result rc = serviceIpcDispatch(&g_lblSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + struct { + u64 magic; + u64 result; + u8 isEnabled; + } *resp; + + serviceIpcParse(&g_lblSrv, &r, sizeof(*resp)); + resp = r.Raw; + + rc = resp->result; + if (R_SUCCEEDED(rc)) + *out_value = resp->isEnabled != 0; + } + + return rc; +}