Merge branch '4627_help_segfault'
[midnight-commander.git] / lib / skin / common.c
blob986b7216ce2210fba2bb73b3a1b65e878ab68b13
1 /*
2 Skins engine.
3 Interface functions
5 Copyright (C) 2009-2024
6 Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <config.h>
29 #include <stdlib.h>
31 #include "internal.h"
32 #include "lib/util.h"
34 #include "lib/tty/color.h" /* tty_use_256colors(); */
36 /*** global variables ****************************************************************************/
38 mc_skin_t mc_skin__default;
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** forward declarations (file scope functions) *************************************************/
46 /*** file scope variables ************************************************************************/
48 static gboolean mc_skin_is_init = FALSE;
50 /* --------------------------------------------------------------------------------------------- */
51 /*** file scope functions ************************************************************************/
52 /* --------------------------------------------------------------------------------------------- */
54 static void
55 mc_skin_hash_destroy_value (gpointer data)
57 tty_color_pair_t *mc_skin_color = (tty_color_pair_t *) data;
59 g_free (mc_skin_color->fg);
60 g_free (mc_skin_color->bg);
61 g_free (mc_skin_color->attrs);
62 g_free (mc_skin_color);
65 /* --------------------------------------------------------------------------------------------- */
67 static char *
68 mc_skin_get_default_name (void)
70 char *tmp_str;
72 /* from command line */
73 if (mc_global.tty.skin != NULL)
74 return g_strdup (mc_global.tty.skin);
76 /* from envirovement variable */
77 tmp_str = getenv ("MC_SKIN");
78 if (tmp_str != NULL)
79 return g_strdup (tmp_str);
81 /* from config. Or 'default' if no present in config */
82 return mc_config_get_string (mc_global.main_config, CONFIG_APP_SECTION, "skin", "default");
85 /* --------------------------------------------------------------------------------------------- */
87 static void
88 mc_skin_reinit (void)
90 mc_skin_deinit ();
91 mc_skin__default.name = mc_skin_get_default_name ();
92 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
93 g_free, mc_skin_hash_destroy_value);
96 /* --------------------------------------------------------------------------------------------- */
98 static void
99 mc_skin_try_to_load_default (void)
101 mc_skin_reinit ();
102 g_free (mc_skin__default.name);
103 mc_skin__default.name = g_strdup ("default");
104 if (!mc_skin_ini_file_load (&mc_skin__default))
106 mc_skin_reinit ();
107 mc_skin_set_hardcoded_skin (&mc_skin__default);
111 /* --------------------------------------------------------------------------------------------- */
112 /*** public functions ****************************************************************************/
113 /* --------------------------------------------------------------------------------------------- */
115 gboolean
116 mc_skin_init (const gchar *skin_override, GError **mcerror)
118 gboolean is_good_init = TRUE;
119 GError *error = NULL;
121 mc_return_val_if_error (mcerror, FALSE);
123 mc_skin__default.have_256_colors = FALSE;
124 mc_skin__default.have_true_colors = FALSE;
126 mc_skin__default.name =
127 skin_override != NULL ? g_strdup (skin_override) : mc_skin_get_default_name ();
129 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
130 g_free, mc_skin_hash_destroy_value);
131 if (!mc_skin_ini_file_load (&mc_skin__default))
133 mc_propagate_error (mcerror, 0,
134 _("Unable to load '%s' skin.\nDefault skin has been loaded"),
135 mc_skin__default.name);
136 mc_skin_try_to_load_default ();
137 is_good_init = FALSE;
139 mc_skin_colors_old_configure (&mc_skin__default);
141 if (!mc_skin_ini_file_parse (&mc_skin__default))
143 mc_propagate_error (mcerror, 0,
144 _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
145 mc_skin__default.name);
147 mc_skin_try_to_load_default ();
148 mc_skin_colors_old_configure (&mc_skin__default);
149 (void) mc_skin_ini_file_parse (&mc_skin__default);
150 is_good_init = FALSE;
152 if (is_good_init && mc_skin__default.have_true_colors && !tty_use_truecolors (&error))
154 mc_propagate_error (mcerror, 0,
156 ("Unable to use '%s' skin with true colors support:\n%s\nDefault skin has been loaded"),
157 mc_skin__default.name, error->message);
158 g_error_free (error);
159 mc_skin_try_to_load_default ();
160 mc_skin_colors_old_configure (&mc_skin__default);
161 (void) mc_skin_ini_file_parse (&mc_skin__default);
162 is_good_init = FALSE;
164 if (is_good_init && mc_skin__default.have_256_colors && !tty_use_256colors (&error))
166 mc_propagate_error (mcerror, 0,
168 ("Unable to use '%s' skin with 256 colors support\non non-256 colors terminal.\nDefault skin has been loaded"),
169 mc_skin__default.name);
170 mc_skin_try_to_load_default ();
171 mc_skin_colors_old_configure (&mc_skin__default);
172 (void) mc_skin_ini_file_parse (&mc_skin__default);
173 is_good_init = FALSE;
175 mc_skin_is_init = TRUE;
176 return is_good_init;
179 /* --------------------------------------------------------------------------------------------- */
181 void
182 mc_skin_deinit (void)
184 tty_color_free_all ();
186 MC_PTR_FREE (mc_skin__default.name);
187 g_hash_table_destroy (mc_skin__default.colors);
188 mc_skin__default.colors = NULL;
190 MC_PTR_FREE (mc_skin__default.description);
192 mc_config_deinit (mc_skin__default.config);
193 mc_skin__default.config = NULL;
195 mc_skin_is_init = FALSE;
198 /* --------------------------------------------------------------------------------------------- */
200 gchar *
201 mc_skin_get (const gchar *group, const gchar *key, const gchar *default_value)
203 if (mc_global.tty.ugly_line_drawing)
204 return g_strdup (default_value);
206 return mc_config_get_string (mc_skin__default.config, group, key, default_value);
209 /* --------------------------------------------------------------------------------------------- */