Fixing Steam blocking screensaver and sleep

February 17, 2022 - Reading time: 2 minutes

Steam has an issue since long time that prohibits energy saving features of KDE. It is possible to fix that by overriding a SDL function with a preloaded library. The workaround was provided in this comment on GitHub.

First we need to create the ~/Scripts/Steam/SDL_inhibitor/sdl_block_screensaver_inhibit.c file that will be compiled later with the following content

/* sdl_block_screensaver_inhibit.c */

#include <stdio.h>

#ifdef __i386__
static char* arch = "x86";
#else
static char* arch = "x64";
#endif

void SDL_DisableScreenSaver(void) {
    fprintf(stderr, "[%s] prevented SDL_DisableScreenSaver()\n", arch);
}

we can then compile both 32 and 64 bits version

gcc -shared -fPIC -ldl -m32 -o sdl_block_screensaver_inhibit.so sdl_block_screensaver_inhibit.c
gcc -shared -fPIC -ldl -m64 -o sdl_block_screensaver_inhibit_64.so sdl_block_screensaver_inhibit.c

and create the ~/Scripts/Steam/SDL_inhibitor/steam_wrapper.sh script to preload the libraries when starting Steam

#!/bin/bash

PATH_TO_LIB="/home/omano/Scripts/Steam/SDL_inhibitor"

LD_PRELOAD="$PATH_TO_LIB/sdl_block_screensaver_inhibit.so $PATH_TO_LIB/sdl_block_screensaver_inhibit_64.so" \
    SDL_VIDEO_ALLOW_SCREENSAVER=1 \
    STEAM_FRAME_FORCE_CLOSE=1 \
    /usr/bin/steam 2>&1 | grep -v "wrong ELF class: " 

and finally, the ~/.local/share/applications/steam.desktop file needs to be modified on the EXEC line to call the wrapper script instead of starting Steam directly

#Exec=STEAM_FRAME_FORCE_CLOSE=1 /usr/bin/steam %U
Exec=/home/omano/Scripts/Steam/SDL_inhibitor/steam_wrapper.sh

Now Steam will stop prohibiting energy saving features.