webperimental: Mountain vision bonus.
[freeciv.git] / client / gui-sdl2 / themes.c
blob96d24ff24095c4b8716d93b2dda241eca158e5de
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)
6 any later version.
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 ***********************************************************************/
13 #ifdef HAVE_CONFIG_H
14 #include <fc_config.h>
15 #endif
17 #ifdef FREECIV_HAVE_DIRENT_H
18 #include <dirent.h>
19 #endif
21 #include <sys/stat.h>
23 /* utility */
24 #include "fc_dirent.h"
25 #include "fcintl.h"
26 #include "log.h"
27 #include "mem.h"
28 #include "string_vector.h"
30 /* client/gui-sdl2 */
31 #include "themespec.h"
33 #include "themes_common.h"
34 #include "themes_g.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];
44 if (theme != NULL) {
45 /* We don't support changing theme once it has been loaded */
46 return;
49 /* free previous loaded theme, if any */
50 theme_free(theme);
51 theme = NULL;
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);
69 exit(EXIT_FAILURE);
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 *));
83 int i = 0;
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);
92 } strvec_iterate_end;
94 return directories;
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)
105 DIR *dir;
106 struct dirent *entry;
107 char **theme_names = fc_malloc(sizeof(char *) * 2);
108 /* Allocated memory size */
109 int t_size = 2;
111 *count = 0;
113 dir = fc_opendir(directory);
114 if (!dir) {
115 /* This isn't directory or we can't list it */
116 return theme_names;
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 */
128 continue;
131 if (!S_ISREG(stat_result.st_mode)) {
132 /* Not a regular file */
133 continue;
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 *));
141 t_size *= 2;
144 theme_names[*count] = fc_strdup(entry->d_name);
145 (*count)++;
148 closedir(dir);
150 return theme_names;