Make sure a window is valid for a subsystem before using it in a messagebox
authorSam Lantinga <slouken@libsdl.org>
Sun, 14 Jul 2013 11:41:57 -0700
changeset 7453 91c3bb321036
parent 7452 fae80e5464e2
child 7454 05595e5359af
Make sure a window is valid for a subsystem before using it in a messagebox
src/video/SDL_video.c
--- a/src/video/SDL_video.c	Sun Jul 14 11:11:42 2013 -0700
+++ b/src/video/SDL_video.c	Sun Jul 14 11:41:57 2013 -0700
@@ -3052,6 +3052,20 @@
 #include "x11/SDL_x11messagebox.h"
 #endif
 
+static SDL_bool SDL_MessageboxValidForDriver(const SDL_MessageBoxData *messageboxdata, SDL_SYSWM_TYPE drivertype)
+{
+    SDL_SysWMinfo info;
+    SDL_Window *window = messageboxdata->window;
+
+    if (!window) {
+        return SDL_TRUE;
+    }
+
+    SDL_VERSION(&info.version);
+    SDL_GetWindowWMInfo(window, &info);
+    return (info.subsystem == drivertype);
+}
+
 int
 SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
 {
@@ -3071,35 +3085,42 @@
     if (!buttonid) {
         buttonid = &dummybutton;
     }
+
     if (_this && _this->ShowMessageBox) {
         retval = _this->ShowMessageBox(_this, messageboxdata, buttonid);
-    } else {
-        /* It's completely fine to call this function before video is initialized */
-        if (messageboxdata->window == NULL) {
+    }
+
+    /* It's completely fine to call this function before video is initialized */
 #if SDL_VIDEO_DRIVER_WINDOWS
-            if ((retval == -1) && (WIN_ShowMessageBox(messageboxdata, buttonid) == 0)) {
-                retval = 0;
-            }
+    if (retval == -1 &&
+        SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_WINDOWS) &&
+        WIN_ShowMessageBox(messageboxdata, buttonid) == 0) {
+        retval = 0;
+    }
 #endif
 #if SDL_VIDEO_DRIVER_COCOA
-            if ((retval == -1) && (Cocoa_ShowMessageBox(messageboxdata, buttonid) == 0)) {
-                retval = 0;
-            }
+    if (retval == -1 &&
+        SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_COCOA) &&
+        Cocoa_ShowMessageBox(messageboxdata, buttonid) == 0) {
+        retval = 0;
+    }
 #endif
 #if SDL_VIDEO_DRIVER_UIKIT
-            if ((retval == -1) && (UIKit_ShowMessageBox(messageboxdata, buttonid) == 0)) {
-                retval = 0;
-            }
+    if (retval == -1 &&
+        SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_UIKIT) &&
+        UIKit_ShowMessageBox(messageboxdata, buttonid) == 0) {
+        retval = 0;
+    }
 #endif
 #if SDL_VIDEO_DRIVER_X11
-            if ((retval == -1) && (X11_ShowMessageBox(messageboxdata, buttonid) == 0)) {
-                retval = 0;
-            }
+    if (retval == -1 &&
+        SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_X11) &&
+        X11_ShowMessageBox(messageboxdata, buttonid) == 0) {
+        retval = 0;
+    }
 #endif
-        }
-        if (retval == -1) {
-            SDL_SetError("No message system available");
-        }
+    if (retval == -1) {
+        SDL_SetError("No message system available");
     }
 
     SDL_ShowCursor(show_cursor_prev);