|
| 1 | +#!/bin/bash |
| 2 | +# From: http://www.thinkwiki.org/wiki/Sample_Fn-F7_script |
| 3 | +# |
| 4 | +# External output may be "VGA" or "VGA-0" or "DVI-0" or "TMDS-1" |
| 5 | +EXTERNAL_OUTPUT="VGA1" |
| 6 | +INTERNAL_OUTPUT="LVDS1" |
| 7 | +# EXTERNAL_LOCATION may be one of: left, right, above, or below |
| 8 | +EXTERNAL_LOCATION="right" |
| 9 | + |
| 10 | +case "$EXTERNAL_LOCATION" in |
| 11 | + left|LEFT) |
| 12 | + EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT" |
| 13 | + ;; |
| 14 | + right|RIGHT) |
| 15 | + EXTERNAL_LOCATION="--right-of $INTERNAL_OUTPUT" |
| 16 | + ;; |
| 17 | + top|TOP|above|ABOVE) |
| 18 | + EXTERNAL_LOCATION="--above $INTERNAL_OUTPUT" |
| 19 | + ;; |
| 20 | + bottom|BOTTOM|below|BELOW) |
| 21 | + EXTERNAL_LOCATION="--below $INTERNAL_OUTPUT" |
| 22 | + ;; |
| 23 | + *) |
| 24 | + EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT" |
| 25 | + ;; |
| 26 | +esac |
| 27 | + |
| 28 | +function screen_external(){ |
| 29 | + xrandr --output $INTERNAL_OUTPUT --off |
| 30 | + xrandr --output $EXTERNAL_OUTPUT --auto |
| 31 | +} |
| 32 | + |
| 33 | +function screen_internal(){ |
| 34 | + xrandr --output $EXTERNAL_OUTPUT --off |
| 35 | + xrandr --output $INTERNAL_OUTPUT --auto |
| 36 | +} |
| 37 | + |
| 38 | +function screen_mirror(){ |
| 39 | + xrandr --output $INTERNAL_OUTPUT --auto |
| 40 | + xrandr --output $EXTERNAL_OUTPUT --auto --same-as $INTERNAL_OUTPUT |
| 41 | +} |
| 42 | + |
| 43 | +function screen_both(){ |
| 44 | + xrandr --output $INTERNAL_OUTPUT --auto |
| 45 | + xrandr --output $EXTERNAL_OUTPUT --auto $EXTERNAL_LOCATION |
| 46 | +} |
| 47 | + |
| 48 | +function screen_toggle(){ |
| 49 | + # Figure out current state |
| 50 | + INTERNAL_STATE=$(xrandr | grep $INTERNAL_OUTPUT | grep con | sed "s/.*connected //" | sed "s/(.*//") |
| 51 | + EXTERNAL_STATE=$(xrandr | grep $EXTERNAL_OUTPUT | grep con | sed "s/.*connected //" | sed "s/(.*//") |
| 52 | + |
| 53 | + if [ -z "$INTERNAL_STATE" ]; then |
| 54 | + STATE="external" |
| 55 | + elif [ -z "$EXTERNAL_STATE" ]; then |
| 56 | + STATE="internal" |
| 57 | + else |
| 58 | + INTERNAL_STATE=$(echo $INTERNAL_STATE | sed "s/[0-9]*x[0-9]*//") |
| 59 | + EXTERNAL_STATE=$(echo $EXTERNAL_STATE | sed "s/[0-9]*x[0-9]*//") |
| 60 | + if [ "$INTERNAL_STATE" = "$EXTERNAL_STATE" ]; then |
| 61 | + STATE="mirror" |
| 62 | + else |
| 63 | + STATE="both" |
| 64 | + fi |
| 65 | + fi |
| 66 | + |
| 67 | + case "$STATE" in |
| 68 | + internal) |
| 69 | + screen_mirror |
| 70 | + ;; |
| 71 | + mirror) |
| 72 | + screen_external |
| 73 | + ;; |
| 74 | + external) |
| 75 | + screen_both |
| 76 | + ;; |
| 77 | + both) |
| 78 | + screen_internal |
| 79 | + ;; |
| 80 | + *) |
| 81 | + screen_internal |
| 82 | + ;; |
| 83 | + esac |
| 84 | +} |
| 85 | + |
| 86 | +# based on /etc/acpi/screenblank.sh (Ubuntu 7.10) |
| 87 | +# . /usr/share/acpi-support/power-funcs # for getXuser |
| 88 | +getXuser() { |
| 89 | + user=`finger| grep -m1 ":$displaynum " | awk '{print $1}'` |
| 90 | + if [ x"$user" = x"" ]; then |
| 91 | + user=`finger| grep -m1 ":$displaynum" | awk '{print $1}'` |
| 92 | + fi |
| 93 | + if [ x"$user" != x"" ]; then |
| 94 | + userhome=`getent passwd $user | cut -d: -f6` |
| 95 | + export XAUTHORITY=$userhome/.Xauthority |
| 96 | + else |
| 97 | + export XAUTHORITY="" |
| 98 | + fi |
| 99 | +} |
| 100 | +# end of getXuser from /usr/share/acpi-support/power-funcs |
| 101 | +# |
| 102 | +for x in /tmp/.X11-unix/*; do |
| 103 | + displaynum=`echo $x | sed s#/tmp/.X11-unix/X##` |
| 104 | + getXuser; |
| 105 | + if [ x"$XAUTHORITY" != x"" ]; then |
| 106 | + export DISPLAY=":$displaynum" |
| 107 | + screen_toggle |
| 108 | + fi |
| 109 | +done |
| 110 | + |
| 111 | +# What should we do? |
| 112 | +DO="$1" |
| 113 | +if [ -z "$DO" ]; then |
| 114 | + if [ $(basename $0) = "screen" ]; then |
| 115 | + DO="toggle" |
| 116 | + fi |
| 117 | +fi |
| 118 | + |
| 119 | +case "$DO" in |
| 120 | + toggle) |
| 121 | + screen_toggle |
| 122 | + ;; |
| 123 | + internal) |
| 124 | + screen_internal |
| 125 | + ;; |
| 126 | + external) |
| 127 | + screen_external |
| 128 | + ;; |
| 129 | + mirror) |
| 130 | + screen_mirror |
| 131 | + ;; |
| 132 | + both) |
| 133 | + screen_both |
| 134 | + ;; |
| 135 | + status) |
| 136 | + echo "Current Fn-F7 state is: $STATE" |
| 137 | + echo |
| 138 | + echo "Attached monitors:" |
| 139 | + $SU xrandr | grep "\Wconnected" | sed "s/^/ /" |
| 140 | + ;; |
| 141 | + *) |
| 142 | + echo "usage: $0 <command>" >&2 |
| 143 | + echo >&2 |
| 144 | + echo " commands:" >&2 |
| 145 | + echo " status" >&2 |
| 146 | + echo " internal" >&2 |
| 147 | + echo " external" >&2 |
| 148 | + echo " mirror" >&2 |
| 149 | + echo " both" >&2 |
| 150 | + echo " toggle" >&2 |
| 151 | + echo >&2 |
| 152 | + ;; |
| 153 | +esac |
| 154 | + |
0 commit comments