Compare commits

8 Commits

3 changed files with 191 additions and 55 deletions
Vendored
+1
View File
@@ -0,0 +1 @@
monitor-setup.log
+61 -47
View File
@@ -9,7 +9,7 @@ CURR_BRIGHT_LVL_FILE="$CURR_BRIGHT_LVL_PATH/current-brightness-level"
show_hdr_status() { show_hdr_status() {
ICON="/usr/share/icons/breeze-dark/devices/64/monitor.svg" ICON="/usr/share/icons/breeze-dark/devices/64/monitor.svg"
if [[ $(kscreen-doctor -o | grep -A 15 $MONITOR | grep HDR) == *"enabled"* ]]; then if [[ $(kscreen-doctor -o | grep -A 15 "$MONITOR" | grep HDR) == *"enabled"* ]]; then
notify-send -a "HDR Helper" -i $ICON "HDR Enabled" notify-send -a "HDR Helper" -i $ICON "HDR Enabled"
else else
notify-send -a "HDR Helper" -i $ICON "HDR Disabled" notify-send -a "HDR Helper" -i $ICON "HDR Disabled"
@@ -22,70 +22,74 @@ set_brightness() {
mkdir $CURR_BRIGHT_LVL_PATH mkdir $CURR_BRIGHT_LVL_PATH
fi fi
kscreen-doctor -o | \ kscreen-doctor -o |
grep Brightness | \ grep Brightness |
head -n 1 | \ head -n 1 |
grep -oP "(?<=set to )[0-9]+" \ grep -oP "(?<=set to )[0-9]+" \
> $CURR_BRIGHT_LVL_FILE >"$CURR_BRIGHT_LVL_FILE"
if [ -v SDR_TARGET_BRIGHTNESS ]; then if [ -v SDR_TARGET_BRIGHTNESS ]; then
kscreen-doctor output.$MONITOR.brightness.$SDR_TARGET_BRIGHTNESS kscreen-doctor output."$MONITOR".brightness."$SDR_TARGET_BRIGHTNESS"
fi fi
if [ -v SDR_TARGET_BRIGHTNESS_NITS ]; then if [ -v SDR_TARGET_BRIGHTNESS_NITS ]; then
kscreen-doctor output.$MONITOR.sdr-brightness.$SDR_TARGET_BRIGHTNESS_NITS kscreen-doctor output."$MONITOR".sdr-brightness."$SDR_TARGET_BRIGHTNESS_NITS"
fi fi
} }
restore_brightness() { restore_brightness() {
if [ -f $CURR_BRIGHT_LVL_FILE ]; then if [ -f "$CURR_BRIGHT_LVL_FILE" ]; then
kscreen-doctor output.$MONITOR.brightness.$(cat $CURR_BRIGHT_LVL_FILE) kscreen-doctor output."$MONITOR".brightness."$(cat "$CURR_BRIGHT_LVL_FILE")"
rm $CURR_BRIGHT_LVL_FILE rm "$CURR_BRIGHT_LVL_FILE"
fi fi
} }
refresh_display() { refresh_display() {
kscreen-doctor output.$MONITOR.mode.$TEMP_RES > /dev/null 2>&1 kscreen-doctor output."$MONITOR".mode."$TEMP_RES" >/dev/null 2>&1
kscreen-doctor output.$MONITOR.mode.$NATIVE_RES > /dev/null 2>&1 kscreen-doctor output."$MONITOR".mode."$NATIVE_RES" >/dev/null 2>&1
show_hdr_status show_hdr_status
} }
enable_hdr () { enable_hdr() {
kscreen-doctor output.$MONITOR.hdr.enable > /dev/null 2>&1 kscreen-doctor output."$MONITOR".hdr.enable >/dev/null 2>&1
kscreen-doctor output.$MONITOR.wcg.enable > /dev/null 2>&1 kscreen-doctor output."$MONITOR".wcg.enable >/dev/null 2>&1
refresh_display refresh_display
set_brightness set_brightness
} }
disable_hdr() { disable_hdr() {
kscreen-doctor output.$MONITOR.hdr.disable > /dev/null 2>&1 kscreen-doctor output."$MONITOR".hdr.disable >/dev/null 2>&1
kscreen-doctor output.$MONITOR.wcg.disable > /dev/null 2>&1 kscreen-doctor output."$MONITOR".wcg.disable >/dev/null 2>&1
refresh_display refresh_display
restore_brightness restore_brightness
} }
test_hdr_support() { test_hdr_support() {
if [[ $XDG_SESSION_TYPE != "wayland" ]]; then if [[ $XDG_SESSION_TYPE != "wayland" ]]; then
echo "⚠️ Warning: You are not running Wayland. HDR is not supported on X11." echo "⚠️ Warning: You are not running Wayland. HDR is not supported on X11."
exit -5 exit 5
fi fi
HDR_CONTEXT=$(kscreen-doctor -o | grep -A 15 $MONITOR | grep "HDR" | awk '{ print $3 }') HDR_CONTEXT=$(kscreen-doctor -o | grep -A 15 "$MONITOR" | grep "HDR" | awk '{ print $3 }')
if [[ $HDR_CONTEXT == *"incapable"* ]]; then if [[ $HDR_CONTEXT == *"incapable"* ]]; then
echo "❌ HDR capabilities are either disabled or not supported on your current display." echo "❌ HDR capabilities are either disabled or not supported on your current display."
echo "" echo ""
echo "Please check the correct display is configured in monitor.conf and that" echo "Please check the correct display is configured in monitor.conf and that"
echo "HDR is both supported and enabled." echo "HDR is both supported and enabled."
elif [[ $HDR_CONTEXT == *"enabled"* || *"disabled"* ]]; then elif [[ $HDR_CONTEXT == *"abled"* ]]; then
echo "✅ HDR capabilities are supported on your current display." echo "✅ HDR capabilities are supported on your current display."
else else
echo "ERROR: Unhandled HDR status" echo "ERROR: Unhandled HDR status"
exit -4 exit 4
fi fi
}
setup_conf_file() {
python monitor-setup.py
} }
auto_hdr() { auto_hdr() {
if [[ $(kscreen-doctor -o | grep -A 15 $MONITOR | grep HDR) == *"enabled"* ]]; then if [[ $(kscreen-doctor -o | grep -A 15 "$MONITOR" | grep HDR) == *"enabled"* ]]; then
disable_hdr disable_hdr
else else
enable_hdr enable_hdr
@@ -95,8 +99,7 @@ auto_hdr() {
show_version() { show_version() {
echo "HDR Helper v"$VERSION echo "HDR Helper v"$VERSION
} }
show_help() {
show_help () {
echo "=== Usage ===" echo "=== Usage ==="
echo "The default behaviour will automatically enable or disable HDR" echo "The default behaviour will automatically enable or disable HDR"
echo " hdr-helper (no args)" echo " hdr-helper (no args)"
@@ -112,6 +115,8 @@ show_help () {
echo " hdr-helper -h (--help)" echo " hdr-helper -h (--help)"
echo "Tests whether the connected display is HDR capable" echo "Tests whether the connected display is HDR capable"
echo " hdr-helper -t (--test)" echo " hdr-helper -t (--test)"
echo "Automate monitor.conf file setup"
echo " hdr-helper -a (--auto-setup-conf-file)"
echo "" echo ""
echo "=== Info ===" echo "=== Info ==="
echo "Currently only KDE Plasma is supported running Wayland. Ensure that kscreen-doctor" echo "Currently only KDE Plasma is supported running Wayland. Ensure that kscreen-doctor"
@@ -130,27 +135,33 @@ if [[ $(whereis kscreen-doctor) != *"kscreen-doctor" ]]; then
echo "Error: kscreen-doctor cannot be detected, aborting." echo "Error: kscreen-doctor cannot be detected, aborting."
echo "Please note that only KDE Plasma is currently supported on Wayland." echo "Please note that only KDE Plasma is currently supported on Wayland."
echo "" echo ""
exit -1 exit 1
fi fi
import_config() {
# Import monitor.conf values if [[ -f "$CONFIG_FILE" ]]; then
if source "$CONFIG_FILE"; then # shellcheck source=monitor.conf
if [[ $MONITOR == "" ]] || [[ $NATIVE_RES == "" ]] || [[ $TEMP_RES == "" ]]; then source "$CONFIG_FILE"
echo "Error: monitor.conf has not been configured." if [[ $MONITOR == "" ]] || [[ $NATIVE_RES == "" ]] || [[ $TEMP_RES == "" ]]; then
exit -2 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."
echo "Try running 'hdr-helper -a' to try automatically generating one."
exit 3
fi fi
else }
echo "Error: monitor.conf file not found. Please ensure it's been properly setup."
exit -3
fi
# Handle Args # Handle Args
if [[ $1 == "-e" ]] || [[ $1 == "--enable" ]]; then if [[ $1 == "-e" ]] || [[ $1 == "--enable" ]]; then
import_config
enable_hdr enable_hdr
elif [[ $1 == "-d" ]] || [[ $1 == "--disable" ]]; then elif [[ $1 == "-d" ]] || [[ $1 == "--disable" ]]; then
import_config
disable_hdr disable_hdr
elif [[ $1 == "-s" ]] || [[ $1 == "--status" ]]; then elif [[ $1 == "-s" ]] || [[ $1 == "--status" ]]; then
import_config
show_hdr_status show_hdr_status
elif [[ $1 == "-v" ]] || [[ $1 == "--version" ]]; then elif [[ $1 == "-v" ]] || [[ $1 == "--version" ]]; then
show_version show_version
@@ -158,7 +169,10 @@ elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
show_help show_help
elif [[ $1 == "-t" ]] || [[ $1 == "--test" ]]; then elif [[ $1 == "-t" ]] || [[ $1 == "--test" ]]; then
test_hdr_support test_hdr_support
elif [[ $1 == "-a" ]] || [[ $1 == "--auto-setup-conf-file" ]]; then
setup_conf_file
elif [[ $# -eq 0 ]]; then # Automatically Toggle HDR | Default Behaviour elif [[ $# -eq 0 ]]; then # Automatically Toggle HDR | Default Behaviour
import_config
auto_hdr auto_hdr
else else
show_help show_help
+121
View File
@@ -0,0 +1,121 @@
import re
import subprocess
import os
import shutil
class MonitorSetup():
def __init__(self):
self._stg_conf_file = "monitor.conf"
self._conf_file_dir = os.path.join(os.path.expanduser('~'),
".config/hdr-helper")
self._conf_file_path = os.path.join(self._conf_file_dir,
self._stg_conf_file)
################################
self.default_mode = None
self.alt_rate = None
self.alt_res_and_rate = None
self.output_name = None
def extract_display_info(self):
# Run process, capture output as a string
proc = subprocess.run(
["kscreen-doctor -o"],
shell=True,
capture_output=True,
text=True
)
output = proc.stdout
# Remove ANSI escape codes for clean regex matching
output_clean = re.sub(r'\x1b\[[0-9;]*m', '', output)
# Find all resolutions with flags
resolutions = re.findall(r'\d+x\d+@\d+\S', output_clean)
# Identify default display mode and alternatives
for i in resolutions:
if i.endswith('*'):
self.default_mode = i
if self.default_mode and i != self.default_mode:
if i.startswith(self.default_mode.split('@')[0]):
self.alt_rate = i
break
else:
self.alt_res_and_rate = i
# Remove asterisk from default
if self.default_mode:
self.default_mode = self.default_mode.strip('*')
search_output = re.search(r'Output:\s*\d+\s+(\S+)', output_clean)
# Extract and print output name
if search_output:
self.output_name = search_output.group(1)
def log_display_info(self):
_log_file = "monitor-setup.log"
with open(_log_file, "w") as file:
file.write(f"Output Name: {self.output_name}\n")
file.write(f"Default Mode: {self.default_mode}\n")
file.write(f"Alternative Rate: {self.alt_rate}\n")
file.write(f"Alternative Resolution and Rate: {self.alt_res_and_rate}")
if os.path.isfile(_log_file):
print(f"Log file was written to {os.path.join(os.getcwd(),_log_file)}")
def edit_config_file(self):
_validation_check_cnt = 0
if os.path.isfile(self._stg_conf_file):
with open(self._stg_conf_file, "r") as file:
lines = file.readlines()
with open(self._stg_conf_file, "w") as file:
for line in lines:
if "MONITOR=" in line:
line = line.replace("MONITOR=\n", f"MONITOR={self.output_name}\n")
_validation_check_cnt += 1
elif "NATIVE_RES=" in line:
line = line.replace("NATIVE_RES=\n", f"NATIVE_RES={self.default_mode}\n")
_validation_check_cnt += 1
# Prefer alt_rate if available, otherwise alt_res_and_rate
if "TEMP_RES=" in line:
if self.alt_rate:
line = line.replace("TEMP_RES=\n", f"TEMP_RES={self.alt_rate}\n")
_validation_check_cnt += 1
elif self.alt_res_and_rate:
line = line.replace("TEMP_RES=\n", f"TEMP_RES={self.alt_res_and_rate}\n")
_validation_check_cnt += 1
file.write(line)
if _validation_check_cnt != 3:
print("❌ Automation configuration failed, one or more settings could not be detected.")
self.log_display_info()
exit(1)
if not os.path.isfile(self._stg_conf_file):
print("❌ Error creating configuration file.")
def copy_config_file(self):
if not os.path.exists(self._conf_file_dir):
os.makedirs(self._conf_file_dir)
# Backup existing config file if it exists
if os.path.isfile(self._conf_file_path):
shutil.move(self._conf_file_path, self._conf_file_path + ".backup" )
# Copy staging config file to target location
if os.path.isfile(self._stg_conf_file):
shutil.copy(self._stg_conf_file, self._conf_file_path)
print(f"✅ Configuration file was copied to location: {self._conf_file_path}")
setup = MonitorSetup()
setup.extract_display_info()
setup.edit_config_file()
setup.copy_config_file()