Manual pages: mcmcdiffmceditmcview

root/lib/tty/color.c

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

DEFINITIONS

This source file includes following definitions.
  1. mc_color__deinit
  2. tty_color_free_temp_cb
  3. tty_color_get_next_cpn_cb
  4. tty_color_get_next__color_pair_number
  5. tty_init_colors
  6. tty_colors_done
  7. tty_use_colors
  8. tty_try_alloc_color_pair
  9. tty_color_free_temp
  10. tty_color_free_all
  11. tty_color_set_defaults

   1 /*
   2    Color setup.
   3    Interface functions.
   4 
   5    Copyright (C) 1994-2025
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Andrew Borodin <aborodin@vmail.ru>, 2009
  10    Slava Zanko <slavazanko@gmail.com>, 2009
  11    Egmont Koblinger <egmont@gmail.com>, 2010
  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 /** \file color.c
  30  *  \brief Source: color setup
  31  */
  32 
  33 #include <config.h>
  34 
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 #include <string.h>
  38 #include <sys/types.h>  // size_t
  39 
  40 #include "lib/global.h"
  41 
  42 #include "tty.h"
  43 #include "color.h"
  44 
  45 #include "color-internal.h"
  46 
  47 /*** global variables ****************************************************************************/
  48 
  49 static tty_color_pair_t tty_color_defaults = {
  50     .fg = NULL, .bg = NULL, .attrs = NULL, .pair_index = 0
  51 };
  52 
  53 /* Set if we are actually using colors */
  54 gboolean use_colors = FALSE;
  55 
  56 gboolean need_convert_256color = FALSE;
  57 
  58 /*** file scope macro definitions ****************************************************************/
  59 
  60 /*** file scope type declarations ****************************************************************/
  61 
  62 /*** forward declarations (file scope functions) *************************************************/
  63 
  64 /*** file scope variables ************************************************************************/
  65 
  66 static GHashTable *mc_tty_color__hashtable = NULL;
  67 
  68 /* --------------------------------------------------------------------------------------------- */
  69 /*** file scope functions ************************************************************************/
  70 /* --------------------------------------------------------------------------------------------- */
  71 
  72 static void
  73 mc_color__deinit (tty_color_pair_t *color)
     /* [previous][next][first][last][top][bottom][index][help]  */
  74 {
  75     g_free (color->fg);
  76     g_free (color->bg);
  77     g_free (color->attrs);
  78 }
  79 
  80 /* --------------------------------------------------------------------------------------------- */
  81 
  82 static gboolean
  83 tty_color_free_temp_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  84 {
  85     (void) key;
  86     (void) user_data;
  87 
  88     return ((tty_color_lib_pair_t *) value)->is_temp;
  89 }
  90 
  91 /* --------------------------------------------------------------------------------------------- */
  92 
  93 static gboolean
  94 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  95 {
  96     tty_color_lib_pair_t *mc_color_pair = (tty_color_lib_pair_t *) value;
  97     size_t cp = GPOINTER_TO_SIZE (user_data);
  98 
  99     (void) key;
 100 
 101     return (cp == mc_color_pair->pair_index);
 102 }
 103 
 104 /* --------------------------------------------------------------------------------------------- */
 105 
 106 static size_t
 107 tty_color_get_next__color_pair_number (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 108 {
 109     size_t cp_count, cp;
 110 
 111     cp_count = g_hash_table_size (mc_tty_color__hashtable);
 112     for (cp = 0; cp < cp_count; cp++)
 113         if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
 114                                GSIZE_TO_POINTER (cp))
 115             == NULL)
 116             break;
 117 
 118     return cp;
 119 }
 120 
 121 /* --------------------------------------------------------------------------------------------- */
 122 /*** public functions ****************************************************************************/
 123 /* --------------------------------------------------------------------------------------------- */
 124 
 125 void
 126 tty_init_colors (gboolean disable, gboolean force)
     /* [previous][next][first][last][top][bottom][index][help]  */
 127 {
 128     tty_color_init_lib (disable, force);
 129     mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 130 }
 131 
 132 /* --------------------------------------------------------------------------------------------- */
 133 
 134 void
 135 tty_colors_done (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 136 {
 137     tty_color_deinit_lib ();
 138     mc_color__deinit (&tty_color_defaults);
 139     g_hash_table_destroy (mc_tty_color__hashtable);
 140 }
 141 
 142 /* --------------------------------------------------------------------------------------------- */
 143 
 144 gboolean
 145 tty_use_colors (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 146 {
 147     return use_colors;
 148 }
 149 
 150 /* --------------------------------------------------------------------------------------------- */
 151 
 152 int
 153 tty_try_alloc_color_pair (const tty_color_pair_t *color, gboolean is_temp)
     /* [previous][next][first][last][top][bottom][index][help]  */
 154 {
 155     gboolean is_base;
 156     gchar *color_pair;
 157     tty_color_lib_pair_t *mc_color_pair;
 158     int ifg, ibg, attr;
 159 
 160     is_base = (color->fg == NULL || strcmp (color->fg, "base") == 0);
 161     ifg = tty_color_get_index_by_name (is_base ? tty_color_defaults.fg : color->fg);
 162     is_base = (color->bg == NULL || strcmp (color->bg, "base") == 0);
 163     ibg = tty_color_get_index_by_name (is_base ? tty_color_defaults.bg : color->bg);
 164     is_base = (color->attrs == NULL || strcmp (color->attrs, "base") == 0);
 165     attr = tty_attr_get_bits (is_base ? tty_color_defaults.attrs : color->attrs);
 166 
 167     color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
 168     if (color_pair == NULL)
 169         return 0;
 170 
 171     mc_color_pair = (tty_color_lib_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable,
 172                                                                   (gpointer) color_pair);
 173 
 174     if (mc_color_pair != NULL)
 175     {
 176         g_free (color_pair);
 177         return mc_color_pair->pair_index;
 178     }
 179 
 180     if (need_convert_256color)
 181     {
 182         if ((ifg & FLAG_TRUECOLOR) == 0)
 183             ifg = convert_256color_to_truecolor (ifg);
 184 
 185         if ((ibg & FLAG_TRUECOLOR) == 0)
 186             ibg = convert_256color_to_truecolor (ibg);
 187     }
 188 
 189     mc_color_pair = g_try_new0 (tty_color_lib_pair_t, 1);
 190     if (mc_color_pair == NULL)
 191     {
 192         g_free (color_pair);
 193         return 0;
 194     }
 195 
 196     mc_color_pair->is_temp = is_temp;
 197     mc_color_pair->fg = ifg;
 198     mc_color_pair->bg = ibg;
 199     mc_color_pair->attr = attr;
 200     mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
 201 
 202     tty_color_try_alloc_lib_pair (mc_color_pair);
 203 
 204     g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
 205 
 206     return mc_color_pair->pair_index;
 207 }
 208 
 209 /* --------------------------------------------------------------------------------------------- */
 210 
 211 void
 212 tty_color_free_temp (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 213 {
 214     g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_temp_cb, NULL);
 215 }
 216 
 217 /* --------------------------------------------------------------------------------------------- */
 218 
 219 void
 220 tty_color_free_all (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 221 {
 222     g_hash_table_remove_all (mc_tty_color__hashtable);
 223 }
 224 
 225 /* --------------------------------------------------------------------------------------------- */
 226 
 227 void
 228 tty_color_set_defaults (const tty_color_pair_t *color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 229 {
 230     mc_color__deinit (&tty_color_defaults);
 231 
 232     tty_color_defaults.fg = g_strdup (color->fg);
 233     tty_color_defaults.bg = g_strdup (color->bg);
 234     tty_color_defaults.attrs = g_strdup (color->attrs);
 235     tty_color_defaults.pair_index = 0;
 236 }
 237 
 238 /* --------------------------------------------------------------------------------------------- */

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