refreshVP.sh 5.62 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
#!/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/>.

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

David Beniamine's avatar
David Beniamine committed
38 39
DIR=$(realpath $(dirname $0))

40 41 42 43 44 45 46 47
# 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
# 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
66
log  "MAIN_SCREEN '$MAIN_SCREEN'"
David Beniamine's avatar
David Beniamine committed
67 68

screens=$(xrandr | grep " connected" | cut -f 1 -d ' ' \
David Beniamine's avatar
David Beniamine committed
69
    | sed -e 's/ /\n/g' | grep -v "\<$MAIN_SCREEN\>")
David Beniamine's avatar
David Beniamine committed
70

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

David Beniamine's avatar
David Beniamine committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
clone(){
    # 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
        # 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]* ]]
94
                then
David Beniamine's avatar
David Beniamine committed
95 96 97 98 99 100 101 102 103 104 105 106
                    # New mode
                    if [ "$line" == "$m" ]
                    then
                        found=1
                    fi
                else
                    # New screen
                    if [ "$found" == 0 ]
                    then
                        break
                    fi
                    found=0
107
                fi
David Beniamine's avatar
David Beniamine committed
108 109 110 111 112 113
                # echo "Found ? $found"
            done
            if [ "$found" == 1 ]
            then
                mode="$m"
                break
114 115
            fi
        fi
David Beniamine's avatar
David Beniamine committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    done
    # echo "mode $mode"
    
    # Primary screen
    cmd="xrandr --output $MAIN_SCREEN --auto --mode $mode --rotate normal --primary"
    log  "command $cmd"
    
    # Add all connected as copy
    prev_s=$MAIN_SCREEN
    for s in $(echo -e $screens)
    do
        cmd="$cmd --output $s --auto --mode $mode --same-as $prev_s --rotate normal"
        prev_s="$s"
    done
    log  "command after all outputs $cmd"
    
    # 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
    log  "command after all offs $cmd"
    
    run_xrandr_cmd $cmd
}
David Beniamine's avatar
David Beniamine committed
142

David Beniamine's avatar
David Beniamine committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
extend(){
    log "I should extend screens on $1"
    case $1 in
        "gauche")
            placement="--left-of"
            ;;
        "droite")
            placement="--right-of"
            ;;
        "haut")
            placement="--above"
            ;;
        "bas")
            placement="--below"
            ;;
    esac
    cmd="xrandr --output $MAIN_SCREEN --primary --auto"
    log $cmd
    for s in $screens
    do
        cmd="$cmd --output $s $placement $MAIN_SCREEN --auto" 
        log $cmd
    done
    run_xrandr_cmd $cmd
}
David Beniamine's avatar
David Beniamine committed
168

David Beniamine's avatar
David Beniamine committed
169 170 171 172 173 174 175 176
run_xrandr_cmd(){
    # Actually do stuff
    old_cmd=$(cat /tmp/vp)
    new_cmd="$@"
    timeout
    echo $new_cmd > /tmp/vp
    $new_cmd
    log  "Runned $new_cmd"
177 178 179 180 181 182 183 184 185 186
    if $interaction
    then
        zenity --question --title "Configuration des écrans" \
            --text "La configuration des écrans vous convient-elle ?" \
            --timeout=$timeout
        code=$?
    else
        code=1
    fi
    if [ $code != 0 ]
David Beniamine's avatar
David Beniamine committed
187 188 189 190 191 192
    then
        log "Restoring old command : $old_cmd"
        $old_cmd
    fi
    log "exiting on $(date)"
}
David Beniamine's avatar
David Beniamine committed
193

David Beniamine's avatar
Fixup  
David Beniamine committed
194
if [ -z "$screens" ]
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
then
    interaction=false
fi

if $interaction
then
    choice=$(zenity --entry --title "Écran connecté" \
        --text "Selection du mode d'affichage" \
        --entry-text "Clone" 'Écrans étendus à droite' 'Écrans étendus à gauche' \
        'Écrans étendus en haut' 'Écrans étendus en bas' \
        --timeout $timeout)
    code=$?
else
    code=5
fi
David Beniamine's avatar
David Beniamine committed
210 211

log "Choice '$choice' / code $code"
David Beniamine's avatar
David Beniamine committed
212

David Beniamine's avatar
David Beniamine committed
213
if [ -z "$choice" ]
David Beniamine's avatar
Fix #67  
David Beniamine committed
214
then
David Beniamine's avatar
David Beniamine committed
215 216 217
    if [ "$code" -eq 1 ]
    then
        # give up
David Beniamine's avatar
Fixups  
David Beniamine committed
218
        exit
David Beniamine's avatar
David Beniamine committed
219 220 221 222 223 224 225 226 227 228
    else
        # timeout
        log "timeout"
        clone
    fi
elif [ "$choice" == "Clone" ]
then
    clone
else
    extend $(echo $choice | cut -d ' ' -f 4)
David Beniamine's avatar
Fix #67  
David Beniamine committed
229
fi