From 05c050b81e0687b25fc6e2a11d113c65b894fa5c Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Mon, 2 Feb 2026 15:36:39 -0500 Subject: [PATCH] fix: handle back mouse on xwindows and windows based on : https://github.com/debauchee/barrier/commit/9f15b1bcf2953acf609f7580463bf13cdb23d008 fixes: #5682 --- src/lib/deskflow/MouseTypes.h | 2 +- src/lib/platform/MSWindowsDesks.cpp | 4 +- src/lib/platform/MSWindowsScreen.cpp | 4 +- src/lib/platform/MSWindowsScreen.h | 2 +- src/lib/platform/XWindowsScreen.cpp | 57 ++++++++++------------------ 5 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/lib/deskflow/MouseTypes.h b/src/lib/deskflow/MouseTypes.h index 0c045eabe..22cbd17df 100644 --- a/src/lib/deskflow/MouseTypes.h +++ b/src/lib/deskflow/MouseTypes.h @@ -28,4 +28,4 @@ static const ButtonID kMacButtonRight = 2; static const ButtonID kMacButtonMiddle = 3; //@} -static const uint8_t NumButtonIDs = 5; +static const uint8_t NumButtonIDs = 6; diff --git a/src/lib/platform/MSWindowsDesks.cpp b/src/lib/platform/MSWindowsDesks.cpp index e6a02544a..37f339beb 100644 --- a/src/lib/platform/MSWindowsDesks.cpp +++ b/src/lib/platform/MSWindowsDesks.cpp @@ -272,12 +272,12 @@ void MSWindowsDesks::fakeMouseButton(ButtonID button, bool press) flags = press ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP; break; - case kButtonExtra0 + 0: + case kButtonExtra0: data = XBUTTON1; flags = press ? MOUSEEVENTF_XDOWN : MOUSEEVENTF_XUP; break; - case kButtonExtra0 + 1: + case kButtonExtra1: data = XBUTTON2; flags = press ? MOUSEEVENTF_XDOWN : MOUSEEVENTF_XUP; break; diff --git a/src/lib/platform/MSWindowsScreen.cpp b/src/lib/platform/MSWindowsScreen.cpp index 57afe583d..0a318abff 100644 --- a/src/lib/platform/MSWindowsScreen.cpp +++ b/src/lib/platform/MSWindowsScreen.cpp @@ -1506,13 +1506,13 @@ ButtonID MSWindowsScreen::mapButtonFromEvent(WPARAM msg, LPARAM button) const switch (button) { case XBUTTON1: if (GetSystemMetrics(SM_CMOUSEBUTTONS) >= 4) { - return kButtonExtra0 + 0; + return kButtonExtra0; } break; case XBUTTON2: if (GetSystemMetrics(SM_CMOUSEBUTTONS) >= 5) { - return kButtonExtra0 + 1; + return kButtonExtra1; } break; } diff --git a/src/lib/platform/MSWindowsScreen.h b/src/lib/platform/MSWindowsScreen.h index 5430c3dba..ce2458df6 100644 --- a/src/lib/platform/MSWindowsScreen.h +++ b/src/lib/platform/MSWindowsScreen.h @@ -318,7 +318,7 @@ private: HotKeyToIDMap m_hotKeyToIDMap; // map of button state - bool m_buttons[1 + kButtonExtra0 + 1]; + bool m_buttons[NumButtonIDs]; // m_hasMouse is true if there's a mouse attached to the system or // mouse keys is simulating one. we track this so we can force the diff --git a/src/lib/platform/XWindowsScreen.cpp b/src/lib/platform/XWindowsScreen.cpp index a52a59a16..4e708e25e 100644 --- a/src/lib/platform/XWindowsScreen.cpp +++ b/src/lib/platform/XWindowsScreen.cpp @@ -1743,51 +1743,34 @@ KeyID XWindowsScreen::mapKeyFromX(XKeyEvent *event) const ButtonID XWindowsScreen::mapButtonFromX(const XButtonEvent *event) const { - unsigned int button = event->button; - - // first three buttons map to 1, 2, 3 (kButtonLeft, Middle, Right) - if (button >= 1 && button <= 3) { - return static_cast(button); - } - - // buttons 4 and 5 are ignored here. they're used for the wheel. - // buttons 6, 7, etc and up map to 4, 5, etc. - else if (button >= 6) { - return static_cast(button - 2); - } - - // unknown button - else { + switch (unsigned int button = event->button; button) { + case 1: + case 2: + case 3: + return static_cast(button); // Handle Left, Middle and Right buttons + case 8: + return kButtonExtra0; // Mouse 4 + case 9: + return kButtonExtra1; // Mouse 5 + default: return kButtonNone; } } unsigned int XWindowsScreen::mapButtonToX(ButtonID id) const { - // map button -1 to button 4 (+wheel) - if (id == static_cast(-1)) { - id = 4; - } - - // map button -2 to button 5 (-wheel) - else if (id == static_cast(-2)) { - id = 5; - } - - // map buttons 4, 5, etc. to 6, 7, etc. to make room for buttons - // 4 and 5 used to simulate the mouse wheel. - else if (id >= 4) { - id += 2; - } - - // check button is in legal range - if (id < 1 || id > m_buttons.size()) { - // out of range + switch (id) { + case kButtonLeft: + case kButtonRight: + case kButtonMiddle: + return static_cast(id); + case kButtonExtra0: + return 8; + case kButtonExtra1: + return 9; + default: return 0; } - - // map button - return static_cast(id); } void XWindowsScreen::warpCursorNoFlush(int32_t x, int32_t y)