Skip to content

Commit 3116fe6

Browse files
committed
Fix windowevent translation in sdl2-compat
1 parent c951503 commit 3116fe6

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src_c/event.c

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -470,19 +470,40 @@ _pg_pgevent_deproxify(Uint32 type)
470470
return _pg_pgevent_proxify_helper(type, 0);
471471
}
472472

473+
/* Get type of an event, handling WINDOWEVENT translation on SDL2.
474+
* On SDL3 this function is trivial */
475+
static Uint32
476+
_pg_pgevent_type(SDL_Event *event)
477+
{
473478
#if !SDL_VERSION_ATLEAST(3, 0, 0)
474-
/* We don't need to do window event translation because in SDL3 each window
475-
* event is its own thing anyways */
476-
static int
477-
_pg_translate_windowevent(void *_, SDL_Event *event)
479+
if (event->type == SDL_WINDOWEVENT) {
480+
return PGE_WINDOWSHOWN + event->window.event - 1;
481+
}
482+
#endif
483+
return event->type;
484+
}
485+
486+
/* Handle blocking of pseudo-blocked events.
487+
* Currently this only includes WINDOWEVENT, but can be expanded in the
488+
* future.
489+
*/
490+
#if SDL_VERSION_ATLEAST(3, 0, 0)
491+
static bool SDLCALL
492+
#else
493+
static int SDLCALL
494+
#endif
495+
_pg_filter_blocked_events(void *_, SDL_Event *event)
478496
{
497+
#if SDL_VERSION_ATLEAST(3, 0, 0)
498+
if (event->type >= SDL_EVENT_WINDOW_FIRST &&
499+
event->type <= SDL_EVENT_WINDOW_LAST) {
500+
#else
479501
if (event->type == SDL_WINDOWEVENT) {
480-
event->type = PGE_WINDOWSHOWN + event->window.event - 1;
481-
return PG_EventEnabled(_pg_pgevent_proxify(event->type));
502+
#endif
503+
return PG_EventEnabled(_pg_pgevent_proxify(_pg_pgevent_type(event)));
482504
}
483505
return 1;
484506
}
485-
#endif
486507

487508
#if SDL_VERSION_ATLEAST(3, 0, 0)
488509
static bool SDLCALL
@@ -709,6 +730,14 @@ pg_event_filter(void *_, SDL_Event *event)
709730
return RAISE(pgExc_SDLError, SDL_GetError()), 0;
710731
*/
711732
}
733+
/* TODO:
734+
* Any event that gets blocked here will not be visible to the event
735+
* watchers. So things like WINDOWEVENT should never be blocked here.
736+
* This is taken care of in SDL2 codepaths already but needs to also
737+
* be verified in SDL3 porting.
738+
* If the user requests a block on WINDOWEVENTs we are going to handle
739+
* it specially and call it a "pseudo-block", where the filtering will
740+
* happen in a _pg_filter_blocked_events call. */
712741
return PG_EventEnabled(_pg_pgevent_proxify(event->type));
713742
}
714743

@@ -1747,7 +1776,7 @@ pgEvent_New(SDL_Event *event)
17471776
}
17481777

17491778
if (event) {
1750-
e->type = _pg_pgevent_deproxify(event->type);
1779+
e->type = _pg_pgevent_deproxify(_pg_pgevent_type(event));
17511780
e->dict = dict_from_event(event);
17521781
}
17531782
else {
@@ -1846,14 +1875,7 @@ _pg_event_pump(int dopump)
18461875
SDL_PumpEvents();
18471876
}
18481877

1849-
/* WINDOWEVENT translation needed only on SDL2 */
1850-
#if !SDL_VERSION_ATLEAST(3, 0, 0)
1851-
/* We need to translate WINDOWEVENTS. But if we do that from the
1852-
* from event filter, internal SDL stuff that rely on WINDOWEVENT
1853-
* might break. So after every event pump, we translate events from
1854-
* here */
1855-
SDL_FilterEvents(_pg_translate_windowevent, NULL);
1856-
#endif
1878+
SDL_FilterEvents(_pg_filter_blocked_events, NULL);
18571879
}
18581880

18591881
static int

0 commit comments

Comments
 (0)