Hack and / - Lightning Hacks Strike Twice
My laptop doubles as a tablet, and even though I don't use the tablet mode very often, when I do use it, I like to be able to rotate the screen around to portrait mode and back. Now, dock applications exist that can do this with a few clicks, and I always could just try to remember the right xrandr commands, but instead, I wrote a little script that I then bound to one of the hardware buttons on my laptop display. Each time I press the button, it runs the script and rotates the screen another 90 degrees.
The key to the script is to keep track of your current orientation. When xrandr rotates, it rotates only left, right, inverted or normal, so if you already are rotated to the left and rotate left again, it won't change. To accomplish this, I just write the current orientation to a temporary file. Listing 1 shows the full script.
Listing 1. Screen Rotation Script
#!/bin/sh export ORIENTATION=`cat /tmp/.orientation` if [ $ORIENTATION -eq "90" ]; then xrandr --auto xrandr --output LVDS --rotate inverted echo 180 > /tmp/.orientation echo "180" | osd_cat --shadow=2 --align=center \ --pos=bottom --color=green --delay=2 \ --font=lucidasanstypewriter-bold-24 \ --offset 40 & elif [ $ORIENTATION -eq "180" ]; then xrandr --auto xrandr --output LVDS --rotate left echo 270 > /tmp/.orientation echo "270" | osd_cat --shadow=2 --align=center \ --pos=bottom --color=green --delay=2 \ --font=lucidasanstypewriter-bold-24 \ --offset 40 & elif [ $ORIENTATION -eq "270" ]; then xrandr --output LVDS --rotate normal echo "Normal" | osd_cat --shadow=2 --align=center \ --pos=bottom --color=green --delay=2 \ --font=lucidasanstypewriter-bold-24 \ --offset 40 & echo 0 > /tmp/.orientation else xrandr --auto xrandr --output LVDS --rotate right echo 90 > /tmp/.orientation echo "90" | osd_cat --shadow=2 --align=center \ --pos=bottom --color=green --delay=2 \ --font=lucidasanstypewriter-bold-24 \ --offset 40 & fi
Notice in Listing 1 that I also added an echo piped to osd_cat. This is optional and just displays the current orientation on my screen. If you want to use this, be sure you have the osd_cat utility (it's included with the xosd-bin package in Debian and Ubuntu). The way the script is set up, it will run through each of the orientations in order before it goes back to normal. Because the temporary file will be deleted any time the machine reboots, I made sure to set the default mode to rotate 90 degrees.
Kyle Rankin is a Senior Systems Administrator in the San Francisco Bay Area and the author of a number of books, including Knoppix Hacks and Ubuntu Hacks for O'Reilly Media. He is currently the president of the North Bay Linux Users' Group.
Kyle Rankin is a director of engineering operations in the San Francisco Bay Area, the author of a number of books including DevOps Troubleshooting and The Official Ubuntu Server Book, and is a columnist for Linux Journal.
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems
Join editor Bill Childers and Bit9's Paul Riegle on April 27 at 12pm Central to learn how to keep your Linux systems secure.
Free to Linux Journal readers.Register Now!
|diff -u: What's New in Kernel Development||Aug 20, 2014|
|Security Hardening with Ansible||Aug 18, 2014|
|Monitoring Android Traffic with Wireshark||Aug 14, 2014|
|IndieBox: for Gamers Who Miss Boxes!||Aug 13, 2014|
|Non-Linux FOSS: a Virtualized Cisco Infrastructure?||Aug 11, 2014|
|Linux Security Threats on the Rise||Aug 08, 2014|
- diff -u: What's New in Kernel Development
- Security Hardening with Ansible
- NSA: Linux Journal is an "extremist forum" and its readers get flagged for extra surveillance
- New Products
- Tech Tip: Really Simple HTTP Server with Python
- [<Megashare>] Watch Mrs Brown's Boys Movie Online Full Movie HD 2014
- Monitoring Android Traffic with Wireshark
- Returning Values from Bash Functions
- RSS Feeds
- Raspberry Pi: the Perfect Home Server