Unverified Commit 35ce7f35 authored by David Beniamine's avatar David Beniamine
Browse files

Setup screens

parent 2d563084
Loading
Loading
Loading
Loading
+131 −67
Original line number Diff line number Diff line
@@ -16,15 +16,16 @@
# 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
DEBUG=false
timeout=10
DEBUG=true
log(){
    if $DEBUG
    then
        echo "$@" >> /var/log/vp.log
        echo "$@" | tee /var/log/vp.log
    fi
}
log  "starting VP $(date)"
# Get rights to access display
user=$(who | grep -v "root" | head -n 1 | cut -d ' ' -f 1)
if [ ! -z "$user" ]
then
@@ -63,7 +64,7 @@ fi
log  "MAIN_SCREEN '$MAIN_SCREEN'"

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

log  "SCREENS '$screens'"
if [ -z "$screens$MAIN_SCREEN" ]
@@ -72,6 +73,7 @@ then
    exit
fi

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
@@ -133,15 +135,77 @@ do
    done
    log  "command after all offs $cmd"
    
log  "old cmd '$(cat /tmp/vp)'"
log  "new cmd '$cmd'"
if [ "$cmd" == "$(cat /tmp/vp)" ]
    run_xrandr_cmd $cmd
}

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
}

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"
    zenity --question --title "Configuration des écrans" \
        --text "La configuration des écrans vous convient-elle ?" \
        --timeout=$timeout
    if [ $? != 0 ]
    then
	log  "No cmd change, exiting on $(date)"
	exit
        log "Restoring old command : $old_cmd"
        $old_cmd
    fi
echo $cmd > /tmp/vp
    log "exiting on $(date)"
}

# Actually do stuff
$cmd
log  "Runned $cmd, exiting on $(date)"
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=$?

log "Choice '$choice' / code $code"

if [ -z "$choice" ]
then
    if [ "$code" -eq 1 ]
    then
        # give up
        return
    else
        # timeout
        log "timeout"
        clone
    fi
elif [ "$choice" == "Clone" ]
then
    clone
else
    extend $(echo $choice | cut -d ' ' -f 4)
fi