|
|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QWidget>
|
|
|
|
|
|
|
|
|
|
#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) {
|
|
|
|
|
return event;
|
|
|
|
|
}
|
|
|
|
|
// get nswindow from qt widget
|
|
|
|
|
nsview = (NSView *)tapdata->widget->window()->winId();
|
|
|
|
|
if (!nsview) {
|
|
|
|
|
if (tapdata->rc.isEmpty()) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|