Ubuntu 11.10 – Automatic HDMI Toggle (with audio!)

This took me forever (well, three days)! I didn’t really know what I was doing for the most part – I never heard of or used xrandr, xbacklight, and udev rules. After hours of googling and through trial and error, I somehow managed to get this script together and working.

A few notes though:

  • I made this for my laptop and specifically for a HDMI connection. Haven’t tested anything else like VGA (but I put it there in my script as an option just in case I ever wanted to try it out).
  • This only toggles between one display. So if more than one external monitor connection is present (such as HDMI or VGA), according to the order of if/then statements in the script, only the HDMI connection is used.
  • Most of my googling brought up solutions for external/dedicated graphics cards from AMD or Nvidia. HOWEVER, this script has only been tested on my laptop which has integrated graphics (Intel HD 3000).
  • This was tested on Ubuntu 11.10 on an HP-DM4-2191US laptop.

Thanks to this wiki, I learned how to create a udev rule.
SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/MToggle-udev"

Here’s my script (may need to adjust some stuff such as paths and the index/name of the audio profile):
# Charles Cruz
# Monitor Toggle
# The following script toggles between the internal monitor and an external monitor.
# Version 1.3
# Export Xauthority and display
USER="$(who | grep :0\) | cut -f 1 -d ' ')"
export XAUTHORITY=/home/$USER/.Xauthority
export DISPLAY=:0
########### Settings ###########
# Use 'xrandr' to find these
# Check /sys/class/drm for the exact location
DP_STATUS="$(cat /sys/class/drm/card0-DP-1/status)"
VGA_STATUS="$(cat /sys/class/drm/card0-VGA-1/status)"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"
# Backlight settings
# Do no change!
# Check to see if the external display is connected
if [ "${DP_STATUS}" = connected ]; then
if [ "${VGA_STATUS}" = connected ]; then
if [ "${HDMI_STATUS}" = connected ]; then
# The external display is connected
if [ "$EXTERNAL_DISPLAY" != "" ]; then
# Set the display settings
xrandr --output $INTERNAL_DISPLAY --off        # Turn off internal display
xrandr --output $EXTERNAL_DISPLAY --auto     # Turn on external display
# If connected via HDMI, change the sound profile to output HDMI audio
sudo -u $USER pactl set-card-profile 0 output:hdmi-surround
# The external display is not connected
# Restore internal display
xrandr --output $EXTERNAL_DISPLAY --off        # Turn off internal display
xrandr --output $INTERNAL_DISPLAY --auto
# Restore default battery/power brightness
cat /proc/acpi/ac_adapter/AC/state | grep "on-line"
if [ $? -eq 0 ]; then
xbacklight -set    $BACKLIGHT_AC        # Power cable is connected
xbacklight -set    $BACKLIGHT_BATTERY    # Power cable is not connected
# Restore laptop sound profile
sudo -u $USER pactl set-card-profile 0 output:analog-stereo+input:analog-stereo
exit 0

Afterwards, in a terminal I made sure the script was executable and that I update the udev rules:
sudo udevadm control --reload-rules
sudo chmod 755 /usr/local/bin/MToggle-udev

and done! Now it should work!


Ubuntu’s Unity and Gnome 3.2

For over two years, I’ve been using Ubuntu with Gnome 2 and Cairo Dock to do most of my daily computing on my small little netbook. It was cool having something non-mainstream and usable on my computer. I had the best (well, in my humble opinion) portions of the world’s two widely used operating systems – Mac OSX’s dock (which I had from Cairo Dock) and Window’s window list (a feature of Gnome 2). I thought it would last forever, but my appetite and curiosity for open-source progression made me jump the gun; I wiped my current installation of Ubuntu 10.10 and dove into 11.04.

Big mistake, well, almost. What I didn’t know of was Gnome’s 3.0 project (which I’ll talk about later). Ubuntu had different views from Gnome and thus, took a different approach and started a project known as Unity.

When I started using Ubuntu, I never thought of it as the most innovative project. Coming from Window’s XP, it was really different, but not in a bad way. Gnome 2 had relocatable panels that weren’t constricted to one “bar” (unlike Window’s). Like Window’s there was a window list, clock, and application menu. However, there were additional items that could be added and moved about. It felt scary, but exciting, to use an operating system that most people never used or heard about. I started to use WINE for applications like Microsoft Office 2007 in order to edit my documents from school without having to worry about formatting issues. I didn’t really use any other Window’s application; I mean, I had a fucking netbook; there’d be no way I could play any major games, even if I did boot up into my Window’s XP paritition! What I liked about Ubuntu wasn’t so much the applications that they had, but the flexibility and slick looks of the shell.

But WTF was up with Unity?

As soon as I logged in, the first thought that came to mind was “HEEEEELLLLLL NO!” I logged out, and thank the developers for the option, ended up using the classic Gnome 2.0 shell. So let me skip to 11.10.

I thought Unity would get better. I mean, it did get better and somewhat cooler. But it was as useless to me as 11.04’s version. To summarize the look and feel of the interface, I would say it was a cross between a tablet and desktop. At first, you would think that was a great idea. Well, imagine something like ADW Launcher’s dock from android 2.3.x. Now imagine a giant version of that dock pasted to the left side of the screen. There’s no window list like in android like in the Window’s environment or Gnome 2.0 shell. The dock is fixed to the left side and cannot be moved (at least to my knowledge and research from online) or removed. The dock could only be so small; eventually, there’s a whole bunch of crap going on in the dock/launcher – mounted drives, frequently used, but unopened applications pinned to the launcher, and the applications that aren’t pinned, but are still in use. The excess get folded up into a mess and you have to literally scroll through the mess to switch to a window if you’re too lazy to use alt-tab.

But I held in my feelings and sucked it up. For a while, I actually used Unity. The lenses they used for searching was useful. To be honest, I never did use it for anything but to quickly type the first few letters of “firefox” or some other application to launch it. Launching applications was easier from the keyboard, but it made switching windows from the mouse a bit harder. I had to install AWN and use it as a window list. It worked on my netbook, but I missed having my top panel used as the window list and having a bottom dock as a launcher.

So around X-mas, I decided to get a new laptop. I liked my netbook (it was an eeePC 1000HE), but damn, IT WAS SLOW! I didn’t wake up early enough to Black Friday so I missed some good deals for a better one. But, after surfing slickdeals.net, I found a good deal for a 14″ hp laptop for $500 (including tax and everything) with a i5-2430m and a decently big harddrive (I know I’m on it, but I’m too lazy to check). Unfortunately, the first laptop had non-working speakers, so I had to wait a weekend to exchange it.

So with my new laptop in hands (or more accurately, on my lap), I installed Ubuntu 11.10. Asides from a few issues that were fixed by adding Jupiter, Linux kernel 3.0.0-15, and some stuff to grub for booting, everything was fine. I could have stuck with Unity, but it just looked weird on a bigger screen. So I installed Gnome 3 and gave that a shot.

I tried Gnome 3 when I had Ubuntu 11.10 on my netbook. I couldn’t stand it with the lack of extensions. On my new laptop, I got it to a point where I was satisfied with how it looked after installing a “few” extensions. I’m disappointed that it’s not as easily configurable as Gnome 2, that the notification/system tray is at the bottom, and that Compiz doesn’t work! There was a window list extensions that I tried and It looked nice. But I didn’t use it mainly for two reasons – first, it’s a bit buggy (that is, I see a button for close windows) and I can’t move the buttons (although, this reason doesn’t really matter I guess since 99% of the time, I never move them). Because of that, I’m stuck at the moment with AWN, which is fine.

Well, hopefully Gnome and Unity will get better.