Skip to content

Commit 3a17519

Browse files
committed
SDL3: display+window: runtime fixes
1 parent 138b114 commit 3a17519

File tree

2 files changed

+158
-57
lines changed

2 files changed

+158
-57
lines changed

src_c/display.c

Lines changed: 108 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ pg_get_init(PyObject *self, PyObject *_null)
279279
static PyObject *
280280
pg_get_active(PyObject *self, PyObject *_null)
281281
{
282-
SDL_WindowFlags flags = SDL_GetWindowFlags(pg_GetDefaultWindow());
282+
SDL_Window *win = pg_GetDefaultWindow();
283+
if (!win) {
284+
Py_RETURN_FALSE;
285+
}
286+
SDL_WindowFlags flags = SDL_GetWindowFlags(win);
283287

284288
#if SDL_VERSION_ATLEAST(3, 0, 0)
285289
return PyBool_FromLong(!(flags & SDL_WINDOW_HIDDEN) &&
@@ -460,7 +464,12 @@ pg_GetVideoInfo(pg_VideoInfo *info)
460464
}
461465
else {
462466
#if SDL_VERSION_ATLEAST(3, 0, 0)
463-
if ((mode_ptr = SDL_GetCurrentDisplayMode(0))) {
467+
SDL_DisplayID primary_display = SDL_GetPrimaryDisplay();
468+
if (primary_display == 0) {
469+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
470+
return (pg_VideoInfo *)NULL;
471+
}
472+
if ((mode_ptr = SDL_GetCurrentDisplayMode(primary_display))) {
464473
info->current_w = mode_ptr->w;
465474
info->current_h = mode_ptr->h;
466475
formatenum = mode_ptr->format;
@@ -1118,12 +1127,12 @@ PG_CreateWindowCompat(const char *title, int x, int y, int w, int h,
11181127
}
11191128

11201129
#if SDL_VERSION_ATLEAST(3, 0, 0)
1121-
/* Returns 0 on success, negative on failure. */
1122-
static int
1130+
/* Returns true on success, false on failure. */
1131+
static bool
11231132
PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
11241133
bool non_desktop_fullscreen)
11251134
{
1126-
int ret = -1;
1135+
bool ret = false;
11271136
SDL_DisplayMode **modes = NULL;
11281137
SDL_DisplayMode *chosen_mode = NULL;
11291138
if (!SDL_SetWindowFullscreen(window, fullscreen)) {
@@ -1152,11 +1161,28 @@ PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
11521161
}
11531162
}
11541163

1155-
ret = 0;
1164+
/* Try to sync window here.
1165+
* TODO: this call seems to error a lot, so for now skip error handling.
1166+
* Possibly related issue: https://github.com/libsdl-org/SDL/issues/11239
1167+
*/
1168+
SDL_SyncWindow(window);
1169+
ret = true;
11561170
end:
11571171
SDL_free(modes);
11581172
return ret;
11591173
}
1174+
#else
1175+
static bool
1176+
PG_SetWindowFullscreen(SDL_Window *window, bool fullscreen,
1177+
bool non_desktop_fullscreen)
1178+
{
1179+
int flags = 0;
1180+
if (fullscreen) {
1181+
flags = non_desktop_fullscreen ? SDL_WINDOW_FULLSCREEN
1182+
: SDL_WINDOW_FULLSCREEN_DESKTOP;
1183+
}
1184+
return (SDL_SetWindowFullscreen(window, flags) == 0) ? true : false;
1185+
}
11601186
#endif
11611187

11621188
static PyObject *
@@ -1235,6 +1261,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
12351261
}
12361262
}
12371263

1264+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1265+
/* In SDL2, display == 0 meant primary display, so compat code for it */
1266+
if (display == 0) {
1267+
display = SDL_GetPrimaryDisplay();
1268+
if (display == 0) {
1269+
return RAISE(pgExc_SDLError, SDL_GetError());
1270+
}
1271+
}
1272+
#endif
1273+
12381274
if ((vsync == -1) && ((flags & PGS_OPENGL) == 0)) {
12391275
return RAISE(PyExc_ValueError,
12401276
"requested adaptive vsync without OpenGL");
@@ -1437,11 +1473,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
14371473
if (flags & PGS_SCALED && !(flags & PGS_FULLSCREEN)) {
14381474
SDL_Rect display_bounds;
14391475
int fractional_scaling = SDL_FALSE;
1440-
1476+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1477+
if (!SDL_GetDisplayUsableBounds(display, &display_bounds)) {
1478+
return RAISE(pgExc_SDLError, SDL_GetError());
1479+
}
1480+
#else
14411481
if (0 !=
14421482
SDL_GetDisplayUsableBounds(display, &display_bounds)) {
14431483
return RAISE(pgExc_SDLError, SDL_GetError());
14441484
}
1485+
#endif
14451486

14461487
if (SDL_GetHintBoolean("SDL_HINT_RENDER_SCALE_QUALITY",
14471488
SDL_FALSE)) {
@@ -1849,6 +1890,11 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
18491890
}
18501891
}
18511892
}
1893+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1894+
if (!SDL_SyncWindow(win)) {
1895+
return RAISE(pgExc_SDLError, SDL_GetError());
1896+
}
1897+
#endif
18521898

