Implemented X11 fullscreen toggling with modern window managers
authorSam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 17:23:02 -0800
changeset 5302 b1b9ec9874ba
parent 5301 248c5c7d6e29
child 5303 d4074e52e739
Implemented X11 fullscreen toggling with modern window managers
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11window.c
src/video/x11/SDL_x11window.h
--- a/src/video/x11/SDL_x11video.c	Tue Feb 15 16:41:04 2011 -0800
+++ b/src/video/x11/SDL_x11video.c	Tue Feb 15 17:23:02 2011 -0800
@@ -201,6 +201,7 @@
     device->MaximizeWindow = X11_MaximizeWindow;
     device->MinimizeWindow = X11_MinimizeWindow;
     device->RestoreWindow = X11_RestoreWindow;
+    device->SetWindowFullscreen = X11_SetWindowFullscreen;
     device->SetWindowGrab = X11_SetWindowGrab;
     device->DestroyWindow = X11_DestroyWindow;
     device->CreateWindowFramebuffer = X11_CreateWindowFramebuffer;
--- a/src/video/x11/SDL_x11window.c	Tue Feb 15 16:41:04 2011 -0800
+++ b/src/video/x11/SDL_x11window.c	Tue Feb 15 17:23:02 2011 -0800
@@ -772,7 +772,7 @@
 }
 
 static void
-X11_SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
+SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
 {
     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
     SDL_DisplayData *displaydata =
@@ -823,7 +823,7 @@
 void
 X11_MaximizeWindow(_THIS, SDL_Window * window)
 {
-    X11_SetWindowMaximized(_this, window, SDL_TRUE);
+    SetWindowMaximized(_this, window, SDL_TRUE);
 }
 
 void
@@ -841,10 +841,68 @@
 void
 X11_RestoreWindow(_THIS, SDL_Window * window)
 {
-    X11_SetWindowMaximized(_this, window, SDL_FALSE);
+    SetWindowMaximized(_this, window, SDL_FALSE);
     X11_ShowWindow(_this, window);
 }
 
+static void
+SetWindowFullscreen(_THIS, SDL_Window * window, SDL_bool fullscreen)
+{
+    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+    SDL_DisplayData *displaydata =
+        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
+    Display *display = data->videodata->display;
+    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
+    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
+    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
+    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
+
+    if (X11_IsWindowMapped(_this, window)) {
+        XEvent e;
+
+        SDL_zero(e);
+        e.xany.type = ClientMessage;
+        e.xclient.message_type = _NET_WM_STATE;
+        e.xclient.format = 32;
+        e.xclient.window = data->xwindow;
+        e.xclient.data.l[0] =
+            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
+        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
+        e.xclient.data.l[3] = 0l;
+
+        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
+                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
+    } else {
+        int count = 0;
+        Atom atoms[3];
+
+        if (fullscreen) {
+            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
+        }
+        if (window->flags & SDL_WINDOW_MAXIMIZED) {
+            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
+            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
+        }
+        if (count > 0) {
+            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
+                            PropModeReplace, (unsigned char *)atoms, count);
+        } else {
+            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
+        }
+    }
+    XFlush(display);
+}
+
+void
+X11_SetWindowFullscreen(_THIS, SDL_Window * window)
+{
+    if (FULLSCREEN_VISIBLE(window)) {
+        SetWindowFullscreen(_this, window, SDL_TRUE);
+    } else {
+        SetWindowFullscreen(_this, window, SDL_FALSE);
+    }
+}
+
 void
 X11_SetWindowGrab(_THIS, SDL_Window * window)
 {
--- a/src/video/x11/SDL_x11window.h	Tue Feb 15 16:41:04 2011 -0800
+++ b/src/video/x11/SDL_x11window.h	Tue Feb 15 17:23:02 2011 -0800
@@ -54,6 +54,7 @@
 extern void X11_MaximizeWindow(_THIS, SDL_Window * window);
 extern void X11_MinimizeWindow(_THIS, SDL_Window * window);
 extern void X11_RestoreWindow(_THIS, SDL_Window * window);
+extern void X11_SetWindowFullscreen(_THIS, SDL_Window * window);
 extern void X11_SetWindowGrab(_THIS, SDL_Window * window);
 extern void X11_DestroyWindow(_THIS, SDL_Window * window);
 extern SDL_bool X11_GetWindowWMInfo(_THIS, SDL_Window * window,