Function to get the current system color theme

This commit is contained in:
Kovid Goyal
2024-11-07 12:47:06 +05:30
parent 239e08f268
commit b0ebd340fe
13 changed files with 99 additions and 29 deletions

View File

@@ -22,12 +22,7 @@ static const char* supported_namespaces[2] = {FDO_DESKTOP_NAMESPACE, GNOME_DESKT
static char theme_name[128] = {0};
static int theme_size = -1;
static GLFWColorScheme appearance = GLFW_COLOR_SCHEME_NO_PREFERENCE;
static bool cursor_theme_changed = false;
GLFWColorScheme
glfw_current_system_color_theme(void) {
return appearance;
}
static bool cursor_theme_changed = false, appearance_initialized = false;
#define HANDLER(name) static void name(DBusMessage *msg, const char* errmsg, void *data) { \
(void)data; \
@@ -36,6 +31,34 @@ glfw_current_system_color_theme(void) {
return; \
}
HANDLER(get_color_scheme)
uint32_t val;
DBusMessageIter iter, variant_iter;
if (!dbus_message_iter_init(msg, &iter)) return;
dbus_message_iter_recurse(&iter, &variant_iter);
int type = dbus_message_iter_get_arg_type(&variant_iter);
if (type != DBUS_TYPE_UINT32) {
_glfwInputError(GLFW_PLATFORM_ERROR, "ReadOne for color-scheme did not return a uint32"); return;
}
dbus_message_iter_get_basic(&variant_iter, &val);
if (val < 3) appearance = val;
}
GLFWColorScheme
glfw_current_system_color_theme(bool query_if_unintialized) {
if (!appearance_initialized && query_if_unintialized) {
appearance_initialized = true;
DBusConnection *session_bus = glfw_dbus_session_bus();
if (session_bus) {
const char *namespace = FDO_DESKTOP_NAMESPACE, *key = FDO_APPEARANCE_KEY;
glfw_dbus_call_blocking_method(session_bus, DESKTOP_SERVICE, DESKTOP_PATH, DESKTOP_INTERFACE, "ReadOne", DBUS_TIMEOUT_USE_DEFAULT,
get_color_scheme, NULL, DBUS_TYPE_STRING, &namespace, DBUS_TYPE_STRING, &key, DBUS_TYPE_INVALID);
}
}
return appearance;
}
static void
process_fdo_setting(const char *key, DBusMessageIter *value) {
if (strcmp(key, FDO_APPEARANCE_KEY) == 0) {
@@ -43,8 +66,13 @@ process_fdo_setting(const char *key, DBusMessageIter *value) {
uint32_t val;
dbus_message_iter_get_basic(value, &val);
if (val > 2) val = 0;
appearance = val;
_glfwInputColorScheme(appearance, true);
if (!appearance_initialized) {
appearance_initialized = true;
if (val != appearance) {
appearance = val;
_glfwInputColorScheme(appearance, true);
}
}
}
}
}
@@ -131,7 +159,7 @@ read_desktop_settings(DBusConnection *session_bus) {
if (!dbus_message_iter_append_basic(&array_iter, DBUS_TYPE_STRING, &supported_namespaces[i])) return false;
}
if (!dbus_message_iter_close_container(&iter, &array_iter)) { return false; }
return call_method_with_msg(session_bus, msg, DBUS_TIMEOUT_USE_DEFAULT, process_desktop_settings, NULL);
return call_method_with_msg(session_bus, msg, DBUS_TIMEOUT_USE_DEFAULT, process_desktop_settings, NULL, false);
}
void
@@ -166,6 +194,7 @@ on_color_scheme_change(DBusMessage *message) {
if (val > 2) val = 0;
if (val != appearance) {
appearance = val;
appearance_initialized = true;
_glfwInputColorScheme(appearance, false);
}
}