Tuesday, February 10, 2015

Rotating display in Ubuntu and Linux Mint - II


In previous post we learnt how we can rotate display in Linux. In this post we'll create a simple function to make it more convenient. We'll split our function in two parts.

Find Screen label

First thing we need to do is to ask user to input the desired screen label that user wants to rotate. From our previous post we know that xrandr -q is the command that can provide us the information regarding each display with its label. We need to identify connected displays only. We'll store this information into an array using mapfile command.
mapfile -t SCREENS < <(xrandr -q | grep ' connected')
Now the "SCREENS" array holds the values for all connected screens. Now we'll present user a choice to select the desired screen.
local PS3="Select Display Screen: "

select SCREEN in "${SCREENS[@]}"
do
  case $SCREEN in
 *)
 break ;;
  esac
done
  
If user has made a valid choice then the "SCREEN" variable holds a string with information regarding the selected screen. We need only the label from that string. We can cut the string using blank space delimiter.
SCREEN=$(echo $SCREEN | cut -d ' ' -f1)

Find Rotation

In Linux you can rotate your monitor in four different positions:
  • normal
  • inverted
  • left
  • right
We'll present a choice to the user to select one of the rotations.
local ROTATIONS=("normal" "inverted" "left" "right")

local PS3="Select Rotation: "

select ROTATION in "${ROTATIONS[@]}"
do
 case $ROTATION in
  "normal") ;;
  "inverted") ;;
  "left") ;;
  "right") ;;
  *) return 0
 esac
done
  
If user has made a valid choice then we can rotate the screen.
xrandr --output "$SCREEN" --rotate "$ROTATION"
Putting it all together:
rotate(){
 local ROTATIONS=("normal" "inverted" "left" "right")
 mapfile -t SCREENS < <(xrandr -q | grep ' connected')
 
 local PS3="Select Display Screen: "

 select SCREEN in "${SCREENS[@]}"
 do
  case $SCREEN in
   *) 
   break ;;
  esac
 done
 SCREEN=$(echo $SCREEN | cut -d ' ' -f1)
 
 if [[ ! $SCREEN ]]
 then
  echo "Sorry, invalid selection!"
 else
 
  local PS3="Select Rotation: "
  
  select ROTATION in "${ROTATIONS[@]}"
  do
   case $ROTATION in
    "normal") ;;
    "inverted") ;;
    "left") ;;
    "right") ;;
    *) echo "Sorry, invalid selection!"
    return 0
   esac
   #putting this line here will make loop keep running until user enters an invalid input.
   xrandr --output "$SCREEN" --rotate "$ROTATION"
  done
 fi
}
  
Now you can add this function to your ~/.bashrc file and can access it from terminal by just typing rotate.

Friday, February 6, 2015

Rotating display in Ubuntu and Linux Mint - I


Just as in windows you have the option to rotate your screen into any direction in Linux, too. While in windows there are some shortcut keys, a key combination is not configured in Linux by default. But you can do the same thing using some terminal commands.

Find the Screen label

Before rotating a screen, first thing that you need to do is to identify how the screen has been labeled that you want to rotate. xrandr -q is the command that can provide that info. Running on my system gives below output:
Screen 0: minimum 320 x 200, current 3286 x 1080, maximum 32767 x 32767
eDP1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
   1366x768       60.1*+
   1360x768       59.8     60.0  
   1024x768       60.0  
   800x600        60.3     56.2  
   640x480        59.9  
DP1 connected primary 1920x1080+1366+0 (normal left inverted right x axis y axis) 521mm x 293mm
   1920x1080      60.0*+
   1680x1050      60.0  
   1600x900       60.0  
   1280x1024      75.0     60.0  
   1440x900       59.9  
   1280x800       59.8  
   1152x864       75.0  
   1280x720       60.0  
   1024x768       75.1     70.1     60.0  
   832x624        74.6  
   800x600        72.2     75.0     60.3     56.2  
   640x480        75.0     72.8     66.7     60.0  
   720x400        70.1  
HDMI1 disconnected (normal left inverted right x axis y axis)
HDMI2 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
I have two monitors. First one is labeled as eDP1 and second one is labeled as DP1.

Rotate Monitor

Determine the monitor that you want to rotate here from previous command output. Next, we want to rotate it around. For that, we use one of the following commands:
xrandr --output eDP1 --rotate right
xrandr --output eDP1 --rotate left
xrandr --output eDP1 --rotate inverted
xrandr --output eDP1 --rotate normal
Replace eDP1 with your monitor label in above commands and you'll be able to rotate the screen.
In next post I'll show you how you can create a bash script to do the same thing in an easy way.