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-2024
   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 <http://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 static gboolean mc_skin_is_init = FALSE;
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 /*** file scope functions ************************************************************************/
  52 /* --------------------------------------------------------------------------------------------- */
  53 
  54 static void
  55 mc_skin_hash_destroy_value (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  56 {
  57     tty_color_pair_t *mc_skin_color = (tty_color_pair_t *) data;
  58 
  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);
  63 }
  64 
  65 /* --------------------------------------------------------------------------------------------- */
  66 
  67 static char *
  68 mc_skin_get_default_name (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  69 {
  70     char *tmp_str;
  71 
  72     /* from command line */
  73     if (mc_global.tty.skin != NULL)
  74         return g_strdup (mc_global.tty.skin);
  75 
  76     /* from envirovement variable */
  77     tmp_str = getenv ("MC_SKIN");
  78     if (tmp_str != NULL)
  79         return g_strdup (tmp_str);
  80 
  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");
  83 }
  84 
  85 /* --------------------------------------------------------------------------------------------- */
  86 
  87 static void
  88 mc_skin_reinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  89 {
  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);
  94 }
  95 
  96 /* --------------------------------------------------------------------------------------------- */
  97 
  98 static void
  99 mc_skin_try_to_load_default (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 100 {
 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))
 105     {
 106         mc_skin_reinit ();
 107         mc_skin_set_hardcoded_skin (&mc_skin__default);
 108     }
 109 }
 110 
 111 /* --------------------------------------------------------------------------------------------- */
 112 /*** public functions ****************************************************************************/
 113 /* --------------------------------------------------------------------------------------------- */
 114 
 115 gboolean
 116 mc_skin_init (const gchar * skin_override, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 117 {
 118     gboolean is_good_init = TRUE;
 119     GError *error = NULL;
 120 
 121     mc_return_val_if_error (mcerror, FALSE);
 122 
 123     mc_skin__default.have_256_colors = FALSE;
 124     mc_skin__default.have_true_colors = FALSE;
 125 
 126     mc_skin__default.name =
 127         skin_override != NULL ? g_strdup (skin_override) : mc_skin_get_default_name ();
 128 
 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))
 132     {
 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;
 138     }
 139     mc_skin_colors_old_configure (&mc_skin__default);
 140 
 141     if (!mc_skin_ini_file_parse (&mc_skin__default))
 142     {
 143         mc_propagate_error (mcerror, 0,
 144                             _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
 145                             mc_skin__default.name);
 146 
 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;
 151     }
 152     if (is_good_init && mc_skin__default.have_true_colors && !tty_use_truecolors (&error))
 153     {
 154         mc_propagate_error (mcerror, 0,
 155                             _
 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;
 163     }
 164     if (is_good_init && mc_skin__default.have_256_colors && !tty_use_256colors (&error))
 165     {
 166         mc_propagate_error (mcerror, 0,
 167                             _
 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;
 174     }
 175     mc_skin_is_init = TRUE;
 176     return is_good_init;
 177 }
 178 
 179 /* --------------------------------------------------------------------------------------------- */
 180 
 181 void
 182 mc_skin_deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 183 {
 184     tty_color_free_all ();
 185 
 186     MC_PTR_FREE (mc_skin__default.name);
 187     g_hash_table_destroy (mc_skin__default.colors);
 188     mc_skin__default.colors = NULL;
 189 
 190     MC_PTR_FREE (mc_skin__default.description);
 191 
 192     mc_config_deinit (mc_skin__default.config);
 193     mc_skin__default.config = NULL;
 194 
 195     mc_skin_is_init = FALSE;
 196 }
 197 
 198 /* --------------------------------------------------------------------------------------------- */
 199 
 200 gchar *
 201 mc_skin_get (const gchar * group, const gchar * key, const gchar * default_value)
     /* [previous][next][first][last][top][bottom][index][help]  */
 202 {
 203     if (mc_global.tty.ugly_line_drawing)
 204         return g_strdup (default_value);
 205 
 206     return mc_config_get_string (mc_skin__default.config, group, key, default_value);
 207 }
 208 
 209 /* --------------------------------------------------------------------------------------------- */

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