Ubuntu 12.04: Sound Toggle 1.3 – HDMI sound toggle/switch

So I did a few edits to my script that allows me to:

  • Enable/disable the lockscreen. That way, I can close my laptop without having the lockscreen coming up.
  • Restore the backlight level based off of set values.
  • Added notifications.

Script:
#!/bin/bash
# Sound Toggle
# By Charles Cruz
#
# The following script toggles the between laptop speakers and hdmi audio (if detected).
# This also disables/enables
# Version 1.3
 
########### Settings ###########
# Backlight Settings
BACKLIGHT_BATTERY=20
BACKLIGHT_AC=100
 
USERID="$(cat /var/run/ConsoleKit/database | grep -B 6 is_active=true | grep uid= | cut -f 2 -d '=')"
USER="$(grep $USERID /etc/passwd | cut -f 1 -d ':')"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"
GNOME_SCREENSAVER_PROC=`ps xa | grep gnome-screensaver | head -n 1 | awk '{print $1}'`
 
export `grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SCREENSAVER_PROC/environ`
export XAUTHORITY="/home/$USER/.Xauthority"
export DISPLAY="$(cat /var/run/ConsoleKit/database | grep x11_display= | cut -f 2 -d '=')"
 
# HDMI is connected
if [ "$HDMI_STATUS" = connected ]; then
# Send notification
sudo -u $USER notify-send -i display "HDMI connected"
 
# Set the sound card profile
sudo -u $USER pactl set-card-profile 0 output:hdmi-stereo+input:analog-stereo
 
# Disable the lockscreen if it's enabled so you can close the lockscreen
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = true ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled false
fi
 
# HDMI is not connected
else
# Send notification
sudo -u $USER notify-send -i computer "HDMI disconnected"
 
# Set the sound card profile
sudo -u $USER pactl set-card-profile 0 output:analog-stereo+input:analog-stereo
 
# Restore the lock setting, if necessary
if [[ $(sudo -u $USER gsettings get org.gnome.desktop.screensaver lock-enabled) = false ]]; then
sudo -u $USER gsettings set org.gnome.desktop.screensaver lock-enabled true
fi
 
# 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
else
xbacklight -set $BACKLIGHT_BATTERY # Power cable is not connected
fi
fi
 
exit 0

BTW, I renamed the script to “toggle-sound” so if you used a different name, be sure to change the name in the instructions below.

Instructions:

  1. In a terminal:
    sudo gedit /usr/local/bin/toggle-sound
  2. Copy and paste the script above into the document. Save and close it.
  3. In the terminal:
    sudo chmod 755 /usr/local/bin/toggle-sound
     
    echo 'KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/toggle-sound"' | sudo tee /etc/udev/rules.d/hdmi.rules
     
    sudo udevadm control --reload-rules

Use Notify-OSD in Gnome 3.4

With Gnome 3, I had two major pet peeves – the lack of a native window list and the message tray. I was able to fix the first problem with the panel docklet and a bit of theming. I was also able to fix a part of the second problem with the classic system tray extension. But there was still something that bothered me with that second annoyance; notifications! With the shell osd extension, I was able to achieve something similar to that of Notify-OSD, but it would always pop up the little message tray at the bottom of the screen. So annoying, especially if you used a dock like I did! I tried searching for a way to get notify-osd to run, but I couldn’t find anything. So I gave up…

But a few months later, I saw a blog post on Web Upd8 about Linux Deepin. The Linux distro was great; if I fucked up my install of Ubuntu (again) and had to install an OS, then I would have probably install Linux Deepin. I did try it via live usb session, and let me tell you, it looked pretty damn good! I mean, the default theme wasn’t my favorite; it looked like a mix between Windows and Macs. However, I still think it looked 100x better than the default the default Gnome 3 theme. I mean seriously, the default Gnome 3 looks ugly; if there wasn’t any themes and extensions to compensate for the lack of default functionality (which, I’m assuming, works for anyone who’s amazing with the keyboard and their shortcuts) and sexiness, I would have probably stopped using Linux distributions and go back to Windows (because I can’t afford a damn Mac). Well… that’s lie (probably); I may have switched over to KDE…

