|
24 | 24 | #include "SDL_system_theme.h"
|
25 | 25 | #include "../../video/SDL_sysvideo.h"
|
26 | 26 |
|
| 27 | +#include <stdio.h> |
27 | 28 | #include <unistd.h>
|
28 | 29 |
|
29 | 30 | #define PORTAL_DESTINATION "org.freedesktop.portal.Desktop"
|
@@ -150,7 +151,55 @@ bool SDL_SystemTheme_Init(void)
|
150 | 151 | return true;
|
151 | 152 | }
|
152 | 153 |
|
| 154 | +SDL_SystemTheme UbuntuTouch_GetSystemTheme(void) |
| 155 | +{ |
| 156 | + SDL_SystemTheme theme = SDL_SYSTEM_THEME_UNKNOWN; |
| 157 | + FILE *config_file = NULL; |
| 158 | + char *line = NULL; |
| 159 | + size_t line_alloc = 0; |
| 160 | + ssize_t line_size = 0; |
| 161 | + bool is_in_general_category = false; |
| 162 | + |
| 163 | + // "Lomiri": Ubuntu Touch 20.04+ |
| 164 | + // "Ubuntu": Ubuntu Touch 16.04 |
| 165 | + config_file = fopen("/home/phablet/.config/lomiri-ui-toolkit/theme.ini", "r"); |
| 166 | + if (!config_file) { |
| 167 | + config_file = fopen("/home/phablet/.config/ubuntu-ui-toolkit/theme.ini", "r"); |
| 168 | + if (!config_file) { |
| 169 | + return SDL_SYSTEM_THEME_UNKNOWN; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + while ((line_size = getline(&line, &line_alloc, config_file)) != -1) { |
| 174 | + if (line_size >= 1 && line[0] == '[') { |
| 175 | + is_in_general_category = SDL_strcmp(line, "[General]\n") == 0; |
| 176 | + } else if (is_in_general_category && SDL_strncmp(line, "theme=", 6) == 0) { |
| 177 | + if (SDL_strcmp(line, "theme=Lomiri.Components.Themes.SuruDark\n") == 0 || |
| 178 | + SDL_strcmp(line, "theme=Ubuntu.Components.Themes.SuruDark\n") == 0) { |
| 179 | + theme = SDL_SYSTEM_THEME_DARK; |
| 180 | + } else if (SDL_strcmp(line, "theme=Lomiri.Components.Themes.Ambiance\n") == 0 || |
| 181 | + SDL_strcmp(line, "theme=Ubuntu.Components.Themes.Ambiance\n") == 0) { |
| 182 | + theme = SDL_SYSTEM_THEME_LIGHT; |
| 183 | + } else { |
| 184 | + theme = SDL_SYSTEM_THEME_UNKNOWN; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + free(line); // This should NOT be SDL_free() |
| 189 | + } |
| 190 | + |
| 191 | + fclose(config_file); |
| 192 | + |
| 193 | + return theme; |
| 194 | +} |
| 195 | + |
153 | 196 | SDL_SystemTheme SDL_SystemTheme_Get(void)
|
154 | 197 | {
|
| 198 | + if (system_theme_data.theme == SDL_SYSTEM_THEME_UNKNOWN) { |
| 199 | + // TODO: Use inotify to watch for changes, so that the config file |
| 200 | + // doesn't need to be checked each time. |
| 201 | + return UbuntuTouch_GetSystemTheme(); |
| 202 | + } |
| 203 | + |
155 | 204 | return system_theme_data.theme;
|
156 | 205 | }
|
0 commit comments