From 5d791447063e5bda74458d0aed2062231df387b7 Mon Sep 17 00:00:00 2001 From: Ningyuan Li Date: Fri, 19 Jan 2024 23:49:52 +0900 Subject: [PATCH] power state & turn screen on --- src/core/webos/SDL_webos_init.c | 77 +++++++++++++++++++++++++++++++++ src/core/webos/SDL_webos_init.h | 12 +++++ 2 files changed, 89 insertions(+) diff --git a/src/core/webos/SDL_webos_init.c b/src/core/webos/SDL_webos_init.c index 1081508011bc9..7a188aa8035ae 100644 --- a/src/core/webos/SDL_webos_init.c +++ b/src/core/webos/SDL_webos_init.c @@ -14,6 +14,7 @@ static SDL_bool s_appRegistered = SDL_FALSE; static int s_nativeLifeCycleInterfaceVersion = 0; static LSHandle *s_LSHandle = NULL; +static SDL_webOSPowerState s_powerState = SDL_WEBOS_POWER_STATE_UNKNOWN; static int getNativeLifeCycleInterfaceVersion(const char *appId); @@ -24,6 +25,11 @@ static int lifecycleCallbackVersion2(LSHandle *sh, LSMessage *reply, HContext *c static SDL_bool registerScreenSaverRequest(const char *appId); static int screenSaverRequestCallback(LSHandle *sh, LSMessage *reply, HContext *ctx); +static SDL_bool registerPowerState(); +static int powerStateCallback(LSHandle *sh, LSMessage *reply, HContext *ctx); + +static SDL_bool turnOnScreen(); + static HContext s_AppLifecycleContext = { .multiple = 1, .pub = 1, @@ -34,6 +40,11 @@ static HContext s_ScreenSaverRequestContext = { .pub = 1, }; +static HContext s_PowerStateContext = { + .multiple = 1, + .pub = 1, +}; + SDL_bool SDL_webOSSetLSHandle(LSHandle *handle) { s_LSHandle = handle; @@ -69,6 +80,9 @@ int SDL_webOSRegisterApp() if (!registerScreenSaverRequest(appId)) { SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Failed to register screen saver request"); } + if (!registerPowerState()) { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Failed to register power state"); + } } s_appRegistered = SDL_TRUE; return 0; @@ -91,9 +105,28 @@ void SDL_webOSUnregisterApp() } s_ScreenSaverRequestContext.callback = NULL; } + if (s_PowerStateContext.callback != NULL) { + HELPERS_HUnregisterServiceCallback(&s_PowerStateContext); + s_PowerStateContext.callback = NULL; + } s_appRegistered = SDL_FALSE; } +SDL_webOSPowerState SDL_webOSGetPowerState() +{ + return s_powerState; +} + +void SDL_webOSTurnOnScreen() +{ + if (s_powerState == SDL_WEBOS_POWER_STATE_ACTIVE) { + return; + } + if (!turnOnScreen()) { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Failed to turn on screen"); + } +} + int getNativeLifeCycleInterfaceVersion(const char *appId) { char payload[200]; @@ -257,4 +290,48 @@ static int screenSaverRequestCallback(LSHandle *sh, LSMessage *reply, HContext * return 1; } +static SDL_bool registerPowerState() +{ + s_PowerStateContext.callback = powerStateCallback; + return HELPERS_HLunaServiceCall("luna://com.webos.service.tvpower/power/getPowerState", "{\"subscribe\":true}", + &s_PowerStateContext) == 0; +} + +static int powerStateCallback(LSHandle *sh, LSMessage *reply, HContext *ctx) +{ + jdomparser_ref parser = NULL; + jvalue_ref parsed = NULL; + jvalue_ref state = NULL; + if ((parsed = SDL_webOSJsonParse(HELPERS_HLunaServiceMessage(reply), &parser, 1)) == NULL) { + return 0; + } + if (PBNJSON_jobject_get_exists(parsed, J_CSTR_TO_BUF("state"), &state)) { + raw_buffer state_buf = PBNJSON_jstring_get_fast(state); + if (state_buf.m_str) { + if (SDL_strncmp(state_buf.m_str, "Active", state_buf.m_len) == 0) { + s_powerState = SDL_WEBOS_POWER_STATE_ACTIVE; + } else if (SDL_strncmp(state_buf.m_str, "Screen Saver", state_buf.m_len) == 0) { + s_powerState = SDL_WEBOS_POWER_STATE_SCREEN_SAVER; + } else if (SDL_strncmp(state_buf.m_str, "Screen Off", state_buf.m_len) == 0) { + s_powerState = SDL_WEBOS_POWER_STATE_POWER_OFF; + } else { + s_powerState = SDL_WEBOS_POWER_STATE_UNKNOWN; + } + } + } + PBNJSON_jdomparser_release(&parser); + return 1; +} + +static SDL_bool turnOnScreen() +{ + char *output = NULL; + if (!SDL_webOSLunaServiceCallSync("luna://com.webos.service.tvpower/power/turnOnScreen", "{}", 1, &output)) { + SDL_SetError("Failed to call luna://com.webos.service.tvpower/power/turnOnScreen"); + return SDL_FALSE; + } + free(output); + return SDL_TRUE; +} + #endif // __WEBOS__ diff --git a/src/core/webos/SDL_webos_init.h b/src/core/webos/SDL_webos_init.h index 0c8ac049dad70..fd3b1ad058a20 100644 --- a/src/core/webos/SDL_webos_init.h +++ b/src/core/webos/SDL_webos_init.h @@ -25,6 +25,14 @@ #ifndef SDL_webos_init_h_ #define SDL_webos_init_h_ +typedef enum SDL_webOSPowerState +{ + SDL_WEBOS_POWER_STATE_UNKNOWN, + SDL_WEBOS_POWER_STATE_ACTIVE, + SDL_WEBOS_POWER_STATE_POWER_OFF, + SDL_WEBOS_POWER_STATE_SCREEN_SAVER, +} SDL_webOSPowerState; + extern void SDL_webOSInitLSHandle(); extern SDL_bool SDL_webOSAppRegistered(); @@ -33,6 +41,10 @@ extern int SDL_webOSRegisterApp(); extern void SDL_webOSUnregisterApp(); +extern SDL_webOSPowerState SDL_webOSGetPowerState(); + +extern void SDL_webOSTurnOnScreen(); + #endif /* SDL_webos_init_h_ */ #endif /* __WEBOS__ */