davidfuzju / hid-apple-patched

Allows to swap the Fn key and left Control key on Macbook Pro and Apple keyboards in GNU/Linux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A patched hid-apple kernel module


UPDATE Apr 2017: Works on Arch too, for useful information on this proceed here

UPDATE May 2016: DKMS support added (tested on Ubuntu 16.04). Thanks to @almson!

UPDATE Oct 2015: The patch was modified for kernel 4.2 and support of Macbook Pro 2015. Thanks to @Aetf!

About

A patched version of hid-apple allows GNU/Linux user to swap the FN and left Control keys and some other mapping tweaks on Macbook Pro, external Apple keyboards and probably other Apple devices.

The patch was created by free5lot under GPL 2 (or later) licence. I hope it'll go to upstream kernel, so more GNU/Linux users could make their keyboards more comfortable for them.

This project was inspired by a similar patch made by JanmanX (Jan Meznik). His patch has the same idea but a bit different realization. I'd like to thank him for solving this problem in the first place and making his solution public.

Problem

This patch was created because Apple keyboards on Macbook Pro and external keyboard models have an awful location of special keys. To make it more habitual and friendly the fn key and left Control key should be swapped.

The swapping of Alt (Option) and Command is already possible without a patch by setting swap_opt_cmd=1 option to hid-apple kernel module in current versions of Linux kernel. More information is available at Ubuntu's help website.

Installation (via DKMS)

Go to the source code directory.

sudo dkms add .
sudo dkms build hid-apple/1.0
sudo dkms install hid-apple/1.0

Then, create file /etc/modprobe.d/hid_apple.conf. The following configuration emulates a standard PC layout:

options hid_apple fnmode=2
options hid_apple swap_fn_leftctrl=1
options hid_apple swap_opt_cmd=1
options hid_apple rightalt_as_rightctrl=1
options hid_apple ejectcd_as_delete=1

Finally, apply the new config file and reboot

sudo update-initramfs -u
sudo reboot

The advantage of DKMS is that the module is automatically re-built after every kernel upgrade and installation. (This method has been tested on Ubuntu 14.04 and 16.04. On Ubuntu 14.04, you will need to first sudo apt install dkms.)

Configuration

Permanent configuration is done in file /etc/modprobe.d/hid_apple.conf. The format is one option-value pair per line, like swap_fn_leftctrl=1. After writing to the file, do sudo update-initramfs -u and reboot. Temporary configuration (applies immediately but is lost after rebooting) is possible by writing to virtual files in /sys/module/hid_apple/parameters/, like echo 1 | sudo tee /sys/module/hid_apple/parameters/swap_fn_leftctrl.

  • fnmode - Mode of top-row keys
    • 0 = disabled
    • 1 = normally media keys, switchable to function keys by holding Fn key (Default)
    • 2 = normally function keys, switchable to media keys by holding Fn key
  • swap_fn_leftctrl - Swap the Fn and left Control keys
    • 0 = as silkscreened, Mac layout (Default)
    • 1 = swapped, PC layout
  • swap_opt_cmd - Swap the Option ("Alt") and Command ("Flag") keys
    • 0 = as silkscreened, Mac layout (Default)
    • 1 = swapped, PC layout
  • rightalt_as_rightctrl - Use the right Alt key as a right Ctrl key
    • 0 = as silkscreened, Mac layout (Default)
    • 1 = swapped, PC layout
  • ejectcd_as_delete - Use Eject-CD key as Delete key, if available
    • 0 = disabled (Default)
    • 1 = enabled
  • iso_layout - Enable/Disable hardcoded ISO-layout of the keyboard. Possibly relevant for international keyboard layouts
    • 0 = disabled,
    • 1 = enabled (Default)

Warning regarding Secure Boot (on non-Apple computers)

Some distributions, including Ubuntu 16.04, require that all modules are signed if Secure Boot is enabled. This breaks all third-party modules. There are various work-arounds, the easiest of which is to disable secure boot. This is currently not an issue on Apple computers, because Apple firmware does not support Secure Boot. See issue #23.

Alternative, script-based installation

Build and install via scripts provided:

./build.sh

./install.sh

The script will create /etc/modprobe.d/hid_apple.conf for you, after asking a few questions.

This process needs to be repeated after installing a new kernel, after having booted into the new kernel.

Alternative, makefile-based installation

  1. To build make sure you have the kernel development packages for your distribution installed. For example in Ubuntu these packages are called linux-headers-*, where "*" indicates the version and variant of the kernel.

  2. The default kernel header directory in the Makefile is: /usr/src/linux-headers-$(shell uname -r), it automatically detects and uses the version of running kernel. It works in Ubuntu and a lot of other GNU/Linux distributions, just skip this step if you use them. But if in your distribution the kernel header directory is different from the default one in the Makefile export the correct one:

export LINUX_HEADER_DIR=/path/to/kernel/header/dir
  1. To build:
make
  1. To install:
make install
  1. The install will put the module in the 'extra' sub-directory and the default unpatched module will take priority. To give your newly built module priority create a file '/etc/depmod.d/hid-apple.conf' and add the following line:
override hid_apple * extra

Then run:

sudo depmod -a
  1. And update of initramfs maybe required:
sudo update-initramfs -u

Topicality

A lot of GNU/Linux users of Macbook Pro and/or external (wireless) keyboards face the problem of uncomfortable placement of keys. Here are some topics about swap of fn and left control keys, and all of them are checked unsolved or/and closed.

So this patch is probably essential and desirable by users.

Links and mentions

About

Allows to swap the Fn key and left Control key on Macbook Pro and Apple keyboards in GNU/Linux

License:GNU General Public License v2.0


Languages

Language:C 89.5%Language:Shell 10.1%Language:Makefile 0.4%