diff --git a/src_c/joystick.c b/src_c/joystick.c index 806dfd0b77..f721b8a0b9 100644 --- a/src_c/joystick.c +++ b/src_c/joystick.c @@ -112,7 +112,17 @@ static PyObject * get_count(PyObject *self, PyObject *_null) { JOYSTICK_INIT_CHECK(); +#if SDL_VERSION_ATLEAST(3, 0, 0) + int ret; + SDL_JoystickID *joysticks = SDL_GetJoysticks(&ret); + if (!joysticks) { + return RAISE(pgExc_SDLError, SDL_GetError()); + } + SDL_free(joysticks); + return PyLong_FromLong(ret); +#else return PyLong_FromLong(SDL_NumJoysticks()); +#endif } static PyObject * @@ -200,18 +210,53 @@ joy_get_guid(PyObject *self, PyObject *_null) guid = SDL_JoystickGetGUID(joy); } else { +#if SDL_VERSION_ATLEAST(3, 0, 0) + guid = SDL_GetJoystickGUIDForID(pgJoystick_AsID(self)); +#else guid = SDL_JoystickGetDeviceGUID(pgJoystick_AsID(self)); +#endif } +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_GUIDToString(guid, strguid, 33); +#else SDL_JoystickGetGUIDString(guid, strguid, 33); +#endif return PyUnicode_FromString(strguid); } const char * -_pg_powerlevel_string(SDL_JoystickPowerLevel level) +_pg_powerlevel_string(SDL_Joystick *joy) { - switch (level) { +#if SDL_VERSION_ATLEAST(3, 0, 0) + int percent = -1; + SDL_PowerState state = SDL_GetJoystickPowerInfo(joy, &percent); + if (state == SDL_POWERSTATE_ON_BATTERY) { + /* These percentages are based on SDL_JoystickCurrentPowerLevel defined + * in sdl2-compat */ + if (percent > 70) { + return "full"; + } + else if (percent > 20) { + return "medium"; + } + else if (percent > 5) { + return "low"; + } + else { + return "empty"; + } + } + else if (state == SDL_POWERSTATE_UNKNOWN || + state == SDL_POWERSTATE_ERROR) { + return "unknown"; + } + else { + return "wired"; + } +#else + switch (SDL_JoystickCurrentPowerLevel(joy)) { case SDL_JOYSTICK_POWER_EMPTY: return "empty"; case SDL_JOYSTICK_POWER_LOW: @@ -227,12 +272,12 @@ _pg_powerlevel_string(SDL_JoystickPowerLevel level) default: return "unknown"; } +#endif } static PyObject * joy_get_power_level(PyObject *self, PyObject *_null) { - SDL_JoystickPowerLevel level; const char *leveltext; SDL_Joystick *joy = pgJoystick_AsSDL(self); @@ -241,8 +286,7 @@ joy_get_power_level(PyObject *self, PyObject *_null) return RAISE(pgExc_SDLError, "Joystick not initialized"); } - level = SDL_JoystickCurrentPowerLevel(joy); - leveltext = _pg_powerlevel_string(level); + leveltext = _pg_powerlevel_string(joy); return PyUnicode_FromString(leveltext); } @@ -287,7 +331,11 @@ joy_rumble(pgJoystickObject *self, PyObject *args, PyObject *kwargs) low = (Uint32)(lowf * 0xFFFF); high = (Uint32)(highf * 0xFFFF); +#if SDL_VERSION_ATLEAST(3, 0, 0) + if (!SDL_JoystickRumble(joy, low, high, duration)) { +#else if (SDL_JoystickRumble(joy, low, high, duration) == -1) { +#endif Py_RETURN_FALSE; } Py_RETURN_TRUE; @@ -545,9 +593,13 @@ pgJoystick_New(int id) JOYSTICK_INIT_CHECK(); /* Open the SDL device */ +#if !SDL_VERSION_ATLEAST(3, 0, 0) + /* This check should be redundant because SDL_JoystickOpen already checks + * and errors if id is out of bounds on SDL3 */ if (id >= SDL_NumJoysticks()) { return RAISE(pgExc_SDLError, "Invalid joystick device number"); } +#endif joy = SDL_JoystickOpen(id); if (!joy) { return RAISE(pgExc_SDLError, SDL_GetError()); diff --git a/src_c/meson.build b/src_c/meson.build index fb43a584eb..8cae8145a6 100644 --- a/src_c/meson.build +++ b/src_c/meson.build @@ -170,8 +170,6 @@ time = py.extension_module( ) endif -# TODO: support SDL3 -if sdl_api != 3 joystick = py.extension_module( 'joystick', 'joystick.c', @@ -180,7 +178,6 @@ joystick = py.extension_module( install: true, subdir: pg, ) -endif # TODO: support SDL3 @@ -397,9 +394,6 @@ _camera = py.extension_module( endif # pygame.scrap - -# TODO: support SDL3 -if sdl_api != 3 pg_scrap_link = [] # TODO: should this link logic be improved/made meson-ey? if plat == 'win' pg_scrap_link += ['-luser32', '-lgdi32'] @@ -414,7 +408,6 @@ scrap = py.extension_module( install: true, subdir: pg, ) -endif # optional modules diff --git a/src_c/scrap.c b/src_c/scrap.c index ef580e7a99..eb1a95f4b5 100644 --- a/src_c/scrap.c +++ b/src_c/scrap.c @@ -27,9 +27,8 @@ #include #else #include -#endif - #include "SDL_syswm.h" +#endif #include "pygame.h" diff --git a/src_c/scrap_win.c b/src_c/scrap_win.c index 21be080ff9..188a1363c5 100644 --- a/src_c/scrap_win.c +++ b/src_c/scrap_win.c @@ -158,6 +158,15 @@ _create_dib_buffer(char *data, size_t *count) int pygame_scrap_init(void) { +#if SDL_VERSION_ATLEAST(3, 0, 0) + int retval = 0; + window_handle = (HWND)SDL_GetPointerProperty( + SDL_GetWindowProperties(pg_GetDefaultWindow()), + SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL); + if (window_handle) { + retval = 1; + } +#else SDL_SysWMinfo info; int retval = 0; @@ -170,6 +179,7 @@ pygame_scrap_init(void) window_handle = info.info.win.window; retval = 1; } +#endif if (retval) _scrapinitialized = 1;