addresolution 1.63 KB
Newer Older
1
2
3
#!/bin/bash

showExample() { 
4
    echo -e "\ne.g. \""$(basename -- "$0")" eDP1 1920 1080 60\"\n"
5
6
7
8
9
10
11
}

createRes() {
    cvt "$WIDTH" "$HEIGHT" "$REFRESH" | grep Modeline | sed "s/Modeline//" | xargs xrandr --newmode
}

addRes() {
12
13
14
15
    xrandr --addmode "$DEVICE" "$WIDTH"x"$HEIGHT"_"$REFRESH".00
}

switchRes() {
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

    while true; do
        read -p "$1" yn
        case $yn in
            [Yy]* ) xrandr --output "$DEVICE" --mode "$WIDTH"x"$HEIGHT"_"$REFRESH".00 || {
                        echo -e "\nERROR switching resolution with \"xrandr\"\n"
                        return 1
                    }; break;;
            [Nn]* ) break;;
            * ) echo "Please type \"y\" or \"n\"";;
        esac
    done
}

switchResAnyway() {
    switchRes "Adding resolution failed. Try to switch to this resolution anyway? (y/n) "
}

switchResNow() {
    switchRes "Switch to this resolution now? (y/n) "
36
37
38
39
}

# check arguments
if [ -z "$1" ]; then
40
    echo "Please provide an output devide as the first argument"
41
42
43
44
45
    showExample
    exit 128
fi

if [ -z "$2" ]; then
46
    echo "Please provide a screen width as the second argument"
47
48
49
50
51
    showExample
    exit 128
fi

if [ -z "$3" ]; then
52
53
54
55
56
57
58
    echo "Please provide a screen height as the third argument"
    showExample
    exit 128
fi

if [ -z "$4" ]; then
    echo "Please provide a refresh rate as the fourth argument"
59
60
61
62
63
    showExample
    exit 128
fi

#init
64
65
66
67
DEVICE=$1
WIDTH=$2
HEIGHT=$3
REFRESH=$4
68
69
70

createRes || {
    echo -e "\nERROR creating resolution with \"cvt\"\n"
71
    switchResAnyway
72
73
74
75
76
    exit 1
}

addRes || {
    echo -e "\nERROR adding resolution with \"xrandr\"\n"
77
    switchResAnyway
78
79
80
    exit 1
}

81
switchResNow
82
exit 0