Skip to content

Commit

Permalink
Cocoa: Send correct mouse buttons for tablets
Browse files Browse the repository at this point in the history
Tablet vendors allow user configurable pen buttons
where the user may assign a logical mouse button to
a given physical button.

In the case of Wacom tablets this mapping is not reflected
in the buttonMask API, which returns the state of
the physical buttons.

Use NSEvent buttonNummber instead, which returns the
logical button number, after applying user mappings.
Unifiy button state stacking with the mouse handlers.

Handle a special case where buttonNumber returns 0
for tablet right mouse presses. We get these events
via rightMouse* event handlers and can hardcode the
button number.

Change-Id: I06b9b1aa98c49b84f7e3871e694c22c7ad0169d6
Task-number: QTBUG-57487
Task-number: QTBUG-54160
Reviewed-by: Shawn Rutledge <[email protected]>
  • Loading branch information
Morten Johan Sørvig authored and ec1oud committed Apr 24, 2017
1 parent 148188f commit 8bc36c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/plugins/platforms/cocoa/qnsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(QNSViewMouseMoveHelper));
- (void)resetMouseButtons;

- (void)handleMouseEvent:(NSEvent *)theEvent;
- (bool)handleMouseDownEvent:(NSEvent *)theEvent;
- (bool)handleMouseDraggedEvent:(NSEvent *)theEvent;
- (bool)handleMouseUpEvent:(NSEvent *)theEvent;
- (bool)handleMouseDownEvent:(NSEvent *)theEvent withButton:(int)buttonNumber;
- (bool)handleMouseDraggedEvent:(NSEvent *)theEvent withButton:(int)buttonNumber;
- (bool)handleMouseUpEvent:(NSEvent *)theEvent withButton:(int)buttonNumber;
- (void)mouseDown:(NSEvent *)theEvent;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
Expand Down
36 changes: 19 additions & 17 deletions src/plugins/platforms/cocoa/qnsview.mm
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,12 @@ - (void)handleFrameStrutMouseEvent:(NSEvent *)theEvent
QWindowSystemInterface::handleFrameStrutMouseEvent(m_platformWindow->window(), timestamp, qtWindowPoint, qtScreenPoint, m_frameStrutButtons);
}

- (bool)handleMouseDownEvent:(NSEvent *)theEvent
- (bool)handleMouseDownEvent:(NSEvent *)theEvent withButton:(int)buttonNumber
{
if ([self isTransparentForUserInput])
return false;

Qt::MouseButton button = cocoaButton2QtButton([theEvent buttonNumber]);
Qt::MouseButton button = cocoaButton2QtButton(buttonNumber);

QPointF qtWindowPoint;
QPointF qtScreenPoint;
Expand All @@ -753,12 +753,12 @@ - (bool)handleMouseDownEvent:(NSEvent *)theEvent
return true;
}

- (bool)handleMouseDraggedEvent:(NSEvent *)theEvent
- (bool)handleMouseDraggedEvent:(NSEvent *)theEvent withButton:(int)buttonNumber
{
if ([self isTransparentForUserInput])
return false;

Qt::MouseButton button = cocoaButton2QtButton([theEvent buttonNumber]);
Qt::MouseButton button = cocoaButton2QtButton(buttonNumber);

// Forward the event to the next responder if Qt did not accept the
// corresponding mouse down for this button
Expand All @@ -769,12 +769,12 @@ - (bool)handleMouseDraggedEvent:(NSEvent *)theEvent
return true;
}

- (bool)handleMouseUpEvent:(NSEvent *)theEvent
- (bool)handleMouseUpEvent:(NSEvent *)theEvent withButton:(int)buttonNumber
{
if ([self isTransparentForUserInput])
return false;

Qt::MouseButton button = cocoaButton2QtButton([theEvent buttonNumber]);
Qt::MouseButton button = cocoaButton2QtButton(buttonNumber);

// Forward the event to the next responder if Qt did not accept the
// corresponding mouse down for this button
Expand Down Expand Up @@ -864,56 +864,59 @@ - (void)mouseDown:(NSEvent *)theEvent

- (void)mouseDragged:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseDraggedEvent:theEvent];
const bool accepted = [self handleMouseDraggedEvent:theEvent withButton:[theEvent buttonNumber]];
if (!accepted)
[super mouseDragged:theEvent];
}

