JBR-6372: implement detecting of OS theme on linux

This commit is contained in:
Dmitrii Morskii
2023-12-12 16:34:57 +01:00
parent 48dee1515d
commit 51d67613bb
7 changed files with 712 additions and 0 deletions

View File

@@ -174,6 +174,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBAWT, \
DISABLED_WARNINGS_clang_debug_trace.c := format-nonliteral, \
DISABLED_WARNINGS_clang_Trace.c := format-nonliteral, \
DISABLED_WARNINGS_clang_TransformHelper.c := sign-compare, \
DISABLED_WARNINGS_clang_system_properties.c := format-nonliteral, \
DISABLED_WARNINGS_microsoft := 4244 4996, \
DISABLED_WARNINGS_microsoft_awt_Toolkit.cpp := 4267, \
LDFLAGS := $(LDFLAGS_JDKLIB) $(call SET_SHARED_LIBRARY_ORIGIN), \
@@ -1113,6 +1114,7 @@ ifeq ($(call isTargetOs, macosx), true)
DISABLED_WARNINGS_clang_OGLPaints.c := format-nonliteral, \
DISABLED_WARNINGS_clang_PrinterView.m := enum-conversion, \
DISABLED_WARNINGS_clang_SystemHotkey.m := format-nonliteral, \
DISABLED_WARNINGS_clang_system_properties.c := format-nonliteral, \
LDFLAGS := $(LDFLAGS_JDKLIB) \
$(call SET_SHARED_LIBRARY_ORIGIN) \
-L$(INSTALL_LIBRARIES_HERE), \

View File

