Added SDL_WINDOWEVENT_TAKE_FOCUS.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 05 Jan 2016 02:27:50 -0500
changeset 10029 4c992fd19316
parent 10028 da668b612d10
child 10030 061b1b467bd3
Added SDL_WINDOWEVENT_TAKE_FOCUS. This is for corner cases where a multi-window app is activated and wants to make a decision about where focus should go. This patch came from Unreal Engine 4's fork of SDL, compliments of Epic Games.
include/SDL_video.h
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11video.h
src/video/x11/SDL_x11window.c
--- a/include/SDL_video.h	Tue Apr 21 10:10:59 2015 -0400
+++ b/include/SDL_video.h	Tue Jan 05 02:27:50 2016 -0500
@@ -161,7 +161,8 @@
     SDL_WINDOWEVENT_FOCUS_GAINED,   /**< Window has gained keyboard focus */
     SDL_WINDOWEVENT_FOCUS_LOST,     /**< Window has lost keyboard focus */
     SDL_WINDOWEVENT_CLOSE,          /**< The window manager requests that the window be closed */
-    SDL_WINDOWEVENT_HIT_TEST        /** Window had a hit test that wasn't SDL_HITTEST_NORMAL. */
+    SDL_WINDOWEVENT_TAKE_FOCUS,     /**< Window is being offered a focus (should SetWindowInputFocus() on itself or a subwindow, or ignore) */
+    SDL_WINDOWEVENT_HIT_TEST        /**< Window had a hit test that wasn't SDL_HITTEST_NORMAL. */
 } SDL_WindowEventID;
 
 /**
--- a/src/video/x11/SDL_x11events.c	Tue Apr 21 10:10:59 2015 -0400
+++ b/src/video/x11/SDL_x11events.c	Tue Jan 05 02:27:50 2016 -0500
@@ -979,6 +979,16 @@
                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
                 break;
             }
+            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
+                (xevent.xclient.format == 32) &&
+                (xevent.xclient.data.l[0] == videodata->WM_TAKE_FOCUS)) {
+
+#ifdef DEBUG_XEVENTS
+                printf("window %p: WM_TAKE_FOCUS\n", data);
+#endif
+                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_TAKE_FOCUS, 0, 0);
+                break;
+            }
         }
         break;
 
--- a/src/video/x11/SDL_x11video.c	Tue Apr 21 10:10:59 2015 -0400
+++ b/src/video/x11/SDL_x11video.c	Tue Jan 05 02:27:50 2016 -0500
@@ -395,6 +395,7 @@
 #define GET_ATOM(X) data->X = X11_XInternAtom(data->display, #X, False)
     GET_ATOM(WM_PROTOCOLS);
     GET_ATOM(WM_DELETE_WINDOW);
+    GET_ATOM(WM_TAKE_FOCUS);
     GET_ATOM(_NET_WM_STATE);
     GET_ATOM(_NET_WM_STATE_HIDDEN);
     GET_ATOM(_NET_WM_STATE_FOCUSED);
--- a/src/video/x11/SDL_x11video.h	Tue Apr 21 10:10:59 2015 -0400
+++ b/src/video/x11/SDL_x11video.h	Tue Jan 05 02:27:50 2016 -0500
@@ -89,6 +89,7 @@
     /* Useful atoms */
     Atom WM_PROTOCOLS;
     Atom WM_DELETE_WINDOW;
+    Atom WM_TAKE_FOCUS;
     Atom _NET_WM_STATE;
     Atom _NET_WM_STATE_HIDDEN;
     Atom _NET_WM_STATE_FOCUSED;
--- a/src/video/x11/SDL_x11window.c	Tue Apr 21 10:10:59 2015 -0400
+++ b/src/video/x11/SDL_x11window.c	Tue Jan 05 02:27:50 2016 -0500
@@ -574,18 +574,17 @@
                     (unsigned char *)&compositor, 1);
 
     {
-        Atom protocols[2];
+        Atom protocols[3];
         int proto_count = 0;
         const char *ping_hint;
 
-        protocols[proto_count] = data->WM_DELETE_WINDOW; /* Allow window to be deleted by the WM */
-        proto_count++;
-        
+        protocols[proto_count++] = data->WM_DELETE_WINDOW; /* Allow window to be deleted by the WM */
+        protocols[proto_count++] = data->WM_TAKE_FOCUS; /* Since we will want to set input focus explicitly */
+
         ping_hint = SDL_GetHint(SDL_HINT_VIDEO_X11_NET_WM_PING);
         /* Default to using ping if there is no hint */
         if (!ping_hint || SDL_atoi(ping_hint)) {
-            protocols[proto_count] = data->_NET_WM_PING; /* Respond so WM knows we're alive */
-            proto_count++;
+            protocols[proto_count++] = data->_NET_WM_PING; /* Respond so WM knows we're alive */
         }
 
         SDL_assert(proto_count <= sizeof(protocols) / sizeof(protocols[0]));