diff options
Diffstat (limited to 'SDL-3.2.8/src/core/linux/SDL_system_theme.c')
| -rw-r--r-- | SDL-3.2.8/src/core/linux/SDL_system_theme.c | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/SDL-3.2.8/src/core/linux/SDL_system_theme.c b/SDL-3.2.8/src/core/linux/SDL_system_theme.c new file mode 100644 index 0000000..6d6087d --- /dev/null +++ b/SDL-3.2.8/src/core/linux/SDL_system_theme.c | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #include "SDL_dbus.h" | ||
| 24 | #include "SDL_system_theme.h" | ||
| 25 | #include "../../video/SDL_sysvideo.h" | ||
| 26 | |||
| 27 | #include <unistd.h> | ||
| 28 | |||
| 29 | #define PORTAL_DESTINATION "org.freedesktop.portal.Desktop" | ||
| 30 | #define PORTAL_PATH "/org/freedesktop/portal/desktop" | ||
| 31 | #define PORTAL_INTERFACE "org.freedesktop.portal.Settings" | ||
| 32 | #define PORTAL_METHOD "Read" | ||
| 33 | |||
| 34 | #define SIGNAL_INTERFACE "org.freedesktop.portal.Settings" | ||
| 35 | #define SIGNAL_NAMESPACE "org.freedesktop.appearance" | ||
| 36 | #define SIGNAL_NAME "SettingChanged" | ||
| 37 | #define SIGNAL_KEY "color-scheme" | ||
| 38 | |||
| 39 | typedef struct SystemThemeData | ||
| 40 | { | ||
| 41 | SDL_DBusContext *dbus; | ||
| 42 | SDL_SystemTheme theme; | ||
| 43 | } SystemThemeData; | ||
| 44 | |||
| 45 | static SystemThemeData system_theme_data; | ||
| 46 | |||
| 47 | static bool DBus_ExtractThemeVariant(DBusMessageIter *iter, SDL_SystemTheme *theme) { | ||
| 48 | SDL_DBusContext *dbus = system_theme_data.dbus; | ||
| 49 | Uint32 color_scheme; | ||
| 50 | DBusMessageIter variant_iter; | ||
| 51 | |||
| 52 | if (dbus->message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT) | ||
| 53 | return false; | ||
| 54 | dbus->message_iter_recurse(iter, &variant_iter); | ||
| 55 | if (dbus->message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_UINT32) | ||
| 56 | return false; | ||
| 57 | dbus->message_iter_get_basic(&variant_iter, &color_scheme); | ||
| 58 | switch (color_scheme) { | ||
| 59 | case 0: | ||
| 60 | *theme = SDL_SYSTEM_THEME_UNKNOWN; | ||
| 61 | break; | ||
| 62 | case 1: | ||
| 63 | *theme = SDL_SYSTEM_THEME_DARK; | ||
| 64 | break; | ||
| 65 | case 2: | ||
| 66 | *theme = SDL_SYSTEM_THEME_LIGHT; | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | return true; | ||
| 70 | } | ||
| 71 | |||
| 72 | static DBusHandlerResult DBus_MessageFilter(DBusConnection *conn, DBusMessage *msg, void *data) { | ||
| 73 | SDL_DBusContext *dbus = (SDL_DBusContext *)data; | ||
| 74 | |||
| 75 | if (dbus->message_is_signal(msg, SIGNAL_INTERFACE, SIGNAL_NAME)) { | ||
| 76 | DBusMessageIter signal_iter; | ||
| 77 | const char *namespace, *key; | ||
| 78 | |||
| 79 | dbus->message_iter_init(msg, &signal_iter); | ||
| 80 | // Check if the parameters are what we expect | ||
| 81 | if (dbus->message_iter_get_arg_type(&signal_iter) != DBUS_TYPE_STRING) | ||
| 82 | goto not_our_signal; | ||
| 83 | dbus->message_iter_get_basic(&signal_iter, &namespace); | ||
| 84 | if (SDL_strcmp(SIGNAL_NAMESPACE, namespace) != 0) | ||
| 85 | goto not_our_signal; | ||
| 86 | |||
| 87 | if (!dbus->message_iter_next(&signal_iter)) | ||
| 88 | goto not_our_signal; | ||
| 89 | |||
| 90 | if (dbus->message_iter_get_arg_type(&signal_iter) != DBUS_TYPE_STRING) | ||
| 91 | goto not_our_signal; | ||
| 92 | dbus->message_iter_get_basic(&signal_iter, &key); | ||
| 93 | if (SDL_strcmp(SIGNAL_KEY, key) != 0) | ||
| 94 | goto not_our_signal; | ||
| 95 | |||
| 96 | if (!dbus->message_iter_next(&signal_iter)) | ||
| 97 | goto not_our_signal; | ||
| 98 | |||
| 99 | if (!DBus_ExtractThemeVariant(&signal_iter, &system_theme_data.theme)) | ||
| 100 | goto not_our_signal; | ||
| 101 | |||
| 102 | SDL_SetSystemTheme(system_theme_data.theme); | ||
| 103 | return DBUS_HANDLER_RESULT_HANDLED; | ||
| 104 | } | ||
| 105 | not_our_signal: | ||
| 106 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | ||
| 107 | } | ||
| 108 | |||
| 109 | bool SDL_SystemTheme_Init(void) | ||
| 110 | { | ||
| 111 | SDL_DBusContext *dbus = SDL_DBus_GetContext(); | ||
| 112 | DBusMessage *msg; | ||
| 113 | static const char *namespace = SIGNAL_NAMESPACE; | ||
| 114 | static const char *key = SIGNAL_KEY; | ||
| 115 | |||
| 116 | system_theme_data.theme = SDL_SYSTEM_THEME_UNKNOWN; | ||
| 117 | system_theme_data.dbus = dbus; | ||
| 118 | if (!dbus) { | ||
| 119 | return false; | ||
| 120 | } | ||
| 121 | |||
| 122 | msg = dbus->message_new_method_call(PORTAL_DESTINATION, PORTAL_PATH, PORTAL_INTERFACE, PORTAL_METHOD); | ||
| 123 | if (msg) { | ||
| 124 | if (dbus->message_append_args(msg, DBUS_TYPE_STRING, &namespace, DBUS_TYPE_STRING, &key, DBUS_TYPE_INVALID)) { | ||
| 125 | DBusMessage *reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, 300, NULL); | ||
| 126 | if (reply) { | ||
| 127 | DBusMessageIter reply_iter, variant_outer_iter; | ||
| 128 | |||
| 129 | dbus->message_iter_init(reply, &reply_iter); | ||
| 130 | // The response has signature <<u>> | ||
| 131 | if (dbus->message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_VARIANT) | ||
| 132 | goto incorrect_type; | ||
| 133 | dbus->message_iter_recurse(&reply_iter, &variant_outer_iter); | ||
| 134 | if (!DBus_ExtractThemeVariant(&variant_outer_iter, &system_theme_data.theme)) | ||
| 135 | goto incorrect_type; | ||
| 136 | incorrect_type: | ||
| 137 | dbus->message_unref(reply); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | dbus->message_unref(msg); | ||
| 141 | } | ||
| 142 | |||
| 143 | dbus->bus_add_match(dbus->session_conn, | ||
| 144 | "type='signal', interface='"SIGNAL_INTERFACE"'," | ||
| 145 | "member='"SIGNAL_NAME"', arg0='"SIGNAL_NAMESPACE"'," | ||
| 146 | "arg1='"SIGNAL_KEY"'", NULL); | ||
| 147 | dbus->connection_add_filter(dbus->session_conn, | ||
| 148 | &DBus_MessageFilter, dbus, NULL); | ||
| 149 | dbus->connection_flush(dbus->session_conn); | ||
| 150 | return true; | ||
| 151 | } | ||
| 152 | |||
| 153 | SDL_SystemTheme SDL_SystemTheme_Get(void) | ||
| 154 | { | ||
| 155 | return system_theme_data.theme; | ||
| 156 | } | ||
