Friday, August 24, 2012

Linux on X220: Getting microphone mute button to work

As I promised in last blog post, I am going to write about my experiences using Debian on Lenovo Thinkpad X220 laptop.
This post is not exclusively for X220, it should work on all Thinkpads with microphone mute key.

Thinkpad laptops are known for very good support in Linux, so there is only few things that doesn't work out of the box (if you are using recent enough kernel). In today's post, I'll try to show you how to get microphone mute button (next to volume up/down buttons) to work including its LED.

Microphone mute button is by default mapped to Linux keycode that is out of range. This means you probably won't get any output from xev command when pressing this button.
If you are lucky enough, you will get an ACPI event with acpi_listen and then you can follow this how-to (askubuntu.com).

If you don't get any output neither from xev nor from acpi_listen as me, please follow these steps:

Change udev keymaps for your keyboard:

We will modify udev rules:
mkdir /etc/udev/keymaps
cp /lib/udev/keymaps/module-lenovo /etc/udev/keymaps
Now edit this newly created file and change key name at 0x1A line from micmute to prog2:
0x17 prog1 # ThinkPad/ThinkVantage button (high keycode: "vendor")
0x1A prog2 # Microphone mute
You may also change 0x13 zoom to search if you want to use Fn + Space shortcut. Now you need to reload udev rules, try:
udevadm control --reload-rules
You should now see XF86Launch2 key press in xev. If it doesn't work, just reboot.
You can now assign a keyboard shortcut to this key.

Getting LED to work

Recompile kernel

Unfortunately, you'll need to recompile a kernel to light up this diod. I won't show you how to compile a kernel, please search it somewhere else. You need to enable controlling of unsafe LEDs.
Device Drivers  --->
 [*] X86 Platform Specific Device Drivers  --->
   <M>  ThinkPad ACPI Laptop Extras
        [ ]   Maintainer debug facilities
        [ ]   Verbose debug mode
        [*]   Allow control of important LEDs (unsafe)
        [*]   Video output control support
        [*]   Support NVRAM polling for hot keys

CONFIG_THINKPAD_ACPI_UNSAFE_LEDS=y
After recompiling your kernel you are ready to light up the LED.

Writing script for turning the LED on/off

Lets write a script that will turn it on/off according to first argument and save it as led-micmute.sh:
#!/bin/bash
#
# led-micmute.sh [on|off]
#
if [ "$1" = "on" ]; then 
    echo "Turning on...";
    echo "14 on" > /proc/acpi/ibm/led
elif [ "$1" = "off" ]; then
    echo "Turning off...";
    echo "14 off" > /proc/acpi/ibm/led
else
    echo "Invalid option!"
fi;
However, there is one little issue: you need to run it as a root. It does not really fit our needs to use it from script that will run with user privileges. You can edit sudoers file so you will be able to run a script with sudo without typing password.
Note: I don't know how secure it is, but it works. So please think before copying next lines :-)

Create new file eg. /etc/sudoers.d/tp-scripts with following contents:
# needed to turn on micmute led
your_user_name ALL=NOPASSWD: /full/path/to/led-micmute.sh
You should now run sudo led-micmute.sh without need to type a password.

Create a script for muting microphone

And finally, we will create a script that will mute/unmute microphone.
#!/bin/bash
status=$( amixer sget Capture | grep '\[on\]' )

if [ "$status" = "" ]; then 
    echo "Turning on...";
    amixer sset Capture cap
    notify-send -i microphone-sensitivity-high-symbolic "Microphone" "Microphone is now <b>ON</b>";
    sudo /path/to/led-micmute.sh off
else
    echo "Turning off...";
    amixer sset Capture nocap
    notify-send -i microphone-sensitivity-muted-symbolic "Microphone" "Microphone is now <b>MUTED</b>";
    sudo /path/to/led-micmute.sh on
fi;
There is only one thing left how you can improve this script. If you press microphone mute key repeatedly, you will get multiple notifications on screen informing you about microphone state.
To use single notification that will be updated, please take a look at my notify-desktop app.


2 comments:

  1. 바카라사이트
    바카라
    of course like your web site however you have to take
    a look at the spelling on several of your posts. A number of them are rife with
    spelling problems and I find it very bothersome to inform the truth on the other hand
    I'll certainly come again again.

    ReplyDelete
  2. 토토사이트
    먹튀검증


    That is really fascinating, You're a very skilled blogger.
    I've joined your rss feed and stay up for in search
    of more of your excellent post. Additionally, I've shared your site
    in my social networks

    ReplyDelete