Archive for the ‘ Projects/How-To’s ’ Category

How To Remap Caps Lock To Escape

So I think we can all agree that whoever decided the caps lock key should get prime real estate should be stoned.  Seriously, it’s right where an extremely useful key could be placed…like escape!  Keys that can be toggled on and off, especially by software means, should not be given such huge buttons placed where other useful buttons should be in the first place.  I am a heavy Vim user, so I use escape A LOT to change modes.  If you’re an emacs *shuddersss* user you can remap this key to control but I am not going to tell you how as you should be using Vim anyway :p  I’m just kidding, it’s a simple word substitution I will show in a minute.  In order to get this setting to effect both console and X usages we will need to edit two different sets of files, both of which I will demonstrate.  This is a Linux/UNIX specific tutorial so apologize to you poor Windows and OS X users who are stuck out in the cold.  If it is demanded, I can show how to do this in OS X as it is a pain in the ass, but unfortunately I have no idea how to do this in Windows.  Sorry about your luck   :-|   Anyways…

In order to get this to work in the console for all users, stick this in your /etc/rc.local (this is an Arch Linux example, you just need this to be in a file that will be ran as root during startup):

(echo `dumpkeys | grep -i keymaps`; echo keycode 58 = Escape) | loadkeys -

This should make it so Caps Lock is remapped to escape at all times in the virtual consoles. If you wanted it to be remapped to control, just substitute “Control” for “Escape” there towards the end…simple enough.  Or you could just use Vim :)

In order for this to take effect in X, I use Xmodmap to remap the keycodes. The way I do it is to stick these two lines in the file ~/.Xmodmap:

remove Lock = Caps_Lock
keysym Caps_Lock = Escape

Then in my .xinitrc have a line that says:

xmodmap /home/al3k/.Xmodmap &

You can run that command in the console and, since it will be run at every boot, it will be a persistent setting. Having escape at a much more reachable position makes productivity soar…at least for me. It allows me to Vim and Vimperate at a much faster speed. Hope this helps! Post comments if you have any questions, I am happy to help.

Toggling Drivers With Keyboard Shortcuts

I have a Macbook Pro which has a large touchpad that is really sensitive so its always clicking on stuff while I am typing, which is extremely annoying. I have tried going the syndaemon route to disable the touchpad while typing, but it is very unreliable in my experience and I’d rather go about doing this without another process running in the background. This is a Linux specific tutorial, so if you want to know how to do this on Windows or OS X I apologize as this will not work for you. I do most everything in a terminal except web browsing (and even then I use elinks unless I need flash or fancy javascript), DWM is my window manager and I use vimperator with Firefox so I hardly use my mouse as it is.  Who needs a mouse when I already have 100 buttons at my disposal?  Anyway, I was looking for a way to quickly toggle my touchpad driver on and off so I can use it when I need to and then quickly get rid of it again.  While this guide is specifically for DWM and my Macbook touchpad, if you alter the scripts for your particular drivers and set up the keyboard shortcuts with whatever window manager you use, this guide should be fairly universal.

Here is a simple bash script that will determine whether a module is loaded and insert/remove accordingly:

#!/bin/bash
if [ $(lsmod | grep bcm5974 | wc -l) = "0" ]
then
# mouse driver not found, need to load
modprobe bcm5974
swarp 512 300
else
# mouse driver found, need to remove
swarp 9999 9999
rmmod bcm5974
fi

I have this script located in /usr/local/bin/toggletp.  bcm5974 is the name of the macbook’s touchpad driver and swarp is a program that will move your cursor to specific coordinates on the screen.  Unfortunately there is no way to disable the mouse cursor in X so my solution was to move the cursor to the very bottom right corner of the screen where it can’t be seen when the module is disabled.
Now we are going to need a simple sudo wrapper since modprobe/rmmod require root access:

#!/bin/sh
sudo /usr/local/bin/toggletp

