SDL: simplify OpenBSD D-pad calculation with bitwise operation (#7996) (177df)

From 177dfddaf75f58b363354453f0fabbd66b12db65 Mon Sep 17 00:00:00 2001
From: rfht <[EMAIL REDACTED]>
Date: Mon, 17 Jul 2023 20:01:23 -0600
Subject: [PATCH] simplify OpenBSD D-pad calculation with bitwise operation
 (#7996)

(cherry picked from commit 4d63c919f9b0c2bb68e714b72ec7eb3445a113d1)
---
 src/joystick/bsd/SDL_bsdjoystick.c | 57 ++++++++++--------------------
 1 file changed, 19 insertions(+), 38 deletions(-)

diff --git a/src/joystick/bsd/SDL_bsdjoystick.c b/src/joystick/bsd/SDL_bsdjoystick.c
index 3886fbfbb6170..07302e3588d86 100644
--- a/src/joystick/bsd/SDL_bsdjoystick.c
+++ b/src/joystick/bsd/SDL_bsdjoystick.c
@@ -93,40 +93,11 @@
 #define HUG_DPAD_RIGHT 0x92
 #define HUG_DPAD_LEFT  0x93
 
-#define HAT_CENTERED  0x00
 #define HAT_UP        0x01
 #define HAT_RIGHT     0x02
 #define HAT_DOWN      0x04
 #define HAT_LEFT      0x08
-#define HAT_RIGHTUP   (HAT_RIGHT | HAT_UP)
-#define HAT_RIGHTDOWN (HAT_RIGHT | HAT_DOWN)
-#define HAT_LEFTUP    (HAT_LEFT | HAT_UP)
-#define HAT_LEFTDOWN  (HAT_LEFT | HAT_DOWN)
-
-/* calculate the value from the state of the dpad */
-int dpad_to_sdl(Sint32 *dpad)
-{
-    if (dpad[2]) {
-        if (dpad[0])
-            return HAT_RIGHTUP;
-        else if (dpad[1])
-            return HAT_RIGHTDOWN;
-        else
-            return HAT_RIGHT;
-    } else if (dpad[3]) {
-        if (dpad[0])
-            return HAT_LEFTUP;
-        else if (dpad[1])
-            return HAT_LEFTDOWN;
-        else
-            return HAT_LEFT;
-    } else if (dpad[0]) {
-        return HAT_UP;
-    } else if (dpad[1]) {
-        return HAT_DOWN;
-    }
-    return HAT_CENTERED;
-}
+
 #endif
 
 struct report
@@ -724,19 +695,29 @@ static void BSD_JoystickUpdate(SDL_Joystick *joy)
                                                    hitem.logical_minimum);
                     }
 #ifdef __OpenBSD__
-                    else if (usage == HUG_DPAD_UP) {
+                    /* here D-pad directions are reported like separate buttons.
+                     * calculate the SDL hat value from the 4 separate values.
+                     */
+                    switch (usage) {
+	            case HUG_DPAD_UP:
                         dpad[0] = (Sint32)hid_get_data(REP_BUF_DATA(rep), &hitem);
-                        SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
-                    } else if (usage == HUG_DPAD_DOWN) {
+                        break;
+                    case HUG_DPAD_DOWN:
                         dpad[1] = (Sint32)hid_get_data(REP_BUF_DATA(rep), &hitem);
-                        SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
-                    } else if (usage == HUG_DPAD_RIGHT) {
+                        break;
+                    case HUG_DPAD_RIGHT:
                         dpad[2] = (Sint32)hid_get_data(REP_BUF_DATA(rep), &hitem);
-                        SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
-                    } else if (usage == HUG_DPAD_LEFT) {
+                        break;
+                    case HUG_DPAD_LEFT:
                         dpad[3] = (Sint32)hid_get_data(REP_BUF_DATA(rep), &hitem);
-                        SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
+                        break;
+                    //default:
+	                // no-op
                     }
+                    SDL_PrivateJoystickHat(joy, 0, (dpad[0] * HAT_UP) |
+                                                   (dpad[1] * HAT_DOWN) |
+                                                   (dpad[2] * HAT_RIGHT) |
+                                                   (dpad[3] * HAT_LEFT) );
 #endif
                     break;
                 }