Browse Source

mouse: Move mouse button state from a Uint32 to a formal typedef.

Reference Issue #9812.
Ryan C. Gordon 10 months ago
parent
commit
cf2874080f

+ 1 - 1
include/SDL3/SDL_events.h

@@ -380,7 +380,7 @@ typedef struct SDL_MouseMotionEvent
     Uint64 timestamp;   /**< In nanoseconds, populated using SDL_GetTicksNS() */
     SDL_WindowID windowID; /**< The window with mouse focus, if any */
     SDL_MouseID which;  /**< The mouse instance id, SDL_TOUCH_MOUSEID, or SDL_PEN_MOUSEID */
-    Uint32 state;       /**< The current button state */
+    SDL_MouseButtonFlags state;       /**< The current button state */
     float x;            /**< X coordinate, relative to window */
     float y;            /**< Y coordinate, relative to window */
     float xrel;         /**< The relative motion in the X direction */

+ 34 - 27
include/SDL3/SDL_mouse.h

@@ -83,6 +83,37 @@ typedef enum SDL_MouseWheelDirection
     SDL_MOUSEWHEEL_FLIPPED    /**< The scroll direction is flipped / natural */
 } SDL_MouseWheelDirection;
 
+/**
+ * A bitmask used when testing if a mouse's buttons are pressed.
+ *
+ * - Button 1: Left mouse button
+ * - Button 2: Middle mouse button
+ * - Button 3: Right mouse button
+ * - Button 4: Side mouse button 1
+ * - Button 5: Side mouse button 2
+ *
+ * \since This datatype is available since SDL 3.0.0.
+ *
+ * \sa SDL_GetMouseState
+ * \sa SDL_GetGlobalMouseState
+ * \sa SDL_GetRelativeMouseState
+ */
+typedef Uint32 SDL_MouseButtonFlags;
+
+#define SDL_BUTTON_LEFT     1
+#define SDL_BUTTON_MIDDLE   2
+#define SDL_BUTTON_RIGHT    3
+#define SDL_BUTTON_X1       4
+#define SDL_BUTTON_X2       5
+
+#define SDL_BUTTON(X)       (1u << ((X)-1))
+#define SDL_BUTTON_LMASK    SDL_BUTTON(SDL_BUTTON_LEFT)
+#define SDL_BUTTON_MMASK    SDL_BUTTON(SDL_BUTTON_MIDDLE)
+#define SDL_BUTTON_RMASK    SDL_BUTTON(SDL_BUTTON_RIGHT)
+#define SDL_BUTTON_X1MASK   SDL_BUTTON(SDL_BUTTON_X1)
+#define SDL_BUTTON_X2MASK   SDL_BUTTON(SDL_BUTTON_X2)
+
+
 /* Function prototypes */
 
 /**
@@ -160,7 +191,7 @@ extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetMouseFocus(void);
  * \sa SDL_GetGlobalMouseState
  * \sa SDL_GetRelativeMouseState
  */
-extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetMouseState(float *x, float *y);
+extern SDL_DECLSPEC SDL_MouseButtonFlags SDLCALL SDL_GetMouseState(float *x, float *y);
 
 /**
  * Get the current state of the mouse in relation to the desktop.
@@ -190,7 +221,7 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetMouseState(float *x, float *y);
  * \sa SDL_CaptureMouse
  * \sa SDL_GetMouseState
  */
-extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(float *x, float *y);
+extern SDL_DECLSPEC SDL_MouseButtonFlags SDLCALL SDL_GetGlobalMouseState(float *x, float *y);
 
 /**
  * Retrieve the relative state of the mouse.
@@ -209,7 +240,7 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(float *x, float *y);
  *
  * \sa SDL_GetMouseState
  */
-extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetRelativeMouseState(float *x, float *y);
+extern SDL_DECLSPEC SDL_MouseButtonFlags SDLCALL SDL_GetRelativeMouseState(float *x, float *y);
 
 /**
  * Move the mouse cursor to the given position within the window.
@@ -510,30 +541,6 @@ extern SDL_DECLSPEC int SDLCALL SDL_HideCursor(void);
  */
 extern SDL_DECLSPEC SDL_bool SDLCALL SDL_CursorVisible(void);
 