- (void)mouseUp:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseUpEvent:theEvent];
const bool accepted = [self handleMouseUpEvent:theEvent withButton:[theEvent buttonNumber]];
if (!accepted)
[super mouseUp:theEvent];
}

- (void)rightMouseDown:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseDownEvent:theEvent];
// Wacom tablet might not return the correct button number for NSEvent buttonNumber
// on right clicks. Decide here that the button is the "right" button and forward
// the button number to the mouse (and tablet) handler.
const bool accepted = [self handleMouseDownEvent:theEvent withButton:1];
if (!accepted)
[super rightMouseDown:theEvent];
}

- (void)rightMouseDragged:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseDraggedEvent:theEvent];
const bool accepted = [self handleMouseDraggedEvent:theEvent withButton:1];
if (!accepted)
[super rightMouseDragged:theEvent];
}

- (void)rightMouseUp:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseUpEvent:theEvent];
const bool accepted = [self handleMouseUpEvent:theEvent withButton:1];
if (!accepted)
[super rightMouseUp:theEvent];
}

- (void)otherMouseDown:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseDownEvent:theEvent];
const bool accepted = [self handleMouseDownEvent:theEvent withButton:[theEvent buttonNumber]];
if (!accepted)
[super otherMouseDown:theEvent];
}

- (void)otherMouseDragged:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseDraggedEvent:theEvent];
const bool accepted = [self handleMouseDraggedEvent:theEvent withButton:[theEvent buttonNumber]];
if (!accepted)
[super otherMouseDragged:theEvent];
}

- (void)otherMouseUp:(NSEvent *)theEvent
{
const bool accepted = [self handleMouseUpEvent:theEvent];
const bool accepted = [self handleMouseUpEvent:theEvent withButton:[theEvent buttonNumber]];
if (!accepted)
[super otherMouseUp:theEvent];
}
Expand Down Expand Up @@ -1071,7 +1074,6 @@ - (bool)handleTabletEvent: (NSEvent *)theEvent
NSPoint tilt = [theEvent tilt];
int xTilt = qRound(tilt.x * 60.0);
int yTilt = qRound(tilt.y * -60.0);
Qt::MouseButtons buttons = static_cast<Qt::MouseButtons>(static_cast<uint>([theEvent buttonMask]));
qreal tangentialPressure = 0;
qreal rotation = 0;
int z = 0;
Expand All @@ -1090,10 +1092,10 @@ - (bool)handleTabletEvent: (NSEvent *)theEvent
qCDebug(lcQpaTablet, "event on tablet %d with tool %d type %d unique ID %lld pos %6.1f, %6.1f root pos %6.1f, %6.1f buttons 0x%x pressure %4.2lf tilt %d, %d rotation %6.2lf",
deviceId, deviceData.device, deviceData.pointerType, deviceData.uid,
windowPoint.x(), windowPoint.y(), screenPoint.x(), screenPoint.y(),
static_cast<uint>(buttons), pressure, xTilt, yTilt, rotation);
static_cast<uint>(m_buttons), pressure, xTilt, yTilt, rotation);

QWindowSystemInterface::handleTabletEvent(m_platformWindow->window(), timestamp, windowPoint, screenPoint,
deviceData.device, deviceData.pointerType, buttons, pressure, xTilt, yTilt,
deviceData.device, deviceData.pointerType, m_buttons, pressure, xTilt, yTilt,
tangentialPressure, rotation, z, deviceData.uid,
keyboardModifiers);
return true;
Expand Down

0 comments on commit 8bc36c7

Please sign in to comment.