From 1aa458d64b7e4cb4ee502d3945e129770e6af0cb Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sun, 6 Oct 2024 02:25:49 -0700 Subject: [PATCH] SDL3 support for pygame.system --- src_c/meson.build | 3 --- src_c/system.c | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src_c/meson.build b/src_c/meson.build index f2eb2a02d1..6972b28f0f 100644 --- a/src_c/meson.build +++ b/src_c/meson.build @@ -321,8 +321,6 @@ newbuffer = py.extension_module( ) # new/experimental/uncommon stuff, but built by default -# TODO: support SDL3 -if sdl_api != 3 system = py.extension_module( 'system', 'system.c', @@ -331,7 +329,6 @@ system = py.extension_module( install: true, subdir: pg, ) -endif geometry = py.extension_module( 'geometry', diff --git a/src_c/system.c b/src_c/system.c index 7d715821ad..8794a95993 100644 --- a/src_c/system.c +++ b/src_c/system.c @@ -96,8 +96,24 @@ pg_system_get_pref_locales(PyObject *self, PyObject *_null) return NULL; } -#if SDL_VERSION_ATLEAST(2, 0, 14) + // Sorry about the SDL3 gnarliness here, this was the best way I could + // think of to support SDL2/SDL3 at once. The approach is that each + // version is responsible for coming up with a list and a count, + // then the iteration over the list is shared (except for the indexing + // strategy, where SDL2/3 are different) + PyObject *dict, *val = NULL; + int num_locales; + SDL_Locale *current_locale; + +#if SDL_VERSION_ATLEAST(3, 0, 0) + SDL_Locale **locales = SDL_GetPreferredLocales(&num_locales); + if (!locales) { + /* Return an empty list if SDL function does not return any useful + * information */ + return ret_list; + } +#elif SDL_VERSION_ATLEAST(2, 0, 14) SDL_Locale *locales = SDL_GetPreferredLocales(); if (!locales) { /* Return an empty list if SDL function does not return any useful @@ -105,16 +121,28 @@ pg_system_get_pref_locales(PyObject *self, PyObject *_null) return ret_list; } - SDL_Locale *current_locale = locales; - + num_locales = 0; + current_locale = locales; /* The array is terminated when the language attribute of the last struct * in the array is NULL */ while (current_locale->language) { + num_locales++; + current_locale++; + } +#endif + +#if SDL_VERSION_ATLEAST(2, 0, 14) + for (int i = 0; i < num_locales; i++) { dict = PyDict_New(); if (!dict) { goto error; } +#if SDL_VERSION_ATLEAST(3, 0, 0) + current_locale = locales[i]; +#else + current_locale = locales + i; +#endif val = PyUnicode_FromString(current_locale->language); if (!val) { goto error; @@ -145,7 +173,6 @@ pg_system_get_pref_locales(PyObject *self, PyObject *_null) goto error; } Py_DECREF(dict); - current_locale++; } SDL_free(locales);