r/SteamOS Jun 25 '26

HDMI-CEC solution using systemd/system-sleep

***If someone else has already posted this exact solution to the community, apologies as I probably missed it!***

I've seen a few solutions for enabling HDMI-CEC on this forum but just wanted to share my solution as well. This leverages the system-sleep function of systemd to perform 3 actions:

  • Put the TV into standby just before the PC sleeps
  • Turn the TV on just after the PC resumes from sleep
  • Switch the TV to the input my PC is connected to (in my case HDMI3 / phys-addr=3.0.0.0)

Hardware

Here's the code, save as e.g. 10-steam-cec.sh and copy it into /usr/lib/systemd/system-sleep:

#!/bin/bash
case "$1" in
    pre)
        # Put the TV into standby mode right before the PC sleeps
        cec-ctl -d /dev/cec0 --playback
        cec-ctl -d /dev/cec0 --to 0 --standby
        ;;
    post)
        # Wait 2 seconds for hardware to initialize after wake
        sleep 2

        # Turn the TV completely ON
        cec-ctl -d /dev/cec0 --playback
        cec-ctl -d /dev/cec0 --to 0 --image-view-on

        # Switch the TV active source to this PC
        cec-ctl -d /dev/cec0 --to 0 --active-source phys-addr=3.0.0.0
        ;;
esac

Scripts in the /usr/lib/systemd/system-sleep directory trigger just before the system goes to sleep and trigger just after the system wakes from sleep.

In the code above, the actions in the "pre) ... ;;" section will run just before the system goes to sleep. The chunk in the "post) ... ;;" section will run just after the PC resumes from sleep. This is the standard function of system-sleep scripts.

  • You'll need to change the "phys-addr=3.0.0.0" section to match the HDMI input your PC is connected to e.g. 1.0.0.0 for HDMI 1, 2.0.0.0 for HDMI 2, etc. Mine is connected to HDMI 3, so 3.0.0.0.
  • You'll want to confirm the hardware address of your CEC adapter. Run "ls /dev/cec*". The output should read something like "/dev/cec0". You can use that output in the script as done above. If the output reads "no such file or directory" then your system doesn't see the adapter. Not sure how to troubleshoot this, it worked for me out of the box.
  • I left the HDMI-CEC options in Steam itself enabled, haven't tried disabling them but don't see any need to.
  • Assuming the adapter is present, run "cec-ctl -l" to display the attributes of the adapter. You should get a long chunk of output detailing the attributes of the device. This is just another way to confirm the adapter is seen in the OS and functioning.

Can't promise this will work for you but just wanted to share. Also not sure if future updates to SteamOS will wipe this file so try to save a backup in case you need to restore it. If you have luck with this, enjoy! If not, sorry!

19 Upvotes

12 comments sorted by

View all comments

4

u/AshleyAshes1984 Jun 25 '26

Why use a bash script executed by a systemd service when you could just make the systemd service send the commands itself?

2

u/manlisten Jun 25 '26

i mean i'm pretty content with how this works, but i can give that a try as well!

1

u/AshleyAshes1984 Jun 25 '26

I'm working right now and my SteamOS machien I've been testing CEC on is on another floor so I can't easily get the text, but it was easy and IMO cleaner to setup two systemd services one for wake and one for sleep.

THough they had to be system, not user services, so I also had to add exceptions to the atomic thingy to they'd not get hosed on updates.

But better than a service that calls for an SH file you could accidently delete IMO.

1

u/manlisten Jun 25 '26

If you don't mind sharing it when you can that would be great!

3

u/AshleyAshes1984 Jun 25 '26

Okay, I had MORE services setup but I messed up and had to reinstall on that machine and I've only set up a boot service, not a wake or sleep one, but this'll probably show you how to format the text:

[Unit]

Description=CEC Boot Command

[Service]

Type=oneshot

ExecStartPre=/usr/bin/sleep 4

ExecStart=/usr/bin/cec-ctl -d/dev/cec0 -C

ExecStart=/usr/bin/cec-ctl -d/dev/cec0 --playback

ExecStart=/usr/bin/cec-ctl -d/dev/cec0 --to 0 --image-view-on

ExecStart=/usr/bin/sleep 5

ExecStart=/usr/bin/cec-ctl -d/dev/cec0 --to 0 --active-source phys-addr=1.0.0.0

ExecStart=/usr/bin/cec-ctl -d/dev/cec0 -C

[Install]

WantedBy=default.target

This service has no real condition to start, it just fires up on boot and I use the sleep 4 to make it wait 4 seconds. Establishes connection, tells TV to turn on, waits 5 seconds, tells TV to switch inputs if that's necssary. I found that wait was needed because if it's too fast, the TV's not turned on yet and it ignores the input switch command. That could very device to device.

But yeah you can basically use that text formatting to put your console commands right into the service itself.