Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port joystick and scrap to SDL3 #3208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions src_c/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I didn't think about while making the PR: SDL_GetJoystickGUIDForID uses the instance ID and not the device ID returned by pgJoystick_AsID. This block needs more attention and thought

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the joystick port will need some more thought. They removed device indices completely, meaning we need to rework the initialization of joystick objects in general. There is no easy way to emulate this either (and we shouldn't anyway!).

With that said, you can use SDL_GetJoystickID to get the instance_id of an already open joystick.

#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:
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
7 changes: 0 additions & 7 deletions src_c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ time = py.extension_module(
)
endif

# TODO: support SDL3
if sdl_api != 3
joystick = py.extension_module(
'joystick',
'joystick.c',
Expand All @@ -180,7 +178,6 @@ joystick = py.extension_module(
install: true,
subdir: pg,
)
endif


# TODO: support SDL3
Expand Down Expand Up @@ -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']
Expand All @@ -414,7 +408,6 @@ scrap = py.extension_module(
install: true,
subdir: pg,
)
endif

# optional modules

Expand Down
3 changes: 1 addition & 2 deletions src_c/scrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
#include <SDL3/SDL.h>
#else
#include <SDL.h>
#endif

#include "SDL_syswm.h"
ankith26 marked this conversation as resolved.
Show resolved Hide resolved
#endif

#include "pygame.h"

Expand Down
10 changes: 10 additions & 0 deletions src_c/scrap_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -170,6 +179,7 @@ pygame_scrap_init(void)
window_handle = info.info.win.window;
retval = 1;
}
#endif

if (retval)
_scrapinitialized = 1;
Expand Down
Loading