1 /***********************************************************************
2 Freeciv - Copyright (C) 2005 The Freeciv Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #include <fc_config.h>
17 #ifdef FREECIV_HAVE_DIRENT_H
24 #include "fc_dirent.h"
28 #include "string_vector.h"
31 #include "themespec.h"
33 #include "themes_common.h"
36 /*****************************************************************************
37 Loads a gui-sdl2 theme directory/theme_name
38 *****************************************************************************/
39 void gui_load_theme(const char *directory
, const char *theme_name
)
41 char buf
[strlen(directory
) + strlen(DIR_SEPARATOR
) + strlen(theme_name
)
42 + strlen(DIR_SEPARATOR
"theme") + 1];
45 /* We don't support changing theme once it has been loaded */
49 /* free previous loaded theme, if any */
53 fc_snprintf(buf
, sizeof(buf
), "%s" DIR_SEPARATOR
"%s" DIR_SEPARATOR
"theme",
54 directory
, theme_name
);
56 themespec_try_read(buf
);
57 theme_load_sprites(theme
);
60 /*****************************************************************************
61 Clears a theme (sets default system theme)
62 *****************************************************************************/
63 void gui_clear_theme(void)
65 if (!load_theme(gui_options
.gui_sdl2_default_theme_name
)) {
66 /* TRANS: No full stop after the URL, could cause confusion. */
67 log_fatal(_("No Sdl2-client theme was found. For instructions on how to "
68 "get one, please visit %s"), WIKI_URL
);
73 /*****************************************************************************
74 Each gui has its own themes directories.
76 Returns an array containing these strings and sets array size in count.
77 The caller is responsible for freeing the array and the paths.
78 *****************************************************************************/
79 char **get_gui_specific_themes_directories(int *count
)
81 const struct strvec
*data_dirs
= get_data_dirs();
82 char **directories
= fc_malloc(strvec_size(data_dirs
) * sizeof(char *));
85 *count
= strvec_size(data_dirs
);
86 strvec_iterate(data_dirs
, data_dir
) {
87 char buf
[strlen(data_dir
) + strlen("/themes/gui-sdl2") + 1];
89 fc_snprintf(buf
, sizeof(buf
), "%s/themes/gui-sdl2", data_dir
);
91 directories
[i
++] = fc_strdup(buf
);
97 /*****************************************************************************
98 Return an array of names of usable themes in the given directory.
99 Array size is stored in count.
100 Useable theme for gui-sdl2 is a directory which contains file theme.themespec.
101 The caller is responsible for freeing the array and the names
102 *****************************************************************************/
103 char **get_useable_themes_in_directory(const char *directory
, int *count
)
106 struct dirent
*entry
;
107 char **theme_names
= fc_malloc(sizeof(char *) * 2);
108 /* Allocated memory size */
113 dir
= fc_opendir(directory
);
115 /* This isn't directory or we can't list it */
119 while ((entry
= readdir(dir
))) {
120 char buf
[strlen(directory
) + strlen(entry
->d_name
) + 32];
121 struct stat stat_result
;
123 fc_snprintf(buf
, sizeof(buf
),
124 "%s/%s/theme.themespec", directory
, entry
->d_name
);
126 if (fc_stat(buf
, &stat_result
) != 0) {
127 /* File doesn't exist */
131 if (!S_ISREG(stat_result
.st_mode
)) {
132 /* Not a regular file */
136 /* Otherwise it's ok */
138 /* Increase array size if needed */
139 if (*count
== t_size
) {
140 theme_names
= fc_realloc(theme_names
, t_size
* 2 * sizeof(char *));
144 theme_names
[*count
] = fc_strdup(entry
->d_name
);