Skip to content

Commit

Permalink
HID: apple: Swap the Fn and Left Control keys on Apple keyboards
Browse files Browse the repository at this point in the history
This patch allows users to swap the Fn and left Control keys on all Apple
keyboards: internal (e.g. Macbooks) and external (both wired and wireless).
The patch adds a new hid-apple module param: swap_fn_leftctrl (off by default).

Signed-off-by: Zakhar Semenov <mail@free5lot.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
  • Loading branch information
free5lot authored and Jiri Kosina committed May 15, 2020
1 parent 47cf1b4 commit 346338e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions drivers/hid/hid-apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ MODULE_PARM_DESC(swap_opt_cmd, "Swap the Option (\"Alt\") and Command (\"Flag\")
"(For people who want to keep Windows PC keyboard muscle memory. "
"[0] = as-is, Mac layout. 1 = swapped, Windows layout.)");

static unsigned int swap_fn_leftctrl;
module_param(swap_fn_leftctrl, uint, 0644);
MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
"(For people who want to keep PC keyboard muscle memory. "
"[0] = as-is, Mac layout, 1 = swapped, PC layout)");

struct apple_sc {
unsigned long quirks;
unsigned int fn_on;
Expand Down Expand Up @@ -162,6 +168,11 @@ static const struct apple_key_translation swapped_option_cmd_keys[] = {
{ }
};

static const struct apple_key_translation swapped_fn_leftctrl_keys[] = {
{ KEY_FN, KEY_LEFTCTRL },
{ }
};

static const struct apple_key_translation *apple_find_translation(
const struct apple_key_translation *table, u16 from)
{
Expand All @@ -183,9 +194,11 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
bool do_translate;
u16 code = 0;

if (usage->code == KEY_FN) {
u16 fn_keycode = (swap_fn_leftctrl) ? (KEY_LEFTCTRL) : (KEY_FN);

if (usage->code == fn_keycode) {
asc->fn_on = !!value;
input_event(input, usage->type, usage->code, value);
input_event(input, usage->type, KEY_FN, value);
return 1;
}

Expand Down Expand Up @@ -270,6 +283,14 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
}
}

if (swap_fn_leftctrl) {
trans = apple_find_translation(swapped_fn_leftctrl_keys, usage->code);
if (trans) {
input_event(input, usage->type, trans->to, value);
return 1;
}
}

return 0;
}

Expand Down Expand Up @@ -333,6 +354,11 @@ static void apple_setup_input(struct input_dev *input)

for (trans = apple_iso_keyboard; trans->from; trans++)
set_bit(trans->to, input->keybit);

if (swap_fn_leftctrl) {
for (trans = swapped_fn_leftctrl_keys; trans->from; trans++)
set_bit(trans->to, input->keybit);
}
}

static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
Expand Down

0 comments on commit 346338e

Please sign in to comment.