Difference between revisions of "Sample Fn-F7 script"

From ThinkWiki
Jump to: navigation, search
Line 29: Line 29:
 
  '''VGA''' connected 1600x1200+0+0 (normal left inverted right x axis y axis) 432mm x 324mm
 
  '''VGA''' connected 1600x1200+0+0 (normal left inverted right x axis y axis) 432mm x 324mm
 
  '''LVDS''' connected (normal left inverted right x axis y axis)
 
  '''LVDS''' connected (normal left inverted right x axis y axis)
 +
<bash>#!/bin/bash
 +
#@ seva sevatech.com
  
#!/bin/bash
+
EXTERNAL_OUTPUT="'''VGA'''"
#@ seva sevatech.com
+
INTERNAL_OUTPUT="'''LVDS'''"
+
EXTERNAL_LOCATION="'''left'''"
EXTERNAL_OUTPUT="'''VGA'''"
+
 
INTERNAL_OUTPUT="'''LVDS'''"
+
# This is to figure out which user and X11 display to work on
EXTERNAL_LOCATION="'''left'''"
+
# TODO there has to be a better way to do this?
+
SU="su $(w -h -s | grep ":[0-9]" | head -1 | awk '{print $1}') -c"
# This is to figure out which user and X11 display to work on
+
export DISPLAY=$(w -h -s | grep ":[0-9]" | head -1 | awk '{print $3}')
# TODO there has to be a better way to do this?
+
 
SU="su $(w -h -s | grep ":[0-9]" | head -1 | awk '{print $1}') -c"
+
case "$EXTERNAL_LOCATION" in
export DISPLAY=$(w -h -s | grep ":[0-9]" | head -1 | awk '{print $3}')
+
      left|LEFT)
+
              EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT"
case "$EXTERNAL_LOCATION" in
+
              ;;
        left|LEFT)
+
      right|RIGHT)
                EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT"
+
              EXTERNAL_LOCATION="--right-of $INTERNAL_OUTPUT"
                ;;
+
              ;;
        right|RIGHT)
+
      top|TOP|above|ABOVE)
                EXTERNAL_LOCATION="--right-of $INTERNAL_OUTPUT"
+
              EXTERNAL_LOCATION="--above $INTERNAL_OUTPUT"
                ;;
+
              ;;
        top|TOP|above|ABOVE)
+
      bottom|BOTTOM|below|BELOW)
                EXTERNAL_LOCATION="--above $INTERNAL_OUTPUT"
+
              EXTERNAL_LOCATION="--below $INTERNAL_OUTPUT"
                ;;
+
              ;;
        bottom|BOTTOM|below|BELOW)
+
      *)
                EXTERNAL_LOCATION="--below $INTERNAL_OUTPUT"
+
              EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT"
                ;;
+
              ;;
        *)
+
esac
                EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT"
+
 
                ;;
+
# Save the state to the file until we can figure it out by running xrandr
esac
+
# TODO figure out the state from xrandr and xwininfo -root or something?
+
STATE_FILE=/var/lib/thinkpad/screen.state;
# Save the state to the file until we can figure it out by running xrandr
+
if [ ! -e $STATE_FILE ]; then
# TODO figure out the state from xrandr and xwininfo -root or something?
+
      echo "internal" > $STATE_FILE
STATE_FILE=/var/lib/thinkpad/screen.state;
+
fi
if [ ! -e $STATE_FILE ]; then
+
 
        echo "internal" > $STATE_FILE
+
STATE=$(cat $STATE_FILE)
fi
+
 
+
function screen_external(){
STATE=$(cat $STATE_FILE)
+
      $SU "xrandr --output $INTERNAL_OUTPUT --off"
+
      $SU "xrandr --output $EXTERNAL_OUTPUT --auto"
function screen_external(){
+
      echo "external" > $STATE_FILE
        $SU "xrandr --output $INTERNAL_OUTPUT --off"
+
}
        $SU "xrandr --output $EXTERNAL_OUTPUT --auto"
+
 
        echo "external" > $STATE_FILE
+
function screen_internal(){
}
+
      $SU "xrandr --output $EXTERNAL_OUTPUT --off"
+
      $SU "xrandr --output $INTERNAL_OUTPUT --auto"
function screen_internal(){
+
      echo "internal" > $STATE_FILE
        $SU "xrandr --output $EXTERNAL_OUTPUT --off"
+
}
        $SU "xrandr --output $INTERNAL_OUTPUT --auto"
+
 
        echo "internal" > $STATE_FILE
+
function screen_mirror(){
}
+
      $SU "xrandr --output $INTERNAL_OUTPUT --auto"
+
      $SU "xrandr --output $EXTERNAL_OUTPUT --auto --same-as $INTERNAL_OUTPUT"
function screen_mirror(){
+
      echo "mirror" > $STATE_FILE
        $SU "xrandr --output $INTERNAL_OUTPUT --auto"
+
}
        $SU "xrandr --output $EXTERNAL_OUTPUT --auto --same-as $INTERNAL_OUTPUT"
+
 
        echo "mirror" > $STATE_FILE
+
function screen_both(){
}
+
      $SU "xrandr --output $INTERNAL_OUTPUT --auto"
+
      $SU "xrandr --output $EXTERNAL_OUTPUT --auto $EXTERNAL_LOCATION"
function screen_both(){
+
      echo "both" > $STATE_FILE
        $SU "xrandr --output $INTERNAL_OUTPUT --auto"
+
}
        $SU "xrandr --output $EXTERNAL_OUTPUT --auto $EXTERNAL_LOCATION"
+
 
        echo "both" > $STATE_FILE
+
function screen_toggle(){
}
+
      case "$STATE" in
+
              internal)
