refreshVP.sh 3.76 KB
Newer Older
David Beniamine's avatar
David Beniamine committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/bin/bash

# Copyright (C) 2015  Beniamine, David <David.Beniamine@tetras-libre.fr>
# Author: Beniamine, David <David.Beniamine@tetras-libre.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Get rights to access display
David Beniamine's avatar
Fix #67  
David Beniamine committed
20 21 22 23 24 25 26 27
DEBUG=false
log(){
    if $DEBUG
    then
        echo "$@" >> /var/log/vp.log
    fi
}
log  "starting VP $(date)"
David Beniamine's avatar
David Beniamine committed
28 29 30 31 32 33 34
user=$(who | grep -v "root" | head -n 1 | cut -d ' ' -f 1)
if [ ! -z "$user" ]
then
	export DISPLAY=:0
	export XAUTHORITY="/home/$user/.Xauthority"
fi

David Beniamine's avatar
David Beniamine committed
35 36
DIR=$(realpath $(dirname $0))

37 38 39 40 41 42 43 44
# show max between two res
max_res(){
    [ -z "$2" ] && echo $1 && return
    first=$(echo $1 | sed 's/x/*/' | bc)
    second=$(echo $2 | sed 's/x/*/' | bc)
    [ $first -ge $second ] && echo $1 || echo $2
}

David Beniamine's avatar
David Beniamine committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
# Default main screens (external VGA or internal screen)
MAIN_SCREENS="DP-3 VGA-1 LVDS-1 eDP1"

# auto detect main screen
for m in $MAIN_SCREENS
do
	if [ ! -z "$(xrandr | grep " connected" | grep $m)" ]
	then
		MAIN_SCREEN=$m
		break
	fi
done

if [ -z "$MAIN_SCREEN" ]
then
	# No main screen : use the first connected
	MAIN_SCREEN=$(xrandr | grep " connected" | head -n 1 | cut -d ' ' -f 1)
fi
David Beniamine's avatar
Fix #67  
David Beniamine committed
63
log  "MAIN_SCREEN '$MAIN_SCREEN'"
David Beniamine's avatar
David Beniamine committed
64 65 66 67

screens=$(xrandr | grep " connected" | cut -f 1 -d ' ' \
    | sed -e 's/ /\n/g' | grep -v $MAIN_SCREEN)

David Beniamine's avatar
Fix #67  
David Beniamine committed
68 69
log  "SCREENS '$screens'"
if [ -z "$screens$MAIN_SCREEN" ]
70
then
David Beniamine's avatar
Fix #67  
David Beniamine committed
71
    log  "noscreens exiting on $(date)"
72 73 74
    exit
fi

David Beniamine's avatar
David Beniamine committed
75 76 77
# Auto detect best resolution
for m in $(xrandr | awk -v pattern="^$MAIN_SCREEN" '/^.* connected/{IN=0} $0 ~ pattern {IN=1} /^ /{if(IN==1){print $1}}')
do
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    # echo "testing mode $m"
    OTHERS=$(xrandr | awk -v pattern="^$MAIN_SCREEN" '/^.* connected/{IN=1} $0 ~ pattern {IN=0} {if(IN==1){print $1}}')
    if [ -z "$OTHERS" ]
    then
        # one screen look for best common resolution
        mode=$(max_res $m $mode)
    else
        # Several screen look for best common resolution
        for line in $OTHERS
        do
            # echo "line $line"
            if [[ $line =~ [0-9]* ]]
            then
                # New mode
                if [ "$line" == "$m" ]
                then
                    found=1
                fi
            else
                # New screen
                if [ "$found" == 0 ]
                then
                    break
                fi
                found=0
            fi
            # echo "Found ? $found"
        done
        if [ "$found" == 1 ]
        then
            mode="$m"
            break
        fi
    fi
David Beniamine's avatar
David Beniamine committed
112 113 114 115
done
# echo "mode $mode"

# Primary screen
116
cmd="xrandr --output $MAIN_SCREEN --auto --mode $mode --rotate normal --primary"
David Beniamine's avatar
Fix #67  
David Beniamine committed
117
log  "command $cmd"
David Beniamine's avatar
David Beniamine committed
118 119 120 121 122

# Add all connected as copy
prev_s=$MAIN_SCREEN
for s in $(echo -e $screens)
do
123
    cmd="$cmd --output $s --auto --mode $mode --same-as $prev_s --rotate normal"
David Beniamine's avatar
David Beniamine committed
124 125
    prev_s="$s"
done
David Beniamine's avatar
Fix #67  
David Beniamine committed
126
log  "command after all outputs $cmd"
David Beniamine's avatar
David Beniamine committed
127 128 129 130 131 132 133

# Disable all disconnected screens
screens=$(xrandr | grep " disconnected" | cut -f 1 -d ' ' )
for s in $(echo -e $screens)
do
	cmd="$cmd --output $s --off"
done
David Beniamine's avatar
Fix #67  
David Beniamine committed
134
log  "command after all offs $cmd"
David Beniamine's avatar
David Beniamine committed
135

David Beniamine's avatar
Fix #67  
David Beniamine committed
136 137 138 139 140 141 142
log  "old cmd '$(cat /tmp/vp)'"
log  "new cmd '$cmd'"
if [ "$cmd" == "$(cat /tmp/vp)" ]
then
	log  "No cmd change, exiting on $(date)"
	exit
fi
David Beniamine's avatar
David Beniamine committed
143
echo $cmd > /tmp/vp
David Beniamine's avatar
David Beniamine committed
144 145 146

# Actually do stuff
$cmd
David Beniamine's avatar
Fix #67  
David Beniamine committed
147
log  "Runned $cmd, exiting on $(date)"