Manual pages: mcmcdiffmceditmcview

root/lib/skin/colors.c

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

DEFINITIONS

This source file includes following definitions.
  1. mc_skin_color_get_from_hash
  2. mc_skin_color_remove_from_hash
  3. mc_skin_color_add_to_hash
  4. mc_skin_color_get_with_defaults
  5. mc_skin_color_look_up_alias
  6. mc_skin_color_get_from_ini_file
  7. mc_skin_color_set_default_for_terminal
  8. mc_skin_color_cache_init
  9. mc_skin_color_check_inisection
  10. mc_skin_color_check_bw_mode
  11. mc_skin_color_parse_ini_file
  12. mc_skin_color_get

   1 /*
   2    Skins engine.
   3    Work with colors
   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    Andrew Borodin <aborodin@vmail.ru>, 2012
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  27  */
  28 
  29 #include <config.h>
  30 #include <string.h>
  31 
  32 #include "internal.h"
  33 
  34 #include "lib/tty/color.h"
  35 
  36 /*** global variables ****************************************************************************/
  37 
  38 /*** file scope macro definitions ****************************************************************/
  39 
  40 /*** file scope type declarations ****************************************************************/
  41 
  42 typedef struct
  43 {
  44     int role;
  45     const char *group;
  46     const char *key;
  47 } color_keyword_t;
  48 
  49 /*** forward declarations (file scope functions) *************************************************/
  50 
  51 /*** file scope variables ************************************************************************/
  52 
  53 static const color_keyword_t color_keywords[] = {
  54     { CORE_DEFAULT_COLOR, "skin", "terminal_default_color" },
  55     { CORE_NORMAL_COLOR, "core", "_default_" },
  56     { CORE_MARKED_COLOR, "core", "marked" },
  57     { CORE_SELECTED_COLOR, "core", "selected" },
  58     { CORE_MARKED_SELECTED_COLOR, "core", "markselect" },
  59     { CORE_DISABLED_COLOR, "core", "disabled" },
  60     { CORE_REVERSE_COLOR, "core", "reverse" },
  61     { CORE_HEADER_COLOR, "core", "header" },
  62     { CORE_HINTBAR_COLOR, "core", "hintbar" },
  63     { CORE_SHELLPROMPT_COLOR, "core", "shellprompt" },
  64     { CORE_COMMANDLINE_COLOR, "core", "commandline" },
  65     { CORE_COMMANDLINE_MARK_COLOR, "core", "commandlinemark" },
  66     { CORE_SHADOW_COLOR, "core", "shadow" },
  67     { CORE_FRAME_COLOR, "core", "frame" },
  68 
  69     { DIALOG_NORMAL_COLOR, "dialog", "_default_" },
  70     { DIALOG_FOCUS_COLOR, "dialog", "dfocus" },
  71     { DIALOG_HOT_NORMAL_COLOR, "dialog", "dhotnormal" },
  72     { DIALOG_HOT_FOCUS_COLOR, "dialog", "dhotfocus" },
  73     { DIALOG_SELECTED_NORMAL_COLOR, "dialog", "dselnormal" },
  74     { DIALOG_SELECTED_FOCUS_COLOR, "dialog", "dselfocus" },
  75     { DIALOG_TITLE_COLOR, "dialog", "dtitle" },
  76     { DIALOG_FRAME_COLOR, "dialog", "dframe" },
  77 
  78     { ERROR_NORMAL_COLOR, "error", "_default_" },
  79     { ERROR_FOCUS_COLOR, "error", "errdfocus" },
  80     { ERROR_HOT_NORMAL_COLOR, "error", "errdhotnormal" },
  81     { ERROR_HOT_FOCUS_COLOR, "error", "errdhotfocus" },
  82     { ERROR_TITLE_COLOR, "error", "errdtitle" },
  83     { ERROR_FRAME_COLOR, "error", "errdframe" },
  84 
  85     { FILEHIGHLIGHT_DEFAULT_COLOR, "filehighlight", "_default_" },
  86 
  87     { MENU_ENTRY_COLOR, "menu", "_default_" },
  88     { MENU_SELECTED_COLOR, "menu", "menusel" },
  89     { MENU_HOT_COLOR, "menu", "menuhot" },
  90     { MENU_HOTSEL_COLOR, "menu", "menuhotsel" },
  91     { MENU_INACTIVE_COLOR, "menu", "menuinactive" },
  92     { MENU_FRAME_COLOR, "menu", "menuframe" },
  93 
  94     { PMENU_ENTRY_COLOR, "popupmenu", "_default_" },
  95     { PMENU_SELECTED_COLOR, "popupmenu", "menusel" },
  96     { PMENU_TITLE_COLOR, "popupmenu", "menutitle" },
  97     { PMENU_FRAME_COLOR, "popupmenu", "menuframe" },
  98 
  99     { BUTTONBAR_HOTKEY_COLOR, "buttonbar", "hotkey" },
 100     { BUTTONBAR_BUTTON_COLOR, "buttonbar", "button" },
 101 
 102     { STATUSBAR_COLOR, "statusbar", "_default_" },
 103 
 104     { CORE_GAUGE_COLOR, "core", "gauge" },
 105     { CORE_INPUT_COLOR, "core", "input" },
 106     { CORE_INPUT_HISTORY_COLOR, "core", "inputhistory" },
 107     { CORE_COMMAND_HISTORY_COLOR, "core", "commandhistory" },
 108     { CORE_INPUT_MARK_COLOR, "core", "inputmark" },
 109     { CORE_INPUT_UNCHANGED_COLOR, "core", "inputunchanged" },
 110 
 111     { HELP_NORMAL_COLOR, "help", "_default_" },
 112     { HELP_ITALIC_COLOR, "help", "helpitalic" },
 113     { HELP_BOLD_COLOR, "help", "helpbold" },
 114     { HELP_LINK_COLOR, "help", "helplink" },
 115     { HELP_SLINK_COLOR, "help", "helpslink" },
 116     { HELP_TITLE_COLOR, "help", "helptitle" },
 117     { HELP_FRAME_COLOR, "help", "helpframe" },
 118 
 119     { VIEWER_NORMAL_COLOR, "viewer", "_default_" },
 120     { VIEWER_BOLD_COLOR, "viewer", "viewbold" },
 121     { VIEWER_UNDERLINED_COLOR, "viewer", "viewunderline" },
 122     { VIEWER_BOLD_UNDERLINED_COLOR, "viewer", "viewboldunderline" },
 123     { VIEWER_SELECTED_COLOR, "viewer", "viewselected" },
 124     { VIEWER_FRAME_COLOR, "viewer", "viewframe" },
 125 
 126     { EDITOR_NORMAL_COLOR, "editor", "_default_" },
 127     { EDITOR_BOLD_COLOR, "editor", "editbold" },
 128     { EDITOR_MARKED_COLOR, "editor", "editmarked" },
 129     { EDITOR_WHITESPACE_COLOR, "editor", "editwhitespace" },
 130     { EDITOR_NONPRINTABLE_COLOR, "editor", "editnonprintable" },
 131     { EDITOR_RIGHT_MARGIN_COLOR, "editor", "editrightmargin" },
 132     { EDITOR_LINE_STATE_COLOR, "editor", "editlinestate" },
 133     { EDITOR_BACKGROUND_COLOR, "editor", "editbg" },
 134     { EDITOR_FRAME_COLOR, "editor", "editframe" },
 135     { EDITOR_FRAME_ACTIVE_COLOR, "editor", "editframeactive" },
 136     { EDITOR_FRAME_DRAG_COLOR, "editor", "editframedrag" },
 137 
 138     { EDITOR_BOOKMARK_COLOR, "editor", "bookmark" },
 139     { EDITOR_BOOKMARK_FOUND_COLOR, "editor", "bookmarkfound" },
 140 
 141     { DIFFVIEWER_ADDED_COLOR, "diffviewer", "added" },
 142     { DIFFVIEWER_CHANGEDLINE_COLOR, "diffviewer", "changedline" },
 143     { DIFFVIEWER_CHANGEDNEW_COLOR, "diffviewer", "changednew" },
 144     { DIFFVIEWER_CHANGED_COLOR, "diffviewer", "changed" },
 145     { DIFFVIEWER_REMOVED_COLOR, "diffviewer", "removed" },
 146     { DIFFVIEWER_ERROR_COLOR, "diffviewer", "error" },
 147 };
 148 
 149 /* --------------------------------------------------------------------------------------------- */
 150 /*** file scope functions ************************************************************************/
 151 /* --------------------------------------------------------------------------------------------- */
 152 
 153 static tty_color_pair_t *
 154 mc_skin_color_get_from_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
     /* [previous][next][first][last][top][bottom][index][help]  */
 155 {
 156     gchar kname[BUF_TINY];
 157     tty_color_pair_t *mc_skin_color;
 158 
 159     if (group == NULL || key == NULL)
 160         return NULL;
 161 
 162     if (mc_skin == NULL)
 163         mc_skin = &mc_skin__default;
 164 
 165     g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
 166     mc_skin_color = (tty_color_pair_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) kname);
 167 
 168     return mc_skin_color;
 169 }
 170 
 171 /* --------------------------------------------------------------------------------------------- */
 172 
 173 #if 0
 174 static void
 175 mc_skin_color_remove_from_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
     /* [previous][next][first][last][top][bottom][index][help]  */
 176 {
 177     gchar kname[BUF_TINY];
 178     if (group == NULL || key == NULL)
 179         return;
 180 
 181     if (mc_skin == NULL)
 182         mc_skin = &mc_skin__default;
 183 
 184     g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
 185     g_hash_table_remove (mc_skin->colors, (gpointer) kname);
 186 }
 187 #endif
 188 
 189 /* --------------------------------------------------------------------------------------------- */
 190 
 191 static void
 192 mc_skin_color_add_to_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key,
     /* [previous][next][first][last][top][bottom][index][help]  */
 193                            tty_color_pair_t *mc_skin_color)
 194 {
 195     gchar *kname;
 196 
 197     kname = g_strdup_printf ("%s.%s", group, key);
 198     if (kname != NULL)
 199     {
 200         if (g_hash_table_lookup (mc_skin->colors, (gpointer) kname) != NULL)
 201             g_hash_table_remove (mc_skin->colors, (gpointer) kname);
 202 
 203         g_hash_table_insert (mc_skin->colors, (gpointer) kname, (gpointer) mc_skin_color);
 204     }
 205 }
 206 
 207 /* --------------------------------------------------------------------------------------------- */
 208 
 209 static tty_color_pair_t *
 210 mc_skin_color_get_with_defaults (const gchar *group, const gchar *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 211 {
 212     tty_color_pair_t *mc_skin_color;
 213 
 214     mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
 215     if (mc_skin_color != NULL)
 216         return mc_skin_color;
 217 
 218     mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
 219     if (mc_skin_color != NULL)
 220         return mc_skin_color;
 221 
 222     mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
 223     return mc_skin_color;
 224 }
 225 
 226 /* --------------------------------------------------------------------------------------------- */
 227 
 228 /* If an alias is found, alloc a new string for the resolved value and free the input parameter.
 229    Otherwise it's a no-op returning the original string. */
 230 static gchar *
 231 mc_skin_color_look_up_alias (mc_skin_t *mc_skin, gchar *str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 232 {
 233     gchar *orig, *str2;
 234     int hop = 0;
 235 
 236     orig = g_strdup (str);
 237     str2 = g_strdup (str);
 238 
 239     while (TRUE)
 240     {
 241         gchar **values;
 242         gsize items_count;
 243 
 244         values = mc_config_get_string_list (mc_skin->config, "aliases", str, &items_count);
 245         if (items_count != 1)
 246         {
 247             // No such alias declaration found, that is, we've got the resolved value.
 248             g_strfreev (values);
 249             g_free (str2);
 250             g_free (orig);
 251             return str;
 252         }
 253 
 254         g_free (str);
 255         str = g_strdup (values[0]);
 256         g_strfreev (values);
 257 
 258         // str2 resolves at half speed than str. This is used for loop detection.
 259         if (hop++ % 2 != 0)
 260         {
 261             values = mc_config_get_string_list (mc_skin->config, "aliases", str2, &items_count);
 262             g_assert (items_count == 1);
 263             g_free (str2);
 264             str2 = g_strdup (values[0]);
 265             g_strfreev (values);
 266 
 267             if (strcmp (str, str2) == 0)
 268             {
 269                 // Loop detected.
 270                 fprintf (stderr,
 271                          "Loop detected while trying to resolve alias \"%s\" in skin \"%s\"\n",
 272                          orig, mc_skin->name);
 273                 g_free (str);
 274                 g_free (str2);
 275                 return orig;
 276             }
 277         }
 278     }
 279 }
 280 
 281 /* --------------------------------------------------------------------------------------------- */
 282 
 283 static tty_color_pair_t *
 284 mc_skin_color_get_from_ini_file (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
     /* [previous][next][first][last][top][bottom][index][help]  */
 285 {
 286     gsize items_count;
 287     gchar **values;
 288     tty_color_pair_t *mc_skin_color, *tmp;
 289 
 290     values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
 291     if (values == NULL || values[0] == NULL)
 292     {
 293         g_strfreev (values);
 294         return NULL;
 295     }
 296 
 297     mc_skin_color = g_try_new0 (tty_color_pair_t, 1);
 298     if (mc_skin_color == NULL)
 299     {
 300         g_strfreev (values);
 301         return NULL;
 302     }
 303 
 304     tmp = mc_skin_color_get_with_defaults (group, "_default_");
 305     mc_skin_color->fg = (items_count > 0 && values[0][0])
 306         ? mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[0])))
 307         : (tmp != NULL) ? g_strdup (tmp->fg)
 308                         : NULL;
 309     mc_skin_color->bg = (items_count > 1 && values[1][0])
 310         ? mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[1])))
 311         : (tmp != NULL) ? g_strdup (tmp->bg)
 312                         : NULL;
 313     mc_skin_color->attrs = (items_count > 2 && values[2][0])
 314         ? mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[2])))
 315         : (tmp != NULL) ? g_strdup (tmp->attrs)
 316                         : NULL;
 317 
 318     g_strfreev (values);
 319 
 320     mc_skin_color->pair_index = tty_try_alloc_color_pair (mc_skin_color, FALSE);
 321 
 322     return mc_skin_color;
 323 }
 324 
 325 /* --------------------------------------------------------------------------------------------- */
 326 
 327 static void
 328 mc_skin_color_set_default_for_terminal (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 329 {
 330     tty_color_pair_t *mc_skin_color;
 331 
 332     mc_skin_color = g_try_new0 (tty_color_pair_t, 1);
 333     if (mc_skin_color != NULL)
 334     {
 335         mc_skin_color->fg = g_strdup ("default");
 336         mc_skin_color->bg = g_strdup ("default");
 337         mc_skin_color->attrs = NULL;
 338         mc_skin_color->pair_index = tty_try_alloc_color_pair (mc_skin_color, FALSE);
 339     }
 340 }
 341 
 342 /* --------------------------------------------------------------------------------------------- */
 343 
 344 static void
 345 mc_skin_color_cache_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 346 {
 347     for (size_t i = 0; i < G_N_ELEMENTS (color_keywords); i++)
 348         tty_color_role_to_pair[color_keywords[i].role - TTY_COLOR_MAP_OFFSET] =
 349             mc_skin_color_get (color_keywords[i].group, color_keywords[i].key);
 350 }
 351 
 352 /* --------------------------------------------------------------------------------------------- */
 353 
 354 static gboolean
 355 mc_skin_color_check_inisection (const gchar *group)
     /* [previous][next][first][last][top][bottom][index][help]  */
 356 {
 357     return !((strcasecmp ("skin", group) == 0) || (strcasecmp ("aliases", group) == 0)
 358              || (strcasecmp ("lines", group) == 0) || (strncasecmp ("widget-", group, 7) == 0));
 359 }
 360 
 361 /* --------------------------------------------------------------------------------------------- */
 362 
 363 static void
 364 mc_skin_color_check_bw_mode (mc_skin_t *mc_skin)
     /* [previous][next][first][last][top][bottom][index][help]  */
 365 {
 366     gchar **groups, **orig_groups;
 367 
 368     if (tty_use_colors () && !mc_global.tty.disable_colors)
 369         return;
 370 
 371     orig_groups = mc_config_get_groups (mc_skin->config, NULL);
 372 
 373     for (groups = orig_groups; *groups != NULL; groups++)
 374         if (mc_skin_color_check_inisection (*groups))
 375             mc_config_del_group (mc_skin->config, *groups);
 376 
 377     g_strfreev (orig_groups);
 378 
 379     mc_skin_hardcoded_blackwhite_colors (mc_skin);
 380 }
 381 
 382 /* --------------------------------------------------------------------------------------------- */
 383 /*** public functions ****************************************************************************/
 384 /* --------------------------------------------------------------------------------------------- */
 385 
 386 gboolean
 387 mc_skin_color_parse_ini_file (mc_skin_t *mc_skin)
     /* [previous][next][first][last][top][bottom][index][help]  */
 388 {
 389     gboolean ret = FALSE;
 390     gsize items_count;
 391     gchar **groups, **orig_groups;
 392     tty_color_pair_t *mc_skin_color;
 393 
 394     mc_skin_color_check_bw_mode (mc_skin);
 395 
 396     orig_groups = mc_config_get_groups (mc_skin->config, &items_count);
 397     if (*orig_groups == NULL)
 398         goto ret;
 399 
 400     // as first, need to set up default colors
 401     mc_skin_color_set_default_for_terminal ();
 402     mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
 403     if (mc_skin_color == NULL)
 404         goto ret;
 405 
 406     mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
 407 
 408     for (groups = orig_groups; *groups != NULL; groups++)
 409     {
 410         gchar **keys, **orig_keys;
 411 
 412         if (!mc_skin_color_check_inisection (*groups))
 413             continue;
 414 
 415         orig_keys = mc_config_get_keys (mc_skin->config, *groups, NULL);
 416 
 417         for (keys = orig_keys; *keys != NULL; keys++)
 418         {
 419             mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
 420             if (mc_skin_color != NULL)
 421                 mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
 422         }
 423         g_strfreev (orig_keys);
 424     }
 425 
 426     mc_skin_color_cache_init ();
 427 
 428     ret = TRUE;
 429 
 430 ret:
 431     g_strfreev (orig_groups);
 432     return ret;
 433 }
 434 
 435 /* --------------------------------------------------------------------------------------------- */
 436 
 437 int
 438 mc_skin_color_get (const gchar *group, const gchar *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 439 {
 440     tty_color_pair_t *mc_skin_color;
 441 
 442     mc_skin_color = mc_skin_color_get_with_defaults (group, name);
 443 
 444     return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
 445 }
 446 
 447 /* --------------------------------------------------------------------------------------------- */

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