Files
hdr-helper/hdr-helper
Dunestorm 8a43b3187d - Validate Wayland session.
- Supress verbosity of kscreen-doctor.
2025-09-26 21:49:43 +01:00

166 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
VERSION=1.3.0.0
CONFIG_PATH="$HOME/.config/hdr-helper/"
CONFIG_FILE="$CONFIG_PATH/monitor.conf"
CURR_BRIGHT_LVL_PATH="/tmp/hdr-helper/"
CURR_BRIGHT_LVL_FILE="$CURR_BRIGHT_LVL_PATH/current-brightness-level"
show_hdr_status() {
ICON="/usr/share/icons/breeze-dark/devices/64/monitor.svg"
if [[ $(kscreen-doctor -o | grep -A 15 $MONITOR | grep HDR) == *"enabled"* ]]; then
notify-send -a "HDR Helper" -i $ICON "HDR Enabled"
else
notify-send -a "HDR Helper" -i $ICON "HDR Disabled"
fi
}
set_brightness() {
# Store the current brightness level before attempting to update it.
if [ ! -f $CURR_BRIGHT_LVL_PATH ]; then
mkdir $CURR_BRIGHT_LVL_PATH
fi
kscreen-doctor -o | \
grep Brightness | \
head -n 1 | \
grep -oP "(?<=set to )[0-9]+" \
> $CURR_BRIGHT_LVL_FILE
if [ -v SDR_TARGET_BRIGHTNESS ]; then
kscreen-doctor output.$MONITOR.brightness.$SDR_TARGET_BRIGHTNESS
fi
if [ -v SDR_TARGET_BRIGHTNESS_NITS ]; then
kscreen-doctor output.$MONITOR.sdr-brightness.$SDR_TARGET_BRIGHTNESS_NITS
fi
}
restore_brightness() {
if [ -f $CURR_BRIGHT_LVL_FILE ]; then
kscreen-doctor output.$MONITOR.brightness.$(cat $CURR_BRIGHT_LVL_FILE)
rm $CURR_BRIGHT_LVL_FILE
fi
}
refresh_display() {
kscreen-doctor output.$MONITOR.mode.$TEMP_RES > /dev/null 2>&1
kscreen-doctor output.$MONITOR.mode.$NATIVE_RES > /dev/null 2>&1
show_hdr_status
}
enable_hdr () {
kscreen-doctor output.$MONITOR.hdr.enable > /dev/null 2>&1
kscreen-doctor output.$MONITOR.wcg.enable > /dev/null 2>&1
refresh_display
set_brightness
}
disable_hdr() {
kscreen-doctor output.$MONITOR.hdr.disable > /dev/null 2>&1
kscreen-doctor output.$MONITOR.wcg.disable > /dev/null 2>&1
refresh_display
restore_brightness
}
test_hdr_support() {
if [[ $XDG_SESSION_TYPE != "wayland" ]]; then
echo "⚠️ Warning: You are not running Wayland. HDR is not supported on X11."
exit -5
fi
HDR_CONTEXT=$(kscreen-doctor -o | grep -A 15 $MONITOR | grep "HDR" | awk '{ print $3 }')
if [[ $HDR_CONTEXT == *"incapable"* ]]; then
echo "❌ HDR capabilities are either disabled or not supported on your current display."
echo ""
echo "Please check the correct display is configured in monitor.conf and that"
echo "HDR is both supported and enabled."
elif [[ $HDR_CONTEXT == *"enabled"* || *"disabled"* ]]; then
echo "✅ HDR capabilities are supported on your current display."
else
echo "ERROR: Unhandled HDR status"
exit -4
fi
}
auto_hdr() {
if [[ $(kscreen-doctor -o | grep -A 15 $MONITOR | grep HDR) == *"enabled"* ]]; then
disable_hdr
else
enable_hdr
fi
}
show_version() {
echo "HDR Helper v"$VERSION
}
show_help () {
echo "=== Usage ==="
echo "The default behaviour will automatically enable or disable HDR"
echo " hdr-helper (no args)"
echo "Force enable HDR"
echo " hdr-helper -e (--enable)"
echo "Force disable HDR"
echo " hdr-helper -d (--disable)"
echo "Show detected HDR status as a system notification"
echo " hdr-helper -s (--status)"
echo "Display version information"
echo " hdr-helper -v (--version)"
echo "Display this help screen"
echo " hdr-helper -h (--help)"
echo "Tests whether the connected display is HDR capable"
echo " hdr-helper -t (--test)"
echo ""
echo "=== Info ==="
echo "Currently only KDE Plasma is supported running Wayland. Ensure that kscreen-doctor"
echo "is present on your system for HDR Helper to function."
echo ""
echo "=== Config ==="
echo "Copy monitor.conf to ~/.config/hdr-helper and ensure to modify this file with your"
echo "desired settings."
echo ""
echo "Please refer to README.md or https://github.com/dunestorm333/hdr-helper for further"
echo "setup instructions."
}
# Check Dependencies
if [[ $(whereis kscreen-doctor) != *"kscreen-doctor" ]]; then
echo "Error: kscreen-doctor cannot be detected, aborting."
echo "Please note that only KDE Plasma is currently supported on Wayland."
echo ""
exit -1
fi
# Import monitor.conf values
if source "$CONFIG_FILE"; then
if [[ $MONITOR == "" ]] || [[ $NATIVE_RES == "" ]] || [[ $TEMP_RES == "" ]]; then
echo "Error: monitor.conf has not been configured."
exit -2
fi
else
echo "Error: monitor.conf file not found. Please ensure it's been properly setup."
exit -3
fi
# Handle Args
if [[ $1 == "-e" ]] || [[ $1 == "--enable" ]]; then
enable_hdr
elif [[ $1 == "-d" ]] || [[ $1 == "--disable" ]]; then
disable_hdr
elif [[ $1 == "-s" ]] || [[ $1 == "--status" ]]; then
show_hdr_status
elif [[ $1 == "-v" ]] || [[ $1 == "--version" ]]; then
show_version
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
show_help
elif [[ $1 == "-t" ]] || [[ $1 == "--test" ]]; then
test_hdr_support
elif [[ $# -eq 0 ]]; then # Automatically Toggle HDR | Default Behaviour
auto_hdr
else
show_help
fi