#!/bin/bash # # Copyright (c) 2004 Stephan Müller # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # Feedback is welcomed :-) # # Script allows chaning of the resolution of head 2 based on the configuration # of X: resolution 0 is leftmost resolution in Monitor definition of X conf # resolution 1 is second leftmost resolution, and so on # # Script allows to clone the second head on the first head using x11vnc that # copies the second head into a VNC pipe and calling vncviewer. Script # automatically scales the size of the displayed vnc window (if second head is # larger that current head, it shrinks resolution and vice versa). For # calculating the scaling factor, script checks height of head 1 and height of # head 2 # res=-1 #resolution clone="no" scale_factor="" vhead=0 ihead=1 while getopts "r:s:cCh1" Option do case $Option in r) # FIXME Check for integer value and then for # value between 0 and 9 if expr "$OPTARG" : "[0-9][0-9]*$" > /dev/null then res=$OPTARG else echo "Resolution must be numeric and is the index with zero offset in the X config" exit 1 fi ;; c) clone="yes" fullscreen="" ;; C) clone="yes" fullscreen="-fullscreen" ;; 1) vhead=1 ihead=0 ;; s) if expr "$OPTARG" : "[0-9][0-9]*.*[0-9]*$" > /dev/null then scale_factor=$OPTARG else echo "The scaling factor must be an unsigned floating point value" exit 1 fi ;; h) echo "-r Toggles the resolution of the second head to NUM" echo " (resolution 0 is leftmost resolution in Monitor definition of X conf" echo " resolution 1 is second leftmost resolution, and so on)" echo "-c Clone head 2 on head 1 using x11vnc and vncviewer with the proper scaling" echo "-C Clone using fullscreen window" echo "-s manually scale the display by float FACTOR instead of autoscaling" echo "-1 display main head instead of 2nd head (use with -c/-C)" ;; esac done # Set resolution of head 2 if [ $res -ge 0 ] then xrandr -d :0.$ihead -s $res DISPLAY=:0.0 xrefresh DISPLAY=:0.1 xrefresh fi # Exit if not cloning the screen if [ "x$clone" != "xyes" ] then exit 0 fi #Screen "v" height (this is where the viewer is displayed) h1_y=$(xdpyinfo | grep -A 4 "screen #$vhead" | grep "dimensions" | awk '{print $2}' | awk 'BEGIN {FS="x"} {print $2}') #Screen "i" height (this one is used as input) h2_y=$(xdpyinfo | grep -A 4 "screen #$ihead" | grep "dimensions" | awk '{print $2}' | awk 'BEGIN {FS="x"} {print $2}') # Shrink the Y size a bit to leave room for the window titlebar and kicker if [ -z "$fullscreen" ]; then let h1_y=$h1_y-85 fi # use autoscaling if no manual scale factor specified if [ -z "$scale_factor" ] then if [ -z "$fullscreen" ] then # not fullscreen, don't use scale factors >1.0 to magnify SCALE=$(LC_ALL=C awk "BEGIN {s=$h1_y/$h2_y; printf \"%f\n\", (s<1.0 ? s : 1.0)}") else # fullscreen, always scale to full screen height SCALE=$(LC_ALL=C awk "BEGIN {printf \"%f\n\", $h1_y/$h2_y}") fi else # manual scale, needs to be in floating point format SCALE=$(awk "BEGIN {printf \"%f\n\", $scale_factor}") fi export DISPLAY=:0.$vhead x11vnc -scale "$SCALE" \ -deferupdate 20 \ -wait 25 \ -defer 25 \ -localhost \ -cursorpos \ -display :0.$ihead \ -bg -q > /dev/null 2>&1 && \ vncviewer -viewonly $fullscreen localhost:0 > /dev/null &