Setup screens

parent 2d563084
......@@ -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,76 +73,139 @@ then
exit
fi
# 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]* ]]
then
# New mode
if [ "$line" == "$m" ]
then
found=1
fi
else
# New screen
if [ "$found" == 0 ]
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]* ]]
then
break
# New mode
if [ "$line" == "$m" ]
then
found=1
fi
else
# New screen
if [ "$found" == 0 ]
then
break
fi
found=0
fi
found=0
# echo "Found ? $found"
done
if [ "$found" == 1 ]
then
mode="$m"
break
fi
# echo "Found ? $found"
done
if [ "$found" == 1 ]
then
mode="$m"
break
fi
fi
done
# echo "mode $mode"
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
}
# Primary screen
cmd="xrandr --output $MAIN_SCREEN --auto --mode $mode --rotate normal --primary"
log "command $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
}
# 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"
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 "Restoring old command : $old_cmd"
$old_cmd
fi
log "exiting on $(date)"
}
# 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"
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"
log "old cmd '$(cat /tmp/vp)'"
log "new cmd '$cmd'"
if [ "$cmd" == "$(cat /tmp/vp)" ]
if [ -z "$choice" ]
then
log "No cmd change, exiting on $(date)"
exit
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
echo $cmd > /tmp/vp
# Actually do stuff
$cmd
log "Runned $cmd, exiting on $(date)"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment