diff mbox series

HID: apple: Swap Control and Command keys on Apple keyboards

Message ID 7AC64680-44F7-4605-95E9-B5FF97A78D2A@live.com
State Accepted
Commit fd7b68b763c4dfa65e3c145c624427d5fd11202f
Headers show
Series HID: apple: Swap Control and Command keys on Apple keyboards | expand

Commit Message

Aditya Garg Nov. 4, 2022, 11:55 a.m. UTC
From: Aditya Garg <gargaditya08@live.com>

This patch allows users to swap the control and command keys. This can be
useful for the Mac users who are used to using Command instead of Control
in macOS for various commonly used shortcuts.

Signed-off-by: Aditya Garg <gargaditya08@live.com>
---
 drivers/hid/hid-apple.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

Comments

Jiri Kosina Nov. 14, 2022, 10:48 p.m. UTC | #1
On Fri, 4 Nov 2022, Aditya Garg wrote:

> From: Aditya Garg <gargaditya08@live.com>
> 
> This patch allows users to swap the control and command keys. This can be
> useful for the Mac users who are used to using Command instead of Control
> in macOS for various commonly used shortcuts.
> 
> Signed-off-by: Aditya Garg <gargaditya08@live.com>

Applied, thanks.

The number of module parameters the apple driver has been acquiring over 
years is becoming a little bit worrisome ... I am not sure that the actual 
users are keeping track. Is there any userspace utlizing it in a friendly 
manner?
Aditya Garg Nov. 16, 2022, 1:23 p.m. UTC | #2
> Applied, thanks.
> 
> The number of module parameters the apple driver has been acquiring over 
> years is becoming a little bit worrisome ... I am not sure that the actual 
> users are keeping track.

I guess the Arch wiki on Apple keyboards does a good job to keep users updated.

https://wiki.archlinux.org/title/Apple_Keyboard

> Is there any userspace utlizing it in a friendly 
> manner?

I work for the t2linux community involving users with T2 Macs, and I guess the previous module parameters in the driver were being used by the users. Although I ain't aware about any softwares which make using these parameters easy, although I guess a simple bash script placed in /usr/bin can be made to make things easy. I had made a similar one for the Touch Bar driver for the Macs (the driver shall be sent to you soon btw!)

We also have tools like xmodmap which can do the same function as these module parameters do, and if used, we actually don’t need the module parameters. But these tools are a bit unreliable and a bit difficult to set up as compared to module parameters, as per the feedback received from various Mac users.

I guess I was able to clarify what all you asked :)

Regards
Aditya

> — 
> Jiri Kosina
> SUSE Labs
>
diff mbox series

Patch

diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index c671ce946..86188e803 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -59,6 +59,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_ctrl_cmd;
+module_param(swap_ctrl_cmd, uint, 0644);
+MODULE_PARM_DESC(swap_ctrl_cmd, "Swap the Control (\"Ctrl\") and Command (\"Flag\") keys. "
+		"(For people who are used to Mac shortcuts involving Command instead of Control. "
+		"[0] = No change. 1 = Swapped.)");
+
 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. "
@@ -308,7 +314,15 @@  static const struct apple_key_translation swapped_option_cmd_keys[] = {
 	{ KEY_LEFTALT,	KEY_LEFTMETA },
 	{ KEY_LEFTMETA,	KEY_LEFTALT },
 	{ KEY_RIGHTALT,	KEY_RIGHTMETA },
-	{ KEY_RIGHTMETA,KEY_RIGHTALT },
+	{ KEY_RIGHTMETA, KEY_RIGHTALT },
+	{ }
+};
+
+static const struct apple_key_translation swapped_ctrl_cmd_keys[] = {
+	{ KEY_LEFTCTRL,	KEY_LEFTMETA },
+	{ KEY_LEFTMETA,	KEY_LEFTCTRL },
+	{ KEY_RIGHTCTRL, KEY_RIGHTMETA },
+	{ KEY_RIGHTMETA, KEY_RIGHTCTRL },
 	{ }
 };
 
@@ -407,6 +421,13 @@  static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
 			code = trans->to;
 	}
 
+	if (swap_ctrl_cmd) {
+		trans = apple_find_translation(swapped_ctrl_cmd_keys, code);
+
+		if (trans)
+			code = trans->to;
+	}
+
 	if (code == KEY_FN)
 		asc->fn_on = !!value;