7#ifndef QT_NO_TABLETEVENT
9#include <QtGui/qpointingdevice.h>
10#include <QtCore/private/qflatmap_p.h>
12using QCocoaTabletDeviceMap = QFlatMap<qint64,
const QPointingDevice*>;
13Q_GLOBAL_STATIC(QCocoaTabletDeviceMap, devicesInProximity)
15@implementation QNSView (Tablet)
17- (
bool)handleTabletEvent:(NSEvent *)theEvent
19 static bool ignoreButtonMapping = qEnvironmentVariableIsSet(
"QT_MAC_TABLET_IGNORE_BUTTON_MAPPING");
21 if (!m_platformWindow)
24 NSEventType eventType = [theEvent type];
25 if (eventType != NSEventTypeTabletPoint && [theEvent subtype] != NSEventSubtypeTabletPoint)
28 ulong timestamp = [theEvent timestamp] * 1000;
30 QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag();
31 nativeDrag->setLastInputEvent(theEvent, self);
35 [self convertFromScreen:[self screenMousePoint:theEvent] toWindowPoint: &windowPoint andScreenPoint: &screenPoint];
39 const auto deviceId = theEvent.deviceID;
40 const auto *device = devicesInProximity->value(deviceId);
41 if (!device && deviceId == 0) {
44 device = tabletToolInstance(theEvent);
45 devicesInProximity->insert(deviceId, device);
48 if (Q_UNLIKELY(!device))
51 bool down = (eventType != NSEventTypeMouseMoved);
55 pressure = [theEvent pressure];
60 NSPoint tilt = [theEvent tilt];
61 int xTilt = qRound(tilt.x * 60.0);
62 int yTilt = qRound(tilt.y * -60.0);
63 qreal tangentialPressure = 0;
66 if (device->hasCapability(QInputDevice::Capability::ZPosition))
67 z = [theEvent absoluteZ];
69 if (device->hasCapability(QInputDevice::Capability::TangentialPressure))
70 tangentialPressure = ([theEvent tangentialPressure] * 2.0) - 1.0;
72 rotation = 360.0 - [theEvent rotation];
76 Qt::KeyboardModifiers keyboardModifiers = QAppleKeyMapper::fromCocoaModifiers(theEvent.modifierFlags);
77 Qt::MouseButtons buttons = ignoreButtonMapping ?
static_cast<Qt::MouseButtons>(
static_cast<uint>([theEvent buttonMask])) : m_buttons;
79 QWindowSystemInterface::handleTabletEvent(m_platformWindow->window(), timestamp, device, windowPoint, screenPoint,
80 buttons, pressure, xTilt, yTilt, tangentialPressure, rotation, z, keyboardModifiers);
84- (
void)tabletPoint:(NSEvent *)theEvent
86 if ([self isTransparentForUserInput])
87 return [super tabletPoint:theEvent];
89 [self handleTabletEvent: theEvent];
93
94
95
96
97
98
99static const QPointingDevice *tabletToolInstance(NSEvent *theEvent)
101 qint64 uid = theEvent.uniqueID;
102 uint bits = theEvent.vendorPointingDeviceType;
103 if (bits == 0 && uid != 0) {
112 static const quint32 CursorTypeBitMask = 0x0F06;
113 quint32 toolId = bits & CursorTypeBitMask;
114 QInputDevice::Capabilities caps = QInputDevice::Capability::Position |
115 QInputDevice::Capability::Pressure | QInputDevice::Capability::Hover;
116 QInputDevice::DeviceType device;
118 if (((bits & 0x0006) == 0x0002) && (toolId != 0x0902)) {
119 device = QInputDevice::DeviceType::Stylus;
124 device = QInputDevice::DeviceType::Stylus;
127 device = QInputDevice::DeviceType::Airbrush;
128 caps.setFlag(QInputDevice::Capability::TangentialPressure);
132 device = QInputDevice::DeviceType::Mouse;
133 caps.setFlag(QInputDevice::Capability::Scroll);
136 device = QInputDevice::DeviceType::Puck;
139 device = QInputDevice::DeviceType::Stylus;
140 caps.setFlag(QInputDevice::Capability::Rotation);
144 device = QInputDevice::DeviceType::Unknown;
148 uint capabilityMask = theEvent.capabilityMask;
149 if (capabilityMask & 0x0200)
150 caps.setFlag(QInputDevice::Capability::ZPosition);
151 if (capabilityMask & 0x0800)
152 Q_ASSERT(caps.testFlag(QInputDevice::Capability::TangentialPressure));
154 QPointingDevice::PointerType pointerType = QPointingDevice::PointerType::Unknown;
155 switch (theEvent.pointingDeviceType) {
156 case NSPointingDeviceTypeUnknown:
159 case NSPointingDeviceTypePen:
160 pointerType = QPointingDevice::PointerType::Pen;
162 case NSPointingDeviceTypeCursor:
163 pointerType = QPointingDevice::PointerType::Cursor;
165 case NSPointingDeviceTypeEraser:
166 pointerType = QPointingDevice::PointerType::Eraser;
170 const auto uniqueID = QPointingDeviceUniqueId::fromNumericId(uid);
171 auto windowSystemId = theEvent.deviceID;
172 const QPointingDevice *ret = QPointingDevicePrivate::queryTabletDevice(device, pointerType, uniqueID, caps, windowSystemId);
176 ret =
new QPointingDevice(QString(), windowSystemId, device, pointerType,
177 caps, 1, buttonCount, QString(), uniqueID, QCocoaIntegration::instance());
178 QWindowSystemInterface::registerInputDevice(ret);
180 QPointingDevicePrivate *devPriv = QPointingDevicePrivate::get(
const_cast<QPointingDevice *>(ret));
181 devPriv->toolId = toolId;
185- (
void)tabletProximity:(NSEvent *)theEvent
187 if ([self isTransparentForUserInput])
188 return [super tabletProximity:theEvent];
190 const ulong timestamp = theEvent.timestamp * 1000;
191 const qint64 windowSystemId = theEvent.deviceID;
192 const QPointingDevice *device = tabletToolInstance(theEvent);
194 QWindowSystemInterface::handleTabletEnterLeaveProximityEvent(
nullptr, timestamp, device, theEvent.isEnteringProximity);
196 if (theEvent.isEnteringProximity)
197 devicesInProximity->insert_or_assign(windowSystemId, device);
199 devicesInProximity->remove(windowSystemId);