@@ -112,7 +112,17 @@ static PyObject *
112112get_count (PyObject * self , PyObject * _null )
113113{
114114 JOYSTICK_INIT_CHECK ();
115+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
116+ int ret ;
117+ SDL_JoystickID * joysticks = SDL_GetJoysticks (& ret );
118+ if (!joysticks ) {
119+ return RAISE (pgExc_SDLError , SDL_GetError ());
120+ }
121+ SDL_free (joysticks );
122+ return PyLong_FromLong (ret );
123+ #else
115124 return PyLong_FromLong (SDL_NumJoysticks ());
125+ #endif
116126}
117127
118128static PyObject *
@@ -200,18 +210,53 @@ joy_get_guid(PyObject *self, PyObject *_null)
200210 guid = SDL_JoystickGetGUID (joy );
201211 }
202212 else {
213+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
214+ return RAISE (pgExc_SDLError , "Invalid/closed joystick object" );
215+ #else
203216 guid = SDL_JoystickGetDeviceGUID (pgJoystick_AsID (self ));
217+ #endif
204218 }
205219
220+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
221+ SDL_GUIDToString (guid , strguid , 33 );
222+ #else
206223 SDL_JoystickGetGUIDString (guid , strguid , 33 );
224+ #endif
207225
208226 return PyUnicode_FromString (strguid );
209227}
210228
211229const char *
212- _pg_powerlevel_string (SDL_JoystickPowerLevel level )
230+ _pg_powerlevel_string (SDL_Joystick * joy )
213231{
214- switch (level ) {
232+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
233+ int percent = -1 ;
234+ SDL_PowerState state = SDL_GetJoystickPowerInfo (joy , & percent );
235+ if (state == SDL_POWERSTATE_ON_BATTERY ) {
236+ /* These percentages are based on SDL_JoystickCurrentPowerLevel defined
237+ * in sdl2-compat */
238+ if (percent > 70 ) {
239+ return "full" ;
240+ }
241+ else if (percent > 20 ) {
242+ return "medium" ;
243+ }
244+ else if (percent > 5 ) {
245+ return "low" ;
246+ }
247+ else {
248+ return "empty" ;
249+ }
250+ }
251+ else if (state == SDL_POWERSTATE_UNKNOWN ||
252+ state == SDL_POWERSTATE_ERROR ) {
253+ return "unknown" ;
254+ }
255+ else {
256+ return "wired" ;
257+ }
258+ #else
259+ switch (SDL_JoystickCurrentPowerLevel (joy )) {
215260 case SDL_JOYSTICK_POWER_EMPTY :
216261 return "empty" ;
217262 case SDL_JOYSTICK_POWER_LOW :
@@ -227,12 +272,12 @@ _pg_powerlevel_string(SDL_JoystickPowerLevel level)
227272 default :
228273 return "unknown" ;
229274 }
275+ #endif
230276}
231277
232278static PyObject *
233279joy_get_power_level (PyObject * self , PyObject * _null )
234280{
235- SDL_JoystickPowerLevel level ;
236281 const char * leveltext ;
237282 SDL_Joystick * joy = pgJoystick_AsSDL (self );
238283
@@ -241,8 +286,7 @@ joy_get_power_level(PyObject *self, PyObject *_null)
241286 return RAISE (pgExc_SDLError , "Joystick not initialized" );
242287 }
243288
244- level = SDL_JoystickCurrentPowerLevel (joy );
245- leveltext = _pg_powerlevel_string (level );
289+ leveltext = _pg_powerlevel_string (joy );
246290
247291 return PyUnicode_FromString (leveltext );
248292}
@@ -287,7 +331,11 @@ joy_rumble(pgJoystickObject *self, PyObject *args, PyObject *kwargs)
287331 low = (Uint32 )(lowf * 0xFFFF );
288332 high = (Uint32 )(highf * 0xFFFF );
289333
334+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
335+ if (!SDL_JoystickRumble (joy , low , high , duration )) {
336+ #else
290337 if (SDL_JoystickRumble (joy , low , high , duration ) == -1 ) {
338+ #endif
291339 Py_RETURN_FALSE ;
292340 }
293341 Py_RETURN_TRUE ;
@@ -545,9 +593,13 @@ pgJoystick_New(int id)
545593 JOYSTICK_INIT_CHECK ();
546594
547595 /* Open the SDL device */
596+ #if !SDL_VERSION_ATLEAST (3 , 0 , 0 )
597+ /* This check should be redundant because SDL_JoystickOpen already checks
598+ * and errors if id is out of bounds on SDL3 */
548599 if (id >= SDL_NumJoysticks ()) {
549600 return RAISE (pgExc_SDLError , "Invalid joystick device number" );
550601 }
602+ #endif
551603 joy = SDL_JoystickOpen (id );
552604 if (!joy ) {
553605 return RAISE (pgExc_SDLError , SDL_GetError ());
0 commit comments