SDL: Allow opening joysticks that are read-only on Linux

From b8c00bf9148f339dfeb3e14e53948bc5488982dc Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 7 Nov 2021 13:11:29 -0800
Subject: [PATCH] Allow opening joysticks that are read-only on Linux

Fixes https://github.com/libsdl-org/SDL/issues/4713
---
 src/joystick/linux/SDL_sysjoystick.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 984005ff78..9a0a60e1fd 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -963,7 +963,12 @@ PrepareJoystickHwdata(SDL_Joystick *joystick, SDL_joylist_item *item)
                                      &joystick->naxes,
                                      &joystick->nhats);
     } else {
-        const int fd = open(item->path, O_RDWR | O_CLOEXEC, 0);
+        /* Try read-write first, so we can do rumble */
+        int fd = open(item->path, O_RDWR | O_CLOEXEC, 0);
+        if (fd < 0) {
+            /* Try read-only again, at least we'll get events in this case */
+            fd = open(item->path, O_RDONLY | O_CLOEXEC, 0);
+        }
         if (fd < 0) {
             return SDL_SetError("Unable to open %s", item->path);
         }