diff options
Diffstat (limited to 'mga-bg-res/mga-bg-res.sh')
-rwxr-xr-x | mga-bg-res/mga-bg-res.sh | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/mga-bg-res/mga-bg-res.sh b/mga-bg-res/mga-bg-res.sh new file mode 100755 index 0000000..db937cb --- /dev/null +++ b/mga-bg-res/mga-bg-res.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +bgpath="/usr/share/mga/backgrounds" +theme="Mageia-Default" +curlink=$(readlink $bgpath/default.jpg) + +# If the following tests bail out, default to 4:3 aspect ratio (e.g. 1024x768) +res="1600x1200" + +# Search for the optimal background resolution according to the driver +DRIVER=$(grep -A 6 'Device' /etc/X11/xorg.conf | grep 'Driver') # => Driver "DriverName" +DRIVER=$(expr "$DRIVER" : '.*"\(.*\)"') # isolate driver's name +if [ ! -z "$DRIVER" -a -e /usr/sbin/monitor-probe ]; then + res=$(exec /usr/sbin/monitor-probe $DRIVER | grep -m 1 ModeLine) # => ModeLine "1920x1080" ... + res=$(expr "$res" : '.*"\(.*\)".*') # isolate the resolution +fi + +# Check if the symlink is already good +if [ "$curlink" = "$bgpath/$theme-$res.jpg" ]; then + exit 1 +fi + +# Check if this is a supported resolution, if not, find the background with a similar aspect ratio +if [ ! -e "$bgpath/$theme-$res.jpg" ]; then + width=$(echo $res | cut -f1 -d"x") + height=$(echo $res | cut -f2 -d"x") + # Bash only does integer arithmetic, we multiply everything by 1000 to workaround this + ratio=$((1000*$width/$height)) + declare -A refRatios=( ["1250"]="1280x1024" ["1333"]="1600x1200" ["1600"]="1920x1200" + ["1667"]="1280x768" ["1707"]="1024x600" ["1778"]="1920x1080" ) + for key in "${!refRatios[@]}"; do + if [ $(($ratio % $key)) -lt 25 -o $(($key % $ratio)) -lt 25 ]; then + res=${refRatios[$key]} + break + fi + done +fi + +# Check again if the symlink does not already point to this new resolution +# If not, create a new symlink +if [ "$curlink" != "$bgpath/$theme-$res.jpg" ]; then + if [ -e "$bgpath/$theme-$res.jpg" ]; then + ln -sf $bgpath/$theme-$res.jpg $bgpath/default.jpg + else + echo -e "Could not find this file: $bgpath/$theme-$res.jpg.\nPlease check that the mageia-theme-Default package is properly installed, or disable the mga-bg-res systemd service if you do not want to force using the Mageia theme." + fi +fi |