Forgot your password?
typodupeerror
KDE

Journal: X/KDE/ALSA Trick 6

Journal by gameforge

Well, you asked about non-KDE tricks as well; this is partly a trick w/ KDE and partly a trick w/ X, and also w/ ALSA.

I've been using Linux and X-Windows for a fairly long time, as well as KDE (since 1.x). One thing that always bothered me was that I couldn't set up the Windows key exactly the way that it works in Windows. It keeps wanting to treat it as a meta key; KDE's 'Keyboard Shortcuts' control panel in the control center wouldn't let me set it up identical to Windows. It's certainly possible I did something wrong, but I sure played with it long enough...

Further, I have a Logitech Cordless MX Duo; it's a wireless keyboard & mouse. As most Logitech keyboards do, it has Internet buttons and multimedia buttons all over the place; there's also WWW back & forward thumb buttons on the mouse, which are quite convenient under Firefox/Windows.

So, after years of living without, I discovered the 'xmodmap', 'imwheel' and 'xev' commands. To get the mouse forward/backward buttons to work correctly, I execute a simple script called 'setup_mx700' when loading X:


#!/bin/sh
xmodmap -e "pointer = 1 2 3 6 7 4 5"
imwheel -f -k -p -b "67"

Then, in the imwheelrc file, I have this:

"(null)"
None, Up, Alt_L|Left
None, Down, Alt_L|Right

".*"
None, Up, Alt_L|Left
None, Down, Alt_L|Right

This tells X how many buttons are on the mouse, and in which order they are coded. Both imwheel and xmodmap have man pages; you can check them out if you have a different scenario, possibly if your mousewheel and/or middle buttons don't work, etc. Essentially, the back/forward buttons map out to 'Alt+Left' and 'Alt+Right', and the mouse wheel maps to 'up/down' (in case the program I'm using doesn't support mouse wheels). This will certainly vary from setup to setup, which is why I refer to the manpages.

Now for the keyboard issue. As I said, the Win key wouldn't work correctly (probably a messed up X setting somewhere), and none of the Logitech MM and WWW buttons would work. Using 'xev', I mapped out which keys correspond to which key codes. So, I have another script, called 'setup_iTouch' run when I start X, right after 'setup_mx700':

#!/bin/sh
xmodmap -e "keycode 115 = F13"
xmodmap -e "keycode 117 = F15"
xmodmap -e "keycode 160 = F14"
xmodmap -e "keycode 174 = F16"
xmodmap -e "keycode 176 = F17"
xmodmap -e "keycode 164 = F18"
xmodmap -e "keycode 162 = F19"
xmodmap -e "keycode 153 = F20"
xmodmap -e "keycode 144 = F21"

Once again, different setups will have different keycodes; use 'xev' to capture your own keyboard's proprietary codes.

Then you can go into KDE's shortcut and hotkey control panels, and when you try to assign these keys to an action, they pop up w/ no problems at all (as F13, F14, F15, etc.) I was able to assign the Win key to its proper shortcut, and it now behaves identically to Windows (pushing it twice makes it go away, etc.). The other multimedia/shortcut keys were pretty easy to bind and get them to do what I wanted.

However, I could not think of a program that I could use to simply 'mute' the master sound in ALSA, nor commands to simply increase/decrease the master volume in ALSA, which would retain previous settings & whatnot. So, I wrote yet another three scripts (all five of which I store in /usr/local/bin) that can be assigned to their respective keys in KDE's hotkey manager.

These three use an environment variable, $MVOL, to maintain ALSA's master sound level.

First, 'snd_mute':

#!/bin/bash
MVOL=`amixer sget Master | tail -n 1 | cut -c 18-20`
THIRD=`echo $MVOL | cut -c 3`
if [ "$THIRD" = "[" ]; then
      MVOL=`echo $MVOL | cut -c 1-2`
fi
if [ $MVOL = 0 ]; then
      if [ -a /tmp/mute_vol.tmp ]; then
            MVOL=`cat /tmp/mute_vol.tmp`
      else
            echo 0 > /tmp/mute_vol.tmp
      fi
      amixer sset Master $MVOL
else
      echo $MVOL > /tmp/mute_vol.tmp
      amixer sset Master 0
fi

Then, snd_down:

#!/bin/bash
MVOL=`amixer sget Master | tail -n 1 | cut -c 18-20`
THIRD=`echo $MVOL | cut -c 3`
if [ "$THIRD" = "[" ]; then
      MVOL=`echo $MVOL | cut -c 1-2`
fi
if [ $MVOL -gt 0 ]; then
      MVOL=`expr $MVOL - 3`
      if [ $MVOL -lt 0 ]; then
            MVOL=0
      fi
      amixer sset Master $MVOL > /dev/null
fi

And finally, snd_up:

#!/bin/bash
MVOL=`amixer sget Master | tail -n 1 | cut -c 18-20`
THIRD=`echo $MVOL | cut -c 3`
if [ "$THIRD" = "[" ]; then
      MVOL=`echo $MVOL | cut -c 1-2`
fi
if [ $MVOL -lt 100 ]; then
      MVOL=`expr $MVOL + 3`
      if [ $MVOL -gt 100 ]; then
            MVOL=100
      fi
      amixer sset Master $MVOL > /dev/null
fi

To make it more precise, change

MVOL=`expr $MVOL + 3`

to + 1. To make it less precise, change it to + 5. Same for 'snd_down', but use - 1 and - 5.

It may work (with proper tweaking) with some other keyboards/GUI's, I don't know. Feel free to share modifications, questions, comments, etc. You could make it even more complete and get little overlay displays that tell you what's going on, and you could even make the volume control work for other sliders on the mixer; perhaps if the 'alt' key is pressed, it adjusts the 'Surround' volume, and with the 'ctrl' key pressed it adjusts the 'Bass' level, or something like that.

Machines certainly can solve problems, store information, correlate, and play games -- but not with pleasure. -- Leo Rosten

Working...