From 2be222f31dc1efa22375a1f24f49f9e6522c6ed0 Mon Sep 17 00:00:00 2001 From: Stefan Tomanek Date: Fri, 10 Dec 2010 02:01:21 +0100 Subject: [PATCH] merge autorandr and autodisper --- auto-disper | 128 +--------------------------------------------------- autorandr | 74 +++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 144 deletions(-) mode change 100755 => 120000 auto-disper diff --git a/auto-disper b/auto-disper deleted file mode 100755 index 0f8110f..0000000 --- a/auto-disper +++ /dev/null @@ -1,127 +0,0 @@ -#!/bin/sh -# -# Automatically select a display configuration based on connected devives -# -# Stefan Tomanek -# -# requires disper, the command line display switcher: -# http://willem.engen.nl/projects/disper/ -# -# -# How to use: -# -# Save your current display configuration and setup with -# auto-disper --save mobil -# -# Connect an additional display, configure your setup and save it -# auto-disper --save docked -# -# Now auto-disper can detect which hardware setup is active: # auto-disper -# mobile -# docked (detected) -# -# To automatically reload your setup, just append --change to the command line -# -# To manually load a profile, you can use the --load option. -# -# To prevent a profile from being loaded, place a script call "block" in its -# directory. The script is evaluated before the screen setup is inspected, and -# in case of it returning a value of 0 the profile is skipped. This can be used -# to query the status of a docking station you are about to leave. -# -# If no suitable profile can be identified, the current configuration is kept. -# To change this behaviour and switch to a fallback configuration, specify -# --default -# -# Another script called "postswitch "can be placed in the directory -# ~/.auto-disper as well as in all profile directories: The scripts are -# executed after a mode switch has taken place and can notify window managers -# or other applications about it. - -DISPER=/usr/bin/disper -PROFILES=~/.auto-disper/ - -CHANGE_PROFILE=0 -DEFAULT_PROFILE="" -SAVE_PROFILE="" - -blocked() { - local PROFILE="$1" - [ ! -x "$PROFILES/$PROFILE/block" ] && return 1 - - "$PROFILES/$PROFILE/block" "$PROFILE" -} - -load() { - local PROFILE="$1" - if [ "$CHANGE_PROFILE" -eq 1 ]; then - echo " -> loading profile $PROFILE" - $DISPER -i < "$PROFILES/$PROFILE/config" - - [ -x "$PROFILES/$PROFILE/postswitch" ] && \ - "$PROFILES/$PROFILE/postswitch" "$PROFILE" - [ -x "$PROFILES/postswitch" ] && \ - "$PROFILES/postswitch" "$PROFILE" - fi -} - -# process parameters -OPTS=$(getopt -n auto-disper -o s:l:d:c --long change,default:,save:,load: -- "$@") -if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi -eval set -- "$OPTS" - -while true; do - case "$1" in - -c|--change) CHANGE_PROFILE=1; shift ;; - -d|--default) DEFAULT_PROFILE="$2"; shift 2 ;; - -s|--save) SAVE_PROFILE="$2"; shift 2 ;; - -l|--load) LOAD_PROFILE="$2"; shift 2 ;; - --) shift; break ;; - *) echo "Error: $1"; exit 1;; - esac -done - -CURRENT_SETUP="$($DISPER -l | grep '^display ')" - -if [ -n "$SAVE_PROFILE" ]; then - echo "Saving current configuration as profile '${SAVE_PROFILE}'" - mkdir -p "$PROFILES/$SAVE_PROFILE" - echo "$CURRENT_SETUP" > "$PROFILES/$SAVE_PROFILE/setup" - $DISPER -p > "$PROFILES/$SAVE_PROFILE/config" - exit 0 -fi - -if [ -n "$LOAD_PROFILE" ]; then - CHANGE_PROFILE=1 load "$LOAD_PROFILE" - exit $? -fi - -for SETUP_FILE in $PROFILES/*/setup; do - if ! [ -e $SETUP_FILE ]; then - break - fi - PROFILE="$(basename $(dirname "$SETUP_FILE"))" - echo -n "$PROFILE" - - if blocked "$PROFILE"; then - echo " (blocked)" - continue - fi - - FILE_SETUP="$(cat "$PROFILES/$PROFILE/setup")" - if [ "$CURRENT_SETUP" = "$FILE_SETUP" ]; then - echo " (detected)" - load "$PROFILE" - # found the profile, exit with success - exit 0 - else - echo "" - fi -done - -# we did not find the profile, load default -if [ -n "$DEFAULT_PROFILE" ]; then - echo "No suitable profile detected, falling back to $DEFAULT_PROFILE" - load "$DEFAULT_PROFILE" -fi -exit 1 diff --git a/auto-disper b/auto-disper new file mode 120000 index 0000000..94cdea8 --- /dev/null +++ b/auto-disper @@ -0,0 +1 @@ +autorandr \ No newline at end of file diff --git a/autorandr b/autorandr index aaaa069..6d1f91f 100755 --- a/autorandr +++ b/autorandr @@ -6,23 +6,23 @@ # # How to use: # -# Save your current display configuration and setup with -# autorandr --save mobile +# Save your current display configuration and setup with: +# $ autorandr --save mobile # -# Connect an additional display, configure your setup and save it -# autorand --save docked +# Connect an additional display, configure your setup and save it: +# $ autorandr --save docked # # Now autorandr can detect which hardware setup is active: -# # autorandr -# mobile -# docked (detected) +# $ autorandr +# mobile +# docked (detected) # # To automatically reload your setup, just append --change to the command line # # To manually load a profile, you can use the --load option. # # autorandr tries to avoid reloading an identical configuration. To force the -# configuration, apply --force. +# (re)configuration, apply --force. # # To prevent a profile from being loaded, place a script call "block" in its # directory. The script is evaluated before the screen setup is inspected, and @@ -34,11 +34,18 @@ # --default # # Another script called "postswitch "can be placed in the directory -# ~/.auto-disper as well as in all profile directories: The scripts are -# executed after a mode switch has taken place and can notify window managers -# or other applications about it. +# ~/.autorandr as well as in all profile directories: The scripts are executed +# after a mode switch has taken place and can notify window managers or other +# applications about it. +# +# +# While the script uses xrandr by defult, calling it by the name "autodisper" +# or "auto-disper" forces it to use the "disper" utility, which is useful for +# controlling nvidia chipsets. The formats for fingerprinting the current setup +# and saving/loading the current configuration are adjusted accordingly. XRANDR=/usr/bin/xrandr +DISPER=/usr/bin/disper PROFILES=~/.autorandr/ CONFIG=~/.autorandr.conf @@ -48,6 +55,16 @@ DEFAULT_PROFILE="" SAVE_PROFILE="" FP_METHODS="setup_fp_xrandr_edid setup_fp_sysfs_edid" +CURRENT_CFG_METHOD="current_cfg_xrandr" +LOAD_METHOD="load_cfg_xrandr" + +SCRIPTNAME="$(basename $0)" +# when called as autodisper/auto-disper, we assume different defaults +if [ "$SCRIPTNAME" = "auto-disper" ] || [ "$SCRIPTNAME" = "autodisper" ]; then + FP_METHODS="setup_fp_disper" + CURRENT_CFG_METHOD="current_cfg_disper" + LOAD_METHOD="load_cfg_disper" +fi test -f $CONFIG && . $CONFIG @@ -70,6 +87,10 @@ setup_fp_sysfs_edid() { done } +setup_fp_disper() { + $DISPER -l | grep '^display ' +} + setup_fp() { local FP=""; for M in $FP_METHODS; do @@ -83,8 +104,7 @@ setup_fp() { echo "$FP" } - -current_cfg() { +current_cfg_xrandr() { $XRANDR -q | awk ' /^[^ ]+ disconnected / { print "output "$1; @@ -98,6 +118,14 @@ current_cfg() { }' } +current_cfg_disper() { + $DISPER -p +} + +current_cfg() { + $CURRENT_METHOD; +} + blocked() { local PROFILE="$1" [ ! -x "$PROFILES/$PROFILE/block" ] && return 1 @@ -115,11 +143,20 @@ config_equal() { fi } +load_cfg_xrandr() { + sed 's!^!--!' "$1" | xargs $XRANDR +} + +load_cfg_disper() { + $DISPER < -i < "$1" +} + load() { local PROFILE="$1" - if [ -e "$PROFILES/$PROFILE/config" ] ; then + local CONF="$PROFILES/$PROFILE/config" + if [ -e "$CONF" ] ; then echo " -> loading profile $PROFILE" - sed 's!^!--!' "$PROFILES/$PROFILE/config" | xargs xrandr + $LOAD_METHOD "$CONF" [ -x "$PROFILES/$PROFILE/postswitch" ] && \ "$PROFILES/$PROFILE/postswitch" "$PROFILE" @@ -130,7 +167,7 @@ load() { help() { cat <. Another script called "postswitch "can be placed in the directory - ~/.auto-disper as well as in any profile directories: The scripts are executed + ~/.autorandr as well as in any profile directories: The scripts are executed after a mode switch has taken place and can notify window managers. + When called by the name "autodisper" or "auto-disper", the script uses "disper" + instead of "xrandr" to detect, configure and save the display configuration. + EOH exit }