Remapping Your Macbook’s Keyboard in Ubuntu 12.04

I just installed Ubuntu 12.04 (Pangolin) on the Macbook Pro that I got from work. The fact that Apple rearranges the has a different bottom row of keys drives me nuts, though. The placement of the Command keys are where the Alt keys should be, and there is no right ctrl key. My muscle memory is trained for a standard keyboard and fighting it, especially while coding, slows me down a lot.

Luckily, there’s a utility called xmodmap that will let you remap these keys however you want. Here’s what we’re going to do:

Drop the following code into a text editor and save it somewhere. I saved it as .xmodmappings in my home directory:


!
! clean most of the modifiers
clear control
clear mod4
clear mod1

! -----------------
! left side
! ----------------
! keycode 64 is the left alt key
keycode 64 = Super_L
! key code 133 is the left command key
keycode 133 = Alt_L Meta_L

! ------------------
! right side
! -------------------
! key code 134 is the right command key
keycode 134 = Alt_R Meta_R
! keycode 108 is the right alt key
keycode 108 = Control_R

add mod4 = Super_L
add mod1 = Alt_L Meta_L
add mod1 = Alt_R Meta_R
add control = Control_L
add control = Control_R

To test this, drop into a terminal and type:
$ xmodmap ~/.xmodmappings

It should make the following changes:
Left Alt -> Left Super
Left Command -> Left Alt
Right Command -> Right Alt
Right Alt -> Right Ctrl

Now test it out. If everything looks kosher, you can set this to run every time by pulling down the gear/power menu from the taskbar, choosing “Startup Applications”, and adding a new item containing the above command.

Note that this works on a 7th-gen Macbook, and the layouts may differ slightly on other models. If it doesn’t work for you, running xev from a terminal will let you discover the keycodes for your model, which you can substitute into the above script.

Importing iTunes Ratings into Guayadeque

Guayadeque is a nice lightweight music player for linux that supports smart playlists. I wrote a little ruby script to gather all of my ratings from my iTunes database and import them into Guayadeque, so that I can use some of my advanced filters on songs. At some point I’ll probably extend it to import other metadata (last played, date added, etc), but this is a functional first stab at it.

You can download it on Github: iTunes-to-Guayadeque

Using a Dell Mini 10 as an ebook reader in Ubuntu

We just purchased a Dell Mini 10v, and I’ve been quite happy with it so far. One of of the things that occurred to me after holding it, was that it would make a great e-book reader if I could rotate the screen 90 degrees and map one of the edge keys to “turn the page”.

So, I whipped up a little script to do just that. It works very well with any book in HTML format, which means that I can load it in firefox, hit F11 to fill the screen, and read comfortably.


#!/bin/bash
#
# Author: Chris Miller (chrisamiller@gmail.com)
#
# Emulates an ebookreader by rotating the screen 90 degrees and
# mapping the windows key to pagedown
#
case $1 in
  on)
    echo "Mapping Win to PgDn..."
    xmodmap -e "keycode 133 = Next"
    echo "Rotating Screen"
    xrandr --output LVDS --rotate right
    echo "Done."
    ;;
  off)
    echo "Mapping Win back to Win..."
    xmodmap -e "keycode 133 = Super_L"
    echo "Rotating Screen"
    xrandr --output LVDS --rotate normal
    echo "Done."
  ;;
  *)
    echo "Usage: toggles screen rotation and windows key"
    echo "to simulate an e-reader"
    echo "ereadmode (on | off)"
    ;;
esac

To use it, save the script as “ebookmode”, place it somewhere in your path, then make it executable. To start it, type “ebookmode on”. To return to normal operation, type “ebookmode off”.

Update: After upgrading to Karmic, I had to replace “LVDS” with “LVDS1″. You can check to see which output you’re using by typing: “xrandr -q” at the command line.

uTorrent and Ubuntu

RSS feeds plus bittorrent makes auto-downloading video content from the web easy, and keeps our library full of shows to watch through the Xbox. I struggled for a while to find a bittorrent client that was simple and had the features I wanted, though.

Azureus is a bloated mess, Transmission is far too simple, Deluge made feeds a pain in the ass . . . you get the idea. Finally, I found that uTorrent had all the features I wanted in a lightweight, easy to use client. The only problem is, it doesn’t run natively on linux. Thankfully, it runs almost perfectly through Wine, with a taskbar icon and everything.

