SDL: Re-add SDL_HAPTIC_SQUARE for SDL 3.0

From cda2bffeeea18a44c1ab559bb1612dbc4691b075 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 18 Jan 2024 06:55:36 -0800
Subject: [PATCH] Re-add SDL_HAPTIC_SQUARE for SDL 3.0

Fixes https://github.com/libsdl-org/SDL/issues/8753
Closes https://github.com/libsdl-org/SDL/pull/8831
Closes https://github.com/libsdl-org/SDL/pull/8783
---
 include/SDL3/SDL_haptic.h             | 65 ++++++++++++++++-----------
 src/dynapi/SDL_dynapi_procs.h         |  2 +-
 src/haptic/SDL_haptic.c               |  2 +-
 src/haptic/SDL_syshaptic.h            |  2 +-
 src/haptic/darwin/SDL_syshaptic.c     | 11 ++---
 src/haptic/linux/SDL_syshaptic.c      | 11 ++---
 src/haptic/windows/SDL_dinputhaptic.c | 11 ++---
 test/testhaptic.c                     |  5 +--
 8 files changed, 55 insertions(+), 54 deletions(-)

diff --git a/include/SDL3/SDL_haptic.h b/include/SDL3/SDL_haptic.h
index 1cd044acef3f..40582e771c7d 100644
--- a/include/SDL3/SDL_haptic.h
+++ b/include/SDL3/SDL_haptic.h
@@ -166,7 +166,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticCondition
  */
-#define SDL_HAPTIC_CONSTANT   (1u<<0)
+#define SDL_HAPTIC_CONSTANT     (1u<<0)
 
 /**
  *  Sine wave effect supported.
@@ -175,21 +175,16 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticPeriodic
  */
-#define SDL_HAPTIC_SINE       (1u<<1)
+#define SDL_HAPTIC_SINE         (1u<<1)
 
 /**
- *  Left/Right effect supported.
+ *  Square wave effect supported.
  *
- *  Haptic effect for direct control over high/low frequency motors.
+ *  Periodic haptic effect that simulates square waves.
  *
- *  \sa SDL_HapticLeftRight
- * \warning this value was SDL_HAPTIC_SQUARE right before 2.0.0 shipped. Sorry,
- *          we ran out of bits, and this is important for XInput devices.
+ *  \sa SDL_HapticPeriodic
  */
-#define SDL_HAPTIC_LEFTRIGHT     (1u<<2)
-
-/* !!! FIXME: put this back when we have more bits in 2.1 */
-/* #define SDL_HAPTIC_SQUARE     (1<<2) */
+#define SDL_HAPTIC_SQUARE       (1<<2)
 
 /**
  *  Triangle wave effect supported.
@@ -198,7 +193,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticPeriodic
  */
-#define SDL_HAPTIC_TRIANGLE   (1u<<3)
+#define SDL_HAPTIC_TRIANGLE     (1u<<3)
 
 /**
  *  Sawtoothup wave effect supported.
@@ -207,7 +202,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticPeriodic
  */
-#define SDL_HAPTIC_SAWTOOTHUP (1u<<4)
+#define SDL_HAPTIC_SAWTOOTHUP   (1u<<4)
 
 /**
  *  Sawtoothdown wave effect supported.
@@ -225,7 +220,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticRamp
  */
-#define SDL_HAPTIC_RAMP       (1u<<6)
+#define SDL_HAPTIC_RAMP         (1u<<6)
 
 /**
  *  Spring effect supported - uses axes position.
@@ -235,7 +230,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticCondition
  */
-#define SDL_HAPTIC_SPRING     (1u<<7)
+#define SDL_HAPTIC_SPRING       (1u<<7)
 
 /**
  *  Damper effect supported - uses axes velocity.
@@ -245,7 +240,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticCondition
  */
-#define SDL_HAPTIC_DAMPER     (1u<<8)
+#define SDL_HAPTIC_DAMPER       (1u<<8)
 
 /**
  *  Inertia effect supported - uses axes acceleration.
@@ -255,7 +250,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticCondition
  */
-#define SDL_HAPTIC_INERTIA    (1u<<9)
+#define SDL_HAPTIC_INERTIA      (1u<<9)
 
 /**
  *  Friction effect supported - uses axes movement.
@@ -265,14 +260,30 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_HapticCondition
  */
-#define SDL_HAPTIC_FRICTION   (1u<<10)
+#define SDL_HAPTIC_FRICTION     (1u<<10)
+
+/**
+ *  Left/Right effect supported.
+ *
+ *  Haptic effect for direct control over high/low frequency motors.
+ *
+ *  \sa SDL_HapticLeftRight
+ */
+#define SDL_HAPTIC_LEFTRIGHT    (1u<<11)
+
+/**
+ *  Reserved for future use
+ */
+#define SDL_HAPTIC_RESERVED1    (1u<<12)
+#define SDL_HAPTIC_RESERVED2    (1u<<13)
+#define SDL_HAPTIC_RESERVED3    (1u<<14)
 
 /**
  *  Custom effect is supported.
  *
  *  User defined custom haptic effect.
  */
