Some Bits: Nelson's log

Linux gaming: switching video and sound

I use my Linux box with a long cable to play games on my TV. When I plug in the TV I want to change resolutions and change sound output devices for the TV. (The display is mirrored from my desktop). This is the kind of thing I’d only do manually in Windows but in Linux it’s reasonable to automate it. Particularly with an AI doing the hard work.

The switch script

Switching boils down to using kscreen-doctor to set the video mode and pactl to change the audio device. I’m using this script manually and it works great.

#!/bin/bash
# Description: Toggle display resolution and audio output between Desktop and TV modes.
# Safe to run multiple times consecutively.

# ================================
# Configuration Variables
# ================================
DISPLAY_NAME="DP-3"

# Desktop Settings
DESKTOP_RES="6016x3384@60"
DESKTOP_CARD="alsa_card.pci-0000_0b_00.4"
DESKTOP_PROFILE="output:analog-stereo"
DESKTOP_SINK="alsa_output.pci-0000_0b_00.4.analog-stereo"

# TV Settings
TV_RES="3840x2160@60"
TV_CARD="alsa_card.pci-0000_09_00.1"
TV_PROFILE="output:hdmi-surround-extra1"
TV_SINK="alsa_output.pci-0000_09_00.1.hdmi-surround-extra1"

# ================================
# Helper Functions
# ================================
switch_audio() {
    local card="$1"
    local profile="$2"
    local sink="$3"

    # 1. Set Card Profile
    pactl set-card-profile "$card" "$profile" 2>/dev/null

    # 2. Set Default Sink
    pactl set-default-sink "$sink" 2>/dev/null

    # 3. Move active audio streams safely
    pactl list short sink-inputs | awk '{print $1}' | while read -r stream; do
        if [ -n "$stream" ]; then
            pactl move-sink-input "$stream" "$sink" 2>/dev/null
        fi
    done
}

# ================================
# Main Execution
# ================================
MODE="${1:-desktop}"

if [ "$MODE" = "tv" ]; then
    echo "📺 Switching to TV Mode..."
    
    # Switch Display to 2160p
    kscreen-doctor "output.${DISPLAY_NAME}.mode.${TV_RES}"
    
    # Switch Audio to NVIDIA 5.1 Surround
    switch_audio "$TV_CARD" "$TV_PROFILE" "$TV_SINK"

else
    echo "🖥️  Switching to Desktop Mode..."
    
    # Switch Display to 6K
    kscreen-doctor "output.${DISPLAY_NAME}.mode.${DESKTOP_RES}"
    
    # Switch Audio to Starship/Matisse Stereo
    switch_audio "$DESKTOP_CARD" "$DESKTOP_PROFILE" "$DESKTOP_SINK"

fi

echo "Done!"

Switching automatically

I haven’t tried this yet, but Gemini has a reasonable plan for making this happen automatically when I plug in the cable. It boils down to running a user systemd service to run udevadm monitor to get notified when the cable is plugged in.

#!/bin/bash
# Description: Auto-detects HDMI hotplug events and calls switch_mode.sh

# Path to your switch script (adjust if located elsewhere)
SWITCH_SCRIPT="$HOME/bin/switch_mode.sh"

# Automatically find the first HDMI connector status path
HDMI_STATUS_FILE=$(ls /sys/class/drm/card*-HDMI-*/status 2>/dev/null | head -n 1)

if [ -z "$HDMI_STATUS_FILE" ]; then
    echo "Error: No HDMI port found in /sys/class/drm/"
    exit 1
fi

echo "Monitoring HDMI port: $HDMI_STATUS_FILE"

check_and_switch() {
    STATUS=$(cat "$HDMI_STATUS_FILE" 2>/dev/null)
    if [ "$STATUS" = "connected" ]; then
        echo "HDMI Connected -> Switching to TV"
        "$SWITCH_SCRIPT" tv
    else
        echo "HDMI Disconnected -> Switching to Desktop"
        "$SWITCH_SCRIPT" desktop
    fi
}

# 1. Run check immediately when the script starts
check_and_switch

# 2. Listen for DRM kernel display events in real time (no root required)
udevadm monitor --subsystem-match=drm | while read -r line; do
    if echo "$line" | grep -q "change"; then
        # Brief pause to let the hardware settle
        sleep 1
        check_and_switch
    fi
done
[Unit]
Description=HDMI Hotplug Display and Audio Switcher
After=graphical-session.target

[Service]
Type=simple
ExecStart=%h/bin/tv_auto_switch.sh
Restart=always
RestartSec=3

[Install]
WantedBy=graphical-session.target

More switching

I didn’t actually set it up but I’m pretty sure udevadm monitor will work as a way to know when an HDMI cable is plugged in to activate the switch. But I realized I’d rather leave the thing plugged in all the time and only switch modes on my Linux box when the TV receiver switches to its video input. I tried for awhile and couldn’t make it work. An AMD video card might.

Some TVs make it look like you basically disconnected the cable when you switch away from that input. They turn off the HDMI hot plug signal and stop sending EDID. My TV does not do this, on the Linux side it looks like it’s always on.

The TV should be sending HDMI-CEC commands down the wire to notify the device when it’s selected as input. In Linux those should be available on /dev/cec0 or the like. Unfortunately the NVidia graphics drivers just don’t support this. Intel and AMD supposedly do. Yet another way NVidia’s Linux support falls short.

I finally just put a button to invoke my script on the KDE taskbar so it’s very easy to switch modes.

Yay for AI

I have the skills to figure scripts out like this myself. It would take me ~2 hours to research and write and debug this script. Or it takes me 10 minutes with an AI. I never would have bothered without. My expertise is still important: I’m asking the questions of the AI the right way and I can review the suggestions to see if they make sense. But the AI is definitely great at this kind of one-off hack.