Skip to content

Commit de94e90

Browse files
committed
proper Daytime Lightbar and Player LED priority
This adds a else portion for the IsNight Lightbar code. This is done by replicating isNight's dimmer lights and apply it to the Day portion for consistency. additionally: Player LED indicator should now play nicely when Player 0 (onboard inputs) and Player 1 (external) will respect it, although: you'll see the controller showing "Player 2" for a brief moment.
1 parent cebf5e6 commit de94e90

File tree

1 file changed

+53
-80
lines changed

1 file changed

+53
-80
lines changed

UnleashedRecomp/hid/driver/sdl_hid.cpp

Lines changed: 53 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -187,26 +187,28 @@ static void SetControllerInputDevice(Controller* controller)
187187
}
188188
}
189189

190-
static void SetControllerTimeOfDayLED(Controller& controller, bool isNight)
190+
static void SetControllerTimeOfDayLED(Controller& ctrl, bool isNight)
191191
{
192-
// Determine the lightbar color based on night of day.
193-
auto r = isNight ? 22 : 0;
194-
auto g = isNight ? 0 : 37;
195-
auto b = isNight ? 101 : 184;
192+
// Fetch the lightbar color based on the time of day
193+
uint8_t r = 0, g = 0, b = 0;
196194

197-
// Set the LED for the given controller
198-
controller.SetLED(r, g, b);
195+
if (isNight)
196+
{
197+
r = 22; // Night: Red tone
198+
g = 0; // Night: No green
199+
b = 101; // Night: Cool blue
200+
}
201+
else
202+
{
203+
r = 0; // Day: No red
204+
g = 0; // Day: No green
205+
b = 60; // Day: Dimmer blue
206+
}
199207

200-
// Ensure all other controllers mirror Player 1's lightbar
201-
if (controller.controller == g_controllers[0].controller) // Check if it's Player 1
208+
// Apply the lightbar color to the specified controller
209+
if (ctrl.CanPoll())
202210
{
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-
}
211+
ctrl.SetLED(r, g, b);
210212
}
211213
}
212214

@@ -224,28 +226,27 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
224226
auto controller = Controller(event->cdevice.which);
225227
g_controllers[freeIndex] = controller;
226228

227-
// Instantly force the Player LED assignment
228-
if (freeIndex != 0) // External controller
229+
// Assign "Player 1 LED" for Player 0 and Player 1
230+
if (freeIndex == 0 || freeIndex == 1) // Player 0 and Player 1 both use "Player 1 LED"
229231
{
230-
SDL_GameControllerSetPlayerIndex(controller.controller, freeIndex); // Set Player LED immediately
232+
SDL_GameControllerSetPlayerIndex(controller.controller, 1); // Force Player LED to "Player 1"
231233
}
232-
else
234+
else // Additional external controllers (Player 2+)
233235
{
234-
// Disable Player LED for Player 0 immediately
235-
SDL_GameControllerSetPlayerIndex(controller.controller, -1); // Ensure Player 0 stays virtual
236+
SDL_GameControllerSetPlayerIndex(controller.controller, freeIndex); // Assign correct LED for Player 2+
236237
}
237238

238-
// Immediately apply lightbar settings for Player 1 and sync with in-game logic
239-
SetControllerTimeOfDayLED(g_controllers[0], App::s_isWerehog);
239+
// Apply lightbar settings immediately for the new controller
240240
SetControllerTimeOfDayLED(controller, App::s_isWerehog);
241241