-#define SDL_HAPTIC_CUSTOM     (1u<<11)
+#define SDL_HAPTIC_CUSTOM       (1u<<15)
 
 /* @} *//* Haptic effects */
 
@@ -285,7 +296,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_SetHapticGain
  */
-#define SDL_HAPTIC_GAIN       (1u<<12)
+#define SDL_HAPTIC_GAIN       (1u<<16)
 
 /**
  *  Device can set autocenter.
@@ -294,7 +305,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_SetHapticAutocenter
  */
-#define SDL_HAPTIC_AUTOCENTER (1u<<13)
+#define SDL_HAPTIC_AUTOCENTER (1u<<17)
 
 /**
  *  Device can be queried for effect status.
@@ -303,7 +314,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *
  *  \sa SDL_GetHapticEffectStatus
  */
-#define SDL_HAPTIC_STATUS     (1u<<14)
+#define SDL_HAPTIC_STATUS     (1u<<18)
 
 /**
  *  Device can be paused.
@@ -313,7 +324,7 @@ typedef struct SDL_Haptic SDL_Haptic;
  *  \sa SDL_PauseHaptic
  *  \sa SDL_ResumeHaptic
  */
-#define SDL_HAPTIC_PAUSE      (1u<<15)
+#define SDL_HAPTIC_PAUSE      (1u<<19)
 
 
 /**
@@ -511,7 +522,7 @@ typedef struct SDL_HapticConstant
  *
  *  The struct handles the following effects:
  *   - ::SDL_HAPTIC_SINE
- *   - ::SDL_HAPTIC_LEFTRIGHT
+ *   - ::SDL_HAPTIC_SQUARE
  *   - ::SDL_HAPTIC_TRIANGLE
  *   - ::SDL_HAPTIC_SAWTOOTHUP
  *   - ::SDL_HAPTIC_SAWTOOTHDOWN
@@ -557,7 +568,7 @@ typedef struct SDL_HapticConstant
     \endverbatim
  *
  *  \sa SDL_HAPTIC_SINE
- *  \sa SDL_HAPTIC_LEFTRIGHT
+ *  \sa SDL_HAPTIC_SQUARE
  *  \sa SDL_HAPTIC_TRIANGLE
  *  \sa SDL_HAPTIC_SAWTOOTHUP
  *  \sa SDL_HAPTIC_SAWTOOTHDOWN
@@ -566,7 +577,7 @@ typedef struct SDL_HapticConstant
 typedef struct SDL_HapticPeriodic
 {
     /* Header */
-    Uint16 type;        /**< ::SDL_HAPTIC_SINE, ::SDL_HAPTIC_LEFTRIGHT,
+    Uint16 type;        /**< ::SDL_HAPTIC_SINE, ::SDL_HAPTIC_SQUARE
                              ::SDL_HAPTIC_TRIANGLE, ::SDL_HAPTIC_SAWTOOTHUP or
                              ::SDL_HAPTIC_SAWTOOTHDOWN */
     SDL_HapticDirection direction;  /**< Direction of the effect. */
@@ -1050,7 +1061,7 @@ extern DECLSPEC int SDLCALL SDL_GetMaxHapticEffectsPlaying(SDL_Haptic *haptic);
  * \sa SDL_HapticEffectSupported
  * \sa SDL_GetMaxHapticEffects
  */
