diff --git a/QtScrcpy/device/ui/toolform.cpp b/QtScrcpy/device/ui/toolform.cpp index 6fe898a..c21d142 100644 --- a/QtScrcpy/device/ui/toolform.cpp +++ b/QtScrcpy/device/ui/toolform.cpp @@ -7,9 +7,6 @@ #include "ui_toolform.h" #include "iconhelper.h" #include "device.h" -#include "videoform.h" -#include "controller.h" -#include "adbprocess.h" ToolForm::ToolForm(QWidget* adsorbWidget, AdsorbPositions adsorbPos) : MagneticWidget(adsorbWidget, adsorbPos) diff --git a/QtScrcpy/device/ui/videoform.cpp b/QtScrcpy/device/ui/videoform.cpp index 5972608..69b0cad 100644 --- a/QtScrcpy/device/ui/videoform.cpp +++ b/QtScrcpy/device/ui/videoform.cpp @@ -17,7 +17,6 @@ #include "toolform.h" #include "device.h" #include "controller.h" -#include "filehandler.h" #include "config.h" extern "C" { @@ -73,8 +72,32 @@ void VideoForm::initUI() void VideoForm::onGrabCursor(bool grab) { -#if defined(Q_OS_WIN32) || defined(Q_OS_OSX) - MouseTap::getInstance()->enableMouseEventTap(m_videoWidget, grab); +#if defined(Q_OS_WIN32) + QRect rc; + rc = QRect(m_videoWidget->parentWidget()->mapToGlobal(m_videoWidget->pos()) + , m_videoWidget->size()); + // high dpi support + rc.setTopLeft(rc.topLeft() * m_videoWidget->devicePixelRatio()); + 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); + + rc.setX(rc.x() + 100); + rc.setY(rc.y() + 30); + rc.setWidth(rc.width() - 180); + rc.setHeight(rc.height() - 60); + + MouseTap::getInstance()->enableMouseEventTap(rc, grab); #else Q_UNUSED(grab) #endif diff --git a/QtScrcpy/util/mousetap/cocoamousetap.h b/QtScrcpy/util/mousetap/cocoamousetap.h index 419887b..e1757f8 100644 --- a/QtScrcpy/util/mousetap/cocoamousetap.h +++ b/QtScrcpy/util/mousetap/cocoamousetap.h @@ -15,7 +15,7 @@ public: void initMouseEventTap() override; void quitMouseEventTap() override; - void enableMouseEventTap(QWidget* widget, bool enabled) override; + void enableMouseEventTap(QRect rc, bool enabled) override; protected: void run() override; diff --git a/QtScrcpy/util/mousetap/cocoamousetap.mm b/QtScrcpy/util/mousetap/cocoamousetap.mm index c03827c..78afca8 100644 --- a/QtScrcpy/util/mousetap/cocoamousetap.mm +++ b/QtScrcpy/util/mousetap/cocoamousetap.mm @@ -1,6 +1,5 @@ #import #include -#include #include "cocoamousetap.h" @@ -20,55 +19,37 @@ typedef struct MouseEventTapData{ CFMachPortRef tap = Q_NULLPTR; CFRunLoopRef runloop = Q_NULLPTR; CFRunLoopSourceRef runloopSource = Q_NULLPTR; - QWidget* widget = Q_NULLPTR; + QRect rc; } MouseEventTapData; static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { Q_UNUSED(proxy); MouseEventTapData *tapdata = (MouseEventTapData*)refcon; - - NSView *nsview; - NSWindow *nswindow; - NSRect windowRect; - NSRect newWindowRect; - CGPoint eventLocation; - switch (type) { case kCGEventTapDisabledByTimeout: { CGEventTapEnable(tapdata->tap, true); - return NULL; + return nullptr; } case kCGEventTapDisabledByUserInput: { - return NULL; + return nullptr; } default: break; } - if (!tapdata->widget) { + if (tapdata->rc.isEmpty()) { return event; } - // get nswindow from qt widget - nsview = (NSView *)tapdata->widget->window()->winId(); - if (!nsview) { - return event; - } - nswindow = [nsview window]; - eventLocation = CGEventGetUnflippedLocation(event); - windowRect = [nswindow contentRectForFrameRect:[nswindow frame]]; - - windowRect.origin.x += 100; - windowRect.origin.y += 30; - windowRect.size.width -= 180; - windowRect.size.height -= 60; - - newWindowRect = NSMakeRect(windowRect.origin.x, windowRect.origin.y, + NSRect windowRect = 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)) { @@ -171,11 +152,11 @@ void CocoaMouseTap::quitMouseEventTap() } } -void CocoaMouseTap::enableMouseEventTap(QWidget* widget, bool enabled) +void CocoaMouseTap::enableMouseEventTap(QRect rc, bool enabled) { if (m_tapData && m_tapData->tap) { - enabled ? m_tapData->widget = widget : m_tapData->widget = Q_NULLPTR; + enabled ? m_tapData->rc = rc : m_tapData->rc = QRect(); CGEventTapEnable(m_tapData->tap, enabled); } } diff --git a/QtScrcpy/util/mousetap/mousetap.h b/QtScrcpy/util/mousetap/mousetap.h index eab1389..ce1e323 100644 --- a/QtScrcpy/util/mousetap/mousetap.h +++ b/QtScrcpy/util/mousetap/mousetap.h @@ -1,12 +1,14 @@ #ifndef MOUSETAP_H #define MOUSETAP_H +#include + class QWidget; class MouseTap { public: static MouseTap* getInstance(); virtual void initMouseEventTap() = 0; virtual void quitMouseEventTap() = 0; - virtual void enableMouseEventTap(QWidget* widget, bool enabled) = 0; + virtual void enableMouseEventTap(QRect rc, bool enabled) = 0; private: static MouseTap *s_instance; diff --git a/QtScrcpy/util/mousetap/winmousetap.cpp b/QtScrcpy/util/mousetap/winmousetap.cpp index 7303998..ecbf369 100644 --- a/QtScrcpy/util/mousetap/winmousetap.cpp +++ b/QtScrcpy/util/mousetap/winmousetap.cpp @@ -24,17 +24,12 @@ void WinMouseTap::quitMouseEventTap() } -void WinMouseTap::enableMouseEventTap(QWidget *widget, bool enabled) +void WinMouseTap::enableMouseEventTap(QRect rc, bool enabled) { - if (!widget) { + if (enabled && rc.isEmpty()) { return; } if(enabled) { - QRect rc(widget->parentWidget()->mapToGlobal(widget->pos()) - , widget->size()); - // high dpi support - rc.setTopLeft(rc.topLeft() * widget->devicePixelRatio()); - rc.setBottomRight(rc.bottomRight() * widget->devicePixelRatio()); RECT mainRect; mainRect.left = (LONG)rc.left(); mainRect.right = (LONG)rc.right(); diff --git a/QtScrcpy/util/mousetap/winmousetap.h b/QtScrcpy/util/mousetap/winmousetap.h index 9b23f68..a5d4de1 100644 --- a/QtScrcpy/util/mousetap/winmousetap.h +++ b/QtScrcpy/util/mousetap/winmousetap.h @@ -11,7 +11,7 @@ public: void initMouseEventTap() override; void quitMouseEventTap() override; - void enableMouseEventTap(QWidget* widget, bool enabled) override; + void enableMouseEventTap(QRect rc, bool enabled) override; }; #endif // WINMOUSETAP_H