OSDN Git Service

better internet check
[instantos/instantOS.git] / monitor.sh
1 #!/bin/bash
2
3 #####################################################
4 # This script detects and saves screen resolutions ##
5 # It resizes the wallpaper if things change        ##
6 #####################################################
7
8 command -v xrandr &>/dev/null ||
9     (echo "please install xrandr" && exit 1)
10
11 cd
12
13 mkdir -p ~/instantos/monitor &>/dev/null
14 cd ~/instantos/monitor
15
16 POSITIONS="$(xrandr | grep '[^s]connected' | grep -o '[0-9]*x[0-9]*+[0-9]*' | grep -o '[0-9]*$')"
17 AMOUNT=$(wc -l <<<"$POSITIONS")
18
19 RESOLUTIONS=$(xrandr | grep '[^s]connected' | grep -Eo '[0-9]{1,}x[0-9]{1,}\+[0-9]{1,}\+[0-9]{1,}' |
20     grep -o '[0-9]*x[0-9]*' | sed 's/ /\n/g')
21
22 # check if resolutions are already configured
23 # exit if no changes
24 if iconf resolutions; then
25     OLDRES="$(iconf resolutions)"
26     # see if resolution has changed
27     if ! [ "$RESOLUTIONS" = "$OLDRES" ]; then
28         iconf resolutions "$RESOLUTIONS"
29         echo "Resolution change detected"
30     else
31         echo "no resolution change"
32         [ "$1" = "-f" ] || exit
33     fi
34 else
35     iconf resolutions "$RESOLUTIONS"
36 fi
37
38 if [ $(echo "$RESOLUTIONS" | sort -u | wc -l) = "1" ]; then
39     echo "resolutions identical"
40     iconf max $(head -1 <<<"$RESOLUTIONS")
41 else
42     # get monitor with highest resolution
43     let PIXELS1="$(head -1 <<<$RESOLUTIONS | grep -o '^[0-9]*') * $(head -1 <<<$RESOLUTIONS | grep -o '[0-9]*$')"
44     let PIXELS2="$(tail -1 <<<$RESOLUTIONS | grep -o '^[0-9]*') * $(tail -1 <<<$RESOLUTIONS | grep -o '[0-9]*$')"
45     if [ "$PIXELS1" -gt "$PIXELS2" ]; then
46         iconf max "$(head -1 <<<$RESOLUTIONS)"
47     else
48         iconf max "$(tail -1 <<<$RESOLUTIONS)"
49     fi
50 fi
51
52 # rebuild wallpaper after resolution change
53
54 changetrigger() {
55     rm ~/instantos/wallpapers/overlay.*
56     rm -rf ~/instantos/wallpapers/default
57
58     if iconf -i setwallpaper; then
59         instantwallpaper resolution
60     fi
61 }
62
63 if [ "$AMOUNT" = "1" ]; then
64     echo "only one monitor found, further setup not needed"
65     changetrigger
66     exit
67 else
68     if [ "$AMOUNT" -gt 2 ]; then
69         echo "only 2 monitors are testes"
70         exit
71     fi
72     echo "$AMOUNT monitors found"
73 fi
74
75 iconf names "$(xrandr | grep '[^s]connected' | grep -o '^[^ ]*')"
76
77 MONITOR1=$(head -1 <<<"$POSITIONS")
78 MONITOR2=$(tail -1 <<<"$POSITIONS")
79
80 # legacy leftover
81 # in case some program ever needs it
82 if [ "$MONITOR1" -gt "$MONITOR2" ]; then
83     echo "Monitor 1 is ${MONITOR1}px on the right"
84     iconf right "$MONITOR2"
85 else
86     echo "Monitor 2 is ${MONITOR2}px on the right"
87     iconf right "$MONITOR2"
88 fi
89
90 changetrigger