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 key and mouse to SDL3 #3207

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
46 changes: 46 additions & 0 deletions src_c/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ pg_scancodewrapper_subscript(pgScancodeWrapper *self, PyObject *item)
PyObject *adjustedvalue, *ret;
if ((index = PyLong_AsLong(item)) == -1 && PyErr_Occurred())
return NULL;
#if SDL_VERSION_ATLEAST(3, 0, 0)
index = SDL_GetScancodeFromKey(index, NULL);
#else
index = SDL_GetScancodeFromKey(index);
#endif
adjustedvalue = PyLong_FromLong(index);
ret = PyTuple_Type.tp_as_mapping->mp_subscript((PyObject *)self,
adjustedvalue);
Expand Down Expand Up @@ -163,7 +167,11 @@ static PyObject *
key_get_pressed(PyObject *self, PyObject *_null)
{
int num_keys;
#if SDL_VERSION_ATLEAST(3, 0, 0)
const bool *key_state;
#else
const Uint8 *key_state;
#endif
PyObject *ret_obj = NULL;
PyObject *key_tuple;
int i;
Expand Down Expand Up @@ -511,16 +519,42 @@ key_get_focused(PyObject *self, PyObject *_null)
static PyObject *
key_start_text_input(PyObject *self, PyObject *_null)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
/* Can consider making this a method of the Window class, this function
* just does backcompat */
SDL_Window *win = pg_GetDefaultWindow();
if (!win) {
return RAISE(pgExc_SDLError,
"display.set_mode has not been called yet.");
}
if (!SDL_StartTextInput(win)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
#else
/* https://wiki.libsdl.org/SDL_StartTextInput */
SDL_StartTextInput();
#endif
Py_RETURN_NONE;
}

static PyObject *
key_stop_text_input(PyObject *self, PyObject *_null)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
/* Can consider making this a method of the Window class, this function
* just does backcompat */
SDL_Window *win = pg_GetDefaultWindow();
if (!win) {
return RAISE(pgExc_SDLError,
"display.set_mode has not been called yet.");
}
if (!SDL_StopTextInput(win)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
#else
/* https://wiki.libsdl.org/SDL_StopTextInput */
SDL_StopTextInput();
#endif
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -552,11 +586,23 @@ key_set_text_input_rect(PyObject *self, PyObject *obj)
rect2.w = (int)(rect->w * scalex);
rect2.h = (int)(rect->h * scaley);

#if SDL_VERSION_ATLEAST(3, 0, 0)
/* Should consider how to expose the cursor argument to the user, maybe
* this should be new API in Window? */
SDL_SetTextInputArea(sdlWindow, &rect2, 0);
#else
SDL_SetTextInputRect(&rect2);
#endif
Py_RETURN_NONE;
}

#if SDL_VERSION_ATLEAST(3, 0, 0)
/* Should consider how to expose the cursor argument to the user, maybe
* this should be new API in Window? */
SDL_SetTextInputArea(sdlWindow, rect, 0);
#else
SDL_SetTextInputRect(rect);
#endif

Py_RETURN_NONE;
}
Expand Down
6 changes: 0 additions & 6 deletions src_c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ event = py.extension_module(
)
endif

# TODO: support SDL3
if sdl_api != 3
key = py.extension_module(
'key',
'key.c',
Expand All @@ -64,10 +62,7 @@ key = py.extension_module(
install: true,
subdir: pg,
)
endif

# TODO: support SDL3
if sdl_api != 3
mouse = py.extension_module(
'mouse',
'mouse.c',
Expand All @@ -76,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;
Copy link
Member

Choose a reason for hiding this comment

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

So it gets it as a float, how does it get converted to an int? Because it gets passed a function that takes ints.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that just happens implicitly, I didn't bother documenting it because I didn't think it should cause any confusion

#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
Loading