X11: Force the window focus during ShowWindow if there's no window manager.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 29 May 2015 15:21:47 -0400
changeset 9683 aa4e4768c6c1
parent 9682 0265666d0238
child 9684 c7db25205399
X11: Force the window focus during ShowWindow if there's no window manager. Fixes Bugzilla #2997.
src/video/x11/SDL_x11window.c
--- a/src/video/x11/SDL_x11window.c	Thu May 28 19:06:07 2015 -0700
+++ b/src/video/x11/SDL_x11window.c	Fri May 29 15:21:47 2015 -0400
@@ -884,6 +884,24 @@
     X11_XCheckIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
 }
 
+static SDL_bool
+X11_HasWindowManager(const SDL_WindowData *data)
+{
+    const SDL_DisplayData *displaydata =
+        (SDL_DisplayData *) SDL_GetDisplayForWindow(data->window)->driverdata;
+    Display *display = data->videodata->display;
+    const int screen  = displaydata->screen;
+    char atomname[16];
+    Atom atom;
+
+    /* Compliments to Chromium for this technique.
+        Window Managers are supposed to own "WM_Sx" selections, where
+        "x" is the screen number (ICCCM 2.8). */
+    SDL_snprintf(atomname, sizeof (atomname), "WM_S%d", screen);
+    atom = X11_XInternAtom(display, atomname, True);
+    return ((atom != None) && (X11_XGetSelectionOwner(display, atom) != None));
+}
+
 void
 X11_ShowWindow(_THIS, SDL_Window * window)
 {
@@ -899,6 +917,12 @@
         X11_XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
         X11_XFlush(display);
     }
+
+    if (!X11_HasWindowManager(data)) {
+        /* no WM means no FocusIn event, which confuses us. Force it. */
+        X11_XSetInputFocus(display, data->xwindow, RevertToNone, CurrentTime);
+        X11_XFlush(display);
+    }
 }
 
 void