Bystroushaak's blog / English section / Hardware / Apple keyboard and Linux Mint 13

Apple keyboard and Linux Mint 13

Two days ago I finally received my "new" wireless Apple keyboard I bought on Aukro. Czech Post took its time - 6 days for a distance of 200 kilometers. But hey, what can you expect from a state-owned company. But this blogpost isn't about Czech Post, it is about the Apple keyboard and Linux Mint 13.

The keyboard

I've got only the keyboard. The seller didn't send anything else in the package - no original box, no papers or other usual crap - only the keyboard and two rechargeable NiMH AA batteries.

The keyboard is nice and small, but surprisingly not as small as it looks on the photos. It's slightly bigger than a netbook keyboard on Asus EEE 1000h which I used to write almost a half of my blogs. So far, I've had no problems with the size and I find writing on the keyboard very comfortable.

The next thing that strikes me is its un-bendability. It almost looks fragile. But it is not at all. It's one big piece of solid metal (aluminium). Notebook keyboards of the same size are usually bendable and flexible. This keyboard won't bend/twist even a little bit.

A nice feature is the keyboard's portability. It is small and wireless, so you can just lay in a bed, maximize the font size on your LCD and write from bed. This may be even more useful in combination with projector.

Why Apple keyboard?

My Asus EEE 1000h I mentioned earlier is getting old. I am thinking about buying an older Macbook Air on Aukro. You can buy a used one from 8000 to 12000 CZK (417 - 625 USD at the current exchange rate: 1USD = 19.1CZK).

Practically only other alternative is Zenbook, but they are even more expensive and you can't find them on Aukro that easily.

Two weeks ago, I almost bought one Macbook Air in an auction. Then I realized that I don't really know how comfortable the keyboard is. What if I bought the Macbook Air but the keyboard turned out to be a total disaster? I need to be able to write a lot of text on my netbook. I've decided to buy the external Apple keyboard as a betatest - if I'm satisfied I would buy the Air. If I'm not, I would sell the keyboard back on Aukro - Apple products are in demand so there should be no problem reselling.

Two persons I know use Apple keyboards (Air and an external USB version) and both are praising them, so I decided to give this little experimentthe green light and ordered one practically unused keyboard.

Linux Mint 13

It's not that hard to make keyboard work on Mint 13. Default bluetooth manager is broken and pretty crappy, so you just have to install blueman and maybe gnome-bluetooth from package manager. Blueman is basically the same thing as default bluetooth manager, but not terribly outdated and mostly working. Default Mate applet doesn't work in my environment at all.

Blueman applet can be started from System menu, or as blueman-applet command in terminal. Then you can just connect the keyboard in Setup New Device submenu.

Pairing is simple, you just choose PIN (ala 2222), type it on keyboard and press enter, and everything should work. Thats all.

Layout problems

Default layout sucks and nobody, and I really mean nobody, can ever convince me otherwise. Even if John Carmack came to me and said it's best thing in the world which makes him great programmer, I would tell him to go fuck himself.

It sucks, so lets go to make it better.

Fixing the layout

FN key can't be modified in userspace, because it is not send to X. You have to apply patch to hid-apple module and recompile it. Other keys can be swapped with xmodmap.

There is few notes in ubuntu documentation, but the text is outdated and incomplete.

Source codes

I've put the sources for whole process here: https://github.com/Bystroushaak/apple_keyboard

Compiling hid-apple module

Described process is based on this post: http://ubuntuforums.org/showthread.php?t=2146492&p=12676248#post12676248. There is also source of the patched module, but you have to register there to be able to download it.

I've spend a LOT of time by trying to compile the module this way. Problem is, that Linux Mint 13 uses older kernel than described in post and I never compiled kernel module before.

Whole thing is even more complicated: aptitude downloads only 3.2.0-23 kernel source and I had some difficulties to make it work, because I am using 3.2.0-54-generic. I didn't study it that hard (I was trying to make keyboard work - lot of googling), so there may be some way around it, like using proper headers, or applying some patch which will update it to 3.2.0-54. I've tried some things and failed, mainly because my inexperience with linux kernel and how it should be compiled.

What I found working is to download ubuntu sources from their git repo, run few make commands over them and thats it. When I tried same thing with sources downloaded using apt-get source, it failed with lot of strange, cryptic and basically un-googlable messages from make.

Here is my script, which will clone sources, make branch from tag matching your kernel, replace source with patched one, compile module, load it into memory, and put it into initramfs. It is mostly automatic, you will just have to reply to sudo prompt few times.

#! /usr/bin/env bash
# Apple wireless keyboard module installation script
# by Bystroushaak (bystrousak@kitakitsune.org)
 
if [ ! -d ubuntu-precise ]; then
    git clone git://kernel.ubuntu.com/ubuntu/ubuntu-precise.git ubuntu-precise
fi
 
cd ubuntu-precise
 
# check for changes in repository
git checkout master
git pull
 
# create branch from tag in
VER=`uname -r | sed -e s/-generic//`
BRANCH=`git tag | grep $VER | head -n 1`
 
BRANCH_PRESENT=`git branch | grep "$VER"`
if [ -z $BRANCH_PRESENT ]; then
    git checkout -b "apple_keyboard_$VER" "$BRANCH"
else
    git checkout "apple_keyboard_$VER"