18531899
/*return the window's surface (screen)*/
18541900
Py_INCREF(surface);
@@ -1947,6 +1993,11 @@ pg_set_window_position(PyObject *self, PyObject *arg)
19471993
if (win) {
19481994
/* Will raise errors with SDL 3, deal with it during the porting */
19491995
SDL_SetWindowPosition(win, x, y);
1996+
#if SDL_VERSION_ATLEAST(3, 0, 0)
1997+
if (!SDL_SyncWindow(win)) {
1998+
return RAISE(pgExc_SDLError, SDL_GetError());
1999+
}
2000+
#endif
19502001
}
19512002

19522003
Py_RETURN_NONE;
@@ -1973,7 +2024,16 @@ pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds)
19732024
&display_index)) {
19742025
return NULL;
19752026
}
1976-
#if !SDL_VERSION_ATLEAST(3, 0, 0)
2027+
2028+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2029+
/* In SDL2, display == 0 meant primary display, so compat code for it */
2030+
if (display_index == 0) {
2031+
display_index = SDL_GetPrimaryDisplay();
2032+
if (display_index == 0) {
2033+
return RAISE(pgExc_SDLError, SDL_GetError());
2034+
}
2035+
}
2036+
#else
19772037
/* Display ID is not bounded by number of displays in SDL3 */
19782038
if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) {
19792039
return RAISE(PyExc_ValueError,
@@ -2039,7 +2099,15 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds)
20392099
return NULL;
20402100
}
20412101

2042-
#if !SDL_VERSION_ATLEAST(3, 0, 0)
2102+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2103+
/* In SDL2, display == 0 meant primary display, so compat code for it */
2104+
if (display_index == 0) {
2105+
display_index = SDL_GetPrimaryDisplay();
2106+
if (display_index == 0) {
2107+
return RAISE(pgExc_SDLError, SDL_GetError());
2108+
}
2109+
}
2110+
#else
20432111
/* Display ID is not bounded by number of displays in SDL3 */
20442112
if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) {
20452113
return RAISE(PyExc_ValueError,
@@ -2700,6 +2768,11 @@ pg_iconify(PyObject *self, PyObject *_null)
27002768
return RAISE(pgExc_SDLError, "No open window");
27012769
}
27022770
SDL_MinimizeWindow(win);
2771+
#if SDL_VERSION_ATLEAST(3, 0, 0)
2772+
if (!SDL_SyncWindow(win)) {
2773+
return RAISE(pgExc_SDLError, SDL_GetError());
2774+
}
2775+
#endif
27032776
return PyBool_FromLong(1);
27042777
}
27052778

