Skip to content

Commit 42afa56

Browse files
committed
[vulkan_]sdl3: handle positioning errors
- return on errors - do not set the position when neither display/x/y was set (prevent Wayland errors) - check that the only x/y or display was set (not both position and display)
1 parent c5f38fe commit 42afa56

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

src/video_display/sdl3.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void display_frame(struct state_sdl3 *s, struct video_frame *frame);
9393
static struct video_frame *display_sdl3_getf(void *state);
9494
static void display_sdl3_new_message(struct module *mod);
9595
static bool display_sdl3_reconfigure_real(void *state, struct video_desc desc);
96-
static void loadSplashscreen(struct state_sdl3 *s);
96+
static void display_sdl3_done(void *state);
9797

9898
enum deint { DEINT_OFF, DEINT_ON, DEINT_FORCE };
9999

@@ -778,22 +778,30 @@ vulkan_warn(const char *req_renderers_name, const char *actual_renderer_name)
778778
explicit ? "" : " Please report!");
779779
}
780780

781-
static void
781+
static bool
782782
sdl3_set_window_position(struct state_sdl3 *s) {
783783
if (s->display_idx == -1 && s->x == SDL_WINDOWPOS_UNDEFINED &&
784784
s->y == SDL_WINDOWPOS_UNDEFINED) {
785-
return;
785+
return true; // nothing to set
786786
}
787787
int x = s->x;
788788
int y = s->y;
789789
if (s->display_idx != -1) {
790+
if (x != SDL_WINDOWPOS_UNDEFINED || y != SDL_WINDOWPOS_UNDEFINED) {
791+
MSG(ERROR, "Do not set windows positiona and display "
792+
"at the same time!\n");
793+
return false;
794+
}
790795
const SDL_DisplayID display_id =
791796
get_display_id_from_idx(s->display_idx);
797+
if (display_id == 0) {
798+
return false;
799+
}
792800
x = (int) SDL_WINDOWPOS_CENTERED_DISPLAY(display_id);
793801
y = (int) SDL_WINDOWPOS_CENTERED_DISPLAY(display_id);
794802
}
795803
if (SDL_SetWindowPosition(s->window, x, y)) {
796-
return;
804+
return true;
797805
}
798806
const bool is_wayland =
799807
strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0;
@@ -802,11 +810,11 @@ sdl3_set_window_position(struct state_sdl3 *s) {
802810
"In Wayland, display specification is possible only with "
803811
"fullscreen flag ':fs' (%s)\n",
804812
SDL_GetError());
805-
return;
813+
} else {
814+
MSG(ERROR, "Error (SDL_SetWindowPosition): %s\n",
815+
SDL_GetError());
806816
}
807-
const int ll = !is_wayland ? LOG_LEVEL_ERROR : LOG_LEVEL_VERBOSE;
808-
log_msg(ll, MOD_NAME "Error (SDL_SetWindowPosition): %s\n",
809-
SDL_GetError());
817+
return false;
810818
}
811819

812820
static bool
@@ -839,7 +847,10 @@ display_sdl3_reconfigure_real(void *state, struct video_desc desc)
839847
MSG(ERROR, "Unable to create window: %s\n", SDL_GetError());
840848
return false;
841849
}
842-
sdl3_set_window_position(s);
850+
;
851+
if (!sdl3_set_window_position(s)) {
852+
return false;
853+
}
843854

844855
if (s->renderer) {
845856
SDL_DestroyRenderer(s->renderer);
@@ -893,14 +904,14 @@ display_sdl3_reconfigure_real(void *state, struct video_desc desc)
893904
return true;
894905
}
895906

