@@ -388,6 +388,35 @@ controller_stop_rumble(pgControllerObject *self, PyObject *_null)
388388 Py_RETURN_NONE ;
389389}
390390
391+ static PyObject *
392+ controller_set_led (pgControllerObject * self , PyObject * arg )
393+ {
394+ CONTROLLER_INIT_CHECK ();
395+
396+ Uint8 colors [4 ] = {0 , 0 , 0 , 0 };
397+
398+ if (!pg_RGBAFromObjEx (arg , colors , PG_COLOR_HANDLE_ALL )) {
399+ return RAISE (PyExc_TypeError ,
400+ "set_led must be passed in a Color-compatible argument" );
401+ }
402+
403+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
404+ if (SDL_GameControllerSetLED (self -> controller , colors [0 ], colors [1 ], colors [2 ]) < 0 ) {
405+ Py_RETURN_FALSE ;
406+ }
407+ Py_RETURN_TRUE ;
408+ #else
409+ // SDL3 renames the function and sets an error message on failure
410+ bool result = SDL_SetGamepadLED (self -> controller , colors [0 ], colors [1 ], colors [2 ]);
411+ if (!result ) {
412+ // Clear the SDL error message that SDL set, for example if it didn't
413+ // have an addressable LED
414+ (void )SDL_GetError ();
415+ }
416+ return PyBool_FromLong (result );
417+ #endif
418+ }
419+
391420static PyMethodDef controller_methods [] = {
392421 {"from_joystick" , (PyCFunction )controller_from_joystick ,
393422 METH_CLASS | METH_VARARGS | METH_KEYWORDS ,
@@ -414,6 +443,7 @@ static PyMethodDef controller_methods[] = {
414443 DOC_SDL2_CONTROLLER_CONTROLLER_RUMBLE },
415444 {"stop_rumble" , (PyCFunction )controller_stop_rumble , METH_NOARGS ,
416445 DOC_SDL2_CONTROLLER_CONTROLLER_STOPRUMBLE },
446+ {"set_led" , (PyCFunction )controller_set_led , METH_O , DOC_SDL2_CONTROLLER_CONTROLLER_SETLED },
417447 {NULL , NULL , 0 , NULL }};
418448
419449static PyMemberDef controller_members [] = {
@@ -569,6 +599,11 @@ MODINIT_DEFINE(controller)
569599 return NULL ;
570600 }
571601
602+ import_pygame_color ();
603+ if (PyErr_Occurred ()) {
604+ return NULL ;
605+ }
606+
572607 import_pygame_joystick ();
573608 if (PyErr_Occurred ()) {
574609 return NULL ;
0 commit comments