The only problem I had was that when I clicked on a torrent in my browser, I couldn’t get it to auto-launch in uTorrent. The extra layers added by wine complicated things. I finally solved the problem by writing this little bash script:

#!/bin/bash
cd ~/.wine/drive_c/Program\ Files/uTorrent
echo ""
if [ "$1" != "" ]; then
    dt=`date +%s%N`
    cp "$1" /tmp/$dt.torrent
    var="Z:\\tmp\\"$dt".torrent"
    wine utorrent.exe "$var"
else
    wine utorrent.exe
fi

It takes the torrent file given as an argument, saves it to an accessible location, then opens it using uTorrent. Save the script, make it executable, and then set firefox so that it uses the script to open any torrent files. Problem solved.

Slow internet on Ubuntu 8.10

A week or so after updating to Intrepid on my work desktop, I started experiencing really slow internet connections. (around 100k/s). Tried a different network port – no change. Booted into windows, and got 2M/s. After perusing the ubuntu forums, I found this solution:

edit /etc/sysctl.conf and add:
net.ipv4.tcp_sack = 0
net.ipv4.tcp_window_scaling = 0

edit /etc/modprobe.d/aliases to remove:
alias net-pf-10 ipv6
and add:
alias net-pf-10 off
alias net-pf-10 off ipv6

This turns off IPv6, which seems rather heavy-handed, but disabling ipv6 in firefox alone didn’t fix the problem. I suspect that it was the result of a change somewhere in the school’s network, but dealing with their IT dept is like banging your head against a wall repeatedly. Hope this can help someone else with the same issue.

Rotating One Monitor with Ubuntu

Ubuntu’s support for graphical displays has come a really long way. In Hardy 8.04, most of the configuration is handled through a GUI that works very well, in my experience. There are still a few edge cases that require a little manual configuration, though. Today I ran into one of them.

I have two monitors at work, and today I decided to rotate one of them to a vertical orientation. It’s nice for coding, because I can see many more lines of code at once. The other one I like to keep widescreen. Here’s how to accomplish this using an Nvidia dual-headed card, the nvidia proprietary driver (not nv), and two Dell flatscreen monitors.

Assuming you already have your monitors set up and working with the nvidia driver, start by backing up your xorg.conf. If something goes wrong, you can always restore this and be back where you started.

sudo cp /etc/X11/xorg.conf etc/X11/xorg.conf.bck

Then, fire up nvidia-settings from the terminal. Under “X Server Display Configuration”, make sure that you’re using “Separate X screen” and not “Twinview”. Twinview works great when the monitors are in the same orientation, and even gives slightly better performance, but didn’t allow me to rotate just one of the monitors. Check the box that says “Enable Xinerama”, and then write the changes to your X Configuration file.

Now, for the rotation:

sudo emacs /etc/X11/xorg.conf

In the appropriate “Monitor” section, add the lines:

Option "RandRRotation" "on"
Option "Rotate" "CCW"

Change “CCW” to “CW” for clockwise, instead of counter-clockwise.

Save the file, hit CTRL-ALT-Backspace to restart your Xserver, and you should be seeing the results. Great! Well, almost great…

There’s just one problem. There’s a known bug where gnome-terminal doesn’t work properly when nvidia composite drivers are enabled. The first solution in that thread (disabling the composite) didn’t work for me, so I did the following workaround that sets some environment variables every time the terminal is launched. Hit ALT-F2 and type “xterm” (since we can’t use gnome-terminal), then do the following:

sudo mv /usr/bin/gnome-terminal /usr/bin/gnome-terminal2
sudo emacs /usr/bin/gnome-terminal

In the new file you’re now editing, paste the following:

#!/bin/bash
XLIB_SKIP_ARGB_VISUALS=1 gnome-terminal2 $@

Save the file and make it executable:

chmod +x /usr/bin/gnome-terminal

Now, everything should work correctly.

Update 10/28/08:
Due to popular demand, my xorg.conf is now posted here. It’s important to note that using Xinerama disables the composite extension, so compiz eye candy (aka advanced desktop settings) will not work. Fortunately, I value the setup for coding far more than I value shiny spinny desktop cubes.

|