So I attempted to do what they did. I tried deleting notificationDaemon.js from /usr/share/gnome-shell/js/ui and commenting out any instances of it from main.js, but that wasn’t enough! It stopped notifications from working, but Notify-OSD wouldn’t work either. I asked on the Linux Deepin forums and it seems they compiled their own version of gnome-shell…

Luckily, they had their own packages for Gnome 3.4!

I already had gnome-shell installed and notify-osd since I was using Ubuntu. From:
http://packages.linuxdeepin.com/deepin/pool/main/d/deepin-gnome-shell/
I downloaded gnome-shell_3.4.1-1deepin12_amd64.deb (since I’m using a 64-bit version of Ubuntu) and gnome-shell-common_3.4.1-1deepin12_all.deb.

Before I did anything, I copied /usr/share/gnome-shell/js to my home folder. They changed a bit of the layout in the Linux Deepin version, which I liked. However, I had a problem where the top panel, Windows Button, Applications Button, and Search Box in overview overlapped. To fix this, I just copied the original overview.js in ~/js/ui (assuming the js folder was copied to the home folder) back into /usr/share/gnome-shell/js/ui after installing the Linux Deepin version.

Anyway, with synaptic, I removed gnome-shell and gnome-shell-common. No other packages were removed. I did this in a Unity session because I didn’t know what would happen if I was in Gnome Shell.

With Gdebi, I installed gnome-shell-common_3.4.1-1deepin12_all.deb and then gnome-shell_3.4.1-1deepin12_amd64.deb. Not sure if it’ll work in the Ubuntu Software Center, but it should.

Afterwards, I logged out and logged back into Gnome Shell. Opened a terminal and typed “notify-send test” and it worked!

Again, if you have a problem with the overview, then just copy and replace the overview.js from the original gnome-shell into /usr/share/gnome-shell/js/ui. Or, if you want the original gnome-shell look , just replace the js folder in /usr/share/gnome-shell with the original, but delete the notificationDaemon.js and main.js from the original js folder.

Ubuntu 12.04: SToggle – HDMI sound toggle/switch

I’m a newb when it comes to scripting and pretty much anything Linux related. But even so, I’m a bit proud of this script because now I can toggle hdmi audio on my laptop from either my main user account or guest account (that, and the fact that I spent 5 hours trying to find a way to get the script to work)! 😀

So, apparently my udev rules were working. The problem was the script. I learned that running scripts in a terminal, in the background, or by udev isn’t the same; environment variables are different and when you use ‘who’ or ‘w’ in a script that isn’t running in the terminal, it’ll display nothing. I guess in older versions of Ubuntu, when you used
USER="$(who | grep :0 | cut -f 1 -d ' ' | head -n 1)"
a line would show for the gnome-session (tty7)? But as far as I can tell, it wasn’t showing in Ubuntu 12.04 (and apparently 11.10 because in my Ubuntu 11.10 VM, it didn’t show up either). Technically, the script (MToggle) works if you had a terminal opened on TTY opened (probably why I thought it worked). But since I had a terminal opened at most times, I just assumed the script worked.

In 11.10 and 12.04, you could get around this by just putting your actual username for USER, but then it wouldn’t work if you had other users that used the same computer. But then again, the script was made for laptops in mind and I doubt it would work for desktops because it depends on the change of hdmi status… I’m not sure.

But what about guest accounts? What about changing usernames? I didn’t like the fact that I was just using my username in the script to get it to work. So I started searching the filesystem for anything that could tell me about the active user logged on. Eventually I found a file called “database” in “/var/run/ConsoleKit” which had info about “x11_display”, which one was active, and the UID of that corresponded to the displays. So the script:

