From f12258c68a82b954d634e8a5d0d1733e3521e2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nimish=20G=C3=A5tam?= Date: Tue, 20 Jun 2023 21:19:15 +0200 Subject: [PATCH 1/6] Old functions had typos and didn't account for both mods pressed at once. This has been fixed --- quantum/keycode_config.c | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/quantum/keycode_config.c b/quantum/keycode_config.c index 9dd7097c861a..07e9688206e7 100644 --- a/quantum/keycode_config.c +++ b/quantum/keycode_config.c @@ -123,39 +123,25 @@ __attribute__((weak)) uint16_t keycode_config(uint16_t keycode) { __attribute__((weak)) uint8_t mod_config(uint8_t mod) { if (keymap_config.swap_lalt_lgui) { - if ((mod & MOD_RGUI) == MOD_LGUI) { - mod &= ~MOD_LGUI; - mod |= MOD_LALT; - } else if ((mod & MOD_RALT) == MOD_LALT) { - mod &= ~MOD_LALT; - mod |= MOD_LGUI; + // if they're both set or both unset, no need to do anything + if (((mod & MOD_LALT) == MOD_LALT) ^ ((mod & MOD_LGUI) == MOD_LGUI)) { + // we know only 1 is set, XOR with ORd mask to flip the set one + mod ^= (MOD_LALT | MOD_LGUI); } } if (keymap_config.swap_ralt_rgui) { - if ((mod & MOD_RGUI) == MOD_RGUI) { - mod &= ~MOD_RGUI; - mod |= MOD_RALT; - } else if ((mod & MOD_RALT) == MOD_RALT) { - mod &= ~MOD_RALT; - mod |= MOD_RGUI; + if (((mod & MOD_RALT) == MOD_RALT) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { + mod ^= (MOD_RALT | MOD_RGUI); } } if (keymap_config.swap_lctl_lgui) { - if ((mod & MOD_RGUI) == MOD_LGUI) { - mod &= ~MOD_LGUI; - mod |= MOD_LCTL; - } else if ((mod & MOD_RCTL) == MOD_LCTL) { - mod &= ~MOD_LCTL; - mod |= MOD_LGUI; + if (((mod & MOD_LCTL) == MOD_LCTL) ^ ((mod & MOD_LGUI) == MOD_LGUI)) { + mod ^= (MOD_LCTL | MOD_LGUI); } } if (keymap_config.swap_rctl_rgui) { - if ((mod & MOD_RGUI) == MOD_RGUI) { - mod &= ~MOD_RGUI; - mod |= MOD_RCTL; - } else if ((mod & MOD_RCTL) == MOD_RCTL) { - mod &= ~MOD_RCTL; - mod |= MOD_RGUI; + if (((mod & MOD_RCTL) == MOD_RCTL) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { + mod ^= (MOD_RCTL | MOD_RGUI); } } if (keymap_config.no_gui) { From e4340e8b24a442959c0dbc5693d5a55b98af8b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nimish=20G=C3=A5tam?= Date: Wed, 21 Jun 2023 00:21:55 +0200 Subject: [PATCH 2/6] Simplified and commented on code for taking multiple modifier presses into consideration --- quantum/keycode_config.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/quantum/keycode_config.c b/quantum/keycode_config.c index 07e9688206e7..bfd1f4aec1b1 100644 --- a/quantum/keycode_config.c +++ b/quantum/keycode_config.c @@ -122,26 +122,38 @@ __attribute__((weak)) uint16_t keycode_config(uint16_t keycode) { */ __attribute__((weak)) uint8_t mod_config(uint8_t mod) { + + /** + * Note: The right hand MOD variables (MOD_RALT, MOD_RGUI, MOD_RSFT, MOD_RCTL) + * are not 1-bit bitmasks like their left-handed counterparts. They have 2 bits + * set, so some of the usual bitwise operator tricks need to be modified + * to take this into account + */ if (keymap_config.swap_lalt_lgui) { - // if they're both set or both unset, no need to do anything - if (((mod & MOD_LALT) == MOD_LALT) ^ ((mod & MOD_LGUI) == MOD_LGUI)) { - // we know only 1 is set, XOR with ORd mask to flip the set one + // If both modifiers pressed or neither pressed, do nothing + // Otherwise swap the values + // Note: The left mods are ANDed with the right-hand values to check + // if they were pressed with the right hand bit set + if (((mod & MOD_RALT) == MOD_LALT) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { mod ^= (MOD_LALT | MOD_LGUI); } } if (keymap_config.swap_ralt_rgui) { if (((mod & MOD_RALT) == MOD_RALT) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { - mod ^= (MOD_RALT | MOD_RGUI); + // lefthand values to preserve the right hand bit + mod ^= (MOD_LALT | MOD_LGUI ); } } if (keymap_config.swap_lctl_lgui) { - if (((mod & MOD_LCTL) == MOD_LCTL) ^ ((mod & MOD_LGUI) == MOD_LGUI)) { + // left mods ANDed with right-hand values to check for right hand bit + if (((mod & MOD_RCTL) == MOD_LCTL) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { mod ^= (MOD_LCTL | MOD_LGUI); } } if (keymap_config.swap_rctl_rgui) { if (((mod & MOD_RCTL) == MOD_RCTL) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { - mod ^= (MOD_RCTL | MOD_RGUI); + // lefthand values to preserve the right hand bit + mod ^= (MOD_LCTL | MOD_LGUI ); } } if (keymap_config.no_gui) { From 1d276387f99724cd7b56a19a91147d695e72fff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nimish=20G=C3=A5tam?= Date: Wed, 21 Jun 2023 00:33:10 +0200 Subject: [PATCH 3/6] comment styling --- quantum/keycode_config.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/quantum/keycode_config.c b/quantum/keycode_config.c index bfd1f4aec1b1..c3c4901f13e6 100644 --- a/quantum/keycode_config.c +++ b/quantum/keycode_config.c @@ -130,29 +130,30 @@ __attribute__((weak)) uint8_t mod_config(uint8_t mod) { * to take this into account */ if (keymap_config.swap_lalt_lgui) { - // If both modifiers pressed or neither pressed, do nothing - // Otherwise swap the values - // Note: The left mods are ANDed with the right-hand values to check - // if they were pressed with the right hand bit set + /** If both modifiers pressed or neither pressed, do nothing + * Otherwise swap the values + * Note: The left mods are ANDed with the right-hand values to check + * if they were pressed with the right hand bit set + */ if (((mod & MOD_RALT) == MOD_LALT) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { mod ^= (MOD_LALT | MOD_LGUI); } } if (keymap_config.swap_ralt_rgui) { if (((mod & MOD_RALT) == MOD_RALT) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { - // lefthand values to preserve the right hand bit + /* lefthand values to preserve the right hand bit */ mod ^= (MOD_LALT | MOD_LGUI ); } } if (keymap_config.swap_lctl_lgui) { - // left mods ANDed with right-hand values to check for right hand bit + /* left mods ANDed with right-hand values to check for right hand bit */ if (((mod & MOD_RCTL) == MOD_LCTL) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { mod ^= (MOD_LCTL | MOD_LGUI); } } if (keymap_config.swap_rctl_rgui) { if (((mod & MOD_RCTL) == MOD_RCTL) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { - // lefthand values to preserve the right hand bit + /* lefthand values to preserve the right hand bit */ mod ^= (MOD_LCTL | MOD_LGUI ); } } From c95be228de6301bbfa41676418504325c1e001c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nimish=20G=C3=A5tam?= Date: Wed, 21 Jun 2023 08:37:02 +0200 Subject: [PATCH 4/6] ran clang-format --- quantum/keycode_config.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/quantum/keycode_config.c b/quantum/keycode_config.c index c3c4901f13e6..548c616f6bd5 100644 --- a/quantum/keycode_config.c +++ b/quantum/keycode_config.c @@ -122,39 +122,38 @@ __attribute__((weak)) uint16_t keycode_config(uint16_t keycode) { */ __attribute__((weak)) uint8_t mod_config(uint8_t mod) { - /** - * Note: The right hand MOD variables (MOD_RALT, MOD_RGUI, MOD_RSFT, MOD_RCTL) - * are not 1-bit bitmasks like their left-handed counterparts. They have 2 bits - * set, so some of the usual bitwise operator tricks need to be modified - * to take this into account - */ + * Note: The right hand MOD variables (MOD_RALT, MOD_RGUI, MOD_RSFT, MOD_RCTL) + * are not 1-bit bitmasks like their left-handed counterparts. They have 2 bits + * set, so some of the usual bitwise operator tricks need to be modified + * to take this into account + */ if (keymap_config.swap_lalt_lgui) { /** If both modifiers pressed or neither pressed, do nothing - * Otherwise swap the values - * Note: The left mods are ANDed with the right-hand values to check - * if they were pressed with the right hand bit set - */ + * Otherwise swap the values + * Note: The left mods are ANDed with the right-hand values to check + * if they were pressed with the right hand bit set + */ if (((mod & MOD_RALT) == MOD_LALT) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { - mod ^= (MOD_LALT | MOD_LGUI); + mod ^= (MOD_LALT | MOD_LGUI); } } if (keymap_config.swap_ralt_rgui) { if (((mod & MOD_RALT) == MOD_RALT) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { - /* lefthand values to preserve the right hand bit */ - mod ^= (MOD_LALT | MOD_LGUI ); + /* lefthand values to preserve the right hand bit */ + mod ^= (MOD_LALT | MOD_LGUI); } } if (keymap_config.swap_lctl_lgui) { /* left mods ANDed with right-hand values to check for right hand bit */ if (((mod & MOD_RCTL) == MOD_LCTL) ^ ((mod & MOD_RGUI) == MOD_LGUI)) { - mod ^= (MOD_LCTL | MOD_LGUI); + mod ^= (MOD_LCTL | MOD_LGUI); } } if (keymap_config.swap_rctl_rgui) { if (((mod & MOD_RCTL) == MOD_RCTL) ^ ((mod & MOD_RGUI) == MOD_RGUI)) { - /* lefthand values to preserve the right hand bit */ - mod ^= (MOD_LCTL | MOD_LGUI ); + /* lefthand values to preserve the right hand bit */ + mod ^= (MOD_LCTL | MOD_LGUI); } } if (keymap_config.no_gui) { From 866e6a3771353bb2c3d60494d150905ba8be734f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nimish=20G=C3=A5tam?= Date: Sun, 25 Jun 2023 07:33:59 +0200 Subject: [PATCH 5/6] simplified comment per suggestion --- quantum/keycode_config.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/quantum/keycode_config.c b/quantum/keycode_config.c index 548c616f6bd5..6e893e318137 100644 --- a/quantum/keycode_config.c +++ b/quantum/keycode_config.c @@ -123,10 +123,8 @@ __attribute__((weak)) uint16_t keycode_config(uint16_t keycode) { __attribute__((weak)) uint8_t mod_config(uint8_t mod) { /** - * Note: The right hand MOD variables (MOD_RALT, MOD_RGUI, MOD_RSFT, MOD_RCTL) - * are not 1-bit bitmasks like their left-handed counterparts. They have 2 bits - * set, so some of the usual bitwise operator tricks need to be modified - * to take this into account + * Note: This function is for the 5-bit packed mods, NOT the full 8-bit mods. + * More info about the mods can be seen in modifiers.h. */ if (keymap_config.swap_lalt_lgui) { /** If both modifiers pressed or neither pressed, do nothing From c6880464e89defb7ed52eef009887d41465780d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nimish=20G=C3=A5tam?= Date: Sun, 25 Jun 2023 07:55:04 +0200 Subject: [PATCH 6/6] ran clang formatter --- quantum/keycode_config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/keycode_config.c b/quantum/keycode_config.c index 6e893e318137..864488a65c97 100644 --- a/quantum/keycode_config.c +++ b/quantum/keycode_config.c @@ -123,7 +123,7 @@ __attribute__((weak)) uint16_t keycode_config(uint16_t keycode) { __attribute__((weak)) uint8_t mod_config(uint8_t mod) { /** - * Note: This function is for the 5-bit packed mods, NOT the full 8-bit mods. + * Note: This function is for the 5-bit packed mods, NOT the full 8-bit mods. * More info about the mods can be seen in modifiers.h. */ if (keymap_config.swap_lalt_lgui) {