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.
Getting Started with DevOps - Including New Data on IT Performance from Puppet Labs 2015 State of DevOps Report
August 27, 2015
12:00 PM CDT
DevOps represents a profound change from the way most IT departments have traditionally worked: from siloed teams and high-anxiety releases to everyone collaborating on uneventful and more frequent releases of higher-quality code. It doesn't matter how large or small an organization is, or even whether it's historically slow moving or risk averse — there are ways to adopt DevOps sanely, and get measurable results in just weeks.
Free to Linux Journal readers.Register Now!
- Django Models and Migrations
- Hacking a Safe with Bash
- Secure Server Deployments in Hostile Territory, Part II
- Home Automation with Raspberry Pi
- The Controversy Behind Canonical's Intellectual Property Policy
- Huge Package Overhaul for Debian and Ubuntu
- Shashlik - a Tasty New Android Simulator
- KDE Reveals Plasma Mobile
- Embed Linux in Monitoring and Control Systems
- diff -u: What's New in Kernel Development