SDL_system_theme.c 5.6 KB

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