Manual pages: mcmcdiffmceditmcview

root/lib/skin/common.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_skin_hash_destroy_value
  2. mc_skin_get_default_name
  3. mc_skin_reinit
  4. mc_skin_try_to_load_default
  5. mc_skin_init
  6. mc_skin_deinit
  7. mc_skin_get

   1 /*
   2    Skins engine.
   3    Interface functions
   4 
   5    Copyright (C) 2009-2026
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2009
  10    Egmont Koblinger <egmont@gmail.com>, 2010
  11 
  12    This file is part of the Midnight Commander.
  13 
  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.
  18 
  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.
  23 
  24    You should have received a copy of the GNU General Public License
  25    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  26  */
  27 
  28 #include <config.h>
  29 #include <stdlib.h>
  30 
  31 #include "internal.h"
  32 #include "lib/util.h"
  33 
  34 #include "lib/tty/color.h"  // tty_use_256colors();
  35 
  36 /*** global variables ****************************************************************************/
  37 
  38 mc_skin_t mc_skin__default;
  39 
  40 /*** file scope macro definitions ****************************************************************/
  41 
  42 /*** file scope type declarations ****************************************************************/
  43 
  44 /*** forward declarations (file scope functions) *************************************************/
  45 
  46 /*** file scope variables ************************************************************************/
  47 
  48 /* --------------------------------------------------------------------------------------------- */
  49 /*** file scope functions ************************************************************************/
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 static void
  53 mc_skin_hash_destroy_value (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  54 {
  55     tty_color_pair_t *mc_skin_color = (tty_color_pair_t *) data;
  56 
  57     g_free (mc_skin_color->fg);
  58     g_free (mc_skin_color->bg);
  59     g_free (mc_skin_color->attrs);
  60     g_free (mc_skin_color);
  61 }
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 
  65 static char *
  66 mc_skin_get_default_name (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  67 {
  68     char *tmp_str;
  69 
  70     // from command line
  71     if (mc_global.tty.skin != NULL)
  72         return g_strdup (mc_global.tty.skin);
  73 
  74     // from environment variable
  75     tmp_str = getenv ("MC_SKIN");
  76     if (tmp_str != NULL)
  77         return g_strdup (tmp_str);
  78 
  79     //  from config. Or 'default' if no present in config
  80     return mc_config_get_string (mc_global.main_config, CONFIG_APP_SECTION, "skin", "default");
  81 }
  82 
  83 /* --------------------------------------------------------------------------------------------- */
  84 
  85 static void
  86 mc_skin_reinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  87 {
  88     mc_skin_deinit ();
  89     mc_skin__default.name = mc_skin_get_default_name ();
  90     mc_skin__default.colors =
  91         g_hash_table_new_full (g_str_hash, g_str_equal, g_free, mc_skin_hash_destroy_value);
  92 }
  93 
  94 /* --------------------------------------------------------------------------------------------- */
  95 
  96 static void
  97 mc_skin_try_to_load_default (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  98 {
  99     mc_skin_reinit ();
 100     g_free (mc_skin__default.name);
 101     mc_skin__default.name = g_strdup ("default");
 102     if (!mc_skin_ini_file_load (&mc_skin__default))
 103     {
 104         mc_skin_reinit ();
 105         mc_skin_set_hardcoded_skin (&mc_skin__default);
 106     }
 107 }
 108 
 109 /* --------------------------------------------------------------------------------------------- */
 110 /*** public functions ****************************************************************************/
 111 /* --------------------------------------------------------------------------------------------- */
 112 
 113 gboolean
 114 mc_skin_init (const gchar *skin_override, GError **mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 115 {
 116     gboolean is_good_init = TRUE;
 117     GError *error = NULL;
 118 
 119     mc_return_val_if_error (mcerror, FALSE);
 120 
 121     mc_skin__default.have_256_colors = FALSE;
 122     mc_skin__default.have_true_colors = FALSE;
 123 
 124     mc_skin__default.name =
 125         skin_override != NULL ? g_strdup (skin_override) : mc_skin_get_default_name ();
 126 
 127     mc_skin__default.colors =
 128         g_hash_table_new_full (g_str_hash, g_str_equal, g_free, mc_skin_hash_destroy_value);
 129     if (!mc_skin_ini_file_load (&mc_skin__default))
 130     {
 131         mc_propagate_error (mcerror, 0,
 132                             _ ("Unable to load '%s' skin.\nDefault skin has been loaded"),
 133                             mc_skin__default.name);
 134         mc_skin_try_to_load_default ();
 135         is_good_init = FALSE;
 136     }
 137 
 138     if (!mc_skin_ini_file_parse (&mc_skin__default))
 139     {
 140         mc_propagate_error (mcerror, 0,
 141                             _ ("Unable to parse '%s' skin.\nDefault skin has been loaded"),
 142                             mc_skin__default.name);
 143 
 144         mc_skin_try_to_load_default ();
 145         (void) mc_skin_ini_file_parse (&mc_skin__default);
 146         is_good_init = FALSE;
 147     }
 148     if (is_good_init && mc_skin__default.have_true_colors && !tty_use_truecolors (&error))
 149     {
 150         if (!mc_global.tty.disable_colors || skin_override != NULL)
 151             mc_propagate_error (mcerror, 0,
 152                                 _ ("Unable to use '%s' skin with true colors support:\n%s\n"
 153                                    "Default skin has been loaded"),
 154                                 mc_skin__default.name, error->message);
 155         g_error_free (error);
 156         mc_skin_try_to_load_default ();
 157         (void) mc_skin_ini_file_parse (&mc_skin__default);
 158         is_good_init = FALSE;
 159     }
 160     if (is_good_init && mc_skin__default.have_256_colors && !tty_use_256colors (&error))
 161     {
 162         if (!mc_global.tty.disable_colors || skin_override != NULL)
 163             mc_propagate_error (mcerror, 0,
 164                                 _ ("Unable to use '%s' skin with 256 colors support:\n%s\n"
 165                                    "Default skin has been loaded"),
 166                                 mc_skin__default.name, error->message);
 167         g_error_free (error);
 168         mc_skin_try_to_load_default ();
 169         (void) mc_skin_ini_file_parse (&mc_skin__default);
 170         is_good_init = FALSE;
 171     }
 172     return is_good_init;
 173 }
 174 
 175 /* --------------------------------------------------------------------------------------------- */
 176 
 177 void
 178 mc_skin_deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 179 {
 180     tty_color_free_all ();
 181 
 182     MC_PTR_FREE (mc_skin__default.name);
 183     g_hash_table_destroy (mc_skin__default.colors);
 184     mc_skin__default.colors = NULL;
 185 
 186     MC_PTR_FREE (mc_skin__default.description);
 187 
 188     mc_config_deinit (mc_skin__default.config);
 189     mc_skin__default.config = NULL;
 190 }
 191 
 192 /* --------------------------------------------------------------------------------------------- */
 193 
 194 gchar *
 195 mc_skin_get (const gchar *group, const gchar *key, const gchar *default_value)
     /* [previous][next][first][last][top][bottom][index][help]  */
 196 {
 197     if (mc_global.tty.ugly_line_drawing)
 198         return g_strdup (default_value);
 199 
 200     return mc_config_get_string_strict (mc_skin__default.config, group, key, default_value);
 201 }
 202 
 203 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */