summaryrefslogtreecommitdiffstats
path: root/mga-bg-res/mga-bg-res.sh
blob: e73caa4d7f2797ae1234e859e257fd81fba0db2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash

bgpath="/usr/share/mga/backgrounds"
theme="Mageia-Default"
curlink=$(readlink $bgpath/default.jpg)

# 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

# If the previous method did not work, default to 4:3 aspect ratio (e.g. 1024x768)
if [ -z $res ]; then
    res="1600x1200"
fi

# Check if the symlink is already good
if [ "$curlink" = "$bgpath/$theme-$res.jpg" ]; then
    exit 0
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