@@ -25,6 +25,7 @@
package sun.awt;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT;
@@ -51,11 +52,13 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import jdk.internal.misc.InnocuousThread;
import sun.awt.X11.XBaseWindow;
import sun.security.action.GetIntegerAction;
import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection;
import sun.java2d.opengl.OGLRenderQueue;
import sun.security.action.GetPropertyAction;
import sun.util.logging.PlatformLogger;
public abstract class UNIXToolkit extends SunToolkit
{
@@ -108,6 +111,18 @@ public abstract class UNIXToolkit extends SunToolkit
private Boolean nativeGTKAvailable;
private Boolean nativeGTKLoaded;
private BufferedImage tmpImage = null;
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.UNIXToolkit");
private static void printError(String str) {
log.fine(str);
}
private static native void toolkitInit();
protected UNIXToolkit() {
toolkitInit();
initSystemPropertyWatcher();
}
public static int getDatatransferTimeout() {
@SuppressWarnings("removal")
@@ -478,6 +493,8 @@ public abstract class UNIXToolkit extends SunToolkit
return result;
}
private static native int isSystemDarkColorScheme();
@Override
public boolean isRunningOnXWayland() {
return isOnXWayland();
@@ -496,6 +513,35 @@ public abstract class UNIXToolkit extends SunToolkit
// application icons).
private static final WindowFocusListener waylandWindowFocusListener;
private static final String OS_THEME_IS_DARK = "awt.os.theme.isDark";
private static Thread systemPropertyWatcher = null;
private void initSystemPropertyWatcher() {
int initialSystemDarkColorScheme = isSystemDarkColorScheme();
if (initialSystemDarkColorScheme >= 0) {
setDesktopProperty(OS_THEME_IS_DARK, initialSystemDarkColorScheme != 0);
systemPropertyWatcher = InnocuousThread.newThread("SystemPropertyWatcher",
() -> {
while (true) {
try {
int isSystemDarkColorScheme = isSystemDarkColorScheme();
if (isSystemDarkColorScheme >= 0) {
setDesktopProperty(OS_THEME_IS_DARK, isSystemDarkColorScheme != 0);
}
Thread.sleep(1000);
} catch (Exception ignored) {
}
}
});
systemPropertyWatcher.setDaemon(true);
systemPropertyWatcher.start();
}
}
static {
if (isOnXWayland()) {
waylandWindowFocusListener = new WindowAdapter() {

View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, JetBrains s.r.o.. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "dbus_interface.h"
#include <dlfcn.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
#include "jvm_md.h"
#define DBUS_LIB JNI_LIB_NAME("dbus-1")
#define DBUS_LIB_VERSIONED VERSIONED_JNI_LIB_NAME("dbus-1", "3")
static bool isCurrentVersionSupported(DBusApi* dBusApi) {
int major = 0, minor = 0, micro = 0;
dBusApi->dbus_get_version(&major, &minor, &micro);
return major == 1;
}
static bool DBusApi_init(DBusApi* dBusApi, void *libhandle) {
dBusApi->dbus_get_version = dlsym(libhandle, "dbus_get_version");
if (dBusApi->dbus_get_version == NULL || !isCurrentVersionSupported(dBusApi)) {
return false;
}
dBusApi->dbus_error_init = dlsym(libhandle, "dbus_error_init");
dBusApi->dbus_bus_get = dlsym(libhandle, "dbus_bus_get");
dBusApi->dbus_error_is_set = dlsym(libhandle, "dbus_error_is_set");
dBusApi->dbus_bus_request_name = dlsym(libhandle, "dbus_bus_request_name");
dBusApi->dbus_bus_add_match = dlsym(libhandle, "dbus_bus_add_match");
dBusApi->dbus_connection_add_filter = dlsym(libhandle, "dbus_connection_add_filter");
dBusApi->dbus_connection_flush = dlsym(libhandle, "dbus_connection_flush");
dBusApi->dbus_connection_read_write = dlsym(libhandle, "dbus_connection_read_write");
dBusApi->dbus_connection_dispatch = dlsym(libhandle, "dbus_connection_dispatch");
dBusApi->dbus_message_is_signal = dlsym(libhandle, "dbus_message_is_signal");
dBusApi->dbus_message_new_method_call = dlsym(libhandle, "dbus_message_new_method_call");
dBusApi->dbus_message_set_destination = dlsym(libhandle, "dbus_message_set_destination");
dBusApi->dbus_message_iter_init_append = dlsym(libhandle, "dbus_message_iter_init_append");
dBusApi->dbus_message_iter_append_basic = dlsym(libhandle, "dbus_message_iter_append_basic");
dBusApi->dbus_connection_send_with_reply_and_block = dlsym(libhandle, "dbus_connection_send_with_reply_and_block");
dBusApi->dbus_message_iter_init = dlsym(libhandle, "dbus_message_iter_init");
dBusApi->dbus_message_iter_get_arg_type = dlsym(libhandle, "dbus_message_iter_get_arg_type");
dBusApi->dbus_message_iter_get_basic = dlsym(libhandle, "dbus_message_iter_get_basic");
dBusApi->dbus_message_iter_recurse = dlsym(libhandle, "dbus_message_iter_recurse");
dBusApi->dbus_message_iter_next = dlsym(libhandle, "dbus_message_iter_next");
dBusApi->dbus_message_unref = dlsym(libhandle, "dbus_message_unref");
dBusApi->dbus_message_set_auto_start = dlsym(libhandle, "dbus_message_set_auto_start");
return dBusApi->dbus_error_init != NULL && dBusApi->dbus_bus_get != NULL && dBusApi->dbus_error_is_set != NULL &&
dBusApi->dbus_bus_request_name != NULL && dBusApi->dbus_bus_add_match != NULL &&
dBusApi->dbus_connection_add_filter != NULL && dBusApi->dbus_connection_flush != NULL &&
dBusApi->dbus_connection_read_write != NULL && dBusApi->dbus_connection_dispatch != NULL &&
dBusApi->dbus_message_is_signal != NULL && dBusApi->dbus_message_new_method_call != NULL &&
dBusApi->dbus_message_set_destination != NULL && dBusApi->dbus_message_iter_init_append != NULL &&
dBusApi->dbus_message_iter_append_basic != NULL && dBusApi->dbus_connection_send_with_reply_and_block != NULL &&
dBusApi->dbus_message_iter_init != NULL && dBusApi->dbus_message_iter_get_arg_type != NULL &&
dBusApi->dbus_message_iter_get_basic != NULL && dBusApi->dbus_message_iter_recurse != NULL &&
dBusApi->dbus_message_iter_next != NULL && dBusApi->dbus_message_unref != NULL &&
dBusApi->dbus_message_set_auto_start != NULL;
}
DBusApi* DBusApi_setupDBus(void *libhandle) {
DBusApi *dBusApi = (DBusApi*)malloc(sizeof(DBusApi));
if (dBusApi == NULL || !DBusApi_init(dBusApi, libhandle)) {
free(dBusApi);
dBusApi = NULL;
}
return dBusApi;
}
DBusApi* DBusApi_setupDBusDefault() {
void *dbus_libhandle = dlopen(DBUS_LIB, RTLD_LAZY | RTLD_LOCAL);
if (dbus_libhandle == NULL) {
dbus_libhandle = dlopen(DBUS_LIB_VERSIONED, RTLD_LAZY | RTLD_LOCAL);
if (dbus_libhandle == NULL) {
return NULL;
}
}
return DBusApi_setupDBus(dbus_libhandle);
}

View File

@@ -0,0 +1,179 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, JetBrains s.r.o.. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#ifndef JETBRAINSRUNTIME_DBUS_INTERFACE_H
#define JETBRAINSRUNTIME_DBUS_INTERFACE_H
#define DBUS_NAME_FLAG_ALLOW_REPLACEMENT 0x1
#define DBUS_NAME_FLAG_REPLACE_EXISTING 0x2
#define DBUS_NAME_FLAG_DO_NOT_QUEUE 0x4
#define DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER 1
#define DBUS_REQUEST_NAME_REPLY_IN_QUEUE 2
#define DBUS_REQUEST_NAME_REPLY_EXISTS 3
#define DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER 4
typedef enum
{
DBUS_BUS_SESSION, /**< The login session bus */
DBUS_BUS_SYSTEM, /**< The systemwide bus */
DBUS_BUS_STARTER /**< The bus that started us, if any */
} DBusBusType;
typedef enum
{
DBUS_HANDLER_RESULT_HANDLED, /**< Message has had its effect - no need to run more handlers. */
DBUS_HANDLER_RESULT_NOT_YET_HANDLED, /**< Message has not had any effect - see if other handlers want it. */
DBUS_HANDLER_RESULT_NEED_MEMORY /**< Need more memory in order to return #DBUS_HANDLER_RESULT_HANDLED or #DBUS_HANDLER_RESULT_NOT_YET_HANDLED. Please try again later with more memory. */
} DBusHandlerResult;
typedef enum
{
DBUS_DISPATCH_DATA_REMAINS, /**< There is more data to potentially convert to messages. */
DBUS_DISPATCH_COMPLETE, /**< All currently available data has been processed. */
DBUS_DISPATCH_NEED_MEMORY /**< More memory is needed to continue. */
} DBusDispatchStatus;
typedef struct DBusConnection DBusConnection;
typedef struct DBusMessage DBusMessage;
typedef DBusHandlerResult (*DBusHandleMessageFunction) (DBusConnection *connection,
DBusMessage *message,
void *user_data);
typedef void (*DBusFreeFunction) (void *memory);
typedef struct DBusError DBusError;
struct DBusError
{
const char *name;
const char *message;
unsigned int dummy1 : 1;
unsigned int dummy2 : 1;
unsigned int dummy3 : 1;
unsigned int dummy4 : 1;
unsigned int dummy5 : 1;
void *padding1;
};
typedef unsigned int dbus_uint32_t;
typedef dbus_uint32_t dbus_bool_t;
typedef struct DBusMessageIter DBusMessageIter;
struct DBusMessageIter
{
void *dummy1;
void *dummy2;
dbus_uint32_t dummy3;
int dummy4;
int dummy5;
int dummy6;
int dummy7;
int dummy8;
int dummy9;
int dummy10;
int dummy11;
int pad1;
int pad2;
void *pad3;
};
#define DBUS_TYPE_INT16 ((int) 'n')
#define DBUS_TYPE_UINT16 ((int) 'q')
#define DBUS_TYPE_INT32 ((int) 'i')
#define DBUS_TYPE_UINT32 ((int) 'u')
#define DBUS_TYPE_INT64 ((int) 'x')
#define DBUS_TYPE_UINT64 ((int) 't')
#define DBUS_TYPE_DOUBLE ((int) 'd')
#define DBUS_TYPE_BYTE ((int) 'y')
#define DBUS_TYPE_BOOLEAN ((int) 'b')
#define DBUS_TYPE_STRING ((int) 's')
#define DBUS_TYPE_VARIANT ((int) 'v')
#define DBUS_TYPE_INVALID ((int) '\0')
typedef struct DBusApi {
void (*dbus_get_version)(int *major_version_p, int *minor_version_p, int *micro_version_p);
void (*dbus_error_init)(DBusError *error);
DBusConnection *(*dbus_bus_get)(DBusBusType type, DBusError *error);
dbus_bool_t (*dbus_error_is_set)(const DBusError *error);
void (*dbus_error_free)(DBusError *error);
int (*dbus_bus_request_name)(DBusConnection *connection, const char *name, unsigned int flags, DBusError *error);
void (*dbus_bus_add_match)(DBusConnection *connection, const char *rule, DBusError *error);
dbus_bool_t (*dbus_connection_add_filter)(DBusConnection *connection, DBusHandleMessageFunction function,
void *user_data, DBusFreeFunction free_data_function);
void (*dbus_connection_flush)(DBusConnection *connection);
dbus_bool_t (*dbus_connection_read_write)(DBusConnection *connection, int timeout_milliseconds);
DBusDispatchStatus (*dbus_connection_dispatch)(DBusConnection *connection);
dbus_bool_t (*dbus_message_is_signal)(DBusMessage *message, const char *iface, const char *signal_name);
DBusMessage* (*dbus_message_new_method_call)(const char *bus_name, const char *path,
const char *iface, const char *method);
dbus_bool_t (*dbus_message_set_destination)(DBusMessage *message, const char *destination);
void (*dbus_message_iter_init_append)(DBusMessage *message, DBusMessageIter *iter);
dbus_bool_t (*dbus_message_iter_append_basic)(DBusMessageIter *iter, int type, const void *value);
DBusMessage *(*dbus_connection_send_with_reply_and_block)(DBusConnection *connection, DBusMessage *message,
int timeout_milliseconds, DBusError *error);
dbus_bool_t (*dbus_message_iter_init)(DBusMessage *message, DBusMessageIter *iter);
int (*dbus_message_iter_get_arg_type)(DBusMessageIter *iter);
void (*dbus_message_iter_get_basic)(DBusMessageIter *iter, void *value);
void (*dbus_message_iter_recurse)(DBusMessageIter *iter, DBusMessageIter *sub);
dbus_bool_t (*dbus_message_iter_next)(DBusMessageIter *iter);
void (*dbus_message_unref)(DBusMessage *message);
void (*dbus_message_set_auto_start)(DBusMessage *message, dbus_bool_t auto_start);
} DBusApi;
DBusApi* DBusApi_setupDBus(void *libhandle);
DBusApi* DBusApi_setupDBusDefault();
#endif //JETBRAINSRUNTIME_DBUS_INTERFACE_H

View File

@@ -0,0 +1,238 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, JetBrains s.r.o.. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "system_properties.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define UNKNOWN_RESULT -1
#define SETTING_INTERFACE "org.freedesktop.portal.Settings"
#define SETTING_INTERFACE_METHOD "Read"
#define DESKTOP_DESTINATION "org.freedesktop.portal.Desktop"
#define DESKTOP_PATH "/org/freedesktop/portal/desktop"
#define REPLY_TIMEOUT 150
static DBusConnection *connection = NULL;
static JNIEnv *env = NULL;
static DBusApi *dBus = NULL;
static bool initialized = false;
static bool logEnabled = true;
extern JavaVM *jvm;
static void printError(const char* fmt, ...) {
if (!logEnabled) {
return;
}
env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
char* buf = (char*)malloc(1024);
if (env && buf) {
va_list vargs;
va_start(vargs, fmt);
vsnprintf(buf, 1024, fmt, vargs);
jstring text = JNU_NewStringPlatform(env, buf);
free(buf);
va_end(vargs);
jboolean ignoreException;
JNU_CallStaticMethodByName(env, &ignoreException, "sun/awt/UNIXToolkit", "printError",
"(Ljava/lang/String;)V", text);
}
}
static bool dbusCheckError(DBusError *err, const char *msg) {
bool is_error_set = dBus->dbus_error_is_set(err);
if (is_error_set) {
printError("DBus error: %s. %s\n", msg, err->message);
dBus->dbus_error_free(err);
}
return is_error_set;
}
bool SystemProperties_setup(DBusApi *dBus_, JNIEnv *env_) {
env = env_;
dBus = dBus_;
DBusError err;
int ret;
dBus->dbus_error_init(&err);
if ((connection = dBus->dbus_bus_get(DBUS_BUS_SESSION, &err)) == NULL) {
printError("DBus error: connection is Null\n");
return false;
}
if (dbusCheckError(&err, "connection error")) {
return false;
}
ret = dBus->dbus_bus_request_name(connection, "dbus.JBR.server", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER && ret != DBUS_REQUEST_NAME_REPLY_IN_QUEUE) {
printError("DBus error: Failed to acquire service name \n");
return false;
}
if (dbusCheckError(&err, "error request 'dbus.JBR.server' name on the bus")) {
return false;
}
dBus->dbus_connection_flush(connection);
initialized = true;
return true;
}
// current implementation of object decomposition supports only
// primitive types (including a recursive type wrapper)
static bool getBasicIter(void *val, DBusMessageIter *iter, int demand_type) {
int type = dBus->dbus_message_iter_get_arg_type(iter);
switch (type)
{
case DBUS_TYPE_INT16:
case DBUS_TYPE_UINT16:
case DBUS_TYPE_INT32:
case DBUS_TYPE_UINT32:
case DBUS_TYPE_INT64:
case DBUS_TYPE_UINT64:
case DBUS_TYPE_DOUBLE:
case DBUS_TYPE_BYTE:
case DBUS_TYPE_BOOLEAN:
case DBUS_TYPE_STRING:
{
if (type != demand_type) {
return false;
}
dBus->dbus_message_iter_get_basic(iter, val);
return true;
}
case DBUS_TYPE_VARIANT:
{
DBusMessageIter sub_iter;
dBus->dbus_message_iter_recurse(iter, &sub_iter);
bool res = getBasicIter(val, &sub_iter, demand_type);
// current implementation doesn't support types with multiple fields
if (dBus->dbus_message_iter_next(iter)) {
return false;
}
return res;
}
case DBUS_TYPE_INVALID:
default:
return false;
}
}
static bool sendDBusMessageWithReply(const char *messages[], int message_count, void *val, int demand_type) {
DBusError error;
DBusMessage *message = NULL;
DBusMessage *reply = NULL;
DBusMessageIter iter;
bool res = false;
if (!initialized) {
return false;
}
dBus->dbus_error_init(&error);
message = dBus->dbus_message_new_method_call(NULL, DESKTOP_PATH, SETTING_INTERFACE, SETTING_INTERFACE_METHOD);
if (message == NULL) {
printError("DBus error: cannot allocate message\n");
goto cleanup;
}
dBus->dbus_message_set_auto_start(message, true);
if (!dBus->dbus_message_set_destination(message, DESKTOP_DESTINATION)) {
printError("DBus error: cannot set destination\n");
goto cleanup;
}
dBus->dbus_message_iter_init_append(message, &iter);
for (int i = 0; i < message_count; i++) {
if (!dBus->dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &messages[i])) {
printError("DBus error: cannot append to message\n");
goto cleanup;
}
}
if ((reply = dBus->dbus_connection_send_with_reply_and_block(connection, message, REPLY_TIMEOUT, &error)) == NULL) {
printError("DBus error: cannot get reply or sent message. %s\n", dBus->dbus_error_is_set(&error) ? error.message : "");
goto cleanup;
}
if (!dBus->dbus_message_iter_init (reply, &iter)) {
printError("DBus error: cannot process message\n");
goto cleanup;
}
res = getBasicIter(val, &iter, demand_type);
cleanup:
if (reply) {
dBus->dbus_message_unref(reply);
}
if (message) {
dBus->dbus_message_unref(message);
}
return res;
}
JNIEXPORT jint JNICALL Java_sun_awt_UNIXToolkit_isSystemDarkColorScheme() {
static int use_freedesktop_appearance = -1;
const char *freedesktop_appearance_messages[] = {"org.freedesktop.appearance", "color-scheme"};
const char *gnome_desktop_messages[] = {"org.gnome.desktop.interface", "gtk-theme"};
if (use_freedesktop_appearance == -1) {
unsigned int res = 0;
logEnabled = false;
use_freedesktop_appearance =
sendDBusMessageWithReply(freedesktop_appearance_messages, 2, &res, DBUS_TYPE_UINT32);
logEnabled = true;
}
if (use_freedesktop_appearance) {
unsigned int res = 0;
if (!sendDBusMessageWithReply(freedesktop_appearance_messages, 2, &res, DBUS_TYPE_UINT32)) {
return UNKNOWN_RESULT;
}
return res;
} else {
char *res = NULL;
if (!sendDBusMessageWithReply(gnome_desktop_messages, 2, &res, DBUS_TYPE_STRING)) {
return UNKNOWN_RESULT;
}
return (res != NULL) ? strstr(res, "dark") != NULL : UNKNOWN_RESULT;
}
}
JNIEXPORT void JNICALL Java_sun_awt_UNIXToolkit_toolkitInit() {
DBusApi *dBus = DBusApi_setupDBusDefault();
if (dBus) {
SystemProperties_setup(dBus, env);
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, JetBrains s.r.o.. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#ifndef JETBRAINSRUNTIME_SYSTEM_PROPERTIES_H
#define JETBRAINSRUNTIME_SYSTEM_PROPERTIES_H
#include <stdbool.h>
#include <jni.h>
#include <jni_util.h>
#include "dbus_interface.h"
bool SystemProperties_setup(DBusApi *dBus_, JNIEnv *env_);
void SystemProperties_pullEvent(void);
#endif //JETBRAINSRUNTIME_SYSTEM_PROPERTIES_H

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2000-2024 JetBrains s.r.o.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @test
* @summary DetectingOSThemeTest checks that JBR could correctly detect OS theme and notify about theme changing in time
* @run main DetectingOSThemeTest
* @requires (os.family == "linux")
*/
public class DetectingOSThemeTest {
private static final int TIME_TO_WAIT = 2000;
private static final String LIGHT_THEME_NAME = "Light";
private static final String DARK_THEME_NAME = "Dark";
private static final String UNDEFINED_THEME_NAME = "Undefined";
private static String currentTheme() {
Boolean val = (Boolean) Toolkit.getDefaultToolkit().getDesktopProperty("awt.os.theme.isDark");
if (val == null) {
return UNDEFINED_THEME_NAME;
}
return (val) ? DARK_THEME_NAME : LIGHT_THEME_NAME;
}
private static void setOsDarkTheme(String val) {
try {
if (val.equals(DARK_THEME_NAME)) {
Runtime.getRuntime().exec("gsettings set org.gnome.desktop.interface gtk-theme 'Adwaita-dark'");
Runtime.getRuntime().exec("gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'");
} else {
Runtime.getRuntime().exec("gsettings set org.gnome.desktop.interface gtk-theme 'Adwaita'");
Runtime.getRuntime().exec("gsettings set org.gnome.desktop.interface color-scheme 'default'");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String currentTheme = null;
public static void main(String[] args) throws Exception {
currentTheme = currentTheme();
if (currentTheme.equals(UNDEFINED_THEME_NAME)) {
throw new RuntimeException("Test Failed! Cannot detect current OS theme");
}
String initialTheme = currentTheme;
try {
setOsDarkTheme(LIGHT_THEME_NAME);
Thread.sleep(TIME_TO_WAIT);
if (!currentTheme().equals(LIGHT_THEME_NAME)) {
throw new RuntimeException("Test Failed! Initial OS theme supposed to be Light");
}
String[] themesOrder = {DARK_THEME_NAME, LIGHT_THEME_NAME, DARK_THEME_NAME};
Toolkit.getDefaultToolkit().addPropertyChangeListener("awt.os.theme.isDark", evt -> {
currentTheme = currentTheme();
});
for (String nextTheme : themesOrder) {
setOsDarkTheme(nextTheme);
Thread.sleep(TIME_TO_WAIT);
if (!currentTheme().equals(nextTheme)) {
throw new RuntimeException("Test Failed! OS theme which was set doesn't match with detected");
}
if (!currentTheme.equals(nextTheme)) {
throw new RuntimeException("Test Failed! Changing OS theme was not detected");
}
}
} finally {
setOsDarkTheme(initialTheme);
}
}
}