Files
KDE-Display-Profiles/load-display-profile.sh
T
Dawson Matthews 6257e63357 working state
2026-03-08 18:13:21 -06:00

222 lines
6.8 KiB
Bash
Executable File

#!/usr/bin/env bash
VERBOSE=1
function log {
if [ $VERBOSE -eq 1 ]; then
echo "$@"
fi
}
set -e
# Make sure a config file was provided
if [ $# -lt 1 ]; then
echo "Usage: $0 <config file>"
exit
fi
PROFILE=$1
if [[ ! -f "$PROFILE" ]]; then
echo "Profile file not found: $PROFILE" >&2
exit 1
fi
if ! command -v jq &>/dev/null; then
echo "jq not installed!" >&2
exit 1
fi
if ! command -v kscreen-doctor &>/dev/null; then
echo "kscreen-doctor not found!" >&2
exit 1
fi
# Extract outputs list
enabled_outputs=$(jq -c '.outputs[] | select(.enabled == true)' "$PROFILE")
disabled_outputs=$(jq -c '.outputs[] | select(.enabled == false)' "$PROFILE")
outputs=$(jq -c '.outputs[]' "$PROFILE")
# Restore enabled/disabled starting with the enabled monitors
# Starting with a disabled monitor might not work if it was
# previously the only enabled monitor
function enable_outputs {
local outputs=$1
# If empty or only whitespace, return early
[[ -z "$outputs" ]] && return
while IFS= read -r out; do
id=$(echo "$out" | jq -r '.id')
log "[VAR] id:" $id
name=$(echo "$out" | jq -r '.name')
log "[VAR] name:" $name
enabled=$(echo "$out" | jq -r '.enabled')
log "[VAR] enabled:" $enabled
# Enable/disable
if [[ "$enabled" == "true" ]]; then
CMD="kscreen-doctor output.$name.enable"
log "[CMD]" $CMD
$CMD
else
CMD="kscreen-doctor output.$name.disable"
log "[CMD]" $CMD
$CMD
fi
done <<< "$outputs"
}
log "Enabling enabled outputs..."
enable_outputs "$enabled_outputs"
log "Disabling disabled outputs..."
enable_outputs "$disabled_outputs"
function apply_attribute {
output_id=$1
attribute=$2
value=$3
value_map=$4
if [ $value != "null" ]; then
if [[ -n "$value_map" ]]; then
log "[PREVALUE] Value supplied for $output_id $attribute: $value"
value=${value_map["$value"]}
log "[POSTVALUE] Value output for $output_id $attribute: $value"
fi
CMD="kscreen-doctor output.$output_id.$attribute.$value"
log "[CMD]" $CMD
$CMD
else
log "Output $output_id has not attribute $attribute, skipping..."
fi
}
function load_profile_to_outputs {
local outputs=$1
# If empty or only whitespace, return early
[[ -z "$outputs" ]] && return
while IFS= read -r out; do
id=$(echo "$out" | jq -r '.id')
name=$(echo "$out" | jq -r '.name')
posx=$(echo "$out" | jq -r '.pos.x')
posy=$(echo "$out" | jq -r '.pos.y')
rotation=$(echo "$out" | jq -r '.rotation')
scale=$(echo "$out" | jq -r '.scale')
mode_id=$(echo "$out" | jq -r '.currentModeId')
brightness=$(echo "$out" | jq -r '.brightness')
brightness=$(awk "BEGIN {printf \"%d\", $brightness * 100}")
ddcCi=$(echo "$out" | jq -r '.ddcCi')
iccProfilePath=$(echo "$out" | jq -r '.iccProfilePath')
# mode_name=$(echo "$out" | jq -r ".modes[] | select(.id == \"$mode_id\") | .name")
# log "[VAR] mode_name:" $mode_name
refresh_rate=$(echo "$out" | jq -r ".modes[] | select(.id == \"$mode_id\") | .refreshRate")
refresh_rate=$(printf "%.0f" "$refresh_rate")
height=$(echo "$out" | jq -r ".modes[] | select(.id == \"$mode_id\") | .size.height")
width=$(echo "$out" | jq -r ".modes[] | select(.id == \"$mode_id\") | .size.width")
mode="${width}x${height}@${refresh_rate}"
replication_source_id=$(echo "$out" | jq -r ".replicationSource")
hdr=$(echo "$out" | jq -r ".hdr")
maxBpc=$(echo "$out" | jq -r ".maxBpc")
overscan=$(echo "$out" | jq -r ".overscan")
rgbRange=$(echo "$out" | jq -r ".rgbRange")
sdrbrightness=$(echo "$out" | jq -r '."sdr-brightness"')
vrrPolicy=$(echo "$out" | jq -r ".vrrPolicy")
wcg=$(echo "$out" | jq -r ".wcg")
declare -A bool_enable_map
bool_enable_map["true"]="enable"
bool_enable_map["false"]="disable"
declare -A bool_allow_map
bool_allow_map["true"]="allow"
bool_allow_map["false"]="disallow"
declare -A rgb_range_map
rgb_range_map["0"]="automatic"
rgb_range_map["full"]="full"
rgb_range_map["limited"]="limited"
declare -A rotation_map
rotation_map["1"]="normal"
rotation_map["2"]="left"
rotation_map["4"]="inverted"
rotation_map["8"]="right"
apply_attribute $name "wcg" $wcg $bool_enable_map
apply_attribute $name "sdr-brightness" $sdrbrightness
apply_attribute $name "vrrpolicy" $vrrPolicy
apply_attribute $name "rgbrange" $rgbRange $rgb_range_map
apply_attribute $name "overscan" $overscan
apply_attribute $name "hdr" $hdr $bool_enable_map
apply_attribute $name "brightness" $brightness
if [ "$maxBpc" == 0 ]; then
maxBpc="automatic"
fi
apply_attribute $name "maxbpc" $maxBpc
apply_attribute $name "ddcCi" $ddcCi $bool_allow_map
log "[VAR] replication_source_id: $replication_source_id"
if [ $replication_source_id != 0 ]; then
replication_source_name=$(echo "$outputs.[] | select(.id == \"$replication_source_id\" | .name)" )
apply_attribute $name "mirror" $replication_source_name
else
apply_attribute $name "mirror" "none"
fi
priority=$(echo "$out" | jq -r '.priority')
if [ "$iccProfilePath" != "" ]; then
apply_attribute $name "iccProfilePath" $iccProfilePath
fi
apply_attribute $name "mode" $mode
# Position
# CMD="kscreen-doctor output.$name.position.$posx,$posy"
# log "[CMD]" $CMD
# $CMD
fullPosition="$posx,$posy"
apply_attribute $name "position" $fullPosition
apply_attribute $name "scale" $scale
# Rotation (map from JSON names to kscreen-doctor options)
# CMD=""
# case "$rotation" in
# "1") CMD="kscreen-doctor output.$name.rotation.normal" ;;
# "2") CMD="kscreen-doctor output.$name.rotation.left" ;;
# "4") CMD="kscreen-doctor output.$name.rotation.inverted" ;;
# "8") CMD="kscreen-doctor output.$name.rotation.right" ;;
# esac
# log "[CMD]" $CMD
# $CMD
apply_attribute $name "rotation" $rotation $rotation_map
# Primary / Not Primary
if [ $priority -eq 1 ]; then
CMD="kscreen-doctor output.$name.primary"
log "[CMD]" $CMD
$CMD
fi
CMD="kscreen-doctor output.$name.priority.$priority"
log "[CMD]" $CMD
$CMD
done <<< "$outputs"
}
load_profile_to_outputs "$outputs"
echo "Display configuration restored."