function screen_toggle(){
+
                      screen_mirror
        case "$STATE" in
+
                      ;;
                internal)
+
              mirror)
                        screen_mirror
+
                      screen_external
                        ;;
+
                      ;;
                mirror)
+
              external)
                        screen_external
+
                      screen_both
                        ;;
+
                      ;;
                external)
+
              both)
                        screen_both
+
                      screen_internal
                        ;;
+
                      ;;
                both)
+
              *)
                        screen_internal
+
                      screen_internal
                        ;;
+
                      ;;
                *)
+
      esac
                        screen_internal
+
}
                        ;;
+
 
        esac
+
# What should we do?
}
+
DO="$1"
+
if [ -z "$DO" ]; then
# What should we do?
+
      DO=$(basename $0)
DO="$1"
+
fi
if [ -z "$DO" ]; then
+
if [ -z "$DO" ]; then
        DO=$(basename $0)
+
      DO="internal"
fi
+
fi
if [ -z "$DO" ]; then
+
 
        DO="internal"
+
case "$DO" in
fi
+
      toggle|thinkpad-fn-f7)
+
              screen_toggle
case "$DO" in
+
              ;;
        toggle|thinkpad-fn-f7)
+
      internal)
                screen_toggle
+
              screen_internal
                ;;
+
              ;;
        internal)
+
      external)
                screen_internal
+
              screen_external
                ;;
+
              ;;
        external)
+
      mirror)
                screen_external
+
              screen_mirror
                ;;
+
              ;;
        mirror)
+
      both)
                screen_mirror
+
              screen_both
                ;;
+
              ;;
        both)
+
      status)
                screen_both
+
              echo "Current Fn-F7 state is: $STATE"
                ;;
+
              echo
        status)
+
              echo "Attached monitors:"
                echo "Current Fn-F7 state is: $STATE"
+
              xrandr | grep connected | sed "s/^/ /"
                echo
+
              ;;
                echo "Attached monitors:"
+
      *)
                xrandr | grep connected | sed "s/^/ /"
+
              echo "usage: $0 <command>" >&2
                ;;
+
              echo >&2
        *)
+
              echo "  commands:" >&2
                echo "usage: $0 <command>" >&2
+
              echo "          status" >&2
                echo >&2
+
              echo "          internal" >&2
                echo "  commands:" >&2
+
              echo "          external" >&2
                echo "          status" >&2
+
              echo "          mirror" >&2
                echo "          internal" >&2
+
              echo "          both" >&2
                echo "          external" >&2
+
              echo "          toggle" >&2
                echo "          mirror" >&2
+
              echo >&2
                echo "          both" >&2
+
              echo "  If no command is given default is internal" >&2
                echo "          toggle" >&2
+
              ;;
                echo >&2
+
esac</bash>
                echo "  If no command is given default is internal" >&2
 
                ;;
 
esac
 
  
 
As root, run the following commands,
 
As root, run the following commands,

Revision as of 12:06, 13 November 2007

This guide will help you configure Fn-F7 key combination to toggle between internal, mirror, external, or both screens. This was tested on ThinkPad X60s running Fedora 8, please comment if it works or does not work for you.

Add a "Virtual" statement to your /etc/X11/xorg.conf, the total resolution should be large enough to fit all your screens in the configuration you want, for example if you have 1600x1200 monitor to the left of your internal 1024x768 monitor, your total max resolution is 2624x1200 (See Xorg RandR 1.2 for more details):

Section "Screen"
       Identifier "Screen0"
       Device     "Videocard0"
       DefaultDepth     24
       SubSection "Display"
               Viewport   0 0
               Depth     24
               Virtual   2624 1200
       EndSubSection
