From 9f15b1bcf2953acf609f7580463bf13cdb23d008 Mon Sep 17 00:00:00 2001 From: Evan Maddock Date: Mon, 5 Aug 2019 21:59:43 -0400 Subject: [PATCH] Reimplement patch for horizontal scrolling and extra mouse buttons Signed-off-by: Evan Maddock --- .gitignore | 1 + src/lib/barrier/mouse_types.h | 5 +- src/lib/platform/MSWindowsDesks.cpp | 22 ++++- src/lib/platform/MSWindowsHook.cpp | 11 +++ src/lib/platform/MSWindowsScreen.cpp | 11 +-- src/lib/platform/MSWindowsScreen.h | 2 +- src/lib/platform/OSXScreen.mm | 1 + src/lib/platform/XWindowsScreen.cpp | 138 ++++++++++++++------------- src/lib/platform/XWindowsScreen.h | 10 +- 9 files changed, 120 insertions(+), 81 deletions(-) diff --git a/.gitignore b/.gitignore index feeb09e0..10c604b0 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ CMakeCache.txt .vscode/ # Transient in-project-directory dependencies /deps/ +/out/build/x64-Debug diff --git a/src/lib/barrier/mouse_types.h b/src/lib/barrier/mouse_types.h index cf860c00..62a23965 100644 --- a/src/lib/barrier/mouse_types.h +++ b/src/lib/barrier/mouse_types.h @@ -32,10 +32,13 @@ static const ButtonID kButtonNone = 0; static const ButtonID kButtonLeft = 1; static const ButtonID kButtonMiddle = 2; static const ButtonID kButtonRight = 3; +// mouse button 4 static const ButtonID kButtonExtra0 = 4; +// mouse button 5 +static const ButtonID kButtonExtra1 = 5; static const ButtonID kMacButtonRight = 2; static const ButtonID kMacButtonMiddle = 3; //@} -static const UInt8 NumButtonIDs = 5; +static const UInt8 NumButtonIDs = 6; diff --git a/src/lib/platform/MSWindowsDesks.cpp b/src/lib/platform/MSWindowsDesks.cpp index b43a218c..308c427b 100644 --- a/src/lib/platform/MSWindowsDesks.cpp +++ b/src/lib/platform/MSWindowsDesks.cpp @@ -45,6 +45,10 @@ #define SPI_GETSCREENSAVERRUNNING 114 #endif +#if !defined(MOUSEEVENTF_HWHEEL) +#define MOUSEEVENTF_HWHEEL 0x1000 +#endif + // X button stuff #if !defined(WM_XBUTTONDOWN) #define WM_XBUTTONDOWN 0x020B @@ -296,12 +300,12 @@ 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; @@ -602,6 +606,16 @@ MSWindowsDesks::deskThread(void* vdesk) { MSG msg; + BOOL vistaOrGreater = FALSE; + + { + OSVERSIONINFOW osvi; + osvi.dwOSVersionInfoSize = sizeof(osvi); + if (GetVersionExW(&osvi)) { + vistaOrGreater = osvi.dwMajorVersion >= 6; + } + } + // use given desktop for this thread Desk* desk = static_cast(vdesk); desk->m_threadID = GetCurrentThreadId(); @@ -686,10 +700,12 @@ MSWindowsDesks::deskThread(void* vdesk) break; case BARRIER_MSG_FAKE_WHEEL: - // XXX -- add support for x-axis scrolling if (msg.lParam != 0) { mouse_event(MOUSEEVENTF_WHEEL, 0, 0, (DWORD)msg.lParam, 0); } + else if (vistaOrGreater && msg.wParam != 0) { + mouse_event(MOUSEEVENTF_HWHEEL, 0, 0, (DWORD)msg.wParam, 0); + } break; case BARRIER_MSG_CURSOR_POS: { diff --git a/src/lib/platform/MSWindowsHook.cpp b/src/lib/platform/MSWindowsHook.cpp index 2d7845a0..b9b97404 100644 --- a/src/lib/platform/MSWindowsHook.cpp +++ b/src/lib/platform/MSWindowsHook.cpp @@ -25,6 +25,10 @@ #include "common/DataDirectories.h" #include "base/Log.h" +#ifndef WM_MOUSEHWHEEL +#define WM_MOUSEHWHEEL 0x020E +#endif + // // debugging compile flag. when not zero the server doesn't grab // the keyboard when the mouse leaves the server screen. this @@ -473,6 +477,13 @@ mouseHookHandler(WPARAM wParam, SInt32 x, SInt32 y, SInt32 data) } return (g_mode == kHOOK_RELAY_EVENTS); + case WM_MOUSEHWHEEL: + if (g_mode == kHOOK_RELAY_EVENTS) { + // relay event + PostThreadMessage(g_threadID, BARRIER_MSG_MOUSE_WHEEL, 0, data); + } + return (g_mode == kHOOK_RELAY_EVENTS); + case WM_NCMOUSEMOVE: case WM_MOUSEMOVE: if (g_mode == kHOOK_RELAY_EVENTS) { diff --git a/src/lib/platform/MSWindowsScreen.cpp b/src/lib/platform/MSWindowsScreen.cpp index 5246f963..27170342 100644 --- a/src/lib/platform/MSWindowsScreen.cpp +++ b/src/lib/platform/MSWindowsScreen.cpp @@ -952,9 +952,9 @@ MSWindowsScreen::updateButtons() m_buttons[kButtonLeft] = (GetKeyState(VK_LBUTTON) < 0); m_buttons[kButtonRight] = (GetKeyState(VK_RBUTTON) < 0); m_buttons[kButtonMiddle] = (GetKeyState(VK_MBUTTON) < 0); - m_buttons[kButtonExtra0 + 0] = (numButtons >= 4) && + m_buttons[kButtonExtra0] = (numButtons >= 4) && (GetKeyState(VK_XBUTTON1) < 0); - m_buttons[kButtonExtra0 + 1] = (numButtons >= 5) && + m_buttons[kButtonExtra1] = (numButtons >= 5) && (GetKeyState(VK_XBUTTON2) < 0); } @@ -1007,8 +1007,7 @@ MSWindowsScreen::onPreDispatchPrimary(HWND, static_cast(lParam)); case BARRIER_MSG_MOUSE_WHEEL: - // XXX -- support x-axis scrolling - return onMouseWheel(0, static_cast(wParam)); + return onMouseWheel(static_cast(lParam), static_cast(wParam)); case BARRIER_MSG_PRE_WARP: { @@ -1670,13 +1669,13 @@ 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 4245d6c3..14cd5053 100644 --- a/src/lib/platform/MSWindowsScreen.h +++ b/src/lib/platform/MSWindowsScreen.h @@ -307,7 +307,7 @@ private: HotKeyToIDMap m_hotKeyToIDMap; // map of button state - bool m_buttons[1 + kButtonExtra0 + 1]; + bool m_buttons[NumButtonIDs]; // the system shows the mouse cursor when an internal display count // is >= 0. this count is maintained per application but there's diff --git a/src/lib/platform/OSXScreen.mm b/src/lib/platform/OSXScreen.mm index 8c7e24c6..02caaaec 100644 --- a/src/lib/platform/OSXScreen.mm +++ b/src/lib/platform/OSXScreen.mm @@ -422,6 +422,7 @@ OSXScreen::constructMouseButtonEventMap() {kCGEventRightMouseUp, kCGEventRightMouseDragged, kCGEventRightMouseDown}, {kCGEventOtherMouseUp, kCGEventOtherMouseDragged, kCGEventOtherMouseDown}, {kCGEventOtherMouseUp, kCGEventOtherMouseDragged, kCGEventOtherMouseDown}, + {kCGEventOtherMouseUp, kCGEventOtherMouseDragged, kCGEventOtherMouseDown}, {kCGEventOtherMouseUp, kCGEventOtherMouseDragged, kCGEventOtherMouseDown} }; diff --git a/src/lib/platform/XWindowsScreen.cpp b/src/lib/platform/XWindowsScreen.cpp index 5f6c6235..3bde336f 100644 --- a/src/lib/platform/XWindowsScreen.cpp +++ b/src/lib/platform/XWindowsScreen.cpp @@ -66,7 +66,8 @@ XWindowsScreen::XWindowsScreen( IEventQueue* events) : m_isPrimary(isPrimary), m_mouseScrollDelta(mouseScrollDelta), - m_accumulatedScroll(0), + m_x_accumulatedScroll(0), + m_y_accumulatedScroll(0), m_display(NULL), m_root(None), m_window(None), @@ -829,35 +830,37 @@ XWindowsScreen::fakeMouseRelativeMove(SInt32 dx, SInt32 dy) const } void -XWindowsScreen::fakeMouseWheel(SInt32, SInt32 yDelta) const +XWindowsScreen::fakeMouseWheel(SInt32 xDelta, SInt32 yDelta) const { - // XXX -- support x-axis scrolling - if (yDelta == 0) { - return; - } + int numEvents; - int numEvents = accumulateMouseScroll(yDelta); + if ((!xDelta && !yDelta) || (xDelta && yDelta)) { + // Invalid scrolling inputs + return; + } - // choose button depending on rotation direction - const unsigned int xButton = mapButtonToX(static_cast( - (numEvents >= 0) ? -1 : -2)); - if (xButton == 0) { - // If we get here, then the XServer does not support the scroll - // wheel buttons, so send PageUp/PageDown keystrokes instead. - // Patch by Tom Chadwick. - KeyCode keycode = 0; - if (yDelta >= 0) { - keycode = m_impl->XKeysymToKeycode(m_display, XK_Page_Up); - } - else { - keycode = m_impl->XKeysymToKeycode(m_display, XK_Page_Down); - } - if (keycode != 0) { - m_impl->XTestFakeKeyEvent(m_display, keycode, True, CurrentTime); - m_impl->XTestFakeKeyEvent(m_display, keycode, False, CurrentTime); - } - return; - } + // 4, 5, 6, 7 + // up, down, left, right + unsigned int xButton; + + if (yDelta) { // vertical scroll + numEvents = y_accumulateMouseScroll(yDelta); + if (numEvents >= 0) { + xButton = 4; // up + } + else { + xButton = 5; // down + } + } + else { // horizontal scroll + numEvents = x_accumulateMouseScroll(xDelta); + if (numEvents >= 0) { + xButton = 7; // right + } + else { + xButton = 6; // left + } + } numEvents = std::abs(numEvents); @@ -1540,7 +1543,14 @@ XWindowsScreen::onMouseRelease(const XButtonEvent& xbutton) // wheel backward (toward user) sendEvent(m_events->forIPrimaryScreen().wheel(), WheelInfo::alloc(0, -120)); } - // XXX -- support x-axis scrolling + else if (xbutton.button == 6) { + // wheel left + sendEvent(m_events->forIPrimaryScreen().wheel(), WheelInfo::alloc(-120, 0)); + } + else if (xbutton.button == 7) { + // wheel right + sendEvent(m_events->forIPrimaryScreen().wheel(), WheelInfo::alloc(120, 0)); + } } void @@ -1613,11 +1623,20 @@ XWindowsScreen::onMouseMove(const XMotionEvent& xmotion) } int -XWindowsScreen::accumulateMouseScroll(SInt32 yDelta) const +XWindowsScreen::x_accumulateMouseScroll(SInt32 xDelta) const { - m_accumulatedScroll += yDelta; - int numEvents = m_accumulatedScroll / m_mouseScrollDelta; - m_accumulatedScroll -= numEvents * m_mouseScrollDelta; + m_x_accumulatedScroll += xDelta; + int numEvents = m_x_accumulatedScroll / m_mouseScrollDelta; + m_x_accumulatedScroll -= numEvents * m_mouseScrollDelta; + return numEvents; +} + +int +XWindowsScreen::y_accumulateMouseScroll(SInt32 yDelta) const +{ + m_y_accumulatedScroll += yDelta; + int numEvents = m_y_accumulatedScroll / m_mouseScrollDelta; + m_y_accumulatedScroll -= numEvents * m_mouseScrollDelta; return numEvents; } @@ -1840,19 +1859,19 @@ 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) { + // http://xahlee.info/linux/linux_x11_mouse_button_number.html + // and the program `xev` + switch (button) + { + case 1: case 2: case 3: // kButtonLeft, Middle, Right 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 { + case 4: case 5: case 6: case 7: // scroll up, down, left, right -- ignored here + return kButtonNone; + case 8: // mouse button 4 + return kButtonExtra0; + case 9: // mouse button 5 + return kButtonExtra0; + default: // unknown button return kButtonNone; } } @@ -1860,30 +1879,17 @@ XWindowsScreen::mapButtonFromX(const XButtonEvent* event) const 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 kButtonMiddle: case kButtonRight: + return id; + case kButtonExtra0: + return 8; + case kButtonExtra1: + return 9; + default: return 0; } - - // map button - return static_cast(id); } void diff --git a/src/lib/platform/XWindowsScreen.h b/src/lib/platform/XWindowsScreen.h index 71340173..55738397 100644 --- a/src/lib/platform/XWindowsScreen.h +++ b/src/lib/platform/XWindowsScreen.h @@ -139,7 +139,8 @@ private: // Returns the number of scroll events needed after the current delta has // been taken into account - int accumulateMouseScroll(SInt32 yDelta) const; + int x_accumulateMouseScroll(SInt32 xDelta) const; + int y_accumulateMouseScroll(SInt32 yDelta) const; bool detectXI2(); #ifdef HAVE_XI2 @@ -183,10 +184,11 @@ private: // The size of a smallest supported scroll event, in points int m_mouseScrollDelta; - // Accumulates scrolls of less than m_mouseScrollDelta across multiple + // Accumulates scrolls of less than m_?_mouseScrollDelta across multiple // scroll events. We dispatch a scroll event whenever the accumulated scroll - // becomes larger than m_mouseScrollDelta - mutable int m_accumulatedScroll; + // becomes larger than m_?_mouseScrollDelta + mutable int m_x_accumulatedScroll; + mutable int m_y_accumulatedScroll; Display* m_display; Window m_root;