Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Swap buttons on PS2 Mouse/Trackball #9205

Merged
merged 13 commits into from
Aug 5, 2021
10 changes: 10 additions & 0 deletions docs/feature_ps2_mouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :id=invert-buttons

To invert the left & right buttons you 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:
Expand Down
5 changes: 5 additions & 0 deletions tmk_core/protocol/ps2_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +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
// 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
mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
#endif

#ifdef PS2_MOUSE_INVERT_X
mouse_report->x = -mouse_report->x;
Expand Down
26 changes: 18 additions & 8 deletions tmk_core/protocol/ps2_mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,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
jkutianski marked this conversation as resolved.
Show resolved Hide resolved

#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
drashna marked this conversation as resolved.
Show resolved Hide resolved

/* mouse button to start scrolling; set 0 to disable scroll */
#ifndef PS2_MOUSE_SCROLL_BTN_MASK
Expand Down