-/**
- * Used as a mask when testing buttons in buttonstate.
- *
- * - Button 1: Left mouse button
- * - Button 2: Middle mouse button
- * - Button 3: Right mouse button
- * - Button 4: Side mouse button 1
- * - Button 5: Side mouse button 2
- *
- * \since This macro is available since SDL 3.0.0.
- */
-#define SDL_BUTTON(X)       (1u << ((X)-1))
-
-#define SDL_BUTTON_LEFT     1
-#define SDL_BUTTON_MIDDLE   2
-#define SDL_BUTTON_RIGHT    3
-#define SDL_BUTTON_X1       4
-#define SDL_BUTTON_X2       5
-#define SDL_BUTTON_LMASK    SDL_BUTTON(SDL_BUTTON_LEFT)
-#define SDL_BUTTON_MMASK    SDL_BUTTON(SDL_BUTTON_MIDDLE)
-#define SDL_BUTTON_RMASK    SDL_BUTTON(SDL_BUTTON_RIGHT)
-#define SDL_BUTTON_X1MASK   SDL_BUTTON(SDL_BUTTON_X1)
-#define SDL_BUTTON_X2MASK   SDL_BUTTON(SDL_BUTTON_X2)
-
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
 }

+ 3 - 3
src/dynapi/SDL_dynapi_procs.h

@@ -313,7 +313,7 @@ SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetGamepadType,(SDL_Gamepad *a),(a),return)
 SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetGamepadTypeFromString,(const char *a),(a),return)
 SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadVendor,(SDL_Gamepad *a),(a),return)
 SDL_DYNAPI_PROC(SDL_JoystickID*,SDL_GetGamepads,(int *a),(a),return)
