@@ -112,7 +112,17 @@ static PyObject *
112
112
get_count (PyObject * self , PyObject * _null )
113
113
{
114
114
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
115
124
return PyLong_FromLong (SDL_NumJoysticks ());
125
+ #endif
116
126
}
117
127
118
128
static PyObject *
@@ -200,18 +210,53 @@ joy_get_guid(PyObject *self, PyObject *_null)
200
210
guid = SDL_JoystickGetGUID (joy );
201
211
}
202
212
else {
213
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
214
+ return RAISE (pgExc_SDLError , "Invalid/closed joystick object" );
215
+ #else
203
216
guid = SDL_JoystickGetDeviceGUID (pgJoystick_AsID (self ));
217
+ #endif
204
218
}
205
219
220
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
221
+ SDL_GUIDToString (guid , strguid , 33 );
222
+ #else
206
223
SDL_JoystickGetGUIDString (guid , strguid , 33 );
224
+ #endif
207
225
208
226
return PyUnicode_FromString (strguid );
209
227
}
210
228
211
229
const char *
212
- _pg_powerlevel_string (SDL_JoystickPowerLevel level )
230
+ _pg_powerlevel_string (SDL_Joystick * joy )
213
231
{
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 )) {
215
260
case SDL_JOYSTICK_POWER_EMPTY :
216
261
return "empty" ;
217
262
case SDL_JOYSTICK_POWER_LOW :
@@ -227,12 +272,12 @@ _pg_powerlevel_string(SDL_JoystickPowerLevel level)
227
272
default :
228
273
return "unknown" ;
229
274
}
275
+ #endif
230
276
}
231
277
232
278
static PyObject *
233
279
joy_get_power_level (PyObject * self , PyObject * _null )
234
280
{
235
- SDL_JoystickPowerLevel level ;
236
281
const char * leveltext ;
237
282
SDL_Joystick * joy = pgJoystick_AsSDL (self );
238
283
@@ -241,8 +286,7 @@ joy_get_power_level(PyObject *self, PyObject *_null)
241
286
return RAISE (pgExc_SDLError , "Joystick not initialized" );
242
287
}
243
288
244
- level = SDL_JoystickCurrentPowerLevel (joy );
245
- leveltext = _pg_powerlevel_string (level );
289
+ leveltext = _pg_powerlevel_string (joy );
246
290
247
291
return PyUnicode_FromString (leveltext );
248
292
}
@@ -287,7 +331,11 @@ joy_rumble(pgJoystickObject *self, PyObject *args, PyObject *kwargs)
287
331
low = (Uint32 )(lowf * 0xFFFF );
288
332
high = (Uint32 )(highf * 0xFFFF );
289
333
334
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
335
+ if (!SDL_JoystickRumble (joy , low , high , duration )) {
336
+ #else
290
337
if (SDL_JoystickRumble (joy , low , high , duration ) == -1 ) {
338
+ #endif
291
339
Py_RETURN_FALSE ;
292
340
}
293
341
Py_RETURN_TRUE ;
@@ -545,9 +593,13 @@ pgJoystick_New(int id)
545
593
JOYSTICK_INIT_CHECK ();
546
594
547
595
/* 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 */
548
599
if (id >= SDL_NumJoysticks ()) {
549
600
return RAISE (pgExc_SDLError , "Invalid joystick device number" );
550
601
}
602
+ #endif
551
603
joy = SDL_JoystickOpen (id );
552
604
if (!joy ) {
553
605
return RAISE (pgExc_SDLError , SDL_GetError ());
0 commit comments