OSDN Git Service

change theme on runtime
[instantos/instantOS.git] / monitor.sh
1 #!/bin/bash
2
3 command -v xrandr &>/dev/null ||
4     (echo "please install xrandr" && exit 1)
5 cd
6
7 mkdir -p ~/instantos/monitor &>/dev/null
8 cd ~/instantos/monitor
9
10 POSITIONS="$(xrandr | grep '[^s]connected' | grep -o '[0-9]*x[0-9]*+[0-9]*' | grep -o '[0-9]*$')"
11 AMOUNT=$(wc -l <<<"$POSITIONS")
12
13 # get monitor with highest resolution
14 RESOLUTIONS=$(xrandr | grep '[^s]connected' | grep -Eo '[0-9]{1,}x[0-9]{1,}\+[0-9]{1,}\+[0-9]{1,}' |
15     grep -o '[0-9]*x[0-9]*' | sed 's/ /\n/g')
16
17 if OLDRES="$(iconf resolutions)"; then
18     # see if resolution has changed
19     if ! [ "$RESOLUTIONS" = "$OLDRES" ]; then
20         iconf resolutions "$RESOLUTIONS"
21         CHANGERES="True"
22         echo "Resolution change detected"
23     fi
24 fi
25
26 if [ $(echo "$RESOLUTIONS" | sort -u | wc -l) = "1" ]; then
27     echo "resolutions identical"
28     iconf -i max $(head -1 <<<"$RESOLUTIONS")
29 else
30     let PIXELS1="$(head -1 <<<$RESOLUTIONS | grep -o '^[0-9]*') * $(head -1 <<<$RESOLUTIONS | grep -o '[0-9]*$')"
31     let PIXELS2="$(tail -1 <<<$RESOLUTIONS | grep -o '^[0-9]*') * $(tail -1 <<<$RESOLUTIONS | grep -o '[0-9]*$')"
32     if [ "$PIXELS1" -gt "$PIXELS2" ]; then
33         iconf -i max "$(head -1 <<<$RESOLUTIONS)"
34     else
35         iconf -i max "$(tail -1 <<<$RESOLUTIONS)"
36     fi
37 fi
38
39 # rebuild wallpaper after resolution change
40 changetrigger() {
41     if [ -z "$CHANGERES" ]; then
42         echo "no resolution change"
43     else
44         if [ -e ~/instantos/wallpapers ] && command -v instantwallpaper; then
45             rm -rf ~/instantos/wallpapers
46             instantwallpaper w
47         fi
48     fi
49 }
50
51 if [ "$AMOUNT" = "1" ]; then
52     echo "only one monitor found, further setup not needed"
53     changetrigger
54     exit
55 else
56     if [ "$AMOUNT" -gt 2 ]; then
57         echo "only 2 monitors are supported"
58         exit
59     fi
60     echo "$AMOUNT monitors found"
61 fi
62
63 iconf names "$(xrandr | grep '[^s]connected' | grep -o '^[^ ]*')"
64
65 MONITOR1=$(head -1 <<<"$POSITIONS")
66 MONITOR2=$(tail -1 <<<"$POSITIONS")
67
68 if [ "$MONITOR1" -gt "$MONITOR2" ]; then
69     echo "Monitor 1 is ${MONITOR1}px on the right"
70     iconf -i right "$MONITOR2"
71 else
72     echo "Monitor 2 is ${MONITOR2}px on the right"
73     iconf -i right "$MONITOR2"
74 fi
75 changetrigger