From ad2243b0351d3d262ba09b97aa5f2ad0c569e578 Mon Sep 17 00:00:00 2001 From: rankun Date: Sat, 7 Mar 2020 21:49:39 +0800 Subject: [PATCH] fix: mousetap on mac --- QtScrcpy/device/ui/videoform.cpp | 14 ++------- QtScrcpy/util/mousetap/cocoamousetap.mm | 42 +++++++++++-------------- QtScrcpy/util/mousetap/mousetap.h | 1 + 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/QtScrcpy/device/ui/videoform.cpp b/QtScrcpy/device/ui/videoform.cpp index 69b0cad..f3e2aa3 100644 --- a/QtScrcpy/device/ui/videoform.cpp +++ b/QtScrcpy/device/ui/videoform.cpp @@ -81,17 +81,9 @@ void VideoForm::onGrabCursor(bool grab) rc.setBottomRight(rc.bottomRight() * m_videoWidget->devicePixelRatio()); MouseTap::getInstance()->enableMouseEventTap(rc, grab); #elif defined(Q_OS_OSX) - // get nswindow from qt widget - NSView *nsview = (NSView *)m_videoWidget->window()->winId(); - if (!nsview) { - return; - } - NSWindow *nswindow = [nsview window]; - - NSRect windowRect = [nswindow contentRectForFrameRect:[nswindow frame]]; - QRect rc(windowRect.origin.x, windowRect.origin.y, - windowRect.size.width, windowRect.size.height); - + QRect rc = m_videoWidget->geometry(); + rc.setTopLeft(m_videoWidget->mapToGlobal(rc.topLeft())); + rc.setBottomRight(m_videoWidget->mapToGlobal(rc.bottomRight())); rc.setX(rc.x() + 100); rc.setY(rc.y() + 30); rc.setWidth(rc.width() - 180); diff --git a/QtScrcpy/util/mousetap/cocoamousetap.mm b/QtScrcpy/util/mousetap/cocoamousetap.mm index 78afca8..67c009f 100644 --- a/QtScrcpy/util/mousetap/cocoamousetap.mm +++ b/QtScrcpy/util/mousetap/cocoamousetap.mm @@ -45,33 +45,29 @@ static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type return event; } - NSRect windowRect = NSMakeRect(tapdata->rc.left(), tapdata->rc.top(), + NSRect limitWindowRect = NSMakeRect(tapdata->rc.left(), tapdata->rc.top(), tapdata->rc.width(), tapdata->rc.height()); - NSRect newWindowRect = NSMakeRect(windowRect.origin.x, windowRect.origin.y, - windowRect.size.width - 10, windowRect.size.height - 10); - CGPoint eventLocation = CGEventGetUnflippedLocation(event); - //qDebug() << newWindowRect.origin.x << newWindowRect.origin.y << newWindowRect.size.width << newWindowRect.size.height; - - if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), newWindowRect, NO)) { - - /* This is in CGs global screenspace coordinate system, which has a - * flipped Y. - */ - CGPoint newLocation = CGEventGetLocation(event); - - if (eventLocation.x < NSMinX(windowRect)) { - newLocation.x = NSMinX(windowRect); - } else if (eventLocation.x >= NSMaxX(windowRect)) { - newLocation.x = NSMaxX(windowRect) - 1.0; + // check rect samll than limit rect + NSRect checkWindowRect = NSMakeRect(limitWindowRect.origin.x + 10, limitWindowRect.origin.y + 10, + limitWindowRect.size.width - 10, limitWindowRect.size.height - 10); + /* This is in CGs global screenspace coordinate system, which has a + * flipped Y. + */ + CGPoint eventLocation = CGEventGetLocation(event); + if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), checkWindowRect, NO)) { + if (eventLocation.x <= NSMinX(limitWindowRect)) { + eventLocation.x = NSMinX(limitWindowRect) + 1.0; + } else if (eventLocation.x >= NSMaxX(limitWindowRect)) { + eventLocation.x = NSMaxX(limitWindowRect) - 1.0; } - if (eventLocation.y <= NSMinY(windowRect)) { - newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1); - } else if (eventLocation.y > NSMaxY(windowRect)) { - newLocation.y += (eventLocation.y - NSMaxY(windowRect)); + if (eventLocation.y <= NSMinY(limitWindowRect)) { + eventLocation.y = NSMinY(limitWindowRect) + 1.0; + } else if (eventLocation.y >= NSMaxY(limitWindowRect)) { + eventLocation.y = NSMaxY(limitWindowRect) - 1.0; } - CGWarpMouseCursorPosition(newLocation); + CGWarpMouseCursorPosition(eventLocation); CGAssociateMouseAndMouseCursorPosition(YES); if ((CGEventMaskBit(type) & movementEventsMask) == 0) { @@ -80,7 +76,7 @@ static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type * movement events, since they mean that our warp cursor above * behaves strangely. */ - CGEventSetLocation(event, newLocation); + CGEventSetLocation(event, eventLocation); } } diff --git a/QtScrcpy/util/mousetap/mousetap.h b/QtScrcpy/util/mousetap/mousetap.h index ce1e323..8cad8f9 100644 --- a/QtScrcpy/util/mousetap/mousetap.h +++ b/QtScrcpy/util/mousetap/mousetap.h @@ -8,6 +8,7 @@ public: static MouseTap* getInstance(); virtual void initMouseEventTap() = 0; virtual void quitMouseEventTap() = 0; + // rc base global screenspace coordinate system, which has a flipped Y. virtual void enableMouseEventTap(QRect rc, bool enabled) = 0; private: