Skip to content

Commit

Permalink
Port key.c to SDL3
Browse files Browse the repository at this point in the history
  • Loading branch information
ankith26 committed Jan 2, 2025
1 parent 6e7a153 commit 262a407
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
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
3 changes: 0 additions & 3 deletions src_c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ event = py.extension_module(
subdir: pg,
)

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

# TODO: support SDL3
if sdl_api != 3
Expand Down

0 comments on commit 262a407

Please sign in to comment.