@@ -38,23 +38,20 @@ class Controller
3838
3939 SDL_GameControllerType GetControllerType () const
4040 {
41- return SDL_GameControllerGetType (controller );
41+ return SDL_GameControllerTypeForIndex (index );
4242 }
4343
4444 hid::EInputDevice GetInputDevice () const
4545 {
4646 switch (GetControllerType ())
4747 {
48- case SDL_CONTROLLER_TYPE_PS3:
49- case SDL_CONTROLLER_TYPE_PS4:
50- case SDL_CONTROLLER_TYPE_PS5:
51- return hid::EInputDevice::PlayStation;
52- case SDL_CONTROLLER_TYPE_XBOX360:
53- case SDL_CONTROLLER_TYPE_XBOXONE:
54- return hid::EInputDevice::Xbox;
55- default :
56- return hid::EInputDevice::Unknown;
48+ case SDL_CONTROLLER_TYPE_PS3:
49+ case SDL_CONTROLLER_TYPE_PS4:
50+ case SDL_CONTROLLER_TYPE_PS5:
51+ return hid::EInputDevice::PlayStation;
5752 }
53+
54+ return hid::EInputDevice::Xbox;
5855 }
5956
6057 void Close ()
@@ -74,12 +71,6 @@ class Controller
7471 return controller;
7572 }
7673
77- void ClearState ()
78- {
79- memset (&state, 0 , sizeof (state));
80- }
81-
82-
8374 void PollAxis ()
8475 {
8576 if (!CanPoll ())
@@ -143,7 +134,6 @@ class Controller
143134 }
144135};
145136
146-
147137std::array<Controller, 4 > g_controllers;
148138Controller* g_activeController;
149139
@@ -179,11 +169,6 @@ inline Controller* FindController(int which)
179169
180170static void SetControllerInputDevice (Controller* controller)
181171{
182- if (g_activeController && g_activeController != controller)
183- {
184- g_activeController->ClearState ();
185- }
186-
187172 g_activeController = controller;
188173
189174 if (App::s_isLoading)
@@ -204,18 +189,27 @@ static void SetControllerInputDevice(Controller* controller)
204189
205190static void SetControllerTimeOfDayLED (Controller& controller, bool isNight)
206191{
192+ // Determine the lightbar color based on night of day.
207193 auto r = isNight ? 22 : 0 ;
208194 auto g = isNight ? 0 : 37 ;
209195 auto b = isNight ? 101 : 184 ;
210196
211- // Ensure the lightbar is set correctly
212- if (SDL_GameControllerHasLED (controller.controller ))
197+ // Set the LED for the given controller
198+ controller.SetLED (r, g, b);
199+
200+ // Ensure all other controllers mirror Player 1's lightbar
201+ if (controller.controller == g_controllers[0 ].controller ) // Check if it's Player 1
213202 {
214- SDL_GameControllerSetLED (controller.controller , r, g, b);
203+ for (auto & ctrl : g_controllers)
204+ {
205+ if (ctrl.controller != g_controllers[0 ].controller ) // Skip Player 1 itself
206+ {
207+ ctrl.SetLED (r, g, b); // Mirror Player 1's lightbar
208+ }
209+ }
215210 }
216211}
217212
218-
219213int HID_OnSDLEvent (void *, SDL_Event* event)
220214{
221215 switch (event->type )
@@ -227,18 +221,14 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
227221 if (freeIndex != -1 )
228222 {
229223 auto controller = Controller (event->cdevice .which );
230-
231224 g_controllers[freeIndex] = controller;
232225
233- SetControllerTimeOfDayLED (controller, App::s_isWerehog);
226+ // Use App::s_isWerehog to determine if it is night or day
227+ SetControllerTimeOfDayLED (g_controllers[0 ], App::s_isWerehog);
234228
235- // Ensure Player 1's controller is always the active controller
236- if (freeIndex == 0 )
237- {
238- SetControllerInputDevice (&g_controllers[0 ]);
239- }
229+ // Enforce Player 1's lightbar on the newly added controller instantly
230+ SetControllerTimeOfDayLED (controller, App::s_isWerehog);
240231 }
241-
242232 break ;
243233 }
244234
@@ -247,21 +237,26 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
247237 auto * controller = FindController (event->cdevice .which );
248238
249239 if (controller)
240+ {
250241 controller->Close ();
251242
252- // If Player 1's controller is removed, set the next available controller as active
253- if (controller == &g_controllers[0 ])
254- {
255- for (auto & ctrl : g_controllers)
243+ // If Player 1's controller is removed, set the next available controller as active
244+ if (controller == &g_controllers[0 ])
256245 {
257- if ( ctrl. CanPoll () )
246+ for ( auto & ctrl : g_controllers )
258247 {
259- SetControllerInputDevice (&ctrl);
260- break ;
248+ if (ctrl.CanPoll ())
249+ {
250+ SetControllerInputDevice (&ctrl);
251+ g_controllers[0 ] = ctrl;
252+
253+ // Reapply the lightbar color to ensure custom in-game settings
254+ SetControllerTimeOfDayLED (ctrl, App::s_isWerehog);
255+ break ;
256+ }
261257 }
262258 }
263259 }
264-
265260 break ;
266261 }
267262
@@ -293,6 +288,8 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
293288 controller->Poll ();
294289 }
295290
291+ // Reapply the lightbar color to override system changes during input events
292+ SetControllerTimeOfDayLED (*controller, App::s_isWerehog);
296293 break ;
297294 }
298295
@@ -309,7 +306,6 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
309306 SDL_ShowCursor (SDL_ENABLE);
310307
311308 hid::g_inputDevice = hid::EInputDevice::Mouse;
312-
313309 break ;
314310 }
315311
@@ -321,12 +317,12 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
321317 for (auto & controller : g_controllers)
322318 controller.SetVibration ({ 0 , 0 });
323319 }
324-
325320 break ;
326321 }
327322
328323 case SDL_USER_EVILSONIC:
329324 {
325+ // Refresh all controllers to ensure consistent lightbar colors
330326 for (auto & controller : g_controllers)
331327 SetControllerTimeOfDayLED (controller, event->user .code );
332328
@@ -337,8 +333,6 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
337333 return 0 ;
338334}
339335
340-
341-
342336void hid::Init ()
343337{
344338 SDL_SetHint (SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, " 1" );
@@ -352,15 +346,12 @@ void hid::Init()
352346 SDL_SetHint (SDL_HINT_JOYSTICK_HIDAPI_WII, " 1" );
353347 SDL_SetHint (SDL_HINT_XINPUT_ENABLED, " 1" );
354348
355- SDL_SetHint (SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS, " 0" ); // Uses Button Labels. This hint is disabled for Nintendo Controllers.
356-
357349 SDL_InitSubSystem (SDL_INIT_EVENTS);
358350 SDL_AddEventWatch (HID_OnSDLEvent, nullptr );
359351
360352 SDL_InitSubSystem (SDL_INIT_GAMECONTROLLER);
361353}
362354
363-
364355uint32_t hid::GetState (uint32_t dwUserIndex, XAMINPUT_STATE* pState)
365356{
366357 static uint32_t packet;
0 commit comments