From 6ca68635e8cef8407e91bc9542edf196ef708210 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 17 Dec 2021 10:43:11 -0500 Subject: Make button numbers consistent across platforms There's no universal consensus on how buttons are numbered. Left, right, middle as 0, 1, 2 seems to be the most common convention on modern vaguely similar libraries, so I've gone with that. The switch to zero-based indices will obviously break all current client code. Particularly since now is the time to finish any breaking changes before a stable release, I think that is better than only changing the middle and right numbers, which would likely go unnoticed. --- include/pugl/pugl.h | 17 ++++++++++++++++- src/mac.m | 4 ++-- src/win.c | 8 ++++---- src/x11.c | 9 ++++++++- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/include/pugl/pugl.h b/include/pugl/pugl.h index 71578a9..4d26604 100644 --- a/include/pugl/pugl.h +++ b/include/pugl/pugl.h @@ -419,6 +419,21 @@ typedef struct { /** Button press or release event. + + Button numbers start from 0, and are ordered: primary, secondary, middle. + So, on a typical right-handed mouse, the button numbers are: + + Left: 0 + Right: 1 + Middle (often a wheel): 2 + + Higher button numbers are reported in the same order they are represented on + the system. There is no universal standard here, but buttons 3 and 4 are + typically a pair of buttons or a rocker, which are usually bound to "back" + and "forward" operations. + + Note that these numbers may differ from those used on the underlying + platform, since they are manipulated to provide a consistent portable API. */ typedef struct { PuglEventType type; ///< #PUGL_BUTTON_PRESS or #PUGL_BUTTON_RELEASE @@ -429,7 +444,7 @@ typedef struct { double xRoot; ///< Root-relative X coordinate double yRoot; ///< Root-relative Y coordinate PuglMods state; ///< Bitwise OR of #PuglMod flags - uint32_t button; ///< Button number starting from 1 + uint32_t button; ///< Button number starting from 0 } PuglButtonEvent; /** diff --git a/src/mac.m b/src/mac.m index 5a10964..84530b4 100644 --- a/src/mac.m +++ b/src/mac.m @@ -448,7 +448,7 @@ handleCrossing(PuglWrapperView* view, NSEvent* event, const PuglEventType type) rloc.x, [[NSScreen mainScreen] frame].size.height - rloc.y, getModifiers(event), - (uint32_t)[event buttonNumber] + 1, + (uint32_t)[event buttonNumber], }; PuglEvent pressEvent; @@ -469,7 +469,7 @@ handleCrossing(PuglWrapperView* view, NSEvent* event, const PuglEventType type) rloc.x, [[NSScreen mainScreen] frame].size.height - rloc.y, getModifiers(event), - (uint32_t)[event buttonNumber] + 1, + (uint32_t)[event buttonNumber], }; PuglEvent releaseEvent; diff --git a/src/win.c b/src/win.c index eb1d0d1..c7a95d5 100644 --- a/src/win.c +++ b/src/win.c @@ -680,22 +680,22 @@ handleMessage(PuglView* view, UINT message, WPARAM wParam, LPARAM lParam) view->impl->mouseTracked = false; break; case WM_LBUTTONDOWN: - initMouseEvent(&event, view, 1, true, lParam); + initMouseEvent(&event, view, 0, true, lParam); break; case WM_MBUTTONDOWN: initMouseEvent(&event, view, 2, true, lParam); break; case WM_RBUTTONDOWN: - initMouseEvent(&event, view, 3, true, lParam); + initMouseEvent(&event, view, 1, true, lParam); break; case WM_LBUTTONUP: - initMouseEvent(&event, view, 1, false, lParam); + initMouseEvent(&event, view, 0, false, lParam); break; case WM_MBUTTONUP: initMouseEvent(&event, view, 2, false, lParam); break; case WM_RBUTTONUP: - initMouseEvent(&event, view, 3, false, lParam); + initMouseEvent(&event, view, 1, false, lParam); break; case WM_MOUSEWHEEL: initScrollEvent(&event, view, lParam); diff --git a/src/x11.c b/src/x11.c index 614b7ac..ed249a8 100644 --- a/src/x11.c +++ b/src/x11.c @@ -741,7 +741,14 @@ translateEvent(PuglView* const view, XEvent xevent) event.button.xRoot = xevent.xbutton.x_root; event.button.yRoot = xevent.xbutton.y_root; event.button.state = translateModifiers(xevent.xbutton.state); - event.button.button = xevent.xbutton.button; + event.button.button = xevent.xbutton.button - 1; + if (event.button.button == 1) { + event.button.button = 2; + } else if (event.button.button == 2) { + event.button.button = 1; + } else if (event.button.button >= 7) { + event.button.button -= 4; + } } break; case KeyPress: -- cgit v1.2.1