#!/bin/bash
# Sound Toggle
# By Charles Cruz
#
# The following script toggles the between laptop speakers and hdmi audio (if detected).
# Version 1.0

USERID="$(cat /var/run/ConsoleKit/database | grep -B 6 is_active=true | grep uid= | cut -f 2 -d '=')"
USER="$(grep $USERID /etc/passwd | cut -f 1 -d ':')"
HDMI_STATUS="$(cat /sys/class/drm/card0-HDMI-A-1/status)"

if [ "${HDMI_STATUS}" = connected ]; then
sudo -u $USER pactl set-card-profile 0 output:hdmi-surround
else
sudo -u $USER pactl set-card-profile 0 output:analog-stereo+input:analog-stereo
fi

exit 0

Steps:

  1. In a terminal:
    sudo gedit /etc/udev/rules.d/hdmi.rules
  2. Copy, paste, then save:
    SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/SToggle"
  3. In the terminal:
    sudo udevadm control --reload-rules
  4. In the terminal:
    sudo gedit /usr/local/bin/SToggle
  5. Copy and paste the script above and then save it.
  6. In the terminal:
    sudo chmod 755 /usr/local/bin/SToggle

And that should do it! Hope it works!

Ubuntu 12.04 and udev… weird….

I jumped the gun and updated to 12.04 a few weeks ago. Everything’s great and I’m somewhat settled in. I like how the screen toggles now to an external monitor when connected via HDMI automatically. However, I realized that the sound profile still doesn’t toggle with it. No biggy; just had to edit that old script I had earlier and add the udev rule.
However, after testing, I realized it wasn’t toggling automatically; I’d plug in my hdmi cable, check the sound profile via sound settings, and it’s still stuck on my laptop speakers! Was it my script? Couldn’t have been; I tested it and it did change the sound profile. My only guess was udev.
Using:

udevadm monitor

I checked to see if I messed up my udev rule. Now here’s where things get weird. As I monitored udev, my script magically worked! But after I closed out the terminal, it stopped working… I’m confused… I need to look into this more.

Ubuntu 12.04 – Should I Upgrade?

I’m contemplating on whether I should upgrade to 12.04 or not. I’m already 100% sure that I won’t be using Unity, but the possibility of having Gnome 3.4 is making me a little curious. Should I jump the gun? I really don’t notice much of a difference when I upgrade and I’m a bit scared of upgrading from a previous version; usually I just wipe Ubuntu and reinstall. But that would mean reinstalling every application, reconfiguring settings, redownloading gnome extensions, etc. Also, I’m using VMware Player and I’m not completely sure how that’ll turn out. I guess I’ll try it out in June when my spring semester ends and the need for a Windows VM becomes unnecessary.

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.
/etc/udev/rules.d/hdmi.rules
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):
/usr/local/bin/MToggle-udev
#!/bin/sh
#
# 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
DP="DP1"
VGA="VGA1"
HDMI="HDMI1"
INTERNAL_DISPLAY="LVDS1"
# 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
BACKLIGHT_BATTERY=15
BACKLIGHT_AC=50
# Do no change!
EXTERNAL_DISPLAY=""
# Check to see if the external display is connected
if [ "${DP_STATUS}" = connected ]; then
EXTERNAL_DISPLAY=$DP
fi
if [ "${VGA_STATUS}" = connected ]; then
EXTERNAL_DISPLAY=$VGA
fi
if [ "${HDMI_STATUS}" = connected ]; then
EXTERNAL_DISPLAY=$HDMI
fi
# 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
if [ $EXTERNAL_DISPLAY=$HDMI ]; then
sudo -u $USER pactl set-card-profile 0 output:hdmi-surround
fi
# The external display is not connected
else
# 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
else
xbacklight -set    $BACKLIGHT_BATTERY    # Power cable is not connected
fi
# Restore laptop sound profile
sudo -u $USER pactl set-card-profile 0 output:analog-stereo+input:analog-stereo
fi
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.