Now in order for you to be able to execute this script requiring root access without a password, you will need to edit the sudoers file to allow this. I tried using setuid originally until I realized that setuid doesn’t work on shell scripts :(

Here is the line I use in my sudoers file:

%wheel ALL=(root) NOPASSWD: /usr/local/bin/toggletp

Wheel is just a group that my user belongs to. This is just the bsd style way of managing root access. This line allows you to execute the toggletp script as root without a password. Make sure that you set the executable bit on these scripts and lock down access since they can execute commands as root without passwords.

Now, these are the relevant lines from my DWM config.h so set the keyboard shortcuts for my scripts:

/* commands */
static const char *dmenucmd[] = { "dmenu_run", "-fn", font, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL };
static const char *termcmd[]  = { "urxvt", NULL };
static const char *tpcmd[] = { "/home/al3k/build/toggletp", NULL };
static const char *mpdpause[] = { "mpc", "toggle",NULL };
static const char *mpdnext[] = { "mpc", "next", NULL };
static const char *mpdprev[] = { "mpc", "prev", NULL };

static Key keys[] = {
/* modifier                     key        function        argument */
{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
{ MODKEY,                       XK_Return, spawn,          {.v = termcmd } },
{ MODKEY,                       XK_z,      spawn,          {.v = tpcmd } },

The third line and the last line are the chunks relevant to this guide. Copying these lines into your config.h will make it so that every time you press MODKEY+Z, it will execute the sudo wrapper for toggling your driver (mine is located in the build directory in my home folder). Now recompile dwm, reload, and , if everything worked out correctly, you should be able to toggle your laptop touch pad on and off at will with a simple keyboard shortcut!

Leave comments if you have any questions, I’m happy to help. Also, I cannot take credit for the touchpad driver bash script, a friend of mine came up with it and I modifed it for my use.

Media Keys with MPD and DWM

To continue on my last post about mpd, I went on to figure out how to get my Macbook Pro’s media keys working with MPD and DWM. To catch those up who don’t know the acronyms, MPD is Music Player Daemon which is just a daemon that runs in the background that plays music and is controlled by clients (my personal favorite is ncmpcpp). DWM stands for Dynamic Window Manager, which is a super minimalist tiling window manager that is customized entirely by editing its source code and is also characterized by being super freaking awesome. This technique should work for any extra keys you may have on your keyboard and not necessarily just Macbooks.

What you’re going to need: xmodmap (to map keycodes to octal codes so dwm doesn’t bitch), xev (to get the correct keycodes for the keys you want to use), and mpc (just a simple cli client for mpd that takes simple parameters).

Here we go:

Use this snippet to start xev and get only the info that we need:

xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

Once that has started, the press the keys you want to use for play, next, previous etc and write down the corresponding keycodes so you remember.

Use your favorite editor to open up .Xmodmap (name doesnt really matter, this is just what I use) and insert something to this effect:

remove Lock = Caps_Lock
keysym Caps_Lock = Escape
keycode 171 = F30
keycode 172 = F31
keycode 173 = F32

Map whatever your keycodes might be to F and then some incremental number (F30,31,31, etc etc. I just used arbitrary numbers). You can ignore those first two line, I use them to remap my capslock key to escape (which is extremely useful if you’re a vim user…or you just realize how useless capslock is).

The run this to make the keymaps stick:

xmodmap ~/.Xmodmap

In order to make that change permanent you will need to add that line to your .xinitrc as well.

Now to editing the dwm code. These are the relavent lines from my config.h:

/* commands */
static const char *dmenucmd[] = { "dmenu_run", "-fn", font, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL };
static const char *termcmd[]  = { "urxvt", NULL };
static const char *tpcmd[] = { "/home/al3k/build/toggletp", NULL };  
static const char *mpdpause[] = { "mpc", "toggle",NULL };
static const char *mpdnext[] = { "mpc", "next", NULL };
static const char *mpdprev[] = { "mpc", "prev", NULL };

static Key keys[] = {
    /* modifier                     key        function        argument */
    { MODKEY,                   XK_p,        spawn,          {.v = dmenucmd } },
    { MODKEY,                   XK_Return, spawn,          {.v = termcmd } },
    { MODKEY,                   XK_z,        spawn,          {.v = tpcmd } },
    { 0,                            XK_F31,     spawn,          {.v = mpdpause } },
    { 0,                            XK_F30,     spawn,          {.v = mpdnext } },
    { 0,                            XK_F32,     spawn,          {.v = mpdprev } },

You will want to add the mpc and mpd lines exactly as they look there, carefully minding the ,’s and ;’s since this is C code. The 0′s where the modkey should be just mean that there is no modifier key required. Recomplie DWM, reload, test and voila! The keys you mapped should now control mpd as you wish!

Leave some comments if you have any questions, I’ll be happy to help.

High CPU Usage with MPD in Arch Linux x86_64

As an Arch Linux lover, I love all things minimalist. I have recently fallen in love with MPD (Music Player Daemon) due to the fact that I am prone to dropping out of X, working on something (usually something that I broke :p), and then jump starting X once more. MPD allows me to do this and keep on jammin’ to my tunes. Unfortunately, I have encountered a problem where, when updating the database (and sometimes at other times), MPD procedes to eat both of my cores and then all of my memory and swap, resulting in a segfault. This is highly upsetting. Turns out, the culprit is an ancient version of libid3tag in the Arch repos that is causing all of the issues. libid3tag gets hung up on certain id3 tags and then just gets confused and goes on a cpu and memory stealing rampage. I found an bug post just earlier this month on the Arch site documenting this issue and I was glad to find out that I was not the only one with this issue. The gentlemen was kind enough to post a PKGBUILD that included some patches that, ironically, came from Debian repos. I thought Arch was supposed to be the bleeding edge one??!! :p I have posted a link to the bug report which has the PKGBUILD attached near the bottom of the page and I am proud to report it has solved my issue. Kudos to honki for fixing this for us!

http://bugs.archlinux.org/task/18595

Nicklist in Irssi

It annoyed the piss out of me that irssi doesn’t have a list of nicks in a given irc room.  Granted you can just /who but this just seemed like too much work given that programs such as weechat (which I have since swtiched to) have a nicklist right out of the box.  If, for whatever reason, you decide that weechat just isnt for you and you wish to have a nicklist in your irssi, then follow these simple steps.

grab the nicklist script from irssi.org

wget http://scripts.irssi.org/scripts/nicklist.pl

then move said script into your irssi scripts folder

mv nicklist.pl ~/.irssi/scripts

then fire up irssi and issue these two commands:

/script load nicklist
/nicklist screen

Presto!  now you have a nicklist inside of irssi.  Beware of this script however, it does not play well with certain terminal,mrxvt for instance.  There are ways to autoload scripts in irssi but I honestly didn’t use it long enough to be moved to figure it out.  I suggest you check out weechat.

Twitter from the command line!

While it is just as easy to go to twitter.com and update your status, it is just way freakin’ cooler to do it from the command line.  Now I will have to disappoint you Microsoft loyalists and say that this only works in Linux.  My bad.  Anyways, I take no credit for this script at all, I am just informing you of it because it doesn’t show up many places and I thought it was a pretty useful script.  The downside to this little piece of code is that you have to imbed your username and password inside the script itself, which most of the time is a bad idea, but if you’re willing to take the risk, it’s pretty damn sweet.  To do this, just paste this code into /usr/bin/twitter (which you will need to create):

curl --basic --user "USERNAME:PASSWORD" --data-ascii "status=`echo $@|tr ' ' '+'`" "http://twitter.com/statuses/update.json"

Obviously, you will need to replace the capitalized letters with the info required. Then you will need to make this script executable via chmod +x /usr/bin/twitter.  Now to reap the rewards.  Bust out a terminal and type in the work twitter, then write anything you want, that happens to be less that 160 characters long, and it will update your twitter account with whatever you typed after the word twitter.  You are now so much cooler than you were before that it is actually making my brain hurt but, unfortunately, as cool as this may be, it will not get you any chicks.  Enjoy![cc lang="bash"]

The Magic of Xwinwrap

So one thing I miss about windows is the Active Desktop, or at least some features from active desktop.  You can just drop cool animated HTML files into the active desk and it just plain works.  Unfortunately, this is not as easy in linux.  Enter a small program called xwinwrap.  This nifty little bugger lets you run pretty much anything you can think of as a desktop background.  You can even run things like the Quake 3 demo as your background, how freakin’ awesome is that.  The instructions that follow I know for a fact works in Ubuntu Hardy and Intrepid.

1. Copy and paste this into your terminal to satisfy the needs of xwinwrap

apt-get install build-essential libx11-dev x11proto-xext-dev libxrender-dev libxext-dev cvs

2. Now copy and paste this in order to get the xwinwrap source code

cvs -d :pserver:anoncvs@cvs.freedesktop.org:/cvs/xapps co xwinwrap

3.  Now here’s how to compile, type these both into the terminal

cd xwinwrap

and then….

make

4.  This is optional but if you do this it makes the xwinwrap command easier to get to

cp xwinwrap /usr/bin

Now you can do some really cool stuff with this program like play movies, games, programs, screensavers all as the desktop background

Run this in the terminal and witness its coolness. Check out the xwinwrap man page for all the command

xwinwrap -ni -o 0.40 -fs -s -sp -st -b -nf -- /usr/lib/xscreensaver/glmatrix -root  -window-id WID

This will run the matrix screensaver as your background.  Just fool around with the commands and you can run pretty much anything with this sucker.

I waited until the end to include this for you lazy bums who don’t want to compile yourself.  Here is a link to a deb package that will just install it for you.  I did this for your own good b/c compiling your own software is good practice.

Here’s a video to show you what the final product looks like

How to use MySQL for Amarok music database

Amarok is an awesome music program that any tunes junkie should get there hands on.  While I realize that there are a ton of awesome Open Source music players, I just really think that Amarok rocks so I am going to show you a way to make it work even better.  Amarok supports SQLite out of the box, which does its job, but it is not nearly as fast as MySQL in handling large music collections.  I myself have a giant collection, so I need a database engine that can handle the load.  Amarok 2.0 has since been released and it has built-in MySQL support, so no extra setup is necessary.  While this is convenient, the standard repository version of Amarok for most distributions is still, and probably will always be, 1.4 ish.  With that in mind, I am going to give some instructions on how to utilize MySQL for this version.  First off, this guide is for linux users only. Again, my bad.  Secondly, you will need the MySQL server and client packages, which can be obtained with the simple line “apt-get install mysql-server mysql-client” without the quotes of course.  Now that you have the packages you will need to create an Amarok database within MySQL and create a user “amarok” that the program will use to access and maintain the database.  This can be done with the following lines:

$ mysql -p -u root   <<this will get you into the MySQL prompt (you will need to enter the root password that you set up during the isntall process)

now use these lines to set up the database:

CREATE DATABASE amarok;

USE amarok;

GRANT ALL ON amarok.* TO amarok@localhost IDENTIFIED BY 'PUT WHATEVER PASSWORD YOU WANT HERE';

That should get you all ready with a shiny new database and database user that amarok will be able to exploit.  Note: obviously you will need to change the password abaove. I used the obnoxious capital message to catch you attention on purpose.

Now that you have a database, you will need to configure amarok so it can utilize it.  Open up amarok and go to Settings > Configure Amarok > Collection.  You should now see a dropdown box that says “SQLite”.  Change that to MySQL and then the hostname should be localhost, the username should and database should be amarok and the password should be whatever you decided to replace the password with in the code above.  Now hit apply and ta-da!, amarok will be using MySQL as its database engine.  Enjoy!

How to alleviate screen tearing in with Nvidia Linux drivers 180.xx and above

Let me begin first by describing what “screen tearing” is.  Screen tearing happens when you scroll, refresh a page, move to a new page, or something of the like and the screen sort of “tears” and doesn’t refresh all the way, or it artifacts and doesn’t move in some spots.  The screen will update as soon as you click in another spot but it is just extremely annoying when you are reading an article and part of the screen doesn’t scroll.  You will end up clicking several hundred times to get the screen to refresh and I can say that it is just really, really annoying. I am not sure if this is specific to a certain card, I have a GeForce 8400M by the way, or what but my Google searching seems to allude to the fact that it affects several different lines. The only way I have found to fix this problem is to downgrade to the 173.xx driver series, and then the problem will go away (at least it did for me).  I am one who loves having the cutting edge software and all the new bells an whistle, but the screen tearing is just too much to handle.  The direct link to the latest release of the 173 series is below, I hope this solves everyone else’s problem as well.

Nvidia Linux Latest 173 Series Driver (this is the x86 architecture version, so if you have a 64 bit chip you will have to navigate to that version)

Get your stolen bandwidth back

If you are a windows user there is something you should know, you are not getting all of your maximum bandwidth capacity out of your computer.  Windows had this nifty little setting that reserves as much as 20% of your bandwidth for QoS (Which stands for quality of service.)  This should make you angry, at least it made me angry.  I will now teach you how to get that bandwidth back.  As a last note, this only works on XP Professional.

1. Click Start

2. Click Run

3. Type in “gpedit.msc”  and hit OK

4. In the Group Policy window, expand Computer Configuration, and then expand Administrative Templates. Expand Network and click on QoS Packet Scheduler.

5. Click on limit reservable bandwidth in the right window

6. Select the Enabled option and then type zero in the bandwidth box then hit apply and click that OK button

7. Go ahead and restart the computer for good measure and all should be good

You may be wondering why not disable the bandwidth?  Well the stupid thing about this setting is that the only way to get your bandwidth back is to enable the setting and set it to zero.  I know what youre thinking, that’s stupid. I thought so too.