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
Advertisements

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.