From 7600c04cd9a2936e29a159a9f8c910dd1c3e66a4 Mon Sep 17 00:00:00 2001 From: juank Date: Tue, 26 May 2020 06:02:01 -0300 Subject: [PATCH 01/13] [Feature Request] Swap buttons on PS2 Mouse/Trackball --- tmk_core/protocol/ps2_mouse.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index a0e52bc7c3a5..183146900be3 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -150,6 +150,13 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) // remove sign and overflow flags mouse_report->buttons &= PS2_MOUSE_BTN_MASK; +#ifdef PS2_MOUSE_INVERT_BUTTONS + int buttons = mouse_report->buttons; + buttons |= ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) & 1U) << 1; + buttons |= (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT) & 1U; + mouse_report->buttons = buttons; +#endif + #ifdef PS2_MOUSE_INVERT_X mouse_report->x = -mouse_report->x; #endif From de9d50ac79916431febd4db9b31fd37fd1d79cf4 Mon Sep 17 00:00:00 2001 From: juank Date: Tue, 26 May 2020 06:12:10 -0300 Subject: [PATCH 02/13] [Feature Request] Swap buttons on PS2 Mouse/Trackball --- docs/feature_ps2_mouse.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/feature_ps2_mouse.md b/docs/feature_ps2_mouse.md index c1bd8bff50a8..b28adb997e9a 100644 --- a/docs/feature_ps2_mouse.md +++ b/docs/feature_ps2_mouse.md @@ -246,6 +246,16 @@ Fine control over the scrolling is supported with the following defines: #define PS2_MOUSE_SCROLL_DIVISOR_V 2 ``` +### Invert Mouse buttons + +To invert the left & right buttonsyou can put: + +```c +#define PS2_MOUSE_INVERT_BUTTONS +``` + +into config.h. + ### Invert Mouse and Scroll Axes :id=invert-mouse-and-scroll-axes To invert the X and Y axes you can put: From beff95ac2a0817554cf9cff4699e2062828a6933 Mon Sep 17 00:00:00 2001 From: juank Date: Tue, 26 May 2020 06:16:40 -0300 Subject: [PATCH 03/13] Added id: to the doc --- docs/feature_ps2_mouse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/feature_ps2_mouse.md b/docs/feature_ps2_mouse.md index b28adb997e9a..587ca6e0b3ac 100644 --- a/docs/feature_ps2_mouse.md +++ b/docs/feature_ps2_mouse.md @@ -246,7 +246,7 @@ Fine control over the scrolling is supported with the following defines: #define PS2_MOUSE_SCROLL_DIVISOR_V 2 ``` -### Invert Mouse buttons +### Invert Mouse buttons :id=invert-buttons To invert the left & right buttonsyou can put: From 2fa53fdb2feb311fbcab779b80e2ac6c8c4210d3 Mon Sep 17 00:00:00 2001 From: juank Date: Tue, 26 May 2020 06:18:27 -0300 Subject: [PATCH 04/13] Missing space --- docs/feature_ps2_mouse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/feature_ps2_mouse.md b/docs/feature_ps2_mouse.md index 587ca6e0b3ac..c17a327019a6 100644 --- a/docs/feature_ps2_mouse.md +++ b/docs/feature_ps2_mouse.md @@ -248,7 +248,7 @@ Fine control over the scrolling is supported with the following defines: ### Invert Mouse buttons :id=invert-buttons -To invert the left & right buttonsyou can put: +To invert the left & right buttons you can put: ```c #define PS2_MOUSE_INVERT_BUTTONS From 351b635b20e291f75be5df165e77efc62337d315 Mon Sep 17 00:00:00 2001 From: juank Date: Wed, 27 May 2020 01:16:45 -0300 Subject: [PATCH 05/13] Solve comment https://github.com/qmk/qmk_firmware/pull/9205#discussion_r430783182 --- tmk_core/protocol/ps2_mouse.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 183146900be3..d08422772e63 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -151,10 +151,7 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->buttons &= PS2_MOUSE_BTN_MASK; #ifdef PS2_MOUSE_INVERT_BUTTONS - int buttons = mouse_report->buttons; - buttons |= ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) & 1U) << 1; - buttons |= (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT) & 1U; - mouse_report->buttons = buttons; + mouse_report->buttons = (mouse_report->buttons >> PS2_MOUSE_BTN_LEFT & 1U) << PS2_MOUSE_BTN_RIGHT | (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT & 1U) << PS2_MOUSE_BTN_LEFT | (mouse_report->buttons >> PS2_MOUSE_BTN_MIDDLE & 1U) << PS2_MOUSE_BTN_MIDDLE; #endif #ifdef PS2_MOUSE_INVERT_X From 7811e9f2bedf281266c489da688fca773eb4e1cd Mon Sep 17 00:00:00 2001 From: juank Date: Wed, 27 May 2020 20:10:42 -0300 Subject: [PATCH 06/13] Solve comments https://github.com/qmk/qmk_firmware/pull/9205#discussion_r430783182 & https://github.com/qmk/qmk_firmware/pull/9205#discussion_r430783884 --- tmk_core/protocol/ps2_mouse.c | 18 +++++++++++++++--- tmk_core/protocol/ps2_mouse.h | 11 +++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index d08422772e63..37e6beba2104 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -147,11 +147,23 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127); mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); +#ifdef PS2_MOUSE_INVERT_BUTTONS + mouse_report->buttons = ( \ + // Change right to left + BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_LEFT, PS2_MOUSE_BTN_RIGHT) | \ + // Change left to right + BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_RIGHT,PS2_MOUSE_BTN_LEFT) | \ + // Leave the rest untouched + BITMASK_CLEAR(mouse_report->buttons, ( + // Calculate mask for the rest + BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_LEFT) | \ + BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_RIGHT) + )) + // remove sign and overflow flags + ) & PS2_MOUSE_BTN_MASK; +#else // remove sign and overflow flags mouse_report->buttons &= PS2_MOUSE_BTN_MASK; - -#ifdef PS2_MOUSE_INVERT_BUTTONS - mouse_report->buttons = (mouse_report->buttons >> PS2_MOUSE_BTN_LEFT & 1U) << PS2_MOUSE_BTN_RIGHT | (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT & 1U) << PS2_MOUSE_BTN_LEFT | (mouse_report->buttons >> PS2_MOUSE_BTN_MIDDLE & 1U) << PS2_MOUSE_BTN_MIDDLE; #endif #ifdef PS2_MOUSE_INVERT_X diff --git a/tmk_core/protocol/ps2_mouse.h b/tmk_core/protocol/ps2_mouse.h index 30053ef18703..532974f6af9a 100644 --- a/tmk_core/protocol/ps2_mouse.h +++ b/tmk_core/protocol/ps2_mouse.h @@ -21,6 +21,17 @@ along with this program. If not, see . #include #include "debug.h" +/* + * Binary manipulation macros + */ + +/* a=target variable, b=bit number to act upon 0-n, c=bit number to act upon 0-n */ +#define BIT_SET_BY(a, b, c) ((a >> b & 1U) << c) +#define BIT_GET(a, b) (a >> b & 1U) +#define BIT_VALUE(a, b) ((a >> b & 1U) << b) +/* x=target variable, y=mask */ +#define BITMASK_CLEAR(x,y) ((x) & (~(y))) + #define PS2_MOUSE_SEND(command, message) \ do { \ __attribute__((unused)) uint8_t rcv = ps2_host_send(command); \ From 3999300505bb4417352c03db24d707dfe1f28514 Mon Sep 17 00:00:00 2001 From: juank Date: Wed, 27 May 2020 23:08:08 -0300 Subject: [PATCH 07/13] Format code more according to https://docs.qmk.fm/#/coding_conventions_c --- tmk_core/protocol/ps2_mouse.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 37e6beba2104..1a00a131b08f 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -148,21 +148,17 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); #ifdef PS2_MOUSE_INVERT_BUTTONS - mouse_report->buttons = ( \ - // Change right to left - BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_LEFT, PS2_MOUSE_BTN_RIGHT) | \ - // Change left to right - BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_RIGHT,PS2_MOUSE_BTN_LEFT) | \ - // Leave the rest untouched - BITMASK_CLEAR(mouse_report->buttons, ( - // Calculate mask for the rest - BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_LEFT) | \ - BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_RIGHT) - )) - // remove sign and overflow flags + mouse_report->buttons = ( + /* Change right to left */ + BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_LEFT, PS2_MOUSE_BTN_RIGHT) | + /* Change left to right */ + BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_RIGHT, PS2_MOUSE_BTN_LEFT) | + /* Leave the rest */ + BITMASK_CLEAR(mouse_report->buttons, (BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_LEFT) | BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_RIGHT))) + /* Remove sign and overflow flags */ ) & PS2_MOUSE_BTN_MASK; #else - // remove sign and overflow flags + /* remove sign and overflow flags */ mouse_report->buttons &= PS2_MOUSE_BTN_MASK; #endif From 222cc2fa81ea1c59f4cc26486f4a62eca8a39b74 Mon Sep 17 00:00:00 2001 From: juank Date: Thu, 28 May 2020 16:19:51 -0300 Subject: [PATCH 08/13] change logic to LUT --- tmk_core/protocol/ps2_mouse.c | 13 +++--------- tmk_core/protocol/ps2_mouse.h | 37 +++++++++++++++++------------------ 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 1a00a131b08f..b5699b3a3435 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -148,17 +148,10 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); #ifdef PS2_MOUSE_INVERT_BUTTONS - mouse_report->buttons = ( - /* Change right to left */ - BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_LEFT, PS2_MOUSE_BTN_RIGHT) | - /* Change left to right */ - BIT_SET_BY(mouse_report->buttons, PS2_MOUSE_BTN_RIGHT, PS2_MOUSE_BTN_LEFT) | - /* Leave the rest */ - BITMASK_CLEAR(mouse_report->buttons, (BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_LEFT) | BIT_VALUE(PS2_MOUSE_BTN_MASK, PS2_MOUSE_BTN_RIGHT))) - /* Remove sign and overflow flags */ - ) & PS2_MOUSE_BTN_MASK; + // remove sign and overflow flags and apply buttons LUT + mouse_report->buttons = btns_lut[mouse_report->buttons & PS2_MOUSE_BTN_MASK]; #else - /* remove sign and overflow flags */ + // remove sign and overflow flags mouse_report->buttons &= PS2_MOUSE_BTN_MASK; #endif diff --git a/tmk_core/protocol/ps2_mouse.h b/tmk_core/protocol/ps2_mouse.h index 532974f6af9a..909d8d1dc12e 100644 --- a/tmk_core/protocol/ps2_mouse.h +++ b/tmk_core/protocol/ps2_mouse.h @@ -21,17 +21,6 @@ along with this program. If not, see . #include #include "debug.h" -/* - * Binary manipulation macros - */ - -/* a=target variable, b=bit number to act upon 0-n, c=bit number to act upon 0-n */ -#define BIT_SET_BY(a, b, c) ((a >> b & 1U) << c) -#define BIT_GET(a, b) (a >> b & 1U) -#define BIT_VALUE(a, b) ((a >> b & 1U) << b) -/* x=target variable, y=mask */ -#define BITMASK_CLEAR(x,y) ((x) & (~(y))) - #define PS2_MOUSE_SEND(command, message) \ do { \ __attribute__((unused)) uint8_t rcv = ps2_host_send(command); \ @@ -86,14 +75,24 @@ __attribute__((unused)) static enum ps2_mouse_mode_e { * 1|[ X movement(0-255) ] * 2|[ Y movement(0-255) ] */ -#define PS2_MOUSE_BTN_MASK 0x07 -#define PS2_MOUSE_BTN_LEFT 0 -#define PS2_MOUSE_BTN_RIGHT 1 -#define PS2_MOUSE_BTN_MIDDLE 2 -#define PS2_MOUSE_X_SIGN 4 -#define PS2_MOUSE_Y_SIGN 5 -#define PS2_MOUSE_X_OVFLW 6 -#define PS2_MOUSE_Y_OVFLW 7 +#define PS2_MOUSE_BTN_MASK 0x07 // 0b0000111 +#define PS2_MOUSE_BTN_LEFT 0 // 0b0000001 +#define PS2_MOUSE_BTN_RIGHT 1 // 0b0000010 +#define PS2_MOUSE_BTN_MIDDLE 2 // 0b0000100 +#define PS2_MOUSE_X_SIGN 4 // 0b0001000 +#define PS2_MOUSE_Y_SIGN 5 // 0b0010000 +#define PS2_MOUSE_X_OVFLW 6 // 0b0100000 +#define PS2_MOUSE_Y_OVFLW 7 // 0b1000000 + +#ifdef PS2_MOUSE_INVERT_BUTTONS +/* + * LUT for change mouse buttons + * + * table index is the mouse_report->buttons state from PS2 (after &= PS2_MOUSE_BTN_MASK) + * the value replace the mouse_report->buttons before to send to the USB interface + */ +static const __attribute__((unused)) uint8_t btns_lut[8] = {0b000, 0b010, 0b001, 0b011, 0b100, 0b101, 0b110, 0b111}; +#endif /* mouse button to start scrolling; set 0 to disable scroll */ #ifndef PS2_MOUSE_SCROLL_BTN_MASK From d0ed90aff9f6630d872786f9654b93405dbff916 Mon Sep 17 00:00:00 2001 From: juank Date: Thu, 28 May 2020 21:03:17 -0300 Subject: [PATCH 09/13] WIP: Clean up --- tmk_core/protocol/ps2_mouse.c | 10 ++++++++++ tmk_core/protocol/ps2_mouse.h | 26 ++++++++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index b5699b3a3435..459158460dd7 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -36,6 +36,16 @@ static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report); static inline void ps2_mouse_enable_scrolling(void); static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report); +#ifdef PS2_MOUSE_INVERT_BUTTONS +/* + * LUT for change mouse buttons + * + * table index is the mouse_report->buttons state from PS2 (after &= PS2_MOUSE_BTN_MASK) + * the value replace the mouse_report->buttons before to send to the USB interface + */ +static const uint8_t btns_lut[8] = {0b000, 0b010, 0b001, 0b011, 0b100, 0b101, 0b110, 0b111}; +#endif + /* ============================= IMPLEMENTATION ============================ */ /* supports only 3 button mouse at this time */ diff --git a/tmk_core/protocol/ps2_mouse.h b/tmk_core/protocol/ps2_mouse.h index 909d8d1dc12e..30053ef18703 100644 --- a/tmk_core/protocol/ps2_mouse.h +++ b/tmk_core/protocol/ps2_mouse.h @@ -75,24 +75,14 @@ __attribute__((unused)) static enum ps2_mouse_mode_e { * 1|[ X movement(0-255) ] * 2|[ Y movement(0-255) ] */ -#define PS2_MOUSE_BTN_MASK 0x07 // 0b0000111 -#define PS2_MOUSE_BTN_LEFT 0 // 0b0000001 -#define PS2_MOUSE_BTN_RIGHT 1 // 0b0000010 -#define PS2_MOUSE_BTN_MIDDLE 2 // 0b0000100 -#define PS2_MOUSE_X_SIGN 4 // 0b0001000 -#define PS2_MOUSE_Y_SIGN 5 // 0b0010000 -#define PS2_MOUSE_X_OVFLW 6 // 0b0100000 -#define PS2_MOUSE_Y_OVFLW 7 // 0b1000000 - -#ifdef PS2_MOUSE_INVERT_BUTTONS -/* - * LUT for change mouse buttons - * - * table index is the mouse_report->buttons state from PS2 (after &= PS2_MOUSE_BTN_MASK) - * the value replace the mouse_report->buttons before to send to the USB interface - */ -static const __attribute__((unused)) uint8_t btns_lut[8] = {0b000, 0b010, 0b001, 0b011, 0b100, 0b101, 0b110, 0b111}; -#endif +#define PS2_MOUSE_BTN_MASK 0x07 +#define PS2_MOUSE_BTN_LEFT 0 +#define PS2_MOUSE_BTN_RIGHT 1 +#define PS2_MOUSE_BTN_MIDDLE 2 +#define PS2_MOUSE_X_SIGN 4 +#define PS2_MOUSE_Y_SIGN 5 +#define PS2_MOUSE_X_OVFLW 6 +#define PS2_MOUSE_Y_OVFLW 7 /* mouse button to start scrolling; set 0 to disable scroll */ #ifndef PS2_MOUSE_SCROLL_BTN_MASK From 94c625e2811269eb63035ee4b483a91383cf0d68 Mon Sep 17 00:00:00 2001 From: juank Date: Fri, 29 May 2020 03:06:49 -0300 Subject: [PATCH 10/13] WIP: Solution with xor operators to mask the change --- tmk_core/protocol/ps2_mouse.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 459158460dd7..62ee1cb4a650 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -36,14 +36,6 @@ static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report); static inline void ps2_mouse_enable_scrolling(void); static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report); -#ifdef PS2_MOUSE_INVERT_BUTTONS -/* - * LUT for change mouse buttons - * - * table index is the mouse_report->buttons state from PS2 (after &= PS2_MOUSE_BTN_MASK) - * the value replace the mouse_report->buttons before to send to the USB interface - */ -static const uint8_t btns_lut[8] = {0b000, 0b010, 0b001, 0b011, 0b100, 0b101, 0b110, 0b111}; #endif /* ============================= IMPLEMENTATION ============================ */ @@ -158,8 +150,9 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); #ifdef PS2_MOUSE_INVERT_BUTTONS - // remove sign and overflow flags and apply buttons LUT - mouse_report->buttons = btns_lut[mouse_report->buttons & PS2_MOUSE_BTN_MASK]; + // swap left & rigth buttons & remove sign and overflow flags + uint8_t btns_change = ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) ^ (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT)) & 1; + mouse_report->buttons = ((btns_change << PS2_MOUSE_BTN_LEFT) | (btns_change << PS2_MOUSE_BTN_RIGHT)) & PS2_MOUSE_BTN_MASK; #else // remove sign and overflow flags mouse_report->buttons &= PS2_MOUSE_BTN_MASK; From b649448ab33a47bb7a3bd6fe536dfbaff51dad95 Mon Sep 17 00:00:00 2001 From: juank Date: Fri, 29 May 2020 04:47:35 -0300 Subject: [PATCH 11/13] delete #endif & added the missed xor operator (ahhh) --- tmk_core/protocol/ps2_mouse.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 62ee1cb4a650..59ea8cbce0cf 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -36,8 +36,6 @@ static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report); static inline void ps2_mouse_enable_scrolling(void); static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report); -#endif - /* ============================= IMPLEMENTATION ============================ */ /* supports only 3 button mouse at this time */ @@ -149,13 +147,13 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127); mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); -#ifdef PS2_MOUSE_INVERT_BUTTONS - // swap left & rigth buttons & remove sign and overflow flags - uint8_t btns_change = ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) ^ (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT)) & 1; - mouse_report->buttons = ((btns_change << PS2_MOUSE_BTN_LEFT) | (btns_change << PS2_MOUSE_BTN_RIGHT)) & PS2_MOUSE_BTN_MASK; -#else // remove sign and overflow flags mouse_report->buttons &= PS2_MOUSE_BTN_MASK; + +#ifdef PS2_MOUSE_INVERT_BUTTONS + // swap left & rigth buttons & remove sign and overflow flags + uint8_t btns_change = ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) ^ (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT)) & 1; + mouse_report->buttons ^= ((btns_change << PS2_MOUSE_BTN_LEFT) | (btns_change << PS2_MOUSE_BTN_RIGHT)); #endif #ifdef PS2_MOUSE_INVERT_X From 0351a32f5072ff52fc081ed3eba79219757c6901 Mon Sep 17 00:00:00 2001 From: juank Date: Sat, 30 May 2020 09:33:19 -0300 Subject: [PATCH 12/13] Variable (mouse_report->buttons): avoid setting twice https://github.com/qmk/qmk_firmware/pull/9205#discussion_r430783884 --- tmk_core/protocol/ps2_mouse.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 59ea8cbce0cf..15d72691c21d 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -147,13 +147,13 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127); mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); - // remove sign and overflow flags - mouse_report->buttons &= PS2_MOUSE_BTN_MASK; - #ifdef PS2_MOUSE_INVERT_BUTTONS // swap left & rigth buttons & remove sign and overflow flags - uint8_t btns_change = ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) ^ (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT)) & 1; - mouse_report->buttons ^= ((btns_change << PS2_MOUSE_BTN_LEFT) | (btns_change << PS2_MOUSE_BTN_RIGHT)); + uint8_t btns_change = ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) ^ (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT)) & 1; + mouse_report->buttons = (mouse_report->buttons ^ ((btns_change << PS2_MOUSE_BTN_LEFT) | (btns_change << PS2_MOUSE_BTN_RIGHT))) & PS2_MOUSE_BTN_MASK; +#else + // remove sign and overflow flags + mouse_report->buttons &= PS2_MOUSE_BTN_MASK; #endif #ifdef PS2_MOUSE_INVERT_X From c4e7a819c31de5b5c687029c39a74781cac7a28a Mon Sep 17 00:00:00 2001 From: Juan Pablo Kutianski Date: Thu, 5 Aug 2021 15:09:11 -0700 Subject: [PATCH 13/13] Update tmk_core/protocol/ps2_mouse.c Co-authored-by: Nick Brassel --- tmk_core/protocol/ps2_mouse.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tmk_core/protocol/ps2_mouse.c b/tmk_core/protocol/ps2_mouse.c index 15d72691c21d..a47f3f6e3358 100644 --- a/tmk_core/protocol/ps2_mouse.c +++ b/tmk_core/protocol/ps2_mouse.c @@ -148,9 +148,10 @@ static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); #ifdef PS2_MOUSE_INVERT_BUTTONS - // swap left & rigth buttons & remove sign and overflow flags - uint8_t btns_change = ((mouse_report->buttons >> PS2_MOUSE_BTN_LEFT) ^ (mouse_report->buttons >> PS2_MOUSE_BTN_RIGHT)) & 1; - mouse_report->buttons = (mouse_report->buttons ^ ((btns_change << PS2_MOUSE_BTN_LEFT) | (btns_change << PS2_MOUSE_BTN_RIGHT))) & PS2_MOUSE_BTN_MASK; + // swap left & right buttons + uint8_t needs_left = mouse_report->buttons & PS2_MOUSE_BTN_RIGHT; + uint8_t needs_right = mouse_report->buttons & PS2_MOUSE_BTN_LEFT; + mouse_report->buttons = (mouse_report->buttons & ~(PS2_MOUSE_BTN_MASK)) | (needs_left ? PS2_MOUSE_BTN_LEFT : 0) | (needs_right ? PS2_MOUSE_BTN_RIGHT : 0); #else // remove sign and overflow flags mouse_report->buttons &= PS2_MOUSE_BTN_MASK;