896-
static void
907+
static bool
897908
loadSplashscreen(struct state_sdl3 *s)
898909
{
899910
struct video_frame *frame = get_splashscreen();
900911
if (!display_sdl3_reconfigure_real(s, video_desc_from_frame(frame))) {
901912
MSG(WARNING, "Cannot render splashscreeen!\n");
902913
vf_free(frame);
903-
return;
914+
return false;
904915
}
905916
struct video_frame *splash = display_sdl3_getf(s);
906917
memcpy(splash->tiles[0].data, frame->tiles[0].data,
@@ -909,6 +920,7 @@ loadSplashscreen(struct state_sdl3 *s)
909920
display_frame(s, splash); // don't be tempted to use _putf() - it will
910921
// use event queue and there may arise a
911922
// race-condition with recv thread
923+
return true;
912924
}
913925

914926
static bool
@@ -1113,7 +1125,10 @@ display_sdl3_init(struct module *parent, const char *fmt, unsigned int flags)
11131125
keybindings[i].description);
11141126
}
11151127

1116-
loadSplashscreen(s);
1128+
if (!loadSplashscreen(s)) {
1129+
display_sdl3_done(s);
1130+
return NULL;
1131+
}
11171132

11181133
log_msg(LOG_LEVEL_NOTICE, "SDL3 initialized successfully.\n");
11191134

src/video_display/vulkan/vulkan_sdl3.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -727,24 +727,32 @@ get_display_id_from_idx(int idx)
727727
return 0;
728728
}
729729

730-
void
730+
bool
731731
vulkan_sdl3_set_window_position(state_vulkan_sdl3 *s,
732732
const command_line_arguments *args)
733733
{
734734
if (args->display_idx == -1 && args->x == SDL_WINDOWPOS_UNDEFINED &&
735735
args->y == SDL_WINDOWPOS_UNDEFINED) {
736-
return;
736+
return true; // nothing to set
737737
}
738738
int x = args->x;
739739
int y = args->y;
740740
if (args->display_idx != -1) {
741+
if (x != SDL_WINDOWPOS_UNDEFINED || y != SDL_WINDOWPOS_UNDEFINED) {
742+
MSG(ERROR, "Do not set window positon and display at "
743+
"the same time!\n");
744+
return false;
745+
}
741746
const SDL_DisplayID display_id =
742747
get_display_id_from_idx(args->display_idx);
748+
if (display_id == 0) {
749+
return false;
750+
}
743751
x = SDL_WINDOWPOS_CENTERED_DISPLAY(display_id);
744752
y = SDL_WINDOWPOS_CENTERED_DISPLAY(display_id);
745753
}
746754
if (SDL_SetWindowPosition(s->window, x, y)) {
747-
return;
755+
return true;
748756
}
749757
const bool is_wayland =
750758
strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0;
@@ -753,11 +761,11 @@ vulkan_sdl3_set_window_position(state_vulkan_sdl3 *s,
753761
"In Wayland, display specification is possible only with "
754762
"fullscreen flag ':fs' (%s)\n",
755763
SDL_GetError());
756-
return;
764+
} else {
765+
MSG(ERROR, "Error (SDL_SetWindowPosition): %s\n",
766+
SDL_GetError());
757767
}
758-
const int ll = !is_wayland ? LOG_LEVEL_ERROR : LOG_LEVEL_VERBOSE;
759-
log_msg(ll, MOD_NAME "Error (SDL_SetWindowPosition): %s\n",
760-
SDL_GetError());
768+
return false;
761769
}
762770

763771
void* display_vulkan_init(module* parent, const char* fmt, unsigned int flags) {
@@ -840,7 +848,9 @@ void* display_vulkan_init(module* parent, const char* fmt, unsigned int flags) {
840848
return nullptr;
841849
}
842850
s->window_callback = new WindowCallback(s->window);
843-
vulkan_sdl3_set_window_position(s.get(), &args);
851+
if (!vulkan_sdl3_set_window_position(s.get(), &args)) {
852+
return nullptr;
853+
}
844854

845855
uint32_t extension_count = 0;
846856
const char *const *extensions = SDL_Vulkan_GetInstanceExtensions(&extension_count);

0 commit comments

Comments
 (0)