Skip to content

Commit 262a407

Browse files
committed
Port key.c to SDL3
1 parent 6e7a153 commit 262a407

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src_c/key.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ pg_scancodewrapper_subscript(pgScancodeWrapper *self, PyObject *item)
8080
PyObject *adjustedvalue, *ret;
8181
if ((index = PyLong_AsLong(item)) == -1 && PyErr_Occurred())
8282
return NULL;
83+
#if SDL_VERSION_ATLEAST(3, 0, 0)
84+
index = SDL_GetScancodeFromKey(index, NULL);
85+
#else
8386
index = SDL_GetScancodeFromKey(index);
87+
#endif
8488
adjustedvalue = PyLong_FromLong(index);
8589
ret = PyTuple_Type.tp_as_mapping->mp_subscript((PyObject *)self,
8690
adjustedvalue);
@@ -163,7 +167,11 @@ static PyObject *
163167
key_get_pressed(PyObject *self, PyObject *_null)
164168
{
165169
int num_keys;
170+
#if SDL_VERSION_ATLEAST(3, 0, 0)
171+
const bool *key_state;
172+
#else
166173
const Uint8 *key_state;
174+
#endif
167175
PyObject *ret_obj = NULL;
168176
PyObject *key_tuple;
169177
int i;
@@ -511,16 +519,42 @@ key_get_focused(PyObject *self, PyObject *_null)
511519
static PyObject *
512520
key_start_text_input(PyObject *self, PyObject *_null)
513521
{
522+
#if SDL_VERSION_ATLEAST(3, 0, 0)
523+
/* Can consider making this a method of the Window class, this function
524+
* just does backcompat */
525+
SDL_Window *win = pg_GetDefaultWindow();
526+
if (!win) {
527+
return RAISE(pgExc_SDLError,
528+
"display.set_mode has not been called yet.");
529+
}
530+
if (!SDL_StartTextInput(win)) {
531+
return RAISE(pgExc_SDLError, SDL_GetError());
532+
}
533+
#else
514534
/* https://wiki.libsdl.org/SDL_StartTextInput */
515535
SDL_StartTextInput();
536+
#endif
516537
Py_RETURN_NONE;
517538
}
518539

519540
static PyObject *
520541
key_stop_text_input(PyObject *self, PyObject *_null)
521542
{
543+
#if SDL_VERSION_ATLEAST(3, 0, 0)
544+
/* Can consider making this a method of the Window class, this function
545+
* just does backcompat */
546+
SDL_Window *win = pg_GetDefaultWindow();
547+
if (!win) {
548+
return RAISE(pgExc_SDLError,
549+
"display.set_mode has not been called yet.");
550+
}
551+
if (!SDL_StopTextInput(win)) {
552+
return RAISE(pgExc_SDLError, SDL_GetError());
553+
}
554+
#else
522555
/* https://wiki.libsdl.org/SDL_StopTextInput */
523556
SDL_StopTextInput();
557+
#endif
524558
Py_RETURN_NONE;
525559
}
526560

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

589+
#if SDL_VERSION_ATLEAST(3, 0, 0)
590+
/* Should consider how to expose the cursor argument to the user, maybe
591+
* this should be new API in Window? */
592+
SDL_SetTextInputArea(sdlWindow, &rect2, 0);
593+
#else
555594
SDL_SetTextInputRect(&rect2);
595+
#endif
556596
Py_RETURN_NONE;
557597
}
558598

599+
#if SDL_VERSION_ATLEAST(3, 0, 0)
600+
/* Should consider how to expose the cursor argument to the user, maybe
601+
* this should be new API in Window? */
602+
SDL_SetTextInputArea(sdlWindow, rect, 0);
603+
#else
559604
SDL_SetTextInputRect(rect);
605+
#endif
560606

561607
Py_RETURN_NONE;
562608
}

src_c/meson.build

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ event = py.extension_module(
4848
subdir: pg,
4949
)
5050

51-
# TODO: support SDL3
52-
if sdl_api != 3
5351
key = py.extension_module(
5452
'key',
5553
'key.c',
@@ -58,7 +56,6 @@ key = py.extension_module(
5856
install: true,
5957
subdir: pg,
6058
)
61-
endif
6259

6360
# TODO: support SDL3
6461
if sdl_api != 3

0 commit comments

Comments
 (0)