webOS: how to disable the screensaver in your LG app

I’ve recently made a web app for my LG OLED TV, and I’ve encountered an issue where the TV would constantly be showing the fireworks screensaver every 2 minutes or so, even while the app was playing video. After spending hours searching for a solution, I’ve finally discovered this comment by @Mariotaku on a GitHub PR, and it ended up being as simple as using an undocumented API endpoint.

Web apps

You can register on this Luna URI: luna://com.webos.service.tvpower/power/registerScreenSaverRequest

Include the following payload:

{
    "subscribe": true,
    "clientName": "AnyValue"
}

When the screensaver is about to start, you will receive the following message:

{
    "returnValue": true,
    "timestamp": "1388518297",
    "state": "Active",
    "instantBoot": "on"
}

To prevent the screensaver from starting, reply by sending a message to luna://com.webos.service.tvpower/power/responseScreenSaverRequest with the following contents (use the timestamp you’ve received previously):

{
    "clientName": "AnyValue",
    "ack": false,
    "timestamp": "1388518297"
}

Code sample

var bridge = new WebOSServiceBridge();

bridge.onservicecallback = (msg) => {
    message = JSON.parse(msg);
    if (message.state = "Active") {
        bridge.call("luna://com.webos.service.tvpower/power/responseScreenSaverRequest", JSON.stringify({
            "clientName": "myWebApp",
            "ack": false,
            "timestamp": message.timestamp
        }));
    }
}

bridge.call("luna://com.webos.service.tvpower/power/registerScreenSaverRequest", JSON.stringify({
    "subscribe": true,
    "clientName": "myWebApp"
}))

Native apps

Use SDL_DisableScreenSaver and SDL_EnableScreenSaver.