From ef86cbbda34a36ad01eed17f40c0f0cd60802e06 Mon Sep 17 00:00:00 2001 From: Gareth Francis Date: Mon, 8 Apr 2024 20:22:22 +0100 Subject: [PATCH] Remove limit of 4 joysticks in InputHandler_Linux_Joystick --- .../InputHandler_Linux_Joystick.cpp | 63 +++++++++---------- .../InputHandler_Linux_Joystick.h | 14 +++-- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/arch/InputHandler/InputHandler_Linux_Joystick.cpp b/src/arch/InputHandler/InputHandler_Linux_Joystick.cpp index 76663d5f60..e2bfae8377 100644 --- a/src/arch/InputHandler/InputHandler_Linux_Joystick.cpp +++ b/src/arch/InputHandler/InputHandler_Linux_Joystick.cpp @@ -28,15 +28,11 @@ InputHandler_Linux_Joystick::InputHandler_Linux_Joystick() m_bDevicesChanged = false; LOG->Trace( "InputHandler_Linux_Joystick::InputHandler_Linux_Joystick" ); - for(int i = 0; i < NUM_JOYSTICKS; ++i) - fds[i] = -1; - - m_iLastFd = 0; if( LINUXINPUT == nullptr ) LINUXINPUT = new LinuxInputManager; LINUXINPUT->InitDriver(this); - if( fds[0] != -1 ) // LinuxInputManager found at least one valid joystick for us + if( !m_files.empty() ) // LinuxInputManager found at least one valid joystick for us StartThread(); } @@ -45,8 +41,10 @@ InputHandler_Linux_Joystick::~InputHandler_Linux_Joystick() if( m_InputThread.IsCreated() ) StopThread(); - for(int i = 0; i < NUM_JOYSTICKS; ++i) - if(fds[i] != -1) close(fds[i]); + for( auto& f : m_files ) { + if( f.file != -1 ) close(f.file); + } + m_files.clear(); } void InputHandler_Linux_Joystick::StartThread() @@ -78,21 +76,22 @@ bool InputHandler_Linux_Joystick::TryDevice(RString dev) if( m_InputThread.IsCreated() ) { StopThread(); hotplug = true; } /* Thread is stopped! DO NOT RETURN */ { - fds[m_iLastFd] = open( dev, O_RDONLY ); - - if(fds[m_iLastFd] != -1) + FileDescriptor f; + f.file = open( dev, O_RDONLY ); + if(f.file != -1) { char szName[1024]; ZERO( szName ); - if( ioctl(fds[m_iLastFd], JSIOCGNAME(sizeof(szName)), szName) < 0 ) - m_sDescription[m_iLastFd] = ssprintf( "Unknown joystick at %s", dev.c_str() ); + if( ioctl(f.file, JSIOCGNAME(sizeof(szName)), szName) < 0 ) + f.description = ssprintf( "Unknown joystick at %s", dev.c_str() ); else - m_sDescription[m_iLastFd] = szName; + f.description = szName; LOG->Info("LinuxJoystick: Opened %s", dev.c_str() ); - m_iLastFd++; m_bDevicesChanged = true; ret = true; + + m_files.push_back(f); } else LOG->Warn("LinuxJoystick: Failed to open %s: %s", dev.c_str(), strerror(errno) ); } @@ -115,13 +114,13 @@ void InputHandler_Linux_Joystick::InputThread() FD_ZERO(&fdset); int max_fd = -1; - for(int i = 0; i < NUM_JOYSTICKS; ++i) + for(int i = 0; i < m_files.size(); ++i) { - if (fds[i] < 0) + if (m_files[i].file < 0) continue; - FD_SET(fds[i], &fdset); - max_fd = std::max(max_fd, fds[i]); + FD_SET(m_files[i].file, &fdset); + max_fd = std::max(max_fd, m_files[i].file); } if(max_fd == -1) @@ -132,30 +131,30 @@ void InputHandler_Linux_Joystick::InputThread() continue; RageTimer now; - for(int i = 0; i < NUM_JOYSTICKS; ++i) + for(int i = 0; i < m_files.size(); ++i) { - if( fds[i] == -1 ) + if( m_files[i].file == -1 ) continue; - if(!FD_ISSET(fds[i], &fdset)) + if(!FD_ISSET(m_files[i].file, &fdset)) continue; js_event event; - int ret = read(fds[i], &event, sizeof(event)); + int ret = read(m_files[i].file, &event, sizeof(event)); if(ret == -1) { LOG->Warn("Error reading from joystick %i: %s; disabled", i, strerror(errno)); - close(fds[i]); - fds[i] = -1; + close(m_files[i].file); + m_files[i].file = -1; continue; } if(ret != sizeof(event)) { LOG->Warn("Unexpected packet (size %i != %i) from joystick %i; disabled", ret, (int)sizeof(event), i); - close(fds[i]); - fds[i] = -1; + close(m_files[i].file); + m_files[i].file = -1; continue; } @@ -183,8 +182,8 @@ void InputHandler_Linux_Joystick::InputThread() default: LOG->Warn("Unexpected packet (type %i) from joystick %i; disabled", event.type, i); - close(fds[i]); - fds[i] = -1; + close(m_files[i].file); + m_files[i].file = -1; continue; } @@ -200,14 +199,14 @@ void InputHandler_Linux_Joystick::GetDevicesAndDescriptions( std::vector - class InputHandler_Linux_Joystick: public InputHandler { public: - enum { NUM_JOYSTICKS = 4 }; InputHandler_Linux_Joystick(); ~InputHandler_Linux_Joystick(); bool TryDevice(RString dev); @@ -23,11 +21,15 @@ private: static int InputThread_Start( void *p ); void InputThread(); - int fds[NUM_JOYSTICKS]; - int m_iLastFd; - RString m_sDescription[NUM_JOYSTICKS]; + struct FileDescriptor { + int file = -1; + RString description = ""; + }; + + std::vector m_files; RageThread m_InputThread; - bool m_bShutdown, m_bDevicesChanged; + bool m_bShutdown = false; + bool m_bDevicesChanged = false; }; #endif