Skip to content

Commit

Permalink
Port mouse.c to SDL3
Browse files Browse the repository at this point in the history
  • Loading branch information
ankith26 committed Nov 2, 2024
1 parent dbd77b1 commit 8bf0cd7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src_c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ key = py.extension_module(
subdir: pg,
)

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

rect = py.extension_module(
'rect',
Expand Down
41 changes: 41 additions & 0 deletions src_c/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ mouse_set_pos(PyObject *self, PyObject *args)
static PyObject *
mouse_get_pos(PyObject *self, PyObject *args, PyObject *kwargs)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
/* SDL3 changed the mouse API to deal with float coordinates, for now we
* still truncate the result to int before returning to python side.
* This can be changed in a breaking release in the future if needed. */
float x, y;
#else
int x, y;
#endif
int desktop = 0;

static char *kwids[] = {"desktop", NULL};
Expand Down Expand Up @@ -114,7 +121,14 @@ mouse_get_pos(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject *
mouse_get_rel(PyObject *self, PyObject *_null)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
/* SDL3 changed the mouse API to deal with float coordinates, for now we
* still truncate the result to int before returning to python side.
* This can be changed in a breaking release in the future if needed. */
float x, y;
#else
int x, y;
#endif

VIDEO_INIT_CHECK();

Expand Down Expand Up @@ -224,13 +238,18 @@ mouse_set_visible(PyObject *self, PyObject *args)

win = pg_GetDefaultWindow();
if (win) {
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_SetWindowRelativeMouseMode(win,
SDL_GetWindowMouseGrab(win) && !toggle);
#else
int mode = SDL_GetWindowGrab(win);
if ((mode == SDL_ENABLE) & !toggle) {
SDL_SetRelativeMouseMode(1);
}
else {
SDL_SetRelativeMouseMode(0);
}
#endif
window_flags = SDL_GetWindowFlags(win);
if (!toggle && (window_flags & PG_WINDOW_FULLSCREEN_INCLUSIVE)) {
SDL_SetHint(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, "0");
Expand Down Expand Up @@ -263,7 +282,13 @@ mouse_get_visible(PyObject *self, PyObject *_null)

VIDEO_INIT_CHECK();

#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *win = pg_GetDefaultWindow();
result =
win ? (PG_CursorVisible() && !SDL_GetWindowRelativeMouseMode(win)) : 0;
#else
result = (PG_CursorVisible() && !SDL_GetRelativeMouseMode());
#endif

if (0 > result) {
return RAISE(pgExc_SDLError, SDL_GetError());
Expand Down Expand Up @@ -527,7 +552,12 @@ mouse_get_cursor(PyObject *self, PyObject *_null)
static PyObject *
mouse_get_relative_mode(PyObject *self)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *win = pg_GetDefaultWindow();
return PyBool_FromLong(win ? SDL_GetWindowRelativeMouseMode(win) : 0);
#else
return PyBool_FromLong(SDL_GetRelativeMouseMode());
#endif
}

static PyObject *
Expand All @@ -537,9 +567,20 @@ mouse_set_relative_mode(PyObject *self, PyObject *arg)
if (mode == -1) {
return NULL;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *win = pg_GetDefaultWindow();
if (!win) {
return RAISE(pgExc_SDLError,
"display.set_mode has not been called yet.");
}
if (!SDL_SetWindowRelativeMouseMode(win, (bool)mode)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
#else
if (SDL_SetRelativeMouseMode((SDL_bool)mode)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
#endif
Py_RETURN_NONE;
}

Expand Down

0 comments on commit 8bf0cd7

Please sign in to comment.