1

xrandr has a nifty feature that lets you scale your entire screen so that your virtual resolution is larger or smaller than your actual resolution. I use it on my netbook's 1024x600 resolution screen to work in a 1280x750 resolution workspace. Can I set up keyboard shortcuts to increase/decrease/reset the scale factor I'm currently using? Some applications do great with the extra space, while some are harder to read. I know how to set up keyboard shortcuts in my distribution already, but I don't know how I can make xrandr accept this sort of differential input

2

I wrote the following script so that I could change my scale factor dynamically using keyboard shortcuts on my Dell Mini 10v netbook:

#!/bin/bash
#/usr/local/bin/xrandr-scale-tool

#first, we read the config files
if [ -r ~/.config/xrandr-scale-tool/xscale ] 
  then 
    xscale=$(~/.config/xrandr-scale-tool/xscale 
fi
if [ ! -e ~/.config/xrandr-scale-tool/output ] 
  then 
    output=LVDS1 
    echo $output>~/.config/xrandr-scale-tool/output 
fi
if [ ! -e ~/.config/xrandr-scale-tool/baseresx ] 
  then 
    baseresx=1024 
    echo $baseresx>~/.config/xrandr-scale-tool/baseresx 
fi
if [ ! -e ~/.config/xrandr-scale-tool/baseresy ] 
  then 
    baseresy=600 
    echo $baseresy>~/.config/xrandr-scale-tool/baseresy 
fi

#if they aren't readable, exit with status 1
if [ ! -r ~/.config/xrandr-scale-tool/xscale ] || [ ! -r ~/.config/xrandr-scale-tool/baseresx ] || [ ! -r ~/.config/xrandr-scale-tool/baseresy ] || [ ! -r ~/.config/xrandr-scale-tool/output ] 
  then 
    echo A config file could not be read 
    exit 1 
fi

#now, we check the first argument to see what to do
if [ -z "$1" ]
  then
    #restore from settings
    xrandr --output $output --scale ${xscale}x${xscale} --panning 0x0+0+0
    xrandr --output $output --scale ${xscale}x${xscale} --panning `xrandr -q | awk -F'current' -F',' 'NR==1 {gsub("( |current)","");print $2}'`
fi

if [ "$1" == "+" ]
  then
    #increment and apply
    amt=$2
    xscale=`echo $xscale+$amt|bc`
    xrandr --output $output --scale ${xscale}x${xscale} --panning 0x0+0+0
    xrandr --output $output --scale ${xscale}x${xscale} --panning `xrandr -q | awk -F'current' -F',' 'NR==1 {gsub("( |current)","");print $2}'`
    sleep 2
    notify-send "Scale Factor: $xscale" --icon=display
fi

if [ "$1" == "-" ]
  then
    #decrement and apply
    amt=$2
    xscale=`echo $xscale-$amt|bc`
    xrandr --output $output --scale ${xscale}x${xscale} --panning 0x0+0+0
    xrandr --output $output --scale ${xscale}x${xscale} --panning `xrandr -q | awk -F'current' -F',' 'NR==1 {gsub("( |current)","");print $2}'`
    sleep 2
    notify-send "Scale Factor: $xscale" --icon=display
fi

if [ "$1" == "reset" ]
  then
    #reset
    xscale=1
    xrandr --output $output --scale ${xscale}x${xscale} --panning `echo $xscale*$baseresx|bc`x`echo $xscale*$baseresy|bc`
    sleep 2
    notify-send "Scale Factor: $xscale" --icon=display
fi

#record changes in scale to file before exiting
echo $xscale>~/.config/xrandr-scale-tool/xscale
exit 0

This script assumes that your default resolution is 1024x600, and you're using the display on LVDS1. If this isn't the case, edit the files in ~/.config/xrandr-scale-tool to reflect this (config files are generated after the first run).

This script can be run in four ways:
xrandr-scale-tool restores scale settings from the config file, which is saved on each run
xrandr-scale-tool + 0.1 increments the scale by 0.1. You can use any number here
xrandr-scale-tool - 0.1 decrements the scale by 0.1. Again, any number works
xrandr-scale-tool reset resets the scale to 1.
Each time the scale is changed, notify-send is run to show you the current scale factor via a notification.

The way I set mine up is that xrandr-scale-tool + 0.125 is mapped to CtrlAlt+, xrandr-scale-tool - 0.125 is mapped to CtrlAlt-, and xrandr-scale-tool reset is mapped to CtrlAlt0. xrandr-scale-tool is run, with no arguments, on login, so that whichever scale I was using before is in use on my next login.

  • This answer doesn't sound at all like what your question was looking for. Your question sounds like you want to know how to set up keyboard bindings, not control xrandr via a script. If this is indeed what you meant, can you update your question to reflect that? – phemmer Oct 22 '13 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.