fi
 
# copy patched file
cp ../hid-apple.c drivers/hid/hid-apple.c
 
# make prerequisities
make oldconfig
make prepare
timeout 1m make # just to make all proper tools
 
# compile all modules
cd drivers/hid
make -C /lib/modules/`uname -r`/build  M=`pwd`  modules
 
# copy new module into proper directory
OLDD=`pwd`
cd /lib/modules/`uname -r`/kernel/drivers/hid/
sudo cp hid-apple.ko hid-apple.ko_
sudo cp $OLDD/hid-apple.ko .
 
# config apple keyboard to usable mode
echo "options hid_apple fnmode=2 swapctrlfn=1 use_ejectcd_as_delete=1" | sudo tee -a /etc/modprobe.d/hid_apple.conf
 
# reload driver
sudo modprobe -r hid_apple
sudo modprobe hid_apple
 
# put driver into kernel
sudo update-initramfs -u

xmodmap

After you get the hid-apple module working, you will have to change behavior of other keys on keyboard. That can be easily done using program called xmodmap, which allows to change layout of keyboard.

Here is content of .xmodmap file, which you are expected to put into your home directory:

! define left and right ctrl
clear control
keycode 37  = Control_L NoSymbol Control_L NoSymbol Control_L
keycode 108 = Control_R NoSymbol Control_R NoSymbol Control_R
add control = Control_L Control_R

! disable nonbreakable space
keycode 65 = space space

! switch right cmd with alt/ISO_Level3_Shift
clear mod4
clear mod5
keycode 134 = ISO_Level3_Shift
add mod5 = ISO_Level3_Shift

! switch left super and alt
clear Mod1
remove mod1 = Alt_L
remove mod4 = Super_L
add mod4 = Alt_L
add mod1 = Super_L

When you want to change the default behavior of the Apple keyboard, just run the command with file as parameter:

xmodmap ~/.xmodmap

The changes in .xmodmap will only stay until you log out from your system, so if you want to make them permanent, put the command into programs which are loaded automatically when your GUI session starts.

If you encounter problems with layout, you can just type setxkbmap -layout cz into terminal and it will reset the layout into defaut. If you are using different keyboard map than Czech, you may probably want to change the cz parameter to your country code shortcut (en for english): setxkbmap -layout en.

Known bugs

CTRL+LSHIFT+t

There is one big mystery - why the CTRL+LSHIFT+t shortcut doesn't work. Every other shortcut works, every combination of CTRL+LSHIFT+char works, but not with 't'. You have to use CTRL+RSHIFT+t to get it work (for example in firefox to open closed tab).

I don't know why it didn't work - it just didn't. Maybe it is something in the linux hid-apple driver, or maybe the switch matrix in keyboard is designed to suppress this combination. Maybe it is some strange hidden system menu shortcut, or whatever.

If you found what it is, please, share that knowledge in comments, or here: http://www.abclinuxu.cz/poradna/linux/show/384016

CTRL+ALT+Fx

If you want to use this keyboard combination to switch into console, it won't work. I don't know exactly why. If you switch with another keyboard, there is no problem - it works properly, you can even switch from console back to GUI, but not from GUI to console.

Wake from RAM

If you somehow suspend your computer into RAM, you won't be able to wake it with this keyboard. You have to use something else - mouse buttons, another keyboard, power switch..

When the computer wakes up, give it few seconds and then press "power" button on your keyboard - it should get sync automatically.

Printscreen

Printscreen doesn't work, because there is no key for it. You could probably add some key shortcut via xmodmap, but you can do it much more easier in GUI. Go to System/Settings/Keyboard shortcuts menu and remap Take a screenshot, or adding your own call to mate-screenshot.

Result

I've decided to keep the keyboard. It's actually quite nice and writing is pleasurable. It is also definitely less exhausting for my fingers compared to mechanical keyboard with Cherry MX blue switches.

This blog was written on the keyboard. It has something around 10kB of text and it came from me smoothly. With Blackwidow, I am able to write ~68 words per minute. First results on Apple keyboard are around 80 wpm.

English

I've written this in English, because I've spend almost whole day googling how to change the layout to accommodate my needs. I'd noticed, that lot of people were asking same question and usually got no, or only partial answer. Maybe this will serve them and save them lot of time and frustration.

My English skill

This is the biggest chunk of text I've ever written in English so far. That's because I officially don't know English. I have no idea what I am doing, I am just using phrases which are emerging into my consciousness.

I've learned English statistically, by watching LOT of movies in original dabbing and later by reading articles at the Internet. I have no trouble to read (and enjoy) book written in English, but I know almost nothing about grammar, so there may (and will) be lot of typos and strangely structured sentences.

I was afraid to write in English. It's is common fear which many people I know are suffering. Some say, that Czech language is very hard. Grammar nazis aren't just common amongst Czech people - it is something like national tradition everyone hates, but do anyway. I've decided to ignore this.

It's childish not to use another language just for fear that you will be criticized and corrected in discussion. You can't learn language without practicing. It is impossible. I have right to be wrong, because I am just beginner, who just taught himself another language. That should be great feeling, not something to be ashamed of.

If you have any problem with grammar in any of my articles, send me patch of HTML source, or piss off. That's my license agreement.

Thx

Become a Patron