-extern DECLSPEC unsigned int SDLCALL SDL_GetHapticFeatures(SDL_Haptic *haptic);
+extern DECLSPEC Uint32 SDLCALL SDL_GetHapticFeatures(SDL_Haptic *haptic);
 
 
 /**
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 9030305e8827..dc9291c45250 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -447,7 +447,7 @@ SDL_DYNAPI_PROC(SDL_Haptic*,SDL_OpenHaptic,(SDL_HapticID a),(a),return)
 SDL_DYNAPI_PROC(SDL_Haptic*,SDL_OpenHapticFromJoystick,(SDL_Joystick *a),(a),return)
 SDL_DYNAPI_PROC(SDL_Haptic*,SDL_OpenHapticFromMouse,(void),(),return)
 SDL_DYNAPI_PROC(int,SDL_PauseHaptic,(SDL_Haptic *a),(a),return)
-SDL_DYNAPI_PROC(unsigned int,SDL_GetHapticFeatures,(SDL_Haptic *a),(a),return)
+SDL_DYNAPI_PROC(Uint32,SDL_GetHapticFeatures,(SDL_Haptic *a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_InitHapticRumble,(SDL_Haptic *a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_PlayHapticRumble,(SDL_Haptic *a, float b, Uint32 c),(a,b,c),return)
 SDL_DYNAPI_PROC(int,SDL_StopHapticRumble,(SDL_Haptic *a),(a),return)
diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c
index e12db0980da8..1fc666c379f3 100644
--- a/src/haptic/SDL_haptic.c
+++ b/src/haptic/SDL_haptic.c
@@ -366,7 +366,7 @@ int SDL_GetMaxHapticEffectsPlaying(SDL_Haptic *haptic)
     return haptic->nplaying;
 }
 
-unsigned int SDL_GetHapticFeatures(SDL_Haptic *haptic)
+Uint32 SDL_GetHapticFeatures(SDL_Haptic *haptic)
 {
     CHECK_HAPTIC_MAGIC(haptic, 0);
 
diff --git a/src/haptic/SDL_syshaptic.h b/src/haptic/SDL_syshaptic.h
index bb12613b9bfe..31c2cf6b0312 100644
--- a/src/haptic/SDL_syshaptic.h
+++ b/src/haptic/SDL_syshaptic.h
@@ -48,7 +48,7 @@ struct SDL_Haptic
     struct haptic_effect *effects;  /* Allocated effects */
     int neffects;                   /* Maximum amount of effects */
     int nplaying;                   /* Maximum amount of effects to play at the same time */
-    unsigned int supported;         /* Supported effects */
+    Uint32 supported;               /* Supported effects and features */
     int naxes;                      /* Number of axes on the device. */
 
     struct haptic_hwdata *hwdata;   /* Driver dependent */
diff --git a/src/haptic/darwin/SDL_syshaptic.c b/src/haptic/darwin/SDL_syshaptic.c
index 11ed1a73bba9..54ae0502b352 100644
--- a/src/haptic/darwin/SDL_syshaptic.c
+++ b/src/haptic/darwin/SDL_syshaptic.c
@@ -445,8 +445,7 @@ static unsigned int GetSupportedFeatures(SDL_Haptic *haptic)
     /* Test for effects. */
     FF_TEST(FFCAP_ET_CONSTANTFORCE, SDL_HAPTIC_CONSTANT);
     FF_TEST(FFCAP_ET_RAMPFORCE, SDL_HAPTIC_RAMP);
-    /* !!! FIXME: put this back when we have more bits in 2.1 */
-    /* FF_TEST(FFCAP_ET_SQUARE, SDL_HAPTIC_SQUARE); */
+    FF_TEST(FFCAP_ET_SQUARE, SDL_HAPTIC_SQUARE);
     FF_TEST(FFCAP_ET_SINE, SDL_HAPTIC_SINE);
     FF_TEST(FFCAP_ET_TRIANGLE, SDL_HAPTIC_TRIANGLE);
     FF_TEST(FFCAP_ET_SAWTOOTHUP, SDL_HAPTIC_SAWTOOTHUP);
@@ -862,8 +861,7 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
         break;
 
     case SDL_HAPTIC_SINE:
-    /* !!! FIXME: put this back when we have more bits in 2.1 */
-    /* case SDL_HAPTIC_SQUARE: */
+    case SDL_HAPTIC_SQUARE:
     case SDL_HAPTIC_TRIANGLE:
     case SDL_HAPTIC_SAWTOOTHUP:
     case SDL_HAPTIC_SAWTOOTHDOWN:
@@ -1077,9 +1075,8 @@ SDL_SYS_HapticEffectType(Uint16 type)
     case SDL_HAPTIC_RAMP:
         return kFFEffectType_RampForce_ID;
 
-        /* !!! FIXME: put this back when we have more bits in 2.1 */
-        /* case SDL_HAPTIC_SQUARE:
-            return kFFEffectType_Square_ID; */
+    case SDL_HAPTIC_SQUARE:
+        return kFFEffectType_Square_ID;
 
     case SDL_HAPTIC_SINE:
         return kFFEffectType_Sine_ID;
diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c
index 01b82883ed31..53a1c1f3f892 100644
--- a/src/haptic/linux/SDL_syshaptic.c
+++ b/src/haptic/linux/SDL_syshaptic.c
@@ -100,8 +100,7 @@ static int EV_IsHaptic(int fd)
     /* Convert supported features to SDL_HAPTIC platform-neutral features. */
     EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
     EV_TEST(FF_SINE, SDL_HAPTIC_SINE);
