Lagrange [work/v1.15]

GTK: Request dark window theme via X11 window property

=> cd1a7cbe630e4c7a7ffbfd50c5245db5612feb6f

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ebc3e263..672fefd4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -66,6 +66,7 @@ option (ENABLE_TUI              "Enable the Curses TUI instead of GUI" OFF)
 option (ENABLE_WEBP             "Use libwebp to decode .webp images (via pkg-config)" ON)
 option (ENABLE_WINDOWPOS_FIX    "Set position after showing window (workaround for SDL bug)" OFF)
 option (ENABLE_WINSPARKLE       "Use WinSparkle for automatic updates (Windows)" OFF)
+option (ENABLE_X11_XLIB         "Use Xlib directly, if available (X11)" ON)
 option (ENABLE_X11_SWRENDER     "Use software rendering (X11)" OFF)
 set    (CACERT_PEM_PATH         "" CACHE FILEPATH "Root CA cacert.pem file to include as a built-in resource")
 
@@ -303,6 +304,12 @@ if (ENABLE_IPC)
         src/ipc.h
     )
 endif ()
+if (ENABLE_X11_XLIB AND XLIB_FOUND)
+    list (APPEND SOURCES
+        src/x11.c
+        src/x11.h
+    )
+endif ()
 if (ANDROID)
     set (MOBILE 1)
     add_definitions (-DiPlatformAndroidMobile=1)
@@ -432,6 +439,9 @@ endif ()
 if (ENABLE_WINDOWPOS_FIX)
     target_compile_definitions (app PUBLIC LAGRANGE_ENABLE_WINDOWPOS_FIX=1)
 endif ()
+if (TARGET x11-lib)
+    target_link_libraries (app PUBLIC x11-lib)
+endif ()
 if (ENABLE_X11_SWRENDER)
     target_compile_definitions (app PUBLIC LAGRANGE_ENABLE_X11_SWRENDER=1)
 endif ()
diff --git a/Depends.cmake b/Depends.cmake
index 0ffaef90..debd0410 100644
--- a/Depends.cmake
+++ b/Depends.cmake
@@ -1,4 +1,4 @@
-find_package (PkgConfig)
+find_package (PkgConfig REQUIRED)
 
 if (IOS)
     include (Depends-iOS.cmake)
@@ -10,7 +10,6 @@ if (ANDROID)
     return ()
 endif ()
 
-find_package (PkgConfig)
 find_program (MESON_EXECUTABLE meson DOC "Meson build system")
 find_program (NINJA_EXECUTABLE ninja DOC "Ninja build tool")
 include (ExternalProject)
@@ -192,7 +191,18 @@ if (ENABLE_WINSPARKLE)
     message (STATUS "Using WinSparkle: ${WINSPARKLE_DLL}")
 endif ()
 
-find_package (PkgConfig REQUIRED)
+if (ENABLE_X11_XLIB AND NOT ENABLE_TUI)
+    find_package (X11)
+    if (X11_FOUND)
+        set (XLIB_FOUND YES)
+        add_library (x11-lib INTERFACE)
+        target_link_libraries (x11-lib INTERFACE ${X11_LIBRARIES})
+        target_include_directories (x11-lib INTERFACE ${X11_INCLUDE_DIRS})
+        target_compile_definitions (x11-lib INTERFACE LAGRANGE_ENABLE_X11_XLIB=1)
+        message (STATUS "Using Xlib: ${X11_LIBRARIES}")
+    endif ()
+endif ()
+
 if (ENABLE_TUI)
     pkg_check_modules (SDL2 REQUIRED sealcurses)
 else ()
diff --git a/res/about/version.gmi b/res/about/version.gmi
index 207d90f2..62f3aa5f 100644
--- a/res/about/version.gmi
+++ b/res/about/version.gmi
@@ -11,6 +11,7 @@ New features:
 * Indicate that tab autoreloading is enabled by changing the appearance of the navbar reload button.
 
 Changes and enhancements:
+* GTK: Request dark window theme if the app UI color theme is dark.
 
 Fixes:
 
diff --git a/src/app.c b/src/app.c
index fded9721..9697a7c5 100644
--- a/src/app.c
+++ b/src/app.c
@@ -84,6 +84,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 #if defined (iPlatformMsys)
 #   include "win32.h"
 #endif
+#if defined (LAGRANGE_ENABLE_X11_XLIB)
+#   include "x11.h"
+#endif
 #if SDL_VERSION_ATLEAST(2, 0, 14)
 #   include 
 #endif
@@ -2016,6 +2019,9 @@ void processEvents_App(enum iAppEventMode eventMode) {
 #endif
 #if defined (iPlatformMsys)
                     handleCommand_Win32(command_UserEvent(&ev));
+#endif
+#if defined (LAGRANGE_ENABLE_X11_XLIB)
+                    handleCommand_X11(command_UserEvent(&ev));
 #endif
                     if (isMetricsChange_UserEvent(&ev)) {
                         listWindows_App_(d, &windows);
diff --git a/src/x11.c b/src/x11.c
new file mode 100644
index 00000000..e7e0d738
--- /dev/null
+++ b/src/x11.c
@@ -0,0 +1,55 @@
+/* Copyright 2023 Jaakko Keränen 
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+   list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+   this list of conditions and the following disclaimer in the documentation
+   and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
+
+#include "x11.h"
+#include "ui/command.h"
+#include "ui/window.h"
+#include "prefs.h"
+#include "app.h"
+
+#include 
+#include 
+#include 
+
+void setDarkWindowTheme_SDLWindow(SDL_Window *d, iBool setDark) {
+    SDL_SysWMinfo wmInfo;
+    SDL_VERSION(&wmInfo.version);
+    if (SDL_GetWindowWMInfo(d, &wmInfo)) {
+        Display *   dpy   = wmInfo.info.x11.display;
+        Window      wnd   = wmInfo.info.x11.window;
+        Atom        prop  = XInternAtom(dpy, "_GTK_THEME_VARIANT", False);        
+        const char *value = setDark ? "dark" : "light";
+        XChangeProperty(dpy, wnd, prop, XA_STRING, 8, PropModeReplace,
+                        (unsigned char *) value, strlen(value));
+    }
+}
+
+void handleCommand_X11(const char *cmd) {
+    if (equal_Command(cmd, "theme.changed")) {        
+        iConstForEach(PtrArray, iter, mainWindows_App()) {
+            iMainWindow *mw = iter.ptr;
+            setDarkWindowTheme_SDLWindow(
+                mw->base.win, isDark_ColorTheme(prefs_App()->theme));
+        }
+    }
+}
+
diff --git a/src/x11.h b/src/x11.h
new file mode 100644
index 00000000..2eb195ad
--- /dev/null
+++ b/src/x11.h
@@ -0,0 +1,30 @@
+/* Copyright 2023 Jaakko Keränen 
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+   list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+   this list of conditions and the following disclaimer in the documentation
+   and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
+
+#pragma once
+
+#include 
+#include 
+
+void    setDarkWindowTheme_SDLWindow    (SDL_Window *, iBool setDark);
+void    handleCommand_X11               (const char *cmd);
+
Proxy Information
Original URL
gemini://git.skyjake.fi/lagrange/work%2Fv1.15/cdiff/cd1a7cbe630e4c7a7ffbfd50c5245db5612feb6f
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
91.413845 milliseconds
Gemini-to-HTML Time
0.501242 milliseconds

This content has been proxied by September (ba2dc).