From cd1a7cbe630e4c7a7ffbfd50c5245db5612feb6f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= jaakko.keranen@iki.fi
Date: Tue, 3 Jan 2023 12:16:18 +0200
Subject: [PATCH 1/1] GTK: Request dark window theme via X11 window property
IssueID #549
CMakeLists.txt | 10 ++++++++
Depends.cmake | 16 ++++++++++---
res/about/version.gmi | 1 +
src/app.c | 6 +++++
src/x11.c | 55 +++++++++++++++++++++++++++++++++++++++++++
src/x11.h | 30 +++++++++++++++++++++++
6 files changed, 115 insertions(+), 3 deletions(-)
create mode 100644 src/x11.c
create mode 100644 src/x11.h
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)
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)
+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)
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 ()
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:
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)
#endif
+#if defined (LAGRANGE_ENABLE_X11_XLIB)
+# include "x11.h"
+#endif
#if SDL_VERSION_ATLEAST(2, 0, 14)
#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 jaakko.keranen@iki.fi
+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
+2. Redistributions in binary form must reproduce the above copyright notice,
+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 <SDL_syswm.h>
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+void setDarkWindowTheme_SDLWindow(SDL_Window *d, iBool setDark) {
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) {
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 jaakko.keranen@iki.fi
+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
+2. Redistributions in binary form must reproduce the above copyright notice,
+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 <the_Foundation/defs.h>
+#include <SDL_video.h>
+void setDarkWindowTheme_SDLWindow (SDL_Window *, iBool setDark);
+void handleCommand_X11 (const char *cmd);
--
2.25.1
text/plain
This content has been proxied by September (ba2dc).