-SDL_DYNAPI_PROC(Uint32,SDL_GetGlobalMouseState,(float *a, float *b),(a,b),return)
+SDL_DYNAPI_PROC(SDL_MouseButtonFlags,SDL_GetGlobalMouseState,(float *a, float *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetGlobalProperties,(void),(),return)
 SDL_DYNAPI_PROC(SDL_Window*,SDL_GetGrabbedWindow,(void),(),return)
 SDL_DYNAPI_PROC(int,SDL_GetHapticEffectStatus,(SDL_Haptic *a, int b),(a,b),return)
@@ -378,7 +378,7 @@ SDL_DYNAPI_PROC(SDL_MouseID*,SDL_GetMice,(int *a),(a),return)
 SDL_DYNAPI_PROC(SDL_Keymod,SDL_GetModState,(void),(),return)
 SDL_DYNAPI_PROC(SDL_Window*,SDL_GetMouseFocus,(void),(),return)
 SDL_DYNAPI_PROC(const char*,SDL_GetMouseInstanceName,(SDL_MouseID a),(a),return)
-SDL_DYNAPI_PROC(Uint32,SDL_GetMouseState,(float *a, float *b),(a,b),return)
+SDL_DYNAPI_PROC(SDL_MouseButtonFlags,SDL_GetMouseState,(float *a, float *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_DisplayOrientation,SDL_GetNaturalDisplayOrientation,(SDL_DisplayID a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_GetNumAllocations,(void),(),return)
 SDL_DYNAPI_PROC(int,SDL_GetNumAudioDrivers,(void),(),return)
@@ -427,7 +427,7 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GetRectIntersectionFloat,(const SDL_FRect *a, const
 SDL_DYNAPI_PROC(int,SDL_GetRectUnion,(const SDL_Rect *a, const SDL_Rect *b, SDL_Rect *c),(a,b,c),return)
 SDL_DYNAPI_PROC(int,SDL_GetRectUnionFloat,(const SDL_FRect *a, const SDL_FRect *b, SDL_FRect *c),(a,b,c),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_GetRelativeMouseMode,(void),(),return)
-SDL_DYNAPI_PROC(Uint32,SDL_GetRelativeMouseState,(float *a, float *b),(a,b),return)
+SDL_DYNAPI_PROC(SDL_MouseButtonFlags,SDL_GetRelativeMouseState,(float *a, float *b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_GetRenderClipRect,(SDL_Renderer *a, SDL_Rect *b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_GetRenderColorScale,(SDL_Renderer *a, float *b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_GetRenderDrawBlendMode,(SDL_Renderer *a, SDL_BlendMode *b),(a,b),return)

+ 5 - 5
src/events/SDL_mouse.c

@@ -404,10 +404,10 @@ SDL_Mouse *SDL_GetMouse(void)
     return &SDL_mouse;
 }
 
-Uint32 SDL_GetMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID, SDL_bool include_touch)
+SDL_MouseButtonFlags SDL_GetMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID, SDL_bool include_touch)
 {
     int i;
-    Uint32 buttonstate = 0;
+    SDL_MouseButtonFlags buttonstate = 0;
 
     for (i = 0; i < mouse->num_sources; ++i) {
         if (mouseID == SDL_GLOBAL_MOUSE_ID || mouseID == SDL_TOUCH_MOUSEID) {
@@ -1127,7 +1127,7 @@ void SDL_QuitMouse(void)
     SDL_mice = NULL;
 }
 
-Uint32 SDL_GetMouseState(float *x, float *y)
+SDL_MouseButtonFlags SDL_GetMouseState(float *x, float *y)
 {
     SDL_Mouse *mouse = SDL_GetMouse();
 
@@ -1140,7 +1140,7 @@ Uint32 SDL_GetMouseState(float *x, float *y)
     return SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, SDL_TRUE);
 }
 
-Uint32 SDL_GetRelativeMouseState(float *x, float *y)
+SDL_MouseButtonFlags SDL_GetRelativeMouseState(float *x, float *y)
 {
     SDL_Mouse *mouse = SDL_GetMouse();
 
@@ -1155,7 +1155,7 @@ Uint32 SDL_GetRelativeMouseState(float *x, float *y)
     return SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, SDL_TRUE);
 }
 
-Uint32 SDL_GetGlobalMouseState(float *x, float *y)
+SDL_MouseButtonFlags SDL_GetGlobalMouseState(float *x, float *y)
 {
     SDL_Mouse *mouse = SDL_GetMouse();
 

+ 1 - 4
src/events/SDL_mouse_c.h

@@ -78,7 +78,7 @@ typedef struct
     int (*CaptureMouse)(SDL_Window *window);
 
     /* Get absolute mouse coordinates. (x) and (y) are never NULL and set to zero before call. */
-    Uint32 (*GetGlobalMouseState)(float *x, float *y);
+    SDL_MouseButtonFlags (*GetGlobalMouseState)(float *x, float *y);
 
     /* Data common to all mice */
     SDL_Window *focus;
@@ -154,9 +154,6 @@ extern void SDL_SetMouseFocus(SDL_Window *window);
 /* Update the mouse capture window */
 extern int SDL_UpdateMouseCapture(SDL_bool force_release);
 
-/* Get the current mouse button state for a mouse */
-Uint32 SDL_GetMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID, SDL_bool include_touch);
-
 /* You can set either a single scale, or a set of {speed, scale} values in sorted order */
 extern int SDL_SetMouseSystemScale(int num_values, const float *values);
 

+ 3 - 3
src/test/SDL_test_common.c

@@ -941,7 +941,7 @@ static void SDLTest_PrintModState(char *text, size_t maxlen, SDL_Keymod keymod)
     }
 }
 
-static void SDLTest_PrintButtonMask(char *text, size_t maxlen, Uint32 flags)
+static void SDLTest_PrintButtonMask(char *text, size_t maxlen, SDL_MouseButtonFlags flags)
 {
     int i;
     int count = 0;
@@ -2374,7 +2374,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
             if (withControl) {
                 /* Ctrl-A reports absolute mouse position. */
                 float x, y;
-                const Uint32 mask = SDL_GetGlobalMouseState(&x, &y);
+                const SDL_MouseButtonFlags mask = SDL_GetGlobalMouseState(&x, &y);
                 SDL_Log("ABSOLUTE MOUSE: (%g, %g)%s%s%s%s%s\n", x, y,
                         (mask & SDL_BUTTON_LMASK) ? " [LBUTTON]" : "",
                         (mask & SDL_BUTTON_MMASK) ? " [MBUTTON]" : "",
@@ -2478,7 +2478,7 @@ void SDLTest_CommonDrawWindowInfo(SDL_Renderer *renderer, SDL_Window *window, fl
     SDL_Rect rect;
     const SDL_DisplayMode *mode;
     float scaleX, scaleY;
-    Uint32 flags;
+    SDL_MouseButtonFlags flags;
     SDL_DisplayID windowDisplayID = SDL_GetDisplayForWindow(window);
     SDL_RendererInfo info;
     SDL_RendererLogicalPresentation logical_presentation;

+ 2 - 2
src/video/cocoa/SDL_cocoamouse.m

@@ -369,11 +369,11 @@ static int Cocoa_CaptureMouse(SDL_Window *window)
     return 0;
 }
 
-static Uint32 Cocoa_GetGlobalMouseState(float *x, float *y)
+static SDL_MouseButtonFlags Cocoa_GetGlobalMouseState(float *x, float *y)
 {
     const NSUInteger cocoaButtons = [NSEvent pressedMouseButtons];
     const NSPoint cocoaLocation = [NSEvent mouseLocation];
-    Uint32 retval = 0;
+    SDL_MouseButtonFlags retval = 0;
 
     *x = cocoaLocation.x;
     *y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - cocoaLocation.y);

+ 2 - 2
src/video/wayland/SDL_waylandmouse.c

@@ -680,10 +680,10 @@ static void SDLCALL Wayland_EmulateMouseWarpChanged(void *userdata, const char *
  * coordinates when the window has focus, which is good enough for most
  * applications.
  */
-static Uint32 SDLCALL Wayland_GetGlobalMouseState(float *x, float *y)
+static SDL_MouseButtonFlags SDLCALL Wayland_GetGlobalMouseState(float *x, float *y)
 {
     SDL_Window *focus = SDL_GetMouseFocus();
-    Uint32 ret = 0;
+    SDL_MouseButtonFlags ret = 0;
 
     if (focus) {
         int off_x, off_y;

+ 1 - 1
src/video/windows/SDL_windowsevents.c

@@ -237,7 +237,7 @@ static void WIN_CheckWParamMouseButton(Uint64 timestamp, SDL_bool bwParamMousePr
 static void WIN_CheckWParamMouseButtons(Uint64 timestamp, WPARAM wParam, SDL_WindowData *data, SDL_MouseID mouseID)
 {
     if (wParam != data->mouse_button_flags) {
-        Uint32 mouseFlags = SDL_GetMouseState(NULL, NULL);
+        SDL_MouseButtonFlags mouseFlags = SDL_GetMouseState(NULL, NULL);
 
         /* WM_LBUTTONDOWN and friends handle button swapping for us. No need to check SM_SWAPBUTTON here.  */
         WIN_CheckWParamMouseButton(timestamp, (wParam & MK_LBUTTON), mouseFlags, SDL_FALSE, data, SDL_BUTTON_LEFT, mouseID);

+ 2 - 2
src/video/windows/SDL_windowsmouse.c

@@ -390,9 +390,9 @@ static int WIN_CaptureMouse(SDL_Window *window)
     return 0;
 }
 
-static Uint32 WIN_GetGlobalMouseState(float *x, float *y)
+static SDL_MouseButtonFlags WIN_GetGlobalMouseState(float *x, float *y)
 {
-    Uint32 retval = 0;
+    SDL_MouseButtonFlags retval = 0;
     POINT pt = { 0, 0 };
     SDL_bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
 

+ 2 - 2
src/video/x11/SDL_x11mouse.c

@@ -413,7 +413,7 @@ static int X11_CaptureMouse(SDL_Window *window)
     return 0;
 }
 
-static Uint32 X11_GetGlobalMouseState(float *x, float *y)
+static SDL_MouseButtonFlags X11_GetGlobalMouseState(float *x, float *y)
 {
     SDL_VideoData *videodata = SDL_GetVideoDevice()->driverdata;
     SDL_DisplayID *displays;
@@ -439,7 +439,7 @@ static Uint32 X11_GetGlobalMouseState(float *x, float *y)
                     unsigned int mask;
                     if (X11_XQueryPointer(display, RootWindow(display, data->screen), &root, &child, &rootx, &rooty, &winx, &winy, &mask)) {
                         XWindowAttributes root_attrs;
-                        Uint32 buttons = 0;
+                        SDL_MouseButtonFlags buttons = 0;
                         buttons |= (mask & Button1Mask) ? SDL_BUTTON_LMASK : 0;
                         buttons |= (mask & Button2Mask) ? SDL_BUTTON_MMASK : 0;
                         buttons |= (mask & Button3Mask) ? SDL_BUTTON_RMASK : 0;

+ 3 - 3
test/testautomation_mouse.c

@@ -32,7 +32,7 @@ static int mouse_getMouseState(void *arg)
 {
     float x;
     float y;
-    Uint32 state;
+    SDL_MouseButtonFlags state;
 
     /* Pump some events to update mouse state */
     SDL_PumpEvents();
@@ -77,7 +77,7 @@ static int mouse_getRelativeMouseState(void *arg)
 {
     float x;
     float y;
-    Uint32 state;
+    SDL_MouseButtonFlags state;
 
     /* Pump some events to update mouse state */
     SDL_PumpEvents();
@@ -580,7 +580,7 @@ static int mouse_getGlobalMouseState(void *arg)
 {
     float x;
     float y;
-    Uint32 state;
+    SDL_MouseButtonFlags state;
 
     x = -FLT_MAX;
     y = -FLT_MAX;

+ 1 - 1
test/testcontroller.c

@@ -586,7 +586,7 @@ static void ClearBinding(void)
 static void SetDisplayMode(ControllerDisplayMode mode)
 {
     float x, y;
-    Uint32 button_state;
+    SDL_MouseButtonFlags button_state;
 
     if (mode == CONTROLLER_MODE_BINDING) {
         /* Make a backup of the current mapping */