-    /* !!! FIXME: put this back when we have more bits in 2.1 */
-    /* EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE); */
+    EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE);
     EV_TEST(FF_TRIANGLE, SDL_HAPTIC_TRIANGLE);
     EV_TEST(FF_SAW_UP, SDL_HAPTIC_SAWTOOTHUP);
     EV_TEST(FF_SAW_DOWN, SDL_HAPTIC_SAWTOOTHDOWN);
@@ -761,8 +760,7 @@ static int SDL_SYS_ToFFEffect(struct ff_effect *dest, SDL_HapticEffect *src)
         break;
 
     case SDL_HAPTIC_SINE:
-    /* !!! FIXME: put this back when we have more bits in 2.1 */
-    /* case SDL_HAPTIC_SQUARE: */
+    case SDL_HAPTIC_SQUARE:
     case SDL_HAPTIC_TRIANGLE:
     case SDL_HAPTIC_SAWTOOTHUP:
     case SDL_HAPTIC_SAWTOOTHDOWN:
@@ -785,9 +783,8 @@ static int SDL_SYS_ToFFEffect(struct ff_effect *dest, SDL_HapticEffect *src)
         /* Periodic */
         if (periodic->type == SDL_HAPTIC_SINE) {
             dest->u.periodic.waveform = FF_SINE;
-            /* !!! FIXME: put this back when we have more bits in 2.1 */
-            /* else if (periodic->type == SDL_HAPTIC_SQUARE)
-                dest->u.periodic.waveform = FF_SQUARE; */
+        } else if (periodic->type == SDL_HAPTIC_SQUARE) {
+            dest->u.periodic.waveform = FF_SQUARE;
         } else if (periodic->type == SDL_HAPTIC_TRIANGLE) {
             dest->u.periodic.waveform = FF_TRIANGLE;
         } else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP) {
diff --git a/src/haptic/windows/SDL_dinputhaptic.c b/src/haptic/windows/SDL_dinputhaptic.c
index 7c7f9ff64e51..0b7513497f7d 100644
--- a/src/haptic/windows/SDL_dinputhaptic.c
+++ b/src/haptic/windows/SDL_dinputhaptic.c
@@ -260,8 +260,7 @@ static BOOL CALLBACK DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv)
     EFFECT_TEST(GUID_ConstantForce, SDL_HAPTIC_CONSTANT);
     EFFECT_TEST(GUID_CustomForce, SDL_HAPTIC_CUSTOM);
     EFFECT_TEST(GUID_Sine, SDL_HAPTIC_SINE);
-    /* !!! FIXME: put this back when we have more bits in 2.1 */
-    /* EFFECT_TEST(GUID_Square, SDL_HAPTIC_SQUARE); */
+    EFFECT_TEST(GUID_Square, SDL_HAPTIC_SQUARE);
     EFFECT_TEST(GUID_Triangle, SDL_HAPTIC_TRIANGLE);
     EFFECT_TEST(GUID_SawtoothUp, SDL_HAPTIC_SAWTOOTHUP);
     EFFECT_TEST(GUID_SawtoothDown, SDL_HAPTIC_SAWTOOTHDOWN);
@@ -678,8 +677,7 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
         break;
 
     case SDL_HAPTIC_SINE:
-    /* !!! FIXME: put this back when we have more bits in 2.1 */
-    /* case SDL_HAPTIC_SQUARE: */
+    case SDL_HAPTIC_SQUARE:
     case SDL_HAPTIC_TRIANGLE:
     case SDL_HAPTIC_SAWTOOTHUP:
     case SDL_HAPTIC_SAWTOOTHDOWN:
@@ -889,9 +887,8 @@ static REFGUID SDL_SYS_HapticEffectType(SDL_HapticEffect *effect)
     case SDL_HAPTIC_RAMP:
         return &GUID_RampForce;
 
-        /* !!! FIXME: put this back when we have more bits in 2.1 */
-        /* case SDL_HAPTIC_SQUARE:
-            return &GUID_Square; */
+    case SDL_HAPTIC_SQUARE:
+        return &GUID_Square;
 
     case SDL_HAPTIC_SINE:
         return &GUID_Sine;
diff --git a/test/testhaptic.c b/test/testhaptic.c
index c3d703413f0d..e4ab85daf51b 100644
--- a/test/testhaptic.c
+++ b/test/testhaptic.c
@@ -351,9 +351,8 @@ HapticPrintSupported(SDL_Haptic *ptr)
     if (supported & SDL_HAPTIC_SINE) {
         SDL_Log("      sine\n");
     }
-    /* !!! FIXME: put this back when we have more bits in 2.1 */
-    /* if (supported & SDL_HAPTIC_SQUARE)
-        SDL_Log("      square\n"); */
+    if (supported & SDL_HAPTIC_SQUARE)
+        SDL_Log("      square\n");
     if (supported & SDL_HAPTIC_TRIANGLE) {
         SDL_Log("      triangle\n");
     }