@@ -531,6 +531,40 @@ joy_get_hat(PyObject *self, PyObject *args)
531531 return pg_tuple_couple_from_values_int (px , py );
532532}
533533
534+ static PyObject *
535+ joy_set_led (PyObject * self , PyObject * arg )
536+ {
537+ SDL_Joystick * joy = pgJoystick_AsSDL (self );
538+
539+ JOYSTICK_INIT_CHECK ();
540+ if (!joy ) {
541+ return RAISE (pgExc_SDLError , "Joystick not initialized" );
542+ }
543+
544+ Uint8 colors [4 ] = {0 , 0 , 0 , 0 };
545+
546+ if (!pg_RGBAFromObjEx (arg , colors , PG_COLOR_HANDLE_ALL )) {
547+ // Exception already set
548+ return NULL ;
549+ }
550+
551+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
552+ if (SDL_JoystickSetLED (joy , colors [0 ], colors [1 ], colors [2 ]) < 0 ) {
553+ Py_RETURN_FALSE ;
554+ }
555+ Py_RETURN_TRUE ;
556+ #else
557+ // SDL3 renames the function and sets an error message on failure
558+ bool result = SDL_SetJoystickLED (joy , colors [0 ], colors [1 ], colors [2 ]);
559+ if (!result ) {
560+ // Clear the SDL error message that SDL set, for example if it didn't
561+ // have an addressable LED
562+ (void )SDL_GetError ();
563+ }
564+ return PyBool_FromLong (result );
565+ #endif
566+ }
567+
534568static PyMethodDef joy_methods [] = {
535569 {"init" , joy_init , METH_NOARGS , DOC_JOYSTICK_JOYSTICK_INIT },
536570 {"quit" , joy_quit , METH_NOARGS , DOC_JOYSTICK_JOYSTICK_QUIT },
@@ -560,6 +594,7 @@ static PyMethodDef joy_methods[] = {
560594 {"get_numhats" , joy_get_numhats , METH_NOARGS ,
561595 DOC_JOYSTICK_JOYSTICK_GETNUMHATS },
562596 {"get_hat" , joy_get_hat , METH_VARARGS , DOC_JOYSTICK_JOYSTICK_GETHAT },
597+ {"set_led" , joy_set_led , METH_O , DOC_JOYSTICK_JOYSTICK_SETLED },
563598
564599 {NULL , NULL , 0 , NULL }};
565600
@@ -665,6 +700,11 @@ MODINIT_DEFINE(joystick)
665700 return NULL ;
666701 }
667702
703+ import_pygame_color ();
704+ if (PyErr_Occurred ()) {
705+ return NULL ;
706+ }
707+
668708 /* type preparation */
669709 if (PyType_Ready (& pgJoystick_Type ) == -1 ) {
670710 return NULL ;
0 commit comments