EndSection

Restart X server at this point (i.e. logout and login).

Create /etc/acpi/events/thinkpad.conf:

# fn-F7
event=ibm/hotkey HKEY 00000080 00001007
action=/usr/local/sbin/thinkpad-fn-f7

Create /usr/local/sbin/thinkpad-fn-f7:

Note: you will have change EXTERNAL_OUTPUT and INTERNAL_OUTPUT to what xrandr shows, for example VGA and LVDS in this case:

$ xrandr | grep connected
VGA connected 1600x1200+0+0 (normal left inverted right x axis y axis) 432mm x 324mm
LVDS connected (normal left inverted right x axis y axis)

<bash>#!/bin/bash

  1. @ seva sevatech.com

EXTERNAL_OUTPUT="VGA" INTERNAL_OUTPUT="LVDS" EXTERNAL_LOCATION="left"

  1. This is to figure out which user and X11 display to work on
  2. TODO there has to be a better way to do this?

SU="su $(w -h -s | grep ":[0-9]" | head -1 | awk '{print $1}') -c" export DISPLAY=$(w -h -s | grep ":[0-9]" | head -1 | awk '{print $3}')

case "$EXTERNAL_LOCATION" in

      left|LEFT)
              EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT"
              ;;
      right|RIGHT)
              EXTERNAL_LOCATION="--right-of $INTERNAL_OUTPUT"
              ;;
      top|TOP|above|ABOVE)
              EXTERNAL_LOCATION="--above $INTERNAL_OUTPUT"
              ;;
      bottom|BOTTOM|below|BELOW)
              EXTERNAL_LOCATION="--below $INTERNAL_OUTPUT"
              ;;
      *)
              EXTERNAL_LOCATION="--left-of $INTERNAL_OUTPUT"
              ;;

esac

  1. Save the state to the file until we can figure it out by running xrandr
  2. TODO figure out the state from xrandr and xwininfo -root or something?

STATE_FILE=/var/lib/thinkpad/screen.state; if [ ! -e $STATE_FILE ]; then

      echo "internal" > $STATE_FILE

fi

STATE=$(cat $STATE_FILE)

function screen_external(){

      $SU "xrandr --output $INTERNAL_OUTPUT --off"
      $SU "xrandr --output $EXTERNAL_OUTPUT --auto"
      echo "external" > $STATE_FILE

}

function screen_internal(){

      $SU "xrandr --output $EXTERNAL_OUTPUT --off"
      $SU "xrandr --output $INTERNAL_OUTPUT --auto"
      echo "internal" > $STATE_FILE

}

function screen_mirror(){

      $SU "xrandr --output $INTERNAL_OUTPUT --auto"
      $SU "xrandr --output $EXTERNAL_OUTPUT --auto --same-as $INTERNAL_OUTPUT"
      echo "mirror" > $STATE_FILE

}

function screen_both(){

      $SU "xrandr --output $INTERNAL_OUTPUT --auto"
      $SU "xrandr --output $EXTERNAL_OUTPUT --auto $EXTERNAL_LOCATION"
      echo "both" > $STATE_FILE

}

function screen_toggle(){

      case "$STATE" in
              internal)
                      screen_mirror
                      ;;
              mirror)
                      screen_external
                      ;;
              external)
                      screen_both
                      ;;
              both)
                      screen_internal
                      ;;
              *)
                      screen_internal
                      ;;
      esac

}

  1. What should we do?

DO="$1" if [ -z "$DO" ]; then

      DO=$(basename $0)

fi if [ -z "$DO" ]; then

      DO="internal"

fi

case "$DO" in

      toggle|thinkpad-fn-f7)
              screen_toggle
              ;;
      internal)
              screen_internal
              ;;
      external)
              screen_external
              ;;
      mirror)
              screen_mirror
              ;;
      both)
              screen_both
              ;;
      status)
              echo "Current Fn-F7 state is: $STATE"
              echo
              echo "Attached monitors:"
              xrandr | grep connected | sed "s/^/ /"
              ;;
      *)
              echo "usage: $0 <command>" >&2
              echo >&2
              echo "  commands:" >&2
              echo "          status" >&2
              echo "          internal" >&2
              echo "          external" >&2
              echo "          mirror" >&2
              echo "          both" >&2
              echo "          toggle" >&2
              echo >&2
              echo "  If no command is given default is internal" >&2
              ;;

esac</bash>

As root, run the following commands,

# mkdir -p /var/lib/thinkpad
# echo "internal" > /var/lib/thinkpad/screen.state
# chmod 755 /usr/local/sbin/thinkpad-fn-f7
# service acpid restart

You should be ready to go, just press Fn-F7 to try.