SDL_system_theme.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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 "../../video/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 bool DBus_ExtractThemeVariant(DBusMessageIter *iter, SDL_SystemTheme *theme) {
  38. SDL_DBusContext *dbus = system_theme_data.dbus;
  39. Uint32 color_scheme;
  40. DBusMessageIter variant_iter;
  41. if (dbus->message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT)
  42. return false;
  43. dbus->message_iter_recurse(iter, &variant_iter);
  44. if (dbus->message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_UINT32)
  45. return false;
  46. dbus->message_iter_get_basic(&variant_iter, &color_scheme);
  47. switch (color_scheme) {
  48. case 0:
  49. *theme = SDL_SYSTEM_THEME_UNKNOWN;
  50. break;
  51. case 1:
  52. *theme = SDL_SYSTEM_THEME_DARK;
  53. break;
  54. case 2:
  55. *theme = SDL_SYSTEM_THEME_LIGHT;
  56. break;
  57. }
  58. return true;
  59. }
  60. static DBusHandlerResult DBus_MessageFilter(DBusConnection *conn, DBusMessage *msg, void *data) {
  61. SDL_DBusContext *dbus = (SDL_DBusContext *)data;
  62. if (dbus->message_is_signal(msg, SIGNAL_INTERFACE, SIGNAL_NAME)) {
  63. DBusMessageIter signal_iter;
  64. const char *namespace, *key;
  65. dbus->message_iter_init(msg, &signal_iter);
  66. // Check if the parameters are what we expect
  67. if (dbus->message_iter_get_arg_type(&signal_iter) != DBUS_TYPE_STRING)
  68. goto not_our_signal;
  69. dbus->message_iter_get_basic(&signal_iter, &namespace);
  70. if (SDL_strcmp(SIGNAL_NAMESPACE, namespace) != 0)
  71. goto not_our_signal;
  72. if (!dbus->message_iter_next(&signal_iter))
  73. goto not_our_signal;
  74. if (dbus->message_iter_get_arg_type(&signal_iter) != DBUS_TYPE_STRING)
  75. goto not_our_signal;
  76. dbus->message_iter_get_basic(&signal_iter, &key);
  77. if (SDL_strcmp(SIGNAL_KEY, key) != 0)
  78. goto not_our_signal;
  79. if (!dbus->message_iter_next(&signal_iter))
  80. goto not_our_signal;
  81. if (!DBus_ExtractThemeVariant(&signal_iter, &system_theme_data.theme))
  82. goto not_our_signal;
  83. //SDL_SetSystemTheme(system_theme_data.theme);
  84. return DBUS_HANDLER_RESULT_HANDLED;
  85. }
  86. not_our_signal:
  87. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  88. }
  89. bool SDL_SystemTheme_Init(void)
  90. {
  91. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  92. DBusMessage *msg;
  93. static const char *namespace = SIGNAL_NAMESPACE;
  94. static const char *key = SIGNAL_KEY;
  95. system_theme_data.theme = SDL_SYSTEM_THEME_UNKNOWN;
  96. system_theme_data.dbus = dbus;
  97. if (!dbus) {
  98. return false;
  99. }
  100. msg = dbus->message_new_method_call(PORTAL_DESTINATION, PORTAL_PATH, PORTAL_INTERFACE, PORTAL_METHOD);
  101. if (msg) {
  102. if (dbus->message_append_args(msg, DBUS_TYPE_STRING, &namespace, DBUS_TYPE_STRING, &key, DBUS_TYPE_INVALID)) {
  103. DBusMessage *reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, 300, NULL);
  104. if (reply) {
  105. DBusMessageIter reply_iter, variant_outer_iter;
  106. dbus->message_iter_init(reply, &reply_iter);
  107. // The response has signature <<u>>
  108. if (dbus->message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_VARIANT)
  109. goto incorrect_type;
  110. dbus->message_iter_recurse(&reply_iter, &variant_outer_iter);
  111. if (!DBus_ExtractThemeVariant(&variant_outer_iter, &system_theme_data.theme))
  112. goto incorrect_type;
  113. incorrect_type:
  114. dbus->message_unref(reply);
  115. }
  116. }
  117. dbus->message_unref(msg);
  118. }
  119. dbus->bus_add_match(dbus->session_conn,
  120. "type='signal', interface='"SIGNAL_INTERFACE"',"
  121. "member='"SIGNAL_NAME"', arg0='"SIGNAL_NAMESPACE"',"
  122. "arg1='"SIGNAL_KEY"'", NULL);
  123. dbus->connection_add_filter(dbus->session_conn,
  124. &DBus_MessageFilter, dbus, NULL);
  125. dbus->connection_flush(dbus->session_conn);
  126. return true;
  127. }
  128. SDL_SystemTheme SDL_SystemTheme_Get(void)
  129. {
  130. return system_theme_data.theme;
  131. }