242-
// Forcefully override any lingering system defaults right after Lightbar reassignment
243-
SDL_GameControllerSetLED(
244-
controller.controller,
245-
App::s_isWerehog ? 22 : 0,
246-
App::s_isWerehog ? 0 : 37,
247-
App::s_isWerehog ? 101 : 184
248-
);
242+
// Refresh LEDs for all connected controllers
243+
for (auto& ctrl : g_controllers)
244+
{
245+
if (ctrl.CanPoll())
246+
{
247+
SDL_GameControllerSetPlayerIndex(ctrl.controller, ctrl.index); // Ensure correct LED updates
248+
}
249+
}
249250
}
250251
break;
251252
}
@@ -258,30 +259,37 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
258259
{
259260
controller->Close();
260261

261-
// If Player 1's controller is removed, assign the next available controller as Player 1
262-
if (controller == &g_controllers[0])
262+
// If Player 1 disconnects, promote the next available controller to Player 1
263+
if (controller == &g_controllers[1]) // Player 1 removed
263264
{
264265
for (auto& ctrl : g_controllers)
265266
{
266-
if (ctrl.CanPoll())
267+
if (ctrl.CanPoll() && &ctrl != &g_controllers[0]) // Skip Player 0
267268
{
268-
SetControllerInputDevice(&ctrl);
269-
g_controllers[0] = ctrl;
270-
271-
// Instantly reapply Player 1's lightbar and Player LED settings
272-
SetControllerTimeOfDayLED(ctrl, App::s_isWerehog);
273-
SDL_GameControllerSetPlayerIndex(ctrl.controller, 1); // Set as Player 1
269+
g_controllers[1] = ctrl; // Promote next available controller to Player 1
270+
SDL_GameControllerSetPlayerIndex(ctrl.controller, 1); // Reflect Player 1 LED
271+
SetControllerTimeOfDayLED(ctrl, App::s_isWerehog); // Update lightbar
274272
break;
275273
}
276274
}
277275
}
278276

279-
for (std::size_t i = 0; i < g_controllers.size(); ++i)
277+
// Update Player LED indices for all controllers immediately
278+
for (std::size_t i = 2; i < g_controllers.size(); ++i) // Start from Player 2 onward
280279
{
281280
if (g_controllers[i].CanPoll())
282281
{
283-
SDL_GameControllerSetPlayerIndex(g_controllers[i].controller, static_cast<int>(i)); // Set Player LEDs
284-
SetControllerTimeOfDayLED(g_controllers[i], App::s_isWerehog); // Sync lightbar
282+
SDL_GameControllerSetPlayerIndex(g_controllers[i].controller, static_cast<int>(i)); // Assign correct LED index
283+
SetControllerTimeOfDayLED(g_controllers[i], App::s_isWerehog); // Update lightbars
284+
}
285+
}
286+
287+
// Refresh lightbar settings for all connected controllers
288+
for (auto& ctrl : g_controllers)
289+
{
290+
if (ctrl.CanPoll())
291+
{
292+
SetControllerTimeOfDayLED(ctrl, App::s_isWerehog);
285293
}
286294
}
287295
}
@@ -298,6 +306,7 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
298306
if (!controller)
299307
break;
300308

309+
// Process input
301310
if (event->type == SDL_CONTROLLERAXISMOTION)
302311
{
303312
if (abs(event->caxis.value) > 8000)
@@ -316,46 +325,10 @@ int HID_OnSDLEvent(void*, SDL_Event* event)
316325
controller->Poll();
317326
}
318327

319-
// Reapply the lightbar color to override system changes during input events
328+
// Instantly apply updated lightbar settings during input
320329
SetControllerTimeOfDayLED(*controller, App::s_isWerehog);
321330
break;
322331
}
323-
324-
case SDL_KEYDOWN:
325-
case SDL_KEYUP:
326-
hid::g_inputDevice = hid::EInputDevice::Keyboard;
327-
break;
328-
329-
case SDL_MOUSEMOTION:
330-
case SDL_MOUSEBUTTONDOWN:
331-
case SDL_MOUSEBUTTONUP:
332-
{
333-
if (!GameWindow::IsFullscreen() || GameWindow::s_isFullscreenCursorVisible)
334-
SDL_ShowCursor(SDL_ENABLE);
335-
336-
hid::g_inputDevice = hid::EInputDevice::Mouse;
337-
break;
338-
}
339-
340-
case SDL_WINDOWEVENT:
341-
{
342-
if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
343-
{
344-
// Stop vibrating controllers on focus lost.
345-
for (auto& controller : g_controllers)
346-
controller.SetVibration({ 0, 0 });
347-
}
348-
break;
349-
}
350-
351-
case SDL_USER_EVILSONIC:
352-
{
353-
// Refresh all controllers to ensure consistent lightbar colors
354-
for (auto& controller : g_controllers)
355-
SetControllerTimeOfDayLED(controller, event->user.code);
356-
357-
break;
358-
}
359332
}
360333

361334
return 0;

0 commit comments

Comments
 (0)