@@ -3035,7 +3108,6 @@ static PyObject *
30353108
pg_toggle_fullscreen(PyObject *self, PyObject *_null)
30363109
{
30373110
SDL_Window *win = pg_GetDefaultWindow();
3038-
int result;
30393111
SDL_WindowFlags flags;
30403112
int window_w, window_h, w, h, window_display, x, y;
30413113
pgSurfaceObject *display_surface;
@@ -3164,8 +3236,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
31643236
/* TOGGLE FULLSCREEN OFF */
31653237

31663238
if (state->unscaled_render) {
3167-
result = SDL_SetWindowFullscreen(win, 0);
3168-
if (result != 0) {
3239+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
31693240
return RAISE(pgExc_SDLError, SDL_GetError());
31703241
}
31713242
}
@@ -3179,8 +3250,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
31793250
if (scale < 1) {
31803251
scale = 1;
31813252
}
3182-
result = SDL_SetWindowFullscreen(win, 0);
3183-
if (result != 0) {
3253+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
31843254
return RAISE(pgExc_SDLError, SDL_GetError());
31853255
}
31863256
SDL_SetWindowSize(win, w * scale, h * scale);
@@ -3220,8 +3290,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32203290
/* this is literally the only place where state->toggle_windowed_w
32213291
* should ever be read. We only use it because with GL, there is no
32223292
* display surface we can query for dimensions. */
3223-
result = SDL_SetWindowFullscreen(win, 0);
3224-
if (result != 0) {
3293+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
32253294
return RAISE(pgExc_SDLError, SDL_GetError());
32263295
}
32273296
SDL_GL_MakeCurrent(win, state->gl_context);
@@ -3257,8 +3326,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32573326
else if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) ==
32583327
SDL_WINDOW_FULLSCREEN_DESKTOP) {
32593328
#endif
3260-
result = SDL_SetWindowFullscreen(win, 0);
3261-
if (result != 0) {
3329+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
32623330
return RAISE(pgExc_SDLError, SDL_GetError());
32633331
}
32643332
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3287,15 +3355,11 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
32873355
if (win == NULL) {
32883356
return RAISE(pgExc_SDLError, SDL_GetError());
32893357
}
3290-
else {
3291-
result = 0;
3292-
}
32933358
display_surface->surf = SDL_GetWindowSurface(win);
32943359
pg_SetDefaultWindow(win);
32953360
}
32963361
else {
3297-
result = SDL_SetWindowFullscreen(win, 0);
3298-
if (result != 0) {
3362+
if (!PG_SetWindowFullscreen(win, 0, 0)) {
32993363
return RAISE(pgExc_SDLError, SDL_GetError());
33003364
}
33013365
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3330,24 +3394,12 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
33303394
state->fullscreen_backup_y = y;
33313395

33323396
if (state->unscaled_render) {
3333-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3334-
result = PG_SetWindowFullscreen(win, 1, 0);
3335-
#else
3336-
result =
3337-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3338-
#endif
3339-
if (result != 0) {
3397+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
33403398
return RAISE(pgExc_SDLError, SDL_GetError());
33413399
}
33423400
}
33433401
else if (pg_renderer != NULL) {
3344-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3345-
result = PG_SetWindowFullscreen(win, 1, 0);
3346-
#else
3347-
result =
3348-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3349-
#endif
3350-
if (result != 0) {
3402+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
33513403
return RAISE(pgExc_SDLError, SDL_GetError());
33523404
}
33533405
if (is_renderer_software && subsystem == SDL_SYSWM_X11) {
@@ -3380,13 +3432,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
33803432
#endif
33813433
}
33823434
else if (state->using_gl) {
3383-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3384-
result = PG_SetWindowFullscreen(win, 1, 0);
3385-
#else
3386-
result =
3387-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3388-
#endif
3389-
if (result != 0) {
3435+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
33903436
return RAISE(pgExc_SDLError, SDL_GetError());
33913437
}
33923438
SDL_GL_MakeCurrent(win, state->gl_context);
@@ -3411,13 +3457,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34113457
}
34123458
}
34133459
else if (w == display_mode->w && h == display_mode->h) {
3414-
#if SDL_VERSION_ATLEAST(3, 0, 0)
3415-
result = PG_SetWindowFullscreen(win, 1, 0);
3416-
#else
3417-
result =
3418-
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
3419-
#endif
3420-
if (result != 0) {
3460+
if (!PG_SetWindowFullscreen(win, 1, 0)) {
34213461
return RAISE(pgExc_SDLError, SDL_GetError());
34223462
}
34233463
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3434,8 +3474,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34343474
return PyLong_FromLong(-1);
34353475
}
34363476
else {
3437-
result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN);
3438-
if (result != 0) {
3477+
if (!PG_SetWindowFullscreen(win, 1, 1)) {
34393478
return RAISE(pgExc_SDLError, SDL_GetError());
34403479
}
34413480
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3447,7 +3486,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34473486
if (win == NULL) {
34483487
return RAISE(pgExc_SDLError, SDL_GetError());
34493488
}
3450-
if (0 != SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN)) {
3489+
if (!PG_SetWindowFullscreen(win, 1, 1)) {
34513490
return RAISE(pgExc_SDLError, SDL_GetError());
34523491
}
34533492
display_surface->surf = SDL_GetWindowSurface(win);
@@ -3461,7 +3500,12 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null)
34613500
}
34623501
}
34633502
}
3464-
return PyLong_FromLong(result != 0);
3503+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3504+
if (!SDL_SyncWindow(win)) {
3505+
return RAISE(pgExc_SDLError, SDL_GetError());
3506+
}
3507+
#endif
3508+
return PyLong_FromLong(1);
34653509
}
34663510

34673511
/* This API is provisional, and, not finalised, and should not be documented
@@ -3539,6 +3583,11 @@ pg_display_resize_event(PyObject *self, PyObject *event)
35393583
/* do not do anything that would invalidate a display surface! */
35403584
return PyLong_FromLong(-1);
35413585
}
3586+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3587+
if (!SDL_SyncWindow(win)) {
3588+
return RAISE(pgExc_SDLError, SDL_GetError());
3589+
}
3590+
#endif
35423591
Py_RETURN_FALSE;
35433592
}
35443593

@@ -3771,7 +3820,11 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs)
37713820

37723821
int clicked_button_id;
37733822

3823+
#if SDL_VERSION_ATLEAST(3, 0, 0)
3824+
if (!SDL_ShowMessageBox(&msgbox_data, &clicked_button_id)) {
3825+
#else
37743826
if (SDL_ShowMessageBox(&msgbox_data, &clicked_button_id)) {
3827+
#endif
37753828
PyErr_SetString(pgExc_SDLError, SDL_GetError());
37763829
goto error;
37773830
}

0 commit comments

Comments
 (0)